makerdiary / python-keyboard

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

RGB Light Patterns #9

Open mehalter opened 4 years ago

mehalter commented 4 years ago

I have been playing around with the Backlight API and I have had success turning on all the colors and making some simple patterns. Although I haven't figured out how to get the patterns running in the background. Is there any way to do this currently? To have some small code that generates a pattern to change the lights while the keyboard stays functional. Or even a way to program lighting as a reaction to pressing a key?

xiongyihui commented 4 years ago

We can add a blacklight update function after matrix.wait(), and add a timeout to matrix.wait

matrix.wait(10)
# change backlight
backlight.update()

Another way is moving backlight into circuitpython firmware. Use timer's callback to update the backlight. It will be better, but will take more time to implement.

mehalter commented 4 years ago

I have been doing some initial playing around with this. Initially just playing around with adding the lines

self.backlight.pixel(key, 0xFF, 0x00, 0x00)
self.backlight.update()

at this line https://github.com/makerdiary/python-keyboard/blob/8d184d907b8be2306a0c858cf7363393ac945a9a/keyboard/__init__.py#L430

I'm having some weird behavior depending on where I put something like self.backlight.update(). If I put it right after the wait like you said, I get some nasty errors that I don't quite understand yet and cause the keyboard not to run. Do you have any idea why putting the update function or pixel setting in different locations in that function causes it to error out?

xiongyihui commented 4 years ago

The 7th bit of key is 1 when the key is released. Should change self.backlight.pixel(key, 0xFF, 0x00, 0x00) to self.backlight.pixel(key & 0x7F, 0xFF, 0x00, 0x00)

mehalter commented 4 years ago

Do you mean what was happening in this line? Or should I be doing it again?

https://github.com/makerdiary/python-keyboard/blob/8d184d907b8be2306a0c858cf7363393ac945a9a/keyboard/__init__.py#L425

xiongyihui commented 4 years ago

Oh, not need to do it again. I don't see any difference when putting self.backlight.update() in different locations.

I tried to replace the get function to:

    def get(self):
        key = self.matrix.get()
        if key & 0x80 == 0:
            self.heatmap[key] += 1
            self.backlight.pixel(key, 0, 0x80, 0)
        else:
            self.backlight.pixel(key & 0x7F, 0, 0, 0)
        self.backlight.update()
        return key

Do you mind to create a branch to share your code? So I can check it.

mehalter commented 4 years ago

It turned out to be another issue. I was able to fix it. I am currently using something similar to what you put except a check to see if it's the spacebar and in that case light up all 3 LEDs (56, 61, 62) and it's working great and the latency while it went up isn't noticeable during use (7 nanoseconds)

mehalter commented 4 years ago

I just opened up a PR with the current state of this development (#10)