forked from lincomatic/LiquidTWI2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
LiquidTWI2.h
255 lines (215 loc) · 6.59 KB
/
LiquidTWI2.h
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
#ifndef LiquidTWI2_h
#define LiquidTWI2_h
#include <inttypes.h>
#include "Print.h"
// for memory-constrained projects, comment out the MCP230xx that doesn't apply
#define MCP23017 // Adafruit RGB LCD (PANELOLU2 is now supported without additional define)
//#define MCP23008 // Adafruit I2C Backpack
// for setMCPType()
#define LTI_TYPE_MCP23008 0
#define LTI_TYPE_MCP23017 1
#define DEFAULT_TYPE LTI_TYPE_MCP23017
// if DETECT_DEVICE is enabled, then when constructor's detectDevice != 0
// device will be detected in the begin() function...
// if the device isn't detected in begin() then we won't try to talk to the
// device in any of the other functions... this allows you to compile the
// code w/o an LCD installed, and not get hung in the write functions
#define DETECT_DEVICE // enable device detection code
#define WIRE_FREQU 100000 // 100000-400000
// for setBacklight() with MCP23017
#define OFF 0x0
#define RED 0x1
#define YELLOW 0x3
#define GREEN 0x2
#define TEAL 0x6
#define BLUE 0x4
#define VIOLET 0x5
#define WHITE 0x7
// Standard directional button bits
#define BUTTON_UP 0x08
#define BUTTON_DOWN 0x04
#define BUTTON_LEFT 0x10
#define BUTTON_RIGHT 0x02
#define BUTTON_SELECT 0x01
// Panelolu2 encoder button bits (which has only rotary encoder and encoder button)
#define PANELOLU2_ENCODER_C 0x04 // == encoder button
#define PANELOLU2_ENCODER_B 0x02
#define PANELOLU2_ENCODER_A 0x01
// readButtons() will only return these bit values
// (the Panelolu2 encoder bits are subset of these bits)
#define ALL_BUTTON_BITS (BUTTON_UP|BUTTON_DOWN|BUTTON_LEFT|BUTTON_RIGHT|BUTTON_SELECT)
#define MCP23008_ADDRESS 0x27
#define MCP23008_IOCON_def 0x05 // (IOCON)
#define MCP23008_SEQOP 0x20 // switch off auto increment
// registers
#define MCP23008_IODIR 0x00
#define MCP23008_IPOL 0x01
#define MCP23008_GPINTEN 0x02
#define MCP23008_DEFVAL 0x03
#define MCP23008_INTCON 0x04
#define MCP23008_IOCON 0x05
#define MCP23008_GPPU 0x06
#define MCP23008_INTF 0x07
#define MCP23008_INTCAP 0x08
#define MCP23008_GPIO 0x09
#define MCP23008_OLAT 0x0A
#define MCP23017_ADDRESS 0x20
#define TWI2X
#ifdef TWI2X
// control register before bankswitch
#define MCP23017_IOCONB_def 0x0B
// registers after Bankswitch
#define MCP23017_IODIRA 0x00 // 0x00
#define MCP23017_IPOLA 0x01 // 0x02
#define MCP23017_GPINTENA 0x02 // 0x04
#define MCP23017_DEFVALA 0x03 // 0x06
#define MCP23017_INTCONA 0x04 // 0x08
#define MCP23017_IOCONA 0x05 // 0x0A
#define MCP23017_GPPUA 0x06 // 0x0C
#define MCP23017_INTFA 0x07 // 0x0E
#define MCP23017_INTCAPA 0x08 // 0x10
#define MCP23017_GPIOA 0x09 // 0x12
#define MCP23017_OLATA 0x0A // 0x14
#define MCP23017_IODIRB 0x10 // 0x01
#define MCP23017_IPOLB 0x11 // 0x03
#define MCP23017_GPINTENB 0x12 // 0x05
#define MCP23017_DEFVALB 0x13 // 0x07
#define MCP23017_INTCONB 0x14 // 0x09
#define MCP23017_IOCONB 0x15 // 0x0B
#define MCP23017_GPPUB 0x16 // 0x0D
#define MCP23017_INTFB 0x17 // 0x0F
#define MCP23017_INTCAPB 0x18 // 0x11
#define MCP23017_GPIOB 0x19 // 0x13
#define MCP23017_OLATB 0x1A // 0x15
#else / default settings with auto increment of registerupdate
// registers
#define MCP23017_IODIRA 0x00
#define MCP23017_IPOLA 0x02
#define MCP23017_GPINTENA 0x04
#define MCP23017_DEFVALA 0x06
#define MCP23017_INTCONA 0x08
#define MCP23017_IOCONA 0x0A
#define MCP23017_GPPUA 0x0C
#define MCP23017_INTFA 0x0E
#define MCP23017_INTCAPA 0x10
#define MCP23017_GPIOA 0x12
#define MCP23017_OLATA 0x14
#define MCP23017_IODIRB 0x01
#define MCP23017_IPOLB 0x03
#define MCP23017_GPINTENB 0x05
#define MCP23017_DEFVALB 0x07
#define MCP23017_INTCONB 0x09
#define MCP23017_IOCONB 0x0B
#define MCP23017_GPPUB 0x0D
#define MCP23017_INTFB 0x0F
#define MCP23017_INTCAPB 0x11
#define MCP23017_GPIOB 0x13
#define MCP23017_OLATB 0x15
#endif
// commands
#define LCD_CLEARDISPLAY 0x01
#define LCD_RETURNHOME 0x02
#define LCD_ENTRYMODESET 0x04
#define LCD_DISPLAYCONTROL 0x08
#define LCD_CURSORSHIFT 0x10
#define LCD_FUNCTIONSET 0x20
#define LCD_SETCGRAMADDR 0x40
#define LCD_SETDDRAMADDR 0x80
// flags for display entry mode
#define LCD_ENTRYRIGHT 0x00
#define LCD_ENTRYLEFT 0x02
#define LCD_ENTRYSHIFTINCREMENT 0x01
#define LCD_ENTRYSHIFTDECREMENT 0x00
// flags for display on/off control
#define LCD_BLINKON 0x01
#define LCD_BLINKOFF 0x00
#define LCD_CURSORON 0x02
#define LCD_CURSOROFF 0x00
#define LCD_DISPLAYON 0x04
#define LCD_DISPLAYOFF 0x00
#define LCD_BACKLIGHT 0x08 // used to pick out the backlight flag since _displaycontrol will never set itself
// flags for display/cursor shift
#define LCD_DISPLAYMOVE 0x08
#define LCD_CURSORMOVE 0x00
#define LCD_MOVERIGHT 0x04
#define LCD_MOVELEFT 0x00
// flags for function set
//we only support 4-bit mode
#define LCD_8BITMODE 0x10
#define LCD_4BITMODE 0x00
#define LCD_2LINE 0x08
#define LCD_1LINE 0x00
#define LCD_5x10DOTS 0x04
#define LCD_5x8DOTS 0x00
class LiquidTWI2 : public Print {
public:
LiquidTWI2(uint8_t i2cAddr,uint8_t detectDevice=0,uint8_t backlightInverted=0);
void begin(uint8_t cols, uint8_t rows,uint8_t charsize = LCD_5x8DOTS);
#ifdef DETECT_DEVICE
uint8_t LcdDetected() { return _deviceDetected; }
#endif // DETECT_DEVICE
void clear();
void home();
void noDisplay();
void display();
void noBlink();
void blink();
void noCursor();
void cursor();
void scrollDisplayLeft();
void scrollDisplayRight();
void leftToRight();
void rightToLeft();
void autoscroll();
void noAutoscroll();
void setBacklight(uint8_t status);
void createChar(uint8_t, uint8_t[]);
void setCursor(uint8_t, uint8_t);
#if defined(ARDUINO) && (ARDUINO >= 100) // scl
virtual size_t write(uint8_t);
#else
virtual void write(uint8_t);
#endif
virtual size_t write(uint8_t *, uint8_t );
void command(uint8_t);
#ifdef MCP23017
uint8_t readButtons();
//check registers
uint8_t readRegister(uint8_t);
//set registers
void setRegister(uint8_t, uint8_t);
//make some noise
void buzz(long,uint16_t);
#endif
void setMCPType(uint8_t mcptype) {
#if defined(MCP23017)&& defined(MCP23008)
_mcpType = mcptype;
#endif //defined(MCP23017)&& defined(MCP23008)
}
void setClock(uint32_t);
private:
void send(uint8_t,uint8_t);
#ifdef MCP23017
void burstBits16(uint16_t);
void burstBits8b(uint8_t);
#endif
#ifdef MCP23008
void burstBits8(uint8_t);
#endif
uint8_t _displayfunction;
uint8_t _displaycontrol;
uint8_t _displaymode;
uint8_t _numlines,_currline;
uint8_t _i2cAddr;
uint8_t _backlightInverted;
#ifdef DETECT_DEVICE
uint8_t _deviceDetected;
#endif // DETECT_DEVICE
#ifdef MCP23017
uint16_t _backlightBits; // only for MCP23017
#endif
#if defined(MCP23017)&&defined(MCP23008)
uint8_t _mcpType; // LTI_MODE_xx
#endif
};
#endif