Serial Wombat Arduino Library
Loading...
Searching...
No Matches
SerialWombatLiquidCrystal.h
Go to the documentation of this file.
1#pragma once
2/*
3Copyright 2022-2023 Broadwell Consulting Inc.
4
5"Serial Wombat" is a registered trademark of Broadwell Consulting Inc. in
6the United States. See SerialWombat.com for usage guidance.
7
8Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14
15The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17
18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
25*/
26
27
28#include <stdint.h>
29#include "SerialWombat.h"
30
31
34
67
68class
69 SerialWombatLiquidCrystal : public Print, public SerialWombatPin
70{
71public:
82 SerialWombatLiquidCrystal(SerialWombatChip& serialWombat, uint8_t rs, uint8_t enable, uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7):SerialWombatPin(serialWombat)
83 {
85 _pin = enable;
86 _rs = rs;
87 _d4 = d4;
88 _d5 = d5;
89 _d6 = d6;
90 _d7 = d7;
91 }
92
93
101 int16_t begin(uint8_t cols = 80, uint8_t rows = 1)
102 {
103 uint8_t tx[8] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_MODE0,
104 _pin,
105 _pinMode,
106 _rs,
107 _d4,
108 _d5,
109 _d6,
110 _d7 };
111 int16_t result = _sw.sendPacket(tx);
112 if (result < 0)
113 {
114 return (result);
115 }
116 _rows = rows;
117 _columns = cols;
118 result = setRowOffsets(0, 0x40, cols, 0x40 + cols);
119 if (result < 0)
120 {
121 return (result);
122 }
123
124 return(0);
125 }
126
136 int16_t clear(bool delayAfterClear = true)
137 {
138 return command(0x01); // Clear display
139 if (delayAfterClear)
140 {
141 delay(2);
142 }
143 }
144
145
156 int16_t home(bool delayAfterHome = true)
157 {
158 return command(0x02); // Clear display
159 if (delayAfterHome)
160 {
161 delay(2);
162 }
163 }
164
173 int16_t setCursor(uint8_t col, uint8_t row)
174 {
175 if (row >= _rows)
176 {
177 return -1;
178 }
179
180 if (col >= _columns)
181 {
182 return -1;
183 }
184 return command(0x80 + _offsets[row] + col);
185 }
186
192 int16_t noCursor()
193 {
194 uint8_t tx[8] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_MODE3,
195 _pin,
196 _pinMode,
197 0, // DisplayControl
198 (uint8_t)~0x02, // And Mask
199 0, // OR MASK
200 0x55,0x55
201 };
202 return _sw.sendPacket(tx);
203 }
204
210 int16_t cursor()
211 {
212 uint8_t tx[8] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_MODE3,
213 _pin,
214 _pinMode,
215 0, // DisplayControl
216 0xFF, // And Mask
217 0x02, // OR MASK
218 0x55,0x55
219 };
220 return _sw.sendPacket(tx);
221 }
222
228 int16_t blink()
229 {
230 uint8_t tx[8] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_MODE3,
231 _pin,
232 _pinMode,
233 0, // DisplayControl
234
235 0xFF, // And Mask
236 0x01, // OR MASK
237 0x55,0x55
238 };
239 return _sw.sendPacket(tx);
240 }
241
247 int16_t noBlink()
248 {
249 uint8_t tx[8] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_MODE3,
250 _pin,
251 _pinMode,
252 0, // DisplayControl
253
254 (uint8_t)~0x01, // And Mask
255 0, // OR MASK
256 0x55,0x55
257 };
258 return _sw.sendPacket(tx);
259 }
260
261
267 int16_t display()
268 {
269 uint8_t tx[8] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_MODE3,
270 _pin,
271 _pinMode,
272 0, // DisplayControl
273
274 0xFF, // And Mask
275 0x04, // OR MASK
276 0x55,0x55
277 };
278 return _sw.sendPacket(tx);
279 }
280
286 int16_t noDisplay()
287 {
288 uint8_t tx[8] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_MODE3,
289 _pin,
290 _pinMode,
291 0, // DisplayControl
292
293 (uint8_t)~0x04, // And Mask
294 0, // OR MASK
295 0x55,0x55
296 };
297 return _sw.sendPacket(tx);
298 }
299
308 {
309 return command(0x18); // Display Move left LCD
310
311 }
312
321 {
322 return command(0x1C); // Display Move right LCD
323 }
324
330 int16_t autoscroll()
331 {
332 uint8_t tx[8] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_MODE3,
333 _pin,
334 _pinMode,
335 1, // DisplayMode
336 0xFF, // And Mask
337 0x01, // OR MASK
338 0x55,0x55
339 };
340 return _sw.sendPacket(tx);
341 }
342
343
349 int16_t noAutoscroll()
350 {
351 uint8_t tx[8] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_MODE3,
352 _pin,
353 _pinMode,
354 1, // DisplayMode
355 (uint8_t)~0x01, // And Mask
356 0x00, // OR MASK
357 0x55,0x55
358 };
359 return _sw.sendPacket(tx);
360 }
361
362
370 int16_t leftToRight()
371 {
372 uint8_t tx[8] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_MODE3,
373 _pin,
374 _pinMode,
375 1, // DisplayMode
376 0xFF, // And Mask
377 0x02, // OR MASK
378 0x55,0x55
379 };
380 return _sw.sendPacket(tx);
381 }
382
390 int16_t rightToLeft()
391 {
392 uint8_t tx[8] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_MODE3,
393 _pin,
394 _pinMode,
395 1, // DisplayMode
396 (uint8_t)~0x02, // And Mask
397 0x00, // OR MASK
398 0x55,0x55
399 };
400 return _sw.sendPacket(tx);
401 }
402
413 size_t write(uint8_t data)
414{
415 uint8_t tx[8] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_MODE4,
416 _pin,
417 (uint8_t)PIN_MODE_LIQUIDCRYSTAL,
418 2, // Data
419 data,
420 0x55,
421 0x55,
422 0x55
423 };
424 if (_sw.sendPacket(tx) >= 0)
425 {
426 return 1;
427 }
428 return 0;
429}
430
442 size_t write(uint8_t* buffer, size_t count)
443 {
444 size_t sent = 0;
445 while (sent < count - 5)
446 {
447 uint8_t tx[8] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_MODE5,
448 _pin,
449 (uint8_t)PIN_MODE_LIQUIDCRYSTAL,
450 buffer[sent],
451 buffer[sent + 1],
452 buffer[sent + 2],
453 buffer[sent + 3],
454 buffer[sent + 4],
455 };
456 if (_sw.sendPacket(tx) < 0)
457 {
458 return sent;
459 }
460 sent += 5;
461 }
462 while (sent < count - 1)
463 {
464 uint8_t tx[8] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_MODE4,
465 _pin,
466 (uint8_t)PIN_MODE_LIQUIDCRYSTAL,
467 2, // Data
468 buffer[sent],
469 2, // Data
470 buffer[sent + 1],
471 0x55
472 };
473 if (_sw.sendPacket(tx) < 0)
474 {
475 return sent;
476 }
477 sent += 2;
478 }
479 if (sent < count)
480 {
481 uint8_t tx[8] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_MODE4,
482 _pin,
483 (uint8_t)PIN_MODE_LIQUIDCRYSTAL,
484 2, // Data
485 buffer[sent],
486 0x55,
487 0x55,
488 0x55
489 };
490 if (_sw.sendPacket(tx) >= 0)
491 {
492 ++sent;
493 }
494 }
495 return sent;
496 }
497
506 int16_t createChar(uint8_t index, uint8_t bitmap[])
507 {
508 if (index >= 8)
509 {
510 return (-1);
511 }
512 int16_t result = command((uint8_t)(0x40 | (index << 3))); // Set character ram location
513 if (result < 0)
514 {
515 return result;
516 }
517 for (int i = 0; i < 8; ++i)
518 {
519 result =(int16_t) write(bitmap[i]);
520 if (result < 0)
521 {
522 return result;
523 }
524 }
525 return 0;
526 }
527
543 int16_t setRowOffsets(uint8_t row1, uint8_t row2, uint8_t row3, uint8_t row4)
544 {
545 _offsets[0] = row1;
546 _offsets[1] = row2;
547 _offsets[2] = row3;
548 _offsets[3] = row4;
549 uint8_t tx[8] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_MODE8,
550 _pin,
551 _pinMode,
552 0,row1,0x55,0x55
553 };
554 int16_t result = _sw.sendPacket(tx); if (result < 0) { return result; };
555 tx[3] = 1; tx[4] = row2;
556 result = _sw.sendPacket(tx); if (result < 0) { return result; };
557 tx[3] = 2; tx[4] = row3;
558 result = _sw.sendPacket(tx); if (result < 0) { return result; };
559 tx[3] = 3; tx[4] = row4;
560 result = _sw.sendPacket(tx); if (result < 0) { return result; };
561 return(0);
562 }
563
571 int16_t command(uint8_t cmd)
572 {
573 uint8_t tx[8] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_MODE4,
574 _pin,
575 (uint8_t)PIN_MODE_LIQUIDCRYSTAL,
576 1, // Command
577 cmd,
578 0x55,
579 0x55,
580 0x55
581 };
582 return _sw.sendPacket(tx);
583 }
584
590 uint8_t pin() { return _pin; }
591
597 uint8_t swPinModeNumber() { return _pinMode; }
598
612 int16_t initializeBufferCopy(uint16_t bufferIndex, uint8_t width = 255)
613 {
614 if (width == 255)
615 {
616 width = _columns;
617 }
618 uint8_t tx[8] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_MODE6,
619 _pin,
620 (uint8_t)PIN_MODE_LIQUIDCRYSTAL,
621 (byte)(bufferIndex & 0xFF),
622 (byte)(bufferIndex >>8),
623 width,
624 0x55,
625 0x55,
626
627 };
628 return _sw.sendPacket(tx);
629 }
630//TODO int16_t dataMonitor(uint8_t pin, uint8_t col, uint8_t row, uint8_t index, bool hex=false);
631
644 int16_t beginE2(uint8_t e2Pin)
645 {
646 uint8_t tx[8] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_MODE7,
647 _pin,
648 (uint8_t)PIN_MODE_LIQUIDCRYSTAL,
649 e2Pin,
650 0x55,
651 0x55,
652 0x55,
653 0x55,
654
655 };
656 return _sw.sendPacket(tx);
657 }
658
659 using Print::write; //Inherit the write commands from print.
660
661
662private:
663 uint8_t _rs = 255,
664 _d4 = 255,
665 _d5 = 255,
666 _d6 = 255,
667 _d7 = 255;
668 uint8_t _rows = 2;
669 uint8_t _columns = 40;
670 uint8_t _offsets[8] = { 0,64,20,84,0,64,20,84 };
671
672};
@ PIN_MODE_LIQUIDCRYSTAL
(28)
Class for a Serial Wombat chip. Each Serial Wombat chip on a project should have its own instance.
int16_t setRowOffsets(uint8_t row1, uint8_t row2, uint8_t row3, uint8_t row4)
Set the offsets in the LCD on-board memory for the beginning of each row.
int16_t noCursor()
Makes the cursor invisible.
int16_t scrollDisplayLeft()
Display moves left when scrolling.
int16_t blink()
Makes the cursor blink.
int16_t display()
Enables LCD display.
int16_t begin(uint8_t cols=80, uint8_t rows=1)
Initialization routine for SerialWombatLiquidCrystal.
int16_t leftToRight()
LCD Display entry occurs left to right.
int16_t beginE2(uint8_t e2Pin)
Add a second e pin for use wtih 40x4 LCD displays.
int16_t setCursor(uint8_t col, uint8_t row)
Set the cursor / next entry position.
int16_t noAutoscroll()
Turn off autoscrolling mode on the LCD display (not in the Serial Wombat chip)
int16_t noDisplay()
Disables LCD display.
size_t write(uint8_t *buffer, size_t count)
Write an array of data directly to the LCD.
int16_t home(bool delayAfterHome=true)
Set the LCD cursor / entry position to the 0,0 position.
int16_t noBlink()
Makes the cursor not blink.
int16_t rightToLeft()
LCD Display entry occurs right to left.
int16_t autoscroll()
Turn on autoscrolling mode on the LCD display (not in the Serial Wombat chip)
int16_t cursor()
Makes the cursor visible.
int16_t clear(bool delayAfterClear=true)
Clear the LCD display.
SerialWombatLiquidCrystal(SerialWombatChip &serialWombat, uint8_t rs, uint8_t enable, uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)
Class constructor for SerialWombatLiquidCrystal.
int16_t scrollDisplayRight()
Display moves right when scrolling.
int16_t createChar(uint8_t index, uint8_t bitmap[])
Write bitmap data to the display for custom characters.
uint8_t swPinModeNumber()
returns the pin mode number. Override for use with classes that require this as a virtual function
uint8_t pin()
returns the e pin number. Override for use with classes that require this as a virtual function
size_t write(uint8_t data)
Write a byte of data directly to the LCD.
int16_t initializeBufferCopy(uint16_t bufferIndex, uint8_t width=255)
kicks off the Serial Wombat chip state machine that copies one character to the LCD each second from ...
int16_t command(uint8_t cmd)
send a command directly to the LCD
SerialWombatChip & _sw
SerialWombatPin(SerialWombatChip &serialWombatChip)
Instantiates a Serial Wombat Pin.