-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
LCD_I2C.hpp
286 lines (233 loc) · 7.61 KB
/
LCD_I2C.hpp
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
/*******************************************************************************
* @file LCD_I2C.hpp
* @author Cristian Cristea
* @date September 27, 2021
* @brief Header file for the LCD_I2C class.
*
* @copyright Copyright (C) 2021 Cristian Cristea
******************************************************************************/
#pragma once
#include <hardware/gpio.h>
#include <hardware/i2c.h>
#include <string_view>
#include <cstdint>
#include <array>
class LCD_I2C final
{
private:
using byte = uint8_t;
// Commands
static constexpr byte CLEAR_DISPLAY = 0x01;
static constexpr byte RETURN_HOME = 0x02;
static constexpr byte ENTRY_MODE_SET = 0x04;
static constexpr byte DISPLAY_CONTROL = 0x08;
static constexpr byte CURSOR_SHIFT = 0x10;
static constexpr byte FUNCTION_SET = 0x20;
static constexpr byte SET_CGRAM_ADDR = 0x40;
static constexpr byte SET_DDRAM_ADDR = 0x80;
// Flags for display entry mode set
static constexpr byte ENTRY_RIGHT = 0x00;
static constexpr byte ENTRY_LEFT = 0x02;
static constexpr byte ENTRY_SHIFT_INCREMENT = 0x01;
static constexpr byte ENTRY_SHIFT_DECREMENT = 0x00;
// Flags for display on/off control
static constexpr byte DISPLAY_ON = 0x04;
static constexpr byte DISPLAY_OFF = 0x00;
static constexpr byte CURSOR_ON = 0x02;
static constexpr byte CURSOR_OFF = 0x00;
static constexpr byte BLINK_ON = 0x01;
static constexpr byte BLINK_OFF = 0x00;
// Flags for cursor or display shift
static constexpr byte DISPLAY_MOVE = 0x08;
static constexpr byte CURSOR_MOVE = 0x00;
static constexpr byte MOVE_RIGHT = 0x04;
static constexpr byte MOVE_LEFT = 0x00;
// Flags for function set
static constexpr byte MODE_8_BIT = 0x10;
static constexpr byte MODE_4_BIT = 0x00;
static constexpr byte LINE_2 = 0x08;
static constexpr byte LINE_1 = 0x00;
static constexpr byte DOTS_5x10 = 0x04;
static constexpr byte DOTS_5x8 = 0x00;
// Flags for backlight control
static constexpr byte BACKLIGHT = 0x08;
static constexpr byte NO_BACKLIGHT = 0x00;
// Special flags
static constexpr byte ENABLE = 0x04;
static constexpr byte READ_WRITE = 0x02;
static constexpr byte REGISTER_SELECT = 0x01;
static constexpr byte COMMAND = 0x00;
static constexpr byte CHAR = 0x01;
public:
static constexpr byte CUSTOM_SYMBOL_SIZE = 8;
using array = std::array<byte, CUSTOM_SYMBOL_SIZE>;
private:
byte address {};
byte columns {};
byte rows {};
byte backlight {};
byte display_function {};
byte display_control {};
byte display_mode {};
i2c_inst * I2C_instance {nullptr};
/**
* Wrapper function for SDK's internal I2C protocol write function.
*
* @param val Value to be written
*/
inline void I2C_Write_Byte(byte val) const noexcept;
/**
* Creates a short impulse on the enable pin of the LCD.
*
* @param val Value to be written along with the pulse
*/
void Pulse_Enable(byte val) const noexcept;
/**
* Sends a nibble of data using the I2C protocol.
*
* @param val Value to be sent
*/
inline void Send_Nibble(byte val) const noexcept;
/**
* Send a byte of data as two nibbles using the function
* Send_Nibble(byte val).
*
* @param val Value to be sent
* @param mode The mode used when sending
*/
inline void Send_Byte(byte val, byte mode) const noexcept;
/**
* Sends a command to the LCD display using the function
* Send_Byte(byte val, byte mode).
*
* @param val Value to be sent
*/
inline void Send_Command(byte val) const noexcept;
/**
* Sends a character to the LCD display using the function
* Send_Byte(byte val, byte mode).
*
* @param val Value to be sent
*/
inline void Send_Char(byte val) const noexcept;
/**
* Sends an address or line of a custom character to the LCD display using
* the function Send_Byte(byte val, byte mode).
*
* @param val Value to be sent
*/
inline void Send_Register_Select(byte val) const noexcept;
/**
* Establishes communication with the LCD using the I2C protocol and sets
* its default state: backlight is off, cursor and cursor blinking is off
* and the cursor's initial position is set at the beginning of the screen.
*/
inline void Init() noexcept;
public:
/**
* [Constructor] Initialises the I2C communication protocol using the
* provided instance and pins and calls the display's Init() function.
*
* @param address The I2C address
* @param columns The LCD's number of columns
* @param rows The LCD's number of rows
* @param I2C The I2C instance
* @param SDA The serial data pin
* @param SCL The serial clock pin
*/
LCD_I2C(byte address, byte columns, byte rows, i2c_inst * I2C = PICO_DEFAULT_I2C_INSTANCE,
uint SDA = PICO_DEFAULT_I2C_SDA_PIN, uint SCL = PICO_DEFAULT_I2C_SCL_PIN) noexcept;
/**
* Turns the display on.
*/
void DisplayOn() noexcept;
/**
* Turns the display off.
*/
void DisplayOff() noexcept;
/**
* Turns the backlight on.
*/
void BacklightOn() noexcept;
/**
* Turns the backlight off.
*/
void BacklightOff() noexcept;
/**
* Sets the backlight light on/off according to the parameter.
*
* @param light_on True for light on and False for off
*/
void SetBacklight(bool light_on) noexcept;
/**
* Turns the cursor on.
*/
void CursorOn() noexcept;
/**
* Turns the cursor off.
*/
void CursorOff() noexcept;
/**
* Turns the cursor's blinking on.
*/
void CursorBlinkOn() noexcept;
/**
* Turns the cursor's blinking off.
*/
void CursorBlinkOff() noexcept;
/**
* Sets the text flow from left to right.
*/
void SetTextLeftToRight() noexcept;
/**
* Sets the text flow from right to left.
*/
void SetTextRightToLeft() noexcept;
/**
* Clears the display and sets cursor's position at the beginning of the
* screen.
*/
void Clear() const noexcept;
/**
* Sets cursor's position at the beginning of the screen.
*/
void Home() const noexcept;
/**
* Sets the display's cursor to the position defined by the @p row and
* @p column.
*
* @param row The vertical position (Y axis)
* @param column The horizontal position (X axis)
*/
void SetCursor(byte row, byte column) const noexcept;
/**
* Prints the character on the display at the current cursor position.
*
* @param character The character to be printed
*/
void PrintChar(byte character) const noexcept;
/**
* Prints the string on the display starting at the current cursor position.
*
* @param str The string to be printed
*/
void PrintString(std::string_view str) const noexcept;
/**
* Prints the custom character on the display at the current cursor
* position, by specifying the location in memory.
*
* @param location The memory address
*/
void PrintCustomChar(byte location) const noexcept;
/**
* Creates a custom character by specifying the location in memory to be
* stored (8 locations maximum, starting from 0) and an array of 8 bytes.
* @code
* constexpr std::array BELL = {0x04, 0x0E, 0x0E, 0x1F, 0x00, 0x04, 0x00, 0x00};
* @endcode
*
* @param location The memory address
* @param char_map The byte array
*/
void CreateCustomChar(byte location, array char_map) const noexcept;
};