forked from makerust/TheKNOB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTheKNOB.ino
450 lines (379 loc) · 11.8 KB
/
TheKNOB.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/* -- The Kinesthetic Novelty Oblate Button --
*/
#include <driver/dac.h>
#include <BleKeyboard.h>//works with version 0.2.3 of the library
//https://github.com/T-vK/ESP32-BLE-Keyboard/tree/0.2.3
BleKeyboard bleKeyboard("The KNOB", "Pangolin Design Team", 69);
#include <Adafruit_NeoPixel.h>
#include "mailbox.hpp"
#define DEBUG
#ifdef DEBUG
#define DEBUG_PRINT(x) Serial.print(x)
#define DEBUG_PRINTLN(x) Serial.println(x)
#else
#define DEBUG_PRINT(x)
#define DEBUG_PRINTLN(x)
#endif
//Pin defines
constexpr auto BATT_VOLT_PIN = 34;
constexpr auto CHG_STAT = 35;
constexpr auto USB_VOLT_PIN = 32;
constexpr auto BUTTON_1 = 33;
constexpr auto BUTTON_2 = 26;
constexpr auto ENCODER_BUTTON = 27;
constexpr auto ENCODER_B = 14;
constexpr auto ENCODER_A = 13;
constexpr auto LED_PIN = 4;
#define LED_COUNT 3
enum class Action : uint8_t{
no_action = 0,
fast_forward = 2,
reverse = 3,
volume_up = 5,
volume_down = 6,
function_press = 1,
encoder_press = 4
};
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
RTC_DATA_ATTR int bootCount = 0;
RTC_DATA_ATTR int wakeDelayB = 0;
/*
ESP32 ADC Data:
uses a 47k, 10k res div on bat voltage pin to scale 4.7v to 0.75v
0-4095 bit depth
ref 1085mV
vi = (vo * (Rb + Rt))/ Rb
scaling ratio = 5.7
batt volt: 3.80v res div 640mV
ADC 4095
*/
constexpr float adc_volt_div_correction = 5.7;
constexpr float refV = 1.085;
constexpr float precB = 4095;
constexpr float usbVoltThresh = 0.6;
float volt_read(uint8_t pin, float refV, float precB) {
float batt_bits = analogRead(pin);
float voltage = (batt_bits * refV) / precB;
return voltage;
}
int batt_chg_percent(uint8_t pin, float refV, float precB, float resDivRatio) {
//This method will be super flawed for a Li battery
float batt_volt_roll = 0;
int batt_buff_size = 30;
for(int i =0; i <= (batt_buff_size-1); i++){
batt_volt_roll += (resDivRatio * volt_read(pin, refV, precB));
//delay(3);
}
float batt_volt_now = batt_volt_roll/batt_buff_size;
//float batt_volt_now = resDivRatio * volt_read(pin, refV, precB);
if (batt_volt_now >= 4.1 )
return 100;
else if (batt_volt_now >= 3.9)
return 80;
else if (batt_volt_now >= 3.8)
return 60;
else if (batt_volt_now >= 3.7)
return 40;
else
return 20;
}
//Global mailbox array
mailbox<Action, 40> key_mailbox;
//This is used as a mutex in ESP code to handle interrupts
struct critical_section{
static portMUX_TYPE mux;
critical_section(){
portENTER_CRITICAL(&mux);//disable interrupts
}
~critical_section(){
portEXIT_CRITICAL(&mux);//enable interrupts
}
};
portMUX_TYPE critical_section::mux = portMUX_INITIALIZER_UNLOCKED;
void IRAM_ATTR enc_ISR() {
auto c = critical_section{};
//-- encoder states are read first since they are fast
// other button state read after debounce
int enc_clk_now = digitalRead(ENCODER_A);
int enc_dt_now = digitalRead(ENCODER_B);
static int enc_composit ;//byte pack of pinstates
//-- variables for Debouncing signals
static unsigned long last_interrupt_time = 0;
unsigned long interrupt_time = millis();
if (interrupt_time - last_interrupt_time > 0) { //debounces interrupts| 0 WORKED VERY WELL
last_interrupt_time = interrupt_time;
//Build a byte of encoder states
int button_state_now = (!digitalRead(BUTTON_1));
enc_composit |= (enc_dt_now << 3);
enc_composit |= (enc_clk_now << 2);
//Position stored here. 0: no change, 1: CW, -1: CCW
int enc_position = 0;
switch (enc_composit) {
case 2:
enc_position = 1;
break;
case 1:
enc_position = -1;
break;
default:
break;
}
enc_composit = enc_composit >> 2;
/* -- Regarding the cases
0 0 0 0 | 0 | X
0 0 0 1 | 1 | CCW
0 0 1 0 | 2 | CW
0 0 1 1 | 3 | X
0 1 0 0 | 4 | CW
0 1 0 1 | 5 | X
0 1 1 0 | 6 | X
0 1 1 1 | 7 | CCW
1 0 0 0 | 8 | CCW
1 0 0 1 | 9 | X
1 0 1 0 | 10| X
1 0 1 1 | 11| CW
1 1 0 0 | 12| X
1 1 0 1 | 13| CW
1 1 1 0 | 14| CCW
1 1 1 1 | 15| X
*/
//in practice, the mailbox is largely unnecessary as it never gets filled
//past the first position
if (!key_mailbox.is_full()) { //Check to not overflow the mailbox array.
// If service cannot empty in time, itmes are not added to mailbox
if (button_state_now == 1) { //if FFRW key pressed, encoder encodes for FFRW
if (enc_position > 0) {
key_mailbox.push_back(Action::fast_forward);
}
if (enc_position < 0) {
key_mailbox.push_back(Action::reverse);
}
}
else {
if (enc_position > 0) { //if not pressed, encode volume
key_mailbox.push_back(Action::volume_up);
}
else if (enc_position < 0) {
key_mailbox.push_back(Action::volume_down);
}
}
}
}
return;
}
void IRAM_ATTR key_detect() {
auto c = critical_section{};
static unsigned long last_interrupt_time2 = 0;
unsigned long interrupt_time2 = millis();
if (interrupt_time2 - last_interrupt_time2 > 300) { //debounces interrupts
last_interrupt_time2 = interrupt_time2;
int encoder_button_state = digitalRead(ENCODER_BUTTON);
int function_button_state = digitalRead(BUTTON_2);
if (!key_mailbox.is_full()) {
if (function_button_state == 0) { //function key press.
key_mailbox.push_back(Action::function_press);
}
if (encoder_button_state == 0) { //encoder key press.
key_mailbox.push_back(Action::encoder_press);
}
}
}
}
#define LED_BRIGHTNESS 5
uint32_t red = strip.ColorHSV(0, 255, LED_BRIGHTNESS);
uint32_t green = strip.ColorHSV(21845, 255, LED_BRIGHTNESS);
uint32_t blue = strip.ColorHSV(43690, 255, LED_BRIGHTNESS);
uint32_t white = strip.ColorHSV(0, 0, LED_BRIGHTNESS);
uint32_t orange = strip.ColorHSV(5461, 255, LED_BRIGHTNESS);
bool blink(bool led_stat, uint32_t color, uint32_t bkgd = 0) {
unsigned long time_millis = millis();
static unsigned long set_time_millis;
if (led_stat == 0 && time_millis % 2750 == 1) {
for (int i = 0; i < (LED_COUNT); i++)
{
strip.setPixelColor(i, color);
}
strip.show();
set_time_millis = time_millis;
led_stat = 1;
}
if (led_stat == 1 && ((time_millis - set_time_millis) > 250)) {
for (int j = 0; j < (LED_COUNT); j++)
{
strip.setPixelColor(j, bkgd);
}
strip.show();
led_stat = 0;
}
return led_stat;
}
bool light(bool led_stat, uint32_t color) {
for (int i = 0; i < (LED_COUNT); i++)
{
strip.setPixelColor(i, color);
}
strip.show();
led_stat = 1;
return led_stat;
}
void setup() {
pinMode(LED_PIN, OUTPUT);
strip.begin();
strip.show(); // Initialize all pixels to 'off'
for (int i = 0; i < LED_COUNT; i++)
{
strip.setPixelColor(i, white);
}
strip.show();
key_mailbox.fill(Action::no_action);
dac_output_disable(DAC_CHANNEL_1);
dac_output_disable(DAC_CHANNEL_2);
// Disable DAC1
REG_CLR_BIT(RTC_IO_PAD_DAC1_REG, RTC_IO_PDAC1_XPD_DAC);
REG_SET_BIT(RTC_IO_PAD_DAC1_REG, RTC_IO_PDAC1_DAC_XPD_FORCE);
// Disable DAC2
REG_CLR_BIT(RTC_IO_PAD_DAC2_REG, RTC_IO_PDAC2_XPD_DAC);
REG_SET_BIT(RTC_IO_PAD_DAC2_REG, RTC_IO_PDAC2_DAC_XPD_FORCE);
// make the pushButton pin an input:
pinMode(BUTTON_2, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(BUTTON_2), key_detect, FALLING);
pinMode(BUTTON_1, INPUT_PULLUP);
//attachInterrupt(digitalPinToInterrupt(BUTTON_1), key_detect, FALLING);
pinMode(ENCODER_BUTTON, INPUT);
attachInterrupt(digitalPinToInterrupt(ENCODER_BUTTON), key_detect, FALLING);
pinMode(ENCODER_A, INPUT);
attachInterrupt(digitalPinToInterrupt(ENCODER_A), enc_ISR, CHANGE);
pinMode(ENCODER_B, INPUT);
attachInterrupt(digitalPinToInterrupt(ENCODER_B), enc_ISR, CHANGE);
pinMode(BATT_VOLT_PIN, INPUT);
// initialize control over the keyboard:
#ifdef DEBUG
Serial.begin(115200);
Serial.println("Starting BLE work!");
#endif
bleKeyboard.begin();
//strip.show();
delay(200);
analogSetAttenuation(ADC_0db);
analogSetWidth(12);
bleKeyboard.setBatteryLevel(batt_chg_percent(BATT_VOLT_PIN, refV, precB, 5.7));
}
void loop() {
static uint32_t led_bkgd = 0;
//timing variables for power saving
static unsigned long last_send_time = 0;
#ifdef DEBUG
unsigned long power_timeout_debug = 3000000;
#else
unsigned long power_timeout_debug = 300000; //Five minutes in ms
#endif
static bool led_on = 0;
static bool usbPower = 0;
if (wakeDelayB == 1) { //A few things to try to have a more seamless wake
strip.setPixelColor(0, 0, 0, 12);
strip.show();
bleKeyboard.setBatteryLevel(batt_chg_percent(1, 1.2, 4096, 5.7));//mostly works
DEBUG_PRINT("Battery level ");
DEBUG_PRINT(batt_chg_percent(BATT_VOLT_PIN, 1.1, 4095, 5.7));
DEBUG_PRINTLN("%");
DEBUG_PRINTLN("Waking up");
delay(1000);
wakeDelayB = 0;
usbPower = 0;
}
usbPower = (volt_read(USB_VOLT_PIN, refV, precB) > usbVoltThresh);
if (bleKeyboard.isConnected() && wakeDelayB == 0) {
if (usbPower) {
led_on = light(led_on, green);
}
else {
led_on = blink(led_on, green, led_bkgd);
}
auto count = key_mailbox.count();
if (count != 0) {
DEBUG_PRINT("Send Mailbox size: ");
DEBUG_PRINTLN(count);
for (auto i = count; i > 0; i--) {
switch (key_mailbox.pop_front()) {
case Action::function_press:
bleKeyboard.write(KEY_F8);
DEBUG_PRINTLN("Send Function Key");
break;
case Action::fast_forward:
bleKeyboard.write(KEY_MEDIA_NEXT_TRACK);
DEBUG_PRINTLN("Send FF Key");
break;
case Action::reverse:
bleKeyboard.write(KEY_MEDIA_PREVIOUS_TRACK);
DEBUG_PRINTLN("Send RW Key");
break;
case Action::encoder_press:
bleKeyboard.write(KEY_MEDIA_PLAY_PAUSE);
DEBUG_PRINTLN("Send Play Pause Key");
break;
case Action::volume_up:
bleKeyboard.write(KEY_MEDIA_VOLUME_UP);
DEBUG_PRINTLN("Send UP Key");
break;
case Action::volume_down:
bleKeyboard.write(KEY_MEDIA_VOLUME_DOWN);
DEBUG_PRINTLN("Send DOWN Key");
break;
default:
break;
}
last_send_time = millis();
}
}
}
else {
if (usbPower) {
led_on = light(led_on, red);
}
else {
led_on = blink(led_on, red, led_bkgd);
}
if (millis() % 1000 == 1) {
DEBUG_PRINTLN("Disconnected");
DEBUG_PRINT("Battery voltage :");
DEBUG_PRINTLN(5.7 * volt_read(BATT_VOLT_PIN, refV, precB));
DEBUG_PRINT("ADC raw read :");
DEBUG_PRINTLN(analogRead(BATT_VOLT_PIN));
DEBUG_PRINT("USB Power? ");
DEBUG_PRINTLN(usbPower);
DEBUG_PRINT("USB voltage: ");
DEBUG_PRINTLN(5.7 * volt_read(USB_VOLT_PIN, refV, precB));
}
}
//--sleep loop
if ( (millis()) - last_send_time > power_timeout_debug) {
strip.setPixelColor(0, blue);
strip.show();
DEBUG_PRINTLN("Enter sleep mode");
wakeDelayB = 1;
delay(200);
strip.setPixelColor(0, 0, 0, 0);
strip.show();
usbPower = 0;
esp_sleep_enable_ext0_wakeup(GPIO_NUM_27, 0);//Needs to be the same number as ENCODER_BUTTON
esp_deep_sleep_start();
}
//-- battery charge update
if (millis() % 600 == 1) {//60000
static int batt_chg = 69;
int batt_chg_now = batt_chg_percent(BATT_VOLT_PIN, refV, precB, 5.7);
if (batt_chg_now != batt_chg or batt_chg == 69) {
batt_chg = batt_chg_now;
bleKeyboard.setBatteryLevel(batt_chg);
DEBUG_PRINT("Battery Level set to ");
DEBUG_PRINTLN(batt_chg);
if (batt_chg <= 20){
led_bkgd = orange;
DEBUG_PRINTLN("Bkgd color changed to orange.");
}
else{
led_bkgd = 0;
}
}
}
}