-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.ino
279 lines (234 loc) · 5.84 KB
/
example.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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
* KeyBoh
* https://www.github.com/settorezero/keyboh
* (C)2020 Giovanni Bernardo (Cyb3rn0id), Roberto D'Amico (Bobboteck)
* https://www.settorezero.com
*
* Simple example:
* Thumbstick: will move the mouse pointer
* Click on Thumbstick button: left mouse click
* Encoder rotation: mouse scroll
* Click on Encoder button: right mouse click
* Button 20: switch between 2 mouse speeds
*/
#include "HID-Project.h" // HID-Project library by Nico-Hood
int row[]={A0,A1,A2,A3}; // matrix keypad - rows
int col[]={9,10,11,12,13}; // matrix keypad - columns
// encoder
#define ENC_BTN 6
#define ENC_A 7
#define ENC_B 8
// thumbstick
#define STICK_BTN 4
#define STICK_X A5
#define STICK_Y A4
uint8_t mouse_movement_delay=20; // ms, the analog thumbstick will be checked every this value ms givind mouse pointer some delay
void setup()
{
uint8_t i=0;
// rows setup
for (i=0; i<4; i++)
{
pinMode(row[i], INPUT_PULLUP);
}
// columns setup
for (i=0; i<5; i++)
{
pinMode(col[i], OUTPUT);
digitalWrite(col[i], HIGH);
}
pinMode(ENC_A, INPUT_PULLUP);
pinMode(ENC_B, INPUT_PULLUP);
pinMode(ENC_BTN, INPUT_PULLUP);
pinMode(STICK_BTN, INPUT_PULLUP);
// encoder interrupt
attachInterrupt(digitalPinToInterrupt(ENC_A), encoderTick, CHANGE); // Pulse interrupt
// HID peripherals initialization
Keyboard.begin();
Mouse.begin();
//Consumer.begin(); // used for multimedia buttons
//Gamepad.begin();
}
void loop()
{
// check pressed button on the 5x4 matrix
// buttons are numbered from 1 to 20 as on the PCB
// the number of the button pressed is (column+1)+(row*5)
static uint8_t pressed_button=0; // pressed button on matrix keypad from 1 to 20, 0=no button pressed
static long last_T; // used for giving some delay to mouse movement
// keyboad scan
for(uint8_t c=0; c<5; c++) // column scan
{
digitalWrite(col[c], LOW);
for(uint8_t r=0; r<4; r++) // row scan
{
if(digitalRead(row[r])==LOW)
{
delay(20); // antibounce
if(digitalRead(row[r])==LOW)
{
pressed_button=(c+1)+(r*5);
while(digitalRead(row[r])==LOW); // stay until button released
}
}
} // \row scan
digitalWrite(col[c], HIGH);
} // \column scan
// a button is pressed, recall the associated function
if (pressed_button>0)
{
do_keypad_stuff(pressed_button);
pressed_button=0;
}
// check encoder button
if (digitalRead(ENC_BTN)==LOW)
{
delay(20);
if (digitalRead(ENC_BTN)==LOW)
{
do_encoder_button_stuff();
while(digitalRead(ENC_BTN)==LOW);
}
}
// check thumbstick button
if (digitalRead(STICK_BTN)==LOW)
{
delay(20);
if (digitalRead(STICK_BTN)==LOW)
{
do_thumbstick_button_stuff();
while(digitalRead(STICK_BTN)==LOW);
}
}
// check analog thumbstick axis
int16_t x=analogRead(STICK_X);
int16_t y=analogRead(STICK_Y);
if (last_T>millis()) last_T=millis(); // millis() rollover
if (millis()-last_T>mouse_movement_delay)
{
do_thumbstick_analog_stuff(x,y);
last_T=millis();
}
} // \loop
// encoder interrupt
void encoderTick()
{
static long t=millis();
static int8_t m=0; // movement: 0=no movement, 1=clockwise, -1=counter-clockwise
static int8_t c=0; // the tick will be checked two consecutive times, this will count the consecutive times
if (t>millis()) t=millis(); // millis() rollover
if (millis()-t<30) return; // to few time passed, maybe is a bounce
c++; // a tick has occurred
if (digitalRead(ENC_A)==digitalRead(ENC_B))
{
if (c==1) m=1; // first tick with clockwise movement
if (c==2) // second tick
{
if (m==1) // the movement is still clockwise?
{
Mouse.move(0,0,1); // x,y, scroll up
}
c=0;
m=0;
}
}
else // movement is counter-clockwise
{
if (c==1) m=-1;
if (c==2)
{
if (m==-1)
{
Mouse.move(0,0,-1); // x,y, scroll down
}
c=0;
m=0;
}
}
t=millis(); // last time we checked
}
// do function associated to buttons on keypad
void do_keypad_stuff(uint8_t pb)
{
switch (pb)
{
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
break;
case 8:
break;
case 9:
break;
case 10:
break;
case 11:
break;
case 12:
break;
case 13:
break;
case 14:
break;
case 15:
break;
case 16:
break;
case 17:
break;
case 18:
break;
case 19:
break;
case 20:
// button 20 will switch between 2 mouse delays (fast/slow)
if (mouse_movement_delay)
{
mouse_movement_delay=0;
return;
}
else
{
mouse_movement_delay=20;
return;
}
break;
default:
return;
break;
}
return;
}
// click on encoder button
void do_encoder_button_stuff(void)
{
Mouse.click(MOUSE_RIGHT); // mouse right click
}
// click on thumbstick button
void do_thumbstick_button_stuff(void)
{
Mouse.click(MOUSE_LEFT); // mouse left click
}
// movement of thumbstick axis
void do_thumbstick_analog_stuff(uint16_t x, uint16_t y)
{
int8_t dx=0;
int8_t dy=0;
// we'll use the analog joystick as a digital joystick
// so I set a deadband and I'll check only the movement direction, not the movement amount
if (x>600) dx=-1; // move left
else if (x<400) dx=1; // move right
if (y>600) dy=-1; // move up
else if (y<400) dy=1; // move down
Mouse.move(dx,dy,0); //x, y, scroll => no scroll in this case
}