done
TM1637.cpp@0:dc8789a6e21f, 2016-01-30 (annotated)
- Committer:
- wim
- Date:
- Sat Jan 30 20:35:25 2016 +0000
- Revision:
- 0:dc8789a6e21f
- Child:
- 1:f63d87466f55
TM1637 Library, for TM1637 LEDcontroller
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wim | 0:dc8789a6e21f | 1 | /* mbed TM1637 Library, for TM1637 LED controller |
wim | 0:dc8789a6e21f | 2 | * Copyright (c) 2016, v01: WH, Initial version |
wim | 0:dc8789a6e21f | 3 | * |
wim | 0:dc8789a6e21f | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
wim | 0:dc8789a6e21f | 5 | * of this software and associated documentation files (the "Software"), to deal |
wim | 0:dc8789a6e21f | 6 | * in the Software without restriction, inclumosig without limitation the rights |
wim | 0:dc8789a6e21f | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
wim | 0:dc8789a6e21f | 8 | * copies of the Software, and to permit persons to whom the Software is |
wim | 0:dc8789a6e21f | 9 | * furnished to do so, subject to the following conditions: |
wim | 0:dc8789a6e21f | 10 | * |
wim | 0:dc8789a6e21f | 11 | * The above copyright notice and this permission notice shall be included in |
wim | 0:dc8789a6e21f | 12 | * all copies or substantial portions of the Software. |
wim | 0:dc8789a6e21f | 13 | * |
wim | 0:dc8789a6e21f | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
wim | 0:dc8789a6e21f | 15 | * IMPLIED, INCLUmosiG BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
wim | 0:dc8789a6e21f | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
wim | 0:dc8789a6e21f | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
wim | 0:dc8789a6e21f | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
wim | 0:dc8789a6e21f | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
wim | 0:dc8789a6e21f | 20 | * THE SOFTWARE. |
wim | 0:dc8789a6e21f | 21 | */ |
wim | 0:dc8789a6e21f | 22 | #include "mbed.h" |
wim | 0:dc8789a6e21f | 23 | #include "TM1637.h" |
wim | 0:dc8789a6e21f | 24 | |
wim | 0:dc8789a6e21f | 25 | /** Constructor for class for driving TM1637 LED controller with Serial bus interface device. |
wim | 0:dc8789a6e21f | 26 | * @brief Supports 6 digits @ 8 segments. |
wim | 0:dc8789a6e21f | 27 | * |
wim | 0:dc8789a6e21f | 28 | * @param PinName mosi Serial bus MOSI pin |
wim | 0:dc8789a6e21f | 29 | * @param PinName miso Serial bus MISO pin |
wim | 0:dc8789a6e21f | 30 | * @param PinName sclk Serial bus SCLK pin |
wim | 0:dc8789a6e21f | 31 | */ |
wim | 0:dc8789a6e21f | 32 | TM1637::TM1637(PinName mosi, PinName miso, PinName sclk) : _mosi(mosi), _miso(miso), _sclk(sclk) { |
wim | 0:dc8789a6e21f | 33 | |
wim | 0:dc8789a6e21f | 34 | _init(); |
wim | 0:dc8789a6e21f | 35 | } |
wim | 0:dc8789a6e21f | 36 | |
wim | 0:dc8789a6e21f | 37 | /** Init the Serial interface and the controller |
wim | 0:dc8789a6e21f | 38 | * @param none |
wim | 0:dc8789a6e21f | 39 | * @return none |
wim | 0:dc8789a6e21f | 40 | */ |
wim | 0:dc8789a6e21f | 41 | void TM1637::_init(){ |
wim | 0:dc8789a6e21f | 42 | |
wim | 0:dc8789a6e21f | 43 | //TM1637 uses a Serial bus that looks like I2C, but really is not. |
wim | 0:dc8789a6e21f | 44 | //It has Start and Stop conditions like I2C and an Ack pulse, but instead of slaveaddresses and a RW bit it just transmits commands and data. |
wim | 0:dc8789a6e21f | 45 | //init Serial bus |
wim | 0:dc8789a6e21f | 46 | _mosi=1; |
wim | 0:dc8789a6e21f | 47 | _sclk=1; |
wim | 0:dc8789a6e21f | 48 | |
wim | 0:dc8789a6e21f | 49 | //init controller |
wim | 0:dc8789a6e21f | 50 | _display = TM1637_DSP_ON; |
wim | 0:dc8789a6e21f | 51 | _bright = TM1637_BRT_DEF; |
wim | 0:dc8789a6e21f | 52 | _writeCmd(TM1637_DSP_CTRL_CMD, _display | _bright ); // Display control cmd, display on/off, brightness |
wim | 0:dc8789a6e21f | 53 | |
wim | 0:dc8789a6e21f | 54 | _writeCmd(TM1637_DATA_SET_CMD, TM1637_DATA_WR | TM1637_ADDR_INC | TM1637_MODE_NORM); // Data set cmd, normal mode, auto incr, write data |
wim | 0:dc8789a6e21f | 55 | } |
wim | 0:dc8789a6e21f | 56 | |
wim | 0:dc8789a6e21f | 57 | |
wim | 0:dc8789a6e21f | 58 | /** Clear the screen and locate to 0 |
wim | 0:dc8789a6e21f | 59 | */ |
wim | 0:dc8789a6e21f | 60 | void TM1637::cls() { |
wim | 0:dc8789a6e21f | 61 | |
wim | 0:dc8789a6e21f | 62 | _start(); |
wim | 0:dc8789a6e21f | 63 | |
wim | 0:dc8789a6e21f | 64 | _write(TM1637_ADDR_SET_CMD | 0x00); // Address set cmd, 0 |
wim | 0:dc8789a6e21f | 65 | for (int cnt=0; cnt<TM1637_DISPLAY_MEM; cnt++) { |
wim | 0:dc8789a6e21f | 66 | _write(0x00); // data |
wim | 0:dc8789a6e21f | 67 | } |
wim | 0:dc8789a6e21f | 68 | |
wim | 0:dc8789a6e21f | 69 | _stop(); |
wim | 0:dc8789a6e21f | 70 | } |
wim | 0:dc8789a6e21f | 71 | |
wim | 0:dc8789a6e21f | 72 | /** Set Brightness |
wim | 0:dc8789a6e21f | 73 | * |
wim | 0:dc8789a6e21f | 74 | * @param char brightness (3 significant bits, valid range 0..7 (1/16 .. 14/14 dutycycle) |
wim | 0:dc8789a6e21f | 75 | * @return none |
wim | 0:dc8789a6e21f | 76 | */ |
wim | 0:dc8789a6e21f | 77 | void TM1637::setBrightness(char brightness){ |
wim | 0:dc8789a6e21f | 78 | |
wim | 0:dc8789a6e21f | 79 | _bright = brightness & TM1637_BRT_MSK; // mask invalid bits |
wim | 0:dc8789a6e21f | 80 | |
wim | 0:dc8789a6e21f | 81 | _writeCmd(TM1637_DSP_CTRL_CMD, _display | _bright ); // Display control cmd, display on/off, brightness |
wim | 0:dc8789a6e21f | 82 | } |
wim | 0:dc8789a6e21f | 83 | |
wim | 0:dc8789a6e21f | 84 | /** Set the Display mode On/off |
wim | 0:dc8789a6e21f | 85 | * |
wim | 0:dc8789a6e21f | 86 | * @param bool display mode |
wim | 0:dc8789a6e21f | 87 | */ |
wim | 0:dc8789a6e21f | 88 | void TM1637::setDisplay(bool on) { |
wim | 0:dc8789a6e21f | 89 | |
wim | 0:dc8789a6e21f | 90 | if (on) { |
wim | 0:dc8789a6e21f | 91 | _display = TM1637_DSP_ON; |
wim | 0:dc8789a6e21f | 92 | } |
wim | 0:dc8789a6e21f | 93 | else { |
wim | 0:dc8789a6e21f | 94 | _display = TM1637_DSP_OFF; |
wim | 0:dc8789a6e21f | 95 | } |
wim | 0:dc8789a6e21f | 96 | |
wim | 0:dc8789a6e21f | 97 | _writeCmd(TM1637_DSP_CTRL_CMD, _display | _bright ); // Display control cmd, display on/off, brightness |
wim | 0:dc8789a6e21f | 98 | } |
wim | 0:dc8789a6e21f | 99 | |
wim | 0:dc8789a6e21f | 100 | /** Write databyte to TM1637 |
wim | 0:dc8789a6e21f | 101 | * @param int address display memory location to write byte |
wim | 0:dc8789a6e21f | 102 | * @param char data byte written at given address |
wim | 0:dc8789a6e21f | 103 | * @return none |
wim | 0:dc8789a6e21f | 104 | */ |
wim | 0:dc8789a6e21f | 105 | void TM1637::writeData(char data, int address) { |
wim | 0:dc8789a6e21f | 106 | |
wim | 0:dc8789a6e21f | 107 | _start(); |
wim | 0:dc8789a6e21f | 108 | |
wim | 0:dc8789a6e21f | 109 | _write(TM1637_ADDR_SET_CMD | (address & TM1637_ADDR_MSK)); // Set Address cmd |
wim | 0:dc8789a6e21f | 110 | _write(data); // data |
wim | 0:dc8789a6e21f | 111 | |
wim | 0:dc8789a6e21f | 112 | _stop(); |
wim | 0:dc8789a6e21f | 113 | } |
wim | 0:dc8789a6e21f | 114 | |
wim | 0:dc8789a6e21f | 115 | /** Write Display datablock to TM1637 |
wim | 0:dc8789a6e21f | 116 | * @param DisplayData_t data Array of TM1637_DISPLAY_MEM (=16) bytes for displaydata |
wim | 0:dc8789a6e21f | 117 | * @param length number bytes to write (valid range 0..(TM1637_MAX_NR_GRIDS * TM1637_BYTES_PER_GRID) (=16), when starting at address 0) |
wim | 0:dc8789a6e21f | 118 | * @param int address display memory location to write bytes (default = 0) |
wim | 0:dc8789a6e21f | 119 | * @return none |
wim | 0:dc8789a6e21f | 120 | */ |
wim | 0:dc8789a6e21f | 121 | void TM1637::writeData(DisplayData_t data, int length, int address) { |
wim | 0:dc8789a6e21f | 122 | |
wim | 0:dc8789a6e21f | 123 | _start(); |
wim | 0:dc8789a6e21f | 124 | |
wim | 0:dc8789a6e21f | 125 | // sanity check |
wim | 0:dc8789a6e21f | 126 | address &= TM1637_ADDR_MSK; |
wim | 0:dc8789a6e21f | 127 | if (length < 0) {length = 0;} |
wim | 0:dc8789a6e21f | 128 | if ((length + address) > TM1637_DISPLAY_MEM) {length = (TM1637_DISPLAY_MEM - address);} |
wim | 0:dc8789a6e21f | 129 | |
wim | 0:dc8789a6e21f | 130 | // _write(TM1637_ADDR_SET_CMD | 0x00); // Set Address at 0 |
wim | 0:dc8789a6e21f | 131 | _write(TM1637_ADDR_SET_CMD | address); // Set Address |
wim | 0:dc8789a6e21f | 132 | |
wim | 0:dc8789a6e21f | 133 | for (int idx=0; idx<length; idx++) { |
wim | 0:dc8789a6e21f | 134 | // _write(data[idx]); // data |
wim | 0:dc8789a6e21f | 135 | _write(data[address + idx]); // data |
wim | 0:dc8789a6e21f | 136 | } |
wim | 0:dc8789a6e21f | 137 | |
wim | 0:dc8789a6e21f | 138 | _stop(); |
wim | 0:dc8789a6e21f | 139 | } |
wim | 0:dc8789a6e21f | 140 | |
wim | 0:dc8789a6e21f | 141 | /** Read keydata block from TM1637 |
wim | 0:dc8789a6e21f | 142 | * @param *keydata Ptr to bytes for keydata |
wim | 0:dc8789a6e21f | 143 | * @return bool keypress True when at least one key was pressed |
wim | 0:dc8789a6e21f | 144 | * |
wim | 0:dc8789a6e21f | 145 | */ |
wim | 0:dc8789a6e21f | 146 | bool TM1637::getKeys(KeyData_t *keydata) { |
wim | 0:dc8789a6e21f | 147 | |
wim | 0:dc8789a6e21f | 148 | _start(); |
wim | 0:dc8789a6e21f | 149 | |
wim | 0:dc8789a6e21f | 150 | // Enable Key Read mode |
wim | 0:dc8789a6e21f | 151 | _write(TM1637_DATA_SET_CMD | TM1637_KEY_RD | TM1637_ADDR_INC | TM1637_MODE_NORM); // Data set cmd, normal mode, auto incr, read data |
wim | 0:dc8789a6e21f | 152 | |
wim | 0:dc8789a6e21f | 153 | // Read keys |
wim | 0:dc8789a6e21f | 154 | *keydata = _read(0xFF); |
wim | 0:dc8789a6e21f | 155 | |
wim | 0:dc8789a6e21f | 156 | _stop(); |
wim | 0:dc8789a6e21f | 157 | |
wim | 0:dc8789a6e21f | 158 | // Restore Data Write mode |
wim | 0:dc8789a6e21f | 159 | _writeCmd(TM1637_DATA_SET_CMD, TM1637_DATA_WR | TM1637_ADDR_INC | TM1637_MODE_NORM); // Data set cmd, normal mode, auto incr, write data |
wim | 0:dc8789a6e21f | 160 | |
wim | 0:dc8789a6e21f | 161 | return (*keydata != TM1637_SW_NONE); |
wim | 0:dc8789a6e21f | 162 | } |
wim | 0:dc8789a6e21f | 163 | |
wim | 0:dc8789a6e21f | 164 | |
wim | 0:dc8789a6e21f | 165 | /** Generate Start condition for TM1637 |
wim | 0:dc8789a6e21f | 166 | * @param none |
wim | 0:dc8789a6e21f | 167 | * @return none |
wim | 0:dc8789a6e21f | 168 | */ |
wim | 0:dc8789a6e21f | 169 | void TM1637::_start() { |
wim | 0:dc8789a6e21f | 170 | |
wim | 0:dc8789a6e21f | 171 | _mosi=0; |
wim | 0:dc8789a6e21f | 172 | wait_us(1); |
wim | 0:dc8789a6e21f | 173 | _sclk=0; |
wim | 0:dc8789a6e21f | 174 | wait_us(1); |
wim | 0:dc8789a6e21f | 175 | } |
wim | 0:dc8789a6e21f | 176 | |
wim | 0:dc8789a6e21f | 177 | /** Generate Stop condition for TM1637 |
wim | 0:dc8789a6e21f | 178 | * @param none |
wim | 0:dc8789a6e21f | 179 | * @return none |
wim | 0:dc8789a6e21f | 180 | */ |
wim | 0:dc8789a6e21f | 181 | void TM1637::_stop() { |
wim | 0:dc8789a6e21f | 182 | |
wim | 0:dc8789a6e21f | 183 | _mosi=0; |
wim | 0:dc8789a6e21f | 184 | wait_us(1); |
wim | 0:dc8789a6e21f | 185 | _sclk=1; |
wim | 0:dc8789a6e21f | 186 | wait_us(1); |
wim | 0:dc8789a6e21f | 187 | _mosi=1; |
wim | 0:dc8789a6e21f | 188 | wait_us(1); |
wim | 0:dc8789a6e21f | 189 | } |
wim | 0:dc8789a6e21f | 190 | |
wim | 0:dc8789a6e21f | 191 | /** Send byte to TM1637 |
wim | 0:dc8789a6e21f | 192 | * @param int data |
wim | 0:dc8789a6e21f | 193 | * @return none |
wim | 0:dc8789a6e21f | 194 | */ |
wim | 0:dc8789a6e21f | 195 | void TM1637::_write(int data) { |
wim | 0:dc8789a6e21f | 196 | |
wim | 0:dc8789a6e21f | 197 | for (int bit=0; bit<8; bit++) { |
wim | 0:dc8789a6e21f | 198 | //The TM1637 expects LSB first |
wim | 0:dc8789a6e21f | 199 | if (((data >> bit) & 0x01) == 0x01) { |
wim | 0:dc8789a6e21f | 200 | _mosi=1; |
wim | 0:dc8789a6e21f | 201 | } |
wim | 0:dc8789a6e21f | 202 | else { |
wim | 0:dc8789a6e21f | 203 | _mosi=0; |
wim | 0:dc8789a6e21f | 204 | } |
wim | 0:dc8789a6e21f | 205 | wait_us(1); |
wim | 0:dc8789a6e21f | 206 | _sclk=1; |
wim | 0:dc8789a6e21f | 207 | wait_us(1); |
wim | 0:dc8789a6e21f | 208 | _sclk=0; |
wim | 0:dc8789a6e21f | 209 | wait_us(1); |
wim | 0:dc8789a6e21f | 210 | } |
wim | 0:dc8789a6e21f | 211 | |
wim | 0:dc8789a6e21f | 212 | // dummy Ack |
wim | 0:dc8789a6e21f | 213 | _sclk=1; |
wim | 0:dc8789a6e21f | 214 | wait_us(1); |
wim | 0:dc8789a6e21f | 215 | _sclk=0; |
wim | 0:dc8789a6e21f | 216 | wait_us(1); |
wim | 0:dc8789a6e21f | 217 | } |
wim | 0:dc8789a6e21f | 218 | |
wim | 0:dc8789a6e21f | 219 | /** Read byte from TM1637 |
wim | 0:dc8789a6e21f | 220 | * @param int senddata |
wim | 0:dc8789a6e21f | 221 | * @return read byte |
wim | 0:dc8789a6e21f | 222 | */ |
wim | 0:dc8789a6e21f | 223 | char TM1637::_read(int data) { |
wim | 0:dc8789a6e21f | 224 | char keycode = 0; |
wim | 0:dc8789a6e21f | 225 | |
wim | 0:dc8789a6e21f | 226 | for (int bit=0; bit<8; bit++) { |
wim | 0:dc8789a6e21f | 227 | |
wim | 0:dc8789a6e21f | 228 | // Prepare to read next bit, LSB first |
wim | 0:dc8789a6e21f | 229 | keycode = keycode << 1; |
wim | 0:dc8789a6e21f | 230 | |
wim | 0:dc8789a6e21f | 231 | //The TM1637 expects LSB first |
wim | 0:dc8789a6e21f | 232 | if (((data >> bit) & 0x01) == 0x01) { |
wim | 0:dc8789a6e21f | 233 | _mosi=1; |
wim | 0:dc8789a6e21f | 234 | } |
wim | 0:dc8789a6e21f | 235 | else { |
wim | 0:dc8789a6e21f | 236 | _mosi=0; |
wim | 0:dc8789a6e21f | 237 | } |
wim | 0:dc8789a6e21f | 238 | wait_us(1); |
wim | 0:dc8789a6e21f | 239 | _sclk=1; |
wim | 0:dc8789a6e21f | 240 | wait_us(1); |
wim | 0:dc8789a6e21f | 241 | |
wim | 0:dc8789a6e21f | 242 | // Read next bit, LSB first |
wim | 0:dc8789a6e21f | 243 | if (_miso) { keycode |= 0x01; } |
wim | 0:dc8789a6e21f | 244 | |
wim | 0:dc8789a6e21f | 245 | _sclk=0; |
wim | 0:dc8789a6e21f | 246 | wait_us(1); |
wim | 0:dc8789a6e21f | 247 | } |
wim | 0:dc8789a6e21f | 248 | |
wim | 0:dc8789a6e21f | 249 | // dummy Ack |
wim | 0:dc8789a6e21f | 250 | _sclk=1; |
wim | 0:dc8789a6e21f | 251 | wait_us(1); |
wim | 0:dc8789a6e21f | 252 | _sclk=0; |
wim | 0:dc8789a6e21f | 253 | wait_us(1); |
wim | 0:dc8789a6e21f | 254 | |
wim | 0:dc8789a6e21f | 255 | return keycode; |
wim | 0:dc8789a6e21f | 256 | } |
wim | 0:dc8789a6e21f | 257 | |
wim | 0:dc8789a6e21f | 258 | |
wim | 0:dc8789a6e21f | 259 | /** Write command and parameter to TM1637 |
wim | 0:dc8789a6e21f | 260 | * @param int cmd Command byte |
wim | 0:dc8789a6e21f | 261 | * &Param int data Parameters for command |
wim | 0:dc8789a6e21f | 262 | * @return none |
wim | 0:dc8789a6e21f | 263 | */ |
wim | 0:dc8789a6e21f | 264 | void TM1637::_writeCmd(int cmd, int data){ |
wim | 0:dc8789a6e21f | 265 | |
wim | 0:dc8789a6e21f | 266 | _start(); |
wim | 0:dc8789a6e21f | 267 | |
wim | 0:dc8789a6e21f | 268 | _write((cmd & TM1637_CMD_MSK) | (data & ~TM1637_CMD_MSK)); |
wim | 0:dc8789a6e21f | 269 | |
wim | 0:dc8789a6e21f | 270 | _stop(); |
wim | 0:dc8789a6e21f | 271 | } |
wim | 0:dc8789a6e21f | 272 | |
wim | 0:dc8789a6e21f | 273 | |
wim | 0:dc8789a6e21f | 274 | #if (CATALEX_TEST == 1) |
wim | 0:dc8789a6e21f | 275 | // Derived class for TM1637 used in LED&KEY display unit |
wim | 0:dc8789a6e21f | 276 | // |
wim | 0:dc8789a6e21f | 277 | |
wim | 0:dc8789a6e21f | 278 | /** Constructor for class for driving TM1637 LED controller as used in CATALEX |
wim | 0:dc8789a6e21f | 279 | * |
wim | 0:dc8789a6e21f | 280 | * @brief Supports 8 Digits of 7 Segments + DP. |
wim | 0:dc8789a6e21f | 281 | * |
wim | 0:dc8789a6e21f | 282 | * @param PinName mosi Serial bus MOSI pin |
wim | 0:dc8789a6e21f | 283 | * @param PinName miso Serial bus MISO pin |
wim | 0:dc8789a6e21f | 284 | * @param PinName sclk Serial bus SCLK pin |
wim | 0:dc8789a6e21f | 285 | */ |
wim | 0:dc8789a6e21f | 286 | TM1637_CATALEX::TM1637_CATALEX(PinName mosi, PinName miso, PinName sclk) : TM1637(mosi, miso, sclk) { |
wim | 0:dc8789a6e21f | 287 | _column = 0; |
wim | 0:dc8789a6e21f | 288 | _columns = CATALEX_NR_DIGITS; |
wim | 0:dc8789a6e21f | 289 | } |
wim | 0:dc8789a6e21f | 290 | |
wim | 0:dc8789a6e21f | 291 | #if(0) |
wim | 0:dc8789a6e21f | 292 | #if DOXYGEN_ONLY |
wim | 0:dc8789a6e21f | 293 | /** Write a character to the Display |
wim | 0:dc8789a6e21f | 294 | * |
wim | 0:dc8789a6e21f | 295 | * @param c The character to write to the display |
wim | 0:dc8789a6e21f | 296 | */ |
wim | 0:dc8789a6e21f | 297 | int putc(int c); |
wim | 0:dc8789a6e21f | 298 | |
wim | 0:dc8789a6e21f | 299 | /** Write a formatted string to the Display |
wim | 0:dc8789a6e21f | 300 | * |
wim | 0:dc8789a6e21f | 301 | * @param format A printf-style format string, followed by the |
wim | 0:dc8789a6e21f | 302 | * variables to use in formatting the string. |
wim | 0:dc8789a6e21f | 303 | */ |
wim | 0:dc8789a6e21f | 304 | int printf(const char* format, ...); |
wim | 0:dc8789a6e21f | 305 | #endif |
wim | 0:dc8789a6e21f | 306 | #endif |
wim | 0:dc8789a6e21f | 307 | |
wim | 0:dc8789a6e21f | 308 | /** Locate cursor to a screen column |
wim | 0:dc8789a6e21f | 309 | * |
wim | 0:dc8789a6e21f | 310 | * @param column The horizontal position from the left, indexed from 0 |
wim | 0:dc8789a6e21f | 311 | */ |
wim | 0:dc8789a6e21f | 312 | void TM1637_CATALEX::locate(int column) { |
wim | 0:dc8789a6e21f | 313 | //sanity check |
wim | 0:dc8789a6e21f | 314 | if (column < 0) {column = 0;} |
wim | 0:dc8789a6e21f | 315 | if (column > (_columns - 1)) {column = _columns - 1;} |
wim | 0:dc8789a6e21f | 316 | |
wim | 0:dc8789a6e21f | 317 | _column = column; |
wim | 0:dc8789a6e21f | 318 | } |
wim | 0:dc8789a6e21f | 319 | |
wim | 0:dc8789a6e21f | 320 | |
wim | 0:dc8789a6e21f | 321 | /** Number of screen columns |
wim | 0:dc8789a6e21f | 322 | * |
wim | 0:dc8789a6e21f | 323 | * @param none |
wim | 0:dc8789a6e21f | 324 | * @return columns |
wim | 0:dc8789a6e21f | 325 | */ |
wim | 0:dc8789a6e21f | 326 | int TM1637_CATALEX::columns() { |
wim | 0:dc8789a6e21f | 327 | return _columns; |
wim | 0:dc8789a6e21f | 328 | } |
wim | 0:dc8789a6e21f | 329 | |
wim | 0:dc8789a6e21f | 330 | |
wim | 0:dc8789a6e21f | 331 | /** Clear the screen and locate to 0 |
wim | 0:dc8789a6e21f | 332 | * @param bool clrAll Clear Icons also (default = false) |
wim | 0:dc8789a6e21f | 333 | */ |
wim | 0:dc8789a6e21f | 334 | void TM1637_CATALEX::cls(bool clrAll) { |
wim | 0:dc8789a6e21f | 335 | |
wim | 0:dc8789a6e21f | 336 | if (clrAll) { |
wim | 0:dc8789a6e21f | 337 | //clear local buffer (inclumosig Icons) |
wim | 0:dc8789a6e21f | 338 | for (int idx=0; idx < CATALEX_NR_GRIDS; idx++) { |
wim | 0:dc8789a6e21f | 339 | _displaybuffer[idx] = 0x00; |
wim | 0:dc8789a6e21f | 340 | } |
wim | 0:dc8789a6e21f | 341 | } |
wim | 0:dc8789a6e21f | 342 | else { |
wim | 0:dc8789a6e21f | 343 | //clear local buffer (preserving Icons) |
wim | 0:dc8789a6e21f | 344 | for (int idx=0; idx < CATALEX_NR_GRIDS; idx++) { |
wim | 0:dc8789a6e21f | 345 | _displaybuffer[idx] = _displaybuffer[idx] & MASK_ICON_GRID[idx]; |
wim | 0:dc8789a6e21f | 346 | } |
wim | 0:dc8789a6e21f | 347 | } |
wim | 0:dc8789a6e21f | 348 | |
wim | 0:dc8789a6e21f | 349 | writeData(_displaybuffer, (CATALEX_NR_GRIDS * TM1637_BYTES_PER_GRID)); |
wim | 0:dc8789a6e21f | 350 | |
wim | 0:dc8789a6e21f | 351 | _column = 0; |
wim | 0:dc8789a6e21f | 352 | } |
wim | 0:dc8789a6e21f | 353 | |
wim | 0:dc8789a6e21f | 354 | /** Set Icon |
wim | 0:dc8789a6e21f | 355 | * |
wim | 0:dc8789a6e21f | 356 | * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 16 LSBs |
wim | 0:dc8789a6e21f | 357 | * @return none |
wim | 0:dc8789a6e21f | 358 | */ |
wim | 0:dc8789a6e21f | 359 | void TM1637_CATALEX::setIcon(Icon icon) { |
wim | 0:dc8789a6e21f | 360 | int addr, icn; |
wim | 0:dc8789a6e21f | 361 | |
wim | 0:dc8789a6e21f | 362 | icn = icon & 0xFFFF; |
wim | 0:dc8789a6e21f | 363 | addr = (icon >> 24) & 0xFF; |
wim | 0:dc8789a6e21f | 364 | addr = (addr - 1); |
wim | 0:dc8789a6e21f | 365 | |
wim | 0:dc8789a6e21f | 366 | //Save char...and set bits for icon to write |
wim | 0:dc8789a6e21f | 367 | _displaybuffer[addr] = _displaybuffer[addr] | LO(icn); |
wim | 0:dc8789a6e21f | 368 | // writeData(_displaybuffer, (CATALEX_NR_GRIDS * TM1637_BYTES_PER_GRID)); |
wim | 0:dc8789a6e21f | 369 | writeData(_displaybuffer, TM1637_BYTES_PER_GRID, addr); |
wim | 0:dc8789a6e21f | 370 | } |
wim | 0:dc8789a6e21f | 371 | |
wim | 0:dc8789a6e21f | 372 | /** Clr Icon |
wim | 0:dc8789a6e21f | 373 | * |
wim | 0:dc8789a6e21f | 374 | * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 16 LSBs |
wim | 0:dc8789a6e21f | 375 | * @return none |
wim | 0:dc8789a6e21f | 376 | */ |
wim | 0:dc8789a6e21f | 377 | void TM1637_CATALEX::clrIcon(Icon icon) { |
wim | 0:dc8789a6e21f | 378 | int addr, icn; |
wim | 0:dc8789a6e21f | 379 | |
wim | 0:dc8789a6e21f | 380 | icn = icon & 0xFFFF; |
wim | 0:dc8789a6e21f | 381 | addr = (icon >> 24) & 0xFF; |
wim | 0:dc8789a6e21f | 382 | addr = (addr - 1); |
wim | 0:dc8789a6e21f | 383 | |
wim | 0:dc8789a6e21f | 384 | //Save char...and clr bits for icon to write |
wim | 0:dc8789a6e21f | 385 | _displaybuffer[addr] = _displaybuffer[addr] & ~LO(icn); |
wim | 0:dc8789a6e21f | 386 | // writeData(_displaybuffer, (CATALEX_NR_GRIDS * TM1637_BYTES_PER_GRID)); |
wim | 0:dc8789a6e21f | 387 | writeData(_displaybuffer, TM1637_BYTES_PER_GRID, addr); |
wim | 0:dc8789a6e21f | 388 | } |
wim | 0:dc8789a6e21f | 389 | |
wim | 0:dc8789a6e21f | 390 | |
wim | 0:dc8789a6e21f | 391 | /** Set User Defined Characters (UDC) |
wim | 0:dc8789a6e21f | 392 | * |
wim | 0:dc8789a6e21f | 393 | * @param unsigned char udc_idx The Index of the UDC (0..7) |
wim | 0:dc8789a6e21f | 394 | * @param int udc_data The bitpattern for the UDC (8 bits) |
wim | 0:dc8789a6e21f | 395 | */ |
wim | 0:dc8789a6e21f | 396 | void TM1637_CATALEX::setUDC(unsigned char udc_idx, int udc_data) { |
wim | 0:dc8789a6e21f | 397 | |
wim | 0:dc8789a6e21f | 398 | //Sanity check |
wim | 0:dc8789a6e21f | 399 | if (udc_idx > (CATALEX_NR_UDC-1)) { |
wim | 0:dc8789a6e21f | 400 | return; |
wim | 0:dc8789a6e21f | 401 | } |
wim | 0:dc8789a6e21f | 402 | // Mask out Icon bits? |
wim | 0:dc8789a6e21f | 403 | |
wim | 0:dc8789a6e21f | 404 | _UDC_7S[udc_idx] = LO(udc_data); |
wim | 0:dc8789a6e21f | 405 | } |
wim | 0:dc8789a6e21f | 406 | |
wim | 0:dc8789a6e21f | 407 | |
wim | 0:dc8789a6e21f | 408 | /** Write a single character (Stream implementation) |
wim | 0:dc8789a6e21f | 409 | */ |
wim | 0:dc8789a6e21f | 410 | int TM1637_CATALEX::_putc(int value) { |
wim | 0:dc8789a6e21f | 411 | int addr; |
wim | 0:dc8789a6e21f | 412 | bool validChar = false; |
wim | 0:dc8789a6e21f | 413 | char pattern = 0x00; |
wim | 0:dc8789a6e21f | 414 | |
wim | 0:dc8789a6e21f | 415 | if ((value == '\n') || (value == '\r')) { |
wim | 0:dc8789a6e21f | 416 | //No character to write |
wim | 0:dc8789a6e21f | 417 | validChar = false; |
wim | 0:dc8789a6e21f | 418 | |
wim | 0:dc8789a6e21f | 419 | //Update Cursor |
wim | 0:dc8789a6e21f | 420 | _column = 0; |
wim | 0:dc8789a6e21f | 421 | } |
wim | 0:dc8789a6e21f | 422 | else if ((value == '.') || (value == ',')) { |
wim | 0:dc8789a6e21f | 423 | //No character to write |
wim | 0:dc8789a6e21f | 424 | validChar = false; |
wim | 0:dc8789a6e21f | 425 | pattern = S7_DP; // placeholder for all DPs |
wim | 0:dc8789a6e21f | 426 | |
wim | 0:dc8789a6e21f | 427 | // Check to see that DP can be shown for current column |
wim | 0:dc8789a6e21f | 428 | if (_column > 0) { |
wim | 0:dc8789a6e21f | 429 | //Translate between _column and displaybuffer entries |
wim | 0:dc8789a6e21f | 430 | //Add DP to bitpattern of digit left of current column. |
wim | 0:dc8789a6e21f | 431 | addr = (_column - 1); |
wim | 0:dc8789a6e21f | 432 | |
wim | 0:dc8789a6e21f | 433 | //Save icons...and set bits for decimal point to write |
wim | 0:dc8789a6e21f | 434 | _displaybuffer[addr] = _displaybuffer[addr] | pattern; |
wim | 0:dc8789a6e21f | 435 | // writeData(_displaybuffer, (CATALEX_NR_GRIDS * TM1637_BYTES_PER_GRID)); |
wim | 0:dc8789a6e21f | 436 | writeData(_displaybuffer, TM1637_BYTES_PER_GRID, addr); |
wim | 0:dc8789a6e21f | 437 | |
wim | 0:dc8789a6e21f | 438 | //No Cursor Update |
wim | 0:dc8789a6e21f | 439 | } |
wim | 0:dc8789a6e21f | 440 | } |
wim | 0:dc8789a6e21f | 441 | else if ((value >= 0) && (value < CATALEX_NR_UDC)) { |
wim | 0:dc8789a6e21f | 442 | //Character to write |
wim | 0:dc8789a6e21f | 443 | validChar = true; |
wim | 0:dc8789a6e21f | 444 | pattern = _UDC_7S[value]; |
wim | 0:dc8789a6e21f | 445 | } |
wim | 0:dc8789a6e21f | 446 | |
wim | 0:dc8789a6e21f | 447 | #if (SHOW_ASCII == 1) |
wim | 0:dc8789a6e21f | 448 | //display all ASCII characters |
wim | 0:dc8789a6e21f | 449 | else if ((value >= FONT_7S_START) && (value <= FONT_7S_END)) { |
wim | 0:dc8789a6e21f | 450 | //Character to write |
wim | 0:dc8789a6e21f | 451 | validChar = true; |
wim | 0:dc8789a6e21f | 452 | pattern = FONT_7S[value - FONT_7S_START]; |
wim | 0:dc8789a6e21f | 453 | } // else |
wim | 0:dc8789a6e21f | 454 | #else |
wim | 0:dc8789a6e21f | 455 | //display only digits and hex characters |
wim | 0:dc8789a6e21f | 456 | else if (value == '-') { |
wim | 0:dc8789a6e21f | 457 | //Character to write |
wim | 0:dc8789a6e21f | 458 | validChar = true; |
wim | 0:dc8789a6e21f | 459 | pattern = C7_MIN; |
wim | 0:dc8789a6e21f | 460 | } |
wim | 0:dc8789a6e21f | 461 | else if ((value >= (int)'0') && (value <= (int) '9')) { |
wim | 0:dc8789a6e21f | 462 | //Character to write |
wim | 0:dc8789a6e21f | 463 | validChar = true; |
wim | 0:dc8789a6e21f | 464 | pattern = FONT_7S[value - (int) '0']; |
wim | 0:dc8789a6e21f | 465 | } |
wim | 0:dc8789a6e21f | 466 | else if ((value >= (int) 'A') && (value <= (int) 'F')) { |
wim | 0:dc8789a6e21f | 467 | //Character to write |
wim | 0:dc8789a6e21f | 468 | validChar = true; |
wim | 0:dc8789a6e21f | 469 | pattern = FONT_7S[10 + value - (int) 'A']; |
wim | 0:dc8789a6e21f | 470 | } |
wim | 0:dc8789a6e21f | 471 | else if ((value >= (int) 'a') && (value <= (int) 'f')) { |
wim | 0:dc8789a6e21f | 472 | //Character to write |
wim | 0:dc8789a6e21f | 473 | validChar = true; |
wim | 0:dc8789a6e21f | 474 | pattern = FONT_7S[10 + value - (int) 'a']; |
wim | 0:dc8789a6e21f | 475 | } //else |
wim | 0:dc8789a6e21f | 476 | #endif |
wim | 0:dc8789a6e21f | 477 | |
wim | 0:dc8789a6e21f | 478 | if (validChar) { |
wim | 0:dc8789a6e21f | 479 | //Character to write |
wim | 0:dc8789a6e21f | 480 | |
wim | 0:dc8789a6e21f | 481 | //Translate between _column and displaybuffer entries |
wim | 0:dc8789a6e21f | 482 | addr = _column; |
wim | 0:dc8789a6e21f | 483 | |
wim | 0:dc8789a6e21f | 484 | //Save icons...and set bits for character to write |
wim | 0:dc8789a6e21f | 485 | _displaybuffer[addr] = (_displaybuffer[addr] & MASK_ICON_GRID[_column]) | pattern; |
wim | 0:dc8789a6e21f | 486 | |
wim | 0:dc8789a6e21f | 487 | // writeData(_displaybuffer, (CATALEX_NR_GRIDS * TM1637_BYTES_PER_GRID)); |
wim | 0:dc8789a6e21f | 488 | writeData(_displaybuffer, TM1637_BYTES_PER_GRID, addr); |
wim | 0:dc8789a6e21f | 489 | |
wim | 0:dc8789a6e21f | 490 | //Update Cursor |
wim | 0:dc8789a6e21f | 491 | _column++; |
wim | 0:dc8789a6e21f | 492 | if (_column > (CATALEX_NR_DIGITS - 1)) { |
wim | 0:dc8789a6e21f | 493 | _column = 0; |
wim | 0:dc8789a6e21f | 494 | } |
wim | 0:dc8789a6e21f | 495 | |
wim | 0:dc8789a6e21f | 496 | } // if validChar |
wim | 0:dc8789a6e21f | 497 | |
wim | 0:dc8789a6e21f | 498 | return value; |
wim | 0:dc8789a6e21f | 499 | } |
wim | 0:dc8789a6e21f | 500 | |
wim | 0:dc8789a6e21f | 501 | |
wim | 0:dc8789a6e21f | 502 | // get a single character (Stream implementation) |
wim | 0:dc8789a6e21f | 503 | int TM1637_CATALEX::_getc() { |
wim | 0:dc8789a6e21f | 504 | return -1; |
wim | 0:dc8789a6e21f | 505 | } |
wim | 0:dc8789a6e21f | 506 | |
wim | 0:dc8789a6e21f | 507 | #endif |