|
| 1 | +// |
| 2 | +// FILE: I2C_LCD.cpp |
| 3 | + |
| 4 | +// VERSION: 0.1.0 |
| 5 | +// DATE: 2023-12-16 |
| 6 | +// PUPROSE: Arduino library for I2C_LCD |
| 7 | +// URL: https://github.com/RobTillaart/I2C_LCD |
| 8 | + |
| 9 | + |
| 10 | +#include "I2C_LCD.h" |
| 11 | + |
| 12 | + |
| 13 | +// TODO redo simplify names shorter |
| 14 | +// or keep them compatible? |
| 15 | +#define I2C_LCD_CLEARDISPLAY 0x01 |
| 16 | +#define I2C_LCD_RETURNHOME 0x02 |
| 17 | +#define I2C_LCD_ENTRYMODESET 0x04 |
| 18 | +#define I2C_LCD_DISPLAYCONTROL 0x08 |
| 19 | +#define I2C_LCD_CURSORSHIFT 0x10 |
| 20 | +#define I2C_LCD_FUNCTIONSET 0x20 |
| 21 | +#define I2C_LCD_SETCGRAMADDR 0x40 |
| 22 | +#define I2C_LCD_SETDDRAMADDR 0x80 |
| 23 | + |
| 24 | +#define I2C_LCD_ENTRYRIGHT 0x00 |
| 25 | +#define I2C_LCD_ENTRYLEFT 0x02 |
| 26 | +#define I2C_LCD_ENTRYSHIFTINCREMENT 0x01 |
| 27 | +#define I2C_LCD_ENTRYSHIFTDECREMENT 0x00 |
| 28 | + |
| 29 | +#define I2C_LCD_DISPLAYON 0x04 |
| 30 | +#define I2C_LCD_DISPLAYOFF 0x00 |
| 31 | +#define I2C_LCD_CURSORON 0x02 |
| 32 | +#define I2C_LCD_CURSOROFF 0x00 |
| 33 | +#define I2C_LCD_BLINKON 0x01 |
| 34 | +#define I2C_LCD_BLINKOFF 0x00 |
| 35 | + |
| 36 | +#define I2C_LCD_DISPLAYMOVE 0x08 |
| 37 | +#define I2C_LCD_CURSORMOVE 0x00 |
| 38 | +#define I2C_LCD_MOVERIGHT 0x04 |
| 39 | +#define I2C_LCD_MOVELEFT 0x00 |
| 40 | + |
| 41 | +#define I2C_LCD_8BITMODE 0x10 |
| 42 | +#define I2C_LCD_4BITMODE 0x00 |
| 43 | +#define I2C_LCD_2LINE 0x08 |
| 44 | +#define I2C_LCD_1LINE 0x00 |
| 45 | +#define I2C_LCD_5x10DOTS 0x04 |
| 46 | +#define I2C_LCD_5x8DOTS 0x00 |
| 47 | + |
| 48 | + |
| 49 | +I2C_LCD::I2C_LCD(uint8_t address, TwoWire * wire) |
| 50 | +{ |
| 51 | + _address = address; |
| 52 | + _wire = wire; |
| 53 | +} |
| 54 | + |
| 55 | + |
| 56 | +void I2C_LCD::config (uint8_t address, uint8_t enable, uint8_t readwrite, uint8_t registerselect, |
| 57 | + uint8_t data4, uint8_t data5, uint8_t data6, uint8_t data7, |
| 58 | + uint8_t backlight) |
| 59 | +{ |
| 60 | + if (_address != address) return; // compatible |
| 61 | + _enable = ( 1 << enable); |
| 62 | + _readWrite = ( 1 << readwrite); |
| 63 | + _registerSelect = ( 1 << registerselect); |
| 64 | + _dataPin[0] = ( 1 << data4); |
| 65 | + _dataPin[1] = ( 1 << data5); |
| 66 | + _dataPin[2] = ( 1 << data6); |
| 67 | + _dataPin[3] = ( 1 << data7); |
| 68 | + _backLight = ( 1 << backlight); |
| 69 | + |
| 70 | + _pinsInOrder = ((data4 < data5) && (data5 < data6) && (data6 < data7)); |
| 71 | +} |
| 72 | + |
| 73 | + |
| 74 | +void I2C_LCD::begin(uint8_t cols, uint8_t rows) |
| 75 | +{ |
| 76 | + _cols = cols; |
| 77 | + _rows = rows; |
| 78 | + |
| 79 | + // ALL LOW. |
| 80 | + _wire->beginTransmission(_address); |
| 81 | + _wire->write(0x00); |
| 82 | + _wire->endTransmission(); |
| 83 | + |
| 84 | + // wait for 50 ms |
| 85 | + delay(50); |
| 86 | + |
| 87 | + // Force 4 bit mode // todo timing... |
| 88 | + write4bits(0x03); |
| 89 | + delay(5); |
| 90 | + write4bits(0x03); |
| 91 | + delay(1); |
| 92 | + write4bits(0x03); |
| 93 | + delay(1); |
| 94 | + write4bits(0x02); |
| 95 | + delay(1); |
| 96 | + |
| 97 | + // set "two" lines LCD - always a 20 x 4 for now. |
| 98 | + sendCommand(I2C_LCD_FUNCTIONSET | I2C_LCD_2LINE); |
| 99 | +} |
| 100 | + |
| 101 | + |
| 102 | +void I2C_LCD::setBacklight(bool on) |
| 103 | +{ |
| 104 | + if (on) display(); // todo fix for real. |
| 105 | + else noDisplay(); |
| 106 | +} |
| 107 | + |
| 108 | + |
| 109 | +void I2C_LCD::display() |
| 110 | +{ |
| 111 | + sendCommand(I2C_LCD_DISPLAYCONTROL | I2C_LCD_DISPLAYON ); |
| 112 | +} |
| 113 | + |
| 114 | + |
| 115 | +void I2C_LCD::noDisplay() |
| 116 | +{ |
| 117 | + sendCommand(I2C_LCD_DISPLAYCONTROL | I2C_LCD_DISPLAYOFF); |
| 118 | +} |
| 119 | + |
| 120 | + |
| 121 | +void I2C_LCD::clear() |
| 122 | +{ |
| 123 | + sendCommand(I2C_LCD_CLEARDISPLAY); |
| 124 | + delay(2); |
| 125 | +} |
| 126 | + |
| 127 | + |
| 128 | +void I2C_LCD::home() |
| 129 | +{ |
| 130 | + sendCommand(I2C_LCD_RETURNHOME); |
| 131 | + delay(2); |
| 132 | +} |
| 133 | + |
| 134 | + |
| 135 | +void I2C_LCD::setCursor(uint8_t col, uint8_t row) |
| 136 | +{ |
| 137 | + uint8_t startpos[4] = { 0x00, 0x40, 0x14, 0x54 }; |
| 138 | + sendCommand(I2C_LCD_SETDDRAMADDR | (startpos[row] + col) ); |
| 139 | +} |
| 140 | + |
| 141 | + |
| 142 | +size_t I2C_LCD::write(uint8_t c) |
| 143 | +{ |
| 144 | + sendData(c); // add timestamp.. |
| 145 | + return 1; |
| 146 | +}; |
| 147 | + |
| 148 | + |
| 149 | +////////////////////////////////////////////////////////// |
| 150 | +// |
| 151 | +// PRIVATE |
| 152 | +// |
| 153 | + |
| 154 | +// TODO merge these two |
| 155 | +// optimize 95% identical. |
| 156 | +void I2C_LCD::sendCommand(uint8_t value) |
| 157 | +{ |
| 158 | + uint8_t MSN = _backLight; |
| 159 | + uint8_t LSN = MSN; |
| 160 | + |
| 161 | + // pins are in the right order. |
| 162 | + // TODO determine a flag and |
| 163 | + if (_pinsInOrder) |
| 164 | + { |
| 165 | + MSN |= value & 0xF0; |
| 166 | + LSN |= value << 4; |
| 167 | + } |
| 168 | + else |
| 169 | + { |
| 170 | + uint8_t tmp = value >> 4; |
| 171 | + for ( uint8_t i = 0; i < 4; i++ ) |
| 172 | + { |
| 173 | + if ( tmp & 0x01 ) MSN |= _dataPin[i]; |
| 174 | + tmp >>= 1; |
| 175 | + } |
| 176 | + tmp = value & 0x0F; |
| 177 | + for ( uint8_t i = 0; i < 4; i++ ) |
| 178 | + { |
| 179 | + if ( tmp & 0x01 ) LSN |= _dataPin[i]; |
| 180 | + tmp >>= 1; |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + _wire->beginTransmission(_address); |
| 185 | + _wire->write(MSN | _enable); |
| 186 | + _wire->write(MSN); |
| 187 | + _wire->write(LSN | _enable); |
| 188 | + _wire->write(LSN); |
| 189 | + _wire->endTransmission(); |
| 190 | +} |
| 191 | + |
| 192 | + |
| 193 | +void I2C_LCD::sendData(uint8_t value) |
| 194 | +{ |
| 195 | + uint8_t MSN = _registerSelect | _backLight; |
| 196 | + uint8_t LSN = MSN; |
| 197 | + |
| 198 | + // if pins are in the right order speed up. |
| 199 | + // todo determine a flag in config. |
| 200 | + if (_pinsInOrder) |
| 201 | + { |
| 202 | + MSN |= value & 0xF0; |
| 203 | + LSN |= value << 4; |
| 204 | + } |
| 205 | + else |
| 206 | + { |
| 207 | + uint8_t tmp = value >> 4; |
| 208 | + for ( uint8_t i = 0; i < 4; i++ ) |
| 209 | + { |
| 210 | + if ( tmp & 0x01 ) MSN |= _dataPin[i]; |
| 211 | + tmp >>= 1; |
| 212 | + } |
| 213 | + tmp = value & 0x0F; |
| 214 | + for ( uint8_t i = 0; i < 4; i++ ) |
| 215 | + { |
| 216 | + if ( tmp & 0x01 ) LSN |= _dataPin[i]; |
| 217 | + tmp >>= 1; |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + _wire->beginTransmission(_address); |
| 222 | + _wire->write(MSN | _enable); |
| 223 | + _wire->write(MSN); |
| 224 | + _wire->write(LSN | _enable); |
| 225 | + _wire->write(LSN); |
| 226 | + _wire->endTransmission(); |
| 227 | +} |
| 228 | + |
| 229 | + |
| 230 | +// really needed for setup |
| 231 | +void I2C_LCD::write4bits(uint8_t value) |
| 232 | +{ |
| 233 | + uint8_t cmd = _backLight; |
| 234 | + |
| 235 | + for ( uint8_t i = 0; i < 4; i++ ) |
| 236 | + { |
| 237 | + if ( value & 0x01 ) cmd |= _dataPin[i]; |
| 238 | + value >>= 1; |
| 239 | + } |
| 240 | + |
| 241 | + _wire->beginTransmission(_address); |
| 242 | + _wire->write(cmd | _enable); |
| 243 | + // _wire->endTransmission(); |
| 244 | + // _wire->beginTransmission(_address); |
| 245 | + _wire->write(cmd); |
| 246 | + _wire->endTransmission(); |
| 247 | +} |
| 248 | + |
| 249 | + |
| 250 | +// -- END OF FILE -- |
| 251 | + |
0 commit comments