forked from threeme3/usdx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQCX-SSB.ino
3345 lines (3065 loc) · 136 KB
/
QCX-SSB.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
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
// QCX-SSB.ino - https://github.com/threeme3/QCX-SSB
//
// Copyright 2019, 2020 Guido PE1NNZ <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#define VERSION "1.02h"
#define QCX 1 // If you DO NOT have a QCX then comment-out (add two-slashes // in the beginning of this line)
// QCX pin defintions
#define LCD_D4 0 //PD0 (pin 2)
#define LCD_D5 1 //PD1 (pin 3)
#define LCD_D6 2 //PD2 (pin 4)
#define LCD_D7 3 //PD3 (pin 5)
#define LCD_EN 4 //PD4 (pin 6)
#define FREQCNT 5 //PD5 (pin 11)
#define ROT_A 6 //PD6 (pin 12)
#define ROT_B 7 //PD7 (pin 13)
#define RX 8 //PB0 (pin 14)
#define SIDETONE 9 //PB1 (pin 15)
#define KEY_OUT 10 //PB2 (pin 16)
#define SIG_OUT 11 //PB3 (pin 17)
#define DAH 12 //PB4 (pin 18)
#define DIT 13 //PB5 (pin 19)
#define AUDIO1 14 //PC0/A0 (pin 23)
#define AUDIO2 15 //PC1/A1 (pin 24)
#define DVM 16 //PC2/A2 (pin 25)
#define BUTTONS 17 //PC3/A3 (pin 26)
#define LCD_RS 18 //PC4 (pin 27)
#define SDA 18 //PC4 (pin 27)
#define SCL 19 //PC5 (pin 28)
//#define NTX 11 //PB3 (pin 17) - experimental: LOW on TX
/*
// UCX installation: On blank chip, use (standard Arduino Uno) fuse settings (E:FD, H:DE, L:FF), and use customized Optiboot bootloader for 20MHz clock, then upload via serial interface (with RX, TX and DTR lines connected to pin 1, 2, 3 respectively)
// UCX pin defintions
+#define SDA 3 //PD3 (pin 5)
+#define SCL 4 //PD4 (pin 6)
+#define ROT_A 6 //PD6 (pin 12)
+#define ROT_B 7 //PD7 (pin 13)
+#define RX 8 //PB0 (pin 14)
+#define SIDETONE 9 //PB1 (pin 15)
+#define KEY_OUT 10 //PB2 (pin 16)
+#define NTX 11 //PB3 (pin 17)
+#define DAH 12 //PB4 (pin 18)
+#define DIT 13 //PB5 (pin 19)
+#define AUDIO1 14 //PC0/A0 (pin 23)
+#define AUDIO2 15 //PC1/A1 (pin 24)
+#define DVM 16 //PC2/A2 (pin 25)
+#define BUTTONS 17 //PC3/A3 (pin 26)
// In addition set:
#define OLED 1
#define ONEBUTTON 1
#undef DEBUG
adjust I2C and I2C_ ports,
ssb_cap=1; dsp_cap=2;
#define _DELAY() for(uint8_t i = 0; i != 5; i++) asm("nop");
#define F_XTAL 20004000
#define F_CPU F_XTAL
experimentally: #define AUTO_ADC_BIAS 1
*/
#include <avr/sleep.h>
#include <avr/wdt.h>
//FUSES = { .low = 0xFF, .high = 0xD6, .extended = 0xFD }; // Fuse settings should be these at programming.
class LCD : public Print { // inspired by: http://www.technoblogy.com/show?2BET
public: // LCD1602 display in 4-bit mode, RS is pull-up and kept low when idle to prevent potential display RFI via RS line
#define _dn 0 // PD0 to PD3 connect to D4 to D7 on the display
#define _en 4 // PC4 - MUST have pull-up resistor
#define _rs 4 // PC4 - MUST have pull-up resistor
//#define LCD_RS_HI() DDRC &= ~(1 << _rs); // RS high (pull-up)
//#define LCD_RS_LO() DDRC |= 1 << _rs; // RS low (pull-down)
#define LCD_RS_LO() PORTC &= ~(1 << _rs); // RS low
#define LCD_RS_HI() PORTC |= (1 << _rs); // RS high
#define LCD_EN_LO() PORTD &= ~(1 << _en); // EN low
#define LCD_EN_HI() PORTD |= (1 << _en); // EN high
#define LCD_PREP_NIBBLE(b) (PORTD & ~(0xf << _dn)) | (b) << _dn | 1 << _en // Send data and enable high
void begin(uint8_t x = 0, uint8_t y = 0){ // Send command , make sure at least 40ms after power-up before sending commands
bool reinit = (x == 0) && (y == 0);
DDRD |= 0xf << _dn | 1 << _en; // Make data, EN outputs
DDRC |= 1 << _rs;
//PORTC &= ~(1 << _rs); // Set RS low in case to support pull-down when DDRC is output
delayMicroseconds(50000); // *
LCD_RS_LO(); LCD_EN_LO();
cmd(0x33); // Ensures display is in 8-bit mode
delayMicroseconds(4500); cmd(0x33); delayMicroseconds(4500); cmd(0x33); delayMicroseconds(150); // * Ensures display is in 8-bit mode
cmd(0x32); // Puts display in 4-bit mode
cmd(0x28); // * Function set: 2-line, 5x8
cmd(0x0c); // Display on
if(reinit) return;
cmd(0x01); // Clear display
delay(3); // Allow to execute Clear on display [https://www.sparkfun.com/datasheets/LCD/HD44780.pdf, p.49, p58]
cmd(0x06); // * Entrymode: left, shift-dec
}
void nib(uint8_t b){ // Send four bit nibble to display
PORTD = LCD_PREP_NIBBLE(b); // Send data and enable high
//asm("nop"); // Enable high pulse width must be at least 230ns high, data-setup time 80ns
delayMicroseconds(4);
LCD_EN_LO();
//delayMicroseconds(52); // Execution time
delayMicroseconds(60); // Execution time
}
void cmd(uint8_t b){ nib(b >> 4); nib(b & 0xf); }// Write command: send nibbles while RS low
size_t write(uint8_t b){ // Write data: send nibbles while RS high
//LCD_EN_HI(); // Complete Enable cycle must be at least 500ns (so start early)
uint8_t nibh = LCD_PREP_NIBBLE(b >> 4); // Prepare high nibble data and enable high
PORTD = nibh; // Send high nibble data and enable high
uint8_t nibl = LCD_PREP_NIBBLE(b & 0xf); // Prepare low nibble data and enable high
//asm("nop"); // Enable high pulse width must be at least 230ns high, data-setup time 80ns; ATMEGA clock-cycle is 50ns (so at least 5 cycles)
LCD_RS_HI();
LCD_EN_LO();
PORTD = nibl; // Send low nibble data and enable high
LCD_RS_LO();
//asm("nop"); asm("nop"); // Complete Enable cycle must be at least 500ns
//PORTD = nibl; // Send low nibble data and enable high
//asm("nop"); // Enable high pulse width must be at least 230ns high, data-setup time 80ns; ATMEGA clock-cycle is 50ns (so at least 5 cycles)
LCD_RS_HI();
LCD_EN_LO();
LCD_RS_LO();
delayMicroseconds(60); // Execution time (37+4)*1.25 us
PORTD |= 0x02; // To support serial-interface keep LCD_D5 high, so that DVM is not pulled-down via D
return 1;
}
void setCursor(uint8_t x, uint8_t y){ cmd(0x80 | (x + y * 0x40)); }
void cursor(){ cmd(0x0e); }
void noCursor(){ cmd(0x0c); }
void noDisplay(){ cmd(0x08); }
void createChar(uint8_t l, uint8_t glyph[]){ cmd(0x40 | ((l & 0x7) << 3)); for(int i = 0; i != 8; i++) write(glyph[i]); }
};
/*
class LCD : public Print { // inspired by: http://www.technoblogy.com/show?2BET
public: // LCD1602 display in 4-bit mode, RS is pull-up and kept low when idle to prevent potential display RFI via RS line
#define _dn 0 // PD0 to PD3 connect to D4 to D7 on the display
#define _en 4 // PC4 - MUST have pull-up resistor
#define _rs 4 // PC4 - MUST have pull-up resistor
#define LCD_RS_HI() DDRC &= ~(1 << _rs); // RS high (pull-up)
#define LCD_RS_LO() DDRC |= 1 << _rs; // RS low (pull-down)
#define LCD_EN_LO() PORTD &= ~(1 << _en); // EN low
#define LCD_PREP_NIBBLE(b) (PORTD & ~(0xf << _dn)) | (b) << _dn | 1 << _en // Send data and enable high
void begin(uint8_t x, uint8_t y){ // Send command
DDRD |= 0xf << _dn | 1 << _en; // Make data, EN and RS pins outputs
PORTC &= ~(1 << _rs); // Set RS low in case to support pull-down when DDRC is output
delayMicroseconds(50000); // * At least 40ms after power rises above 2.7V before sending commands
LCD_RS_LO();
cmd(0x33); // Ensures display is in 8-bit mode
cmd(0x32); // Puts display in 4-bit mode
cmd(0x0e); // Display and cursor on
cmd(0x01); // Clear display
delay(3); // Allow to execute on display [https://www.sparkfun.com/datasheets/LCD/HD44780.pdf, p.49, p58]
}
void nib(uint8_t b){ // Send four bit nibble to display
PORTD = LCD_PREP_NIBBLE(b); // Send data and enable high
delayMicroseconds(4);
LCD_EN_LO();
delayMicroseconds(60); // Execution time (was: 37)
}
void cmd(uint8_t b){ nib(b >> 4); nib(b & 0xf); }// Write command: send nibbles while RS low
size_t write(uint8_t b){ // Write data: send nibbles while RS high
uint8_t nibh = LCD_PREP_NIBBLE(b >> 4); // Prepare high nibble data and enable high
uint8_t nibl = LCD_PREP_NIBBLE(b & 0xf); // Prepare low nibble data and enable high
PORTD = nibh; // Send high nibble data and enable high
LCD_RS_HI();
LCD_EN_LO();
PORTD = nibl; // Send low nibble data and enable high
LCD_RS_LO();
LCD_RS_HI();
LCD_EN_LO();
LCD_RS_LO();
delayMicroseconds(41); // Execution time
PORTD |= 0x02; // To support serial-interface keep LCD_D5 high, so that DVM is not pulled-down via D
return 1;
}
void setCursor(uint8_t x, uint8_t y){ cmd(0x80 | (x + y * 0x40)); }
void cursor(){ cmd(0x0e); }
void noCursor(){ cmd(0x0c); }
void noDisplay(){ cmd(0x08); }
void createChar(uint8_t l, uint8_t glyph[]){ cmd(0x40 | ((l & 0x7) << 3)); for(int i = 0; i != 8; i++) write(glyph[i]); }
};
*/
#include <LiquidCrystal.h>
class LCD_ : public LiquidCrystal {
public: // QCXLiquidCrystal extends LiquidCrystal library for pull-up driven LCD_RS, as done on QCX. LCD_RS needs to be set to LOW in advance of calling any operation.
//LCD_(uint8_t rs = LCD_RS, uint8_t en = LCD_EN, uint8_t d4 = LCD_D4, uint8_t d5, = LCD_D5 uint8_t d6 = LCD_D6, uint8_t d7 = LCD_D7) : LiquidCrystal(rs, en, d4, d5, d6, d7){ };
LCD_() : LiquidCrystal(LCD_RS, LCD_EN, LCD_D4, LCD_D5, LCD_D6, LCD_D7){ };
virtual size_t write(uint8_t value){ // overwrites LiquidCrystal::write() and re-implements LCD data writes
pinMode(LCD_RS, INPUT); // pull-up LCD_RS
write4bits(value >> 4);
write4bits(value);
pinMode(LCD_RS, OUTPUT); // pull-down LCD_RS
return 1;
};
void write4bits(uint8_t value){
digitalWrite(LCD_D4, (value >> 0) & 0x01);
digitalWrite(LCD_D5, (value >> 1) & 0x01);
digitalWrite(LCD_D6, (value >> 2) & 0x01);
digitalWrite(LCD_D7, (value >> 3) & 0x01);
digitalWrite(LCD_EN, LOW); // pulseEnable
delayMicroseconds(1);
digitalWrite(LCD_EN, HIGH);
delayMicroseconds(1); // enable pulse must be >450ns
digitalWrite(LCD_EN, LOW);
delayMicroseconds(100); // commands need > 37us to settle
};
};
// I2C class used by SSD1306 driver; you may connect a SSD1306 (128x32) display on LCD header pins: 1 (GND); 2 (VCC); 13 (SDA); 14 (SCL)
class I2C_ { // direct port I/O (disregards/does-not-need pull-ups)
public:
#define _DELAY() for(uint8_t i = 0; i != 4; i++) asm("nop"); // 4=731kb/s
#define _I2C_SDA (1<<2) // PD2
#define _I2C_SCL (1<<3) // PD3
#define _I2C_INIT() _I2C_SDA_HI(); _I2C_SCL_HI(); DDRD |= (_I2C_SDA | _I2C_SCL);
#define _I2C_SDA_HI() PORTD |= _I2C_SDA; _DELAY();
#define _I2C_SDA_LO() PORTD &= ~_I2C_SDA; _DELAY();
#define _I2C_SCL_HI() PORTD |= _I2C_SCL; _DELAY();
#define _I2C_SCL_LO() PORTD &= ~_I2C_SCL; _DELAY();
#define _I2C_START() _I2C_SDA_LO(); _I2C_SCL_LO(); _I2C_SDA_HI();
#define _I2C_STOP() _I2C_SCL_HI(); _I2C_SDA_HI();
#define _I2C_SUSPEND() //_I2C_SDA_LO(); // SDA_LO to allow re-use as output port
#define _SendBit(data, bit) \
if(data & 1 << bit){ \
_I2C_SDA_HI(); \
} else { \
_I2C_SDA_LO(); \
} \
_I2C_SCL_HI(); \
_I2C_SCL_LO();
inline void start(){ _I2C_INIT(); _I2C_START(); };
inline void stop() { _I2C_STOP(); _I2C_SUSPEND(); };
inline void SendByte(uint8_t data){
_SendBit(data, 7);
_SendBit(data, 6);
_SendBit(data, 5);
_SendBit(data, 4);
_SendBit(data, 3);
_SendBit(data, 2);
_SendBit(data, 1);
_SendBit(data, 0);
_I2C_SDA_HI(); // recv ACK
_DELAY(); //
_I2C_SCL_HI();
_I2C_SCL_LO();
}
void SendRegister(uint8_t addr, uint8_t* data, uint8_t n){
start();
SendByte(addr << 1);
while(n--) SendByte(*data++);
stop();
}
//void SendRegister(uint8_t addr, uint8_t val){ SendRegister(addr, &val, 1); }
void begin(){};
void beginTransmission(uint8_t addr){ start(); SendByte(addr << 1); };
bool write(uint8_t byte){ SendByte(byte); return 1; };
uint8_t endTransmission(){ stop(); return 0; };
};
I2C_ Wire;
//#include <Wire.h>
/* // 6x8 technoblogy font
const uint8_t font[]PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x5F, 0x00, 0x00, 0x00,
0x00, 0x07, 0x00, 0x07, 0x00, 0x00,
0x14, 0x7F, 0x14, 0x7F, 0x14, 0x00,
0x24, 0x2A, 0x7F, 0x2A, 0x12, 0x00,
0x23, 0x13, 0x08, 0x64, 0x62, 0x00,
0x36, 0x49, 0x56, 0x20, 0x50, 0x00,
0x00, 0x08, 0x07, 0x03, 0x00, 0x00,
0x00, 0x1C, 0x22, 0x41, 0x00, 0x00,
0x00, 0x41, 0x22, 0x1C, 0x00, 0x00,
0x2A, 0x1C, 0x7F, 0x1C, 0x2A, 0x00,
0x08, 0x08, 0x3E, 0x08, 0x08, 0x00,
0x00, 0x80, 0x70, 0x30, 0x00, 0x00,
0x08, 0x08, 0x08, 0x08, 0x08, 0x00,
0x00, 0x00, 0x60, 0x60, 0x00, 0x00,
0x20, 0x10, 0x08, 0x04, 0x02, 0x00,
0x3E, 0x51, 0x49, 0x45, 0x3E, 0x00,
0x00, 0x42, 0x7F, 0x40, 0x00, 0x00,
0x72, 0x49, 0x49, 0x49, 0x46, 0x00,
0x21, 0x41, 0x49, 0x4D, 0x33, 0x00,
0x18, 0x14, 0x12, 0x7F, 0x10, 0x00,
0x27, 0x45, 0x45, 0x45, 0x39, 0x00,
0x3C, 0x4A, 0x49, 0x49, 0x31, 0x00,
0x41, 0x21, 0x11, 0x09, 0x07, 0x00,
0x36, 0x49, 0x49, 0x49, 0x36, 0x00,
0x46, 0x49, 0x49, 0x29, 0x1E, 0x00,
0x00, 0x00, 0x14, 0x00, 0x00, 0x00,
0x00, 0x40, 0x34, 0x00, 0x00, 0x00,
0x00, 0x08, 0x14, 0x22, 0x41, 0x00,
0x14, 0x14, 0x14, 0x14, 0x14, 0x00,
0x00, 0x41, 0x22, 0x14, 0x08, 0x00,
0x02, 0x01, 0x59, 0x09, 0x06, 0x00,
0x3E, 0x41, 0x5D, 0x59, 0x4E, 0x00,
0x7C, 0x12, 0x11, 0x12, 0x7C, 0x00,
0x7F, 0x49, 0x49, 0x49, 0x36, 0x00,
0x3E, 0x41, 0x41, 0x41, 0x22, 0x00,
0x7F, 0x41, 0x41, 0x41, 0x3E, 0x00,
0x7F, 0x49, 0x49, 0x49, 0x41, 0x00,
0x7F, 0x09, 0x09, 0x09, 0x01, 0x00,
0x3E, 0x41, 0x41, 0x51, 0x73, 0x00,
0x7F, 0x08, 0x08, 0x08, 0x7F, 0x00,
0x00, 0x41, 0x7F, 0x41, 0x00, 0x00,
0x20, 0x40, 0x41, 0x3F, 0x01, 0x00,
0x7F, 0x08, 0x14, 0x22, 0x41, 0x00,
0x7F, 0x40, 0x40, 0x40, 0x40, 0x00,
0x7F, 0x02, 0x1C, 0x02, 0x7F, 0x00,
0x7F, 0x04, 0x08, 0x10, 0x7F, 0x00,
0x3E, 0x41, 0x41, 0x41, 0x3E, 0x00,
0x7F, 0x09, 0x09, 0x09, 0x06, 0x00,
0x3E, 0x41, 0x51, 0x21, 0x5E, 0x00,
0x7F, 0x09, 0x19, 0x29, 0x46, 0x00,
0x26, 0x49, 0x49, 0x49, 0x32, 0x00,
0x03, 0x01, 0x7F, 0x01, 0x03, 0x00,
0x3F, 0x40, 0x40, 0x40, 0x3F, 0x00,
0x1F, 0x20, 0x40, 0x20, 0x1F, 0x00,
0x3F, 0x40, 0x38, 0x40, 0x3F, 0x00,
0x63, 0x14, 0x08, 0x14, 0x63, 0x00,
0x03, 0x04, 0x78, 0x04, 0x03, 0x00,
0x61, 0x59, 0x49, 0x4D, 0x43, 0x00,
0x00, 0x7F, 0x41, 0x41, 0x41, 0x00,
0x02, 0x04, 0x08, 0x10, 0x20, 0x00,
0x00, 0x41, 0x41, 0x41, 0x7F, 0x00,
0x04, 0x02, 0x01, 0x02, 0x04, 0x00,
0x40, 0x40, 0x40, 0x40, 0x40, 0x00,
0x00, 0x03, 0x07, 0x08, 0x00, 0x00,
0x20, 0x54, 0x54, 0x78, 0x40, 0x00,
0x7F, 0x28, 0x44, 0x44, 0x38, 0x00,
0x38, 0x44, 0x44, 0x44, 0x28, 0x00,
0x38, 0x44, 0x44, 0x28, 0x7F, 0x00,
0x38, 0x54, 0x54, 0x54, 0x18, 0x00,
0x00, 0x08, 0x7E, 0x09, 0x02, 0x00,
0x18, 0xA4, 0xA4, 0x9C, 0x78, 0x00,
0x7F, 0x08, 0x04, 0x04, 0x78, 0x00,
0x00, 0x44, 0x7D, 0x40, 0x00, 0x00,
0x20, 0x40, 0x40, 0x3D, 0x00, 0x00,
0x7F, 0x10, 0x28, 0x44, 0x00, 0x00,
0x00, 0x41, 0x7F, 0x40, 0x00, 0x00,
0x7C, 0x04, 0x78, 0x04, 0x78, 0x00,
0x7C, 0x08, 0x04, 0x04, 0x78, 0x00,
0x38, 0x44, 0x44, 0x44, 0x38, 0x00,
0xFC, 0x18, 0x24, 0x24, 0x18, 0x00,
0x18, 0x24, 0x24, 0x18, 0xFC, 0x00,
0x7C, 0x08, 0x04, 0x04, 0x08, 0x00,
0x48, 0x54, 0x54, 0x54, 0x24, 0x00,
0x04, 0x04, 0x3F, 0x44, 0x24, 0x00,
0x3C, 0x40, 0x40, 0x20, 0x7C, 0x00,
0x1C, 0x20, 0x40, 0x20, 0x1C, 0x00,
0x3C, 0x40, 0x30, 0x40, 0x3C, 0x00,
0x44, 0x28, 0x10, 0x28, 0x44, 0x00,
0x4C, 0x90, 0x90, 0x90, 0x7C, 0x00,
0x44, 0x64, 0x54, 0x4C, 0x44, 0x00,
0x00, 0x08, 0x36, 0x41, 0x00, 0x00,
0x00, 0x00, 0x77, 0x00, 0x00, 0x00,
0x00, 0x41, 0x36, 0x08, 0x00, 0x00,
0x02, 0x01, 0x02, 0x04, 0x02, 0x00,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00 };
#define FONT_W 12//6
#define FONT_H 2
#define FONT_STRETCHV 1
#define FONT_STRETCHH 1//0
*/
// C64 real
const uint8_t font[]PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x4f, 0x4f, 0x00, 0x00, 0x00,
0x00, 0x07, 0x07, 0x00, 0x00, 0x07, 0x07, 0x00,
0x14, 0x7f, 0x7f, 0x14, 0x14, 0x7f, 0x7f, 0x14,
0x00, 0x24, 0x2e, 0x6b, 0x6b, 0x3a, 0x12, 0x00,
0x00, 0x63, 0x33, 0x18, 0x0c, 0x66, 0x63, 0x00,
0x00, 0x32, 0x7f, 0x4d, 0x4d, 0x77, 0x72, 0x50,
0x00, 0x00, 0x00, 0x04, 0x06, 0x03, 0x01, 0x00,
0x00, 0x00, 0x1c, 0x3e, 0x63, 0x41, 0x00, 0x00,
0x00, 0x00, 0x41, 0x63, 0x3e, 0x1c, 0x00, 0x00,
0x08, 0x2a, 0x3e, 0x1c, 0x1c, 0x3e, 0x2a, 0x08,
0x00, 0x08, 0x08, 0x3e, 0x3e, 0x08, 0x08, 0x00,
0x00, 0x00, 0x80, 0xe0, 0x60, 0x00, 0x00, 0x00,
0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00,
0x00, 0x00, 0x00, 0x60, 0x60, 0x00, 0x00, 0x00,
0x00, 0x40, 0x60, 0x30, 0x18, 0x0c, 0x06, 0x02,
0x00, 0x3e, 0x7f, 0x49, 0x45, 0x7f, 0x3e, 0x00,
0x00, 0x40, 0x44, 0x7f, 0x7f, 0x40, 0x40, 0x00,
0x00, 0x62, 0x73, 0x51, 0x49, 0x4f, 0x46, 0x00,
0x00, 0x22, 0x63, 0x49, 0x49, 0x7f, 0x36, 0x00,
0x00, 0x18, 0x18, 0x14, 0x16, 0x7f, 0x7f, 0x10,
0x00, 0x27, 0x67, 0x45, 0x45, 0x7d, 0x39, 0x00,
0x00, 0x3e, 0x7f, 0x49, 0x49, 0x7b, 0x32, 0x00,
0x00, 0x03, 0x03, 0x79, 0x7d, 0x07, 0x03, 0x00,
0x00, 0x36, 0x7f, 0x49, 0x49, 0x7f, 0x36, 0x00,
0x00, 0x26, 0x6f, 0x49, 0x49, 0x7f, 0x3e, 0x00,
0x00, 0x00, 0x00, 0x24, 0x24, 0x00, 0x00, 0x00,
0x00, 0x00, 0x80, 0xe4, 0x64, 0x00, 0x00, 0x00,
0x00, 0x08, 0x1c, 0x36, 0x63, 0x41, 0x41, 0x00,
0x00, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x00,
0x00, 0x41, 0x41, 0x63, 0x36, 0x1c, 0x08, 0x00,
0x00, 0x02, 0x03, 0x51, 0x59, 0x0f, 0x06, 0x00,
0x00, 0x3e, 0x7f, 0x41, 0x4d, 0x4f, 0x2e, 0x00,
0x00, 0x7c, 0x7e, 0x0b, 0x0b, 0x7e, 0x7c, 0x00,
0x00, 0x7f, 0x7f, 0x49, 0x49, 0x7f, 0x36, 0x00,
0x00, 0x3e, 0x7f, 0x41, 0x41, 0x63, 0x22, 0x00,
0x00, 0x7f, 0x7f, 0x41, 0x63, 0x3e, 0x1c, 0x00,
0x00, 0x7f, 0x7f, 0x49, 0x49, 0x41, 0x41, 0x00,
0x00, 0x7f, 0x7f, 0x09, 0x09, 0x01, 0x01, 0x00,
0x00, 0x3e, 0x7f, 0x41, 0x49, 0x7b, 0x3a, 0x00,
0x00, 0x7f, 0x7f, 0x08, 0x08, 0x7f, 0x7f, 0x00,
0x00, 0x00, 0x41, 0x7f, 0x7f, 0x41, 0x00, 0x00,
0x00, 0x20, 0x60, 0x41, 0x7f, 0x3f, 0x01, 0x00,
0x00, 0x7f, 0x7f, 0x1c, 0x36, 0x63, 0x41, 0x00,
0x00, 0x7f, 0x7f, 0x40, 0x40, 0x40, 0x40, 0x00,
0x00, 0x7f, 0x7f, 0x06, 0x0c, 0x06, 0x7f, 0x7f,
0x00, 0x7f, 0x7f, 0x0e, 0x1c, 0x7f, 0x7f, 0x00,
0x00, 0x3e, 0x7f, 0x41, 0x41, 0x7f, 0x3e, 0x00,
0x00, 0x7f, 0x7f, 0x09, 0x09, 0x0f, 0x06, 0x00,
0x00, 0x1e, 0x3f, 0x21, 0x61, 0x7f, 0x5e, 0x00,
0x00, 0x7f, 0x7f, 0x19, 0x39, 0x6f, 0x46, 0x00,
0x00, 0x26, 0x6f, 0x49, 0x49, 0x7b, 0x32, 0x00,
0x00, 0x01, 0x01, 0x7f, 0x7f, 0x01, 0x01, 0x00,
0x00, 0x3f, 0x7f, 0x40, 0x40, 0x7f, 0x3f, 0x00,
0x00, 0x1f, 0x3f, 0x60, 0x60, 0x3f, 0x1f, 0x00,
0x00, 0x7f, 0x7f, 0x30, 0x18, 0x30, 0x7f, 0x7f,
0x00, 0x63, 0x77, 0x1c, 0x1c, 0x77, 0x63, 0x00,
0x00, 0x07, 0x0f, 0x78, 0x78, 0x0f, 0x07, 0x00,
0x00, 0x61, 0x71, 0x59, 0x4d, 0x47, 0x43, 0x00,
0x00, 0x00, 0x7f, 0x7f, 0x41, 0x41, 0x00, 0x00,
0x00, 0x02, 0x06, 0x0c, 0x18, 0x30, 0x60, 0x40,
0x00, 0x00, 0x41, 0x41, 0x7f, 0x7f, 0x00, 0x00,
0x00, 0x08, 0x0c, 0xfe, 0xfe, 0x0c, 0x08, 0x00,
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
0x00, 0x01, 0x03, 0x06, 0x04, 0x00, 0x00, 0x00,
0x00, 0x20, 0x74, 0x54, 0x54, 0x7c, 0x78, 0x00,
0x00, 0x7e, 0x7e, 0x48, 0x48, 0x78, 0x30, 0x00,
0x00, 0x38, 0x7c, 0x44, 0x44, 0x44, 0x00, 0x00,
0x00, 0x30, 0x78, 0x48, 0x48, 0x7e, 0x7e, 0x00,
0x00, 0x38, 0x7c, 0x54, 0x54, 0x5c, 0x18, 0x00,
0x00, 0x00, 0x08, 0x7c, 0x7e, 0x0a, 0x0a, 0x00,
0x00, 0x98, 0xbc, 0xa4, 0xa4, 0xfc, 0x7c, 0x00,
0x00, 0x7e, 0x7e, 0x08, 0x08, 0x78, 0x70, 0x00,
0x00, 0x00, 0x48, 0x7a, 0x7a, 0x40, 0x00, 0x00,
0x00, 0x00, 0x80, 0x80, 0x80, 0xfa, 0x7a, 0x00,
0x00, 0x7e, 0x7e, 0x10, 0x38, 0x68, 0x40, 0x00,
0x00, 0x00, 0x42, 0x7e, 0x7e, 0x40, 0x00, 0x00,
0x00, 0x7c, 0x7c, 0x18, 0x38, 0x1c, 0x7c, 0x78,
0x00, 0x7c, 0x7c, 0x04, 0x04, 0x7c, 0x78, 0x00,
0x00, 0x38, 0x7c, 0x44, 0x44, 0x7c, 0x38, 0x00,
0x00, 0xfc, 0xfc, 0x24, 0x24, 0x3c, 0x18, 0x00,
0x00, 0x18, 0x3c, 0x24, 0x24, 0xfc, 0xfc, 0x00,
0x00, 0x7c, 0x7c, 0x04, 0x04, 0x0c, 0x08, 0x00,
0x00, 0x48, 0x5c, 0x54, 0x54, 0x74, 0x24, 0x00,
0x00, 0x04, 0x04, 0x3e, 0x7e, 0x44, 0x44, 0x00,
0x00, 0x3c, 0x7c, 0x40, 0x40, 0x7c, 0x7c, 0x00,
0x00, 0x1c, 0x3c, 0x60, 0x60, 0x3c, 0x1c, 0x00,
0x00, 0x1c, 0x7c, 0x70, 0x38, 0x70, 0x7c, 0x1c,
0x00, 0x44, 0x6c, 0x38, 0x38, 0x6c, 0x44, 0x00,
0x00, 0x9c, 0xbc, 0xa0, 0xe0, 0x7c, 0x3c, 0x00,
0x00, 0x44, 0x64, 0x74, 0x5c, 0x4c, 0x44, 0x00,
0x00, 0x08, 0x3e, 0x77, 0x41, 0x41, 0x00, 0x00,
0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00,
0x00, 0x00, 0x41, 0x41, 0x77, 0x3e, 0x08, 0x00,
0x00, 0x04, 0x02, 0x02, 0x04, 0x04, 0x02, 0x00
};
#define FONT_W 8
#define FONT_H 2
#define FONT_STRETCHV 1
#define FONT_STRETCHH 0
/* //16x8 C-64 kind of
const uint8_t font[]PROGMEM = {
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // 0x20
0x00,0x06,0x5F,0x5F,0x06,0x00,0x00,0x00, // 0x21
0x00,0x07,0x07,0x00,0x07,0x07,0x00,0x00, // 0x22
0x14,0x7F,0x7F,0x14,0x7F,0x7F,0x14,0x00, // 0x23
0x24,0x2E,0x6B,0x6B,0x3A,0x12,0x00,0x00, // 0x24
0x46,0x66,0x30,0x18,0x0C,0x66,0x62,0x00, // 0x25
0x30,0x7A,0x4F,0x5D,0x37,0x7A,0x48,0x00, // 0x26
0x04,0x07,0x03,0x00,0x00,0x00,0x00,0x00, // 0x27
0x00,0x1C,0x3E,0x63,0x41,0x00,0x00,0x00, // 0x28
0x00,0x41,0x63,0x3E,0x1C,0x00,0x00,0x00, // 0x29
0x08,0x2A,0x3E,0x1C,0x1C,0x3E,0x2A,0x08, // 0x2A
0x08,0x08,0x3E,0x3E,0x08,0x08,0x00,0x00, // 0x2B
0x00,0xA0,0xE0,0x60,0x00,0x00,0x00,0x00, // 0x2C
0x08,0x08,0x08,0x08,0x08,0x08,0x00,0x00, // 0x2D
0x00,0x00,0x60,0x60,0x00,0x00,0x00,0x00, // 0x2E
0x60,0x30,0x18,0x0C,0x06,0x03,0x01,0x00, // 0x2F
0x3E,0x7F,0x59,0x4D,0x7F,0x3E,0x00,0x00, // 0x30
0x42,0x42,0x7F,0x7F,0x40,0x40,0x00,0x00, // 0x31
0x62,0x73,0x59,0x49,0x6F,0x66,0x00,0x00, // 0x32
0x22,0x63,0x49,0x49,0x7F,0x36,0x00,0x00, // 0x33
0x18,0x1C,0x16,0x13,0x7F,0x7F,0x10,0x00, // 0x34
0x27,0x67,0x45,0x45,0x7D,0x39,0x00,0x00, // 0x35
0x3C,0x7E,0x4B,0x49,0x79,0x30,0x00,0x00, // 0x36
0x03,0x63,0x71,0x19,0x0F,0x07,0x00,0x00, // 0x37
0x36,0x7F,0x49,0x49,0x7F,0x36,0x00,0x00, // 0x38
0x06,0x4F,0x49,0x69,0x3F,0x1E,0x00,0x00, // 0x39
0x00,0x00,0x6C,0x6C,0x00,0x00,0x00,0x00, // 0x3A
0x00,0xA0,0xEC,0x6C,0x00,0x00,0x00,0x00, // 0x3B
0x08,0x1C,0x36,0x63,0x41,0x00,0x00,0x00, // 0x3C
0x14,0x14,0x14,0x14,0x14,0x14,0x00,0x00, // 0x3D
0x00,0x41,0x63,0x36,0x1C,0x08,0x00,0x00, // 0x3E
0x02,0x03,0x51,0x59,0x0F,0x06,0x00,0x00, // 0x3F
0x3E,0x7F,0x41,0x5D,0x5D,0x1F,0x1E,0x00, // 0x40
0x7C,0x7E,0x13,0x13,0x7E,0x7C,0x00,0x00, // 0x41
0x41,0x7F,0x7F,0x49,0x49,0x7F,0x36,0x00, // 0x42
0x1C,0x3E,0x63,0x41,0x41,0x63,0x22,0x00, // 0x43
0x41,0x7F,0x7F,0x41,0x63,0x7F,0x1C,0x00, // 0x44
0x41,0x7F,0x7F,0x49,0x5D,0x41,0x63,0x00, // 0x45
0x41,0x7F,0x7F,0x49,0x1D,0x01,0x03,0x00, // 0x46
0x1C,0x3E,0x63,0x41,0x51,0x73,0x72,0x00, // 0x47
0x7F,0x7F,0x08,0x08,0x7F,0x7F,0x00,0x00, // 0x48
0x00,0x41,0x7F,0x7F,0x41,0x00,0x00,0x00, // 0x49
0x30,0x70,0x40,0x41,0x7F,0x3F,0x01,0x00, // 0x4A
0x41,0x7F,0x7F,0x08,0x1C,0x77,0x63,0x00, // 0x4B
0x41,0x7F,0x7F,0x41,0x40,0x60,0x70,0x00, // 0x4C
0x7F,0x7F,0x06,0x0C,0x06,0x7F,0x7F,0x00, // 0x4D
0x7F,0x7F,0x06,0x0C,0x18,0x7F,0x7F,0x00, // 0x4E
0x1C,0x3E,0x63,0x41,0x63,0x3E,0x1C,0x00, // 0x4F
0x41,0x7F,0x7F,0x49,0x09,0x0F,0x06,0x00, // 0x50
0x1E,0x3F,0x21,0x71,0x7F,0x5E,0x00,0x00, // 0x51
0x41,0x7F,0x7F,0x19,0x39,0x6F,0x46,0x00, // 0x52
0x26,0x67,0x4D,0x59,0x7B,0x32,0x00,0x00, // 0x53
0x03,0x41,0x7F,0x7F,0x41,0x03,0x00,0x00, // 0x54
0x7F,0x7F,0x40,0x40,0x7F,0x7F,0x00,0x00, // 0x55
0x1F,0x3F,0x60,0x60,0x3F,0x1F,0x00,0x00, // 0x56
0x7F,0x7F,0x30,0x18,0x30,0x7F,0x7F,0x00, // 0x57
0x63,0x77,0x1C,0x08,0x1C,0x77,0x63,0x00, // 0x58
0x07,0x4F,0x78,0x78,0x4F,0x07,0x00,0x00, // 0x59
0x67,0x73,0x59,0x4D,0x47,0x63,0x71,0x00, // 0x5A
0x00,0x7F,0x7F,0x41,0x41,0x00,0x00,0x00, // 0x5B
0x01,0x03,0x06,0x0C,0x18,0x30,0x60,0x00, // 0x5C
0x00,0x41,0x41,0x7F,0x7F,0x00,0x00,0x00, // 0x5D
0x08,0x0C,0x06,0x03,0x06,0x0C,0x08,0x00, // 0x5E
0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80, // 0x5F
0x00,0x00,0x03,0x07,0x04,0x00,0x00,0x00, // 0x60
0x20,0x74,0x54,0x54,0x3C,0x78,0x40,0x00, // 0x61
0x41,0x3F,0x7F,0x44,0x44,0x7C,0x38,0x00, // 0x62
0x38,0x7C,0x44,0x44,0x6C,0x28,0x00,0x00, // 0x63
0x30,0x78,0x48,0x49,0x3F,0x7F,0x40,0x00, // 0x64
0x38,0x7C,0x54,0x54,0x5C,0x18,0x00,0x00, // 0x65
0x48,0x7E,0x7F,0x49,0x03,0x02,0x00,0x00, // 0x66
0x98,0xBC,0xA4,0xA4,0xF8,0x7C,0x04,0x00, // 0x67
0x41,0x7F,0x7F,0x08,0x04,0x7C,0x78,0x00, // 0x68
0x00,0x44,0x7D,0x7D,0x40,0x00,0x00,0x00, // 0x69
0x40,0xC4,0x84,0xFD,0x7D,0x00,0x00,0x00, // 0x6A
0x41,0x7F,0x7F,0x10,0x38,0x6C,0x44,0x00, // 0x6B
0x00,0x41,0x7F,0x7F,0x40,0x00,0x00,0x00, // 0x6C
0x7C,0x7C,0x0C,0x18,0x0C,0x7C,0x78,0x00, // 0x6D
0x7C,0x7C,0x04,0x04,0x7C,0x78,0x00,0x00, // 0x6E
0x38,0x7C,0x44,0x44,0x7C,0x38,0x00,0x00, // 0x6F
0x84,0xFC,0xF8,0xA4,0x24,0x3C,0x18,0x00, // 0x70
0x18,0x3C,0x24,0xA4,0xF8,0xFC,0x84,0x00, // 0x71
0x44,0x7C,0x78,0x44,0x1C,0x18,0x00,0x00, // 0x72
0x48,0x5C,0x54,0x54,0x74,0x24,0x00,0x00, // 0x73
0x00,0x04,0x3E,0x7F,0x44,0x24,0x00,0x00, // 0x74
0x3C,0x7C,0x40,0x40,0x3C,0x7C,0x40,0x00, // 0x75
0x1C,0x3C,0x60,0x60,0x3C,0x1C,0x00,0x00, // 0x76
0x3C,0x7C,0x60,0x30,0x60,0x7C,0x3C,0x00, // 0x77
0x44,0x6C,0x38,0x10,0x38,0x6C,0x44,0x00, // 0x78
0x9C,0xBC,0xA0,0xA0,0xFC,0x7C,0x00,0x00, // 0x79
0x4C,0x64,0x74,0x5C,0x4C,0x64,0x00,0x00, // 0x7A
0x08,0x08,0x3E,0x77,0x41,0x41,0x00,0x00, // 0x7B
0x00,0x00,0x00,0x77,0x77,0x00,0x00,0x00, // 0x7C
0x41,0x41,0x77,0x3E,0x08,0x08,0x00,0x00, // 0x7D
0x02,0x03,0x01,0x03,0x02,0x03,0x01,0x00, // 0x7E
0x78,0x7C,0x46,0x43,0x46,0x7C,0x78,0x00}; // 0x7F
#define FONT_W 8
#define FONT_H 2
#define FONT_STRETCHV 1
#define FONT_STRETCHH 0
*/
#define BRIGHT 1
static const uint8_t ssd1306_init_sequence [] PROGMEM = { // Initialization Sequence
// 0xAE, // Display OFF (sleep mode)
0x20, 0b10, // Set Memory Addressing Mode
// 00=Horizontal Addressing Mode; 01=Vertical Addressing Mode;
// 10=Page Addressing Mode (RESET); 11=Invalid
0xB0, // Set Page Start Address for Page Addressing Mode, 0-7
0xC8, // Set COM Output Scan Direction. Flip Veritically.
0x00, // Set low nibble of column address
0x10, // Set high nibble of column address
0x40, // Set display start line address
#ifdef BRIGHT
0x81, /*32*/ 0x7F, // Set contrast control register
#else
0x81, 32, // Set contrast control register
#endif
0xA1, // Set Segment Re-map. A0=column 0 mapped to SEG0; A1=column 127 mapped to SEG0. Flip Horizontally
0xA6, // Set display mode. A6=Normal; A7=Inverse
0xA8, 0x1F, // Set multiplex ratio(1 to 64)
0xA4, // Output RAM to Display
// 0xA4=Output follows RAM content; 0xA5,Output ignores RAM content
0xD3, 0x00, // Set display offset. 00 = no offset
0xD5, 0x80, // --set display clock divide ratio/oscillator frequency
#ifdef BRIGHT
0xD9, 0xF1, // 0xF1=brighter //0x22, // Set pre-charge period
#else
0xD9, 0x22, // Set pre-charge period
#endif
0xDA, 0x02, // Set com pins hardware configuration
// 0xDB, 0x40, //0x20, // --set vcomh 0x20 = 0.77xVcc
0x8D, 0x14, // Set DC-DC enable
0xAF, // Display ON
};
class SSD1306Device: public Print {
public:
#define SSD1306_ADDR 0x3C // Slave address
#define SSD1306_PAGES 4
#define SSD1306_COMMAND 0x00
#define SSD1306_DATA 0x40
uint8_t oledX = 0, oledY = 0;
uint8_t renderingFrame = 0xB0;
bool wrap = false;
void begin(uint8_t cols, uint8_t rows, uint8_t charsize = 0){
Wire.begin();
Wire.beginTransmission(SSD1306_ADDR); Wire.write(SSD1306_COMMAND);
for (uint8_t i = 0; i < sizeof(ssd1306_init_sequence); i++) {
Wire.write(pgm_read_byte(&ssd1306_init_sequence[i]));
}
Wire.endTransmission();
delayMicroseconds(100);
}
void noCursor(){}
void cursor(){}
void noDisplay(){}
void createChar(uint8_t l, uint8_t glyph[]){}
void _setCursor(uint8_t x, uint8_t y) { oledX = x; oledY = y;
Wire.beginTransmission(SSD1306_ADDR); Wire.write(SSD1306_COMMAND);
Wire.write(renderingFrame | (oledY & 0x07));
Wire.write(0x10 | ((oledX & 0xf0) >> 4));
Wire.write(oledX & 0x0f);
Wire.endTransmission();
}
void setCursor(uint8_t x, uint8_t y) { _setCursor(x * FONT_W, y * FONT_H); }
void newLine() {
oledY+=FONT_H;
if (oledY > SSD1306_PAGES - FONT_H) {
oledY = SSD1306_PAGES - FONT_H;
}
setCursor(0, oledY);
}
size_t write(byte c) {
if((c == '\n') || (oledX > ((uint8_t)128 - FONT_W))) {
if(wrap) newLine();
return 1;
}
uint16_t offset = ((uint16_t)c - ' ') * FONT_W/(FONT_STRETCHH+1) * FONT_H;
uint8_t line = FONT_H;
do
{
if(FONT_STRETCHV) offset = ((uint16_t)c - ' ') * FONT_W/(FONT_STRETCHH+1) * FONT_H/(2*FONT_STRETCHV);
Wire.beginTransmission(SSD1306_ADDR); Wire.write(SSD1306_DATA);
for (uint8_t i = 0; i < (FONT_W/(FONT_STRETCHH+1)); i++) {
uint8_t b = pgm_read_byte(&(font[offset++]));
if(FONT_STRETCHV){
uint8_t b2 = 0;
if(line > 1) for(int i = 0; i!=4; i++) b2 |=/* ! */(b & (1<<i)) ? (1<<(i*2)) | (1<<(i*2)+1): 0x00;
else for(int i = 0; i!=4; i++) b2 |=/* ! */(b & (1<<(i+4))) ? (1<<(i*2)) | (1<<(i*2)+1): 0x00;
Wire.write(b2);
if(FONT_STRETCHH) Wire.write(b2);
} else { Wire.write(b); if(FONT_STRETCHH) Wire.write(b); }
}
Wire.endTransmission();
if (FONT_H == 1) {
oledX+=FONT_W;
}
else {
if (line > 1) {
_setCursor(oledX, oledY + 1);
}
else {
_setCursor(oledX + FONT_W, oledY - (FONT_H - 1));
}
}
}
while (--line);
return 1;
}
void bitmap(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1, const uint8_t bitmap[]) {
uint16_t j = 0;
for (uint8_t y = y0; y < y1; y++) {
_setCursor(x0, y);
Wire.beginTransmission(SSD1306_ADDR); Wire.write(SSD1306_DATA);
for (uint8_t x = x0; x < x1; x++) {
Wire.write(pgm_read_byte(&bitmap[j++]));
}
Wire.endTransmission();
}
setCursor(0, 0);
}
};
//#define OLED 1 // SDD1306 connection on display header: 1=GND(black), 2=5V(red), 13=SDA(brown), 14=SCK(orange)
#ifdef OLED
SSD1306Device lcd;
#else
LCD lcd; // highly-optimized LCD driver, OK for QCX supplied displays
//LCD_ lcd; // slower LCD, suitable for non-QCX supplied displays
//#include <LiquidCrystal.h>
//LiquidCrystal lcd(LCD_RS, LCD_EN, LCD_D4, LCD_D5, LCD_D6, LCD_D7);
#endif
volatile int8_t encoder_val = 0;
volatile int8_t encoder_step = 0;
static uint8_t last_state;
ISR(PCINT2_vect){ // Interrupt on rotary encoder turn
//noInterrupts();
switch(last_state = (last_state << 4) | (digitalRead(ROT_B) << 1) | digitalRead(ROT_A)){ //transition (see: https://www.allaboutcircuits.com/projects/how-to-use-a-rotary-encoder-in-a-mcu-based-project/ )
//#define ENCODER_ENHANCED_RESOLUTION 1
#ifdef ENCODER_ENHANCED_RESOLUTION // Option: enhance encoder from 24 to 96 steps/revolution, see: appendix 1, https://www.sdr-kits.net/documents/PA0KLT_Manual.pdf
case 0x31: case 0x10: case 0x02: case 0x23: encoder_val++; break;
case 0x32: case 0x20: case 0x01: case 0x13: encoder_val--; break;
#else
case 0x31: case 0x10: case 0x02: case 0x23: if(encoder_step < 0) encoder_step = 0; encoder_step++; if(encoder_step > 3){ encoder_step = 0; encoder_val++; } break;
case 0x32: case 0x20: case 0x01: case 0x13: if(encoder_step > 0) encoder_step = 0; encoder_step--; if(encoder_step < -3){ encoder_step = 0; encoder_val--; } break;
#endif
}
//interrupts();
}
void encoder_setup()
{
pinMode(ROT_A, INPUT_PULLUP);
pinMode(ROT_B, INPUT_PULLUP);
PCMSK2 |= (1 << PCINT22) | (1 << PCINT23); // interrupt-enable for ROT_A, ROT_B pin changes; see https://github.com/EnviroDIY/Arduino-SDI-12/wiki/2b.-Overview-of-Interrupts
PCICR |= (1 << PCIE2);
last_state = (digitalRead(ROT_B) << 1) | digitalRead(ROT_A);
interrupts();
}
/*
class Encoder {
public:
volatile int8_t val = 0;
volatile int8_t step = 0;
uint8_t last_state;
Encoder(){
pinMode(ROT_A, INPUT_PULLUP);
pinMode(ROT_B, INPUT_PULLUP);
PCMSK2 |= (1 << PCINT22) | (1 << PCINT23); // interrupt-enable for ROT_A, ROT_B pin changes; see https://github.com/EnviroDIY/Arduino-SDI-12/wiki/2b.-Overview-of-Interrupts
PCICR |= (1 << PCIE2);
last_state = (digitalRead(ROT_B) << 1) | digitalRead(ROT_A);
sei();
}
void event(){
switch(last_state = (last_state << 4) | (digitalRead(ROT_B) << 1) | digitalRead(ROT_A)){ //transition (see: https://www.allaboutcircuits.com/projects/how-to-use-a-rotary-encoder-in-a-mcu-based-project/ )
case 0x31: case 0x10: case 0x02: case 0x23: if(step < 0) step = 0; step++; if(step > 3){ step = 0; val++; } break;
case 0x32: case 0x20: case 0x01: case 0x13: if(step > 0) step = 0; step--; if(step < -3){ step = 0; val--; } break;
}
}
};
Encoder enc;
ISR(PCINT2_vect){ // Interrupt on rotary encoder turn
enc.event();
}*/
class I2C {
public:
#define I2C_DELAY 4 // Determines I2C Speed (2=939kb/s (too fast!!); 3=822kb/s; 4=731kb/s; 5=658kb/s; 6=598kb/s). Increase this value when you get I2C tx errors (E05); decrease this value when you get a CPU overload (E01). An increment eats ~3.5% CPU load; minimum value is 3 on my QCX, resulting in 84.5% CPU load
#define I2C_DDR DDRC // Pins for the I2C bit banging
#define I2C_PIN PINC
#define I2C_PORT PORTC
#define I2C_SDA (1 << 4) // PC4
#define I2C_SCL (1 << 5) // PC5
#define DELAY(n) for(uint8_t i = 0; i != n; i++) asm("nop");
#define I2C_SDA_GET() I2C_PIN & I2C_SDA
#define I2C_SCL_GET() I2C_PIN & I2C_SCL
#define I2C_SDA_HI() I2C_DDR &= ~I2C_SDA;
#define I2C_SDA_LO() I2C_DDR |= I2C_SDA;
#define I2C_SCL_HI() I2C_DDR &= ~I2C_SCL; DELAY(I2C_DELAY);
#define I2C_SCL_LO() I2C_DDR |= I2C_SCL; DELAY(I2C_DELAY);
I2C(){
I2C_PORT &= ~( I2C_SDA | I2C_SCL );
I2C_SCL_HI();
I2C_SDA_HI();
suspend();
}
~I2C(){
I2C_PORT &= ~( I2C_SDA | I2C_SCL );
I2C_DDR &= ~( I2C_SDA | I2C_SCL );
}
inline void start(){
resume(); //prepare for I2C
I2C_SCL_LO();
I2C_SDA_HI();
}
inline void stop(){
I2C_SCL_HI();
I2C_SDA_HI();
I2C_DDR &= ~(I2C_SDA | I2C_SCL); // prepare for a start: pull-up both SDA, SCL
suspend();
}
#define SendBit(data, mask) \
if(data & mask){ \
I2C_SDA_HI(); \
} else { \
I2C_SDA_LO(); \
} \
I2C_SCL_HI(); \
I2C_SCL_LO();
/*#define SendByte(data) \
SendBit(data, 1 << 7) \
SendBit(data, 1 << 6) \
SendBit(data, 1 << 5) \
SendBit(data, 1 << 4) \
SendBit(data, 1 << 3) \
SendBit(data, 1 << 2) \
SendBit(data, 1 << 1) \
SendBit(data, 1 << 0) \
I2C_SDA_HI(); // recv ACK \
DELAY(I2C_DELAY); \
I2C_SCL_HI(); \
I2C_SCL_LO();*/
inline void SendByte(uint8_t data){
SendBit(data, 1 << 7);
SendBit(data, 1 << 6);
SendBit(data, 1 << 5);
SendBit(data, 1 << 4);
SendBit(data, 1 << 3);
SendBit(data, 1 << 2);
SendBit(data, 1 << 1);
SendBit(data, 1 << 0);
I2C_SDA_HI(); // recv ACK
DELAY(I2C_DELAY);
I2C_SCL_HI();
I2C_SCL_LO();
}
inline uint8_t RecvBit(uint8_t mask){
I2C_SCL_HI();
uint16_t i = 60000;
for(;(!I2C_SCL_GET()) && i; i--); // wait util slave release SCL to HIGH (meaning data valid), or timeout at 3ms
if(!i){ lcd.setCursor(0, 1); lcd.print(F("E07 I2C timeout")); }
uint8_t data = I2C_SDA_GET();
I2C_SCL_LO();
return (data) ? mask : 0;
}
inline uint8_t RecvByte(uint8_t last){
uint8_t data = 0;
data |= RecvBit(1 << 7);
data |= RecvBit(1 << 6);
data |= RecvBit(1 << 5);
data |= RecvBit(1 << 4);
data |= RecvBit(1 << 3);
data |= RecvBit(1 << 2);
data |= RecvBit(1 << 1);
data |= RecvBit(1 << 0);
if(last){
I2C_SDA_HI(); // NACK
} else {
I2C_SDA_LO(); // ACK
}
DELAY(I2C_DELAY);
I2C_SCL_HI();
I2C_SDA_HI(); // restore SDA for read
I2C_SCL_LO();
return data;
}
inline void resume(){
#ifdef LCD_RS_PORTIO
I2C_PORT &= ~I2C_SDA; // pin sharing SDA/LCD_RS mitigation
#endif
}
inline void suspend(){
I2C_SDA_LO(); // pin sharing SDA/LCD_RS: pull-down LCD_RS; QCXLiquidCrystal require this for any operation
}
};
#define log2(n) (log(n) / log(2))
// /*
I2C i2c;
class SI5351 {
public:
volatile int32_t _fout;
volatile uint8_t _div; // note: uint8_t asserts fout > 3.5MHz with R_DIV=1
volatile uint16_t _msa128min512;
volatile uint32_t _msb128;
volatile uint8_t pll_regs[8];
#define BB0(x) ((uint8_t)(x)) // Bash byte x of int32_t
#define BB1(x) ((uint8_t)((x)>>8))
#define BB2(x) ((uint8_t)((x)>>16))
#define FAST __attribute__((optimize("Ofast")))
#define F_XTAL 27005000 // Crystal freq in Hz, nominal frequency 27004300
//#define F_XTAL 25004000 // Alternate SI clock
//#define F_XTAL 20004000 // A shared-single 20MHz processor/pll clock
volatile uint32_t fxtal = F_XTAL;
inline void FAST freq_calc_fast(int16_t df) // note: relies on cached variables: _msb128, _msa128min512, _div, _fout, fxtal
{
#define _MSC 0x80000 //0x80000: 98% CPU load 0xFFFFF: 114% CPU load
uint32_t msb128 = _msb128 + ((int64_t)(_div * (int32_t)df) * _MSC * 128) / fxtal;
//#define _MSC 0xFFFFF // Old algorithm 114% CPU load, shortcut for a fixed fxtal=27e6
//register uint32_t xmsb = (_div * (_fout + (int32_t)df)) % fxtal; // xmsb = msb * fxtal/(128 * _MSC);
//uint32_t msb128 = xmsb * 5*(32/32) - (xmsb/32); // msb128 = xmsb * 159/32, where 159/32 = 128 * 0xFFFFF / fxtal; fxtal=27e6
//#define _MSC (F_XTAL/128) // 114% CPU load perfect alignment
//uint32_t msb128 = (_div * (_fout + (int32_t)df)) % fxtal;
uint32_t msp1 = _msa128min512 + msb128 / _MSC; // = 128 * _msa + msb128 / _MSC - 512;
uint32_t msp2 = msb128 % _MSC; // = msb128 - msb128/_MSC * _MSC;
//pll_regs[0] = BB1(msc); // 3 regs are constant
//pll_regs[1] = BB0(msc);
//pll_regs[2] = BB2(msp1);
pll_regs[3] = BB1(msp1);
pll_regs[4] = BB0(msp1);
pll_regs[5] = ((_MSC&0xF0000)>>(16-4))|BB2(msp2); // top nibble MUST be same as top nibble of _MSC !
pll_regs[6] = BB1(msp2);
pll_regs[7] = BB0(msp2);
}
#define SI5351_ADDR 0x60 // I2C address of Si5351 (typical)
inline void SendPLLBRegisterBulk(){
i2c.start();
i2c.SendByte(SI5351_ADDR << 1);
i2c.SendByte(26+1*8 + 3); // Write to PLLB
i2c.SendByte(pll_regs[3]);
i2c.SendByte(pll_regs[4]);
i2c.SendByte(pll_regs[5]);
i2c.SendByte(pll_regs[6]);
i2c.SendByte(pll_regs[7]);
i2c.stop();
}
void SendRegister(uint8_t reg, uint8_t* data, uint8_t n){
i2c.start();
i2c.SendByte(SI5351_ADDR << 1);
i2c.SendByte(reg);
while (n--) i2c.SendByte(*data++);
i2c.stop();
}
void SendRegister(uint8_t reg, uint8_t val){ SendRegister(reg, &val, 1); }
/*
bool dirty;
void set_pll(uint8_t pll, uint32_t fvco){ // Set PLL (fractional) PLLA=0, PLLB=1
uint8_t msa; uint32_t msb, msc, msp1, msp2, msp3p2;
msa = fvco / fxtal; // Integer part of vco/fxtal
msb = ((uint64_t)(fvco % fxtal)*_MSC) / fxtal; // Fractional part
msc = _MSC;
msp1 = 128*msa + 128*msb/msc - 512;
msp2 = 128*msb - 128*msb/msc * msc; // msp3 == msc
msp3p2 = (((msc & 0x0F0000) <<4) | msp2); // msp3 on top nibble
uint8_t pll_regs[8] = { BB1(msc), BB0(msc), BB2(msp1), BB1(msp1), BB0(msp1), BB2(msp3p2), BB1(msp2), BB0(msp2) };
SendRegister(26+pll*8, pll_regs, 8); // Write to PLL[pll]
}
void set_ms(uint8_t clk, uint8_t linked_pll, uint8_t msa, uint8_t rdiv, uint8_t phase){ // Set Multisynth divider (integer) CLK0=0, CLK1=1, CLK2=2
uint32_t msp1 = (128*msa - 512) | (((uint32_t)rdiv)<<20); // msp1 and msp2=0, msp3=1, not fractional
uint8_t ms_regs[8] = {0, 1, BB2(msp1), BB1(msp1), BB0(msp1), 0, 0, 0};
SendRegister(42+clk*8, ms_regs, 8); // Write to MS[clk]
SendRegister(16+clk, (linked_pll*0x20)|0x0C|3|0x40); // CLK[clk]: PLL[pll] local msynth; 3=8mA; 0x40=integer division, bit7:6=0->power-up
SendRegister(165+clk, phase * msa / 90); // CLK[clk]: phase (on change -> reset PLL)
static uint16_t pm[3]; // to detect a need for a PLL reset
if(pm[clk] != ((phase)*msa/90)){ pm[clk] = (phase)*msa/90; dirty=true; } // 0x20 reset PLLA; 0x80 reset PLLB
}
void set_freq(uint8_t clk, uint8_t pll, uint32_t fout, uint8_t phase){
uint8_t rdiv = 0; // CLK pin sees fout/(2^rdiv)
if(fout < 500000){ rdiv = 7; fout *= 128; }; // Divide by 128 for fout 4..500kHz
uint16_t d = (16 * fxtal) / fout; // Integer part
if(fout > 30000000) d = (34 * fxtal) / fout; // when fvco is getting too low (400 MHz)
if( (d * (fout - 5000) / fxtal) != (d * (fout + 5000) / fxtal) ) d--; // Test if multiplier remains same for freq deviation +/- 5kHz, if not use different divider to make same
uint32_t fvco = d * fout; // Variable PLL VCO frequency at integer multiple of fout at around 27MHz*16 = 432MHz
set_pll(pll, fvco);
set_ms(clk, pll, fvco / fout, rdiv, phase);
_fout = fout; // cache
_div = d;
_msa128min512 = fvco / fxtal * 128 - 512;
_msb128=((uint64_t)(fvco % fxtal)*_MSC*128) / fxtal;
}
void pll_reset(){ if(dirty){ dirty=false; SendRegister(177, 0xA0); } } // reset PLLA and PLLB, if necessary
void oe(uint8_t mask){ SendRegister(3, ~mask); } // Output-enable mask: CLK2=4; CLK1=2; CLK0=1
void freq(uint32_t f, uint8_t i, uint8_t q){ // Set CLK0,1 (PLLA) to fout Hz with phase i, q, and prepare CLK2 (PLL2).
set_freq(0, 0, f, i);
set_freq(1, 0, f, q);