python-keyboard icon indicating copy to clipboard operation
python-keyboard copied to clipboard

Feature Request: Charging Indicators

Open mehalter opened this issue 3 years ago • 2 comments

It would be nice to have some sort of battery status keybinding that lights up across the top row with how charged the keyboard is and indicate whether the keyboard is currently charging or discharging. Using the top row is just an idea, this could be implemented in a number of different ways.

mehalter avatar Sep 27 '20 19:09 mehalter

I have implemented something similar here:

https://github.com/BrianPugh/makerdiary-m60-config

The important bits are (might need some imports depending on how you organize your code):

from time import sleep

charger_in = digitalio.DigitalInOut(microcontroller.pin.P0_03)  # NOTE: low/False=charging, high/True=not_charging
charger_in.pull = digitalio.Pull.UP
def battery_charge():
    return not charger_in.value

# code.py
def macro_handler_batt(dev, is_down):  # invoke this in your macro_handler
    if is_down:
        is_charging = battery_charge()
        level = int(round(battery_level() / 7.14))
        dev.backlight.off()
        for i in range(level):
            if i == 0 and is_charging:
                dev.backlight.pixel(i, 255, 0, 0)
            else:
                dev.backlight.pixel(i, 0, 255, 0)

            if i != level - 1:
                sleep(0.03)

            dev.backlight.update()
    else:
        dev.backlight.set_mode(dev.backlight.mode)

So whenever you press this macro it does the following:

  1. Turns off the backlight upon macro press.
  2. Sets "esc" to red if charging, green otherwise.
  3. Animates a bar along the top of your keyboard in green with the current charge level.
  4. Restores the backlight upon macro release.

BrianPugh avatar Jan 01 '21 21:01 BrianPugh

I also just committed more changes, like having the macro handlers be aware if shift or ctrl has been also pushed. I have it so that if I have shift pressed while pressing my battery macro, it will type the exact battery percentage.

BrianPugh avatar Jan 02 '21 00:01 BrianPugh