PiSugar / pisugar-power-manager-rs

PiSugar Power Manger in rust language
https://www.pisugar.com/
GNU General Public License v3.0
130 stars 15 forks source link

Flash LEDs on Button Click ? #97

Open idowens opened 1 year ago

idowens commented 1 year ago

Is your feature request related to a problem? Please describe. I'm trying to script a .sh or .py script that will flash the PiSugar 2 Pro LED based on the current batter life (0-19% 1 flash, 20-39% 2 flash, ... , 79-100% OR charging=true is 5 flashes.

Shown below is the code I'm trying to execute on "single button click" but it's not getting me very far. I'm almost positive it's because it has no idea what to do with "flash_led" command--I'm just using it as a placeholder at the moment.

Best I can tell from reverse-engineering a little bit, I'm going to have to send signals or breaks to the GPIO that controls the LED? Any input would be appreciated.

#!/bin/bash

flash_led() {
    for ((i=0; i<$1; i++)); do
        sudo python3 /path/to/flash_led.sh
    done
}

check_battery_status() {
    battery_output=$(echo "get battery" | nc -U /tmp/pisugar-server.sock)
    charging_output=$(echo "get battery_charging" | nc -U /tmp/pisugar-server.sock)

    battery_percentage=$(echo "$battery_output" | tr -d '\r' | awk '{print $1}')
    is_charging=$(echo "$charging_output" | tr -d '\r' | awk '{print tolower($1)}')

    if [[ $battery_percentage -ge 0 && $battery_percentage -le 19 ]]; then
        flash_led 1
    elif [[ $battery_percentage -ge 21 && $battery_percentage -le 39 ]]; then
        flash_led 2
    elif [[ $battery_percentage -ge 40 && $battery_percentage -le 59 ]]; then
        flash_led 3
    elif [[ $battery_percentage -ge 60 && $battery_percentage -le 79 ]]; then
        flash_led 4
    elif [[ $battery_percentage -ge 80 || $is_charging == "true" ]]; then
        flash_led 5
    fi
}

check_battery_status