forked from threeme3/usdx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
usdx.ino
5858 lines (5359 loc) · 242 KB
/
usdx.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, 2021 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.02w"
// Configuration switches; remove/add a double-slash at line-start to enable/disable a feature; to save space disable e.g. CAT, DIAG, KEYER
#define DIAG 1 // Hardware diagnostics on startup (only disable when your rig is working)
#define KEYER 1 // CW keyer
#define CAT 1 // CAT-interface
#define F_XTAL 27005000 // 27MHz SI5351 crystal
//#define F_XTAL 25004000 // 25MHz SI5351 crystal (enable for WB2CBA-uSDX, SI5351 break-out board or uSDXDuO)
//#define F_XTAL 25000000 // 25MHz SI5351 crystal (enable for 25MHz TCXO)
//#define SWAP_ROTARY 1 // Swap rotary direction (enable for WB2CBA-uSDX)
//#define QCX 1 // Supports older (non-SDR) QCX HW modifications (QCX, QCX-SSB, QCX-DSP with I/Q alignment-feature)
//#define OLED_SSD1306 1 // OLED display (SSD1306 128x32 or 128x64), connect SDA (PD2), SCL (PD3)
//#define OLED_SH1106 1 // OLED display (SH1106 1.3" inch display), connect SDA (PD2), SCL (PD3), NOTE that this display is pretty slow
//#define LCD_I2C 1 // LCD with I2C (PCF8574 module ), connect SDA (PD2), SCL (PD3), NOTE that this display is pretty slow
#define LPF_SWITCHING_DL2MAN_USDX_REV3 1 // Enable 8-band filter bank switching: latching relays wired to a TCA/PCA9555 GPIO extender on the PC4/PC5 I2C bus; relays are using IO0.0 as common (ground), IO1.0..7 used by the individual latches K0-7 switching respectively LPFs for 10m, 15m, 17m, 20m, 30m, 40m, 60m, 80m
//#define LPF_SWITCHING_DL2MAN_USDX_REV3_NOLATCH 1 // Enable 8-band filter bank switching: non-latching relays wired to a TCA/PCA9555 GPIO extender on the PC4/PC5 I2C bus; relays are using IO0.0 as common (ground), IO1.0..7 used by the individual latches K0-7 switching respectively LPFs for 10m, 15m, 17m, 20m, 30m, 40m, 60m, 80m. Enable this if you are using 8-band non-latching version for the relays, the radio will draw extra 15mA current but will work ity any relay (Tnx OH2UDS/TA7W Baris)
//#define LPF_SWITCHING_DL2MAN_USDX_REV2 1 // Enable 5-band filter bank switching: latching relays wired to a TCA/PCA9555 GPIO extender on the PC4/PC5 I2C bus; relays are using IO0.1 as common (ground), IO0.3, IO0.5, IO0.7, IO1.1, IO1.3 used by the individual latches K1-5 switching respectively LPFs for 20m, 30m, 40m, 60m, 80m
//#define LPF_SWITCHING_DL2MAN_USDX_REV2_BETA 1 // Enable 5-band filter bank switching: latching relays wired to a PCA9539PW GPIO extender on the PC4/PC5 I2C bus; relays are using IO0.1 as common (ground), IO0.3, IO0.5, IO0.7, IO1.1, IO1.3 used by the individual latches K1-5 switching respectively LPFs for 20m, 30m, 40m, 60m, 80m
//#define LPF_SWITCHING_DL2MAN_USDX_REV1 1 // Enable 3-band filter bank switching: latching relays wired to a PCA9536D GPIO extender on the PC4/PC5 I2C bus; relays are using IO0 as common (ground), IO1-IO3 used by the individual latches K1-3 switching respectively LPFs for 20m, 40m, 80m
//#define LPF_SWITCHING_WB2CBA_USDX_OCTOBAND 1 // Enable 8-band filter bank switching: non-latching relays wired to a MCP23008 GPIO extender on the PC4/PC5 I2C bus; relays are using GND as common (ground), GP0..7 used by the individual latches K1-8 switching respectively LPFs for 80m, 60m, 40m, 30m, 20m, 17m, 15m, 10m
//#define LPF_SWITCHING_PE1DDA_USDXDUO 14 // Enable 2-band filter bank switching: non-latching relay wired to pin PD5 (pin 11); specify as value the frequency in MHz for which (and above) the relay should be altered (e.g. put 14 to enable the relay at 14MHz and above to use the 20m LPF).
#define SI5351_ADDR 0x60 // SI5351A I2C address: 0x60 for SI5351A-B-GT, Si5351A-B04771-GT, MS5351M; 0x62 for SI5351A-B-04486-GT; 0x6F for SI5351A-B02075-GT; see here for other variants: https://www.silabs.com/TimingUtility/timing-download-document.aspx?OPN=Si5351A-B02075-GT&OPNRevision=0&FileType=PublicAddendum
//#define F_MCU 16000000 // 16MHz ATMEGA328P crystal (enable for unmodified Arduino Uno/Nano boards with 16MHz crystal). You may change this value to any other crystal frequency (up to 28MHz may work)
// Advanced configuration switches
//#define CONDENSED 1 // Display in 4 line mode (for OLED and LCD2004 modules)
//#define CAT_EXT 1 // Extended CAT support: remote button and screen control commands over CAT
//#define CAT_STREAMING 1 // Extended CAT support: audio streaming over CAT, once enabled and triggered with CAT cmd, samplerate 7812Hz, 8-bit unsigned audio is sent over UART. The ";" is omited in the data-stream, and only sent to indicate the beginning and end of a CAT cmd.
#define CW_DECODER 1 // CW decoder
#define TX_ENABLE 1 // Disable this for RX only (no transmit), e.g. to support uSDX for kids idea: https://groups.io/g/ucx/topic/81030243#6276
#define KEY_CLICK 1 // Reduce key clicks by envelope shaping
#define SEMI_QSK 1 // Just after keying the transmitter, keeps the RX muted for a short amount of time in the anticipation for continued keying
#define RIT_ENABLE 1 // Receive-In-Transit alternates the receiving frequency with an user-defined offset to compensate for any necessary tuning needed on receive
#define VOX_ENABLE 1 // Voice-On-Xmit which is switching the transceiver into transmit as soon audio is detected (above noise gate level)
//#define MOX_ENABLE 1 // Monitor-On-Xmit which is audio monitoring on speaker during transmit
//#define FAST_AGC 1 // Adds fast AGC option (good for CW)
//#define VSS_METER 1 // Supports Vss measurement (as s-meter option), requires resistor of 1M between 12V and pin 26 (PC3)
//#define SWR_METER 1 // Supports SWR meter with bridge on A6/A7 (LQPF ATMEGA328P) by Alain, K1FM, see: https://groups.io/g/ucx/message/6262 and https://groups.io/g/ucx/message/6361
//#define ONEBUTTON 1 // Use single (encoder) button to control full the rig; optionally use L/R buttons to completely replace rotory encoder function
//#define DEBUG 1 // for development purposes only (adds debugging features such as CPU, sample-rate measurement, additional parameters)
//#define TESTBENCH 1 // Tests RX chain by injection of sine wave, measurements results are sent over serial
//#define CW_FREQS_QRP 1 // Defaults to CW QRP frequencies when changing bands
//#define CW_FREQS_FISTS 1 // Defaults to CW FISTS frequencies when changing bands
#define CW_MESSAGE 1 // Transmits pre-defined CW messages on-demand (left-click menu item 4.2)
//#define CW_MESSAGE_EXT 1 // Additional CW messages
//#define TX_DELAY 1 // Enables a delay in the actual transmission to allow relay-switching to be completed before the power is applied (see also NTX, PTX definitions below for GPIO that can switch relay/PA)
//#define NTX 11 // Enables LOW on TX, used as PTT out to enable external PAs (a value of 11 means PB3 is used)
//#define PTX 11 // Enables HIGH on TX, used as PTT out to enable external PAs (a value of 11 means PB3 is used)
//#define CLOCK 1 // Enables clock
#define CW_INTERMEDIATE 1 // CW decoder shows intermediate characters (only available for LCD and F_MCU at 20M), sequences like: EIS[HV] EIUF EAW[JP] EARL TMO TMG[ZQ] TND[BX] TNK[YC], may be good to learn CW; a full list of possible sequences: EISH5 EISV3 EIUF EIUU2 EAWJ1 EAWP EARL TMOO0 TMOO9 TMOO8 TMGZ7 TMGQ TNDB6 TNDX TNKY TNKC
//#define F_XTAL 20000000 // Enable this for uSDXDuO, 20MHz SI5351 crystal
//#define TX_CLK0_CLK1 1 // Enable this for uSDXDuO, i.e. when PA is driven by CLK0, CLK1 (not CLK2); NTX pin may be used for enabling the TX path (this is like RX pin, except that RX may also be used as attenuator)
//#define F_CLK2 12000000 // Enables a fixed CLK2 clock output of choice (only applicable when TX_CLK0_CLK1 is enabled), e.g. for up-converter or to clock UART USB device
// 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)
//#define PTX 11 //PB3 (pin 17)
#ifdef SWAP_ROTARY
#undef ROT_A
#undef ROT_B
#define ROT_A 7 //PD7 (pin 13)
#define ROT_B 6 //PD6 (pin 12)
#endif
#if (defined(OLED_SSD1306) || defined(OLED_SH1106))
#define OLED 1
#endif
#if (defined(CAT) || defined(TESTBENCH)) && !(OLED)
#define _SERIAL 1 // Coexistence support for serial port and LCD on the same pins
#endif
#ifdef LPF_SWITCHING_DL2MAN_USDX_REV3_NOLATCH
#define LPF_SWITCHING_DL2MAN_USDX_REV3 1
#endif
#ifdef TX_CLK0_CLK1
#ifdef F_CLK2
#define TX1RX0 0b11111000
#define TX1RX1 0b11111000
#define TX0RX1 0b11111000
#define TX0RX0 0b11111011
#else //!F_CLK2
#define TX1RX0 0b11111100
#define TX1RX1 0b11111100
#define TX0RX1 0b11111100
#define TX0RX0 0b11111111
#endif //F_CLK2
#else //!TX_CLK0_CLK1
#define TX1RX0 0b11111011
#define TX1RX1 0b11111000
#define TX0RX1 0b11111100
#define TX0RX0 0b11111111
#endif //TX_CLK0_CLK1
#if defined(F_CLK2) && !defined(TX_CLK0_CLK1)
#error "TX_CLK0_CLK1 must be enabled in order to use F_CLK2."
#endif
#ifndef TX_ENABLE
#undef KEYER
#undef TX_DELAY
#undef SEMI_QSK
#undef RIT_ENABLE
#undef VOX_ENABLE
#undef MOX_ENABLE
#endif //!TX_ENABLE
#ifdef SWR_METER
float FWD;
float SWR;
float ref_V = 5 * 1.15;
static uint32_t stimer;
#define PIN_FWD A6
#define PIN_REF A7
#endif
/*
// 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
#define ONEBUTTON_INV 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
*/
//FUSES = { .low = 0xFF, .high = 0xD6, .extended = 0xFD }; // Fuse settings should be set at programming (Arduino IDE > Tools > Burn bootloader)
#if(ARDUINO < 10810)
#error "Unsupported Arduino IDE version, use Arduino IDE 1.8.10 or later from https://www.arduino.cc/en/software"
#endif
#if !(defined(ARDUINO_ARCH_AVR))
#error "Unsupported architecture, select Arduino IDE > Tools > Board > Arduino AVR Boards > Arduino Uno."
#endif
#if(F_CPU != 16000000)
#error "Unsupported clock frequency, Arduino IDE must specify 16MHz clock; alternate crystal frequencies may be specified with F_MCU."
#endif
#undef F_CPU
#define F_CPU 20007000 // Actual crystal frequency of 20MHz XTAL1, note that this declaration is just informative and does not correct the timing in Arduino functions like delay(); hence a 1.25 factor needs to be added for correction.
#ifndef F_MCU
#define F_MCU 20000000 // 20MHz ATMEGA328P crystal
#endif
extern char __bss_end;
static int freeMemory(){ char* sp = reinterpret_cast<char*>(SP); return sp - &__bss_end; } // see: http://www.nongnu.org/avr-libc/user-manual/malloc.html
#ifdef CAT_EXT
volatile uint8_t cat_key = 0;
uint8_t _digitalRead(uint8_t pin){ // reads pin or (via CAT) artificially overriden pins
serialEvent(); // allows CAT update
if(cat_key){ return (pin == BUTTONS) ? ((cat_key&0x07) > 0) : (pin == DIT) ? ~cat_key&0x10 : (pin == DAH) ? ~cat_key&0x20 : 0; } // overrides digitalRead(DIT, DAH, BUTTONS);
return digitalRead(pin);
}
#else
#define _digitalRead(x) digitalRead(x)
#endif //CAT_EXT
//#define ONEBUTTON_INV 1 // Encoder button goes from PC3 to GND (instead PC3 to 5V, with 10k pull down)
#ifdef ONEBUTTON_INV
uint8_t inv = 1;
#else
uint8_t inv = 0;
#endif
//#ifdef KEYER
// Iambic Morse Code Keyer Sketch, Contribution by Uli, DL2DBG. Copyright (c) 2009 Steven T. Elliott Source: http://openqrp.org/?p=343, Trimmed by Bill Bishop - wrb[at]wrbishop.com. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details: Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
// keyerControl bit definitions
#define DIT_L 0x01 // Dit latch
#define DAH_L 0x02 // Dah latch
#define DIT_PROC 0x04 // Dit is being processed
#define PDLSWAP 0x08 // 0 for normal, 1 for swap
#define IAMBICB 0x10 // 0 for Iambic A, 1 for Iambic B
#define IAMBICA 0x00 // 0 for Iambic A, 1 for Iambic B
#define SINGLE 2 // Keyer Mode 0 1 -> Iambic2 2 ->SINGLE
int keyer_speed = 25;
static unsigned long ditTime; // No. milliseconds per dit
static uint8_t keyerControl;
static uint8_t keyerState;
static uint8_t keyer_mode = 2; //-> SINGLE
static uint8_t keyer_swap = 0; //-> DI/DAH
static uint32_t ktimer;
static int Key_state;
int debounce;
enum KSTYPE {IDLE, CHK_DIT, CHK_DAH, KEYED_PREP, KEYED, INTER_ELEMENT }; // State machine states
void update_PaddleLatch() // Latch dit and/or dah press, called by keyer routine
{
if(_digitalRead(DIT) == LOW) {
keyerControl |= keyer_swap ? DAH_L : DIT_L;
}
if(_digitalRead(DAH) == LOW) {
keyerControl |= keyer_swap ? DIT_L : DAH_L;
}
}
void loadWPM (int wpm) // Calculate new time constants based on wpm value
{
#if(F_MCU != 20000000)
ditTime = (1200ULL * F_MCU/16000000)/wpm; //ditTime = 1200/wpm; compensated for F_CPU clock (running in a 16MHz Arduino environment)
#else
ditTime = (1200 * 5/4)/wpm; //ditTime = 1200/wpm; compensated for 20MHz clock (running in a 16MHz Arduino environment)
#endif
}
//#endif //KEYER
static uint8_t practice = false; // Practice mode
volatile uint8_t cat_active = 0;
volatile uint32_t rxend_event = 0;
volatile uint8_t vox = 0;
#include <avr/sleep.h>
#include <avr/wdt.h>
//#define _I2C_DIRECT_IO 1 // Enables communications that is not using the standard I/O pull-down approach with pull-up resistors, instead I/O is directly driven with 0V/5V
class I2C_ { // Secundairy I2C class used by I2C LCD/OLED, uses alternate pins: PD2 (SDA) and PD3 (SCL)
public:
#if(F_MCU > 20900000)
#ifdef OLED_SH1106
#define _DELAY() for(uint8_t i = 0; i != 9; i++) asm("nop");
#else
#ifdef OLED_SSD1306
#define _DELAY() for(uint8_t i = 0; i != 6; i++) asm("nop");
#else // other (I2C_LCD)
#define _DELAY() for(uint8_t i = 0; i != 7; i++) asm("nop");
#endif
#endif
#else // slow F_MCU
#ifdef OLED_SH1106
#define _DELAY() for(uint8_t i = 0; i != 8; i++) asm("nop");
#else
#ifdef OLED_SSD1306
#define _DELAY() for(uint8_t i = 0; i != 4; i++) asm("nop"); // 4=731kb/s
#else // other (I2C_LCD)
#define _DELAY() for(uint8_t i = 0; i != 5; i++) asm("nop");
#endif
#endif
#endif // F_MCU
#define _I2C_SDA (1<<2) // PD2
#define _I2C_SCL (1<<3) // PD3
#ifdef _I2C_DIRECT_IO
#define _I2C_INIT() _I2C_SDA_HI(); _I2C_SCL_HI(); DDRD |= (_I2C_SDA | _I2C_SCL); // direct I/O (no need for pull-ups)
#define _I2C_SDA_HI() PORTD |= _I2C_SDA;
#define _I2C_SDA_LO() PORTD &= ~_I2C_SDA;
#define _I2C_SCL_HI() PORTD |= _I2C_SCL; _DELAY();
#define _I2C_SCL_LO() PORTD &= ~_I2C_SCL; _DELAY();
#else // !_I2C_DIRECT_IO
#define _I2C_INIT() PORTD &= ~_I2C_SDA; PORTD &= ~_I2C_SCL; _I2C_SDA_HI(); _I2C_SCL_HI(); // open-drain
#define _I2C_SDA_HI() DDRD &= ~_I2C_SDA;
#define _I2C_SDA_LO() DDRD |= _I2C_SDA;
#define _I2C_SCL_HI() DDRD &= ~_I2C_SCL; _DELAY();
#define _I2C_SCL_LO() DDRD |= _I2C_SCL; _DELAY();
#endif // !_I2C_DIRECT_IO
#define _I2C_START() _I2C_SDA_LO(); _DELAY(); _I2C_SCL_LO(); // _I2C_SDA_HI();
#define _I2C_STOP() _I2C_SDA_LO(); _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>
uint8_t backlight = 8;
//#define RS_HIGH_ON_IDLE 1 // Experimental LCD support where RS line is high on idle periods to comply with SDA I2C standard.
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 // PD4 - MUST have pull-up resistor
#define _rs 4 // PC4 - MUST have pull-up resistor
#define RS_PULLUP 1 // Use pullup on RS line, ensures compliancy to the absolute maximum ratings for the si5351 sda input that is shared with rs pin of lcd
#ifdef RS_PULLUP
#define LCD_RS_HI() DDRC &= ~(1 << _rs); asm("nop"); // RS high (pull-up)
#define LCD_RS_LO() DDRC |= 1 << _rs; // RS low (pull-down)
#else
#define LCD_RS_LO() PORTC &= ~(1 << _rs); // RS low
#define LCD_RS_HI() PORTC |= (1 << _rs); // RS high
#endif //RS_PULLUP
#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
uint8_t _cols;
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);
#ifdef LCD_I2C
#define PCF_ADDR 0x27 // LCD I2C address where PCF8574 addess selection A0, A1, A2 are all open
#define PCF_RS 0x01
#define PCF_RW 0x02 // the 0xF0 bits are used for 4-bit data to the display.
#define PCF_EN 0x04
#define PCF_BACKLIGHT 0x08
Wire.beginTransmission(PCF_ADDR);
Wire.write(0);
Wire.endTransmission();
delayMicroseconds(50000);
#else //!LCD_I2C
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();
#endif //!LCD_I2C
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
}
#ifdef LCD_I2C
void nib(uint8_t b, bool isData){ // Send four bit nibble to display
b = (b << 4) | ((backlight) ? PCF_BACKLIGHT : 0) | ((isData) ? PCF_RS : 0);
Wire.write(b | PCF_EN); // write command EN HI
delayMicroseconds(4); // enable pulse must be >450ns
Wire.write(b); // write command EN LO
delayMicroseconds(60); // commands need > 37us to settle
Wire.write(b); // must write for some unknown reason
}
void cmd(uint8_t b){
Wire.beginTransmission(PCF_ADDR);
nib(b >> 4, false); nib(b, false);
Wire.endTransmission();
}
size_t write(uint8_t b){
Wire.beginTransmission(PCF_ADDR);
nib((b >> 4), true); nib((b), true);
Wire.endTransmission();
}
#else //!LCD_I2C
// Since LCD is using PD0(RXD), PD1(TXD) pins in the data-path, some co-existence feature is required when using the serial port.
// The following functions are temporarily dsiabling the serial port when LCD writes happen, and make sure that serial transmission is ended.
// To prevent that LCD writes are received by the serial receiver, PC2 is made HIGH during writes to pull-up TXD via a diode.
// The RXD, TXD lines are connected to the host via 1k resistors, a 1N4148 is placed between PC2 (anode) and the TXD resistor.
// There are two drawbacks when continuous LCD writes happen: 1. noise is leaking via the AREF pull-ups into the receiver 2. serial data cannot be received.
void pre(){
#ifdef _SERIAL
if(!vox) if(cat_active){ Serial.flush(); for(; millis() < rxend_event;)wdt_reset(); PORTC |= 1<<2; DDRC |= 1<<2; } UCSR0B &= ~((1<<RXEN0)|(1<<TXEN0)); // Complete serial TX and RX; mask PD1 LCD data-exchange by pulling-up TXD via PC2 HIGH; enable PD0/PD1, disable serial port
#endif
noInterrupts(); // do not allow LCD tranfer to be interrupted, to prevent backlight to lighten-up
}
void post(){
if(backlight) PORTD |= 0x08; else PORTD &= ~0x08; // Backlight control
#ifdef _SERIAL
//UCSR0B |= (1<<RXEN0)|(1<<TXEN0); if(!vox) if(cat_active){ DDRC &= ~(1<<2); } // Enable serial port, disable PD0, PD1; disable PC2
UCSR0B |= (1<<RXEN0)|(1<<TXEN0); if(!vox) if(cat_active){ PORTC &= ~(1<<2); } // Enable serial port, disable PD0, PD1; PC2 LOW to prevent CAT TX disruption via MIC input
#endif
interrupts();
}
#ifdef RS_HIGH_ON_IDLE
void cmd(uint8_t b){
pre();
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
LCD_RS_LO();
LCD_EN_LO();
PORTD = nibl; // Send low nibble data and enable high
asm("nop"); asm("nop"); // Keep RS low, but complete enable cycle (should be 500ns)
LCD_EN_LO();
LCD_RS_HI();
post();
delayMicroseconds(60); // Execution time (37+4)*1.25 us
}
size_t write(uint8_t b){ // Write data: send nibbles while RS high
pre();
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
LCD_RS_HI();
LCD_EN_LO();
PORTD = nibl; // Send low nibble data and enable high
asm("nop"); asm("nop"); // Keep RS high, but complete enable cycle (should be 500ns)
LCD_EN_LO();
post();
delayMicroseconds(60); // Execution time (37+4)*1.25 us
return 1;
}
#else //!RS_HIGH_ON_IDLE
void nib(uint8_t b){ // Send four bit nibble to display
pre();
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();
post();
//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
pre();
//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();
post();
delayMicroseconds(60); // Execution time (37+4)*1.25 us
return 1;
}
#endif // RS_HIGH_ON_IDLE
#endif //!LCD_I2C
#ifdef CONDENSED
void setCursor(uint8_t x, uint8_t y){ cmd(0x80 | (x + (uint8_t[]){0x00, 0x40, 0x00 + 20, 0x40 + 20}[y])); } // ONLY for LCD2004 display
#else
void setCursor(uint8_t x, uint8_t y){ cmd(0x80 | (x + y * 0x40)); }
#endif
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
};
};
*/
/* // 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, // 0
0x00, 0x40, 0x44, 0x7f, 0x7f, 0x40, 0x40, 0x00, // 1
0x00, 0x62, 0x73, 0x51, 0x49, 0x4f, 0x46, 0x00, // 2
0x00, 0x22, 0x63, 0x49, 0x49, 0x7f, 0x36, 0x00, // 3
0x00, 0x18, 0x18, 0x14, 0x16, 0x7f, 0x7f, 0x10, // 4
0x00, 0x27, 0x67, 0x45, 0x45, 0x7d, 0x39, 0x00, // 5
0x00, 0x3e, 0x7f, 0x49, 0x49, 0x7b, 0x32, 0x00, // 6
0x00, 0x03, 0x03, 0x79, 0x7d, 0x07, 0x03, 0x00, // 7
0x00, 0x36, 0x7f, 0x49, 0x49, 0x7f, 0x36, 0x00, // 8
0x00, 0x26, 0x6f, 0x49, 0x49, 0x7f, 0x3e, 0x00, // 9
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, // A
0x00, 0x7f, 0x7f, 0x49, 0x49, 0x7f, 0x36, 0x00, // B
0x00, 0x3e, 0x7f, 0x41, 0x41, 0x63, 0x22, 0x00, // C
0x00, 0x7f, 0x7f, 0x41, 0x63, 0x3e, 0x1c, 0x00, // D
0x00, 0x7f, 0x7f, 0x49, 0x49, 0x41, 0x41, 0x00, // E
0x00, 0x7f, 0x7f, 0x09, 0x09, 0x01, 0x01, 0x00, // F
0x00, 0x3e, 0x7f, 0x41, 0x49, 0x7b, 0x3a, 0x00, // G
0x00, 0x7f, 0x7f, 0x08, 0x08, 0x7f, 0x7f, 0x00, // H
0x00, 0x00, 0x41, 0x7f, 0x7f, 0x41, 0x00, 0x00, // I
0x00, 0x20, 0x60, 0x41, 0x7f, 0x3f, 0x01, 0x00, // J
0x00, 0x7f, 0x7f, 0x1c, 0x36, 0x63, 0x41, 0x00, // K
0x00, 0x7f, 0x7f, 0x40, 0x40, 0x40, 0x40, 0x00, // L
0x00, 0x7f, 0x7f, 0x06, 0x0c, 0x06, 0x7f, 0x7f, // M
0x00, 0x7f, 0x7f, 0x0e, 0x1c, 0x7f, 0x7f, 0x00, // N
0x00, 0x3e, 0x7f, 0x41, 0x41, 0x7f, 0x3e, 0x00, // O
0x00, 0x7f, 0x7f, 0x09, 0x09, 0x0f, 0x06, 0x00, // P
0x00, 0x1e, 0x3f, 0x21, 0x61, 0x7f, 0x5e, 0x00, // Q
0x00, 0x7f, 0x7f, 0x19, 0x39, 0x6f, 0x46, 0x00, // R
0x00, 0x26, 0x6f, 0x49, 0x49, 0x7b, 0x32, 0x00, // S
0x00, 0x01, 0x01, 0x7f, 0x7f, 0x01, 0x01, 0x00, // T
0x00, 0x3f, 0x7f, 0x40, 0x40, 0x7f, 0x3f, 0x00, // U
0x00, 0x1f, 0x3f, 0x60, 0x60, 0x3f, 0x1f, 0x00, // V
0x00, 0x7f, 0x7f, 0x30, 0x18, 0x30, 0x7f, 0x7f, // W
0x00, 0x63, 0x77, 0x1c, 0x1c, 0x77, 0x63, 0x00, // X
0x00, 0x07, 0x0f, 0x78, 0x78, 0x0f, 0x07, 0x00, // Y
0x00, 0x61, 0x71, 0x59, 0x4d, 0x47, 0x43, 0x00, // Z
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, // a
0x00, 0x7e, 0x7e, 0x48, 0x48, 0x78, 0x30, 0x00, // b
0x00, 0x38, 0x7c, 0x44, 0x44, 0x44, 0x00, 0x00, // c
0x00, 0x30, 0x78, 0x48, 0x48, 0x7e, 0x7e, 0x00, // d
0x00, 0x38, 0x7c, 0x54, 0x54, 0x5c, 0x18, 0x00, // e
0x00, 0x00, 0x08, 0x7c, 0x7e, 0x0a, 0x0a, 0x00, // f
0x00, 0x98, 0xbc, 0xa4, 0xa4, 0xfc, 0x7c, 0x00, // g
0x00, 0x7e, 0x7e, 0x08, 0x08, 0x78, 0x70, 0x00, // h
0x00, 0x00, 0x48, 0x7a, 0x7a, 0x40, 0x00, 0x00, // i
0x00, 0x00, 0x80, 0x80, 0x80, 0xfa, 0x7a, 0x00, // j
0x00, 0x7e, 0x7e, 0x10, 0x38, 0x68, 0x40, 0x00, // k
0x00, 0x00, 0x42, 0x7e, 0x7e, 0x40, 0x00, 0x00, // l
0x00, 0x7c, 0x7c, 0x18, 0x38, 0x1c, 0x7c, 0x78, // m
0x00, 0x7c, 0x7c, 0x04, 0x04, 0x7c, 0x78, 0x00, // n
0x00, 0x38, 0x7c, 0x44, 0x44, 0x7c, 0x38, 0x00, // o
0x00, 0xfc, 0xfc, 0x24, 0x24, 0x3c, 0x18, 0x00, // p
0x00, 0x18, 0x3c, 0x24, 0x24, 0xfc, 0xfc, 0x00, // q
0x00, 0x7c, 0x7c, 0x04, 0x04, 0x0c, 0x08, 0x00, // r
0x00, 0x48, 0x5c, 0x54, 0x54, 0x74, 0x24, 0x00, // s
0x00, 0x04, 0x04, 0x3e, 0x7e, 0x44, 0x44, 0x00, // t
0x00, 0x3c, 0x7c, 0x40, 0x40, 0x7c, 0x7c, 0x00, // u
0x00, 0x1c, 0x3c, 0x60, 0x60, 0x3c, 0x1c, 0x00, // v
0x00, 0x1c, 0x7c, 0x70, 0x38, 0x70, 0x7c, 0x1c, // w
0x00, 0x44, 0x6c, 0x38, 0x38, 0x6c, 0x44, 0x00, // x
0x00, 0x9c, 0xbc, 0xa0, 0xe0, 0x7c, 0x3c, 0x00, // y
0x00, 0x44, 0x64, 0x74, 0x5c, 0x4c, 0x44, 0x00, // z
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, // ~
0b0000000, // 126+1; logo
0b1010101,
0b0101010,
0b0101010,
0b0010100,
0b0010100,
0b0001000,
0b0001000,
0b00000, // 126+2; s-meter, 0 bars
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000, // 126+3; s-meter, 1 bars
0b00000,
0b00000,
0b11111,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000, // 126+4; s-meter, 2 bars
0b00000,
0b00000,
0b11111,
0b00000,
0b11111,
0b00000,
0b00000,
0b00000, // 126+5; s-meter, 3 bars
0b00000,
0b00000,
0b11111,
0b00000,
0b11111,
0b00000,
0b11111,
0b00000, // 126+6; vfo-a
0b11100,
0b11110,
0b00101,
0b00101,
0b11110,
0b11100,
0b00000,
0b00000, // 126+7; vfo-b
0b11111,
0b11111,
0b10101,
0b10101,
0b01010,
0b01010,
0b00000 };
#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 INVERSE 1
static const uint8_t oled_init_sequence [] PROGMEM = { // Initialization Sequence https://cdn-shop.adafruit.com/datasheets/SSD1306.pdf
//0xAE, // Display OFF (sleep mode)
0xD5, 0x80, // 0x01--set display clock divide ratio/oscillator frequency OK? (0x80 (or >=0x10) needed when multiplex ration set to 0x3F)
#ifdef CONDENSED
0xA8, 0x3F, // Set multiplex ratio(1 to 64) 128x64
#else
0xA8, 0x1F, // Set multiplex ratio(1 to 64) 128x32
#endif
0xD3, 0x00, // Set display offset. 00 = no offset
#ifndef OLED_SH1106 // for SSD1306 only:
0x40, // Set display start line address
0x8D, 0x14, // Set charge pump, internal VCC
0x20, 0x02, // Set Memory Addressing; 0=Horizontal Mode; 1=Vertical Mode; 2=Page Mode
0xA4, // Output RAM to Display (display all on resume) 0xA4=Output follows RAM content; 0xA5,Output ignores RAM content
#endif //!OLED_SH1106
0xA1, // Set Segment Re-map. A0=column 0 mapped to SEG0; A1=column 127 mapped to SEG0. Flip Horizontally
0xC8, // Set COM Output Scan Direction. Flip Veritically.
#ifdef CONDENSED
0xDA, 0x12, // Set com pins hardware configuration 128x64
#else
0xDA, 0x02, // Set com pins hardware configuration 128x32
#endif
0x81, 0x80, // Set contrast control register
0xDB, 0x40, // Set vcomh 0x20 = 0.77xVcc
0xD9, 0xF1, // 0xF1=brighter //0x22 Set pre-charge period
0xB0 | 0x0, // Set page address, 0-7
#ifdef OLED_SH1106
0xAD, 0x8B, // SH1106 Set pump mode: pump ON
0x30 | 0x2, // SH1106 Pump voltage 8.0V
#endif //OLED_SH1106
#ifdef INVERSE
0xA7, // Set display mode: Inverse
#else
0xA6, // Set display mode: Normal
#endif
//0x00, // Set low nibble of column address
//0x10, // Set high nibble of column address
0xAF, // Display ON
};
class OLEDDevice: public Print { // https://www.buydisplay.com/download/manual/ER-OLED0.91-3_Series_Datasheet.pdf