-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBexiPad.ino
291 lines (212 loc) · 7.14 KB
/
BexiPad.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
280
281
282
283
284
285
286
287
288
289
290
291
#include "Keyboard.h"
const int cDiv = 2; //dividor of clock, higher the value better the ADC accuracy, may require recalibration
const int key0 = 32; //code of key 1
const int key1 = 108; //code of key 2
float act0 = 0.5f; //activation fraction of key 1
float act1 = 0.5f; //activation fraction of key 2
bool RapidTrigger = true; //Rapid Trigger
const float RTrange = 0.1f; //Rapid trigger fraction
const int res = 8; //define resolution - 8/10/12 bits, will require recalibration
const int Aprox = 3; //define by how much an ADC read value has to change for calculation code to get executed
// Calibration Values
const int min0 = 20; //Minimum meassured value for key 1
const int min1 = 20; //Minimum meassured value for key 2
const int max0 = 200; //Maximum meassured value for key 1
const int max1 = 200; //Minimum meassured value for key 2
// Dont change - variables that just get used during the code runtime
volatile bool key0on = false;
volatile bool key1on = false;
volatile int RTlimit = 0;
const int gClk = 3;
volatile int Analog0 = 0;
volatile int Analog1 = 0;
volatile float a0 = 0.0f;
volatile float a1 = 0.0f;
volatile int PA0 = 0;
volatile int PA1 = 0;
volatile int resolution;
volatile int RTR0;
volatile int RTR1;
volatile int RTL0 = 0;
volatile int RTL1 = 0;
void setup() {
calc(); //Calculates values based on calibration and Resolution
genericClockSetup(gClk, cDiv); //Sets up clock speeds
ADCSetup(); //Configures ADC
Keyboard.begin(); //Start of USBHID communication
activateKey1(); //First cycle for Key1
readKey1();
}
void loop() {
activateKey0();
calcKey1();
readKey0();
activateKey1();
calcKey0();
readKey1();
}
void keyb0() {
if (key0on == false && Analog0 > act0) {
Keyboard.press(key0);
key0on = true;
} else if (key0on == true && Analog0 < act0) {
Keyboard.release(key0);
key0on = false;
}
}
void keyb1() {
if (key1on == false && Analog1 > act1) {
Keyboard.press(key1);
key1on = true;
} else if (key1on == true && Analog1 < act1) {
Keyboard.release(key1);
key1on = false;
}
}
void keyrt0() {
if (!key0on) {
if (Analog0 > act0 && Analog0 - RTL0 > RTR0) {
Keyboard.press(key0);
key0on = true;
};
if (RTL0 > Analog0) {
RTL0 = Analog0;
};
} else {
if (RTL0 - Analog0 > RTR0 || Analog0 < act0) {
Keyboard.release(key0);
key0on = false;
};
if (RTL0 < Analog0) {
RTL0 = Analog0;
};
};
}
void keyrt1() {
if (!key1on) {
if (Analog1 > act1 && Analog1 - RTL1 > RTR1) {
Keyboard.press(key1);
key1on = true;
};
if (RTL1 > Analog1) {
RTL1 = Analog1;
};
} else {
if (RTL1 - Analog1 > RTR1 || Analog1 < act1) {
Keyboard.release(key1);
key1on = false;
};
if (RTL1 < Analog1) {
RTL1 = Analog1;
};
};
}
void activateKey0() {
/* Setup from which pin to read from, with refference to what, and what GAIN to use */
ADC->INPUTCTRL.reg = ADC_INPUTCTRL_GAIN_DIV2 | ADC_INPUTCTRL_MUXNEG_GND | ADC_INPUTCTRL_MUXPOS_PIN0;
/* Start the ADC using a software trigger. */
ADC->SWTRIG.bit.START = true;
}
void calcKey0() {
/* Execute calculations if the values have changed, and based on if Rapid Trigger is enabled */
if (abs(PA0-Analog0)>Aprox) {
PA0 = Analog0;
Analog0 = int(a0 * (PA0 - min0));
if (RapidTrigger) {
keyrt0();
} else {
keyb0();
};
};
}
void readKey0() {
/* Wait for ADC values to be ready */
while (ADC->INTFLAG.bit.RESRDY == 0)
;
ADC->INTFLAG.reg = ADC_INTFLAG_RESRDY;
/* Write down ADC values */
Analog0 = ADC->RESULT.reg;
}
void activateKey1() {
/* Setup from which pin to read from, with refference to what, and what GAIN to use */
ADC->INPUTCTRL.reg = ADC_INPUTCTRL_GAIN_DIV2 | ADC_INPUTCTRL_MUXNEG_GND | ADC_INPUTCTRL_MUXPOS_PIN2;
ADC->SWTRIG.bit.START = true;
}
void calcKey1() {
/* Execute calculations if the values have changed, and based on if Rapid Trigger is enabled */
if (abs(PA1-Analog1)>Aprox) {
PA1 = Analog1;
Analog1 = int(a1 * (PA1 - min1));
if (RapidTrigger) {
keyrt1();
} else {
keyb1();
};
};
}
void readKey1() {
/* Wait for ADC values to be ready */
while (ADC->INTFLAG.bit.RESRDY == 0)
;
ADC->INTFLAG.reg = ADC_INTFLAG_RESRDY;
/* Write down ADC values */
Analog1 = ADC->RESULT.reg;
}
void calc() {
resolution = pow(2, res) - 1;
a0 = ((max0 - min0) / float(resolution));
a1 = ((max1 - min1) / float(resolution));
act0 = int(act0 * a0 * resolution);
act1 = int(act1 * a1 * resolution);
RTR0 = int(RTrange * a0 * resolution);
RTR1 = int(RTrange * a1 * resolution);
RTL0 = RTL0;
RTL1 = RTL1;
}
void genericClockSetup(int clk, int dFactor) {
// Enable the APBC clock for the ADC
REG_PM_APBCMASK |= PM_APBCMASK_ADC;
//This allows you to setup a div factor for the selected clock certain clocks allow certain division factors: Generic clock generators 3 - 8 8 division factor bits - DIV[7:0]
GCLK->GENDIV.reg |= GCLK_GENDIV_ID(clk) | GCLK_GENDIV_DIV(dFactor);
while (GCLK->STATUS.reg & GCLK_STATUS_SYNCBUSY)
;
//configure the generator of the generic clock with 48MHz clock
GCLK->GENCTRL.reg |= GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_DFLL48M | GCLK_GENCTRL_ID(clk);
while (GCLK->STATUS.reg & GCLK_STATUS_SYNCBUSY)
;
//enable clock, set gen clock number, and ID to where the clock goes (30 is ADC)
GCLK->CLKCTRL.reg |= GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN(clk) | GCLK_CLKCTRL_ID(30);
while (GCLK->STATUS.bit.SYNCBUSY)
;
}
void ADCSetup() {
/* Calibrate values. */
uint32_t bias = (*((uint32_t *)ADC_FUSES_BIASCAL_ADDR) & ADC_FUSES_BIASCAL_Msk) >> ADC_FUSES_BIASCAL_Pos;
uint32_t linearity = (*((uint32_t *)ADC_FUSES_LINEARITY_0_ADDR) & ADC_FUSES_LINEARITY_0_Msk) >> ADC_FUSES_LINEARITY_0_Pos;
linearity |= ((*((uint32_t *)ADC_FUSES_LINEARITY_1_ADDR) & ADC_FUSES_LINEARITY_1_Msk) >> ADC_FUSES_LINEARITY_1_Pos) << 5;
/* Wait for bus synchronization. */
while (ADC->STATUS.bit.SYNCBUSY) {};
/* Write the calibration data. */
ADC->CALIB.reg = ADC_CALIB_BIAS_CAL(bias) | ADC_CALIB_LINEARITY_CAL(linearity);
while (ADC->STATUS.bit.SYNCBUSY) {};
/* Use the internal VCC reference. This is 1/2 of what's on VCCA.
since VCCA is typically 3.3v, this is 1.65v.
*/
ADC->REFCTRL.reg = ADC_REFCTRL_REFSEL_INTVCC1;
/* Number of ADC samples to capture */
ADC->AVGCTRL.reg = ADC_AVGCTRL_SAMPLENUM_1;
/* Sets resolution and uses smallest possible divider so cDIV has the most control */
if (res == 8) {
ADC->CTRLB.reg = ADC_CTRLB_PRESCALER_DIV4 | ADC_CTRLB_RESSEL_8BIT;
} else if (res == 10) {
ADC->CTRLB.reg = ADC_CTRLB_PRESCALER_DIV4 | ADC_CTRLB_RESSEL_10BIT;
} else if (res == 12) {
ADC->CTRLB.reg = ADC_CTRLB_PRESCALER_DIV4 | ADC_CTRLB_RESSEL_12BIT;
} else {
Serial.println("Unsupported resolution, change the value res to 8 10 or 12");
};
ADC->SAMPCTRL.reg = 0x00; //Ensures speed isnt limites
while (ADC->STATUS.bit.SYNCBUSY) {};
/* Enable the ADC. */
ADC->CTRLA.bit.ENABLE = true;
}