1 | #ifndef LiquidTWI2_h
|
2 | #define LiquidTWI2_h
|
3 |
|
4 | #include <inttypes.h>
|
5 | #include "Print.h"
|
6 |
|
7 | // for memory-constrained projects, comment out the MCP230xx that doesn't apply
|
8 | #define MCP23017 // Adafruit RGB LCD (PANELOLU2 is now supported without additional define)
|
9 | #define MCP23008 // Adafruit I2C Backpack
|
10 |
|
11 | // if DETECT_DEVICE is enabled, then when constructor's detectDevice != 0
|
12 | // device will be detected in the begin() function...
|
13 | // if the device isn't detected in begin() then we won't try to talk to the
|
14 | // device in any of the other functions... this allows you to compile the
|
15 | // code w/o an LCD installed, and not get hung in the write functions
|
16 | #define DETECT_DEVICE // enable device detection code
|
17 |
|
18 | // for setBacklight() with MCP23017
|
19 | #define OFF 0x0
|
20 | #define RED 0x1
|
21 | #define YELLOW 0x3
|
22 | #define GREEN 0x2
|
23 | #define TEAL 0x6
|
24 | #define BLUE 0x4
|
25 | #define VIOLET 0x5
|
26 | #define WHITE 0x7
|
27 |
|
28 | // Standard directional button bits
|
29 | #define BUTTON_UP 0x08
|
30 | #define BUTTON_DOWN 0x04
|
31 | #define BUTTON_LEFT 0x10
|
32 | #define BUTTON_RIGHT 0x02
|
33 | #define BUTTON_SELECT 0x01
|
34 |
|
35 | // Panelolu2 encoder button bits (which has only rotary encoder and encoder button)
|
36 | #define PANELOLU2_ENCODER_C 0x04 // == encoder button
|
37 | #define PANELOLU2_ENCODER_B 0x02
|
38 | #define PANELOLU2_ENCODER_A 0x01
|
39 |
|
40 | // readButtons() will only return these bit values
|
41 | // (the Panelolu2 encoder bits are subset of these bits)
|
42 | #define ALL_BUTTON_BITS (BUTTON_UP|BUTTON_DOWN|BUTTON_LEFT|BUTTON_RIGHT|BUTTON_SELECT)
|
43 |
|
44 | #define MCP23008_ADDRESS 0x20
|
45 |
|
46 | // registers
|
47 | #define MCP23008_IODIR 0x00
|
48 | #define MCP23008_IPOL 0x01
|
49 | #define MCP23008_GPINTEN 0x02
|
50 | #define MCP23008_DEFVAL 0x03
|
51 | #define MCP23008_INTCON 0x04
|
52 | #define MCP23008_IOCON 0x05
|
53 | #define MCP23008_GPPU 0x06
|
54 | #define MCP23008_INTF 0x07
|
55 | #define MCP23008_INTCAP 0x08
|
56 | #define MCP23008_GPIO 0x09
|
57 | #define MCP23008_OLAT 0x0A
|
58 |
|
59 | #define MCP23017_ADDRESS 0x20
|
60 |
|
61 | // registers
|
62 | #define MCP23017_IODIRA 0x00
|
63 | #define MCP23017_IPOLA 0x02
|
64 | #define MCP23017_GPINTENA 0x04
|
65 | #define MCP23017_DEFVALA 0x06
|
66 | #define MCP23017_INTCONA 0x08
|
67 | #define MCP23017_IOCONA 0x0A
|
68 | #define MCP23017_GPPUA 0x0C
|
69 | #define MCP23017_INTFA 0x0E
|
70 | #define MCP23017_INTCAPA 0x10
|
71 | #define MCP23017_GPIOA 0x12
|
72 | #define MCP23017_OLATA 0x14
|
73 |
|
74 |
|
75 | #define MCP23017_IODIRB 0x01
|
76 | #define MCP23017_IPOLB 0x03
|
77 | #define MCP23017_GPINTENB 0x05
|
78 | #define MCP23017_DEFVALB 0x07
|
79 | #define MCP23017_INTCONB 0x09
|
80 | #define MCP23017_IOCONB 0x0B
|
81 | #define MCP23017_GPPUB 0x0D
|
82 | #define MCP23017_INTFB 0x0F
|
83 | #define MCP23017_INTCAPB 0x11
|
84 | #define MCP23017_GPIOB 0x13
|
85 | #define MCP23017_OLATB 0x15
|
86 |
|
87 | // commands
|
88 | #define LCD_CLEARDISPLAY 0x01
|
89 | #define LCD_RETURNHOME 0x02
|
90 | #define LCD_ENTRYMODESET 0x04
|
91 | #define LCD_DISPLAYCONTROL 0x08
|
92 | #define LCD_CURSORSHIFT 0x10
|
93 | #define LCD_FUNCTIONSET 0x20
|
94 | #define LCD_SETCGRAMADDR 0x40
|
95 | #define LCD_SETDDRAMADDR 0x80
|
96 |
|
97 | // flags for display entry mode
|
98 | #define LCD_ENTRYRIGHT 0x00
|
99 | #define LCD_ENTRYLEFT 0x02
|
100 | #define LCD_ENTRYSHIFTINCREMENT 0x01
|
101 | #define LCD_ENTRYSHIFTDECREMENT 0x00
|
102 |
|
103 | // flags for display on/off control
|
104 | #define LCD_BLINKON 0x01
|
105 | #define LCD_BLINKOFF 0x00
|
106 | #define LCD_CURSORON 0x02
|
107 | #define LCD_CURSOROFF 0x00
|
108 | #define LCD_DISPLAYON 0x04
|
109 | #define LCD_DISPLAYOFF 0x00
|
110 | #define LCD_BACKLIGHT 0x08 // used to pick out the backlight flag since _displaycontrol will never set itself
|
111 |
|
112 | // flags for display/cursor shift
|
113 | #define LCD_DISPLAYMOVE 0x08
|
114 | #define LCD_CURSORMOVE 0x00
|
115 | #define LCD_MOVERIGHT 0x04
|
116 | #define LCD_MOVELEFT 0x00
|
117 |
|
118 | // flags for function set
|
119 | //we only support 4-bit mode #define LCD_8BITMODE 0x10
|
120 | #define LCD_4BITMODE 0x00
|
121 | #define LCD_2LINE 0x08
|
122 | #define LCD_1LINE 0x00
|
123 | #define LCD_5x10DOTS 0x04
|
124 | #define LCD_5x8DOTS 0x00
|
125 |
|
126 | // for setMCPType()
|
127 | #define LTI_TYPE_MCP23008 0
|
128 | #define LTI_TYPE_MCP23017 1
|
129 | #define DEFAULT_TYPE LTI_TYPE_MCP23008
|
130 |
|
131 | class LiquidTWI2 : public Print {
|
132 | public:
|
133 | LiquidTWI2(uint8_t i2cAddr,uint8_t detectDevice=0,uint8_t backlightInverted=0);
|
134 |
|
135 | void begin(uint8_t cols, uint8_t rows,uint8_t charsize = LCD_5x8DOTS);
|
136 |
|
137 | #ifdef DETECT_DEVICE
|
138 | uint8_t LcdDetected() { return _deviceDetected; }
|
139 | #endif // DETECT_DEVICE
|
140 | void clear();
|
141 | void home();
|
142 |
|
143 | void noDisplay();
|
144 | void display();
|
145 | void noBlink();
|
146 | void blink();
|
147 | void noCursor();
|
148 | void cursor();
|
149 | void scrollDisplayLeft();
|
150 | void scrollDisplayRight();
|
151 | void leftToRight();
|
152 | void rightToLeft();
|
153 | void autoscroll();
|
154 | void noAutoscroll();
|
155 |
|
156 | void setBacklight(uint8_t status);
|
157 |
|
158 | void createChar(uint8_t, uint8_t[]);
|
159 | void setCursor(uint8_t, uint8_t);
|
160 | #if defined(ARDUINO) && (ARDUINO >= 100) // scl
|
161 | virtual size_t write(uint8_t);
|
162 | #else
|
163 | virtual void write(uint8_t);
|
164 | #endif
|
165 | void command(uint8_t);
|
166 | #ifdef MCP23017
|
167 | uint8_t readButtons();
|
168 | //check registers
|
169 | uint8_t readRegister(uint8_t);
|
170 | //set registers
|
171 | void setRegister(uint8_t, uint8_t);
|
172 | //make some noise
|
173 | void buzz(long,uint16_t);
|
174 | #endif
|
175 | void setMCPType(uint8_t mcptype) {
|
176 | #if defined(MCP23017)&&defined(MCP23008)
|
177 | _mcpType = mcptype;
|
178 | #endif //defined(MCP23017)&&defined(MCP23008)
|
179 | }
|
180 |
|
181 |
|
182 | private:
|
183 | void send(uint8_t, uint8_t);
|
184 | #ifdef MCP23017
|
185 | void burstBits16(uint16_t);
|
186 | void burstBits8b(uint8_t);
|
187 | //void burstBits8a(uint8_t);
|
188 | #endif
|
189 | #ifdef MCP23008
|
190 | void burstBits8(uint8_t);
|
191 | #endif
|
192 |
|
193 | uint8_t _displayfunction;
|
194 | uint8_t _displaycontrol;
|
195 | uint8_t _displaymode;
|
196 | uint8_t _numlines,_currline;
|
197 | uint8_t _i2cAddr;
|
198 | uint8_t _backlightInverted;
|
199 | #ifdef DETECT_DEVICE
|
200 | uint8_t _deviceDetected;
|
201 | #endif // DETECT_DEVICE
|
202 | #ifdef MCP23017
|
203 | uint16_t _backlightBits; // only for MCP23017
|
204 | #endif
|
205 | #if defined(MCP23017)&&defined(MCP23008)
|
206 | uint8_t _mcpType; // LTI_MODE_xx
|
207 | #endif
|
208 |
|
209 | };
|
210 |
|
211 | #endif
|