forked from GreenEllipsis/DFRobot_RGBLCD1602
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DFRobot_RGBLCD1602.cpp
418 lines (371 loc) · 12 KB
/
DFRobot_RGBLCD1602.cpp
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*!
* @file DFRobot_RGBLCD1602.cpp
* @brief DFRobot_RGBLCD1602 class infrastructure, the implementation of basic methods
* @copyright Copyright (c) 2023 Brian Alano (http://www.greenellipsis.org)
* @licence The MIT License (MIT)
* @maintainer [GreenEllipsis]([email protected])
* @note ported from [DFRobot_RGBLCD1602](https://github.com/DFRobot/DFRobot_RGBLCD1602)
* @note maintained by [yangfeng]([email protected])
* @version V0.0.0
* @date 2023-03-02
* @url https://github.com/GreenEllipsis/DFRobot_RGBLCD1602
*/
// Standard C libraries
#include <inttypes.h>
#include <stdio.h>
#include <string.h>
// ESP-IDF libraries
#include "esp_err.h"
#include "esp_check.h"
#include "esp_log.h"
// component libraries
#include "DFRobot_RGBLCD1602.h"
#define LIB_TAG "DFRobot_RGBLCD1602"
const uint8_t color_define[4][3] =
{
{255, 255, 255}, // white
{255, 0, 0}, // red
{0, 255, 0}, // green
{0, 0, 255}, // blue, also monochrome
};
// /*******************************public*******************************/
DFRobot_RGBLCD1602::DFRobot_RGBLCD1602(uint8_t lcdCols, uint8_t lcdRows, i2c_port_t i2c_num, uint8_t lcdAddr, uint8_t RGBAddr)
{
_lcdAddr = lcdAddr;
_RGBAddr = RGBAddr;
_cols = lcdCols;
_rows = lcdRows;
_i2c_num = i2c_num;
}
/**
* @brief scan an I2C address
*
*/
esp_err_t DFRobot_RGBLCD1602::i2c_scan(const uint8_t addr)
{
esp_err_t ret;
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (addr << 1) | I2C_MASTER_WRITE, 1);
i2c_master_stop(cmd);
ret = i2c_master_cmd_begin(_i2c_num, cmd, LCD_COMMAND_DELAY_MS / portTICK_PERIOD_MS);
i2c_cmd_link_delete(cmd);
ESP_LOGD(_TAG, "i2c_scan(0x%2x) returning:%i", addr, (int)ret);
return ret;
}
/**
* @brief test communication to device.
* @note i2c_driver_install must be done before init() by the calling program
* @return esp_err_t returns the return value of i2c_scan()
**/
// TODO change LOGI to LOGD
esp_err_t DFRobot_RGBLCD1602::init()
{
esp_err_t ret;
// find the LCD
ESP_RETURN_ON_ERROR(i2c_scan(LCD_LCD_ADDRESS), _TAG, "lcd not found");
// find the RGB device and set the color PWM registers
ret = i2c_scan(LCD_RGB_ADDRESS);
if (ret == ESP_OK) // found it
{
_RGBAddr = LCD_RGB_ADDRESS;
REG_RED = 0x04;
REG_GREEN = 0x03;
REG_BLUE = 0x02;
}
else
{
ret = i2c_scan(LCD_RGB_ADDRESS_ALT2);
if (ret == ESP_OK) // found it
{
_RGBAddr = LCD_RGB_ADDRESS_ALT2;
REG_RED = 0x06; // pwm2
REG_GREEN = 0x05; // pwm1
REG_BLUE = 0x04; // pwm0
}
else
{
ret = i2c_scan(LCD_RGB_ADDRESS_ALT);
if (ret == ESP_OK) // found it
{
_RGBAddr = LCD_RGB_ADDRESS_ALT;
REG_RED = 0x06; // pwm2
REG_GREEN = 0x07; // pwm1
REG_BLUE = 0x08; // pwm0
}
}
}
ESP_LOGD(_TAG, "backlight assigned to 0%02x", _RGBAddr);
_showFunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS;
begin(_rows);
return ret;
}
esp_err_t DFRobot_RGBLCD1602::clear()
{
esp_err_t ret = command(LCD_CLEARDISPLAY); // clear display, set cursor position to zero
vTaskDelay(pdMS_TO_TICKS(12)); // this command takes a long time!
home();
return ret;
}
esp_err_t DFRobot_RGBLCD1602::home()
{
esp_err_t ret = command(LCD_RETURNHOME); // set cursor position to zero
vTaskDelay(pdMS_TO_TICKS(12)); // this command takes a long time!
return ret;
}
esp_err_t DFRobot_RGBLCD1602::noDisplay()
{
_showControl &= ~LCD_DISPLAYON;
return command(LCD_DISPLAYCONTROL | _showControl);
}
esp_err_t DFRobot_RGBLCD1602::display()
{
_showControl |= LCD_DISPLAYON;
return command(LCD_DISPLAYCONTROL | _showControl);
}
esp_err_t DFRobot_RGBLCD1602::stopBlink()
{
_showControl &= ~LCD_BLINKON;
return command(LCD_DISPLAYCONTROL | _showControl);
}
esp_err_t DFRobot_RGBLCD1602::blink()
{
_showControl |= LCD_BLINKON;
return command(LCD_DISPLAYCONTROL | _showControl);
}
esp_err_t DFRobot_RGBLCD1602::noCursor()
{
_showControl &= ~LCD_CURSORON;
return command(LCD_DISPLAYCONTROL | _showControl);
}
esp_err_t DFRobot_RGBLCD1602::cursor()
{
_showControl |= LCD_CURSORON;
return command(LCD_DISPLAYCONTROL | _showControl);
}
// void DFRobot_RGBLCD1602::scrollDisplayLeft(void)
// {
// command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT);
// }
// void DFRobot_RGBLCD1602::scrollDisplayRight(void)
// {
// command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT);
// }
// void DFRobot_RGBLCD1602::leftToRight(void)
// {
// _showMode |= LCD_ENTRYLEFT;
// command(LCD_ENTRYMODESET | _showMode);
// }
// void DFRobot_RGBLCD1602::rightToLeft(void)
// {
// _showMode &= ~LCD_ENTRYLEFT;
// command(LCD_ENTRYMODESET | _showMode);
// }
esp_err_t DFRobot_RGBLCD1602::noAutoscroll(void)
{
_showMode &= ~LCD_ENTRYSHIFTINCREMENT;
return command(LCD_ENTRYMODESET | _showMode);
}
esp_err_t DFRobot_RGBLCD1602::autoscroll(void)
{
_showMode |= LCD_ENTRYSHIFTINCREMENT;
return command(LCD_ENTRYMODESET | _showMode);
}
esp_err_t DFRobot_RGBLCD1602::customSymbol(uint8_t location, uint8_t charmap[])
{
location &= 0x7; // we only have 8 locations 0-7
esp_err_t ret = command(LCD_SETCGRAMADDR | (location << 3));
if (ret != ESP_OK)
return ret;
uint8_t data[9];
data[0] = 0x40;
for (int i = 0; i < 8; i++)
{
data[i + 1] = charmap[i];
}
return i2c_master_write_to_device(_i2c_num, _lcdAddr,
data, 9,
5 / portTICK_PERIOD_MS);
}
esp_err_t DFRobot_RGBLCD1602::setCursor(uint8_t col, uint8_t row)
{
col = (row == 0 ? col | 0x80 : col | 0xc0);
const uint8_t data[] = {0x80, col};
return i2c_master_write_to_device(_i2c_num, _lcdAddr,
data, 2,
LCD_COMMAND_DELAY_MS / portTICK_PERIOD_MS);
}
// this function generally doesn't work as expected. Better to use setRGB().
void DFRobot_RGBLCD1602::setPWM(uint8_t color, uint8_t pwm)
{
setReg(color, pwm);
if (_RGBAddr == 0x6b)
{
setReg(0x07, pwm);
}
}
esp_err_t DFRobot_RGBLCD1602::setRGB(uint8_t r, uint8_t g, uint8_t b)
{
ESP_RETURN_ON_ERROR(setReg(REG_RED, r), _TAG, "setReg(0x%2x, 0x%2x)", REG_RED, r);
ESP_RETURN_ON_ERROR(setReg(REG_GREEN, g), _TAG, "setReg(0x%2x, 0x%2x)", REG_GREEN, g);
ESP_RETURN_ON_ERROR(setReg(REG_BLUE, b), _TAG, "setReg(0x%2x, 0x%2x)", REG_BLUE, b);
if (_RGBAddr == 0x6b)
{
setReg(0x07, 0xFF);
}
return ESP_OK;
}
// void DFRobot_RGBLCD1602::setColor(uint8_t color)
// {
// if(color > 3)return ;
// setRGB(color_define[color][0], color_define[color][1], color_define[color][2]);
// }
esp_err_t DFRobot_RGBLCD1602::blinkLED(void)
{
///< blink period in seconds = (<reg 7> + 1) / 24
///< on/off ratio = <reg 6> / 256
setReg(0x07, 0x17); // blink every second
return setReg(0x06, 0x7f); // half on, half off
}
esp_err_t DFRobot_RGBLCD1602::noBlinkLED(void)
{
setReg(0x07, 0x00);
return setReg(0x06, 0xff);
}
inline size_t DFRobot_RGBLCD1602::write(uint8_t value)
{
// TEST const uint8_t data[] = {0x40, value, 0};
const uint8_t data[] = {0x40, value};
esp_err_t ret = i2c_master_write_to_device(_i2c_num, _lcdAddr,
data, 2,
LCD_COMMAND_DELAY_MS / portTICK_PERIOD_MS);
if (ret == ESP_OK)
{
return 1;
}
else
{
return 0;
}
}
inline esp_err_t DFRobot_RGBLCD1602::command(uint8_t value)
{
const uint8_t data[] = {0x80, value};
return i2c_master_write_to_device(_i2c_num, _lcdAddr,
data, 2,
LCD_COMMAND_DELAY_MS / portTICK_PERIOD_MS);
}
#ifndef ARDUINO // roll our own print functions
void DFRobot_RGBLCD1602::print(const char chr)
{
write(chr);
}
void DFRobot_RGBLCD1602::print(const char *str)
{
while (*str)
{
write(*str++);
}
}
void DFRobot_RGBLCD1602::print(const int i)
{
char str[7];
sprintf(str, "%i", i);
print(str);
}
void DFRobot_RGBLCD1602::print(const float f, uint8_t decimalPlaces)
{
char str[16];
sprintf(str, "%.2f", f);
print(str);
}
#endif //ARDUINO
// void DFRobot_RGBLCD1602::setBacklight(bool mode){
// if(mode){
// setColorWhite(); // turn backlight on
// }else{
// closeBacklight(); // turn backlight off
// }
// }
// /*******************************private*******************************/
esp_err_t DFRobot_RGBLCD1602::begin(uint8_t rows, uint8_t charSize)
{
if (rows > 1)
{
_showFunction |= LCD_2LINE;
}
_numLines = rows;
_currLine = 0;
///< for some 1 line displays you can select a 10 pixel high font
if ((charSize != 0) && (rows == 1))
{
_showFunction |= LCD_5x10DOTS;
}
///< SEE PAGE 45/46 FOR INITIALIZATION SPECIFICATION!
///< according to datasheet, we need at least 40ms after power rises above 2.7V
///< before sending commands. Arduino can turn on way befer 4.5V so we'll wait 50
vTaskDelay(pdMS_TO_TICKS(100));
///< this is according to the hitachi HD44780 datasheet
///< page 45 figure 23
///< Send function set command sequence
command(LCD_FUNCTIONSET | _showFunction);
vTaskDelay(pdMS_TO_TICKS(5)); // wait more than 4.1ms
///< second try
command(LCD_FUNCTIONSET | _showFunction);
vTaskDelay(pdMS_TO_TICKS(5));
///< third go
ESP_ERROR_CHECK(command(LCD_FUNCTIONSET | _showFunction));
///< turn the display on with no cursor or blinking default
_showControl = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF;
display();
///< clear it off
clear();
///< Initialize to default text direction (for romance languages)
_showMode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT;
///< set the entry mode
command(LCD_ENTRYMODESET | _showMode);
if (_RGBAddr == LCD_RGB_ADDRESS)
{
///< backlight init
setReg(LCD_REG_MODE1, 0);
///< set LEDs controllable by both PWM and GRPPWM registers
setReg(LCD_REG_OUTPUT, 0xFF);
///< set MODE2 values
///< 0010 0000 -> 0x20 (DMBLNK to 1, ie blinky mode)
setReg(LCD_REG_MODE2, 0x20);
}
else if (_RGBAddr == 0x6B)
{
setReg(0x2F, 0x00);
setReg(0x00, 0x20);
setReg(0x01, 0x00);
setReg(0x02, 0x01);
setReg(0x03, 4);
}
else
{
setReg(0x04, 0x15);
}
setColorWhite();
return ESP_OK;
}
// esp_err_t DFRobot_RGBLCD1602::send(const uint8_t *data, uint8_t len)
// {
// // _pWire->beginTransmission(_lcdAddr); // transmit to device #4
// // for(int i=0; i<len; i++) {
// // _pWire->write(data[i]);
// // vTaskDelay(pdMS_TO_TICKS(5));
// // }
// // _pWire->endTransmission(); // stop transmitting
// return i2c_master_write_to_device(_i2c_num, _lcdAddr,
// data, len,
// LCD_COMMAND_DELAY_MS / portTICK_PERIOD_MS);
// }
esp_err_t DFRobot_RGBLCD1602::setReg(uint8_t addr, uint8_t data)
{
ESP_LOGD(_TAG, "i2c_master_write_to_device(port:%i, addr:0x%02x, reg:0x%02x, data:0x%02x)", _i2c_num, _RGBAddr, addr, data);
const uint8_t write_buffer[] = {addr, data};
return i2c_master_write_to_device(_i2c_num, _RGBAddr,
write_buffer, 2,
LCD_COMMAND_DELAY_MS / portTICK_PERIOD_MS);
}