-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLCD03.cpp
158 lines (138 loc) · 4.15 KB
/
LCD03.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
/*
LCD03.cpp - Arduino library for I2C LCD03 display from Robot Electronics
see http://www.robot-electronics.co.uk/htm/Lcd03tech.htm
Copyright (c) 2013 Ben Arblaster. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "LCD03.h"
#include <inttypes.h>
#include "Arduino.h"
#include <Wire.h>
char _i2c_address;
#ifdef ARDUINO_ARCH_esp826
uint8_t _sda_pin;
uint8_t _scl_pin;
LCD03::LCD03(char i2c_address, uint8_t sda_pin, uint8_t scl_pin) {
_sda_pin = sda_pin;
_scl_pin = scl_pin;
#else
LCD03::LCD03(char i2c_address) {
#endif
// Convert 8-bit address from LCD to 7-bit address for Wire
_i2c_address = i2c_address>>1;
}
void LCD03::begin(uint8_t cols, uint8_t rows) {
#ifdef ARDUINO_ARCH_esp826
Wire.begin(_sda_pin, _scl_pin);
#else
Wire.begin();
#endif
noCursor();
clear();
}
void LCD03::setCursor(uint8_t pos) {
pos++;
Wire.beginTransmission(_i2c_address);
Wire.write(REG_COMMAND);
Wire.write(LCD_CURSORPOS);
Wire.write(pos);
Wire.endTransmission();
}
void LCD03::setCursor(uint8_t col, uint8_t row) {
// convert LiquidCrystal col & rows to LCD03
col++;
row++;
Wire.beginTransmission(_i2c_address);
Wire.write(REG_COMMAND);
Wire.write(LCD_CURSORPOSXY);
Wire.write(row);
Wire.write(col);
Wire.endTransmission();
}
void LCD03::tabSet(uint8_t ts) {
Wire.beginTransmission(_i2c_address);
Wire.write(REG_COMMAND);
Wire.write(LCD_TABSET);
Wire.write(ts);
Wire.endTransmission();
}
void LCD03::createChar(uint8_t location, uint8_t charmap[]) {
// remap custom chars from 0-7 to match LiquidCrystal
location += LCD_CUSTOMCHAR_BASE;
Wire.beginTransmission(_i2c_address);
Wire.write(REG_COMMAND);
Wire.write(LCD_CUSTOMCHAR);
Wire.write(location);
for(int i = 0; i < 8; i++) {
Wire.write(charmap[i] |= LCD_CUSTOMCHAR_MASK);
}
Wire.endTransmission();
}
// get the free buffer capacity in bytes
uint8_t LCD03::bufferFreeBytes() {
Wire.requestFrom(_i2c_address, 1);
if(Wire.available()) {
return Wire.read();
}
}
uint16_t LCD03::readKeypad () {
Wire.requestFrom(_i2c_address, 3);
uint8_t lowByte;
uint8_t highByte;
uint16_t bothBytes;
// Don't care about the first byte
Wire.read();
// Read the low and high bytes
lowByte = Wire.read();
highByte = Wire.read();
bothBytes = (lowByte | highByte << 8);
return bothBytes;
}
// write a single byte in one transmission
size_t LCD03::write(uint8_t value) {
// remap custom chars to 0-7 to match LiquidCrystal
if(value < 8) {
value += LCD_CUSTOMCHAR_BASE;
}
send(value);
// assume write was a success
return 1;
}
// write multiple bytes in as many transmissions as required
size_t LCD03::write(const uint8_t *buffer, size_t size) {
Wire.beginTransmission(_i2c_address);
//wait until the buffer is empty enough
while(bufferFreeBytes() < BUFFER_LENGTH);
Wire.write(REG_COMMAND);
for(int i = 0; i < size; i++) {
// we've filled the I2C buffer, flush it and start a new transmission
if(i != 0 && i % (BUFFER_LENGTH-1) == 0) {
Wire.endTransmission();
//wait until the buffer is empty enough
while(bufferFreeBytes() < BUFFER_LENGTH);
Wire.beginTransmission(_i2c_address);
Wire.write(REG_COMMAND);
}
Wire.write(buffer[i]);
}
Wire.endTransmission();
// assume write was a success
return size;
}
// send a single byte command in a single transmission
void LCD03::send(uint8_t command) {
Wire.beginTransmission(_i2c_address);
Wire.write(REG_COMMAND);
Wire.write(command);
Wire.endTransmission();
}