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

Feature analogin #1057

Open
wants to merge 3 commits into
base: feature-analogin
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions docs/en/analogin.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ from kmk.modules.analogin import AnalogInput
a = AnalogInput(
input: AnalogIn,
filter: Optional(Callable[AnalogIn, int]) = lambda input:input.value>>8,
sensitivity: 1,
)

a.value
Expand All @@ -55,6 +56,10 @@ An `AnalogIn` like object.
A customizable function that reads and transforms `input.value`.
The default transformation maps uint16 ([0-65535]) to uint8 ([0-255]) resolution.

#### `sensitivity`
Prevents noise from analog inputs from spamming events but will cause stepping
in input if set to larger values

#### `value`

Holds the transformed value of the `AnalogIn` input.
Expand Down Expand Up @@ -95,25 +100,26 @@ AK = AnalogKey(
## Examples

### Analogio with AnalogKeys
Emits a key press when analog input goes above the threshold

```python
import board
from analogio import AnalogIn
from kmk.modules.analogin import AnalogIn
from kmk.modules.analogin import AnalogInput, AnalogInputs, AnalogKey

analog = AnalogIn(
analogInputmap = AnalogInAnalogInputs(
[
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)],
[AnalogKey(KC.X), AnalogKey(KC.Y), AnalogKey(KC.Z)], ##layer 1
[KC.TRNS, KC.NO, AnalogKey(KC.W, threshold=96)], ##layer 2
],
)

keyboard.modules.append(analog)
keyboard.modules.append(analogInputmap)
```

### External DAC with AnalogEvent
Expand Down Expand Up @@ -149,7 +155,21 @@ HTT = AnalogEvent(

a0 = AnalogInput(dac, lambda _: int(_.value / 0xFFFF * 1980) + 20)

analog = AnalogIn(
analogInputmap = AnalogInputs(
[a0],
[[HTT]],
```


### Common Filters for different ranges

```python

#0-255
lambda input:input.value>>8) #default Filter
lambda input:(~input.value>>8)+256) #default Filter Inverted

#0-127
lambda input:input.value>>9)
lambda input:(~input.value>>9)+128) #Inverted
```
5 changes: 3 additions & 2 deletions kmk/modules/analogin.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ def on_stop(self, event, keyboard):


class AnalogInput:
def __init__(self, input, filter=lambda input: input.value >> 8):
def __init__(self, input, filter=lambda input: input.value >> 8, sensitivity=1):
self.input = input
self.value = 0
self.delta = 0
self.sensitivity = sensitivity
self.filter = filter

def update(self):
Expand All @@ -56,7 +57,7 @@ def update(self):
'''
value = self.filter(self.input)
self.delta = value - self.value
if self.delta != 0:
if self.delta not in range(-self.sensitivity, self.sensitivity):
self.value = value
return value

Expand Down
2 changes: 1 addition & 1 deletion kmk/modules/midi.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def on_powersave_disable(self, keyboard):

def send(self, message):
if self.midi:
self.midi.send(message)
self.midi.send(message,message.channel)

def on_press(self, key, keyboard, *args, **kwargs):
self.send(key.on_press_msg)
Expand Down