move logic to python
This commit is contained in:
parent
70277a1a5e
commit
73ca03e981
2 changed files with 30 additions and 33 deletions
26
rotary-kb.py
26
rotary-kb.py
|
@ -19,20 +19,22 @@ def print_state():
|
|||
global pos
|
||||
print(keys_display)
|
||||
print(" " * pos + "^")
|
||||
print(pos)
|
||||
|
||||
while True:
|
||||
event = arduino.readline().decode("utf-8").replace("\r\n", "")
|
||||
if event == "cw":
|
||||
pos = (pos + 1) % len(keys)
|
||||
elif event == "ccw":
|
||||
pos = (pos - 1 + len(keys)) % len(keys)
|
||||
elif event == "down":
|
||||
kb.press(keys[pos])
|
||||
active_btn = pos
|
||||
elif event == "up":
|
||||
kb.release(keys[active_btn])
|
||||
active_btn = -1
|
||||
|
||||
if event:
|
||||
if event.startswith("btn."):
|
||||
state = event[4]
|
||||
if state == "0":
|
||||
kb.press(keys[pos])
|
||||
active_btn = pos
|
||||
else:
|
||||
kb.release(keys[active_btn])
|
||||
active_btn = -1
|
||||
# print(event)
|
||||
else:
|
||||
pos = int(event)
|
||||
print_state()
|
||||
print_state()
|
||||
print(event)
|
||||
|
||||
|
|
37
src/main.cpp
37
src/main.cpp
|
@ -1,10 +1,11 @@
|
|||
#include <Arduino.h>
|
||||
|
||||
#define PIN_BTN 5
|
||||
// CLK / pin A of rotary encoder
|
||||
#define PIN_A 11
|
||||
// DT / pin B of rotary encoder
|
||||
#define PIN_B 8
|
||||
|
||||
#define PIN_A 11 // Connected to CLK
|
||||
#define PIN_B 8 // Connected to DT
|
||||
int rotation = 0;
|
||||
int prev_a;
|
||||
int prev_btn;
|
||||
//#define DEBUG
|
||||
|
@ -22,28 +23,22 @@ void setup() {
|
|||
void loop() {
|
||||
// debug_signal();
|
||||
int a = digitalRead(PIN_A);
|
||||
int btn = digitalRead(PIN_BTN);
|
||||
|
||||
if (a != prev_a){ // Means the knob is rotating
|
||||
if (digitalRead(PIN_B) != a) { // Means pin A Changed first - We're Rotating Clockwise
|
||||
rotation++;
|
||||
#ifdef DEBUG
|
||||
Serial.print("CW ");
|
||||
#endif
|
||||
} else {// Otherwise B changed first and we're moving CCW
|
||||
rotation--;
|
||||
#ifdef DEBUG
|
||||
Serial.print("CCW ");
|
||||
#endif
|
||||
if (digitalRead(PIN_B) != a) { // Means pin A changed first - rotating clockwise
|
||||
Serial.println("cw");
|
||||
} else {// Otherwise B changed first - counter clockwise
|
||||
Serial.println("ccw");
|
||||
}
|
||||
#ifdef DEBUG
|
||||
Serial.print("pos: ");
|
||||
#endif
|
||||
Serial.println(rotation);
|
||||
}
|
||||
|
||||
int btn = digitalRead(PIN_BTN);
|
||||
if (btn != prev_btn) {
|
||||
Serial.print("btn.");
|
||||
Serial.println(btn);
|
||||
if (btn) {
|
||||
Serial.println("up");
|
||||
}
|
||||
else {
|
||||
Serial.println("down");
|
||||
}
|
||||
}
|
||||
|
||||
prev_btn = btn;
|
||||
|
|
Loading…
Reference in a new issue