TEST
Dependents: ADXL345Test ADXL345Test1
Fork of TM1638 by
TM1638.cpp@1:1f2453ed85d7, 2016-01-09 (annotated)
- Committer:
- wim
- Date:
- Sat Jan 09 14:30:43 2016 +0000
- Revision:
- 1:1f2453ed85d7
- Parent:
- 0:201dfacbe0e5
- Child:
- 2:532ce15ea9ec
TM1638 LED controller, Initial version.; Supports LED&KEY module.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wim | 0:201dfacbe0e5 | 1 | /* mbed TM1638 Library, for TM1638 LED controller |
wim | 0:201dfacbe0e5 | 2 | * Copyright (c) 2015, v01: WH, Initial version |
wim | 0:201dfacbe0e5 | 3 | * |
wim | 0:201dfacbe0e5 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
wim | 0:201dfacbe0e5 | 5 | * of this software and associated documentation files (the "Software"), to deal |
wim | 0:201dfacbe0e5 | 6 | * in the Software without restriction, including without limitation the rights |
wim | 0:201dfacbe0e5 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
wim | 0:201dfacbe0e5 | 8 | * copies of the Software, and to permit persons to whom the Software is |
wim | 0:201dfacbe0e5 | 9 | * furnished to do so, subject to the following conditions: |
wim | 0:201dfacbe0e5 | 10 | * |
wim | 0:201dfacbe0e5 | 11 | * The above copyright notice and this permission notice shall be included in |
wim | 0:201dfacbe0e5 | 12 | * all copies or substantial portions of the Software. |
wim | 0:201dfacbe0e5 | 13 | * |
wim | 0:201dfacbe0e5 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
wim | 0:201dfacbe0e5 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
wim | 0:201dfacbe0e5 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
wim | 0:201dfacbe0e5 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
wim | 0:201dfacbe0e5 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
wim | 0:201dfacbe0e5 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
wim | 0:201dfacbe0e5 | 20 | * THE SOFTWARE. |
wim | 0:201dfacbe0e5 | 21 | */ |
wim | 0:201dfacbe0e5 | 22 | #include "mbed.h" |
wim | 0:201dfacbe0e5 | 23 | #include "TM1638.h" |
wim | 0:201dfacbe0e5 | 24 | |
wim | 0:201dfacbe0e5 | 25 | /** Constructor for class for driving TM1638 LED controller with SPI bus interface device. |
wim | 0:201dfacbe0e5 | 26 | * @brief Supports 8 digits @ 10 segments. |
wim | 0:201dfacbe0e5 | 27 | * Also supports a scanned keyboard of upto 24 keys. |
wim | 0:201dfacbe0e5 | 28 | * |
wim | 0:201dfacbe0e5 | 29 | * @param PinName mosi, miso, sclk, cs SPI bus pins |
wim | 0:201dfacbe0e5 | 30 | */ |
wim | 0:201dfacbe0e5 | 31 | TM1638::TM1638(PinName mosi, PinName miso, PinName sclk, PinName cs) : _spi(mosi,miso,sclk), _cs(cs) { |
wim | 0:201dfacbe0e5 | 32 | |
wim | 0:201dfacbe0e5 | 33 | _init(); |
wim | 0:201dfacbe0e5 | 34 | } |
wim | 0:201dfacbe0e5 | 35 | |
wim | 0:201dfacbe0e5 | 36 | /** Init the SPI interface and the controller |
wim | 0:201dfacbe0e5 | 37 | * @param none |
wim | 0:201dfacbe0e5 | 38 | * @return none |
wim | 0:201dfacbe0e5 | 39 | */ |
wim | 0:201dfacbe0e5 | 40 | void TM1638::_init(){ |
wim | 0:201dfacbe0e5 | 41 | |
wim | 0:201dfacbe0e5 | 42 | //init SPI |
wim | 0:201dfacbe0e5 | 43 | _cs=1; |
wim | 0:201dfacbe0e5 | 44 | _spi.format(8,3); //TM1638 uses mode 3 (Clock High on Idle, Data latched on second (=rising) edge) |
wim | 0:201dfacbe0e5 | 45 | _spi.frequency(500000); |
wim | 0:201dfacbe0e5 | 46 | |
wim | 0:201dfacbe0e5 | 47 | //init controller |
wim | 0:201dfacbe0e5 | 48 | _display = TM1638_DSP_ON; |
wim | 0:201dfacbe0e5 | 49 | _bright = TM1638_BRT_DEF; |
wim | 0:201dfacbe0e5 | 50 | _writeCmd(TM1638_DSP_CTRL_CMD, _display | _bright ); // Display control cmd, display on/off, brightness |
wim | 0:201dfacbe0e5 | 51 | |
wim | 0:201dfacbe0e5 | 52 | _writeCmd(TM1638_DATA_SET_CMD, TM1638_DATA_WR | TM1638_ADDR_INC | TM1638_MODE_NORM); // Data set cmd, normal mode, auto incr, write data |
wim | 0:201dfacbe0e5 | 53 | } |
wim | 0:201dfacbe0e5 | 54 | |
wim | 0:201dfacbe0e5 | 55 | |
wim | 0:201dfacbe0e5 | 56 | /** Clear the screen and locate to 0 |
wim | 0:201dfacbe0e5 | 57 | */ |
wim | 0:201dfacbe0e5 | 58 | void TM1638::cls() { |
wim | 0:201dfacbe0e5 | 59 | |
wim | 0:201dfacbe0e5 | 60 | _cs=0; |
wim | 0:201dfacbe0e5 | 61 | wait_us(1); |
wim | 0:201dfacbe0e5 | 62 | _spi.write(_flip(TM1638_ADDR_SET_CMD | 0x00)); // Address set cmd, 0 |
wim | 0:201dfacbe0e5 | 63 | |
wim | 0:201dfacbe0e5 | 64 | for (int cnt=0; cnt<TM1638_DISPLAY_MEM; cnt++) { |
wim | 0:201dfacbe0e5 | 65 | _spi.write(0x00); // data |
wim | 0:201dfacbe0e5 | 66 | } |
wim | 0:201dfacbe0e5 | 67 | |
wim | 0:201dfacbe0e5 | 68 | wait_us(1); |
wim | 0:201dfacbe0e5 | 69 | _cs=1; |
wim | 0:201dfacbe0e5 | 70 | } |
wim | 0:201dfacbe0e5 | 71 | |
wim | 0:201dfacbe0e5 | 72 | /** Set Brightness |
wim | 0:201dfacbe0e5 | 73 | * |
wim | 0:201dfacbe0e5 | 74 | * @param char brightness (3 significant bits, valid range 0..7 (1/16 .. 14/14 dutycycle) |
wim | 0:201dfacbe0e5 | 75 | * @return none |
wim | 0:201dfacbe0e5 | 76 | */ |
wim | 0:201dfacbe0e5 | 77 | void TM1638::setBrightness(char brightness){ |
wim | 0:201dfacbe0e5 | 78 | |
wim | 0:201dfacbe0e5 | 79 | _bright = brightness & TM1638_BRT_MSK; // mask invalid bits |
wim | 0:201dfacbe0e5 | 80 | |
wim | 0:201dfacbe0e5 | 81 | _writeCmd(TM1638_DSP_CTRL_CMD, _display | _bright ); // Display control cmd, display on/off, brightness |
wim | 0:201dfacbe0e5 | 82 | } |
wim | 0:201dfacbe0e5 | 83 | |
wim | 0:201dfacbe0e5 | 84 | /** Set the Display mode On/off |
wim | 0:201dfacbe0e5 | 85 | * |
wim | 0:201dfacbe0e5 | 86 | * @param bool display mode |
wim | 0:201dfacbe0e5 | 87 | */ |
wim | 0:201dfacbe0e5 | 88 | void TM1638::setDisplay(bool on) { |
wim | 0:201dfacbe0e5 | 89 | |
wim | 0:201dfacbe0e5 | 90 | if (on) { |
wim | 0:201dfacbe0e5 | 91 | _display = TM1638_DSP_ON; |
wim | 0:201dfacbe0e5 | 92 | } |
wim | 0:201dfacbe0e5 | 93 | else { |
wim | 0:201dfacbe0e5 | 94 | _display = TM1638_DSP_OFF; |
wim | 0:201dfacbe0e5 | 95 | } |
wim | 0:201dfacbe0e5 | 96 | |
wim | 0:201dfacbe0e5 | 97 | _writeCmd(TM1638_DSP_CTRL_CMD, _display | _bright ); // Display control cmd, display on/off, brightness |
wim | 0:201dfacbe0e5 | 98 | } |
wim | 0:201dfacbe0e5 | 99 | |
wim | 0:201dfacbe0e5 | 100 | /** Write databyte to TM1638 |
wim | 0:201dfacbe0e5 | 101 | * @param int address display memory location to write byte |
wim | 0:201dfacbe0e5 | 102 | * @param char data byte written at given address |
wim | 0:201dfacbe0e5 | 103 | * @return none |
wim | 0:201dfacbe0e5 | 104 | */ |
wim | 0:201dfacbe0e5 | 105 | void TM1638::writeData(int address, char data) { |
wim | 0:201dfacbe0e5 | 106 | _cs=0; |
wim | 0:201dfacbe0e5 | 107 | wait_us(1); |
wim | 0:201dfacbe0e5 | 108 | _spi.write(_flip(TM1638_ADDR_SET_CMD | (address & TM1638_ADDR_MSK))); // Set Address cmd |
wim | 0:201dfacbe0e5 | 109 | |
wim | 0:201dfacbe0e5 | 110 | _spi.write(_flip(data)); // data |
wim | 0:201dfacbe0e5 | 111 | |
wim | 0:201dfacbe0e5 | 112 | wait_us(1); |
wim | 0:201dfacbe0e5 | 113 | _cs=1; |
wim | 0:201dfacbe0e5 | 114 | } |
wim | 0:201dfacbe0e5 | 115 | |
wim | 0:201dfacbe0e5 | 116 | /** Write Display datablock to TM1638 |
wim | 0:201dfacbe0e5 | 117 | * @param DisplayData_t data Array of TM1638_DISPLAY_MEM (=16) bytes for displaydata (starting at address 0) |
wim | 0:201dfacbe0e5 | 118 | * @param length number bytes to write (valide range 0..TM1638_DISPLAY_MEM (=16), starting at address 0) |
wim | 0:201dfacbe0e5 | 119 | * @return none |
wim | 0:201dfacbe0e5 | 120 | */ |
wim | 0:201dfacbe0e5 | 121 | void TM1638::writeData(DisplayData_t data, int length) { |
wim | 0:201dfacbe0e5 | 122 | _cs=0; |
wim | 0:201dfacbe0e5 | 123 | wait_us(1); |
wim | 0:201dfacbe0e5 | 124 | _spi.write(_flip(TM1638_ADDR_SET_CMD | 0x00)); // Set Address at 0 |
wim | 0:201dfacbe0e5 | 125 | |
wim | 0:201dfacbe0e5 | 126 | // sanity check |
wim | 0:201dfacbe0e5 | 127 | if (length < 0) {length = 0;} |
wim | 0:201dfacbe0e5 | 128 | if (length > TM1638_DISPLAY_MEM) {length = TM1638_DISPLAY_MEM;} |
wim | 0:201dfacbe0e5 | 129 | |
wim | 0:201dfacbe0e5 | 130 | // for (int idx=0; idx<TM1638_DISPLAY_MEM; idx++) { |
wim | 0:201dfacbe0e5 | 131 | for (int idx=0; idx<length; idx++) { |
wim | 0:201dfacbe0e5 | 132 | _spi.write(_flip(data[idx])); // data |
wim | 0:201dfacbe0e5 | 133 | } |
wim | 0:201dfacbe0e5 | 134 | |
wim | 0:201dfacbe0e5 | 135 | wait_us(1); |
wim | 0:201dfacbe0e5 | 136 | _cs=1; |
wim | 0:201dfacbe0e5 | 137 | } |
wim | 0:201dfacbe0e5 | 138 | |
wim | 0:201dfacbe0e5 | 139 | |
wim | 0:201dfacbe0e5 | 140 | /** Read keydata block from TM1638 |
wim | 0:201dfacbe0e5 | 141 | * @param *keydata Ptr to Array of TM1638_KEY_MEM (=4) bytes for keydata |
wim | 0:201dfacbe0e5 | 142 | * @return bool keypress True when at least one key was pressed |
wim | 0:201dfacbe0e5 | 143 | * |
wim | 0:201dfacbe0e5 | 144 | * Note: Due to the hardware configuration the TM1638 key matrix scanner will detect multiple keys pressed at same time, |
wim | 0:201dfacbe0e5 | 145 | * but this may also result in some spurious keys being set in keypress data array. |
wim | 0:201dfacbe0e5 | 146 | * It may be best to ignore all keys in those situations. That option is implemented in this method depending on #define setting. |
wim | 0:201dfacbe0e5 | 147 | */ |
wim | 0:201dfacbe0e5 | 148 | bool TM1638::getKeys(KeyData_t *keydata) { |
wim | 0:201dfacbe0e5 | 149 | int keypress = 0; |
wim | 0:201dfacbe0e5 | 150 | char data; |
wim | 0:201dfacbe0e5 | 151 | |
wim | 0:201dfacbe0e5 | 152 | // Read keys |
wim | 0:201dfacbe0e5 | 153 | _cs=0; |
wim | 0:201dfacbe0e5 | 154 | wait_us(1); |
wim | 0:201dfacbe0e5 | 155 | |
wim | 0:201dfacbe0e5 | 156 | // Enable Key Read mode |
wim | 0:201dfacbe0e5 | 157 | _spi.write(_flip(TM1638_DATA_SET_CMD | TM1638_KEY_RD | TM1638_ADDR_INC | TM1638_MODE_NORM)); // Data set cmd, normal mode, auto incr, read data |
wim | 0:201dfacbe0e5 | 158 | |
wim | 0:201dfacbe0e5 | 159 | for (int idx=0; idx < TM1638_KEY_MEM; idx++) { |
wim | 0:201dfacbe0e5 | 160 | data = _flip(_spi.write(0xFF)); // read keys and correct bitorder |
wim | 0:201dfacbe0e5 | 161 | |
wim | 0:201dfacbe0e5 | 162 | if (data != 0) { // Check for any pressed key |
wim | 0:201dfacbe0e5 | 163 | for (int bit=0; bit < TM1638_KEY_BITS; bit++) { |
wim | 0:201dfacbe0e5 | 164 | if (data & (1 << bit)) {keypress++;} // Test all significant bits |
wim | 0:201dfacbe0e5 | 165 | } |
wim | 0:201dfacbe0e5 | 166 | } |
wim | 0:201dfacbe0e5 | 167 | |
wim | 0:201dfacbe0e5 | 168 | (*keydata)[idx] = data; // Store keydata after correcting bitorder |
wim | 0:201dfacbe0e5 | 169 | } |
wim | 0:201dfacbe0e5 | 170 | |
wim | 0:201dfacbe0e5 | 171 | wait_us(1); |
wim | 0:201dfacbe0e5 | 172 | _cs=1; |
wim | 0:201dfacbe0e5 | 173 | |
wim | 0:201dfacbe0e5 | 174 | // Restore Data Write mode |
wim | 0:201dfacbe0e5 | 175 | _writeCmd(TM1638_DATA_SET_CMD, TM1638_DATA_WR | TM1638_ADDR_INC | TM1638_MODE_NORM); // Data set cmd, normal mode, auto incr, write data |
wim | 0:201dfacbe0e5 | 176 | |
wim | 0:201dfacbe0e5 | 177 | #if(1) |
wim | 0:201dfacbe0e5 | 178 | // Dismiss multiple keypresses at same time |
wim | 0:201dfacbe0e5 | 179 | return (keypress == 1); |
wim | 0:201dfacbe0e5 | 180 | #else |
wim | 0:201dfacbe0e5 | 181 | // Allow multiple keypress and accept possible spurious keys |
wim | 0:201dfacbe0e5 | 182 | return (keypress > 0); |
wim | 0:201dfacbe0e5 | 183 | #endif |
wim | 0:201dfacbe0e5 | 184 | } |
wim | 0:201dfacbe0e5 | 185 | |
wim | 0:201dfacbe0e5 | 186 | |
wim | 0:201dfacbe0e5 | 187 | /** Helper to reverse all command or databits. The TM1638 expects LSB first, whereas SPI is MSB first |
wim | 0:201dfacbe0e5 | 188 | * @param char data |
wim | 0:201dfacbe0e5 | 189 | * @return bitreversed data |
wim | 0:201dfacbe0e5 | 190 | */ |
wim | 0:201dfacbe0e5 | 191 | char TM1638::_flip(char data) { |
wim | 0:201dfacbe0e5 | 192 | char value=0; |
wim | 0:201dfacbe0e5 | 193 | |
wim | 0:201dfacbe0e5 | 194 | if (data & 0x01) {value |= 0x80;} ; |
wim | 0:201dfacbe0e5 | 195 | if (data & 0x02) {value |= 0x40;} ; |
wim | 0:201dfacbe0e5 | 196 | if (data & 0x04) {value |= 0x20;} ; |
wim | 0:201dfacbe0e5 | 197 | if (data & 0x08) {value |= 0x10;} ; |
wim | 0:201dfacbe0e5 | 198 | if (data & 0x10) {value |= 0x08;} ; |
wim | 0:201dfacbe0e5 | 199 | if (data & 0x20) {value |= 0x04;} ; |
wim | 0:201dfacbe0e5 | 200 | if (data & 0x40) {value |= 0x02;} ; |
wim | 0:201dfacbe0e5 | 201 | if (data & 0x80) {value |= 0x01;} ; |
wim | 0:201dfacbe0e5 | 202 | return value; |
wim | 0:201dfacbe0e5 | 203 | } |
wim | 0:201dfacbe0e5 | 204 | |
wim | 0:201dfacbe0e5 | 205 | |
wim | 0:201dfacbe0e5 | 206 | /** Write command and parameter to TM1638 |
wim | 0:201dfacbe0e5 | 207 | * @param int cmd Command byte |
wim | 0:201dfacbe0e5 | 208 | * &Param int data Parameters for command |
wim | 0:201dfacbe0e5 | 209 | * @return none |
wim | 0:201dfacbe0e5 | 210 | */ |
wim | 0:201dfacbe0e5 | 211 | void TM1638::_writeCmd(int cmd, int data){ |
wim | 0:201dfacbe0e5 | 212 | |
wim | 0:201dfacbe0e5 | 213 | _cs=0; |
wim | 0:201dfacbe0e5 | 214 | wait_us(1); |
wim | 0:201dfacbe0e5 | 215 | // _spi.write(_flip( (cmd & 0xF0) | (data & 0x0F))); |
wim | 0:201dfacbe0e5 | 216 | _spi.write(_flip( (cmd & TM1638_CMD_MSK) | (data & ~TM1638_CMD_MSK))); |
wim | 0:201dfacbe0e5 | 217 | |
wim | 0:201dfacbe0e5 | 218 | wait_us(1); |
wim | 0:201dfacbe0e5 | 219 | _cs=1; |
wim | 0:201dfacbe0e5 | 220 | } |
wim | 0:201dfacbe0e5 | 221 | |
wim | 0:201dfacbe0e5 | 222 | |
wim | 0:201dfacbe0e5 | 223 | #if (LEDKEY8_TEST == 1) |
wim | 0:201dfacbe0e5 | 224 | // Derived class for TM1638 used in LED&KEY display unit |
wim | 0:201dfacbe0e5 | 225 | // |
wim | 0:201dfacbe0e5 | 226 | |
wim | 0:201dfacbe0e5 | 227 | /** Constructor for class for driving TM1638 LED controller as used in LEDKEY8 |
wim | 0:201dfacbe0e5 | 228 | * |
wim | 0:201dfacbe0e5 | 229 | * @brief Supports 8 Digits of 7 Segments + DP + LED Icons. Also supports a scanned keyboard of 8. |
wim | 0:201dfacbe0e5 | 230 | * |
wim | 0:201dfacbe0e5 | 231 | * @param PinName mosi, miso, sclk, cs SPI bus pins |
wim | 0:201dfacbe0e5 | 232 | */ |
wim | 0:201dfacbe0e5 | 233 | TM1638_LEDKEY8::TM1638_LEDKEY8(PinName mosi, PinName miso, PinName sclk, PinName cs) : TM1638(mosi, miso, sclk, cs) { |
wim | 0:201dfacbe0e5 | 234 | _column = 0; |
wim | 0:201dfacbe0e5 | 235 | _columns = LEDKEY8_NR_DIGITS; |
wim | 0:201dfacbe0e5 | 236 | } |
wim | 0:201dfacbe0e5 | 237 | |
wim | 0:201dfacbe0e5 | 238 | #if(0) |
wim | 0:201dfacbe0e5 | 239 | #if DOXYGEN_ONLY |
wim | 0:201dfacbe0e5 | 240 | /** Write a character to the Display |
wim | 0:201dfacbe0e5 | 241 | * |
wim | 0:201dfacbe0e5 | 242 | * @param c The character to write to the display |
wim | 0:201dfacbe0e5 | 243 | */ |
wim | 0:201dfacbe0e5 | 244 | int putc(int c); |
wim | 0:201dfacbe0e5 | 245 | |
wim | 0:201dfacbe0e5 | 246 | /** Write a formatted string to the Display |
wim | 0:201dfacbe0e5 | 247 | * |
wim | 0:201dfacbe0e5 | 248 | * @param format A printf-style format string, followed by the |
wim | 0:201dfacbe0e5 | 249 | * variables to use in formatting the string. |
wim | 0:201dfacbe0e5 | 250 | */ |
wim | 0:201dfacbe0e5 | 251 | int printf(const char* format, ...); |
wim | 0:201dfacbe0e5 | 252 | #endif |
wim | 0:201dfacbe0e5 | 253 | #endif |
wim | 0:201dfacbe0e5 | 254 | |
wim | 0:201dfacbe0e5 | 255 | /** Locate cursor to a screen column |
wim | 0:201dfacbe0e5 | 256 | * |
wim | 0:201dfacbe0e5 | 257 | * @param column The horizontal position from the left, indexed from 0 |
wim | 0:201dfacbe0e5 | 258 | */ |
wim | 0:201dfacbe0e5 | 259 | void TM1638_LEDKEY8::locate(int column) { |
wim | 0:201dfacbe0e5 | 260 | //sanity check |
wim | 0:201dfacbe0e5 | 261 | if (column < 0) {column = 0;} |
wim | 0:201dfacbe0e5 | 262 | if (column > (_columns - 1)) {column = _columns - 1;} |
wim | 0:201dfacbe0e5 | 263 | |
wim | 0:201dfacbe0e5 | 264 | _column = column; |
wim | 0:201dfacbe0e5 | 265 | } |
wim | 0:201dfacbe0e5 | 266 | |
wim | 0:201dfacbe0e5 | 267 | |
wim | 0:201dfacbe0e5 | 268 | /** Number of screen columns |
wim | 0:201dfacbe0e5 | 269 | * |
wim | 0:201dfacbe0e5 | 270 | * @param none |
wim | 0:201dfacbe0e5 | 271 | * @return columns |
wim | 0:201dfacbe0e5 | 272 | */ |
wim | 0:201dfacbe0e5 | 273 | int TM1638_LEDKEY8::columns() { |
wim | 0:201dfacbe0e5 | 274 | return _columns; |
wim | 0:201dfacbe0e5 | 275 | } |
wim | 0:201dfacbe0e5 | 276 | |
wim | 0:201dfacbe0e5 | 277 | |
wim | 0:201dfacbe0e5 | 278 | /** Clear the screen and locate to 0 |
wim | 0:201dfacbe0e5 | 279 | * @param bool clrAll Clear Icons also (default = false) |
wim | 0:201dfacbe0e5 | 280 | */ |
wim | 0:201dfacbe0e5 | 281 | void TM1638_LEDKEY8::cls(bool clrAll) { |
wim | 0:201dfacbe0e5 | 282 | |
wim | 0:201dfacbe0e5 | 283 | if (clrAll) { |
wim | 0:201dfacbe0e5 | 284 | //clear local buffer (including Icons) |
wim | 0:201dfacbe0e5 | 285 | for (int idx=0; idx < (LEDKEY8_NR_GRIDS << 1); idx++) { |
wim | 0:201dfacbe0e5 | 286 | _displaybuffer[idx] = 0x00; |
wim | 0:201dfacbe0e5 | 287 | } |
wim | 0:201dfacbe0e5 | 288 | } |
wim | 0:201dfacbe0e5 | 289 | else { |
wim | 0:201dfacbe0e5 | 290 | //clear local buffer (preserving Icons) |
wim | 0:201dfacbe0e5 | 291 | for (int idx=0; idx < LEDKEY8_NR_GRIDS; idx++) { |
wim | 0:201dfacbe0e5 | 292 | _displaybuffer[(idx<<1)] = _displaybuffer[(idx<<1)] & MASK_ICON_GRID[idx][0]; |
wim | 0:201dfacbe0e5 | 293 | _displaybuffer[(idx<<1) + 1] = _displaybuffer[(idx<<1) + 1] & MASK_ICON_GRID[idx][1]; |
wim | 0:201dfacbe0e5 | 294 | } |
wim | 0:201dfacbe0e5 | 295 | } |
wim | 0:201dfacbe0e5 | 296 | |
wim | 0:201dfacbe0e5 | 297 | writeData(_displaybuffer, (LEDKEY8_NR_GRIDS*2)); |
wim | 0:201dfacbe0e5 | 298 | |
wim | 0:201dfacbe0e5 | 299 | _column = 0; |
wim | 0:201dfacbe0e5 | 300 | } |
wim | 0:201dfacbe0e5 | 301 | |
wim | 0:201dfacbe0e5 | 302 | /** Set Icon |
wim | 0:201dfacbe0e5 | 303 | * |
wim | 1:1f2453ed85d7 | 304 | * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 16 LSBs |
wim | 0:201dfacbe0e5 | 305 | * @return none |
wim | 0:201dfacbe0e5 | 306 | */ |
wim | 0:201dfacbe0e5 | 307 | void TM1638_LEDKEY8::setIcon(Icon icon) { |
wim | 0:201dfacbe0e5 | 308 | int addr, icn; |
wim | 0:201dfacbe0e5 | 309 | |
wim | 1:1f2453ed85d7 | 310 | icn = icon & 0xFFFF; |
wim | 0:201dfacbe0e5 | 311 | addr = (icon >> 24) & 0xFF; |
wim | 0:201dfacbe0e5 | 312 | addr = (addr - 1) << 1; |
wim | 0:201dfacbe0e5 | 313 | |
wim | 0:201dfacbe0e5 | 314 | //Save char...and set bits for icon to write |
wim | 0:201dfacbe0e5 | 315 | _displaybuffer[addr] = _displaybuffer[addr] | LO(icn); |
wim | 0:201dfacbe0e5 | 316 | _displaybuffer[addr+1] = _displaybuffer[addr+1] | HI(icn); |
wim | 0:201dfacbe0e5 | 317 | writeData(_displaybuffer, (LEDKEY8_NR_GRIDS*2)); |
wim | 0:201dfacbe0e5 | 318 | } |
wim | 0:201dfacbe0e5 | 319 | |
wim | 0:201dfacbe0e5 | 320 | /** Clr Icon |
wim | 0:201dfacbe0e5 | 321 | * |
wim | 1:1f2453ed85d7 | 322 | * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 16 LSBs |
wim | 0:201dfacbe0e5 | 323 | * @return none |
wim | 0:201dfacbe0e5 | 324 | */ |
wim | 0:201dfacbe0e5 | 325 | void TM1638_LEDKEY8::clrIcon(Icon icon) { |
wim | 0:201dfacbe0e5 | 326 | int addr, icn; |
wim | 0:201dfacbe0e5 | 327 | |
wim | 1:1f2453ed85d7 | 328 | icn = icon & 0xFFFF; |
wim | 0:201dfacbe0e5 | 329 | addr = (icon >> 24) & 0xFF; |
wim | 0:201dfacbe0e5 | 330 | addr = (addr - 1) << 1; |
wim | 0:201dfacbe0e5 | 331 | |
wim | 0:201dfacbe0e5 | 332 | //Save char...and clr bits for icon to write |
wim | 0:201dfacbe0e5 | 333 | _displaybuffer[addr] = _displaybuffer[addr] & ~LO(icn); |
wim | 0:201dfacbe0e5 | 334 | _displaybuffer[addr+1] = _displaybuffer[addr+1] & ~HI(icn); |
wim | 0:201dfacbe0e5 | 335 | writeData(_displaybuffer, (LEDKEY8_NR_GRIDS*2)); |
wim | 0:201dfacbe0e5 | 336 | } |
wim | 0:201dfacbe0e5 | 337 | |
wim | 0:201dfacbe0e5 | 338 | |
wim | 0:201dfacbe0e5 | 339 | /** Set User Defined Characters (UDC) |
wim | 0:201dfacbe0e5 | 340 | * |
wim | 0:201dfacbe0e5 | 341 | * @param unsigned char udc_idx The Index of the UDC (0..7) |
wim | 0:201dfacbe0e5 | 342 | * @param int udc_data The bitpattern for the UDC (8 bits) |
wim | 0:201dfacbe0e5 | 343 | */ |
wim | 0:201dfacbe0e5 | 344 | void TM1638_LEDKEY8::setUDC(unsigned char udc_idx, int udc_data) { |
wim | 0:201dfacbe0e5 | 345 | |
wim | 0:201dfacbe0e5 | 346 | //Sanity check |
wim | 0:201dfacbe0e5 | 347 | if (udc_idx > (LEDKEY8_NR_UDC-1)) { |
wim | 0:201dfacbe0e5 | 348 | return; |
wim | 0:201dfacbe0e5 | 349 | } |
wim | 0:201dfacbe0e5 | 350 | // Mask out Icon bits? |
wim | 0:201dfacbe0e5 | 351 | |
wim | 0:201dfacbe0e5 | 352 | _UDC_7S[udc_idx] = LO(udc_data); |
wim | 0:201dfacbe0e5 | 353 | } |
wim | 0:201dfacbe0e5 | 354 | |
wim | 0:201dfacbe0e5 | 355 | |
wim | 0:201dfacbe0e5 | 356 | /** Write a single character (Stream implementation) |
wim | 0:201dfacbe0e5 | 357 | */ |
wim | 0:201dfacbe0e5 | 358 | int TM1638_LEDKEY8::_putc(int value) { |
wim | 0:201dfacbe0e5 | 359 | int addr; |
wim | 0:201dfacbe0e5 | 360 | bool validChar = false; |
wim | 0:201dfacbe0e5 | 361 | char pattern = 0x00; |
wim | 0:201dfacbe0e5 | 362 | |
wim | 0:201dfacbe0e5 | 363 | if ((value == '\n') || (value == '\r')) { |
wim | 0:201dfacbe0e5 | 364 | //No character to write |
wim | 0:201dfacbe0e5 | 365 | validChar = false; |
wim | 0:201dfacbe0e5 | 366 | |
wim | 0:201dfacbe0e5 | 367 | //Update Cursor |
wim | 0:201dfacbe0e5 | 368 | _column = 0; |
wim | 0:201dfacbe0e5 | 369 | } |
wim | 1:1f2453ed85d7 | 370 | else if ((value == '.') || (value == ',')) { |
wim | 1:1f2453ed85d7 | 371 | //No character to write |
wim | 1:1f2453ed85d7 | 372 | validChar = false; |
wim | 1:1f2453ed85d7 | 373 | pattern = S7_DP; // placeholder for all DPs |
wim | 1:1f2453ed85d7 | 374 | |
wim | 1:1f2453ed85d7 | 375 | // Check to see that DP can be shown for current column |
wim | 1:1f2453ed85d7 | 376 | if (_column > 0) { |
wim | 1:1f2453ed85d7 | 377 | //Translate between _column and displaybuffer entries |
wim | 1:1f2453ed85d7 | 378 | //Add DP to bitpattern of digit left of current column. |
wim | 1:1f2453ed85d7 | 379 | addr = (_column - 1) << 1; |
wim | 1:1f2453ed85d7 | 380 | |
wim | 1:1f2453ed85d7 | 381 | //Save icons...and set bits for decimal point to write |
wim | 1:1f2453ed85d7 | 382 | _displaybuffer[addr] = _displaybuffer[addr] | pattern; |
wim | 1:1f2453ed85d7 | 383 | // _displaybuffer[addr+1] = _displaybuffer[addr+1] | pattern; |
wim | 1:1f2453ed85d7 | 384 | |
wim | 1:1f2453ed85d7 | 385 | writeData(_displaybuffer, (LEDKEY8_NR_GRIDS*2)); |
wim | 1:1f2453ed85d7 | 386 | |
wim | 1:1f2453ed85d7 | 387 | //No Cursor Update |
wim | 1:1f2453ed85d7 | 388 | } |
wim | 1:1f2453ed85d7 | 389 | } |
wim | 1:1f2453ed85d7 | 390 | else if (value == '-') { |
wim | 1:1f2453ed85d7 | 391 | //No character to write |
wim | 1:1f2453ed85d7 | 392 | validChar = true; |
wim | 1:1f2453ed85d7 | 393 | pattern = C7_MIN; |
wim | 1:1f2453ed85d7 | 394 | } |
wim | 0:201dfacbe0e5 | 395 | else if ((value >= 0) && (value < LEDKEY8_NR_UDC)) { |
wim | 0:201dfacbe0e5 | 396 | //Character to write |
wim | 0:201dfacbe0e5 | 397 | validChar = true; |
wim | 0:201dfacbe0e5 | 398 | pattern = _UDC_7S[value]; |
wim | 0:201dfacbe0e5 | 399 | } |
wim | 0:201dfacbe0e5 | 400 | else if ((value >= (int)'0') && (value <= (int) '9')) { |
wim | 0:201dfacbe0e5 | 401 | //Character to write |
wim | 0:201dfacbe0e5 | 402 | validChar = true; |
wim | 0:201dfacbe0e5 | 403 | pattern = FONT_7S[value - (int) '0']; |
wim | 0:201dfacbe0e5 | 404 | } |
wim | 0:201dfacbe0e5 | 405 | else if ((value >= (int) 'A') && (value <= (int) 'F')) { |
wim | 0:201dfacbe0e5 | 406 | //Character to write |
wim | 0:201dfacbe0e5 | 407 | validChar = true; |
wim | 0:201dfacbe0e5 | 408 | pattern = FONT_7S[10 + value - (int) 'A']; |
wim | 0:201dfacbe0e5 | 409 | } |
wim | 0:201dfacbe0e5 | 410 | else if ((value >= (int) 'a') && (value <= (int) 'f')) { |
wim | 0:201dfacbe0e5 | 411 | //Character to write |
wim | 0:201dfacbe0e5 | 412 | validChar = true; |
wim | 0:201dfacbe0e5 | 413 | pattern = FONT_7S[10 + value - (int) 'a']; |
wim | 0:201dfacbe0e5 | 414 | } //else |
wim | 0:201dfacbe0e5 | 415 | |
wim | 0:201dfacbe0e5 | 416 | if (validChar) { |
wim | 0:201dfacbe0e5 | 417 | //Character to write |
wim | 0:201dfacbe0e5 | 418 | |
wim | 0:201dfacbe0e5 | 419 | //Translate between _column and displaybuffer entries |
wim | 0:201dfacbe0e5 | 420 | addr = _column << 1; |
wim | 0:201dfacbe0e5 | 421 | |
wim | 0:201dfacbe0e5 | 422 | //Save icons...and set bits for character to write |
wim | 0:201dfacbe0e5 | 423 | _displaybuffer[addr] = (_displaybuffer[addr] & MASK_ICON_GRID[_column][0]) | pattern; |
wim | 0:201dfacbe0e5 | 424 | // _displaybuffer[addr+1] = (_displaybuffer[addr+1] & MASK_ICON_GRID[_column][0]) | pattern; |
wim | 0:201dfacbe0e5 | 425 | |
wim | 0:201dfacbe0e5 | 426 | writeData(_displaybuffer, (LEDKEY8_NR_GRIDS*2)); |
wim | 0:201dfacbe0e5 | 427 | |
wim | 0:201dfacbe0e5 | 428 | //Update Cursor |
wim | 0:201dfacbe0e5 | 429 | _column++; |
wim | 0:201dfacbe0e5 | 430 | if (_column > (LEDKEY8_NR_DIGITS - 1)) { |
wim | 0:201dfacbe0e5 | 431 | _column = 0; |
wim | 0:201dfacbe0e5 | 432 | } |
wim | 0:201dfacbe0e5 | 433 | |
wim | 0:201dfacbe0e5 | 434 | } // if validChar |
wim | 0:201dfacbe0e5 | 435 | |
wim | 0:201dfacbe0e5 | 436 | return value; |
wim | 0:201dfacbe0e5 | 437 | } |
wim | 0:201dfacbe0e5 | 438 | |
wim | 0:201dfacbe0e5 | 439 | |
wim | 0:201dfacbe0e5 | 440 | // get a single character (Stream implementation) |
wim | 0:201dfacbe0e5 | 441 | int TM1638_LEDKEY8::_getc() { |
wim | 0:201dfacbe0e5 | 442 | return -1; |
wim | 0:201dfacbe0e5 | 443 | } |
wim | 0:201dfacbe0e5 | 444 | |
wim | 0:201dfacbe0e5 | 445 | #endif |