This commit is contained in:
Crispy 2022-05-16 16:44:44 +02:00
commit abbcd9d60e
5 changed files with 148 additions and 0 deletions

5
.gitignore vendored Normal file
View file

@ -0,0 +1,5 @@
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch

10
.vscode/extensions.json vendored Normal file
View file

@ -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"
]
}

14
platformio.ini Normal file
View file

@ -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

38
rotary-kb.py Executable file
View file

@ -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()

81
src/main.cpp Normal file
View file

@ -0,0 +1,81 @@
#include <Arduino.h>
#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);
// }
// }