-
Notifications
You must be signed in to change notification settings - Fork 0
/
AltSerialGraphicLCD.cpp
668 lines (588 loc) · 19.3 KB
/
AltSerialGraphicLCD.cpp
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
/* -!- C++ -!- ********************************************* Alternative
* Graphic Serial LCD Libary Header File
*
* Parts of this library are based on the original Sparkfun library by:
*
* Joel Bartlett
* SparkFun Electronics
* 9-25-13
*
* Jon Green - 2015-04-01
* New interface and in-line definitions added 2015-04-01 and library
* re-formatted
*
* Dan Cech - 2019-01-07
* Removed PROGMEM to support use on ESP8266/ESP32
*
* Copyright (c) 2019 Dan Cech
* Copyright (c) 2015 Jon Green
*
* 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.
*
***************************************************************************/
#include <stdarg.h>
#include <Arduino.h>
#include <SoftwareSerial.h>
#include "AltSerialGraphicLCD.h"
static char const _crlf[] = "\r\n";
//----------------------------------------------------------------------------
// Constructor
GLCD::GLCD(SoftwareSerial& software_serial)
: serial(software_serial)
{
blocked = 0; // Assume unblocked.
graphics_on = 0; // Graphics sending is off.
crlf = _crlf; // The end of line string
}
#ifdef GLCD_GET_IS_REQUIRED // We do not use this disable.
//----------------------------------------------------------------------------
// Gat a character from the screen, if there is nothing available then return
// -1.
int
GLCD::get ()
{
int cc;
// Silently consume the XON/OFF and return anything else to the caller.
while ((cc = serial.read()) != -1)
{
// mask out any top bit.
uint8_t rc = cc & 0x7f;
// Check for a XON/XOFF signal
if (rc == GLCD_CHAR_XON)
{
// Unblocked, reset the flag and countdown.
blocked = 0;
}
else if (rc == GLCD_CHAR_XOFF)
{
blocked = 1; // Blocking
}
break;
}
// Return the character
return cc;
}
#endif // End we do not use this disable
//----------------------------------------------------------------------------
// Wait until the screen is ready to receive a character.
void
GLCD::ready ()
{
int rc;
// Consume all of the input pending.
while ((rc = serial.read()) != -1)
{
uint8_t uc = rc & 0x7f;
// Process the XON/XOFF characters
if (uc == GLCD_CHAR_XON)
this->blocked = 0;
else if (uc == GLCD_CHAR_XOFF)
this->blocked = 1;
}
// Test to ensure that the screen is not requesting us to stop sending.
if (this->blocked != 0)
{
unsigned long startMillis = millis();
unsigned long nowMillis;
// Keep polling the serial while we are blocked. Make sure we do not
// block indefinitely by using a timer. There is a chance that we
// migth drop an XON so we do not want to wait forever when nothing
// might arrive.
for (;;)
{
// Read the serial
if ((rc = serial.read ()) != -1)
{
uint8_t uc = rc & 0x7f;
// Process the XON/XOFF characters
if (uc == GLCD_CHAR_XON)
{
this->blocked = 0;
// Get out quickly
break;
}
else if (uc == GLCD_CHAR_XOFF)
{
// Still blocked, reset the timer.
this->blocked = 1;
startMillis = millis();
}
}
// Make sure we have not missed a XON character, if XON is not
// received in 20 milliseconds then continue, there should be
// enough buffer space to hold the command, we will get a XOFF if
// the buffer is still full.
nowMillis = millis();
if ((nowMillis - startMillis) > 20)
{
// Assume we are unblocked.
this->blocked = 0;
break;
}
// Make sure that the counter does not wrap, if it does
// (unlikely) then get another snapshot of the timer.
else if (nowMillis < startMillis)
startMillis = nowMillis;
}
}
}
//----------------------------------------------------------------------------
// Put a character to the screen. Check that we are not blocked.
void
GLCD::put (uint8_t cc)
{
// Wait for the screen to be ready.
this->ready();
// Send the character, we are not blocked.
serial.write (cc);
}
/////////////////////////////////////////////////////////////////////////////
/// Print a flash string
///
/// @param [in] s The flash string to print.
/// const __FlashStringHelper *
void
GLCD::putstr_P (const char *s)
{
uint8_t cc;
uint8_t ii = 0;
// Read to the end of the string.
while ((cc = pgm_read_byte(s++)) != '\0')
{
// Test for XON/XOFF
if ((ii & xoff_mask) == 0)
this->ready (); // XON/XOFF check
serial.write (cc); // Write the data.
}
}
/////////////////////////////////////////////////////////////////////////////
/// Print a regular string
///
/// @param [in] s The string to print.
///
void
GLCD::putstr (char *s)
{
uint8_t cc;
uint8_t ii = 0;
// Read to the end of the string.
while ((cc = *s++) != '\0')
{
// Test for XON/XOFF
if ((ii & xoff_mask) == 0)
this->ready (); // XON/XOFF check
serial.write (cc); // Write the data
}
}
//----------------------------------------------------------------------------
// Write a character block from RAM to the screen.
void
GLCD::write (uint8_t *data, int length)
{
// Write out 'length' bytes of data from RAM
while (length > 0)
{
uint8_t cc = *data++;
// Periodically test for XON/XOFF
if ((length & xoff_mask) == 0)
this->ready ();
serial.write (cc);
length--;
}
}
//----------------------------------------------------------------------------
// Write a character block from program memory to the screen.
void
GLCD::write_P (const uint8_t *data, int length)
{
// Write out 'length' bytes of data from RAM
while (length > 0)
{
uint8_t cc = pgm_read_byte(data++);
// Periodically test for XON/XOFF
if ((length & xoff_mask) == 0)
this->ready ();
serial.write (cc);
length--;
}
}
//----------------------------------------------------------------------------
// Put a command to the screen.
void
GLCD::putcmd (uint8_t cmd, uint8_t argc, ...)
{
// Wait for the screen to be ready.
this->ready ();
// Check for graphics mode.
if (graphics_on == 0)
serial.write (GLCD_CHAR_CMD);
// Push out the command.
serial.write (cmd);
// Pick up any arguments and send them.
if (argc > 0)
{
uint8_t argm; // Argument mask
uint8_t argd; // Dynamic argument
va_list ap; // Variable argument list
// Get the argument pointer.
va_start (ap, argc);
// Save the argument mask
argm = argc;
argc = argc & 7; // There are max 6 arguments
if (argc > 0)
{
do
{
uint8_t cc; // Temporary character
cc = va_arg (ap, int) & 0xff; // Get variable argument.
serial.write (cc); // Send to the serial.
}
while (--argc > 0);
}
// Process the argument mask.
if ((argd = (argm & GLCD_ARG_TYPE_MASK)) != 0)
{
uint8_t cc;
int length;
uint8_t *ptr;
// Argument is (...., width, height, ptr)
if (argd == GLCD_ARG_SPRITE_WH)
{
// Get the width;
cc = (uint8_t)(va_arg (ap, int) & 0xff);
serial.write (cc);
length = cc;
// Get the height. The height is specified in pixels and must
// be converted to bytes
cc = (uint8_t)(va_arg (ap, int) & 0xff);
serial.write (cc);
length *= (uint8_t)((cc + 7) >> 3);
// Change the type to GLCD_ARG_SIZEOF for the reading of
// data and process as (..., length, ptr).
argd = GLCD_ARG_SIZEOF;
ptr = va_arg (ap, uint8_t *);
}
// Argument is (...., length, ptr)
else if (argd == GLCD_ARG_SIZEOF)
{
// Get the length.
length = va_arg (ap, int);
ptr = va_arg (ap, uint8_t *);
}
// Argument is (...., ptr) which point to sprite data.
else if (argd == GLCD_ARG_SPRITE)
{
// Get the pointer.
ptr = va_arg (ap, uint8_t *);
// Get the width
if ((argm & GLCD_ARG_PROGMEM) != 0)
cc = pgm_read_byte (ptr++);
else
cc = *ptr++;
serial.write (cc);
length = cc;
// Get the height. The height is specified in pixels and must
// be converted to bytes
if ((argm & GLCD_ARG_PROGMEM) != 0)
cc = pgm_read_byte (ptr++);
else
cc = *ptr++;
serial.write (cc);
length *= (cc + 7) >> 3;
// Change the type to GLCD_ARG_SIZEOF for the reading of
// data and process as (..., length, ptr).
argd = GLCD_ARG_SIZEOF;
}
// Argument is (..., ptr) where ptr is a list of (x,y)
// coordinates terminated with the marker (y & 0x80 != 0)
else if (argd == GLCD_ARG_XY_LIST)
{
// Handle
if ((argm & GLCD_ARG_PROGMEM) != 0)
{
const uint8_t *p;
p = va_arg (ap, const uint8_t *);
do
{
// Make sure we can write
this->ready();
// Get x
cc = pgm_read_byte (p++);
serial.write (cc);
// Get y
cc = pgm_read_byte (p++);
serial.write (cc);
}
while ((cc & 0x80) == 0);
}
// Handle regular memory
else
{
char *p = va_arg (ap, char *);
do
{
// Make sure we can write
this->ready();
// Get x
serial.write (*p++);
// Get y
cc = *p++;
serial.write (cc);
}
while ((cc & 0x80) == 0);
}
}
else if (argd == GLCD_ARG_FFSTRING)
{
// Put the string
if ((argm & GLCD_ARG_PROGMEM) != 0)
this->putstr_P (va_arg (ap, const char *));
else
this->putstr (va_arg (ap, char *));
// Terminate the string with 0xff
serial.write (0xff);
}
// Argument is (..., length, ptr)
if (argd == GLCD_ARG_SIZEOF)
{
// Perform a write operation.
if ((argm & GLCD_ARG_PROGMEM) != 0)
this->write_P ((const uint8_t *) ptr, length);
else
this->write (ptr, length);
}
}
// Close the variable argument list.
va_end (ap);
}
}
//////////////////////////////////////////////////////////////////////////////
// Wait for a character with a timeout.
//
int
GLCD::waitc (uint8_t expected, int msdelay)
{
int cc;
int ii;
// Wait for a response of 'expected', we allow 2 seconds of inactivity to retrieve.
ii = msdelay;
for (;;)
{
// Break out if this is the query response.
if ((cc = serial.read()) != -1)
{
uint8_t uc8 = cc & 0xff;
// See if this is the character we are looking for
if (expected != 0)
{
// If we are looking for a specific character then return
// when we have a match.
if (uc8 == expected)
break;
}
else
{
// We are looking for any character - return what we read.
break;
}
// Handle XON/XOFF
if (uc8 == GLCD_CHAR_XON)
this->blocked = 0;
else if (uc8 == GLCD_CHAR_XOFF)
this->blocked = 1;
// Reset the inactivity counter.
ii = msdelay;
}
// Make sure we have not expired the loop.
if (ii == 0)
break; // Return error.
// Delay 1ms and decrement the wait counter.
delay (1);
ii--;
}
// Must have timed out.
return cc;
}
/////////////////////////////////////////////////////////////////////////////
/// Reset the screen.
///
void
GLCD::reset()
{
int cc; // Working character
// First make sure that we can communicate with the screen. If a bitblt
// or polygon operation was interrrupted accross out reset then the
// screen will be waiting for more characters so we need to feed it
// before we perform the reset.
do
{
// See if the screen is responsive. We use a non-drawable and
// non-command character
if ((cc = this->echoWait (0xf7, 1)) == -1)
{
// Un-responsive, push a dummy pixel. This will feed any bitblt
// in addtion to terminating any polygon line, otherwise the
// pixel draw will be clipped off-screen and do nothing.
this->drawPixel (0xff, 0xff);
}
}
while (cc != 0xf7);
// Flush any pending write data.
while (serial.read() != -1)
/* Do nothing */;
// We have re-established control of the screen, turn graphics off.
this->graphics_on = 0; // Graphics sending is off.
// Get the size of the screen, we will be lazy and simply initialise by
// getting the values straight from the screen and not deduce anything.
// xdim = this->query (0x40);
// ydim = this->query (0x41);
while ((cc = this->query (10)) == -1)
;
// Set up the dimensions
if (cc == 0)
{
xdim = 128;
ydim = 64;
}
else
{
xdim = 160;
ydim = 128;
}
// Send a reset.
this->putcmd (GLCD_CMD_RESET, 0);
// Wait for up to 2s for the XON to indicate the screen has started. This
// is lots of time and should be a lot quicker than this.
this->waitc (GLCD_CHAR_XON, 2000);
this->blocked = 0;
// Finished - we are now in a usable initial state and can send commands.
}
/////////////////////////////////////////////////////////////////////////////
/// Query the screen for information.
///
/// @param [in] id The identitiy of the datum to retrieve
///
/// @return The data associated with the identity or -1 on error.
int
GLCD::query (uint8_t id)
{
int cc;
int dd;
// Put the query command.
this->putcmd (GLCD_CMD_QUERY, 1, id);
// Wait for a 'Q' response to the query and return the next character
// read.
if ((cc = this->waitc ('Q', 2000)) == 'Q')
cc = this->waitc (0, 500);
return cc;
}
/////////////////////////////////////////////////////////////////////////////
/// Echo to the screen and wait for the character to come back
///
/// @param [in] echar The character to echo
/// @param [in] msdelay The millisecond delay
///
/// @return The character received.
int
GLCD::echoWait (uint8_t echar, int msdelay)
{
int cc;
// Put the query command.
echo (echar);
// Wait for the echo character to come back.
return waitc (echar, msdelay);
}
//-------------------------------------------------------------------------------------------
// Change the baud rate.
//
// '1'/0x31/49 or 0x01 = 4800bps
// '2'/0x32/50 or 0x02 = 9600bps
// '3'/0x33/51 or 0x03 = 19,200bps
// '4'/0x34/52 or 0x04 = 38,400bps
// '5'/0x35/53 or 0x05 = 57,600bps
// '6'/0x36/54 or 0x06 = 115,200bps
void
GLCD::setBaud (byte baud)
{
// Changes the baud rate.
this->putcmd (GLCD_CMD_CHANGE_BAUD_RATE, 1, baud);
delay(100);
// Allow an integer argument.
if (baud >= '0')
baud -= '0';
// These statements change the SoftwareSerial baud rate to match the baud
// rate of the LCD.
if ((baud >= 1) && (baud <= 6))
{
uint32_t rate;;
// Stop the current serial session.
serial.end();
switch (baud)
{
case 1:
rate = 4800;
break;
case 2:
rate = 9600;
break;
case 3:
rate = 19200;
break;
case 4:
rate = 38400;
break;
case 5:
rate = 57600;
break;
default:
rate = 115200;
break;
}
serial.begin (rate);
}
}
//-------------------------------------------------------------------------------------------
void
GLCD::restoreDefaultBaud()
{
//This function is used to restore the default baud rate in case you change it
//and forget to which rate it was changed.
serial.end();//end the transmission at whatever the current baud rate is
// Cycle through every other possible buad rate and attempt to change the
// rate back to 115200
serial.end();
serial.begin(4800);
this->setBaud (6); //set back to 115200
serial.end();
serial.begin(9600);
this->setBaud (6); //set back to 115200
serial.end();
serial.begin(19200);
this->setBaud (6); //set back to 115200
serial.end();
serial.begin(38400);
this->setBaud (6); //set back to 115200
serial.end();
serial.begin(57600);
this->setBaud (6); //set back to 115200
serial.end();
serial.begin(115200);
this->clearScreen();
this->putstr (F("Baud restored to 115200"));
}