-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrotary_keyboard.ino
74 lines (66 loc) · 2.26 KB
/
rotary_keyboard.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <Keyboard.h>
#include <Mouse.h>
// GLOBALS
// Operation signal goes to Pin 8
int pin_op = 8;
// Pulse signal goes to Pin 9
int pin_pulse = 9;
// For counting the pulses
int pulses = 0;
// For keeping operation state
int last_op = LOW;
// For keeping pulse state
int last_pulse = HIGH;
// Array for characters to be emitted.
// This may seem weird. You could put "0" at position 0 and use % 10 when addressing this array.
// But I liked this one. Just moving the wheel a little but not pulling all the way to "1" allows
// you to enter a space (or whatever you put in the array at position 0).
String characters[] = {" ","1","2","3","4","5","6","7","8","9","0"};
// ONCE
void setup() {
// Runs once (initialization)
pinMode(pin_op, INPUT);
pinMode(pin_pulse, INPUT);
pulses = 0;
}
// CONTINUOUS
void loop() {
// Read pin values
int op = digitalRead(pin_op);
int pulse_status = digitalRead(pin_pulse);
// Logic
if (op == HIGH && last_op == LOW) {
// Op line on Pin 8 just changed from LOW to HIGH -> Dialing started
// Uncomment for debug: Keyboard.print("// Dialing started");
last_op = HIGH;
}
else if (op == HIGH && last_op == HIGH) {
// Op line (Pin 8) remains high. We just have to count the pulses (on
// Pin 9) here. https://en.wikipedia.org/wiki/Pulse_dialing
// Uncomment for (verbose) debug: Keyboard.print(pulse_status);
if (pulse_status == LOW and last_pulse == HIGH) {
// Pulse (Pin 9) goes from HIGH to LOW -> increment and keep the state
pulses += 1;
last_pulse = LOW;
} else if (pulse_status == HIGH and last_pulse == LOW) {
// Pulse (Pin 9) goes from LOW to HIGH -> just keep the state
last_pulse = HIGH;
}
}
else {
// We get here when op line (Pin 8) is LOW
if (last_op == HIGH) {
// Op line on Pin 8 just changed from HIGH to LOW -> Dialing finished.
// We need to emit the character from the array at the position of the
// pulses we counted.
Keyboard.print(characters[pulses]);
// Uncomment for debug: Keyboard.println(" Dialing ended");
// Save state
last_op = op;
// Reset pulse counter
pulses = 0;
}
}
// Let it breathe
delay(5);
}