-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemacspedal_uno_hid.ino
More file actions
105 lines (87 loc) · 2.79 KB
/
emacspedal_uno_hid.ino
File metadata and controls
105 lines (87 loc) · 2.79 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
95
96
97
98
99
100
101
102
103
104
105
//-----------------------------------------------------------------------------------------
// Key pedal decoder for the Arduino microcontroller
// http://www.arduino.cc/
//
// Send scancodes from Technics pedalboard (4 opening switches)
// through Arduino HID firmware.
//
// Quick HOWTO:
// 1. Compile and load this code onto the Arduino MC main processor
// 2. Flash usb-hid firmware onto the USB interface processor
// See http://mitchtech.net/arduino-usb-hid-keyboard/
// 3. Hook up the buttons to the selected input pins of your Arduino MC
// 4. Unplug and reconnect USB
//
//
// To reprogram the Arduino, reflash the standard usb-serial firmware first
//
//
//
// (C) Carl-Fredrik Enell 2012
// fredrik (at) kyla.kiruna.se
//
// Released under the GNU General Public License
// http://www.gnu.org/licenses/gpl.html
//
//-------------------------------------------------------------------------------------
//Serial speed expected by the usb-hid firmware
#define BAUD 9600
//Number of pedals
#define NBUTTONS 4
//Modifiers expected by HID firmware
const int ScanCodes[NBUTTONS]={1,2,64,4};//LEFTCTRL, LEFTSHIFT, RIGHTALT, LEFTALT
//Arduino digital inputs, change to your need
const int PedalPin[NBUTTONS]={6,7,12,13};
//Status register
int SwStat[NBUTTONS];
//Loop counter
int pincount;
//Debounce timer variables, see http://www.arduino.cc/en/Tutorial/Debounce
long lastchange[NBUTTONS]; //Last change
const long debounce=200; //Debounce delay (ms), change if necessary
//Initialisation
void setup() {
//Set up usbserial
Serial.begin(BAUD);
//Initialise inputs and their status registers
for(pincount=0; pincount<NBUTTONS; pincount++) {
pinMode(PedalPin[pincount],INPUT); //All pins to input mode
digitalWrite(PedalPin[pincount],HIGH); //Turn on internal pullup resistors
SwStat[pincount]=LOW; //Switches normally closed
lastchange[pincount]=0; //Debounce timer
}
}
//Main
void loop() {
int reading; //Current status
int dum;
if (pincount >= NBUTTONS) {
pincount=0; //Loop over NBUTTONS buttons
}
reading=digitalRead(PedalPin[pincount]); //Pin status
//Check whether switch status has certainly changed
if(reading != SwStat[pincount] && millis()-lastchange[pincount] > debounce) {
if(reading==HIGH) {
//Switch was pressed
SwStat[pincount]=HIGH;
hid_byte(ScanCodes[pincount]);
}
else {
//Switch was released
SwStat[pincount]=LOW;
hid_byte(0);
}
dum=Serial.read(); // read LED status and discard
lastchange[pincount]=millis(); //Time of status change
}
pincount++;
}
void hid_byte (uint8_t sc) {
const uint8_t nc=0;
int i;
//send 8 bytes of data: modifier,0,0,0,0,0,0,0
Serial.write(sc);
for(i=2; i<=8; i++) {
Serial.write(nc,1);
}
}