Serial Wombat Arduino Library

Loading...
Searching...
No Matches
SerialWombatLiquidCrystal.h
Go to the documentation of this file.
1#pragma once
2/*
3 Copyright 2022-2023 Broadwell Consulting Inc.
4
5"Serial Wombat" is a registered trademark of Broadwell Consulting Inc. in
6 the United States. See SerialWombat.com for usage guidance.
7
8 Permission 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
15 The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17
18 THE 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
68 class
69 SerialWombatLiquidCrystal : public Print, public SerialWombatPin
70{
71 public:
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 {
84 _pinMode = PIN_MODE_LIQUIDCRYSTAL;
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
307 int16_t scrollDisplayLeft()
308 {
309 return command(0x18); // Display Move left LCD
310
311 }
312
320 int16_t scrollDisplayRight()
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
662 private:
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};
SerialWombat.h
SerialWombatCommands::CONFIGURE_PIN_MODE0
@ CONFIGURE_PIN_MODE0
(200)
Definition SerialWombat.h:237
SerialWombatCommands::CONFIGURE_PIN_MODE3
@ CONFIGURE_PIN_MODE3
(203)
Definition SerialWombat.h:240
SerialWombatCommands::CONFIGURE_PIN_MODE4
@ CONFIGURE_PIN_MODE4
(204)
Definition SerialWombat.h:241
SerialWombatCommands::CONFIGURE_PIN_MODE5
@ CONFIGURE_PIN_MODE5
(205)
Definition SerialWombat.h:242
SerialWombatCommands::CONFIGURE_PIN_MODE8
@ CONFIGURE_PIN_MODE8
(208)
Definition SerialWombat.h:245
SerialWombatCommands::CONFIGURE_PIN_MODE6
@ CONFIGURE_PIN_MODE6
(206)
Definition SerialWombat.h:243
SerialWombatCommands::CONFIGURE_PIN_MODE7
@ CONFIGURE_PIN_MODE7
(207)
Definition SerialWombat.h:244
PIN_MODE_LIQUIDCRYSTAL
@ PIN_MODE_LIQUIDCRYSTAL
(28)
Definition SerialWombat.h:286
SerialWombatChip
Class for a Serial Wombat chip. Each Serial Wombat chip on a project should have its own instance.
Definition SerialWombat.h:313
SerialWombatLiquidCrystal::setRowOffsets
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.
Definition SerialWombatLiquidCrystal.h:543
SerialWombatLiquidCrystal::noCursor
int16_t noCursor()
Makes the cursor invisible.
Definition SerialWombatLiquidCrystal.h:192
SerialWombatLiquidCrystal::scrollDisplayLeft
int16_t scrollDisplayLeft()
Display moves left when scrolling.
Definition SerialWombatLiquidCrystal.h:307
SerialWombatLiquidCrystal::blink
int16_t blink()
Makes the cursor blink.
Definition SerialWombatLiquidCrystal.h:228
SerialWombatLiquidCrystal::display
int16_t display()
Enables LCD display.
Definition SerialWombatLiquidCrystal.h:267
SerialWombatLiquidCrystal::begin
int16_t begin(uint8_t cols=80, uint8_t rows=1)
Initialization routine for SerialWombatLiquidCrystal.
Definition SerialWombatLiquidCrystal.h:101
SerialWombatLiquidCrystal::leftToRight
int16_t leftToRight()
LCD Display entry occurs left to right.
Definition SerialWombatLiquidCrystal.h:370
SerialWombatLiquidCrystal::beginE2
int16_t beginE2(uint8_t e2Pin)
Add a second e pin for use wtih 40x4 LCD displays.
Definition SerialWombatLiquidCrystal.h:644
SerialWombatLiquidCrystal::setCursor
int16_t setCursor(uint8_t col, uint8_t row)
Set the cursor / next entry position.
Definition SerialWombatLiquidCrystal.h:173
SerialWombatLiquidCrystal::noAutoscroll
int16_t noAutoscroll()
Turn off autoscrolling mode on the LCD display (not in the Serial Wombat chip)
Definition SerialWombatLiquidCrystal.h:349
SerialWombatLiquidCrystal::noDisplay
int16_t noDisplay()
Disables LCD display.
Definition SerialWombatLiquidCrystal.h:286
SerialWombatLiquidCrystal::write
size_t write(uint8_t *buffer, size_t count)
Write an array of data directly to the LCD.
Definition SerialWombatLiquidCrystal.h:442
SerialWombatLiquidCrystal::home
int16_t home(bool delayAfterHome=true)
Set the LCD cursor / entry position to the 0,0 position.
Definition SerialWombatLiquidCrystal.h:156
SerialWombatLiquidCrystal::noBlink
int16_t noBlink()
Makes the cursor not blink.
Definition SerialWombatLiquidCrystal.h:247
SerialWombatLiquidCrystal::rightToLeft
int16_t rightToLeft()
LCD Display entry occurs right to left.
Definition SerialWombatLiquidCrystal.h:390
SerialWombatLiquidCrystal::autoscroll
int16_t autoscroll()
Turn on autoscrolling mode on the LCD display (not in the Serial Wombat chip)
Definition SerialWombatLiquidCrystal.h:330
SerialWombatLiquidCrystal::cursor
int16_t cursor()
Makes the cursor visible.
Definition SerialWombatLiquidCrystal.h:210
SerialWombatLiquidCrystal::clear
int16_t clear(bool delayAfterClear=true)
Clear the LCD display.
Definition SerialWombatLiquidCrystal.h:136
SerialWombatLiquidCrystal::SerialWombatLiquidCrystal
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.
Definition SerialWombatLiquidCrystal.h:82
SerialWombatLiquidCrystal::scrollDisplayRight
int16_t scrollDisplayRight()
Display moves right when scrolling.
Definition SerialWombatLiquidCrystal.h:320
SerialWombatLiquidCrystal::createChar
int16_t createChar(uint8_t index, uint8_t bitmap[])
Write bitmap data to the display for custom characters.
Definition SerialWombatLiquidCrystal.h:506
SerialWombatLiquidCrystal::swPinModeNumber
uint8_t swPinModeNumber()
returns the pin mode number. Override for use with classes that require this as a virtual function
Definition SerialWombatLiquidCrystal.h:597
SerialWombatLiquidCrystal::pin
uint8_t pin()
returns the e pin number. Override for use with classes that require this as a virtual function
Definition SerialWombatLiquidCrystal.h:590
SerialWombatLiquidCrystal::write
size_t write(uint8_t data)
Write a byte of data directly to the LCD.
Definition SerialWombatLiquidCrystal.h:413
SerialWombatLiquidCrystal::initializeBufferCopy
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 ...
Definition SerialWombatLiquidCrystal.h:612
SerialWombatLiquidCrystal::command
int16_t command(uint8_t cmd)
send a command directly to the LCD
Definition SerialWombatLiquidCrystal.h:571
SerialWombatPin::_sw
SerialWombatChip & _sw
Definition SerialWombatPin.h:214
SerialWombatPin::SerialWombatPin
SerialWombatPin(SerialWombatChip &serialWombatChip)
Instantiates a Serial Wombat Pin.
Definition SerialWombatPin.h:45
SerialWombatPin::_pin
uint8_t _pin
Definition SerialWombatPin.h:213
SerialWombatPin::_pinMode
uint8_t _pinMode
Definition SerialWombatPin.h:215