Skip to content
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

Implement generalized analog input module #1054

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft

Conversation

xs5871
Copy link
Collaborator

@xs5871 xs5871 commented Dec 10, 2024

Draft of a better analog input module as mentioned in #638.

@piman13
Copy link

piman13 commented Dec 14, 2024

so AnalogIn is being imported twice in this exsample
I think its suppose to be from kmk.modules.analogin import AnalogInput, AnalogKey

import board
from analogio import AnalogIn
from kmk.modules.analogin import AnalogIn

Other note where is this suppose to be placed
or is this my lack of understanding of kmk

you import the stuff above in code.py
but KC is not avable in code.py
putting it in keymap.py would make sense so are the imports suppose to be there as well?

analog = AnalogIn(
    [
        AnalogInput(AnalogIn(board.A0)),
        AnalogInput(AnalogIn(board.A1)),
        AnalogInput(AnalogIn(board.A2)),
    ],
    [
        [AnalogKey(KC.X), AnalogKey(KC.Y), AnalogKey(KC.Z)],
        [KC.TRNS, KC.NO, AnalogKey(KC.W, threshold=96)],
    ],
)

keyboard.modules.append(analog)

@piman13
Copy link

piman13 commented Dec 14, 2024

oh and is the analog = AnalogIn suppose to be AnalogInputs as well?

analog = AnalogIn(
    [
        AnalogInput(AnalogIn(board.A0)),
        AnalogInput(AnalogIn(board.A1)),
        AnalogInput(AnalogIn(board.A2)),
    ],
    [
        [AnalogKey(KC.X), AnalogKey(KC.Y), AnalogKey(KC.Z)],
        [KC.TRNS, KC.NO, AnalogKey(KC.W, threshold=96)],
    ],
)

edit "AnalogInputs" not AnalogInput

@piman13
Copy link

piman13 commented Dec 14, 2024

also a minimum change for on_change needs to be added either as a override-able value or just a 1 or 2% min-max (pots like to float a little and hopping between 1-10 units all the time keeps triggering it

@piman13
Copy link

piman13 commented Dec 15, 2024

question... I've added pulls to the analogin branch does this need redone or if you merge it it will the draft update?
other note. so much better to work with compared to potentiometer.py and I'm working on example code for calling midi to set the volume of stuff on the computer

Now is there any good way to pass a int to this so we don't have to copy pasta what is effectively the same code
Something like SDL(0)

SLD0 = AnalogEvent(
    on_change=lambda self, event, keyboard : KC.MIDI_CC(7, event.value, channel=0).on_press(keyboard),
)
SLD1 = AnalogEvent(
    on_change=lambda self, event, keyboard : KC.MIDI_CC(7, event.value, channel=1).on_press(keyboard),
)
SLD2 = AnalogEvent(
    on_change=lambda self, event, keyboard : KC.MIDI_CC(7, event.value, channel=2).on_press(keyboard),
)
SLD3 = AnalogEvent(
    on_change=lambda self, event, keyboard : KC.MIDI_CC(7, event.value, channel=3).on_press(keyboard),
)

@xs5871
Copy link
Collaborator Author

xs5871 commented Dec 16, 2024

@piman13

so AnalogIn is being imported twice in this exsample I think its suppose to be from kmk.modules.analogin import AnalogInput, AnalogKey

I had to change all the names midway through, because I didn't notice that CP already uses the name analogin.
Docs are fixed now

Other note where is this suppose to be placed or is this my lack of understanding of kmk

you import the stuff above in code.py but KC is not avable in code.py putting it in keymap.py would make sense so are the imports suppose to be there as well?

Yes, you have to import KC. The docs show examples, they don't spell out a complete configuration.

also a minimum change for on_change needs to be added either as a override-able value or just a 1 or 2% min-max (pots like to float a little and hopping between 1-10 units all the time keeps triggering it

  1. That's what the filter argument is for.
  2. The default filter already downscales to 8bit. Most MCUs have 12bit ADC, so that's a noise reduction factor of 16. In my experience that is enough in pretty much any reasonably designed circuit. If your ADC measurements still vary by 10 units, then that is definitely a hardware/design issue. Check your ADCs input impedance requirements.

Now is there any good way to pass a int to this so we don't have to copy pasta what is effectively the same code
Something like SDL(0)

Why use midi keys in the first place? Just use the midi API directly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants