Skip to content

Instantly share code, notes, and snippets.

@JeanRibes
Created August 4, 2021 20:32
Show Gist options
  • Save JeanRibes/6797b2773c1ee15bb010159929116a0f to your computer and use it in GitHub Desktop.
Save JeanRibes/6797b2773c1ee15bb010159929116a0f to your computer and use it in GitHub Desktop.
RGB mouse as a battery monitor

Install piper and psutil

#!/usr/bin/env python3
from psutil import sensors_battery
from piper.ratbagd import Ratbagd, RatbagdLed
from time import sleep
STATE_ALARM = 'alarme'
STATE_OK = 'ok'
STATE_CHARGING = 'charging'
STATE_UNPLUG = 'unplug'
color_ok = (115, 210, 22)
color_alarme = (255, 95, 0)
color_unplug = (255, 0, 0)
color_charge = (128, 255, 255)
timing_alarme = 500
timing_ok = 3000
timing_charge = 2000
def set_rgb(color, duration, brightness=200):
r = Ratbagd(api_version=1)
souris = r.devices[0]
profil = souris.profiles[0]
led = profil.leds[0]
led.mode = RatbagdLed.Mode.BREATHING
led.color = color
led.brightness = brightness
led.effect_duration = duration
souris.commit()
if __name__ == '__main__':
state = 'err'
while True:
bat = sensors_battery()
if bat.percent < 30:
new_state = STATE_ALARM
else:
new_state = STATE_OK
if bat.power_plugged:
if bat.percent >= 99:
new_state = STATE_UNPLUG
else:
new_state = STATE_CHARGING
if new_state != state:
print(new_state)
state = new_state
if state == STATE_OK:
set_rgb(color_ok, timing_ok)
elif state == STATE_ALARM:
set_rgb(color_alarme, timing_alarme)
elif state == STATE_CHARGING:
set_rgb(color_charge, timing_charge)
elif state == STATE_UNPLUG:
set_rgb(color_unplug, timing_alarme)
sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment