-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArduinoDeepDeck.ino
682 lines (564 loc) · 13.9 KB
/
ArduinoDeepDeck.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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
#include <Wire.h>
#include <Adafruit_GFX.h>
/*DSD prefix library are part of shared i2c workaround*/
#include <DSD_Adafruit_SSD1306.h>
#include <DSD_Adafruit_APDS9960.h>
#include <BleKeyboard.h>
#include <FastLED.h>
#include <ESP32Encoder.h>
#include <Keypad.h>
#include <ctype.h>
/*
User Interface
GUI using OLED and knob , navigation is done by using right knob.
At start up it will show Splash , to acces to the menus press left knob button.
Every press switch to another menu , rotating will change the option.
There are 3 menus:
Layout
Effects
Settings (Only dummy)
At Layou menu you can select one of this
Layout 1 is
{'a', 's', 'd', 'f'},
{'q', 'w', 'e', 'r'},
{'z', 'x', 'c', 'v'},
{'t', 'y', 'u', '#'},
Layout 2 is
{'7', '8', '9', '+'},
{'4', '5', '6', '-'},
{'1', '2', '3', '='},
{'0', '%', '$', '#'},
Layout 3 is
{'A', 'S', 'D', 'F'},
{'Q', 'W', 'E', 'R'},
{'Z', 'X', 'C', 'V'},
{'T', 'Y', 'U', '#'},
At Effects menu you can select one of this
Fadeing
Rainbow
Breathing
At Settings menu you can select one of this
Speed
Color
Brightness
Gestures
To activate the gesture please hold your hand infront of the sensor for a while.
Only required once.
Bluetooth keyboard
The default name of Bluetooth device is "AhuyamaKeyboard".
the LED in the top left corner will indicate if is device is connected to the PC.
RedLight is not connected , BlueLight means connected
Features:
Keypad : every button will send a character accodint to layout
Gesture : left and right gesture will skip fordward and backward a song
Knob : Left knob controls the volume
*/
/*Don't remove the lines bellow .It's a workaround to solve shared i2c issue */
#if defined(ESP32)
SemaphoreHandle_t shared_i2c_mutex;
#define DSD_I2C_MUTEX_CREATE shared_i2c_mutex = xSemaphoreCreateMutex()
#endif
/*Deepdeck pin mapping*/
#include "Definitions.h"
/*Deepdeck pin mapping*/
#include "BoardPins.h"
/*Layout definition for keyboard (button matrix)*/
#include "KeyBoardLayout.h"
/*User Interface*/
#include "UI.h"
/* Global var for battery level */
byte gBatteryLevel = 100;
/*Ble keyboard Classes*/
BleKeyboard bleKeyboard(BLE_KEYBOARD_NAME, BLE_KEYBOARD_MANUFACTURER, BLE_KEYBOARD_DEFAULT_BAT_LEVEL);
#define APDS9960_INT GESTURE_INT_PIN
DSD_Adafruit_APDS9960 apds;
/*Encoders Classes*/
ESP32Encoder ENC1;
int ENC1_min = 0;
int ENC1_max = 2;
ESP32Encoder ENC2;
int ENC2_min = 0;
int ENC2_max = 100;
/* flag to disable gesture if needed*/
byte disable_gesture = 0;
byte current_layout = LAYOUT_0; // Start with the numeric keypad.
/*Fix the position between the phisical distribution of the keys and
the virtual*/
void FixLayerPosition(const char *src, char *dst)
{
int i, j;
char *p = (char *)src;
for (i = 0; i < ROWS; i++)
for (j = 0; j < COLS; j++)
{
byte index = keys_postions[i][j];
dst[i * COLS + j] = p[index];
}
}
/* change keyboard layer using index*/
void SetKeyboardLayer(byte index)
{
current_layout = index;
FixLayerPosition(KPLayouts[current_layout], KeypadLayer);
}
/* get the current layer index */
byte GetKeyboardLayer(void)
{
return current_layout;
}
/*Keypad Class*/
Keypad keypad(makeKeymap(KeypadLayer), rowPins, colPins, sizeof(rowPins), sizeof(colPins));
/* OLED Class*/
DSD_Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
/* LEDs variables*/
CRGB matrix_leds[MATRIX_NUM_LEDS];
CRGB status_leds[STATUS_NUM_DATA_PIN];
/*Global GUI variables*/
int brightness = 100;
int menu_count = -1;
int menu_option = -1;
Menu menu;
int menu_state;
int menu_ops;
Layout layout;
Effect effect;
Settings param;
void setup()
{
/*Start Serial to print out messages*/
Serial.begin(115200);
/*wait till arduino terminal is ready*/
delay(2000);
/*Don't remove the linee bellow .It's a workaround to solve shared i2c issue */
DSD_I2C_MUTEX_CREATE;
Serial.println(F("Welcome to DeepDeck Ahuyama!!!"));
/*Keypad matrix of matrix_leds*/
FastLED.addLeds<WS2812, MATRIX_NUM_DATA_PIN, GRB>(matrix_leds, MATRIX_NUM_LEDS);
/*Top Two status matrix_leds*/
FastLED.addLeds<WS2812, STATUS_NUM_DATA_PIN, GRB>(status_leds, STATUS_NUM_LEDS);
/*common matrix_leds Brightness*/
FastLED.setBrightness(brightness);
/*Initialize Display*/
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_I2C_ADDRESS))
{
Serial.println(F("SSD1306 allocation failed"));
for (;;)
;
}
SplashScreen();
pinMode(batPin, INPUT);
ESP32Encoder::useInternalWeakPullResistors = UP;
ENC1.attachHalfQuad(ENC1_CLK_PIN, ENC1_DT_PIN);
ENC1.clearCount();
ENC1.setFilter(100);
ENC2.attachHalfQuad(ENC2_CLK_PIN, ENC2_DT_PIN);
ENC2.clearCount();
ENC2.setFilter(100);
pinMode(ENC1_SW_PIN, INPUT_PULLUP);
pinMode(ENC2_SW_PIN, INPUT_PULLUP);
keypad.addEventListener(keypadEvent); // Add an event listener.
keypad.setHoldTime(500); // Default is 1000mS
SetKeyboardLayer(LAYOUT_0);
bleKeyboard.begin();
while (!apds.begin(10, APDS9960_AGAIN_4X,
APDS9960_ADDRESS, &Wire))
{
Serial.println("failed to initialize device! Please check your wiring.");
}
Serial.println("Device initialized!");
/* gesture mode will be entered once proximity mode senses something close.
This mean you need to hold your hand close to it for 2 seconds */
apds.enableProximity(true);
apds.enableGesture(true);
/* task for gesture sensing core 0*/
xTaskCreatePinnedToCore(
taskGesture, /* Task function. */
"taskGesture", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
NULL, /* Task handle to keep track of created task */
0); /* pin task to core 0 */
/* task for keypad leds effects core 1*/
xTaskCreatePinnedToCore(
taskLeds, /* Task function. */
"taskLeds", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
NULL, /* Task handle to keep track of created task */
1); /* pin task to core 1 */
}
void taskGesture(void *parameter)
{
while (1)
{
CheckGesture();
delay(50);
}
}
void taskLeds(void *parameter)
{
while (1)
{
LedRefresh();
delay(20);
}
}
/*
Auxiliar function to handle especial keypad events , like hold press , pressed, released
*/
void keypadEvent(KeypadEvent key)
{
byte kpadState = keypad.getState();
switch (kpadState)
{
case PRESSED:
break;
case HOLD:
if (key == '#')
{
}
break;
case RELEASED:
break;
}
}
/* Function to handle gestures and perfomr actions*/
void handleGesture()
{
static unsigned long GestureTicks = 0;
/*don't check the gestures too often*/
if ((millis() - GestureTicks) <= 50)
return;
GestureTicks = millis();
unsigned long tick = millis();
uint8_t gesture = apds.readGesture();
if (!gesture)
return;
/*print out gesture lapse */
Serial.print("gesture lapse : ");
Serial.println((millis() - tick));
/*ignore gesture if disable falg is activated */
if (disable_gesture)
{
disable_gesture = 0;
Serial.println("NONE3");
return;
}
/*ignore gesture if the gesture took so long */
if ((millis() - tick) > 3000)
{
Serial.println("NONE2");
return;
}
/* print and do an action depending on the gesture*/
switch (gesture)
{
case APDS9960_UP:
Serial.println("UP");
break;
case APDS9960_DOWN:
Serial.println("DOWN");
break;
case APDS9960_LEFT:
Serial.println("LEFT");
if (bleKeyboard.isConnected())
bleKeyboard.write(KEY_MEDIA_PREVIOUS_TRACK);
break;
case APDS9960_RIGHT:
Serial.println("RIGHT");
if (bleKeyboard.isConnected())
bleKeyboard.write(KEY_MEDIA_NEXT_TRACK);
break;
default:
Serial.println("NONE");
}
}
void CheckGesture(void)
{
handleGesture();
}
void CheckBLE(void)
{
static unsigned long BLETicks = 0;
/* every 500ms check if BLE is connected*/
if ((millis() - BLETicks) <= 500)
return;
BLETicks = millis();
/*Depending on BLE connection status change the color of the led*/
if (bleKeyboard.isConnected())
{
status_leds[0] = CRGB::Blue;
}
else
{
status_leds[0] = CRGB::Red;
}
FastLED.show();
}
void CheckKeypad(void)
{
static unsigned long KeypadTicks = 0;
if ((millis() - KeypadTicks) <= 10)
return;
KeypadTicks = millis();
char key = keypad.getKey();
if (key)
{
Serial.print("KEY pressed: ");
Serial.println(key);
if (bleKeyboard.isConnected())
{
bleKeyboard.write((char)key);
}
else
{
}
}
}
void CheckEncoders(void)
{
static unsigned long encoderTicks = 0;
if ((millis() - encoderTicks) <= 100)
return;
encoderTicks = millis();
static long pval1 = 0;
long val = ENC1.getCount();
// Serial.print("ENC1 val: ");
// Serial.println(val);
long delta = pval1 - val;
pval1 = val;
if (delta)
{
disable_gesture = 1;
}
// Serial.print("ENC1 delta: ");
// Serial.println(delta);
static long count1 = 0;
count1 = count1 + delta;
if (count1 < ENC1_min)
{
count1 = ENC1_max;
}
else if (count1 > ENC1_max)
{
count1 = ENC1_min;
}
// Serial.print("ENC1 count: ");
// Serial.println(count1);
menu_option = count1;
static long pval2 = 0;
val = ENC2.getCount();
// Serial.print("ENC2 val: ");
// Serial.println(val);
delta = pval2 - val;
pval2 = val;
if (bleKeyboard.isConnected())
{
if (delta > 0)
bleKeyboard.write(KEY_MEDIA_VOLUME_UP);
if (delta < 0)
bleKeyboard.write(KEY_MEDIA_VOLUME_DOWN);
}
// Serial.print("ENC2 delta: ");
// Serial.println(delta);
static long count2 = 0;
count2 = count2 + delta;
if (count2 < ENC2_min)
{
count2 = ENC2_max;
}
else if (count2 > ENC2_max)
{
count2 = ENC2_min;
}
// Serial.print("ENC2 count: ");
// Serial.println(count2);
static unsigned long ENC1SWTick = 0;
int ENC1SWState = digitalRead(ENC1_SW_PIN);
static unsigned long ENC1SWLapse = 50;
if (ENC1SWState == HIGH)
{
if (millis() - ENC1SWTick > ENC1SWLapse)
{
ENC1SWLapse = 250;
ENC1SWTick = millis();
Serial.println("ENC1 button pressed");
if (menu_count == -1)
{
menu_count = 0;
menu_option = 0;
ENC1.setCount(menu_option);
ENC1_min = 0;
ENC1_max = 2;
}
else
{
if (menu_count < 2)
menu_count++;
else
menu_count = 0;
}
}
}
else
{
ENC1SWLapse = 50;
}
static unsigned long ENC2SWTick = 0;
int ENC2SWState = digitalRead(ENC2_SW_PIN);
if (ENC2SWState == HIGH)
{
if (millis() - ENC2SWTick > 50)
{
Serial.println("ENC2 button pressed");
ENC2SWTick = millis();
}
}
}
/* GUI */
void loop()
{
CheckEncoders();
CheckKeypad();
CheckBLE();
GUILoop();
delay(10);
}
void displayPrintHCenter(uint16_t YPos, String text)
{
int16_t x1;
int16_t y1;
uint16_t width;
uint16_t height;
display.getTextBounds(text, 0, 0, &x1, &y1, &width, &height);
display.setCursor((SCREEN_WIDTH - width) / 2, YPos);
display.print(text); // text to display
display.display();
}
void SplashScreen(void)
{
display.clearDisplay();
display.display();
display.setTextColor(WHITE, BLACK);
display.cp437(true);
display.clearDisplay();
display.setTextSize(1);
displayPrintHCenter(1, "DeepDeck Ahuyama");
display.setTextSize(2);
displayPrintHCenter(20, "Welcome");
display.display();
}
void LedRefresh()
{
switch (effect)
{
// rainbow
case effect1:
fill_rainbow(matrix_leds, MATRIX_NUM_LEDS, hue, 30);
FastLED.show();
hue += 3;
break;
// fading
case effect2:
fill_solid(matrix_leds, MATRIX_NUM_LEDS, CHSV(hue, 255, brightness));
FastLED.show();
brightness = brightness + fadeAmount;
if (brightness <= 10 || brightness >= 250)
fadeAmount = -fadeAmount;
break;
// breathing
case effect3:
fill_solid(matrix_leds, MATRIX_NUM_LEDS, CHSV(hue, 255, 192));
FastLED.show();
hue += 3;
break;
}
}
void GUILoop(void)
{
static int last_menu_count = -1;
static int last_menu_option = -1;
if ((menu_count == -1) || (menu_option == -1))
return;
if ((last_menu_count == menu_count) && (last_menu_option == menu_option))
return;
Serial.printf("menu %u option %u\n", menu_count, menu_option);
last_menu_count = menu_count;
last_menu_option = menu_option;
display.clearDisplay();
display.setTextSize(1);
switch (menu_count)
{
case layer:
displayPrintHCenter(2, "Layout");
break;
case light:
displayPrintHCenter(2, "Effects");
break;
case settings:
displayPrintHCenter(2, "Settings");
break;
}
display.setTextSize(2);
display.setCursor(0, 20);
if (menu_count == 0)
{
switch (menu_option)
{
default:
case layout1:
SetKeyboardLayer(LAYOUT_0);
displayPrintHCenter(20, "Layout1");
break;
case layout2:
SetKeyboardLayer(LAYOUT_1);
displayPrintHCenter(20, "Layout2");
break;
case layout3:
SetKeyboardLayer(LAYOUT_2);
displayPrintHCenter(20, "Layout3");
break;
}
}
else if (menu_count == light)
{
switch (menu_option)
{
default:
case effect1:
effect = effect1;
displayPrintHCenter(20, "Breathing");
break;
case effect2:
effect = effect2;
displayPrintHCenter(20, "Fadeing");
break;
case effect3:
effect = effect3;
displayPrintHCenter(20, "Rainbow");
break;
}
}
else if (menu_count == settings)
{
switch (menu_option)
{
default:
case color:
param = opacity;
displayPrintHCenter(20, "Brightness");
break;
case opacity:
param = speed;
displayPrintHCenter(20, "Speed");
break;
case speed:
param = color;
displayPrintHCenter(20, "Color");
break;
}
}
display.display();
}