-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLiquidCrystal.cpp
More file actions
976 lines (822 loc) · 23.5 KB
/
LiquidCrystal.cpp
File metadata and controls
976 lines (822 loc) · 23.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
///////////////////////////////////////////////////////////////////////////////
//
// Arduino Liquid Crystal (LCD/VFD) driver library
// Copyright (c) 2012 David A. Mellis <dam@mellis.org>
// Copyright (c) 2019 Roger A. Krupski <rakrupski@verizon.net>
//
// Last update: 6 May 2019
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
///////////////////////////////////////////////////////////////////////////////
#include "LiquidCrystal.h"
// serial interface, hardware reset not available
LiquidCrystal::LiquidCrystal (
uint8_t siso, uint8_t stb, uint8_t sck
) // 3
{
initalize (MODE_S, siso, stb, sck, NO_RST, 0, 0, 0, 0, 0, 0, 0, NO_RST);
}
// serial interface, hardware reset is available (D0 pin is used for reset)
LiquidCrystal::LiquidCrystal (
uint8_t siso, uint8_t stb, uint8_t sck, uint8_t reset
) // 4
{
initalize (MODE_S, siso, stb, sck, reset, 0, 0, 0, 0, 0, 0, 0, NO_RST);
}
// parallel interface 4 bits without active r/w (must tie r/w low manually)
LiquidCrystal::LiquidCrystal (
uint8_t rs, /* no rw */ uint8_t en,
uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7
) // 6
{
initalize (MODE_4, rs, NO_RW, en, 0, 0, 0, 0, d4, d5, d6, d7, NO_RST);
}
// parallel interface 4 bits with active r/w
LiquidCrystal::LiquidCrystal (
uint8_t rs, uint8_t rw, uint8_t en,
uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7
) // 7
{
initalize (MODE_4, rs, rw, en, 0, 0, 0, 0, d4, d5, d6, d7, NO_RST);
}
// parallel interface 4 bits with active r/w and active reset
LiquidCrystal::LiquidCrystal (
uint8_t rs, uint8_t rw, uint8_t en,
uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7,
uint8_t v0
) // 8
{
initalize (MODE_4, rs, rw, en, 0, 0, 0, 0, d4, d5, d6, d7, v0);
}
// parallel interface 8 bits without active r/w
LiquidCrystal::LiquidCrystal (
uint8_t rs, /* no rw */ uint8_t en,
uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7
) // 10
{
initalize (MODE_8, rs, NO_RW, en, d0, d1, d2, d3, d4, d5, d6, d7, NO_RST);
}
// parallel interface 8 bits with active r/w
LiquidCrystal::LiquidCrystal (
uint8_t rs, uint8_t rw, uint8_t en,
uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7
) // 11
{
initalize (MODE_8, rs, rw, en, d0, d1, d2, d3, d4, d5, d6, d7, NO_RST);
}
// parallel interface 8 bits with active r/w and active reset
LiquidCrystal::LiquidCrystal (
uint8_t rs, uint8_t rw, uint8_t en,
uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7,
uint8_t v0
) // 12
{
initalize (MODE_8, rs, rw, en, d0, d1, d2, d3, d4, d5, d6, d7, v0);
}
void LiquidCrystal::initalize (
uint8_t bitmode, uint8_t rs, uint8_t rw, uint8_t en,
uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7,
uint8_t v0
)
{
uint8_t n, x;
_bit_mode = bitmode; // 4 bit (0x04), 8 bit (0x08) or serial (0xFF) mode flag
if (_bit_mode == MODE_S) { // 0xFF == serial mode
_bit_mode = MODE_8; // reset it to 8 bit mode
_serial_mode = 1; // flag "we are in serial mode"
_reset_pin = d0; // alternate use of pin
// serial command byte template (Noritake CU20049-UW2J manual pg. 12)
// bit [7...3] = 1
// bit [2] = read/write (1=read,0=write)
// bit [1] = register select (1=data,0=command)
// bit [0] = 0
_serial_cmd = ((_RSBIT | _RWBIT | _SYNC));
n = digitalPinToPort (rs); // SISO pin is on RS
_SIO_BIT = digitalPinToBitMask (rs);
_SIO_PIN = portInputRegister (n);
_SIO_PORT = portOutputRegister (n);
_SIO_DDR = portModeRegister (n);
*_SIO_DDR |= _SIO_BIT;
*_SIO_PORT |= _SIO_BIT;
n = digitalPinToPort (rw); // STROBE pin is on RW
_STB_BIT = digitalPinToBitMask (rw);
_STB_PORT = portOutputRegister (n);
_STB_DDR = portModeRegister (n);
*_STB_DDR |= _STB_BIT;
*_STB_PORT |= _STB_BIT;
n = digitalPinToPort (en); // SCLOCK pin is on EN
_SCK_BIT = digitalPinToBitMask (en);
_SCK_PORT = portOutputRegister (n);
_SCK_DDR = portModeRegister (n);
*_SCK_DDR |= _SCK_BIT;
*_SCK_PORT |= _SCK_BIT;
if (_reset_pin != NO_RST) { // if we are using reset...
n = digitalPinToPort (d0); // RESET pin is on D0
_RST_BIT = digitalPinToBitMask (d0);
_RST_PORT = portOutputRegister (n);
_RST_DDR = portModeRegister (n);
*_RST_DDR |= _RST_BIT; // set it as output
*_RST_PORT &= ~_RST_BIT; // lower reset pin
__builtin_avr_delay_cycles (F_CPU / (_MSEC / 1.0)); // delay 1 msec
*_RST_PORT |= _RST_BIT; // raise reset pin
}
} else { // parallel mode
const uint8_t data_pin[] = {
d0, d1, d2, d3, d4, d5, d6, d7
};
_serial_mode = 0; // flag "not serial mode"
_rw_pin = rw; // global copy of r/w
n = digitalPinToPort (rs); // register select
_RS_BIT = digitalPinToBitMask (rs); // get bitmasks for parallel I/O
_RS_PORT = portOutputRegister (n); // get output ports
_RS_DDR = portModeRegister (n); // get DDR registers
*_RS_DDR |= _RS_BIT; // ddr = output
*_RS_PORT |= _RS_BIT; // initial setting RS = HIGH = data
n = digitalPinToPort (en); // enable
_EN_BIT = digitalPinToBitMask (en);
_EN_PORT = portOutputRegister (n);
_EN_DDR = portModeRegister (n);
*_EN_DDR |= _EN_BIT; // ddr = output
*_EN_PORT &= ~_EN_BIT; // initial = low
if (_rw_pin != NO_RW) { // read/write
n = digitalPinToPort (rw);
_RW_BIT = digitalPinToBitMask (rw);
_RW_PORT = portOutputRegister (n);
_RW_DDR = portModeRegister (n);
*_RW_DDR |= _RW_BIT; // ddr = output
*_RW_PORT &= ~_RW_BIT; // initial setting RW = LOW = write
}
if (v0 != NO_RST) {
_reset_pin = v0; // alternate use of pin
n = digitalPinToPort (_reset_pin); // RESET pin is on V0
_RST_BIT = digitalPinToBitMask (_reset_pin);
_RST_PORT = portOutputRegister (n);
_RST_DDR = portModeRegister (n);
*_RST_DDR |= _RST_BIT; // set it as output
*_RST_PORT &= ~_RST_BIT; // lower reset pin
__builtin_avr_delay_cycles (F_CPU / (_MSEC / 10));
*_RST_PORT |= _RST_BIT; // raise reset pin
}
x = 8;
while (x--) {
n = digitalPinToPort (data_pin[x]);
_BIT_MASK[x] = digitalPinToBitMask (data_pin[x]); // get bitmask
_DATA_DDR[x] = portModeRegister (n); // get DDR register
_DATA_PORT[x] = portOutputRegister (n); // get output port register
_DATA_PIN[x] = portInputRegister (n); // get input port register
// if we are in 4 bit mode then only set d7...d4
if (x == _bit_mode) {
break;
}
}
}
begin (16, 1);
}
void LiquidCrystal::init (uint8_t cols, uint8_t rows, uint8_t dotsize)
{
begin (cols, rows, dotsize);
}
void LiquidCrystal::begin (uint8_t cols, uint8_t rows, uint8_t dotsize)
{
uint8_t x;
_numCols = cols;
_numRows = rows;
// setup default DDRAM offsets
setRowOffsets (0x00, 0x40, 0x14, 0x54);
// use this one if LCD/VFD lines 2 and 3 don't line up properly
// setRowOffsets (0x00, 0x40, 0x10, 0x50);
// we need at least 40ms after power rises above 2.7V before sending commands.
__builtin_avr_delay_cycles (F_CPU / (_MSEC / 50.0));
x = _bit_mode; // save actual bitmode
_bit_mode = MODE_8; // force reset to be 8 bit
// build _displayMode template
// default: increment mode, no shift
_displayMode = (ENTRYMODESET | INCREMENT);
// build _displayControl template
// default: display off, cursor off, blink off
_displayControl = (DISPLAYCTRL);
// build _displayCursor template
// default: cursor move, cursor moves right
_displayCursor = (CURSORSHIFT | MOVERIGHT);
// build _displayFunction template
// default: 8 bit, 1 line, 5 x 8 character
_displayFunction = (FUNCTIONSET | BITMODE8);
// send reset sequence
_send_cmd (_displayFunction);
__builtin_avr_delay_cycles (F_CPU / (_MSEC / 10.0));
_send_cmd (_displayFunction);
__builtin_avr_delay_cycles (F_CPU / (_MSEC / 1.0));
_send_cmd (_displayFunction);
__builtin_avr_delay_cycles (F_CPU / (_MSEC / 1.0));
if (x == MODE_4) { // if actual bitmode is 4 then clear the 8 bit flag
_displayFunction &= ~BITMODE8;
}
if (_numRows > 1) {
_displayFunction |= LINES2;
}
if (dotsize) {
_displayFunction |= DOTS5X10;
}
// finish display reset
_send_cmd (_displayFunction); // set the interface bit mode
_bit_mode = x; // now driver uses 4 or 8 bits
_send_cmd (_displayMode); // entry mode set
_displayControl |= DISPLAYON;
_send_cmd (_displayControl); // turn display on
if (_serial_mode) { // probably a VFD
_send_cmd (FUNCTIONSET);
_send_data (0); // set brightness 100% (VFD only)
}
clearScreen(); // clear display
vt_Reset(); // init vt parser
}
void LiquidCrystal::setBrightness (uint8_t pct)
{
uint8_t brite = 0x03;
uint16_t x = 1000;
// constrain percent
pct = (pct > 100) ? 100 : pct;
// shut off HV inverter & filament on VFD displays
// if brightness of "0" is selected.
if (!pct) {
setDisplay (0);
return;
} else {
setDisplay (1);
}
// multiply everything by 10 so fractional
// numbers are calculated as integers
while (x && brite) {
if ((pct * 10) > (x + 5)) {
brite--;
}
x -= (1000 / 4); // 4 brightness steps
}
// execute a function set
_send_cmd (_displayFunction);
// send the brightness control bits - 0b00:100%, 0b01:75%, 0b10:50%, 0b11:25%
_send_data (brite); // set brightness (VFD only)
}
void LiquidCrystal::home (void)
{
_send_cmd (RETURNHOME);
__builtin_avr_delay_cycles (F_CPU / (_MSEC / 20.0));
setCursor (0, 0);
}
void LiquidCrystal::clearScreen (void)
{
clear();
}
void LiquidCrystal::clear (void)
{
_send_cmd (CLEARDISPLAY);
__builtin_avr_delay_cycles (F_CPU / (_MSEC / 20.0));
setCursor (0, 0);
}
// if using a 16x4 LCD/VFD and line 3 & 4 are not placed correctly try:
// this ---> setRowOffsets(0x00, 0x40, 0x14, 0x54);
// or this ---> setRowOffsets(0x00, 0x40, 0x10, 0x50);
void LiquidCrystal::setRowOffsets (uint8_t row0, uint8_t row1, uint8_t row2, uint8_t row3)
{
_row_offsets[0] = row0;
_row_offsets[1] = row1;
_row_offsets[2] = row2;
_row_offsets[3] = row3;
}
void LiquidCrystal::setLine (uint8_t x, uint8_t y)
{
setCursor (x, y);
}
void LiquidCrystal::getLine (uint8_t &x, uint8_t &y)
{
x = _cur_x;
y = _cur_y;
}
void LiquidCrystal::setCursor (uint8_t x, uint8_t y)
{
_cur_x = x; // record cursor X pos
_cur_y = y; // record cursor Y pos
_send_cmd (SETDDRAMADDR | (_cur_x + _row_offsets[_cur_y]));
}
void LiquidCrystal::getCursor (uint8_t &x, uint8_t &y)
{
x = _cur_x;
y = _cur_y;
}
// for user only
void LiquidCrystal::pushCursor (void)
{
getCursor (_save_x, _save_y);
}
// for user only
void LiquidCrystal::popCursor (void)
{
setCursor (_save_x, _save_y);
}
// compatibility with original LiquidCrystal functions
void LiquidCrystal::noDisplay (void)
{
setDisplay (0);
}
void LiquidCrystal::display (void)
{
setDisplay (1);
}
void LiquidCrystal::noCursor (void)
{
setUnderline (0);
}
void LiquidCrystal::cursor (void)
{
setUnderline (1);
}
void LiquidCrystal::noBlink (void)
{
setBlink (0);
}
void LiquidCrystal::blink (void)
{
setBlink (1);
}
void LiquidCrystal::noUnderline (void)
{
setUnderline (0);
}
void LiquidCrystal::underline (void)
{
setUnderline (1);
}
// This will 'left justify' text from the cursor
void LiquidCrystal::noAutoscroll (void)
{
setAutoscroll (0);
}
// This will 'right justify' text from the cursor
void LiquidCrystal::autoscroll (void)
{
setAutoscroll (1);
}
// Turn the display on/off (quickly)
// NOTE: Also shuts off the cathode!
void LiquidCrystal::setDisplay (uint8_t on)
{
on ? _displayControl |= DISPLAYON : _displayControl &= ~DISPLAYON;
_send_cmd (_displayControl);
}
// Turns the underline cursor on/off
void LiquidCrystal::setUnderline (uint8_t on)
{
on ? _displayControl |= CURSORON : _displayControl &= ~CURSORON;
_send_cmd (_displayControl);
}
// Turn on and off the blinking cursor
void LiquidCrystal::setBlink (uint8_t on)
{
on ? _displayControl |= BLINKON : _displayControl &= ~BLINKON;
_send_cmd (_displayControl);
}
void LiquidCrystal::setAutoscroll (uint8_t on)
{
on ? _displayMode |= DISPLAYSHIFT : _displayMode &= ~DISPLAYSHIFT;
_send_cmd (_displayMode);
}
// These commands scroll the display without changing the RAM
void LiquidCrystal::scrollDisplayLeft (void)
{
_displayCursor |= (CURSORSHIFT | DISPLAYMOVE | MOVERIGHT);
_displayCursor &= ~MOVERIGHT;
_send_cmd (_displayCursor);
}
void LiquidCrystal::scrollDisplayRight (void)
{
_displayCursor |= (CURSORSHIFT | DISPLAYMOVE | MOVERIGHT);
_send_cmd (_displayCursor);
}
// This is for text that flows Left to Right
void LiquidCrystal::leftToRight (void)
{
_displayMode |= INCREMENT;
_send_cmd (_displayMode);
}
// This is for text that flows Right to Left
void LiquidCrystal::rightToLeft (void)
{
_displayMode &= ~INCREMENT;
_send_cmd (_displayMode);
}
// custom bitmaps in SRAM
void LiquidCrystal::createChar (uint8_t addr, const char *bitmap)
{
createChar (addr, (const uint8_t *)(bitmap));
}
// custom bitmaps in SRAM
void LiquidCrystal::createChar (uint8_t addr, const uint8_t *bitmap)
{
uint8_t n;
_clearChar (addr); // erase old LCD/VFD data
_send_cmd (SETCGRAMADDR | ((addr % 8) * 8));
for (n = 0; n < 8; n++) {
_send_data (bitmap[n]); // 8 bytes to a char (but only 5 bits)
}
home(); // make sure cursor isn't fubar
}
// custom bitmaps in PROGMEM
void LiquidCrystal::createChar_P (uint8_t addr, const char *bitmap)
{
createChar_P (addr, (const uint8_t *)(bitmap));
}
void LiquidCrystal::createChar_P (uint8_t addr, const uint8_t *bitmap)
{
uint8_t n;
_clearChar (addr); // erase old LCD/VFD data
_send_cmd (SETCGRAMADDR | ((addr % 8) * 8));
for (n = 0; n < 8; n++) {
_send_data (pgm_read_byte (bitmap + n));
}
home(); // make sure cursor isn't fubar
}
// custom bitmaps in EEPROM
void LiquidCrystal::createChar_E (uint8_t addr, const char *bitmap)
{
createChar_E (addr, (const uint8_t *)(bitmap));
}
// custom bitmaps in EEPROM
void LiquidCrystal::createChar_E (uint8_t addr, const uint8_t *bitmap)
{
uint8_t n;
_clearChar (addr); // erase old LCD/VFD data
_send_cmd (SETCGRAMADDR | ((addr % 8) * 8));
for (n = 0; n < 8; n++) {
_send_data (eeprom_read_byte ((const uint8_t *)(bitmap + n)));
}
home(); // make sure cursor isn't fubar
}
void LiquidCrystal::vt_Reset (void)
{
vt_state = 0;
vt_cmd = 0;
vt_args = 8;
while (vt_args--) {
vt_arg[vt_args] = 0;
}
vt_args = 0;
}
size_t LiquidCrystal::vt_Exec (void)
{
uint8_t args;
switch (vt_cmd) {
case 'f':
case 'H': {
// must have 0, 1 or 2 params
// based on cols and rows starting at ZERO unlike the ANSI cmd
if (vt_args < 3) {
if ((vt_arg[0] < _numCols) && (vt_arg[1] < _numRows)) {
setLine (vt_arg[0], vt_arg[1]);
}
}
break;
}
case 'J': {
// valid param is missing, 0, 1, 2 or 3
if (vt_args < 4) {
clearScreen();
}
break;
}
case 'm': {
for (args = 0; args < vt_args; args++) { // handle multiple SGR params
switch (vt_arg[args]) {
case 0: { // reset / normal
setBrightness (75);
continue;
}
case 1: { // bold / bright
setBrightness (100);
continue;
}
case 2: { // faint / dim
setBrightness (25);
continue;
}
default: {
continue;
}
}
}
break;
}
}
vt_Reset();
return 0;
}
size_t LiquidCrystal::write (uint8_t c)
{
switch (vt_state) {
// state 0 is "ordinary character" or "ground state"
case 0: {
if (c == 0x1B) { // VT code starts with ESC
vt_state++; // flag "got ESC, look for more VT
return 0; // got part of a vt sequence, don't print it
} else { // not start of an ANSI sequence
vt_Reset(); // reset parser
break; // fall through to main write
}
}
// state 1 is "got VT escape (0x1B) look for left bracket (0x5B)"
case 1: {
if (c == '[') { // VT esc code followed by "["
vt_state++; // flag "got a sequence, look for a param"
return 0; // got part of a vt sequence, don't print it
} else { // don't have esc[
vt_Reset(); // reset parser
break; // fall through to main write
}
}
// state 2...9 is "get parameter" (max 8 params)
case 2 ... 9: {
if (isdigit (c)) { // if 0...9 then it's a parameter
vt_arg[vt_args] *= 10; // parse out...
vt_arg[vt_args] += (c - '0'); // ...first param
return 0; // got part of a vt sequence, don't print it
} else if (c == ';') { // semicolon flags a parameter delimiter
vt_args++; // count parsed arg
vt_state++; // flag "got param delimiter, look for next param"
return 0; // got part of a vt sequence, don't print it
} else if (! ((c < '@') || (c > '~'))) { // 0x40...0x7E marks end of VT command
vt_cmd = c; // copy VT command
vt_args++; // normalize count
return vt_Exec(); // got a valid sequence, exec it
}
}
default: {
vt_Reset(); // unknown piece of vt, reject it and print
break;
}
}
switch (c) {
case '\b': {
return _backSpace();
}
case '\t': {
return _doTabs (4);
break;
}
case '\n': {
return _lineFeed();
}
case '\f': {
clearScreen();
return 0;
}
case '\r': {
return _carriageReturn();
}
default: {
_send_data (c);
if (_cur_x < (_numCols - 1)) { // if next col pos isn't at end
_cur_x++;
} else {
_cur_x = 0;
if (_cur_y < (_numRows - 1)) { // need new row
_cur_y++;
} else {
_cur_x = 0;
_cur_y = 0;
}
}
setCursor (_cur_x, _cur_y);
break;
}
}
return 1;
}
void LiquidCrystal::_clearChar (uint8_t addr)
{
uint8_t n;
_send_cmd (SETCGRAMADDR | ((addr % 8) * 8));
for (n = 0; n < 8; n++) {
_send_data (0); // erase old
}
}
size_t LiquidCrystal::_backSpace (void)
{
uint8_t _tmp_x = _cur_x;
uint8_t _tmp_y = _cur_y;
if (_tmp_x) {
_tmp_x--;
} else {
_tmp_x = (_numCols - 1);
if (_tmp_y) {
_tmp_y--;
} else {
_tmp_y = (_numRows - 1);
}
}
setCursor (_tmp_x, _tmp_y);
write ((uint8_t)(' '));
setCursor (_tmp_x, _tmp_y);
return 0;
}
size_t LiquidCrystal::_lineFeed (void)
{
if (_cur_y < (_numRows - 1)) {
_cur_y++;
} else {
_cur_y = 0;
}
setCursor (_cur_x, _cur_y);
return 0;
}
size_t LiquidCrystal::_carriageReturn (void)
{
setCursor (0, _cur_y);
return 0;
}
// move cursor to next tab stop (4 places = 1 "tab")
size_t LiquidCrystal::_doTabs (uint8_t _tab_size)
{
size_t n = 0;
if (! (_cur_x % _tab_size)) {
n += write ((uint8_t)(' '));
}
while (_cur_x % _tab_size) {
n += write ((uint8_t)(' '));
}
return n;
}
uint8_t LiquidCrystal::_recv_stat (void)
{
return _recv (_STAT); // rs = low
}
uint8_t LiquidCrystal::_recv_data (void)
{
return _recv (_DATA); // rs = high
}
void LiquidCrystal::_send_cmd (uint8_t cmd)
{
_send (cmd, _CMD); // rs = low
}
void LiquidCrystal::_send_data (uint8_t dat)
{
_send (dat, _DATA); // rs = high
}
// read either status or data determined by rs (register select)
// if rs = 1 then we are reading DD RAM or CG RAM
// if rs = 0 then we are reading BF (Busy Flag) and LCD/VFD address
// RW is set high (read) for SPI, it's set high (read) for
// parallel ONLY IF the RW pin is selected, defined and used.
uint8_t LiquidCrystal::_recv (uint8_t rs)
{
uint8_t c;
if (_serial_mode) { // set or clear RS bit in serial command byte
rs ? _serial_cmd |= _RSBIT : _serial_cmd &= ~_RSBIT;
_serial_cmd |= _RWBIT; // read mode
*_STB_PORT &= ~_STB_BIT; // assert strobe
_serialSend (_serial_cmd); // send command via serial
c = _serialRecv(); // recv data via serial
*_STB_PORT |= _STB_BIT; // de-assert strobe
} else { // set or clear RS pin (parallel mode)
rs ? *_RS_PORT |= _RS_BIT : *_RS_PORT &= ~_RS_BIT;
if (_rw_pin != NO_RW) {
*_RW_PORT |= _RW_BIT; // set r/w high = read
_setDDR (_READ); // set port to read
} else {
return 0; // can't set r/w so just return
}
if (_bit_mode == MODE_4) {
c = (_recv4bits() << 4); // recv top half of byte
c |= _recv4bits(); // recv bottom half of byte
} else {
c = _recv8bits(); // recv status or data via parallel
}
}
return c;
}
// write either command or data determined by rs (register select)
// if rs = 1 then we are transferring text or command param data
// if rs = 0 then we are sending an HD44780 command
// RW is set low (write) for SPI, it's set low (write) for parallel
// ONLY IF the RW pin is selected, defined and used.
void LiquidCrystal::_send (uint8_t c, uint8_t rs)
{
if (_serial_mode) { // set or clear RS bit in serial command byte
rs ? _serial_cmd |= _RSBIT : _serial_cmd &= ~_RSBIT;
_serial_cmd &= ~_RWBIT; // write mode
*_STB_PORT &= ~_STB_BIT; // assert strobe
_serialSend (_serial_cmd); // send command via serial
_serialSend (c); // send data via serial
*_STB_PORT |= _STB_BIT; // de-assert strobe
} else { // set or clear RS pin (parallel mode)
rs ? *_RS_PORT |= _RS_BIT : *_RS_PORT &= ~_RS_BIT;
if (_rw_pin != NO_RW) {
*_RW_PORT &= ~_RW_BIT; // set r/w low = write
_setDDR (_WRITE); // set port to write
}
if (_bit_mode == MODE_4) {
_send4bits (c >> 4); // send top half of byte
_send4bits (c & 0x0F); // send bottom half of byte
} else {
_send8bits (c); // send command or data via parallel
}
}
}
// parallel 4 bit mode (we receive top 4 bits, then bottom 4)
uint8_t LiquidCrystal::_recv4bits (void)
{
uint8_t c = 0;
uint8_t n = 4;
while (n--) { // 4 bits parallel
*_DATA_PIN[n + 4] & _BIT_MASK[n + 4] ? c |= (1 << n) : c &= ~(1 << n); // receive bit
}
*_EN_PORT |= _EN_BIT;
__builtin_avr_delay_cycles (F_CPU / (_USEC / 1.0));
*_EN_PORT &= ~_EN_BIT;
return c;
}
// parallel 8 bit mode (we receive all 8 bits at once)
uint8_t LiquidCrystal::_recv8bits (void)
{
uint8_t c = 0;
uint8_t n = 8;
while (n--) { // 8 bits parallel
*_DATA_PIN[n] & _BIT_MASK[n] ? c |= (1 << n) : c &= ~(1 << n); // receive bit
}
*_EN_PORT |= _EN_BIT;
__builtin_avr_delay_cycles (F_CPU / (_USEC / 1.0));
*_EN_PORT &= ~_EN_BIT;
return c;
}
// parallel 4 bit mode (we send top 4 bits, then bottom 4)
void LiquidCrystal::_send4bits (uint8_t c)
{
uint8_t n = 4; // bit count
while (n--) { // 4 bits parallel
c & (1 << n) ? *_DATA_PORT[n + 4] |= _BIT_MASK[n + 4] : *_DATA_PORT[n + 4] &= ~_BIT_MASK[n + 4];
}
*_EN_PORT |= _EN_BIT;
__builtin_avr_delay_cycles (F_CPU / (_USEC / 1.0));
*_EN_PORT &= ~_EN_BIT; // latch data
}
// parallel 8 bit mode (we send all 8 bits at once)
void LiquidCrystal::_send8bits (uint8_t c)
{
uint8_t n = 8; // bit count
while (n--) { // 8 bits parallel
c & (1 << n) ? *_DATA_PORT[n] |= _BIT_MASK[n] : *_DATA_PORT[n] &= ~_BIT_MASK[n];
}
*_EN_PORT |= _EN_BIT;
__builtin_avr_delay_cycles (F_CPU / (_USEC / 1.0));
*_EN_PORT &= ~_EN_BIT; // latch data
}
void LiquidCrystal::_serialSend (uint8_t c)
{
uint8_t n = 8;
*_SIO_DDR |= _SIO_BIT; // SIO as output
while (n--) {
*_SCK_PORT &= ~_SCK_BIT; // set sck low
__builtin_avr_delay_cycles (F_CPU / (_NSEC / 150.0));
c & (1 << n) ? *_SIO_PORT |= _SIO_BIT : *_SIO_PORT &= ~_SIO_BIT; // write bit
*_SCK_PORT |= _SCK_BIT; // set sck high
}
}
uint8_t LiquidCrystal::_serialRecv (void)
{
uint8_t c = 0;
uint8_t n = 8;
*_SIO_DDR &= ~_SIO_BIT; // SIO as input
while (n--) {
*_SCK_PORT &= ~_SCK_BIT; // set sck low
__builtin_avr_delay_cycles (F_CPU / (_NSEC / 150.0));
*_SIO_PIN & _SIO_BIT ? c |= (1 << n) : c &= ~(1 << n); // read bit
*_SCK_PORT |= _SCK_BIT; // set sck high
}
return c;
}
void LiquidCrystal::_setDDR (uint8_t pattern)
{
uint8_t x = 8;
while (x--) {
pattern ? *_DATA_DDR[x] &= ~_BIT_MASK[x] : *_DATA_DDR[x] |= _BIT_MASK[x];
// if we are in 4 bit mode then only set d7...d4
if (x == _bit_mode) {
break;
}
}
}
// end of LiquidCrystal.cpp