From 73ca03e981bc679d79971447bc3ade008995456c Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Mon, 16 May 2022 17:01:20 +0200 Subject: [PATCH] move logic to python --- rotary-kb.py | 26 ++++++++++++++------------ src/main.cpp | 37 ++++++++++++++++--------------------- 2 files changed, 30 insertions(+), 33 deletions(-) diff --git a/rotary-kb.py b/rotary-kb.py index e13753b..0ae03f0 100755 --- a/rotary-kb.py +++ b/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) diff --git a/src/main.cpp b/src/main.cpp index f1bf87b..e919e36 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,10 +1,11 @@ #include #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;