-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathi2c-lcd1602.c
627 lines (557 loc) · 20.8 KB
/
i2c-lcd1602.c
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
/*
* MIT License
*
* Copyright (c) 2018 David Antliff
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/**
* @file
*
* @brief
* The LCD1602 controller is an HD44780-compatible controller that normally operates
* via an 8-bit or 4-bit wide parallel bus.
*
* https://www.sparkfun.com/datasheets/LCD/HD44780.pdf
*
* The LCD1602 controller is connected to a PCF8574A I/O expander via the I2C bus.
* Only the top four bits are connected to the controller's data lines. The lower
* four bits are used as control lines:
*
* - B7: data bit 3
* - B6: data bit 2
* - B5: data bit 1
* - B4: data bit 0
* - B3: backlight (BL): off = 0, on = 1
* - B2: enable (EN): change from 1 to 0 to clock data into controller
* - B1: read/write (RW): write = 0, read = 1
* - B0: register select (RS): command = 0, data = 1
*
* Therefore to send a command byte requires the following operations:
*
* // First nibble:
* val = command & 0xf0 // extract top nibble
* val |= 0x04 // RS = 0 (command), RW = 0 (write), EN = 1
* i2c_write_byte(i2c_address, val)
* sleep(2ms)
* val &= 0xfb // EN = 0
* i2c_write_byte(i2c_address, val)
*
* // Second nibble:
* val = command & 0x0f // extract bottom nibble
* val |= 0x04 // RS = 0 (command), RW = 0 (write), EN = 1
* i2c_write_byte(i2c_address, val)
* sleep(2ms)
* val &= 0xfb // EN = 0
* i2c_write_byte(i2c_address, val)
*
* Sending a data byte is very similar except that RS = 1 (data)
*
* When the controller powers up, it defaults to:
*
* - display cleared
* - 8-bit interface, 1 line display, 5x8 dots per character
* - increment by 1 set
* - no shift
*
* The controller must be set to 4-bit operation before proper communication can commence.
* The initialisation sequence for 4-bit operation is:
*
* 0. wait > 15ms after Vcc rises to 4.5V, or > 40ms after Vcc rises to 2.7V
* 1. send nibble 0x03 // select 8-bit interface
* 2. wait > 4.1ms
* 3. send nibble 0x03 // select 8-bit interface again
* 4. wait > 100us
* 5. send command 0x32 // select 4-bit interface
* 6. send command 0x28 // set 2 lines and 5x7(8?) dots per character
* 7. send command 0x0c // display on, cursor off
* 8. send command 0x06 // move cursor right when writing, no scroll
* 9. send command 0x80 // set cursor to home position (row 1, column 1)
*/
#include <stddef.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_log.h"
#include "i2c-lcd1602.h"
#define TAG "i2c-lcd1602"
// Delays (microseconds)
#define DELAY_POWER_ON 50000 // wait at least 40us after VCC rises to 2.7V
#define DELAY_INIT_1 4500 // wait at least 4.1ms (fig 24, page 46)
#define DELAY_INIT_2 4500 // wait at least 4.1ms (fig 24, page 46)
#define DELAY_INIT_3 120 // wait at least 100us (fig 24, page 46)
#define DELAY_CLEAR_DISPLAY 2000
#define DELAY_RETURN_HOME 2000
#define DELAY_ENABLE_PULSE_WIDTH 1 // enable pulse must be at least 450ns wide
#define DELAY_ENABLE_PULSE_SETTLE 50 // command requires > 37us to settle (table 6 in datasheet)
// Commands
#define COMMAND_CLEAR_DISPLAY 0x01
#define COMMAND_RETURN_HOME 0x02
#define COMMAND_ENTRY_MODE_SET 0x04
#define COMMAND_DISPLAY_CONTROL 0x08
#define COMMAND_SHIFT 0x10
#define COMMAND_FUNCTION_SET 0x20
#define COMMAND_SET_CGRAM_ADDR 0x40
#define COMMAND_SET_DDRAM_ADDR 0x80
// COMMAND_ENTRY_MODE_SET flags
#define FLAG_ENTRY_MODE_SET_ENTRY_INCREMENT 0x02
#define FLAG_ENTRY_MODE_SET_ENTRY_DECREMENT 0x00
#define FLAG_ENTRY_MODE_SET_ENTRY_SHIFT_ON 0x01
#define FLAG_ENTRY_MODE_SET_ENTRY_SHIFT_OFF 0x00
// COMMAND_DISPLAY_CONTROL flags
#define FLAG_DISPLAY_CONTROL_DISPLAY_ON 0x04
#define FLAG_DISPLAY_CONTROL_DISPLAY_OFF 0x00
#define FLAG_DISPLAY_CONTROL_CURSOR_ON 0x02
#define FLAG_DISPLAY_CONTROL_CURSOR_OFF 0x00
#define FLAG_DISPLAY_CONTROL_BLINK_ON 0x01
#define FLAG_DISPLAY_CONTROL_BLINK_OFF 0x00
// COMMAND_SHIFT flags
#define FLAG_SHIFT_MOVE_DISPLAY 0x08
#define FLAG_SHIFT_MOVE_CURSOR 0x00
#define FLAG_SHIFT_MOVE_LEFT 0x04
#define FLAG_SHIFT_MOVE_RIGHT 0x00
// COMMAND_FUNCTION_SET flags
#define FLAG_FUNCTION_SET_MODE_8BIT 0x10
#define FLAG_FUNCTION_SET_MODE_4BIT 0x00
#define FLAG_FUNCTION_SET_LINES_2 0x08
#define FLAG_FUNCTION_SET_LINES_1 0x00
#define FLAG_FUNCTION_SET_DOTS_5X10 0x04
#define FLAG_FUNCTION_SET_DOTS_5X8 0x00
// Control flags
#define FLAG_BACKLIGHT_ON 0b00001000 // backlight enabled (disabled if clear)
#define FLAG_BACKLIGHT_OFF 0b00000000 // backlight disabled
#define FLAG_ENABLE 0b00000100
#define FLAG_READ 0b00000010 // read (write if clear)
#define FLAG_WRITE 0b00000000 // write
#define FLAG_RS_DATA 0b00000001 // data (command if clear)
#define FLAG_RS_COMMAND 0b00000000 // command
static bool _is_init(const i2c_lcd1602_info_t * i2c_lcd1602_info)
{
bool ok = false;
if (i2c_lcd1602_info != NULL)
{
if (i2c_lcd1602_info->init)
{
ok = true;
}
else
{
ESP_LOGE(TAG, "i2c_lcd1602_info is not initialised");
}
}
else
{
ESP_LOGE(TAG, "i2c_lcd1602_info is NULL");
}
return ok;
}
// Set or clear the specified flag depending on condition
static uint8_t _set_or_clear(uint8_t flags, bool condition, uint8_t flag)
{
if (condition)
{
flags |= flag;
}
else
{
flags &= ~flag;
}
return flags;
}
// send data to the I/O Expander
static esp_err_t _write_to_expander(const i2c_lcd1602_info_t * i2c_lcd1602_info, uint8_t data)
{
// backlight flag must be included with every write to maintain backlight state
ESP_LOGD(TAG, "_write_to_expander 0x%02x", data | i2c_lcd1602_info->backlight_flag);
return smbus_send_byte(i2c_lcd1602_info->smbus_info, data | i2c_lcd1602_info->backlight_flag);
}
// IMPORTANT - for the display to stay "in sync" it is important that errors do not interrupt the
// 2 x nibble sequence.
// clock data from expander to LCD by causing a falling edge on Enable
static esp_err_t _strobe_enable(const i2c_lcd1602_info_t * i2c_lcd1602_info, uint8_t data)
{
esp_err_t err1 = _write_to_expander(i2c_lcd1602_info, data | FLAG_ENABLE);
ets_delay_us(DELAY_ENABLE_PULSE_WIDTH);
esp_err_t err2 = _write_to_expander(i2c_lcd1602_info, data & ~FLAG_ENABLE);
ets_delay_us(DELAY_ENABLE_PULSE_SETTLE);
return err1 ? err1 : err2;
}
// send top nibble to the LCD controller
static esp_err_t _write_top_nibble(const i2c_lcd1602_info_t * i2c_lcd1602_info, uint8_t data)
{
ESP_LOGD(TAG, "_write_top_nibble 0x%02x", data);
esp_err_t err1 = _write_to_expander(i2c_lcd1602_info, data);
esp_err_t err2 = _strobe_enable(i2c_lcd1602_info, data);
return err1 ? err1 : err2;
}
// send command or data to controller
static esp_err_t _write(const i2c_lcd1602_info_t * i2c_lcd1602_info, uint8_t value, uint8_t register_select_flag)
{
ESP_LOGD(TAG, "_write 0x%02x | 0x%02x", value, register_select_flag);
esp_err_t err1 = _write_top_nibble(i2c_lcd1602_info, (value & 0xf0) | register_select_flag);
esp_err_t err2 = _write_top_nibble(i2c_lcd1602_info, ((value & 0x0f) << 4) | register_select_flag);
return err1 ? err1 : err2;
}
// send command to controller
static esp_err_t _write_command(const i2c_lcd1602_info_t * i2c_lcd1602_info, uint8_t command)
{
ESP_LOGD(TAG, "_write_command 0x%02x", command);
return _write(i2c_lcd1602_info, command, FLAG_RS_COMMAND);
}
// send data to controller
static esp_err_t _write_data(const i2c_lcd1602_info_t * i2c_lcd1602_info, uint8_t data)
{
ESP_LOGD(TAG, "_write_data 0x%02x", data);
return _write(i2c_lcd1602_info, data, FLAG_RS_DATA);
}
// Public API
i2c_lcd1602_info_t * i2c_lcd1602_malloc(void)
{
i2c_lcd1602_info_t * i2c_lcd1602_info = malloc(sizeof(*i2c_lcd1602_info));
if (i2c_lcd1602_info != NULL)
{
memset(i2c_lcd1602_info, 0, sizeof(*i2c_lcd1602_info));
ESP_LOGD(TAG, "malloc i2c_lcd1602_info_t %p", i2c_lcd1602_info);
}
else
{
ESP_LOGE(TAG, "malloc i2c_lcd1602_info_t failed");
}
return i2c_lcd1602_info;
}
void i2c_lcd1602_free(i2c_lcd1602_info_t ** i2c_lcd1602_info)
{
if (i2c_lcd1602_info != NULL && (*i2c_lcd1602_info != NULL))
{
ESP_LOGD(TAG, "free i2c_lcd1602_info_t %p", *i2c_lcd1602_info);
free(*i2c_lcd1602_info);
*i2c_lcd1602_info = NULL;
}
else
{
ESP_LOGE(TAG, "free i2c_lcd1602_info_t failed");
}
}
esp_err_t i2c_lcd1602_init(i2c_lcd1602_info_t * i2c_lcd1602_info, smbus_info_t * smbus_info,
bool backlight, uint8_t num_rows, uint8_t num_columns, uint8_t num_visible_columns)
{
esp_err_t err = ESP_FAIL;
if (i2c_lcd1602_info != NULL)
{
i2c_lcd1602_info->smbus_info = smbus_info;
i2c_lcd1602_info->backlight_flag = backlight ? FLAG_BACKLIGHT_ON : FLAG_BACKLIGHT_OFF;
i2c_lcd1602_info->num_rows = num_rows;
i2c_lcd1602_info->num_columns = num_columns;
i2c_lcd1602_info->num_visible_columns = num_visible_columns;
// display on, no cursor, no blinking
i2c_lcd1602_info->display_control_flags = FLAG_DISPLAY_CONTROL_DISPLAY_ON | FLAG_DISPLAY_CONTROL_CURSOR_OFF | FLAG_DISPLAY_CONTROL_BLINK_OFF;
// left-justified left-to-right text
i2c_lcd1602_info->entry_mode_flags = FLAG_ENTRY_MODE_SET_ENTRY_INCREMENT | FLAG_ENTRY_MODE_SET_ENTRY_SHIFT_OFF;
i2c_lcd1602_info->init = true;
// See page 45/46 of HD44780 data sheet for the initialisation procedure.
// Wait at least 40ms after power rises above 2.7V before sending commands.
ets_delay_us(DELAY_POWER_ON);
err = i2c_lcd1602_reset(i2c_lcd1602_info);
}
else
{
ESP_LOGE(TAG, "i2c_lcd1602_info is NULL");
err = ESP_FAIL;
}
return err;
}
esp_err_t i2c_lcd1602_reset(const i2c_lcd1602_info_t * i2c_lcd1602_info)
{
esp_err_t first_err = ESP_OK;
esp_err_t last_err = ESP_FAIL;
// put Expander into known state - Register Select and Read/Write both low
if ((last_err = _write_to_expander(i2c_lcd1602_info, 0)) != ESP_OK)
{
if (first_err == ESP_OK)
first_err = last_err;
ESP_LOGE(TAG, "reset: _write_to_expander 1 failed: %d", last_err);
}
ets_delay_us(1000);
// select 4-bit mode on LCD controller - see datasheet page 46, figure 24.
if ((last_err = _write_top_nibble(i2c_lcd1602_info, 0x03 << 4)) != ESP_OK)
{
if (first_err == ESP_OK)
first_err = last_err;
ESP_LOGE(TAG, "reset: _write_top_nibble 1 failed: %d", last_err);
}
ets_delay_us(DELAY_INIT_1);
// repeat
if ((last_err = _write_top_nibble(i2c_lcd1602_info, 0x03 << 4)) != ESP_OK)
{
if (first_err == ESP_OK)
first_err = last_err;
ESP_LOGE(TAG, "reset: _write_top_nibble 2 failed: %d", last_err);
}
ets_delay_us(DELAY_INIT_2);
// repeat
if ((last_err = _write_top_nibble(i2c_lcd1602_info, 0x03 << 4)) != ESP_OK)
{
if (first_err == ESP_OK)
first_err = last_err;
ESP_LOGE(TAG, "reset: _write_top_nibble 3 failed: %d", last_err);
}
ets_delay_us(DELAY_INIT_3);
// select 4-bit mode
if ((last_err = _write_top_nibble(i2c_lcd1602_info, 0x02 << 4)) != ESP_OK)
{
if (first_err == ESP_OK)
first_err = last_err;
ESP_LOGE(TAG, "reset: _write_top_nibble 4 failed: %d", last_err);
}
// now we can use the command()/write() functions
if ((last_err = _write_command(i2c_lcd1602_info, COMMAND_FUNCTION_SET | FLAG_FUNCTION_SET_MODE_4BIT | FLAG_FUNCTION_SET_LINES_2 | FLAG_FUNCTION_SET_DOTS_5X8)) != ESP_OK)
{
if (first_err == ESP_OK)
first_err = last_err;
ESP_LOGE(TAG, "reset: _write_command 1 failed: %d", last_err);
}
if ((last_err = _write_command(i2c_lcd1602_info, COMMAND_DISPLAY_CONTROL | i2c_lcd1602_info->display_control_flags)) != ESP_OK)
{
if (first_err == ESP_OK)
first_err = last_err;
ESP_LOGE(TAG, "reset: _write_command 2 failed: %d", last_err);
}
if ((last_err = i2c_lcd1602_clear(i2c_lcd1602_info)) != ESP_OK)
{
if (first_err == ESP_OK)
first_err = last_err;
ESP_LOGE(TAG, "reset: i2c_lcd1602_clear failed: %d", last_err);
}
if ((last_err = _write_command(i2c_lcd1602_info, COMMAND_ENTRY_MODE_SET | i2c_lcd1602_info->entry_mode_flags)) != ESP_OK)
{
if (first_err == ESP_OK)
first_err = last_err;
ESP_LOGE(TAG, "reset: _write_command 3 failed: %d", last_err);
}
if ((last_err = i2c_lcd1602_home(i2c_lcd1602_info)) != ESP_OK)
{
if (first_err == ESP_OK)
first_err = last_err;
ESP_LOGE(TAG, "reset: i2c_lcd1602_home failed: %d", last_err);
}
return first_err;
}
esp_err_t i2c_lcd1602_clear(const i2c_lcd1602_info_t * i2c_lcd1602_info)
{
esp_err_t err = ESP_FAIL;
if (_is_init(i2c_lcd1602_info))
{
err = _write_command(i2c_lcd1602_info, COMMAND_CLEAR_DISPLAY);
if (err == ESP_OK)
{
ets_delay_us(DELAY_CLEAR_DISPLAY);
}
}
return err;
}
esp_err_t i2c_lcd1602_home(const i2c_lcd1602_info_t * i2c_lcd1602_info)
{
esp_err_t err = ESP_FAIL;
if (_is_init(i2c_lcd1602_info))
{
err = _write_command(i2c_lcd1602_info, COMMAND_RETURN_HOME);
if (err == ESP_OK)
{
ets_delay_us(DELAY_RETURN_HOME);
}
}
return err;
}
esp_err_t i2c_lcd1602_move_cursor(const i2c_lcd1602_info_t * i2c_lcd1602_info, uint8_t col, uint8_t row)
{
esp_err_t err = ESP_FAIL;
if (_is_init(i2c_lcd1602_info))
{
const int row_offsets[] = { 0x00, 0x40, 0x14, 0x54 };
if (row > i2c_lcd1602_info->num_rows)
{
row = i2c_lcd1602_info->num_rows - 1;
}
if (col > i2c_lcd1602_info->num_columns)
{
col = i2c_lcd1602_info->num_columns - 1;
}
err = _write_command(i2c_lcd1602_info, COMMAND_SET_DDRAM_ADDR | (col + row_offsets[row]));
}
return err;
}
esp_err_t i2c_lcd1602_set_backlight(i2c_lcd1602_info_t * i2c_lcd1602_info, bool enable)
{
esp_err_t err = ESP_FAIL;
if (_is_init(i2c_lcd1602_info))
{
i2c_lcd1602_info->backlight_flag = _set_or_clear(i2c_lcd1602_info->backlight_flag, enable, FLAG_BACKLIGHT_ON);
err = _write_to_expander(i2c_lcd1602_info, 0);
}
return err;
}
esp_err_t i2c_lcd1602_set_display(i2c_lcd1602_info_t * i2c_lcd1602_info, bool enable)
{
esp_err_t err = ESP_FAIL;
if (_is_init(i2c_lcd1602_info))
{
i2c_lcd1602_info->display_control_flags = _set_or_clear(i2c_lcd1602_info->display_control_flags, enable, FLAG_DISPLAY_CONTROL_DISPLAY_ON);
err = _write_command(i2c_lcd1602_info, COMMAND_DISPLAY_CONTROL | i2c_lcd1602_info->display_control_flags);
}
return err;
}
esp_err_t i2c_lcd1602_set_cursor(i2c_lcd1602_info_t * i2c_lcd1602_info, bool enable)
{
esp_err_t err = ESP_FAIL;
if (_is_init(i2c_lcd1602_info))
{
i2c_lcd1602_info->display_control_flags = _set_or_clear(i2c_lcd1602_info->display_control_flags, enable, FLAG_DISPLAY_CONTROL_CURSOR_ON);
err = _write_command(i2c_lcd1602_info, COMMAND_DISPLAY_CONTROL | i2c_lcd1602_info->display_control_flags);
}
return err;
}
esp_err_t i2c_lcd1602_set_blink(i2c_lcd1602_info_t * i2c_lcd1602_info, bool enable)
{
esp_err_t err = ESP_FAIL;
if (_is_init(i2c_lcd1602_info))
{
i2c_lcd1602_info->display_control_flags = _set_or_clear(i2c_lcd1602_info->display_control_flags, enable, FLAG_DISPLAY_CONTROL_BLINK_ON);
err = _write_command(i2c_lcd1602_info, COMMAND_DISPLAY_CONTROL | i2c_lcd1602_info->display_control_flags);
}
return err;
}
esp_err_t i2c_lcd1602_set_left_to_right(i2c_lcd1602_info_t * i2c_lcd1602_info)
{
esp_err_t err = ESP_FAIL;
if (_is_init(i2c_lcd1602_info))
{
i2c_lcd1602_info->entry_mode_flags |= FLAG_ENTRY_MODE_SET_ENTRY_INCREMENT;
err = _write_command(i2c_lcd1602_info, COMMAND_ENTRY_MODE_SET | i2c_lcd1602_info->entry_mode_flags);
}
return err;
}
esp_err_t i2c_lcd1602_set_right_to_left(i2c_lcd1602_info_t * i2c_lcd1602_info)
{
esp_err_t err = ESP_FAIL;
if (_is_init(i2c_lcd1602_info))
{
i2c_lcd1602_info->entry_mode_flags &= ~FLAG_ENTRY_MODE_SET_ENTRY_INCREMENT;
err = _write_command(i2c_lcd1602_info, COMMAND_ENTRY_MODE_SET | i2c_lcd1602_info->entry_mode_flags);
}
return err;
}
esp_err_t i2c_lcd1602_set_auto_scroll(i2c_lcd1602_info_t * i2c_lcd1602_info, bool enable)
{
esp_err_t err = ESP_FAIL;
if (_is_init(i2c_lcd1602_info))
{
i2c_lcd1602_info->entry_mode_flags = _set_or_clear(i2c_lcd1602_info->entry_mode_flags, enable, FLAG_ENTRY_MODE_SET_ENTRY_SHIFT_ON);
err = _write_command(i2c_lcd1602_info, COMMAND_ENTRY_MODE_SET | i2c_lcd1602_info->entry_mode_flags);
}
return err;
}
esp_err_t i2c_lcd1602_scroll_display_left(const i2c_lcd1602_info_t * i2c_lcd1602_info)
{
esp_err_t err = ESP_FAIL;
if (_is_init(i2c_lcd1602_info))
{
// RAM is not changed
err = _write_command(i2c_lcd1602_info, COMMAND_SHIFT | FLAG_SHIFT_MOVE_DISPLAY | FLAG_SHIFT_MOVE_LEFT);
}
return err;
}
esp_err_t i2c_lcd1602_scroll_display_right(const i2c_lcd1602_info_t * i2c_lcd1602_info)
{
esp_err_t err = ESP_FAIL;
if (_is_init(i2c_lcd1602_info))
{
// RAM is not changed
err = _write_command(i2c_lcd1602_info, COMMAND_SHIFT | FLAG_SHIFT_MOVE_DISPLAY | FLAG_SHIFT_MOVE_RIGHT);
}
return err;
}
esp_err_t i2c_lcd1602_move_cursor_left(const i2c_lcd1602_info_t * i2c_lcd1602_info)
{
esp_err_t err = ESP_FAIL;
if (_is_init(i2c_lcd1602_info))
{
// RAM is not changed. Shift direction is inverted.
err = _write_command(i2c_lcd1602_info, COMMAND_SHIFT | FLAG_SHIFT_MOVE_CURSOR | FLAG_SHIFT_MOVE_RIGHT);
}
return err;
}
esp_err_t i2c_lcd1602_move_cursor_right(const i2c_lcd1602_info_t * i2c_lcd1602_info)
{
esp_err_t err = ESP_FAIL;
if (_is_init(i2c_lcd1602_info))
{
// RAM is not changed. Shift direction is inverted.
err = _write_command(i2c_lcd1602_info, COMMAND_SHIFT | FLAG_SHIFT_MOVE_CURSOR | FLAG_SHIFT_MOVE_LEFT);
}
return err;
}
esp_err_t i2c_lcd1602_define_char(const i2c_lcd1602_info_t * i2c_lcd1602_info, i2c_lcd1602_custom_index_t index, const uint8_t pixelmap[])
{
esp_err_t err = ESP_FAIL;
if (_is_init(i2c_lcd1602_info))
{
index &= 0x07; // only the first 8 indexes can be used for custom characters
err = _write_command(i2c_lcd1602_info, COMMAND_SET_CGRAM_ADDR | (index << 3));
for (int i = 0; err == ESP_OK && i < 8; ++i)
{
err = _write_data(i2c_lcd1602_info, pixelmap[i]);
}
}
return err;
}
esp_err_t i2c_lcd1602_write_char(const i2c_lcd1602_info_t * i2c_lcd1602_info, uint8_t chr)
{
esp_err_t err = ESP_FAIL;
if (_is_init(i2c_lcd1602_info))
{
err = _write_data(i2c_lcd1602_info, chr);
}
return err;
}
esp_err_t i2c_lcd1602_write_string(const i2c_lcd1602_info_t * i2c_lcd1602_info, const char * string)
{
esp_err_t err = ESP_FAIL;
if (_is_init(i2c_lcd1602_info))
{
//ESP_LOGI(TAG, "i2c_lcd1602_write_string: %s", string);
err = ESP_OK;
for (int i = 0; err == ESP_OK && string[i]; ++i)
{
err = _write_data(i2c_lcd1602_info, string[i]);
}
}
return err;
}
// TEMPLATE
#if 0
esp_err_t i2c_lcd1602_XXX(i2c_lcd1602_info_t * i2c_lcd1602_info)
{
esp_err_t err = ESP_FAIL;
if (_is_init(i2c_lcd1602_info))
{
}
return err;
}
#endif