adafruit / Adafruit_CircuitPython_HT16K33

Adafruit CircuitPython driver for the HT16K33, a LED matrix driver IC.
MIT License
41 stars 29 forks source link

Add keypad function - initial commit #84

Closed LewsTherinTelamon closed 2 years ago

LewsTherinTelamon commented 3 years ago

Add keypad reading using new child class of HT16K33 that writes to the register of the HT16K33 and reads back 6 bytes into a byte array.

LewsTherinTelamon commented 3 years ago

Commit 702449b tested with 8 buttons on KS0, reading as expected. Will do more testing later.

LewsTherinTelamon commented 3 years ago

Potential issues with polling speed, investigating.

LewsTherinTelamon commented 3 years ago

May need to use the interrupt function of the chip to avoid polling issues. Investigating.

evaherrada commented 3 years ago

Hi @LewsTherinTelamon Do you think you might be able to add a quick example of this being implemented? I'll try to test this in a few days and that would be really helpful.

Also, what keypad are you using?

BTW, if you're looking to make the CI pass, you're probably gonna want to run black. We've got a great guide on doing that here https://learn.adafruit.com/improve-your-code-with-pylint/black

Thanks for the PR!

Edit: looks like that last commit caused some black issues and was from a different account. Was that desired, because if not, I'd probably recommend reverting it.

LewsTherinTelamon commented 3 years ago

@dherrada thanks for the black rec, I thought I ran it but must not have. I'll make sure to do that before my next commit.

At the moment I am not using an actual keypad but a 1x6 array of buttons on a breadboard attached to COM1 on the HT16K33, in order to match what my project will use. I will be testing the functionality of the code with more buttons however. That said, per the datasheet there are several ways to setup arrays of buttons and I don't have enough BB space to test each method. Attached is a screenshot of what it looks like in EAGLE. My BB is too messy to be useful to post. My understanding is you can get away without the resistors but I copied the datasheet exactly. ht16k33buttons

Here's an example that can be run in the REPL, the first 3 outputs are what you see if you press only the button connected between COM1/KS0 / ROW3/K1, then read the INT flag, and then read the buttons, then read the flag again. The second 3 outputs are what you see if you press only the button connected between COM1/KS0 / ROW8/K6, then read the INT flag, then read the buttons then read the INT flag again. Please note that this code uses a newer version of the code that has the return of read_int_flag() be a boolean value. That register returns either 0x00 or 0xff. I will commit this soon.

>>> import adafruit_ht16k33.segments as seg
>>> import board
>>> import time
>>> i2c = board.I2C()
>>> display = seg.Seg14x4(i2c)
>>>display.read_int_flag()
True
>>>display.read_buttons()
bytearray(b'\x01\x00\x00\x00\x00\x00')
>>>display.read_int_flag()
False
>>>display.read_int_flag()
True
>>>display.read_buttons()
bytearray(b'\x10\x00\x00\x00\x00\x00')
>>>display.read_int_flag()
False
>>>
evaherrada commented 3 years ago

@LewsTherinTelamon Thanks!

LewsTherinTelamon commented 3 years ago

@dherrada Latest commit has most up to date code. When I can free up some board space I will try a 2x6 array.

LewsTherinTelamon commented 3 years ago

Tested with 2x8 "keypad" of momentary switches, reads as expected.

evaherrada commented 3 years ago

@LewsTherinTelamon Great. I should be getting to it sometime in the next day or two. Sorry for the wait.

LewsTherinTelamon commented 3 years ago

@dherrada No worries, I have to tease out a bug in the hardware this code is meant for anyways. I think it's worth trying i2c.writeto() then i2c.readfrom_into() instead of i2c.writeto_then_readfrom() as well in case it resolves the bug. Seems unlikely but I'm going to try it this weekend.

jposada202020 commented 3 years ago

@LewsTherinTelamon hello :), still interested in working on this. Let me know thanks.

FoamyGuy commented 2 years ago

@evaherrada did you end up testing this out?

@LewsTherinTelamon I'm trying this out but running into some trouble. I'm using this test script derived from the coded you posted:

import adafruit_ht16k33.segments as seg
import board
import time
i2c = board.I2C()
display = seg.Seg14x4(i2c)

while True:
    print(display.read_int_flag())
    time.sleep(0.3)
    cur_value = display.read_buttons()
    print(cur_value)
    time.sleep(0.3)
    print(display.read_int_flag())
    time.sleep(0.3)

But I end up with Unsupported Operation error:

code.py output:
False
Traceback (most recent call last):
  File "code.py", line 10, in <module>
  File "/lib/adafruit_ht16k33/ht16k33.py", line 166, in read_buttons
OSError: [Errno 19] Unsupported operation

It seems that my second attempt to read anything using these new functions results in this error. I tried a few different things reading only the int flag a few times vs. int flag and then buttons and then int flag, but I always got this same error on the second "thing" I attempted to read, no matter which type of thing it was.

I think it would be great if we could include an example script in the examples directory for this functionality as well if possible.

LewsTherinTelamon commented 2 years ago

Hi everyone, work/personal life have gotten in the way of me working on this. @FoamyGuy I don't recall running into that error, that's odd. When I can find my hardware, and I have more time I'll dive back in. This was originally for a Covid related project that has been made less relevant by vaccines :)

Thanks for your continued attention to the branch, love the community.

FoamyGuy commented 2 years ago

I'm going to close this PR for now. @LewsTherinTelamon if you get back into this please feel free to re-open, and to ping me if you have any trouble.