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
|
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 == "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:
|
||||||
if event.startswith("btn."):
|
print_state()
|
||||||
state = event[4]
|
print(event)
|
||||||
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()
|
|
||||||
|
|
||||||
|
|
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