From abbcd9d60e989e54772fa7a3269a711b4005d64d Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Mon, 16 May 2022 16:44:44 +0200 Subject: [PATCH] init --- .gitignore | 5 +++ .vscode/extensions.json | 10 +++++ platformio.ini | 14 +++++++ rotary-kb.py | 38 +++++++++++++++++++ src/main.cpp | 81 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 148 insertions(+) create mode 100644 .gitignore create mode 100644 .vscode/extensions.json create mode 100644 platformio.ini create mode 100755 rotary-kb.py create mode 100644 src/main.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..89cc49c --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..080e70d --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,10 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ], + "unwantedRecommendations": [ + "ms-vscode.cpptools-extension-pack" + ] +} diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..ea23b77 --- /dev/null +++ b/platformio.ini @@ -0,0 +1,14 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:uno] +platform = atmelavr +board = uno +framework = arduino diff --git a/rotary-kb.py b/rotary-kb.py new file mode 100755 index 0000000..e13753b --- /dev/null +++ b/rotary-kb.py @@ -0,0 +1,38 @@ +#!/bin/env python3 +import serial +import time +from pynput import mouse, keyboard + +PORT = "/dev/ttyACM1" + +keys = list(" abcdefghijklmnopqrstuvwxyz") + [keyboard.Key.enter] +keys_display = " abcdefghijklmnopqrstuvwxyz\\" + +kb = keyboard.Controller() +arduino = serial.Serial(port=PORT, baudrate=9600, timeout=.1) + +active_btn = -1 + +pos = 0 + +def print_state(): + global pos + print(keys_display) + print(" " * pos + "^") + +while True: + event = arduino.readline().decode("utf-8").replace("\r\n", "") + 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() + diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..f1bf87b --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,81 @@ +#include + +#define PIN_BTN 5 + +#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 + +void setup() { + pinMode (PIN_A, INPUT); + pinMode (PIN_B, INPUT); + pinMode (PIN_BTN, INPUT_PULLUP); + + prev_a = digitalRead(PIN_A); + prev_btn = digitalRead(PIN_BTN); + Serial.begin(9600); +} + +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 + } + #ifdef DEBUG + Serial.print("pos: "); + #endif + Serial.println(rotation); + } + if (btn != prev_btn) { + Serial.print("btn."); + Serial.println(btn); + } + + prev_btn = btn; + prev_a = a; +} + +// void debug_signal() { +// int a = digitalRead(PIN_A); +// int b = digitalRead(PIN_B); +// if (a != prev_a || b != prev_b) { +// if (a) { +// if (b) { +// Serial.print("█"); +// } +// else { +// Serial.print("▀"); +// } +// } +// else { +// if (b) { +// Serial.print("▄"); +// } +// else { +// Serial.print(" "); +// } +// } +// } +// prev_a = a; +// prev_b = b; +// if (digitalRead(5) == LOW) { +// Serial.println(""); +// delay(100); +// } +// } +