-
Notifications
You must be signed in to change notification settings - Fork 488
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add documentation for the Potentiometer module included with KMK #638
base: master
Are you sure you want to change the base?
Conversation
added documentation for the potentiometer.py module with examples
updated formatting
First off: thank you so much for taking the time to add to the documentation. That's one of our weak points, and it's very appreciated. This is where I see a problem: If you're up for a dive and want to help me improve our handling of analog peripherals, that'd be awesome. |
The potentiometer module is a bit weird, but it does work at least for now for analog signals, even if it relies on deprecated code to make it run. |
That sounds great. Give me a couple of days to give this a proper look at. We may have to change the pot module a tiny bit to get to an MVP. |
Is this gonna get merged at some point? |
It seems the original author has abandoned this PR. |
I never got an update when you said you were gonna take a few days to look at it. Not sure if you made any changes to the pot module, if not it should probably be fine, but i would have to update my repo to double check. |
*Note: this uses `keyboard.add_key` and `keyboard.remove_key` which could be considered legacy.* | ||
|
||
```python | ||
def potentiometer_1_handler(state): | ||
joy1 = int((state.position / 127) * 127) | ||
if joy1 >= 0 and joy1 <= 56: | ||
keyboard.add_key(KC.S) | ||
if joy1 >= 65 and joy1 <= 127: | ||
keyboard.add_key(KC.W) | ||
if joy1 >= 57 and joy1 <= 64: | ||
keyboard.remove_key(KC.S) | ||
keyboard.remove_key(KC.W) | ||
|
||
def potentiometer_2_handler(state): | ||
joy2 = int((state.position / 127) * 127) | ||
if joy2 >= 0 and joy2 <= 58: | ||
keyboard.add_key(KC.A) | ||
if joy2 >= 67 and joy2 <= 127: | ||
keyboard.add_key(KC.D) | ||
if joy2 >= 59 and joy2 <= 66: | ||
keyboard.remove_key(KC.A) | ||
keyboard.remove_key(KC.D) | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can't advise to do any of that.
For one, keyboard
may not be defined where a user defines these handlers. Second, the note to deprecation is nice, but add_key
and remove_key
don't handle all keys correctly and should really be avoided in any code going forward.
Suggestion: we get rid of the awkward (pin, callback, *)-tuple interface and replace it with Potentiometer
instances proper, and add a user-definable on_move
method:
class PotAB(Potentiometer):
def on_move(self, keyboard, module, position, direction):
if ...:
keyboard.resume_process_key(module, key=KC.A, is_pressed=True)
elif ...:
keyboard.resume_process_key(module, key=KC.B, is_pressed=False)
# ... etc.
potentiometer = PotentiometerHandler(
potentiometer = (PotAB(board.A1, inverted=False),)
)
Also, your example would trigger for every tiny movement, again and again, regardless if it passes thresholds between "key presses/releases". That might not be noticable with the example keys, but that is not true for every key and likely not the intended behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i am going to rewrite this part and do some testing with my joystick board, but i believe that this should be pretty easy to accomplish. The initial example is just whatever i could come up with on the fly that would work for my use case, but as the documentation was a bit lacking and my understanding of python and circuitpython is still developing i just offered up whatever i figured out that would work.
i am very happy to learn the proper ways to do things and will work on a revision with better examples as my knowledge expands.
I hope it is not bothering you too much to help with these things as i originally started learning python specifically to develop for circuitpython, and there is some quirks, so having someone knowledgable to help my understanding is great.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's alright. I'd say the documentation should only contain information about the usage and one or two very basic examples. It's not the right place to have a wall of code.
for i in range(int(level_diff / level_inc_step)): | ||
hid_report = keyboard._hid_helper.create_report([cmd]) | ||
hid_report.send() | ||
hid_report.clear_all() | ||
hid_report.send() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm sorry, but that is not going to be an example in the official documentation.
There's so much wrong with this:
- The
hid_report
should never ever be interfaced with user code. - And that's not even a good way to use it. You're throwing away any keys that are currently pressed.
- This does not take into account that there's no feedback about the actual volume.
This is garantueed to generate unecessary bug reports and user frustration.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i am not exactly sure why the hid_report was in there as this is a copied example from ZFR_KBD's RP2.65-F keymap that is included with the KMK repo. I wil have to look into how circuitpython can interface with the os if not by utilizing the hid_report. I am not that well versed in exactly this field so if you or someone else has a better example for this that would be great.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we should put any kind of jank volume control examples into the "official" documentation, purely because of the third bullet point. The same "level" of volume on your keyboard will always produce different levels of volume on your computer. Terrible user experience.
return rgb | ||
|
||
def set_led_brightness(state): | ||
rgb = get_kb_rgb_obj(keyboard) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The recommended pattern is to first assign the RGB module to a global reference, then use that reference, not look it up by type.
Thank you github... There was a period when github would register my reviewes and comments, but not post them. |
Just github doing github things i guess, it is all good. |
Not sure if this helps but as far as implementation and interaction with it. I got it to just run my custom extension to sent the values as midi commands. I'm not sure if this a "Proper" way to use this or how to interact with modules in kmk in general but it works mind you as far as I know there are very few keyboard commands that can take a int in any form (hence using midi) https://github.com/piman13/kmk_firmware/blob/main/docs/en/potentiometer.md pot2midi.py
|
It's been a year since I looked at the |
i agree, but i do believe we should be able to get some kind of "soon to be deprecated" documentation for it, if not just the very basics until a reimplementation is complete. just in case someone wants to experiment with a jerry rigged solution to get their keyboard to work until the new implementation is complete |
I disagree. A feature that is so broken that it requires bad code to get it working should be removed, not documented. |
btw, there's #1054 if you want to check it out. |
This is fair enough. The feature might be broken but it has the basics working. i was just talking from the perspective of a user that wanted a feature to work and managed to get it working. i do not disagree that it needs a rewrite and i am more than happy to help out with such a task, although i do not know how much help i would be because of my somewhat limited knowledge of the complete project codebase and lacking knowledge of advanced usage ov circuitpython. |
You actually can help: By testing and giving feedback on the new analog input module draft PR I linked above. |
KMK includes a potentiometer module, potentiometer.py, but this does not have any documentation. i have written up some basic documentation for this module after some digging and reverse engineering, to help people who are not as persistent as me.