-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
1310 lines (1050 loc) · 40.8 KB
/
main.cpp
File metadata and controls
1310 lines (1050 loc) · 40.8 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
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
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <EEPROMex.h>
#include <avr/pgmspace.h>
#include <OneWire.h>
#include <MemoryFree.h>
#include <OneWireHub.h>
#include <DS2401.h>
#define NUM_ROWS 4
#define OFFSET_X 10
#define OFFSET_Y 10
#define FONT_SIZE 1
#define FONT_HEIGHT 8
#define FONT_WIDTH 6
#define LINE_WIDTH 2
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define RESET_PIN 4
#define DEBUG 1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, RESET_PIN);
/*
* Some words about keys: I intend to use this device only as an
* iButton emulator for now, but later might add Metakom and
* Cyfral keys. iButton key uses 48 bits of information,
* Metakom - 28, Cyfral - 16, so dword should be plenty for a
* key. But we also need to keep key type (1 byte) and key name
* (let's say 32 characters 1 byte each), so memory diagram looks
* like this:
*
* Key {
* byte type;
* char name[32];
* byte key[8];
* }
*
* All of these keys are stored in EEPROM and the first two bytes
* of it are used for the total number of keys.
*/
#define KEY_PIN A3
#define KEY_NAME_OFFSET 1
#define KEY_NAME_LEN 32
#define KEY_TYPE_OFFSET 0
#define KEY_OFFSET 33
#define KEY_TABLE_OFFSET 2
#define KEY_SIZE 41
struct Key {
uint64_t cur_key;
int key_index;
byte key_type;
};
OneWire ibutton(KEY_PIN);
byte read_key(uint64_t *key);
byte copy_key(uint64_t new_key, Adafruit_SSD1306 *display = NULL);
void emulate_key(uint64_t key);
void save_key(bool original_title = false);
void delete_key(int index);
Key get_key_by_index(int index);
int get_key_offset(int index);
void update_key_by_index(Key key);
void writeByte(byte data, int pin);
byte read_ds1990(uint64_t *key);
byte copy_ds1990(uint64_t new_key, Adafruit_SSD1306 *display = NULL);
void emulate_ds1990(uint64_t key);
const int read_functions[] PROGMEM = {
(const int)read_ds1990,
};
const int emulate_functions[] PROGMEM = {
(const int)emulate_ds1990,
};
const int copy_functions[] PROGMEM = {
(const int)copy_ds1990,
};
void top_button ();
void middle_button ();
void bottom_button ();
void check_buttons ();
bool check_button(int index);
void draw(int offset);
void switch_screen(int offset);
void redraw();
void list_screen_top_button_pressed(int offset);
void list_screen_middle_button_pressed(int offset);
void read_screen_menu_middle_button_pressed(int offset);
void list_screen_bottom_button_pressed(int offset);
void list_screen_draw(int offset);
void key_list_top_button_pressed(int offset);
void key_list_middle_button_pressed(int offset);
void key_menu_middle_button_pressed(int offset); // TODO
void key_list_bottom_button_pressed(int offset);
void key_list_draw(int offset);
void display_screen_top_button_pressed(int offset);
void read_screen_middle_button_pressed(int offset);
void emulate_screen_middle_button_pressed(int offset);
void copy_screen_middle_button_pressed(int offset);
void display_screen_bottom_button_pressed(int offset);
void display_screen_draw(int offset);
void display_key_screen_draw(int offset);
#define SCREEN_TOP_BUTTON_OFFSET 0
#define SCREEN_MIDDLE_BUTTON_OFFSET 1
#define SCREEN_BOTTOM_BUTTON_OFFSET 2
#define SCREEN_DRAW_FUNC_OFFSET 3
#define LIST_SCREEN_N_OFFSET 4
#define LIST_SCREEN_STRINGS_OFFSET 5
#define KEY_LIST_MAIN_OFFSET 4
#define KEY_LIST_N_OFFSET 5
#define KEY_LIST_STRINGS_OFFSET 6
#define DISPLAY_SCREEN_NAME_OFFSET 4
#define DISPLAY_SCREEN_SPECIFIER_OFFSET 5
#define DISPLAY_SCREEN_N_OFFSET 6
#define DISPLAY_SCREEN_OPTIONS_OFFSET 7
#define DISPLAY_SCREEN_NAME_Y_OFFSET 7
/*
* So... Arduino Pro Mini has 2Kb of SRAM, which is too little for
* proper screen managment system, considering 1Kb gets used by
* screen buffer. In order to overcome that, screen layout stays
* in Flash memory using PROGMEM keyword
*
* Objects can be represented like this:
* ListScreen {
* // offset points to position in array of strings,
* // corresponding to the screen, keeping the functions
* void (*on_top_button_pressed)(int offset);
* void (*on_middle_button_pressed)(int offset);
* void (*on_bottom_button_pressed)(int offset);
* void (*draw)(int offset);
*
* int n_childen;
* char *names[];
* int offsets[];
* }
*
* KeyList {
* // offset points to position in array of strings,
* // corresponding to the screen, keeping the functions
* void (*on_top_button_pressed)(int offset);
* void (*on_middle_button_pressed)(int offset);
* void (*on_bottom_button_pressed)(int offset);
* void (*draw)(int offset);
*
* int main_offset;
* int n_childen;
* char *names[];
* int offsets[];
* }
*
* DisplayScreen {
* void (*on_top_button_pressed)(int offset);
* void (*on_middle_button_pressed)(int offset);
* void (*on_bottom_button_pressed)(int offset);
* void (*draw)(int offset);
*
* char *name;
* char *specifier;
* int n_options;
* char *names[];
* }
*/
const char str_blank[] PROGMEM = "";
const char str0[] PROGMEM = "READ KEY";
const char str1[] PROGMEM = "EMULATE KEY";
const char str2[] PROGMEM = "COPY KEY";
const char str3[] PROGMEM = "CHOOSE KEY";
const char str4[] PROGMEM = "BRUTE FORCE";
const char str5[] PROGMEM = "BACK";
const char str6[] PROGMEM = "TYPE: ";
const char str7[] PROGMEM = "DS1990";
const char str8[] PROGMEM = "READING...";
const char str9[] PROGMEM = "SUCCESSFULLY READ";
const char str10[] PROGMEM = "SAVE";
const char str11[] PROGMEM = "EMULATE";
const char str12[] PROGMEM = "COPY";
const char str13[] PROGMEM = "CANCEL";
const char str14[] PROGMEM = "EMULATING...";
const char str15[] PROGMEM = "COPYING...";
const char str16[] PROGMEM = "Copying successful";
const char str17[] PROGMEM = "Copying failure";
const char str18[] PROGMEM = "READ BUT NOT IBUTTON";
const char str19[] PROGMEM = "READ BUT WRONG CRC";
const char str20[] PROGMEM = "New key ";
const char str21[] PROGMEM = "DELETE";
const char *const string_arr[] PROGMEM = {str0, str1, str2, str3, str4, str5, str6, str7, str8,
str9, str10, str11, str12, str13, str14, str15, str16, str17, str18, str19, str20, str21};
#define BUFFER_LEN 64
char buffer[BUFFER_LEN];
bool new_data = false;
void check_serial();
void process_serial();
enum Offset {
MAIN_MENU = 0,
READ_SCREEN = 10,
READ_SUCCESSFUL_MENU = 18,
KEY_MENU = 31,
EMULATE_SCREEN = 44,
COPY_SCREEN = 51,
NULL_SCREEN
};
const int screens[] PROGMEM = {
//Main key menu
(const int)key_list_top_button_pressed,
(const int)key_list_middle_button_pressed,
(const int)key_list_bottom_button_pressed,
(const int)key_list_draw,
KEY_MENU,
2,
(const int)str0,
(const int)str_blank,
READ_SCREEN,
NULL_SCREEN,
//Read screen
(const int)display_screen_top_button_pressed,
(const int)read_screen_middle_button_pressed,
(const int)display_screen_bottom_button_pressed,
(const int)display_screen_draw,
(const int)str0,
(const int)str6,
1,
(const int)str7,
//Read screen menu
(const int)list_screen_top_button_pressed,
(const int)read_screen_menu_middle_button_pressed,
(const int)list_screen_bottom_button_pressed,
(const int)list_screen_draw,
4,
(const int)str10,
(const int)str11,
(const int)str12,
(const int)str13,
MAIN_MENU,
EMULATE_SCREEN,
COPY_SCREEN,
MAIN_MENU,
//Key menu
(const int)list_screen_top_button_pressed,
(const int)key_menu_middle_button_pressed,
(const int)list_screen_bottom_button_pressed,
(const int)list_screen_draw,
4,
(const int)str11,
(const int)str12,
(const int)str21,
(const int)str5,
EMULATE_SCREEN,
COPY_SCREEN,
MAIN_MENU,
MAIN_MENU,
//Emulate screen
(const int)display_screen_top_button_pressed,
(const int)emulate_screen_middle_button_pressed,
(const int)display_screen_bottom_button_pressed,
(const int)display_key_screen_draw,
(const int)str1,
(const int)str_blank,
0,
//Copy screen
(const int)display_screen_top_button_pressed,
(const int)copy_screen_middle_button_pressed,
(const int)display_screen_bottom_button_pressed,
(const int)display_key_screen_draw,
(const int)str2,
(const int)str_blank,
0,
};
int prev_screen = 0;
int cur_screen = 0;
int cur_child = 0;
Key global_key = {0, -1, 0};
#define TOP_BUTTON_PIN 2
#define MIDDLE_BUTTON_PIN 3
#define BOTTOM_BUTTON_PIN 4
#define TOP_BUTTON_INDEX 0
#define MIDDLE_BUTTON_INDEX 1
#define BOTTOM_BUTTON_INDEX 2
struct Button {
byte pin : 4;
bool executed : 1;
bool pressed : 1;
void (*func)(void);
};
struct Button buttons[3] = {
{ TOP_BUTTON_PIN, false, false, top_button },
{ MIDDLE_BUTTON_PIN, false, false, middle_button },
{ BOTTOM_BUTTON_PIN, false, false, bottom_button }
};
void setup() {
Serial.begin(9600);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.setRotation(2);
pinMode(TOP_BUTTON_PIN, INPUT_PULLUP);
pinMode(MIDDLE_BUTTON_PIN, INPUT_PULLUP);
pinMode(BOTTOM_BUTTON_PIN, INPUT_PULLUP);
switch_screen(MAIN_MENU);
reinterpret_cast<decltype(draw)*>(pgm_read_word_near(&screens[MAIN_MENU + SCREEN_DRAW_FUNC_OFFSET]))(MAIN_MENU);
}
void loop() {
check_buttons();
check_serial();
process_serial();
}
void check_serial() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '[';
char endMarker = ']';
char rc;
while (Serial.available() > 0 && !new_data) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
buffer[ndx] = rc;
ndx++;
if (ndx >= BUFFER_LEN) {
ndx = BUFFER_LEN - 1;
}
}
else {
buffer[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
new_data = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
void process_serial() {
if (new_data) {
Serial.print("Received: ");
Serial.println(buffer);
if (buffer[0] == 'K') {
int key_num = atoi(buffer + 2);
EEPROM.updateInt(0, key_num);
} else if (buffer[0] == 'D') {
int deleted = atoi(buffer + 2);
delete_key(deleted);
} else if (buffer[0] == 'W') {
byte i = 2, j = 0;
for (; j < KEY_NAME_LEN && buffer[i] != ' '; i++, j++) {
buffer[j] = buffer[i];
}
if (j < KEY_NAME_LEN)
buffer[j] = '\0';
char *cur_pointer = buffer + i + 1;
global_key.key_type = strtol(cur_pointer, &cur_pointer, 10);
global_key.cur_key = 0;
for (j = 0; j < 7; j++) {
((uint8_t*)&global_key.cur_key)[j] = (byte)strtol(cur_pointer, &cur_pointer, 16);
}
((uint8_t*)&global_key.cur_key)[7] = ibutton.crc8((uint8_t*)&global_key.cur_key, 7);
Serial.print(F("Received "));
for (j = 0; j < 8; j++) {
if (((uint8_t*)&global_key.cur_key)[j] / 16 == 0)
Serial.print(0);
Serial.print(((uint8_t*)&global_key.cur_key)[j], HEX);
Serial.print(' ');
}
Serial.println();
save_key(true);
} else if (buffer[0] == 'L') {
int cur_n_keys = EEPROM.readInt(0);
Serial.print(F("Number of keys - "));
Serial.println(cur_n_keys);
for (int i = 0; i < cur_n_keys; i++) {
int cur_key_start = get_key_offset(i);
Serial.print(i);
Serial.print(' ');
for (byte j = 0; j < KEY_NAME_LEN && EEPROM.readByte(cur_key_start + KEY_NAME_OFFSET + j) != '\0'; j++) {
Serial.print((char)EEPROM.readByte(cur_key_start + KEY_NAME_OFFSET + j));
}
Serial.print(' ');
Serial.print(EEPROM.readByte(cur_key_start + KEY_TYPE_OFFSET));
Serial.print(' ');
for (byte j = 0; j < 8; j++) {
if (EEPROM.readByte(cur_key_start + KEY_OFFSET + j) / 16 == 0)
Serial.print(0);
Serial.print(EEPROM.readByte(cur_key_start + KEY_OFFSET + j), HEX);
Serial.print(' ');
}
Serial.println();
}
}
new_data = false;
}
}
#pragma region BUTTONS
void check_buttons () {
for (byte i = 0; i < 3; i++) {
buttons[i].pressed = !digitalRead(buttons[i].pin);
}
delay(25);
for (byte i = 0; i < 3; i++) {
if (buttons[i].pressed && !digitalRead(buttons[i].pin)) {
if (!buttons[i].executed) {
buttons[i].executed = true;
buttons[i].func();
}
} else {
buttons[i].pressed = false;
buttons[i].executed = false;
}
}
}
bool check_button(int index) {
buttons[index].pressed = !digitalRead(buttons[index].pin);
delay(25);
if (buttons[index].pressed && !digitalRead(buttons[index].pin)) {
if (!buttons[index].executed) {
return true;
}
} else {
buttons[index].pressed = false;
buttons[index].executed = false;
}
return false;
}
void top_button () {
reinterpret_cast<decltype(draw)*>(pgm_read_word_near(&screens[cur_screen + SCREEN_TOP_BUTTON_OFFSET]))(cur_screen);
}
void middle_button () {
reinterpret_cast<decltype(draw)*>(pgm_read_word_near(&screens[cur_screen + SCREEN_MIDDLE_BUTTON_OFFSET]))(cur_screen);
}
void bottom_button () {
#if DEBUG
Serial.println(F("bottom_button"));
#endif
reinterpret_cast<decltype(draw)*>(pgm_read_word_near(&screens[cur_screen + SCREEN_BOTTOM_BUTTON_OFFSET]))(cur_screen);
}
#pragma endregion
void switch_screen(int offset) {
prev_screen = cur_screen;
cur_screen = offset;
cur_child = 0;
}
void redraw() {
reinterpret_cast<decltype(draw)*>(pgm_read_word(&screens[cur_screen + SCREEN_DRAW_FUNC_OFFSET]))(cur_screen);
}
#pragma region LIST_SCREEN
void list_screen_top_button_pressed(int offset) {
#if DEBUG
Serial.println(F("list_screen_top_button_pressed"));
#endif
byte n_children = (int)pgm_read_word_near(&screens[offset + LIST_SCREEN_N_OFFSET]);
cur_child = (cur_child + n_children - 1) % n_children;
redraw();
}
void list_screen_middle_button_pressed(int offset) {
#if DEBUG
Serial.println(F("list_screen_middle_button_pressed"));
#endif
byte n_children = (int)pgm_read_word_near(&screens[offset + LIST_SCREEN_N_OFFSET]);
int arr_start = offset + LIST_SCREEN_STRINGS_OFFSET + n_children;
int new_offset = (int)pgm_read_word_near(&screens[arr_start + cur_child]);
switch_screen(new_offset);
redraw();
}
void read_screen_menu_middle_button_pressed(int offset) {
#if DEBUG
Serial.println(F("read_screen_menu_middle_button_pressed"));
#endif
byte n_children = (int)pgm_read_word_near(&screens[offset + LIST_SCREEN_N_OFFSET]);
int arr_start = offset + LIST_SCREEN_STRINGS_OFFSET + n_children;
int new_offset = (int)pgm_read_word_near(&screens[arr_start + cur_child]);
#if DEBUG
Serial.print(F("Current child "));
Serial.println(cur_child);
#endif
if (cur_child == 0)
save_key();
switch_screen(new_offset);
prev_screen = MAIN_MENU;
redraw();
if (new_offset == EMULATE_SCREEN) {
reinterpret_cast<decltype(draw)*>(pgm_read_word_near(&screens[new_offset + SCREEN_MIDDLE_BUTTON_OFFSET]))(new_offset);
} else if (new_offset == COPY_SCREEN) {
reinterpret_cast<decltype(draw)*>(pgm_read_word_near(&screens[new_offset + SCREEN_MIDDLE_BUTTON_OFFSET]))(new_offset);
}
}
void list_screen_bottom_button_pressed(int offset) {
#if DEBUG
Serial.println(F("list_screen_bottom_button_pressed"));
#endif
byte n_children = (int)pgm_read_word_near(&screens[offset + LIST_SCREEN_N_OFFSET]);
cur_child = (cur_child + 1) % n_children;
redraw();
}
void list_screen_draw(int offset) {
byte width = SCREEN_WIDTH - OFFSET_X * 2,
height = (SCREEN_HEIGHT - OFFSET_Y * 2) / NUM_ROWS,
text_y_offset = (height - FONT_HEIGHT) / 2 + 1;
byte n_children = (int)pgm_read_word_near(&screens[offset + LIST_SCREEN_N_OFFSET]);
int arr_start = offset + LIST_SCREEN_STRINGS_OFFSET;
display.clearDisplay();
display.setTextSize(FONT_SIZE);
for (byte start = (cur_child / NUM_ROWS) * NUM_ROWS, i = 0;
(i < NUM_ROWS) && (start < n_children); i++, start++) {
strcpy_P(buffer, (char *)pgm_read_word_near(&screens[arr_start + start]));
display.fillRect(OFFSET_X, OFFSET_Y + height * i,
width, height, start == cur_child);
display.setCursor(OFFSET_X + 1, OFFSET_Y + height * i + text_y_offset);
display.setTextColor(start != cur_child);
display.println(buffer);
if ((int)pgm_read_word_near(&screens[offset + LIST_SCREEN_STRINGS_OFFSET + n_children + start]) == NULL_SCREEN) {
display.fillRect(OFFSET_X + 1, OFFSET_Y + height * i + (height - LINE_WIDTH) / 2, display.width() - (OFFSET_X + 1) * 2, LINE_WIDTH, start != cur_child);
}
}
display.display();
}
void key_list_top_button_pressed(int offset) {
byte n_children = (int)pgm_read_word_near(&screens[offset + KEY_LIST_N_OFFSET]);
byte n_keys = EEPROM.readInt(0);
cur_child = (cur_child + n_children + n_keys - 1) % (n_children + n_keys);
if ((int)pgm_read_word_near(&screens[offset + KEY_LIST_STRINGS_OFFSET + n_children + cur_child]) == NULL_SCREEN)
cur_child = (cur_child + n_children + n_keys - 1) % (n_children + n_keys);
redraw();
}
void key_list_middle_button_pressed(int offset) {
byte n_children = (int)pgm_read_word_near(&screens[offset + KEY_LIST_N_OFFSET]);
if (cur_child < n_children) {
int arr_start = offset + KEY_LIST_STRINGS_OFFSET + n_children;
int new_offset = (int)pgm_read_word_near(&screens[arr_start + cur_child]);
if (new_offset != NULL_SCREEN) {
switch_screen(new_offset);
redraw();
}
} else {
global_key = get_key_by_index(cur_child - n_children);
int new_offset = (int)pgm_read_word(&screens[offset + KEY_LIST_MAIN_OFFSET]);
switch_screen(new_offset);
redraw();
}
}
void key_menu_middle_button_pressed(int offset) {
byte n_children = (int)pgm_read_word_near(&screens[offset + LIST_SCREEN_N_OFFSET]);
int arr_start = offset + LIST_SCREEN_STRINGS_OFFSET + n_children;
int new_offset = (int)pgm_read_word_near(&screens[arr_start + cur_child]);
if (cur_child == 2) {
delete_key(global_key.key_index);
}
switch_screen(new_offset);
redraw();
}
void key_list_bottom_button_pressed(int offset) {
byte n_children = (int)pgm_read_word_near(&screens[offset + KEY_LIST_N_OFFSET]);
byte n_keys = EEPROM.readInt(0);
cur_child = (cur_child + 1) % (n_children + n_keys);
if ((int)pgm_read_word_near(&screens[offset + KEY_LIST_STRINGS_OFFSET + n_children + cur_child]) == NULL_SCREEN)
cur_child = (cur_child + 1) % (n_children + n_keys);
redraw();
}
void key_list_draw(int offset) {
display.clearDisplay();
byte width = SCREEN_WIDTH - OFFSET_X * 2,
height = (SCREEN_HEIGHT - OFFSET_Y * 2) / NUM_ROWS,
text_y_offset = (height - FONT_HEIGHT) / 2 + 1;
byte n_children = (int)pgm_read_word_near(&screens[offset + KEY_LIST_N_OFFSET]);
byte n_keys = EEPROM.readInt(0);
byte start = 0, i = 0;
for (start = (cur_child / NUM_ROWS) * NUM_ROWS, i = 0;
(i < NUM_ROWS) && (start < n_children); i++, start++) {
strcpy_P(buffer, (char *)pgm_read_word(&screens[offset + KEY_LIST_STRINGS_OFFSET + start]));
display.fillRect(OFFSET_X, OFFSET_Y + height * i,
width, height, start == cur_child);
display.setCursor(OFFSET_X + 1, OFFSET_Y + height * i + text_y_offset);
display.setTextColor(start != cur_child);
display.println(buffer);
if ((int)pgm_read_word_near(&screens[offset + KEY_LIST_STRINGS_OFFSET + n_children + start]) == NULL_SCREEN) {
display.fillRect(OFFSET_X + 1, OFFSET_Y + height * i + (height - LINE_WIDTH) / 2, display.width() - (OFFSET_X + 1) * 2, LINE_WIDTH, start != cur_child);
}
}
for (byte j = 0; (start < n_keys + n_children) && (i < NUM_ROWS); start++, i++, j++) {
int key_start = get_key_offset(start - n_children);
byte k = 0;
for (;k < KEY_NAME_LEN && EEPROM.readByte(key_start + KEY_NAME_OFFSET + k) != '\0'; k++)
buffer[k] = EEPROM.readByte(key_start + KEY_NAME_OFFSET + k);
if (k < BUFFER_LEN)
buffer[k] = '\0';
display.fillRect(OFFSET_X, OFFSET_Y + height * i,
width, height, start == cur_child);
display.setCursor(OFFSET_X + 1, OFFSET_Y + height * i + text_y_offset);
display.setTextColor(start != cur_child);
display.println(buffer);
}
display.display();
}
#pragma endregion
#pragma region DISPLAY_SCREEN
void display_screen_top_button_pressed(int offset) {
#if DEBUG
Serial.println(F("display_screen_top_button_pressed"));
#endif
byte n_options = (int) pgm_read_word_near(&screens[offset + DISPLAY_SCREEN_N_OFFSET]);
if (n_options)
cur_child = (cur_child + 1) % n_options;
redraw();
}
void read_screen_middle_button_pressed(int offset) {
#if DEBUG
Serial.println(F("read_screen_middle_button_pressed"));
#endif
global_key.key_type = cur_child;
global_key.key_index = -1;
display.fillRect(0, SCREEN_HEIGHT / 2, SCREEN_WIDTH, FONT_HEIGHT * FONT_SIZE, BLACK);
strcpy_P(buffer, (char *)pgm_read_word_near(&string_arr[8]));
byte msg_len = strlen(buffer);
display.setTextSize(FONT_SIZE);
display.setTextColor(WHITE);
display.setCursor((SCREEN_WIDTH - msg_len * FONT_SIZE * FONT_WIDTH) / 2, SCREEN_HEIGHT / 2);
display.println(buffer);
display.display();
byte exit_code = 1;
while (exit_code == 1) {
if (check_button(MIDDLE_BUTTON_INDEX)) {
buttons[MIDDLE_BUTTON_INDEX].executed = true;
redraw();
return;
}
if (check_button(BOTTOM_BUTTON_INDEX)) {
buttons[BOTTOM_BUTTON_INDEX].executed = true;
switch_screen(prev_screen);
redraw();
return;
}
exit_code = read_key(&global_key.cur_key);
delay(25);
}
display.fillRect(0, SCREEN_HEIGHT / 2, SCREEN_WIDTH, FONT_HEIGHT * FONT_SIZE, BLACK);
switch (exit_code) {
case 0:
strcpy_P(buffer, (char *)pgm_read_word_near(&string_arr[9]));
display.setCursor((SCREEN_WIDTH - 16 * FONT_SIZE * FONT_WIDTH) / 2, SCREEN_HEIGHT / 2 + FONT_SIZE * FONT_HEIGHT);
for (byte i = 0; i < 8; i++) {
if (((uint8_t *)&global_key.cur_key)[i] / 16 == 0)
display.print(0);
display.print(((uint8_t *)&global_key.cur_key)[i], HEX);
}
break;
case 2:
strcpy_P(buffer, (char *)pgm_read_word_near(&string_arr[18]));
break;
case 3:
strcpy_P(buffer, (char *)pgm_read_word_near(&string_arr[19]));
break;
}
msg_len = strlen(buffer);
display.setCursor((SCREEN_WIDTH - msg_len * FONT_SIZE * FONT_WIDTH) / 2, SCREEN_HEIGHT / 2);
display.println(buffer);
display.display();
delay(2000);
if (exit_code == 0)
switch_screen(READ_SUCCESSFUL_MENU);
else
switch_screen(prev_screen);
redraw();
}
void emulate_screen_middle_button_pressed(int offset) {
int n_keys = EEPROM.readInt(0);
display.setTextSize(FONT_SIZE);
display.setTextColor(WHITE);
strcpy_P(buffer, (char *)pgm_read_word_near(&string_arr[14]));
byte msg_len = strlen(buffer);
display.setCursor((SCREEN_WIDTH - msg_len * FONT_SIZE * FONT_WIDTH) / 2, SCREEN_HEIGHT / 2 + 2 * FONT_SIZE * FONT_HEIGHT);
display.println(buffer);
display.display();
for (;; (global_key.key_index != -1) && (global_key.key_index = (global_key.key_index + 1) % n_keys)) {
if (global_key.key_index != -1)
global_key = get_key_by_index(global_key.key_index);
display.fillRect((SCREEN_WIDTH - 16 * FONT_SIZE * FONT_WIDTH) / 2, SCREEN_HEIGHT / 2 + FONT_SIZE * FONT_HEIGHT, 16 * FONT_WIDTH * FONT_SIZE, FONT_HEIGHT * FONT_SIZE, BLACK);
display.setCursor((SCREEN_WIDTH - 16 * FONT_SIZE * FONT_WIDTH) / 2, SCREEN_HEIGHT / 2 + FONT_SIZE * FONT_HEIGHT);
for (byte i = 0; i < 8; i++) {
if (((uint8_t *)&global_key.cur_key)[i] / 16 == 0)
display.print(0);
display.print(((uint8_t *)&global_key.cur_key)[i], HEX);
}
display.display();
emulate_key(global_key.cur_key);
if (check_button(MIDDLE_BUTTON_INDEX)) {
buttons[MIDDLE_BUTTON_INDEX].executed = true;
break;
}
if (check_button(BOTTOM_BUTTON_INDEX)) {
buttons[BOTTOM_BUTTON_INDEX].executed = true;
switch_screen(prev_screen);
break;
}
if (check_button(TOP_BUTTON_INDEX)) {
buttons[TOP_BUTTON_INDEX].executed = true;
if (global_key.key_index == -1)
break;
}
}
redraw();
}
void copy_screen_middle_button_pressed(int offset) {
display.setTextSize(FONT_SIZE);
display.setTextColor(WHITE);
strcpy_P(buffer, (char *)pgm_read_word_near(&string_arr[15]));
byte msg_len = strlen(buffer);
display.setCursor((SCREEN_WIDTH - msg_len * FONT_SIZE * FONT_WIDTH) / 2, SCREEN_HEIGHT / 2 + 2 * FONT_SIZE * FONT_HEIGHT);
display.println(buffer);
display.display();
byte exit_code = 1;
while (exit_code == 1) {
for (byte i = 0; i < 40; i++) {
if (check_button(MIDDLE_BUTTON_INDEX)) {
buttons[MIDDLE_BUTTON_INDEX].executed = true;
redraw();
return;
}
if (check_button(BOTTOM_BUTTON_INDEX)) {
buttons[BOTTOM_BUTTON_INDEX].executed = true;
switch_screen(prev_screen);
redraw();
return;
}
delay(25);
}
exit_code = copy_key(global_key.cur_key, &display);
}
display.fillRect(0, SCREEN_HEIGHT / 2 + 2 * FONT_SIZE * FONT_HEIGHT, SCREEN_WIDTH, FONT_HEIGHT * FONT_SIZE, BLACK);
if (exit_code == 2) {
strcpy_P(buffer, (char *)pgm_read_word_near(&string_arr[17]));
} else {
strcpy_P(buffer, (char *)pgm_read_word_near(&string_arr[16]));
}
msg_len = strlen(buffer);
display.setCursor((SCREEN_WIDTH - msg_len * FONT_SIZE * FONT_WIDTH) / 2, SCREEN_HEIGHT / 2 + 2 * FONT_SIZE * FONT_HEIGHT);
display.println(buffer);
display.display();
delay(2000);
switch_screen(prev_screen);
redraw();
}
void display_screen_bottom_button_pressed(int offset) {
#if DEBUG
Serial.println(F("display_screen_bottom_button_pressed"));
#endif
cur_child = 0;
switch_screen (prev_screen);
redraw();
}
void display_key_screen_draw(int offset) {
#if DEBUG
Serial.println(F("display_screen_draw"));
#endif
strcpy_P(buffer, (char *)pgm_read_word_near(&screens[offset + DISPLAY_SCREEN_NAME_OFFSET]));
byte name_len = strlen(buffer);
display.clearDisplay();
display.setTextSize(FONT_SIZE);
display.setTextColor(WHITE);
display.setCursor((SCREEN_WIDTH - name_len * FONT_SIZE * FONT_WIDTH + 1) / 2, OFFSET_Y + DISPLAY_SCREEN_NAME_Y_OFFSET);
display.println(buffer);
display.setCursor((SCREEN_WIDTH - 16 * FONT_SIZE * FONT_WIDTH) / 2, SCREEN_HEIGHT / 2 + FONT_SIZE * FONT_HEIGHT);
for (byte i = 0; i < 8; i++) {
if (((uint8_t *)&global_key.cur_key)[i] / 16 == 0)
display.print(0);
display.print(((uint8_t *)&global_key.cur_key)[i], HEX);
}
display.display();
}
void display_screen_draw(int offset) {
#if DEBUG
Serial.println(F("display_screen_draw"));
#endif
strcpy_P(buffer, (char *)pgm_read_word_near(&screens[offset + DISPLAY_SCREEN_NAME_OFFSET]));
byte name_len = strlen(buffer);
display.clearDisplay();
display.setTextSize(FONT_SIZE);
display.setTextColor(WHITE);
display.setCursor((SCREEN_WIDTH - name_len * FONT_SIZE * FONT_WIDTH + 1) / 2, OFFSET_Y + DISPLAY_SCREEN_NAME_Y_OFFSET);
display.println(buffer);
byte n_options = (byte)pgm_read_word_near(&screens[offset + DISPLAY_SCREEN_N_OFFSET]);
if (n_options != 0) {
strcpy_P(buffer, (char *)pgm_read_word_near(&screens[offset + DISPLAY_SCREEN_OPTIONS_OFFSET + cur_child]));
byte option_len = strlen(buffer);
strcpy_P(buffer, (char *)pgm_read_word_near(&screens[offset + DISPLAY_SCREEN_SPECIFIER_OFFSET]));
name_len = strlen(buffer);
display.setCursor((SCREEN_WIDTH - (name_len + option_len) * FONT_SIZE * FONT_WIDTH + 1) / 2,
SCREEN_HEIGHT / 2 + 1);
display.println(buffer);
strcpy_P(buffer, (char *)pgm_read_word_near(&screens[offset + DISPLAY_SCREEN_OPTIONS_OFFSET + cur_child]));
display.setCursor((SCREEN_WIDTH - (name_len + option_len) * FONT_SIZE * FONT_WIDTH + 1) / 2 + name_len * FONT_WIDTH * FONT_SIZE,
SCREEN_HEIGHT / 2 + 1);
display.println(buffer);
}
display.display();
}
#pragma endregion
#pragma region KEYS
byte read_key(uint64_t *key) {
return reinterpret_cast<decltype(read_key)*>(pgm_read_word_near(&read_functions[global_key.key_type]))(key);
}
byte copy_key(uint64_t new_key, Adafruit_SSD1306 *display) {
return reinterpret_cast<decltype(copy_key)*>(pgm_read_word_near(©_functions[global_key.key_type]))(new_key, display);
}
void emulate_key(uint64_t key) {
reinterpret_cast<decltype(emulate_key)*>(pgm_read_word_near(&emulate_functions[global_key.key_type]))(key);
}
void save_key(bool original_title) {
int cur_n_keys = EEPROM.readInt(0);