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