makerdiary / python-keyboard

A hand-wired USB & Bluetooth keyboard powered by Python and more
https://makerdiary.com/m60
MIT License
483 stars 60 forks source link

Feature Request: Charging Indicators #18

Open mehalter opened 3 years ago

mehalter commented 3 years ago

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.

BrianPugh commented 3 years ago

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 commented 3 years ago

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.