Fast LCD I2C driver 1.0
Fast driver for LCDs on I2C for Pi-Pico and Arduino
LCD_I2C.hpp
Go to the documentation of this file.
1
13#pragma once
14//#define ARDUINO
15
16// If we are compiling for Arduino, we need some extra files
17
18#ifdef ARDUINO
19#include <Arduino.h>
20#include <inttypes.h>
21#include <Print.h>
22#include <Wire.h>
23// and some function alias'
24inline void sleep_ms(uint32_t time) {delay(time);}
25inline void sleep_us(uint64_t time) {delayMicroseconds(time);}
26#else
27#include <stdint.h>
28#endif
29
30// For Arduino, we are part of the print class
31// For Pi Pico, we are stand alone
42#ifdef ARDUINO
43class LCD_I2C : public Print {
44#else
45class LCD_I2C {
46#endif
47 private:
48
49 using byte = uint8_t;
50
51 // commands
52 static constexpr byte LCD_CLEARDISPLAY = 0x01;
53 static constexpr byte LCD_RETURNHOME = 0x02;
54 static constexpr byte LCD_ENTRYMODESET = 0x04;
55 static constexpr byte LCD_DISPLAYCONTROL = 0x08;
56 static constexpr byte LCD_DISPLAYSHIFT = 0x10;
57 static constexpr byte LCD_FUNCTIONSET = 0x20;
58 static constexpr byte LCD_SETCGRAMADDR = 0x40;
59 static constexpr byte LCD_SETDDRAMADDR = 0x80;
60
61 // flags for display entry mode
62 // use with LCD_ENTRYMODESET
63 static constexpr byte LCD_ENTRYRIGHT = 0x00;
64 static constexpr byte LCD_ENTRYLEFT = 0x02;
65 static constexpr byte LCD_DISPLAYENTRYSHIFT = 0x01;
66 static constexpr byte LCD_NODISPLAYENTRYSHIFT = 0x00;
67
68 // flags for display and cursor control
69 // use with LCD_DISPLAYCONTROL
70 static constexpr byte LCD_DISPLAYON = 0x04;
71 static constexpr byte LCD_DISPLAYOFF = 0x00;
72 static constexpr byte LCD_CURSORON = 0x02;
73 static constexpr byte LCD_CURSOROFF = 0x00;
74 static constexpr byte LCD_BLINKON = 0x01;
75 static constexpr byte LCD_BLINKOFF = 0x00;
76
77 // flags for display and cursor shift
78 // use with LCD_DISPLAYSHIFT
79 static constexpr byte LCD_DISPLAYMOVE = 0x08;
80 static constexpr byte LCD_CURSORMOVE = 0x00;
81 static constexpr byte LCD_MOVERIGHT = 0x04;
82 static constexpr byte LCD_MOVELEFT = 0x00;
83
84 // flags for function set
85 static constexpr byte LCD_8BITMODE = 0x10;
86 static constexpr byte LCD_4BITMODE = 0x00;
87 static constexpr byte LCD_2LINE = 0x08;
88 static constexpr byte LCD_1LINE = 0x00;
89
90 // Control bits in the I2C interface data words
91 static constexpr byte LCD_BACKLIGHT = 0x08;
92 static constexpr byte LCD_NOBACKLIGHT = 0x00;
93
94 static constexpr byte En = 0x4; // Enable bit
95 static constexpr byte Rw = 0x2; // Read/Write bit
96 static constexpr byte Rs = 0x1; // Register select bit
97
98 static constexpr byte ENABLE = En;
99
100 // By default these LCD display drivers are on bus address 0x27
101
102 // Modes for lcd_send_byte
103 static constexpr byte LCD_CHARACTER = 1;
104 static constexpr byte LCD_COMMAND = 0;
105
106 static constexpr byte MAX_LINES = 4;
107 static constexpr byte MAX_CHARS = 20;
108
109 byte _Addr;
110 byte _displayfunction;
111 byte _displaycontrol;
112 byte _displaymode;
113 byte _cols;
114 byte _rows;
115 byte _charsize = 0; // used as boolean flag for 10 pixel high characters
116 byte _backlight;
117 byte _last_mode;
118
119 uint8_t row_address_offset[MAX_LINES] = {0x80, 0xC0, 0x80 + 20, 0xC0 + 20};
120
121 /*
122 * For Arduino, the I2C interface (Wire) has an internal buffer
123 * The buffer length is defined as BUFFER_LENGTH.
124 * Care MUST be taken, since ONLY BUFFER_LENGTH characters can be sent
125 * in a singe transmission, and excess characters will be discarded.
126 * Note that BUFFER_LENGTH varies and can be quite short. UNO is about 30
127 * characters, while the Pi Pico implementation is over 120!
128 *
129 * In the Pi Pico SDK, the I2C interface does not buffer, so we can set the
130 * size to suit ourselves.
131 *
132 * Note that we will ALWAYS buffer internally and transmit in a block, since
133 * testing with Arduino showed that it was faster than sending single bytes
134 * to the Arduino Wire routines.
135 * */
136 #ifdef ARDUINO
137 // The default Arduino length definition is BUFFER_LENGTH
138 // The Pi Pico Arduino version of wire.h defines a DIFFERENT variable name
139 // than some of the other Arduino environments. So we will check!
140 // If we can't find a length we will assume 30 bytes...
141 #ifndef BUFFER_LENGTH
142 #ifdef WIRE_BUFFER_SIZE
143 static constexpr size_t BUFFER_LENGTH = WIRE_BUFFER_SIZE;
144 #else
145 static constexpr size_t BUFFER_LENGTH = 30;
146 #endif
147 #endif
148 #else
149 static constexpr size_t BUFFER_LENGTH = 128;
150 #endif
151
152 byte _buffer[BUFFER_LENGTH];
153
154
155 /*
156 * This is the buffer pointer (and character counter).
157 *
158 */
159 size_t _bufferIn = 0;
160
161 /*
162 * The Pico system needs to know which I2C hardware to use, so we need
163 * to save it! Note that on Arduino systems with more than one I2C bus,
164 * only the first one pointed to by wire can be used!
165 */
166 #ifndef ARDUINO
167 i2c_inst *I2C_instance {nullptr};
168 #endif
169
178 void write_byte(byte val, bool Enable_Buffering = false) noexcept;
179
189 void send_byte(byte val, int mode, bool Enable_Buffering = false) noexcept;
190
197 void init() noexcept;
198
199
200 public:
201
202 #ifdef ARDUINO
203 // DO NOT Document for Arduino
205 #endif
206
211 static constexpr uint8_t CUSTOM_SYMBOL_SIZE = 8;
212
214 static constexpr byte LCD_5x10DOTS = 0x04;
216 static constexpr byte LCD_5x8DOTS = 0x00;
217#ifdef ARDUINO
218// end conditional documentation and begin Arduino_diff group
220
226#endif
227
228
229
230 #ifdef ARDUINO
236
257 LCD_I2C(uint8_t lcd_addr, uint8_t lcd_cols, uint8_t lcd_rows, uint8_t charsize = LCD_5x8DOTS) noexcept;
258
268 inline void begin() noexcept {
269 Wire.begin();
270 init();
271 } ;
273
275
276 #else
277
281
308 LCD_I2C(byte address, byte columns, byte rows, i2c_inst *I2C = PICO_DEFAULT_I2C_INSTANCE) noexcept;
310
311 #endif
312
313
322
330 void writeChar(byte val, bool Enable_Buffering = false) noexcept;
331
342 void writeString(const char str[], bool Enable_Buffering = false) noexcept;
343
349 inline void printstr(const char str[]) noexcept
350 {writeString(str ); } ;
352
363
370 inline size_t write(uint8_t c) noexcept
371 { writeChar(c); return 1; } ;
372
373
387 size_t write(const uint8_t *buffer, size_t size, bool Enable_Buffering = false) noexcept;
388
395 inline size_t write(const char *str) noexcept
396 {
397 if (str == NULL) return 0;
398 return write((const uint8_t *)str, strlen(str));
399 } ;
400
412 inline size_t write(const char *buffer, size_t size, bool Enable_Buffering = false) noexcept
413 {
414 return write((const uint8_t *)buffer, size, Enable_Buffering);
415 } ;
417
422
430 #ifndef ARDUINO
441 void setCursor(byte line, byte position, bool Enable_Buffering= false) noexcept;
442 #else
444
448
469 void setCursor(byte position, byte line, bool Enable_Buffering= false) noexcept;
471 #endif
472
480 void clear(void) noexcept;
481
489 void home(void) noexcept;
490
491
497 void backlight(void) noexcept;
498
504 void noBacklight(void) noexcept;
505
513 inline void setBacklight(uint8_t newVal) noexcept
514 {
515 newVal?backlight():noBacklight();
516 };
517
524 void display(void) noexcept;
525
534 void noDisplay(void) noexcept;
535
537
544
549 void cursor(void) noexcept;
554 inline void cursor_on() noexcept
555 { cursor(); }
556
562 void noCursor(void) noexcept;
567 inline void cursor_off() noexcept
568 { noCursor(); }
569
575 void blink(void) noexcept;
576
581 inline void blink_on() noexcept
582 { blink(); }
583
589 void noBlink(void) noexcept;
590
595 inline void blink_off() noexcept
596 { noBlink(); }
598
603
611 void scrollDisplayLeft(void) noexcept;
612
619 void scrollDisplayRight(void) noexcept;
620
626 void autoscroll(void) noexcept;
627
633 void noAutoscroll(void) noexcept;
634
642 void leftToRight(void) noexcept;
643
650 void rightToLeft(void) noexcept;
652
666
681 void createChar(byte charnum, const byte char_map[]) noexcept;
688 inline void load_custom_character(uint8_t char_num, uint8_t *rows) noexcept
689 { // alias for createChar()
690 createChar(char_num, rows);
691 };
693
699
713 int show(void) noexcept;
715 #ifdef ARDUINO
717 #endif
718};
719
720#ifndef ARDUINO
721#ifndef I2C_Setup_defined
723 #define I2C_Setup_defined
725
748 extern "C" int LCD_I2C_Setup(i2c_inst_t* I2C, uint SDA_Pin, uint SCL_Pin, uint I2C_Clock) noexcept;
749;
750#endif
751#endif
752
753
#define MAX_LINES
Definition: LCD_I2C-C.cpp:31
A class for efficiently driving an LDC display connected to the I2C bus on Arduino or Pi Pico.
Definition: LCD_I2C.hpp:45
void blink(void) noexcept
Display the blinking inverted cursor at the current cursor location.
Definition: LCD_I2C.cpp:243
void scrollDisplayRight(void) noexcept
Scrolls the display and cursor one position to the right. home() will restore it to its original posi...
Definition: LCD_I2C.cpp:272
void noAutoscroll(void) noexcept
Turns off automatic scrolling of the display.
Definition: LCD_I2C.cpp:283
void createChar(byte charnum, const byte char_map[]) noexcept
Create a custom character.
Definition: LCD_I2C.cpp:301
void cursor(void) noexcept
Display the underline cursor at the current cursor location.
Definition: LCD_I2C.cpp:231
void rightToLeft(void) noexcept
Sets the text direction to right to left. Each character output advances the cursor one position to t...
Definition: LCD_I2C.cpp:289
void autoscroll(void) noexcept
Turns on automatic scrolling of the display.
Definition: LCD_I2C.cpp:277
void writeString(const char str[], bool Enable_Buffering=false) noexcept
Output a string to the screen. Naming is more in the style of the Pico SDK, and the Enable_Buffering ...
Definition: LCD_I2C.cpp:132
void backlight(void) noexcept
Turn on the display backlight.
Definition: LCD_I2C.cpp:219
int show(void) noexcept
Empties the buffer to "show" any data not yet output to the screen.
Definition: LCD_I2C.cpp:198
static constexpr byte LCD_5x8DOTS
needed for Arduino Constructor
Definition: LCD_I2C.hpp:216
void scrollDisplayLeft(void) noexcept
Scrolls the display and cursor one position to the left. home() will restore it to its original posit...
Definition: LCD_I2C.cpp:267
void noBacklight(void) noexcept
Turn off the display backlight
Definition: LCD_I2C.cpp:225
void noDisplay(void) noexcept
Turns the display off without modifying data on it.
Definition: LCD_I2C.cpp:261
void printstr(const char str[]) noexcept
An alias for writeString (for compatibility with earlier programs)
Definition: LCD_I2C.hpp:349
size_t write(uint8_t c) noexcept
Override the write byte method of the Arduino print class.
Definition: LCD_I2C.hpp:370
void noBlink(void) noexcept
Disable the blinking inverted cursor at the current cursor location.
Definition: LCD_I2C.cpp:249
void setBacklight(uint8_t newVal) noexcept
Set the backlight to a given value.
Definition: LCD_I2C.hpp:513
void writeChar(byte val, bool Enable_Buffering=false) noexcept
Output a character to the screen.
Definition: LCD_I2C.cpp:144
void cursor_on() noexcept
Alias for cursor() to turn the underline cursor on.
Definition: LCD_I2C.hpp:554
void blink_on() noexcept
Alias for blink() to display the blinking inverted cursor.
Definition: LCD_I2C.hpp:581
void noCursor(void) noexcept
Hides the underline cursor at the current cursor location.
Definition: LCD_I2C.cpp:237
size_t write(const char *buffer, size_t size, bool Enable_Buffering=false) noexcept
Override the write buffer of type char method from Arduino print.
Definition: LCD_I2C.hpp:412
void cursor_off() noexcept
An alias for noCursor to turn off the underline cursor.
Definition: LCD_I2C.hpp:567
void display(void) noexcept
Turns the display on without modifying data on it.
Definition: LCD_I2C.cpp:255
void leftToRight(void) noexcept
Sets the text direction to left to right. Each character output advances the cursor one position to t...
Definition: LCD_I2C.cpp:295
static constexpr uint8_t CUSTOM_SYMBOL_SIZE
A reminder of the size of custom character arrays.
Definition: LCD_I2C.hpp:211
void home(void) noexcept
"Home" the display.
Definition: LCD_I2C.cpp:111
void clear(void) noexcept
Clear the display.
Definition: LCD_I2C.cpp:105
void setCursor(byte line, byte position, bool Enable_Buffering=false) noexcept
Move the input cursor to a location on the screen.
Definition: LCD_I2C.cpp:122
static constexpr byte LCD_5x10DOTS
needed for Arduino Constructor
Definition: LCD_I2C.hpp:214
LCD_I2C(byte address, byte columns, byte rows, i2c_inst *I2C=PICO_DEFAULT_I2C_INSTANCE) noexcept
The Pi Pico Constructor.
Definition: LCD_I2C.cpp:44
void blink_off() noexcept
Alias for noBlink() to turn off the blinking inverted cursor.
Definition: LCD_I2C.hpp:595
void load_custom_character(uint8_t char_num, uint8_t *rows) noexcept
An alias for createChar() for loading custom character data.
Definition: LCD_I2C.hpp:688
int LCD_I2C_Setup(i2c_inst_t *I2C, uint SDA_Pin, uint SCL_Pin, uint I2C_Clock) noexcept
Helper function to set up the I2C bus on Pi Pico.
Definition: LCD_I2C.cpp:314