Serial Wombat Arduino Library
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 
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  {
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  uint32_t startTime = millis();
446  while (sent < count - 5)
447  {
448  uint8_t tx[8] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_MODE5,
449  _pin,
450  (uint8_t)PIN_MODE_LIQUIDCRYSTAL,
451  buffer[sent],
452  buffer[sent + 1],
453  buffer[sent + 2],
454  buffer[sent + 3],
455  buffer[sent + 4],
456  };
457  if (_sw.sendPacket(tx) < 0)
458  {
459  return sent;
460  }
461  sent += 5;
462  }
463  while (sent < count - 1)
464  {
465  uint8_t tx[8] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_MODE4,
466  _pin,
467  (uint8_t)PIN_MODE_LIQUIDCRYSTAL,
468  2, // Data
469  buffer[sent],
470  2, // Data
471  buffer[sent + 1],
472  0x55
473  };
474  if (_sw.sendPacket(tx) < 0)
475  {
476  return sent;
477  }
478  sent += 2;
479  }
480  if (sent < count)
481  {
482  uint8_t tx[8] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_MODE4,
483  _pin,
484  (uint8_t)PIN_MODE_LIQUIDCRYSTAL,
485  2, // Data
486  buffer[sent],
487  0x55,
488  0x55,
489  0x55
490  };
491  if (_sw.sendPacket(tx) >= 0)
492  {
493  ++sent;
494  }
495  }
496  uint32_t endTime = millis();
497  return sent;
498  }
499 
508  int16_t createChar(uint8_t index, uint8_t bitmap[])
509  {
510  if (index >= 8)
511  {
512  return (-1);
513  }
514  int16_t result = command((uint8_t)(0x40 | (index << 3))); // Set character ram location
515  if (result < 0)
516  {
517  return result;
518  }
519  for (int i = 0; i < 8; ++i)
520  {
521  result =(int16_t) write(bitmap[i]);
522  if (result < 0)
523  {
524  return result;
525  }
526  }
527  return 0;
528  }
529 
545  int16_t setRowOffsets(uint8_t row1, uint8_t row2, uint8_t row3, uint8_t row4)
546  {
547  _offsets[0] = row1;
548  _offsets[1] = row2;
549  _offsets[2] = row3;
550  _offsets[3] = row4;
551  uint8_t tx[8] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_MODE8,
552  _pin,
553  _pinMode,
554  0,row1,0x55,0x55
555  };
556  int16_t result = _sw.sendPacket(tx); if (result < 0) { return result; };
557  tx[3] = 1; tx[4] = row2;
558  result = _sw.sendPacket(tx); if (result < 0) { return result; };
559  tx[3] = 2; tx[4] = row3;
560  result = _sw.sendPacket(tx); if (result < 0) { return result; };
561  tx[3] = 3; tx[4] = row4;
562  result = _sw.sendPacket(tx); if (result < 0) { return result; };
563  return(0);
564  }
565 
573  int16_t command(uint8_t cmd)
574  {
575  uint8_t tx[8] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_MODE4,
576  _pin,
577  (uint8_t)PIN_MODE_LIQUIDCRYSTAL,
578  1, // Command
579  cmd,
580  0x55,
581  0x55,
582  0x55
583  };
584  return _sw.sendPacket(tx);
585  }
586 
592  uint8_t pin() { return _pin; }
593 
599  uint8_t swPinModeNumber() { return _pinMode; }
600 
614  int16_t initializeBufferCopy(uint16_t bufferIndex, uint8_t width = 255)
615  {
616  if (width == 255)
617  {
618  width = _columns;
619  }
620  uint8_t tx[8] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_MODE6,
621  _pin,
622  (uint8_t)PIN_MODE_LIQUIDCRYSTAL,
623  (byte)(bufferIndex & 0xFF),
624  (byte)(bufferIndex >>8),
625  width,
626  0x55,
627  0x55,
628 
629  };
630  return _sw.sendPacket(tx);
631  }
632 //TODO int16_t dataMonitor(uint8_t pin, uint8_t col, uint8_t row, uint8_t index, bool hex=false);
633 
646  int16_t beginE2(uint8_t e2Pin)
647  {
648  uint8_t tx[8] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_MODE7,
649  _pin,
650  (uint8_t)PIN_MODE_LIQUIDCRYSTAL,
651  e2Pin,
652  0x55,
653  0x55,
654  0x55,
655  0x55,
656 
657  };
658  return _sw.sendPacket(tx);
659  }
660 
661  using Print::write; //Inherit the write commands from print.
662 
663 
664 private:
665  uint8_t _rs = 255,
666  _d4 = 255,
667  _d5 = 255,
668  _d6 = 255,
669  _d7 = 255;
670  uint8_t _rows = 2;
671  uint8_t _columns = 40;
672  uint8_t _offsets[8] = { 0,64,20,84,0,64,20,84 };
673 
674 };
SerialWombatLiquidCrystal::scrollDisplayLeft
int16_t scrollDisplayLeft()
Display moves left when scrolling.
Definition: SerialWombatLiquidCrystal.h:307
SerialWombatLiquidCrystal::leftToRight
int16_t leftToRight()
LCD Display entry occurs left to right.
Definition: SerialWombatLiquidCrystal.h:370
SerialWombatChip
Class for a Serial Wombat chip. Each Serial Wombat chip on a project should have its own instance.
Definition: SerialWombat.h:286
SerialWombatLiquidCrystal::command
int16_t command(uint8_t cmd)
send a command directly to the LCD
Definition: SerialWombatLiquidCrystal.h:573
SerialWombatCommands::CONFIGURE_PIN_MODE0
@ CONFIGURE_PIN_MODE0
(200)
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:614
SerialWombatLiquidCrystal::autoscroll
int16_t autoscroll()
Turn on autoscrolling mode on the LCD display (not in the Serial Wombat chip)
Definition: SerialWombatLiquidCrystal.h:330
SerialWombatPin::_sw
SerialWombatChip & _sw
Definition: SerialWombatPin.h:134
SerialWombatCommands::CONFIGURE_PIN_MODE7
@ CONFIGURE_PIN_MODE7
(207)
SerialWombatLiquidCrystal::createChar
int16_t createChar(uint8_t index, uint8_t bitmap[])
Write bitmap data to the display for custom characters.
Definition: SerialWombatLiquidCrystal.h:508
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:599
SerialWombatCommands::CONFIGURE_PIN_MODE3
@ CONFIGURE_PIN_MODE3
(203)
SerialWombatLiquidCrystal::home
int16_t home(bool delayAfterHome=true)
Set the LCD cursor / entry position to the 0,0 position.
Definition: SerialWombatLiquidCrystal.h:156
SerialWombatLiquidCrystal::clear
int16_t clear(bool delayAfterClear=true)
Clear the LCD display.
Definition: SerialWombatLiquidCrystal.h:136
SerialWombatCommands::CONFIGURE_PIN_MODE8
@ CONFIGURE_PIN_MODE8
(208)
SerialWombatCommands::CONFIGURE_PIN_MODE4
@ CONFIGURE_PIN_MODE4
(204)
SerialWombat.h
SerialWombatLiquidCrystal::setCursor
int16_t setCursor(uint8_t col, uint8_t row)
Set the cursor / next entry position.
Definition: SerialWombatLiquidCrystal.h:173
SerialWombatLiquidCrystal::begin
int16_t begin(uint8_t cols=80, uint8_t rows=1)
Initialization routine for SerialWombatLiquidCrystal.
Definition: SerialWombatLiquidCrystal.h:101
SerialWombatLiquidCrystal::noCursor
int16_t noCursor()
Makes the cursor invisible.
Definition: SerialWombatLiquidCrystal.h:192
SerialWombatLiquidCrystal::cursor
int16_t cursor()
Makes the cursor visible.
Definition: SerialWombatLiquidCrystal.h:210
SerialWombatLiquidCrystal::beginE2
int16_t beginE2(uint8_t e2Pin)
Add a second e pin for use wtih 40x4 LCD displays.
Definition: SerialWombatLiquidCrystal.h:646
SerialWombatLiquidCrystal::blink
int16_t blink()
Makes the cursor blink.
Definition: SerialWombatLiquidCrystal.h:228
SerialWombatPin
Describes a Serial Wombat Pin. Is base class for other pin modes.
Definition: SerialWombatPin.h:38
SerialWombatLiquidCrystal::noDisplay
int16_t noDisplay()
Disables LCD display.
Definition: SerialWombatLiquidCrystal.h:286
SerialWombatLiquidCrystal::noBlink
int16_t noBlink()
Makes the cursor not blink.
Definition: SerialWombatLiquidCrystal.h:247
SerialWombatPin::_pinMode
uint8_t _pinMode
Definition: SerialWombatPin.h:135
SerialWombatChip::sendPacket
int sendPacket(uint8_t tx[], uint8_t rx[])
Send an 8 byte packet to the Serial Wombat chip and wait for 8 bytes back.
Definition: SerialWombat.cpp:115
SerialWombatLiquidCrystal::scrollDisplayRight
int16_t scrollDisplayRight()
Display moves right when scrolling.
Definition: SerialWombatLiquidCrystal.h:320
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:592
SerialWombatLiquidCrystal::write
size_t write(uint8_t *buffer, size_t count)
Write an array of data directly to the LCD.
Definition: SerialWombatLiquidCrystal.h:442
SerialWombatPin::_pin
uint8_t _pin
Definition: SerialWombatPin.h:133
SerialWombatLiquidCrystal::display
int16_t display()
Enables LCD display.
Definition: SerialWombatLiquidCrystal.h:267
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:545
SerialWombatLiquidCrystal::write
size_t write(uint8_t data)
Write a byte of data directly to the LCD.
Definition: SerialWombatLiquidCrystal.h:413
PIN_MODE_LIQUIDCRYSTAL
@ PIN_MODE_LIQUIDCRYSTAL
(28)
Definition: SerialWombat.h:267
SerialWombatLiquidCrystal::noAutoscroll
int16_t noAutoscroll()
Turn off autoscrolling mode on the LCD display (not in the Serial Wombat chip)
Definition: SerialWombatLiquidCrystal.h:349
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
SerialWombatCommands::CONFIGURE_PIN_MODE5
@ CONFIGURE_PIN_MODE5
(205)
SerialWombatLiquidCrystal::rightToLeft
int16_t rightToLeft()
LCD Display entry occurs right to left.
Definition: SerialWombatLiquidCrystal.h:390
SerialWombatCommands::CONFIGURE_PIN_MODE6
@ CONFIGURE_PIN_MODE6
(206)
SerialWombatLiquidCrystal
A Class which connects to HD44780 / 1602 / 4002 / 2004 or similar interface Character LCDs.
Definition: SerialWombatLiquidCrystal.h:68