Text console library for ST7032 text LCD controller over I2C interface.
TextLCD_ST7032I2C.cpp@0:db0ffd858ca1, 2015-04-06 (annotated)
- Committer:
- kayekss
- Date:
- Mon Apr 06 20:16:13 2015 +0000
- Revision:
- 0:db0ffd858ca1
- Child:
- 1:0b6f271981a5
Initial release.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kayekss | 0:db0ffd858ca1 | 1 | // ==================================================== Mar 18 2015, kayeks == |
kayekss | 0:db0ffd858ca1 | 2 | // TextLCD_ST7032I2C.cpp |
kayekss | 0:db0ffd858ca1 | 3 | // =========================================================================== |
kayekss | 0:db0ffd858ca1 | 4 | // Text console library for ST7032 text LCD controller over I2C interface. |
kayekss | 0:db0ffd858ca1 | 5 | |
kayekss | 0:db0ffd858ca1 | 6 | #include <string.h> |
kayekss | 0:db0ffd858ca1 | 7 | #include "TextLCD_ST7032I2C.h" |
kayekss | 0:db0ffd858ca1 | 8 | |
kayekss | 0:db0ffd858ca1 | 9 | TextLCD_ST7032I2C::TextLCD_ST7032I2C(PinName sda, PinName scl, uint8_t address, |
kayekss | 0:db0ffd858ca1 | 10 | uint8_t columns, uint8_t rows) |
kayekss | 0:db0ffd858ca1 | 11 | : |
kayekss | 0:db0ffd858ca1 | 12 | _i2c(sda, scl) |
kayekss | 0:db0ffd858ca1 | 13 | { |
kayekss | 0:db0ffd858ca1 | 14 | // Copy parameters |
kayekss | 0:db0ffd858ca1 | 15 | _columns = MIN(columns, 16); |
kayekss | 0:db0ffd858ca1 | 16 | _rows = MIN(rows, 2); |
kayekss | 0:db0ffd858ca1 | 17 | _address = address & 0xfe; |
kayekss | 0:db0ffd858ca1 | 18 | |
kayekss | 0:db0ffd858ca1 | 19 | // Allocate line buffer |
kayekss | 0:db0ffd858ca1 | 20 | _lineBuffer = new uint8_t*[_rows]; |
kayekss | 0:db0ffd858ca1 | 21 | for (uint8_t i = 0; i < _rows; i++) { |
kayekss | 0:db0ffd858ca1 | 22 | _lineBuffer[i] = new uint8_t[_columns]; |
kayekss | 0:db0ffd858ca1 | 23 | } |
kayekss | 0:db0ffd858ca1 | 24 | } |
kayekss | 0:db0ffd858ca1 | 25 | |
kayekss | 0:db0ffd858ca1 | 26 | TextLCD_ST7032I2C::~TextLCD_ST7032I2C() { |
kayekss | 0:db0ffd858ca1 | 27 | for (uint8_t i = 0; i < _rows; i++) { |
kayekss | 0:db0ffd858ca1 | 28 | delete[] _lineBuffer[i]; |
kayekss | 0:db0ffd858ca1 | 29 | } |
kayekss | 0:db0ffd858ca1 | 30 | delete[] _lineBuffer; |
kayekss | 0:db0ffd858ca1 | 31 | } |
kayekss | 0:db0ffd858ca1 | 32 | |
kayekss | 0:db0ffd858ca1 | 33 | void TextLCD_ST7032I2C::clear() { |
kayekss | 0:db0ffd858ca1 | 34 | for (uint8_t j = 0; j < _rows; j++) { |
kayekss | 0:db0ffd858ca1 | 35 | locate(0, j); |
kayekss | 0:db0ffd858ca1 | 36 | for (uint8_t i = 0; i < _columns; i++) { |
kayekss | 0:db0ffd858ca1 | 37 | data(' '); |
kayekss | 0:db0ffd858ca1 | 38 | } |
kayekss | 0:db0ffd858ca1 | 39 | } |
kayekss | 0:db0ffd858ca1 | 40 | } |
kayekss | 0:db0ffd858ca1 | 41 | |
kayekss | 0:db0ffd858ca1 | 42 | void TextLCD_ST7032I2C::init(uint8_t osc, uint8_t rab, uint8_t contrast, Bias bias) { |
kayekss | 0:db0ffd858ca1 | 43 | // Copy parameters |
kayekss | 0:db0ffd858ca1 | 44 | _osc = osc & 0x07; |
kayekss | 0:db0ffd858ca1 | 45 | _rab = MIN(rab, 7); |
kayekss | 0:db0ffd858ca1 | 46 | _contrast = MIN(contrast, 63); |
kayekss | 0:db0ffd858ca1 | 47 | _bias = bias; |
kayekss | 0:db0ffd858ca1 | 48 | |
kayekss | 0:db0ffd858ca1 | 49 | command(FUNCTION_8B_2LINE_7DOT_IS0); |
kayekss | 0:db0ffd858ca1 | 50 | command(FUNCTION_8B_2LINE_7DOT_IS1); |
kayekss | 0:db0ffd858ca1 | 51 | switch (bias) { |
kayekss | 0:db0ffd858ca1 | 52 | case Bias1_4: |
kayekss | 0:db0ffd858ca1 | 53 | command(IS1_BIAS4_OSC + _osc); |
kayekss | 0:db0ffd858ca1 | 54 | break; |
kayekss | 0:db0ffd858ca1 | 55 | case Bias1_5: |
kayekss | 0:db0ffd858ca1 | 56 | command(IS1_BIAS5_OSC + _osc); |
kayekss | 0:db0ffd858ca1 | 57 | break; |
kayekss | 0:db0ffd858ca1 | 58 | } |
kayekss | 0:db0ffd858ca1 | 59 | command(IS1_CONTRAST + (_contrast & 0x0f)); |
kayekss | 0:db0ffd858ca1 | 60 | command(IS1_ICON_ON_BOOST_ON_CONTRAST + (_contrast >> 4)); |
kayekss | 0:db0ffd858ca1 | 61 | command(IS1_FOLLOWER_ON_RAB + _rab); |
kayekss | 0:db0ffd858ca1 | 62 | if (_rows == 1) { |
kayekss | 0:db0ffd858ca1 | 63 | command(FUNCTION_8B_1LINE_7DOT_IS0); |
kayekss | 0:db0ffd858ca1 | 64 | } else if (_rows == 2) { |
kayekss | 0:db0ffd858ca1 | 65 | command(FUNCTION_8B_2LINE_7DOT_IS0); |
kayekss | 0:db0ffd858ca1 | 66 | } |
kayekss | 0:db0ffd858ca1 | 67 | command(DISPLAY_ON_CURSOR_OFF_POS_OFF); |
kayekss | 0:db0ffd858ca1 | 68 | } |
kayekss | 0:db0ffd858ca1 | 69 | |
kayekss | 0:db0ffd858ca1 | 70 | void TextLCD_ST7032I2C::cls() { |
kayekss | 0:db0ffd858ca1 | 71 | clear(); |
kayekss | 0:db0ffd858ca1 | 72 | for (uint8_t j = 0; j < _rows; j++) { |
kayekss | 0:db0ffd858ca1 | 73 | memset(_lineBuffer[j], ' ', _columns); |
kayekss | 0:db0ffd858ca1 | 74 | } |
kayekss | 0:db0ffd858ca1 | 75 | _column = 0; |
kayekss | 0:db0ffd858ca1 | 76 | _row = 0; |
kayekss | 0:db0ffd858ca1 | 77 | } |
kayekss | 0:db0ffd858ca1 | 78 | |
kayekss | 0:db0ffd858ca1 | 79 | void TextLCD_ST7032I2C::locate(uint8_t column, uint8_t row) { |
kayekss | 0:db0ffd858ca1 | 80 | if (column < _columns && row < _rows) { |
kayekss | 0:db0ffd858ca1 | 81 | command(DDRAM + ((row << 6) | column)); |
kayekss | 0:db0ffd858ca1 | 82 | _column = column; |
kayekss | 0:db0ffd858ca1 | 83 | _row = row; |
kayekss | 0:db0ffd858ca1 | 84 | } |
kayekss | 0:db0ffd858ca1 | 85 | } |
kayekss | 0:db0ffd858ca1 | 86 | |
kayekss | 0:db0ffd858ca1 | 87 | void TextLCD_ST7032I2C::iconOn() { |
kayekss | 0:db0ffd858ca1 | 88 | is1(); |
kayekss | 0:db0ffd858ca1 | 89 | command(IS1_ICON_ON_BOOST_ON_CONTRAST + (_contrast >> 4)); |
kayekss | 0:db0ffd858ca1 | 90 | } |
kayekss | 0:db0ffd858ca1 | 91 | |
kayekss | 0:db0ffd858ca1 | 92 | void TextLCD_ST7032I2C::iconOff() { |
kayekss | 0:db0ffd858ca1 | 93 | is1(); |
kayekss | 0:db0ffd858ca1 | 94 | command(IS1_ICON_OFF_BOOST_ON_CONTRAST + (_contrast >> 4)); |
kayekss | 0:db0ffd858ca1 | 95 | } |
kayekss | 0:db0ffd858ca1 | 96 | |
kayekss | 0:db0ffd858ca1 | 97 | void TextLCD_ST7032I2C::setIcon(uint8_t iconAddr, uint8_t bits) { |
kayekss | 0:db0ffd858ca1 | 98 | is1(); |
kayekss | 0:db0ffd858ca1 | 99 | command(IS1_ICON_ADDR + (iconAddr & 0x0f)); |
kayekss | 0:db0ffd858ca1 | 100 | data(bits & 0x1f); |
kayekss | 0:db0ffd858ca1 | 101 | } |
kayekss | 0:db0ffd858ca1 | 102 | |
kayekss | 0:db0ffd858ca1 | 103 | int TextLCD_ST7032I2C::_putc(int c) { |
kayekss | 0:db0ffd858ca1 | 104 | switch (c) { |
kayekss | 0:db0ffd858ca1 | 105 | case '\r': // Carriage return |
kayekss | 0:db0ffd858ca1 | 106 | _column = 0; |
kayekss | 0:db0ffd858ca1 | 107 | locate(_column, _row); |
kayekss | 0:db0ffd858ca1 | 108 | break; |
kayekss | 0:db0ffd858ca1 | 109 | case '\n': // Line feed |
kayekss | 0:db0ffd858ca1 | 110 | if (_row == _rows - 1) { |
kayekss | 0:db0ffd858ca1 | 111 | shiftUp(); |
kayekss | 0:db0ffd858ca1 | 112 | } else { |
kayekss | 0:db0ffd858ca1 | 113 | _row++; |
kayekss | 0:db0ffd858ca1 | 114 | } |
kayekss | 0:db0ffd858ca1 | 115 | break; |
kayekss | 0:db0ffd858ca1 | 116 | case '\f': // Form feed |
kayekss | 0:db0ffd858ca1 | 117 | // Clear screen |
kayekss | 0:db0ffd858ca1 | 118 | cls(); |
kayekss | 0:db0ffd858ca1 | 119 | break; |
kayekss | 0:db0ffd858ca1 | 120 | default: |
kayekss | 0:db0ffd858ca1 | 121 | // Break line if the cursor reaches end |
kayekss | 0:db0ffd858ca1 | 122 | if (_column == _columns) { |
kayekss | 0:db0ffd858ca1 | 123 | if (_row == _rows - 1) { |
kayekss | 0:db0ffd858ca1 | 124 | shiftUp(); |
kayekss | 0:db0ffd858ca1 | 125 | } else { |
kayekss | 0:db0ffd858ca1 | 126 | _row++; |
kayekss | 0:db0ffd858ca1 | 127 | } |
kayekss | 0:db0ffd858ca1 | 128 | _column = 0; |
kayekss | 0:db0ffd858ca1 | 129 | } |
kayekss | 0:db0ffd858ca1 | 130 | locate(_column, _row); |
kayekss | 0:db0ffd858ca1 | 131 | data(c); |
kayekss | 0:db0ffd858ca1 | 132 | _lineBuffer[_row][_column] = c; |
kayekss | 0:db0ffd858ca1 | 133 | _column++; |
kayekss | 0:db0ffd858ca1 | 134 | break; |
kayekss | 0:db0ffd858ca1 | 135 | } |
kayekss | 0:db0ffd858ca1 | 136 | return 0; |
kayekss | 0:db0ffd858ca1 | 137 | } |
kayekss | 0:db0ffd858ca1 | 138 | |
kayekss | 0:db0ffd858ca1 | 139 | int TextLCD_ST7032I2C::_getc() { |
kayekss | 0:db0ffd858ca1 | 140 | return 0; |
kayekss | 0:db0ffd858ca1 | 141 | } |
kayekss | 0:db0ffd858ca1 | 142 | |
kayekss | 0:db0ffd858ca1 | 143 | void TextLCD_ST7032I2C::shiftUp() { |
kayekss | 0:db0ffd858ca1 | 144 | // Move line buffer content |
kayekss | 0:db0ffd858ca1 | 145 | for (uint8_t j = 0; j < _rows - 1; j++) { |
kayekss | 0:db0ffd858ca1 | 146 | memcpy(_lineBuffer[j], _lineBuffer[j + 1], _columns); |
kayekss | 0:db0ffd858ca1 | 147 | } |
kayekss | 0:db0ffd858ca1 | 148 | // Clear last line |
kayekss | 0:db0ffd858ca1 | 149 | memset(_lineBuffer[_rows - 1], ' ', _columns); |
kayekss | 0:db0ffd858ca1 | 150 | |
kayekss | 0:db0ffd858ca1 | 151 | // Redraw |
kayekss | 0:db0ffd858ca1 | 152 | for (uint8_t j = 0; j < _rows; j++) { |
kayekss | 0:db0ffd858ca1 | 153 | locate(0, j); |
kayekss | 0:db0ffd858ca1 | 154 | for (uint8_t i = 0; i < _columns; i++) { |
kayekss | 0:db0ffd858ca1 | 155 | data(_lineBuffer[j][i]); |
kayekss | 0:db0ffd858ca1 | 156 | } |
kayekss | 0:db0ffd858ca1 | 157 | } |
kayekss | 0:db0ffd858ca1 | 158 | } |
kayekss | 0:db0ffd858ca1 | 159 | |
kayekss | 0:db0ffd858ca1 | 160 | void TextLCD_ST7032I2C::is0() { |
kayekss | 0:db0ffd858ca1 | 161 | if (_rows == 1) { |
kayekss | 0:db0ffd858ca1 | 162 | command(FUNCTION_8B_1LINE_7DOT_IS0); |
kayekss | 0:db0ffd858ca1 | 163 | } else if (_rows == 2) { |
kayekss | 0:db0ffd858ca1 | 164 | command(FUNCTION_8B_2LINE_7DOT_IS0); |
kayekss | 0:db0ffd858ca1 | 165 | } |
kayekss | 0:db0ffd858ca1 | 166 | } |
kayekss | 0:db0ffd858ca1 | 167 | |
kayekss | 0:db0ffd858ca1 | 168 | void TextLCD_ST7032I2C::is1() { |
kayekss | 0:db0ffd858ca1 | 169 | if (_rows == 1) { |
kayekss | 0:db0ffd858ca1 | 170 | command(FUNCTION_8B_1LINE_7DOT_IS1); |
kayekss | 0:db0ffd858ca1 | 171 | } else if (_rows == 2) { |
kayekss | 0:db0ffd858ca1 | 172 | command(FUNCTION_8B_2LINE_7DOT_IS1); |
kayekss | 0:db0ffd858ca1 | 173 | } |
kayekss | 0:db0ffd858ca1 | 174 | } |
kayekss | 0:db0ffd858ca1 | 175 | |
kayekss | 0:db0ffd858ca1 | 176 | void TextLCD_ST7032I2C::command(uint8_t c) { |
kayekss | 0:db0ffd858ca1 | 177 | char cbuf[2]; |
kayekss | 0:db0ffd858ca1 | 178 | |
kayekss | 0:db0ffd858ca1 | 179 | cbuf[0] = 0x00; |
kayekss | 0:db0ffd858ca1 | 180 | cbuf[1] = c; |
kayekss | 0:db0ffd858ca1 | 181 | _i2c.write(_address, cbuf, 2); |
kayekss | 0:db0ffd858ca1 | 182 | } |
kayekss | 0:db0ffd858ca1 | 183 | |
kayekss | 0:db0ffd858ca1 | 184 | void TextLCD_ST7032I2C::data(uint8_t d) { |
kayekss | 0:db0ffd858ca1 | 185 | char dbuf[2]; |
kayekss | 0:db0ffd858ca1 | 186 | |
kayekss | 0:db0ffd858ca1 | 187 | dbuf[0] = 0x40; |
kayekss | 0:db0ffd858ca1 | 188 | dbuf[1] = d; |
kayekss | 0:db0ffd858ca1 | 189 | _i2c.write(_address, dbuf, 2); |
kayekss | 0:db0ffd858ca1 | 190 | } |