-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemacspedal_leonardo.ino
More file actions
94 lines (74 loc) · 2.84 KB
/
emacspedal_leonardo.ino
File metadata and controls
94 lines (74 loc) · 2.84 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// CFE's pedal keyboard version 3.0
//
// For Atmel 32u4 based controllers such as Arduino Leonardo and compatible,
// with USB interface and USB HID functions integrated in the PIC
// (Does not work with other Arduinos which have a USB-serial converter)
//
// Carl-Fredrik Enell 20190310
// fredrik@kyla.kiruna.se
#include <Keyboard.h>
/* Configuration *****************************************/
/* Settings: Change for your pedal setup */
static const int noPedals = 4;
/* Key input, prefer the 4 digital IOs without PWM */
static const uint8_t pedalPin[] = {2, 4, 7, 8} ;
static const uint8_t modePin = 12; /* Mode switch input */
/* Key mappings: Change to your preferences */
/* modePin closed: Pageflip function up, home, end, down */
static const char keycode0[] = {KEY_PAGE_UP, KEY_HOME, KEY_END, KEY_PAGE_DOWN};
/* modePin open: Emacs function Ctrl, Shift, AltGr, Alt */
static const char keycode1[] = {KEY_LEFT_CTRL, KEY_LEFT_SHIFT, KEY_RIGHT_ALT, KEY_LEFT_ALT};
/* Status array. Initialise to pedals up = inputs grounded */
/* Swap these everywhere if your pedals have Normally Open (NO) switches */
/* NB make sure not to swap modePin statuses so avoid search and replace... */
static uint8_t pedalState[] = {LOW, LOW, LOW, LOW};
/************** End of user configuration ********************/
uint8_t modeRead;
uint8_t pedalRead;
int n;
void setup() {
/* Initialise USB keyboard mode */
Keyboard.begin();
/* Set connectors to input and turn on pullup */
/* Pedals */
for (n = 0; n < noPedals; n++) {
pinMode(pedalPin[n], INPUT_PULLUP);
}
/* Mode switch input */
pinMode(modePin, INPUT_PULLUP);
}
void loop() {
/* Main loop: read pedals */
for(n = 0; n < noPedals; n++) {
modeRead = digitalRead(modePin);
pedalRead = digitalRead(pedalPin[n]);
/* State change detect */
if (pedalRead != pedalState[n]) {
pedalState[n] = pedalRead;
/* Swap HIGH and LOW for NO switches */
if (pedalRead == HIGH) {
/* Pedal pressed */
if (modeRead == LOW) {
/* Mode selector closed: Motion key: no repeat wanted on pedal -> press and release immediately*/
Keyboard.press(keycode0[n]);
delay(10);
Keyboard.releaseAll();
}
else {
/* Mode selector open: press modifier key */
Keyboard.press(keycode1[n]);
}
}
else {
/* Pedal released */
if (modeRead == HIGH) {
/* Mode selector open: release modifier key */
Keyboard.release(keycode1[n]);
}
/* Mode selector closed: no action needed here*/
}
} /* End pedal state check */
} /*End read pedals*/
/* Sleep 50 ms */
delay(50);
}