TM1638 LED controller. Max 80 LEDs, Max 24 Key scan. Supports LED&KEY, QYF-TM1638 and JY-LKM1638 module.

Dependents:   mbed_TM1638 Otjimaniya

See here for more information.

Committer:
wim
Date:
Sun Jan 31 12:11:06 2016 +0000
Revision:
4:b2bbdc58967e
Parent:
3:25ddabfadc8c
Added QYF-TM1638 and LKM1638, refactoring of writeData()

Who changed what in which revision?

UserRevisionLine numberNew 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 2:532ce15ea9ec 3 * 2016, v02: WH, Added ASCII alphabet display selector, refactored display and keyboard defines
wim 4:b2bbdc58967e 4 * 2016, v03: WH, Added QYF-TM1638 and LKM1638, refactoring of writeData()
wim 0:201dfacbe0e5 5 *
wim 0:201dfacbe0e5 6 * Permission is hereby granted, free of charge, to any person obtaining a copy
wim 0:201dfacbe0e5 7 * of this software and associated documentation files (the "Software"), to deal
wim 0:201dfacbe0e5 8 * in the Software without restriction, including without limitation the rights
wim 0:201dfacbe0e5 9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
wim 0:201dfacbe0e5 10 * copies of the Software, and to permit persons to whom the Software is
wim 0:201dfacbe0e5 11 * furnished to do so, subject to the following conditions:
wim 0:201dfacbe0e5 12 *
wim 0:201dfacbe0e5 13 * The above copyright notice and this permission notice shall be included in
wim 0:201dfacbe0e5 14 * all copies or substantial portions of the Software.
wim 0:201dfacbe0e5 15 *
wim 0:201dfacbe0e5 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
wim 0:201dfacbe0e5 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
wim 0:201dfacbe0e5 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
wim 0:201dfacbe0e5 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
wim 0:201dfacbe0e5 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
wim 0:201dfacbe0e5 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
wim 0:201dfacbe0e5 22 * THE SOFTWARE.
wim 0:201dfacbe0e5 23 */
wim 0:201dfacbe0e5 24 #include "mbed.h"
wim 0:201dfacbe0e5 25 #include "TM1638.h"
wim 0:201dfacbe0e5 26
wim 0:201dfacbe0e5 27 /** Constructor for class for driving TM1638 LED controller with SPI bus interface device.
wim 0:201dfacbe0e5 28 * @brief Supports 8 digits @ 10 segments.
wim 0:201dfacbe0e5 29 * Also supports a scanned keyboard of upto 24 keys.
wim 0:201dfacbe0e5 30 *
wim 0:201dfacbe0e5 31 * @param PinName mosi, miso, sclk, cs SPI bus pins
wim 0:201dfacbe0e5 32 */
wim 0:201dfacbe0e5 33 TM1638::TM1638(PinName mosi, PinName miso, PinName sclk, PinName cs) : _spi(mosi,miso,sclk), _cs(cs) {
wim 0:201dfacbe0e5 34
wim 0:201dfacbe0e5 35 _init();
wim 0:201dfacbe0e5 36 }
wim 0:201dfacbe0e5 37
wim 0:201dfacbe0e5 38 /** Init the SPI interface and the controller
wim 0:201dfacbe0e5 39 * @param none
wim 0:201dfacbe0e5 40 * @return none
wim 0:201dfacbe0e5 41 */
wim 0:201dfacbe0e5 42 void TM1638::_init(){
wim 0:201dfacbe0e5 43
wim 0:201dfacbe0e5 44 //init SPI
wim 0:201dfacbe0e5 45 _cs=1;
wim 0:201dfacbe0e5 46 _spi.format(8,3); //TM1638 uses mode 3 (Clock High on Idle, Data latched on second (=rising) edge)
wim 0:201dfacbe0e5 47 _spi.frequency(500000);
wim 0:201dfacbe0e5 48
wim 0:201dfacbe0e5 49 //init controller
wim 0:201dfacbe0e5 50 _display = TM1638_DSP_ON;
wim 0:201dfacbe0e5 51 _bright = TM1638_BRT_DEF;
wim 0:201dfacbe0e5 52 _writeCmd(TM1638_DSP_CTRL_CMD, _display | _bright ); // Display control cmd, display on/off, brightness
wim 0:201dfacbe0e5 53
wim 0:201dfacbe0e5 54 _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 55 }
wim 0:201dfacbe0e5 56
wim 0:201dfacbe0e5 57
wim 0:201dfacbe0e5 58 /** Clear the screen and locate to 0
wim 0:201dfacbe0e5 59 */
wim 0:201dfacbe0e5 60 void TM1638::cls() {
wim 0:201dfacbe0e5 61
wim 0:201dfacbe0e5 62 _cs=0;
wim 0:201dfacbe0e5 63 wait_us(1);
wim 0:201dfacbe0e5 64 _spi.write(_flip(TM1638_ADDR_SET_CMD | 0x00)); // Address set cmd, 0
wim 0:201dfacbe0e5 65
wim 0:201dfacbe0e5 66 for (int cnt=0; cnt<TM1638_DISPLAY_MEM; cnt++) {
wim 0:201dfacbe0e5 67 _spi.write(0x00); // data
wim 0:201dfacbe0e5 68 }
wim 0:201dfacbe0e5 69
wim 0:201dfacbe0e5 70 wait_us(1);
wim 0:201dfacbe0e5 71 _cs=1;
wim 0:201dfacbe0e5 72 }
wim 0:201dfacbe0e5 73
wim 0:201dfacbe0e5 74 /** Set Brightness
wim 0:201dfacbe0e5 75 *
wim 0:201dfacbe0e5 76 * @param char brightness (3 significant bits, valid range 0..7 (1/16 .. 14/14 dutycycle)
wim 0:201dfacbe0e5 77 * @return none
wim 0:201dfacbe0e5 78 */
wim 0:201dfacbe0e5 79 void TM1638::setBrightness(char brightness){
wim 0:201dfacbe0e5 80
wim 0:201dfacbe0e5 81 _bright = brightness & TM1638_BRT_MSK; // mask invalid bits
wim 0:201dfacbe0e5 82
wim 0:201dfacbe0e5 83 _writeCmd(TM1638_DSP_CTRL_CMD, _display | _bright ); // Display control cmd, display on/off, brightness
wim 0:201dfacbe0e5 84 }
wim 0:201dfacbe0e5 85
wim 0:201dfacbe0e5 86 /** Set the Display mode On/off
wim 0:201dfacbe0e5 87 *
wim 0:201dfacbe0e5 88 * @param bool display mode
wim 0:201dfacbe0e5 89 */
wim 0:201dfacbe0e5 90 void TM1638::setDisplay(bool on) {
wim 0:201dfacbe0e5 91
wim 0:201dfacbe0e5 92 if (on) {
wim 0:201dfacbe0e5 93 _display = TM1638_DSP_ON;
wim 0:201dfacbe0e5 94 }
wim 0:201dfacbe0e5 95 else {
wim 0:201dfacbe0e5 96 _display = TM1638_DSP_OFF;
wim 0:201dfacbe0e5 97 }
wim 0:201dfacbe0e5 98
wim 0:201dfacbe0e5 99 _writeCmd(TM1638_DSP_CTRL_CMD, _display | _bright ); // Display control cmd, display on/off, brightness
wim 0:201dfacbe0e5 100 }
wim 0:201dfacbe0e5 101
wim 4:b2bbdc58967e 102
wim 0:201dfacbe0e5 103 /** Write databyte to TM1638
wim 4:b2bbdc58967e 104 * @param char data byte written at given address
wim 0:201dfacbe0e5 105 * @param int address display memory location to write byte
wim 0:201dfacbe0e5 106 * @return none
wim 0:201dfacbe0e5 107 */
wim 4:b2bbdc58967e 108 void TM1638::writeData(char data, int address) {
wim 0:201dfacbe0e5 109 _cs=0;
wim 0:201dfacbe0e5 110 wait_us(1);
wim 0:201dfacbe0e5 111 _spi.write(_flip(TM1638_ADDR_SET_CMD | (address & TM1638_ADDR_MSK))); // Set Address cmd
wim 0:201dfacbe0e5 112
wim 0:201dfacbe0e5 113 _spi.write(_flip(data)); // data
wim 0:201dfacbe0e5 114
wim 0:201dfacbe0e5 115 wait_us(1);
wim 0:201dfacbe0e5 116 _cs=1;
wim 0:201dfacbe0e5 117 }
wim 0:201dfacbe0e5 118
wim 4:b2bbdc58967e 119
wim 0:201dfacbe0e5 120 /** Write Display datablock to TM1638
wim 4:b2bbdc58967e 121 * @param DisplayData_t data Array of TM1638_DISPLAY_MEM (=16) bytes for displaydata
wim 4:b2bbdc58967e 122 * @param length number bytes to write (valid range 0..TM1638_DISPLAY_MEM (=16), when starting at address 0)
wim 4:b2bbdc58967e 123 * @param int address display memory location to write bytes (default = 0)
wim 0:201dfacbe0e5 124 * @return none
wim 0:201dfacbe0e5 125 */
wim 4:b2bbdc58967e 126 void TM1638::writeData(DisplayData_t data, int length, int address) {
wim 0:201dfacbe0e5 127 _cs=0;
wim 0:201dfacbe0e5 128 wait_us(1);
wim 4:b2bbdc58967e 129
wim 0:201dfacbe0e5 130 // sanity check
wim 4:b2bbdc58967e 131 address &= TM1638_ADDR_MSK;
wim 0:201dfacbe0e5 132 if (length < 0) {length = 0;}
wim 4:b2bbdc58967e 133 if ((length + address) > TM1638_DISPLAY_MEM) {length = (TM1638_DISPLAY_MEM - address);}
wim 0:201dfacbe0e5 134
wim 4:b2bbdc58967e 135 // _spi.write(_flip(TM1638_ADDR_SET_CMD | 0x00)); // Set Address at 0
wim 4:b2bbdc58967e 136 _spi.write(_flip(TM1638_ADDR_SET_CMD | address)); // Set Address
wim 4:b2bbdc58967e 137
wim 0:201dfacbe0e5 138 for (int idx=0; idx<length; idx++) {
wim 4:b2bbdc58967e 139 //_spi.write(_flip(data[idx])); // data
wim 4:b2bbdc58967e 140 _spi.write(_flip(data[address + idx])); // data
wim 0:201dfacbe0e5 141 }
wim 0:201dfacbe0e5 142
wim 0:201dfacbe0e5 143 wait_us(1);
wim 0:201dfacbe0e5 144 _cs=1;
wim 0:201dfacbe0e5 145 }
wim 0:201dfacbe0e5 146
wim 0:201dfacbe0e5 147
wim 0:201dfacbe0e5 148 /** Read keydata block from TM1638
wim 0:201dfacbe0e5 149 * @param *keydata Ptr to Array of TM1638_KEY_MEM (=4) bytes for keydata
wim 0:201dfacbe0e5 150 * @return bool keypress True when at least one key was pressed
wim 0:201dfacbe0e5 151 *
wim 0:201dfacbe0e5 152 * Note: Due to the hardware configuration the TM1638 key matrix scanner will detect multiple keys pressed at same time,
wim 0:201dfacbe0e5 153 * but this may also result in some spurious keys being set in keypress data array.
wim 0:201dfacbe0e5 154 * 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 155 */
wim 0:201dfacbe0e5 156 bool TM1638::getKeys(KeyData_t *keydata) {
wim 0:201dfacbe0e5 157 int keypress = 0;
wim 0:201dfacbe0e5 158 char data;
wim 0:201dfacbe0e5 159
wim 0:201dfacbe0e5 160 // Read keys
wim 0:201dfacbe0e5 161 _cs=0;
wim 0:201dfacbe0e5 162 wait_us(1);
wim 0:201dfacbe0e5 163
wim 0:201dfacbe0e5 164 // Enable Key Read mode
wim 0:201dfacbe0e5 165 _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 166
wim 0:201dfacbe0e5 167 for (int idx=0; idx < TM1638_KEY_MEM; idx++) {
wim 0:201dfacbe0e5 168 data = _flip(_spi.write(0xFF)); // read keys and correct bitorder
wim 0:201dfacbe0e5 169
wim 2:532ce15ea9ec 170 data = data & TM1638_KEY_MSK; // Mask valid bits
wim 0:201dfacbe0e5 171 if (data != 0) { // Check for any pressed key
wim 2:532ce15ea9ec 172 for (int bit=0; bit < 8; bit++) {
wim 0:201dfacbe0e5 173 if (data & (1 << bit)) {keypress++;} // Test all significant bits
wim 0:201dfacbe0e5 174 }
wim 0:201dfacbe0e5 175 }
wim 0:201dfacbe0e5 176
wim 0:201dfacbe0e5 177 (*keydata)[idx] = data; // Store keydata after correcting bitorder
wim 0:201dfacbe0e5 178 }
wim 0:201dfacbe0e5 179
wim 0:201dfacbe0e5 180 wait_us(1);
wim 0:201dfacbe0e5 181 _cs=1;
wim 0:201dfacbe0e5 182
wim 0:201dfacbe0e5 183 // Restore Data Write mode
wim 0:201dfacbe0e5 184 _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 185
wim 0:201dfacbe0e5 186 #if(1)
wim 0:201dfacbe0e5 187 // Dismiss multiple keypresses at same time
wim 0:201dfacbe0e5 188 return (keypress == 1);
wim 0:201dfacbe0e5 189 #else
wim 0:201dfacbe0e5 190 // Allow multiple keypress and accept possible spurious keys
wim 0:201dfacbe0e5 191 return (keypress > 0);
wim 0:201dfacbe0e5 192 #endif
wim 0:201dfacbe0e5 193 }
wim 0:201dfacbe0e5 194
wim 0:201dfacbe0e5 195
wim 0:201dfacbe0e5 196 /** Helper to reverse all command or databits. The TM1638 expects LSB first, whereas SPI is MSB first
wim 0:201dfacbe0e5 197 * @param char data
wim 0:201dfacbe0e5 198 * @return bitreversed data
wim 0:201dfacbe0e5 199 */
wim 3:25ddabfadc8c 200 #if(1)
wim 0:201dfacbe0e5 201 char TM1638::_flip(char data) {
wim 0:201dfacbe0e5 202 char value=0;
wim 0:201dfacbe0e5 203
wim 0:201dfacbe0e5 204 if (data & 0x01) {value |= 0x80;} ;
wim 0:201dfacbe0e5 205 if (data & 0x02) {value |= 0x40;} ;
wim 0:201dfacbe0e5 206 if (data & 0x04) {value |= 0x20;} ;
wim 0:201dfacbe0e5 207 if (data & 0x08) {value |= 0x10;} ;
wim 0:201dfacbe0e5 208 if (data & 0x10) {value |= 0x08;} ;
wim 0:201dfacbe0e5 209 if (data & 0x20) {value |= 0x04;} ;
wim 0:201dfacbe0e5 210 if (data & 0x40) {value |= 0x02;} ;
wim 0:201dfacbe0e5 211 if (data & 0x80) {value |= 0x01;} ;
wim 0:201dfacbe0e5 212 return value;
wim 0:201dfacbe0e5 213 }
wim 3:25ddabfadc8c 214 #else
wim 3:25ddabfadc8c 215 char TM1638::_flip(char data) {
wim 0:201dfacbe0e5 216
wim 3:25ddabfadc8c 217 data = (((data & 0xAA) >> 1) | ((data & 0x55) << 1));
wim 3:25ddabfadc8c 218 data = (((data & 0xCC) >> 2) | ((data & 0x33) << 2));
wim 3:25ddabfadc8c 219 return (((data & 0xF0) >> 4) | ((data & 0x0F) << 4));
wim 3:25ddabfadc8c 220 }
wim 3:25ddabfadc8c 221 #endif
wim 0:201dfacbe0e5 222
wim 0:201dfacbe0e5 223 /** Write command and parameter to TM1638
wim 0:201dfacbe0e5 224 * @param int cmd Command byte
wim 0:201dfacbe0e5 225 * &Param int data Parameters for command
wim 0:201dfacbe0e5 226 * @return none
wim 0:201dfacbe0e5 227 */
wim 0:201dfacbe0e5 228 void TM1638::_writeCmd(int cmd, int data){
wim 0:201dfacbe0e5 229
wim 0:201dfacbe0e5 230 _cs=0;
wim 0:201dfacbe0e5 231 wait_us(1);
wim 0:201dfacbe0e5 232 // _spi.write(_flip( (cmd & 0xF0) | (data & 0x0F)));
wim 0:201dfacbe0e5 233 _spi.write(_flip( (cmd & TM1638_CMD_MSK) | (data & ~TM1638_CMD_MSK)));
wim 0:201dfacbe0e5 234
wim 0:201dfacbe0e5 235 wait_us(1);
wim 0:201dfacbe0e5 236 _cs=1;
wim 0:201dfacbe0e5 237 }
wim 0:201dfacbe0e5 238
wim 0:201dfacbe0e5 239
wim 0:201dfacbe0e5 240 #if (LEDKEY8_TEST == 1)
wim 0:201dfacbe0e5 241 // Derived class for TM1638 used in LED&KEY display unit
wim 0:201dfacbe0e5 242 //
wim 0:201dfacbe0e5 243
wim 0:201dfacbe0e5 244 /** Constructor for class for driving TM1638 LED controller as used in LEDKEY8
wim 0:201dfacbe0e5 245 *
wim 0:201dfacbe0e5 246 * @brief Supports 8 Digits of 7 Segments + DP + LED Icons. Also supports a scanned keyboard of 8.
wim 0:201dfacbe0e5 247 *
wim 0:201dfacbe0e5 248 * @param PinName mosi, miso, sclk, cs SPI bus pins
wim 0:201dfacbe0e5 249 */
wim 0:201dfacbe0e5 250 TM1638_LEDKEY8::TM1638_LEDKEY8(PinName mosi, PinName miso, PinName sclk, PinName cs) : TM1638(mosi, miso, sclk, cs) {
wim 0:201dfacbe0e5 251 _column = 0;
wim 0:201dfacbe0e5 252 _columns = LEDKEY8_NR_DIGITS;
wim 0:201dfacbe0e5 253 }
wim 0:201dfacbe0e5 254
wim 0:201dfacbe0e5 255 #if(0)
wim 0:201dfacbe0e5 256 #if DOXYGEN_ONLY
wim 0:201dfacbe0e5 257 /** Write a character to the Display
wim 0:201dfacbe0e5 258 *
wim 0:201dfacbe0e5 259 * @param c The character to write to the display
wim 0:201dfacbe0e5 260 */
wim 0:201dfacbe0e5 261 int putc(int c);
wim 0:201dfacbe0e5 262
wim 0:201dfacbe0e5 263 /** Write a formatted string to the Display
wim 0:201dfacbe0e5 264 *
wim 0:201dfacbe0e5 265 * @param format A printf-style format string, followed by the
wim 0:201dfacbe0e5 266 * variables to use in formatting the string.
wim 0:201dfacbe0e5 267 */
wim 0:201dfacbe0e5 268 int printf(const char* format, ...);
wim 0:201dfacbe0e5 269 #endif
wim 0:201dfacbe0e5 270 #endif
wim 0:201dfacbe0e5 271
wim 0:201dfacbe0e5 272 /** Locate cursor to a screen column
wim 0:201dfacbe0e5 273 *
wim 0:201dfacbe0e5 274 * @param column The horizontal position from the left, indexed from 0
wim 0:201dfacbe0e5 275 */
wim 0:201dfacbe0e5 276 void TM1638_LEDKEY8::locate(int column) {
wim 0:201dfacbe0e5 277 //sanity check
wim 0:201dfacbe0e5 278 if (column < 0) {column = 0;}
wim 0:201dfacbe0e5 279 if (column > (_columns - 1)) {column = _columns - 1;}
wim 0:201dfacbe0e5 280
wim 0:201dfacbe0e5 281 _column = column;
wim 0:201dfacbe0e5 282 }
wim 0:201dfacbe0e5 283
wim 0:201dfacbe0e5 284
wim 0:201dfacbe0e5 285 /** Number of screen columns
wim 0:201dfacbe0e5 286 *
wim 0:201dfacbe0e5 287 * @param none
wim 0:201dfacbe0e5 288 * @return columns
wim 0:201dfacbe0e5 289 */
wim 0:201dfacbe0e5 290 int TM1638_LEDKEY8::columns() {
wim 0:201dfacbe0e5 291 return _columns;
wim 0:201dfacbe0e5 292 }
wim 0:201dfacbe0e5 293
wim 0:201dfacbe0e5 294
wim 0:201dfacbe0e5 295 /** Clear the screen and locate to 0
wim 0:201dfacbe0e5 296 * @param bool clrAll Clear Icons also (default = false)
wim 0:201dfacbe0e5 297 */
wim 0:201dfacbe0e5 298 void TM1638_LEDKEY8::cls(bool clrAll) {
wim 0:201dfacbe0e5 299
wim 0:201dfacbe0e5 300 if (clrAll) {
wim 0:201dfacbe0e5 301 //clear local buffer (including Icons)
wim 0:201dfacbe0e5 302 for (int idx=0; idx < (LEDKEY8_NR_GRIDS << 1); idx++) {
wim 0:201dfacbe0e5 303 _displaybuffer[idx] = 0x00;
wim 0:201dfacbe0e5 304 }
wim 0:201dfacbe0e5 305 }
wim 0:201dfacbe0e5 306 else {
wim 0:201dfacbe0e5 307 //clear local buffer (preserving Icons)
wim 0:201dfacbe0e5 308 for (int idx=0; idx < LEDKEY8_NR_GRIDS; idx++) {
wim 0:201dfacbe0e5 309 _displaybuffer[(idx<<1)] = _displaybuffer[(idx<<1)] & MASK_ICON_GRID[idx][0];
wim 0:201dfacbe0e5 310 _displaybuffer[(idx<<1) + 1] = _displaybuffer[(idx<<1) + 1] & MASK_ICON_GRID[idx][1];
wim 0:201dfacbe0e5 311 }
wim 0:201dfacbe0e5 312 }
wim 0:201dfacbe0e5 313
wim 2:532ce15ea9ec 314 writeData(_displaybuffer, (LEDKEY8_NR_GRIDS * TM1638_BYTES_PER_GRID));
wim 0:201dfacbe0e5 315
wim 0:201dfacbe0e5 316 _column = 0;
wim 0:201dfacbe0e5 317 }
wim 0:201dfacbe0e5 318
wim 0:201dfacbe0e5 319 /** Set Icon
wim 0:201dfacbe0e5 320 *
wim 1:1f2453ed85d7 321 * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 16 LSBs
wim 0:201dfacbe0e5 322 * @return none
wim 0:201dfacbe0e5 323 */
wim 0:201dfacbe0e5 324 void TM1638_LEDKEY8::setIcon(Icon icon) {
wim 0:201dfacbe0e5 325 int addr, icn;
wim 0:201dfacbe0e5 326
wim 1:1f2453ed85d7 327 icn = icon & 0xFFFF;
wim 0:201dfacbe0e5 328 addr = (icon >> 24) & 0xFF;
wim 4:b2bbdc58967e 329 addr = (addr - 1) << 1; // * TM1638_BYTES_PER_GRID
wim 0:201dfacbe0e5 330
wim 0:201dfacbe0e5 331 //Save char...and set bits for icon to write
wim 0:201dfacbe0e5 332 _displaybuffer[addr] = _displaybuffer[addr] | LO(icn);
wim 0:201dfacbe0e5 333 _displaybuffer[addr+1] = _displaybuffer[addr+1] | HI(icn);
wim 4:b2bbdc58967e 334 // writeData(_displaybuffer, (LEDKEY8_NR_GRIDS * TM1638_BYTES_PER_GRID));
wim 4:b2bbdc58967e 335 writeData(_displaybuffer, TM1638_BYTES_PER_GRID, addr);
wim 0:201dfacbe0e5 336 }
wim 0:201dfacbe0e5 337
wim 0:201dfacbe0e5 338 /** Clr Icon
wim 0:201dfacbe0e5 339 *
wim 1:1f2453ed85d7 340 * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 16 LSBs
wim 0:201dfacbe0e5 341 * @return none
wim 0:201dfacbe0e5 342 */
wim 0:201dfacbe0e5 343 void TM1638_LEDKEY8::clrIcon(Icon icon) {
wim 0:201dfacbe0e5 344 int addr, icn;
wim 0:201dfacbe0e5 345
wim 1:1f2453ed85d7 346 icn = icon & 0xFFFF;
wim 0:201dfacbe0e5 347 addr = (icon >> 24) & 0xFF;
wim 2:532ce15ea9ec 348 addr = (addr - 1) << 1; // * TM1638_BYTES_PER_GRID
wim 0:201dfacbe0e5 349
wim 0:201dfacbe0e5 350 //Save char...and clr bits for icon to write
wim 0:201dfacbe0e5 351 _displaybuffer[addr] = _displaybuffer[addr] & ~LO(icn);
wim 0:201dfacbe0e5 352 _displaybuffer[addr+1] = _displaybuffer[addr+1] & ~HI(icn);
wim 4:b2bbdc58967e 353 // writeData(_displaybuffer, (LEDKEY8_NR_GRIDS * TM1638_BYTES_PER_GRID));
wim 4:b2bbdc58967e 354 writeData(_displaybuffer, TM1638_BYTES_PER_GRID, addr);
wim 0:201dfacbe0e5 355 }
wim 0:201dfacbe0e5 356
wim 0:201dfacbe0e5 357
wim 0:201dfacbe0e5 358 /** Set User Defined Characters (UDC)
wim 0:201dfacbe0e5 359 *
wim 0:201dfacbe0e5 360 * @param unsigned char udc_idx The Index of the UDC (0..7)
wim 0:201dfacbe0e5 361 * @param int udc_data The bitpattern for the UDC (8 bits)
wim 0:201dfacbe0e5 362 */
wim 0:201dfacbe0e5 363 void TM1638_LEDKEY8::setUDC(unsigned char udc_idx, int udc_data) {
wim 0:201dfacbe0e5 364
wim 0:201dfacbe0e5 365 //Sanity check
wim 0:201dfacbe0e5 366 if (udc_idx > (LEDKEY8_NR_UDC-1)) {
wim 0:201dfacbe0e5 367 return;
wim 0:201dfacbe0e5 368 }
wim 0:201dfacbe0e5 369 // Mask out Icon bits?
wim 0:201dfacbe0e5 370
wim 0:201dfacbe0e5 371 _UDC_7S[udc_idx] = LO(udc_data);
wim 0:201dfacbe0e5 372 }
wim 0:201dfacbe0e5 373
wim 0:201dfacbe0e5 374
wim 0:201dfacbe0e5 375 /** Write a single character (Stream implementation)
wim 0:201dfacbe0e5 376 */
wim 0:201dfacbe0e5 377 int TM1638_LEDKEY8::_putc(int value) {
wim 0:201dfacbe0e5 378 int addr;
wim 0:201dfacbe0e5 379 bool validChar = false;
wim 0:201dfacbe0e5 380 char pattern = 0x00;
wim 0:201dfacbe0e5 381
wim 0:201dfacbe0e5 382 if ((value == '\n') || (value == '\r')) {
wim 0:201dfacbe0e5 383 //No character to write
wim 0:201dfacbe0e5 384 validChar = false;
wim 0:201dfacbe0e5 385
wim 0:201dfacbe0e5 386 //Update Cursor
wim 0:201dfacbe0e5 387 _column = 0;
wim 0:201dfacbe0e5 388 }
wim 1:1f2453ed85d7 389 else if ((value == '.') || (value == ',')) {
wim 1:1f2453ed85d7 390 //No character to write
wim 1:1f2453ed85d7 391 validChar = false;
wim 1:1f2453ed85d7 392 pattern = S7_DP; // placeholder for all DPs
wim 1:1f2453ed85d7 393
wim 1:1f2453ed85d7 394 // Check to see that DP can be shown for current column
wim 1:1f2453ed85d7 395 if (_column > 0) {
wim 1:1f2453ed85d7 396 //Translate between _column and displaybuffer entries
wim 1:1f2453ed85d7 397 //Add DP to bitpattern of digit left of current column.
wim 4:b2bbdc58967e 398 addr = (_column - 1) << 1; // * TM1638_BYTES_PER_GRID
wim 1:1f2453ed85d7 399
wim 1:1f2453ed85d7 400 //Save icons...and set bits for decimal point to write
wim 1:1f2453ed85d7 401 _displaybuffer[addr] = _displaybuffer[addr] | pattern;
wim 1:1f2453ed85d7 402 // _displaybuffer[addr+1] = _displaybuffer[addr+1] | pattern;
wim 1:1f2453ed85d7 403
wim 4:b2bbdc58967e 404 // writeData(_displaybuffer, (LEDKEY8_NR_GRIDS * TM1638_BYTES_PER_GRID));
wim 4:b2bbdc58967e 405 writeData(_displaybuffer, TM1638_BYTES_PER_GRID, addr);
wim 4:b2bbdc58967e 406
wim 1:1f2453ed85d7 407 //No Cursor Update
wim 1:1f2453ed85d7 408 }
wim 1:1f2453ed85d7 409 }
wim 0:201dfacbe0e5 410 else if ((value >= 0) && (value < LEDKEY8_NR_UDC)) {
wim 0:201dfacbe0e5 411 //Character to write
wim 0:201dfacbe0e5 412 validChar = true;
wim 0:201dfacbe0e5 413 pattern = _UDC_7S[value];
wim 0:201dfacbe0e5 414 }
wim 2:532ce15ea9ec 415
wim 2:532ce15ea9ec 416 #if (SHOW_ASCII == 1)
wim 2:532ce15ea9ec 417 //display all ASCII characters
wim 2:532ce15ea9ec 418 else if ((value >= FONT_7S_START) && (value <= FONT_7S_END)) {
wim 2:532ce15ea9ec 419 //Character to write
wim 2:532ce15ea9ec 420 validChar = true;
wim 2:532ce15ea9ec 421 pattern = FONT_7S[value - FONT_7S_START];
wim 2:532ce15ea9ec 422 } // else
wim 2:532ce15ea9ec 423 #else
wim 2:532ce15ea9ec 424 //display only digits and hex characters
wim 2:532ce15ea9ec 425 else if (value == '-') {
wim 2:532ce15ea9ec 426 //Character to write
wim 2:532ce15ea9ec 427 validChar = true;
wim 2:532ce15ea9ec 428 pattern = C7_MIN;
wim 2:532ce15ea9ec 429 }
wim 0:201dfacbe0e5 430 else if ((value >= (int)'0') && (value <= (int) '9')) {
wim 0:201dfacbe0e5 431 //Character to write
wim 0:201dfacbe0e5 432 validChar = true;
wim 0:201dfacbe0e5 433 pattern = FONT_7S[value - (int) '0'];
wim 0:201dfacbe0e5 434 }
wim 0:201dfacbe0e5 435 else if ((value >= (int) 'A') && (value <= (int) 'F')) {
wim 0:201dfacbe0e5 436 //Character to write
wim 0:201dfacbe0e5 437 validChar = true;
wim 0:201dfacbe0e5 438 pattern = FONT_7S[10 + value - (int) 'A'];
wim 0:201dfacbe0e5 439 }
wim 0:201dfacbe0e5 440 else if ((value >= (int) 'a') && (value <= (int) 'f')) {
wim 0:201dfacbe0e5 441 //Character to write
wim 0:201dfacbe0e5 442 validChar = true;
wim 0:201dfacbe0e5 443 pattern = FONT_7S[10 + value - (int) 'a'];
wim 0:201dfacbe0e5 444 } //else
wim 2:532ce15ea9ec 445 #endif
wim 0:201dfacbe0e5 446
wim 0:201dfacbe0e5 447 if (validChar) {
wim 0:201dfacbe0e5 448 //Character to write
wim 0:201dfacbe0e5 449
wim 0:201dfacbe0e5 450 //Translate between _column and displaybuffer entries
wim 2:532ce15ea9ec 451 addr = _column << 1; // * TM1638_BYTES_PER_GRID
wim 0:201dfacbe0e5 452
wim 0:201dfacbe0e5 453 //Save icons...and set bits for character to write
wim 0:201dfacbe0e5 454 _displaybuffer[addr] = (_displaybuffer[addr] & MASK_ICON_GRID[_column][0]) | pattern;
wim 0:201dfacbe0e5 455 // _displaybuffer[addr+1] = (_displaybuffer[addr+1] & MASK_ICON_GRID[_column][0]) | pattern;
wim 0:201dfacbe0e5 456
wim 4:b2bbdc58967e 457 // writeData(_displaybuffer, (LEDKEY8_NR_GRIDS * TM1638_BYTES_PER_GRID));
wim 4:b2bbdc58967e 458 writeData(_displaybuffer, TM1638_BYTES_PER_GRID, addr);
wim 4:b2bbdc58967e 459
wim 0:201dfacbe0e5 460 //Update Cursor
wim 0:201dfacbe0e5 461 _column++;
wim 0:201dfacbe0e5 462 if (_column > (LEDKEY8_NR_DIGITS - 1)) {
wim 0:201dfacbe0e5 463 _column = 0;
wim 0:201dfacbe0e5 464 }
wim 0:201dfacbe0e5 465
wim 0:201dfacbe0e5 466 } // if validChar
wim 0:201dfacbe0e5 467
wim 0:201dfacbe0e5 468 return value;
wim 0:201dfacbe0e5 469 }
wim 0:201dfacbe0e5 470
wim 0:201dfacbe0e5 471
wim 0:201dfacbe0e5 472 // get a single character (Stream implementation)
wim 0:201dfacbe0e5 473 int TM1638_LEDKEY8::_getc() {
wim 0:201dfacbe0e5 474 return -1;
wim 0:201dfacbe0e5 475 }
wim 0:201dfacbe0e5 476
wim 0:201dfacbe0e5 477 #endif
wim 4:b2bbdc58967e 478
wim 4:b2bbdc58967e 479
wim 4:b2bbdc58967e 480 #if (QYF_TEST == 1)
wim 4:b2bbdc58967e 481 // Derived class for TM1638 used in QYF-TM1638 display unit
wim 4:b2bbdc58967e 482 //
wim 4:b2bbdc58967e 483
wim 4:b2bbdc58967e 484 /** Constructor for class for driving TM1638 LED controller as used in QYF
wim 4:b2bbdc58967e 485 *
wim 4:b2bbdc58967e 486 * @brief Supports 8 Digits of 7 Segments + DP. Also supports a scanned keyboard of 16 keys.
wim 4:b2bbdc58967e 487 *
wim 4:b2bbdc58967e 488 * @param PinName mosi, miso, sclk, cs SPI bus pins
wim 4:b2bbdc58967e 489 */
wim 4:b2bbdc58967e 490 TM1638_QYF::TM1638_QYF(PinName mosi, PinName miso, PinName sclk, PinName cs) : TM1638(mosi, miso, sclk, cs) {
wim 4:b2bbdc58967e 491 _column = 0;
wim 4:b2bbdc58967e 492 _columns = QYF_NR_DIGITS;
wim 4:b2bbdc58967e 493 }
wim 4:b2bbdc58967e 494
wim 4:b2bbdc58967e 495 #if(0)
wim 4:b2bbdc58967e 496 #if DOXYGEN_ONLY
wim 4:b2bbdc58967e 497 /** Write a character to the Display
wim 4:b2bbdc58967e 498 *
wim 4:b2bbdc58967e 499 * @param c The character to write to the display
wim 4:b2bbdc58967e 500 */
wim 4:b2bbdc58967e 501 int putc(int c);
wim 4:b2bbdc58967e 502
wim 4:b2bbdc58967e 503 /** Write a formatted string to the Display
wim 4:b2bbdc58967e 504 *
wim 4:b2bbdc58967e 505 * @param format A printf-style format string, followed by the
wim 4:b2bbdc58967e 506 * variables to use in formatting the string.
wim 4:b2bbdc58967e 507 */
wim 4:b2bbdc58967e 508 int printf(const char* format, ...);
wim 4:b2bbdc58967e 509 #endif
wim 4:b2bbdc58967e 510 #endif
wim 4:b2bbdc58967e 511
wim 4:b2bbdc58967e 512 /** Locate cursor to a screen column
wim 4:b2bbdc58967e 513 *
wim 4:b2bbdc58967e 514 * @param column The horizontal position from the left, indexed from 0
wim 4:b2bbdc58967e 515 */
wim 4:b2bbdc58967e 516 void TM1638_QYF::locate(int column) {
wim 4:b2bbdc58967e 517 //sanity check
wim 4:b2bbdc58967e 518 if (column < 0) {column = 0;}
wim 4:b2bbdc58967e 519 if (column > (_columns - 1)) {column = _columns - 1;}
wim 4:b2bbdc58967e 520
wim 4:b2bbdc58967e 521 _column = column;
wim 4:b2bbdc58967e 522 }
wim 4:b2bbdc58967e 523
wim 4:b2bbdc58967e 524
wim 4:b2bbdc58967e 525 /** Number of screen columns
wim 4:b2bbdc58967e 526 *
wim 4:b2bbdc58967e 527 * @param none
wim 4:b2bbdc58967e 528 * @return columns
wim 4:b2bbdc58967e 529 */
wim 4:b2bbdc58967e 530 int TM1638_QYF::columns() {
wim 4:b2bbdc58967e 531 return _columns;
wim 4:b2bbdc58967e 532 }
wim 4:b2bbdc58967e 533
wim 4:b2bbdc58967e 534
wim 4:b2bbdc58967e 535 /** Clear the screen and locate to 0
wim 4:b2bbdc58967e 536 * @param bool clrAll Clear Icons also (default = false)
wim 4:b2bbdc58967e 537 */
wim 4:b2bbdc58967e 538 void TM1638_QYF::cls(bool clrAll) {
wim 4:b2bbdc58967e 539
wim 4:b2bbdc58967e 540 if (clrAll) {
wim 4:b2bbdc58967e 541 //clear local buffer (including Icons)
wim 4:b2bbdc58967e 542 for (int idx=0; idx < (QYF_NR_GRIDS << 1); idx++) {
wim 4:b2bbdc58967e 543 _displaybuffer[idx] = 0x00;
wim 4:b2bbdc58967e 544 }
wim 4:b2bbdc58967e 545 }
wim 4:b2bbdc58967e 546 else {
wim 4:b2bbdc58967e 547 //clear local buffer (preserving Icons)
wim 4:b2bbdc58967e 548 for (int idx=0; idx < QYF_NR_GRIDS; idx++) {
wim 4:b2bbdc58967e 549 _displaybuffer[(idx<<1)] = _displaybuffer[(idx<<1)] & MASK_ICON_GRID[idx][0];
wim 4:b2bbdc58967e 550 _displaybuffer[(idx<<1) + 1] = _displaybuffer[(idx<<1) + 1] & MASK_ICON_GRID[idx][1];
wim 4:b2bbdc58967e 551 }
wim 4:b2bbdc58967e 552 }
wim 4:b2bbdc58967e 553
wim 4:b2bbdc58967e 554 writeData(_displaybuffer, (QYF_NR_GRIDS * TM1638_BYTES_PER_GRID));
wim 4:b2bbdc58967e 555
wim 4:b2bbdc58967e 556 _column = 0;
wim 4:b2bbdc58967e 557 }
wim 4:b2bbdc58967e 558
wim 4:b2bbdc58967e 559 /** Set Icon
wim 4:b2bbdc58967e 560 *
wim 4:b2bbdc58967e 561 * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 16 LSBs
wim 4:b2bbdc58967e 562 * @return none
wim 4:b2bbdc58967e 563 */
wim 4:b2bbdc58967e 564 void TM1638_QYF::setIcon(Icon icon) {
wim 4:b2bbdc58967e 565 int addr, icn;
wim 4:b2bbdc58967e 566
wim 4:b2bbdc58967e 567 icn = icon & 0xFFFF;
wim 4:b2bbdc58967e 568 addr = (icon >> 24) & 0xFF;
wim 4:b2bbdc58967e 569 addr = (addr - 1) << 1; // * TM1638_BYTES_PER_GRID
wim 4:b2bbdc58967e 570
wim 4:b2bbdc58967e 571 //Save char...and set bits for icon to write
wim 4:b2bbdc58967e 572 _displaybuffer[addr] = _displaybuffer[addr] | LO(icn);
wim 4:b2bbdc58967e 573 _displaybuffer[addr+1] = _displaybuffer[addr+1] | HI(icn);
wim 4:b2bbdc58967e 574 // writeData(_displaybuffer, (QYF_NR_GRIDS * TM1638_BYTES_PER_GRID));
wim 4:b2bbdc58967e 575 writeData(_displaybuffer, TM1638_BYTES_PER_GRID, addr);
wim 4:b2bbdc58967e 576 }
wim 4:b2bbdc58967e 577
wim 4:b2bbdc58967e 578 /** Clr Icon
wim 4:b2bbdc58967e 579 *
wim 4:b2bbdc58967e 580 * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 16 LSBs
wim 4:b2bbdc58967e 581 * @return none
wim 4:b2bbdc58967e 582 */
wim 4:b2bbdc58967e 583 void TM1638_QYF::clrIcon(Icon icon) {
wim 4:b2bbdc58967e 584 int addr, icn;
wim 4:b2bbdc58967e 585
wim 4:b2bbdc58967e 586 icn = icon & 0xFFFF;
wim 4:b2bbdc58967e 587 addr = (icon >> 24) & 0xFF;
wim 4:b2bbdc58967e 588 addr = (addr - 1) << 1; // * TM1638_BYTES_PER_GRID
wim 4:b2bbdc58967e 589
wim 4:b2bbdc58967e 590 //Save char...and clr bits for icon to write
wim 4:b2bbdc58967e 591 _displaybuffer[addr] = _displaybuffer[addr] & ~LO(icn);
wim 4:b2bbdc58967e 592 _displaybuffer[addr+1] = _displaybuffer[addr+1] & ~HI(icn);
wim 4:b2bbdc58967e 593 // writeData(_displaybuffer, (QYF_NR_GRIDS * TM1638_BYTES_PER_GRID));
wim 4:b2bbdc58967e 594 writeData(_displaybuffer, TM1638_BYTES_PER_GRID, addr);
wim 4:b2bbdc58967e 595 }
wim 4:b2bbdc58967e 596
wim 4:b2bbdc58967e 597
wim 4:b2bbdc58967e 598 /** Set User Defined Characters (UDC)
wim 4:b2bbdc58967e 599 *
wim 4:b2bbdc58967e 600 * @param unsigned char udc_idx The Index of the UDC (0..7)
wim 4:b2bbdc58967e 601 * @param int udc_data The bitpattern for the UDC (8 bits)
wim 4:b2bbdc58967e 602 */
wim 4:b2bbdc58967e 603 void TM1638_QYF::setUDC(unsigned char udc_idx, int udc_data) {
wim 4:b2bbdc58967e 604
wim 4:b2bbdc58967e 605 //Sanity check
wim 4:b2bbdc58967e 606 if (udc_idx > (QYF_NR_UDC-1)) {
wim 4:b2bbdc58967e 607 return;
wim 4:b2bbdc58967e 608 }
wim 4:b2bbdc58967e 609 // Mask out Icon bits?
wim 4:b2bbdc58967e 610
wim 4:b2bbdc58967e 611 _UDC_7S[udc_idx] = LO(udc_data);
wim 4:b2bbdc58967e 612 }
wim 4:b2bbdc58967e 613
wim 4:b2bbdc58967e 614
wim 4:b2bbdc58967e 615 /** Write a single character (Stream implementation)
wim 4:b2bbdc58967e 616 */
wim 4:b2bbdc58967e 617 int TM1638_QYF::_putc(int value) {
wim 4:b2bbdc58967e 618 bool validChar = false;
wim 4:b2bbdc58967e 619 char pattern = 0x00;
wim 4:b2bbdc58967e 620 char bit = 0x00;
wim 4:b2bbdc58967e 621
wim 4:b2bbdc58967e 622 if ((value == '\n') || (value == '\r')) {
wim 4:b2bbdc58967e 623 //No character to write
wim 4:b2bbdc58967e 624 validChar = false;
wim 4:b2bbdc58967e 625
wim 4:b2bbdc58967e 626 //Update Cursor
wim 4:b2bbdc58967e 627 _column = 0;
wim 4:b2bbdc58967e 628 }
wim 4:b2bbdc58967e 629 else if ((value == '.') || (value == ',')) {
wim 4:b2bbdc58967e 630 //No character to write
wim 4:b2bbdc58967e 631 validChar = false;
wim 4:b2bbdc58967e 632 pattern = S7_DP; // placeholder for all DPs
wim 4:b2bbdc58967e 633
wim 4:b2bbdc58967e 634 // Check to see that DP can be shown for current column
wim 4:b2bbdc58967e 635 if (_column > 0) {
wim 4:b2bbdc58967e 636 //Add DP to bitpattern of digit left of current column.
wim 4:b2bbdc58967e 637 bit = 1 << (8 - _column); // bitposition for the previous _column
wim 4:b2bbdc58967e 638
wim 4:b2bbdc58967e 639 _displaybuffer[14] = (_displaybuffer[14] | bit); // set bit
wim 4:b2bbdc58967e 640
wim 4:b2bbdc58967e 641 writeData(_displaybuffer, (QYF_NR_GRIDS * TM1638_BYTES_PER_GRID));
wim 4:b2bbdc58967e 642
wim 4:b2bbdc58967e 643 //No Cursor Update
wim 4:b2bbdc58967e 644 }
wim 4:b2bbdc58967e 645 }
wim 4:b2bbdc58967e 646 else if ((value >= 0) && (value < QYF_NR_UDC)) {
wim 4:b2bbdc58967e 647 //Character to write
wim 4:b2bbdc58967e 648 validChar = true;
wim 4:b2bbdc58967e 649 pattern = _UDC_7S[value];
wim 4:b2bbdc58967e 650 }
wim 4:b2bbdc58967e 651
wim 4:b2bbdc58967e 652 #if (SHOW_ASCII == 1)
wim 4:b2bbdc58967e 653 //display all ASCII characters
wim 4:b2bbdc58967e 654 else if ((value >= FONT_7S_START) && (value <= FONT_7S_END)) {
wim 4:b2bbdc58967e 655 //Character to write
wim 4:b2bbdc58967e 656 validChar = true;
wim 4:b2bbdc58967e 657 pattern = FONT_7S[value - FONT_7S_START];
wim 4:b2bbdc58967e 658 } // else
wim 4:b2bbdc58967e 659 #else
wim 4:b2bbdc58967e 660 //display only digits and hex characters
wim 4:b2bbdc58967e 661 else if (value == '-') {
wim 4:b2bbdc58967e 662 //Character to write
wim 4:b2bbdc58967e 663 validChar = true;
wim 4:b2bbdc58967e 664 pattern = C7_MIN;
wim 4:b2bbdc58967e 665 }
wim 4:b2bbdc58967e 666 else if ((value >= (int)'0') && (value <= (int) '9')) {
wim 4:b2bbdc58967e 667 //Character to write
wim 4:b2bbdc58967e 668 validChar = true;
wim 4:b2bbdc58967e 669 pattern = FONT_7S[value - (int) '0'];
wim 4:b2bbdc58967e 670 }
wim 4:b2bbdc58967e 671 else if ((value >= (int) 'A') && (value <= (int) 'F')) {
wim 4:b2bbdc58967e 672 //Character to write
wim 4:b2bbdc58967e 673 validChar = true;
wim 4:b2bbdc58967e 674 pattern = FONT_7S[10 + value - (int) 'A'];
wim 4:b2bbdc58967e 675 }
wim 4:b2bbdc58967e 676 else if ((value >= (int) 'a') && (value <= (int) 'f')) {
wim 4:b2bbdc58967e 677 //Character to write
wim 4:b2bbdc58967e 678 validChar = true;
wim 4:b2bbdc58967e 679 pattern = FONT_7S[10 + value - (int) 'a'];
wim 4:b2bbdc58967e 680 } //else
wim 4:b2bbdc58967e 681 #endif
wim 4:b2bbdc58967e 682
wim 4:b2bbdc58967e 683 if (validChar) {
wim 4:b2bbdc58967e 684 //Character to write
wim 4:b2bbdc58967e 685
wim 4:b2bbdc58967e 686 // Very annoying bitmapping :(
wim 4:b2bbdc58967e 687 // This display module uses a single byte of each grid to drive a specific segment of all digits.
wim 4:b2bbdc58967e 688 // So the bits in byte 0 (Grid 1) drive all A-segments, the bits in byte 2 (Grid 2) drive all B-segments etc.
wim 4:b2bbdc58967e 689 // Bit0 is for the segment in Digit 8, Bit1 is for the segment in Digit 7 etc.. This bit manipulation is handled in _putc().
wim 4:b2bbdc58967e 690
wim 4:b2bbdc58967e 691 bit = 1 << (7 - _column); // bitposition for the current _column
wim 4:b2bbdc58967e 692
wim 4:b2bbdc58967e 693 if (pattern & S7_A) {_displaybuffer[0] = (_displaybuffer[0] | bit); } // set bit
wim 4:b2bbdc58967e 694 else {_displaybuffer[0] = (_displaybuffer[0] & ~bit);} // clr bit
wim 4:b2bbdc58967e 695
wim 4:b2bbdc58967e 696 if (pattern & S7_B) {_displaybuffer[2] = (_displaybuffer[2] | bit); } // set bit
wim 4:b2bbdc58967e 697 else {_displaybuffer[2] = (_displaybuffer[2] & ~bit);} // clr bit
wim 4:b2bbdc58967e 698
wim 4:b2bbdc58967e 699 if (pattern & S7_C) {_displaybuffer[4] = (_displaybuffer[4] | bit); } // set bit
wim 4:b2bbdc58967e 700 else {_displaybuffer[4] = (_displaybuffer[4] & ~bit);} // clr bit
wim 4:b2bbdc58967e 701
wim 4:b2bbdc58967e 702 if (pattern & S7_D) {_displaybuffer[6] = (_displaybuffer[6] | bit); } // set bit
wim 4:b2bbdc58967e 703 else {_displaybuffer[6] = (_displaybuffer[6] & ~bit);} // clr bit
wim 4:b2bbdc58967e 704
wim 4:b2bbdc58967e 705 if (pattern & S7_E) {_displaybuffer[8] = (_displaybuffer[8] | bit); } // set bit
wim 4:b2bbdc58967e 706 else {_displaybuffer[8] = (_displaybuffer[8] & ~bit);} // clr bit
wim 4:b2bbdc58967e 707
wim 4:b2bbdc58967e 708 if (pattern & S7_F) {_displaybuffer[10] = (_displaybuffer[10] | bit); } // set bit
wim 4:b2bbdc58967e 709 else {_displaybuffer[10] = (_displaybuffer[10] & ~bit);} // clr bit
wim 4:b2bbdc58967e 710
wim 4:b2bbdc58967e 711 if (pattern & S7_G) {_displaybuffer[12] = (_displaybuffer[12] | bit); } // set bit
wim 4:b2bbdc58967e 712 else {_displaybuffer[12] = (_displaybuffer[12] & ~bit);} // clr bit
wim 4:b2bbdc58967e 713
wim 4:b2bbdc58967e 714 if (pattern & S7_DP) {_displaybuffer[14] = (_displaybuffer[14] | bit); } // set bit
wim 4:b2bbdc58967e 715 else {_displaybuffer[14] = (_displaybuffer[14] & ~bit);} // clr bit
wim 4:b2bbdc58967e 716
wim 4:b2bbdc58967e 717 //Save icons...and set bits for character to write
wim 4:b2bbdc58967e 718 // _displaybuffer[addr] = (_displaybuffer[addr] & MASK_ICON_GRID[_column][0]) | pattern;
wim 4:b2bbdc58967e 719 // _displaybuffer[addr+1] = (_displaybuffer[addr+1] & MASK_ICON_GRID[_column][0]) | pattern;
wim 4:b2bbdc58967e 720
wim 4:b2bbdc58967e 721 writeData(_displaybuffer, (QYF_NR_GRIDS * TM1638_BYTES_PER_GRID));
wim 4:b2bbdc58967e 722
wim 4:b2bbdc58967e 723 //Update Cursor
wim 4:b2bbdc58967e 724 _column++;
wim 4:b2bbdc58967e 725 if (_column > (QYF_NR_DIGITS - 1)) {
wim 4:b2bbdc58967e 726 _column = 0;
wim 4:b2bbdc58967e 727 }
wim 4:b2bbdc58967e 728
wim 4:b2bbdc58967e 729 } // if validChar
wim 4:b2bbdc58967e 730
wim 4:b2bbdc58967e 731 return value;
wim 4:b2bbdc58967e 732 }
wim 4:b2bbdc58967e 733
wim 4:b2bbdc58967e 734
wim 4:b2bbdc58967e 735 // get a single character (Stream implementation)
wim 4:b2bbdc58967e 736 int TM1638_QYF::_getc() {
wim 4:b2bbdc58967e 737 return -1;
wim 4:b2bbdc58967e 738 }
wim 4:b2bbdc58967e 739
wim 4:b2bbdc58967e 740 #endif
wim 4:b2bbdc58967e 741
wim 4:b2bbdc58967e 742
wim 4:b2bbdc58967e 743
wim 4:b2bbdc58967e 744 #if (LKM1638_TEST == 1)
wim 4:b2bbdc58967e 745 // Derived class for TM1638 used in LMK1638 display unit
wim 4:b2bbdc58967e 746 //
wim 4:b2bbdc58967e 747
wim 4:b2bbdc58967e 748 /** Constructor for class for driving TM1638 LED controller as used in LKM1638
wim 4:b2bbdc58967e 749 *
wim 4:b2bbdc58967e 750 * @brief Supports 8 Digits of 7 Segments + DP + Bi-Color LED Icons. Also supports a scanned keyboard of 8.
wim 4:b2bbdc58967e 751 *
wim 4:b2bbdc58967e 752 * @param PinName mosi, miso, sclk, cs SPI bus pins
wim 4:b2bbdc58967e 753 */
wim 4:b2bbdc58967e 754 TM1638_LKM1638::TM1638_LKM1638(PinName mosi, PinName miso, PinName sclk, PinName cs) : TM1638(mosi, miso, sclk, cs) {
wim 4:b2bbdc58967e 755 _column = 0;
wim 4:b2bbdc58967e 756 _columns = LKM1638_NR_DIGITS;
wim 4:b2bbdc58967e 757 }
wim 4:b2bbdc58967e 758
wim 4:b2bbdc58967e 759 #if(0)
wim 4:b2bbdc58967e 760 #if DOXYGEN_ONLY
wim 4:b2bbdc58967e 761 /** Write a character to the Display
wim 4:b2bbdc58967e 762 *
wim 4:b2bbdc58967e 763 * @param c The character to write to the display
wim 4:b2bbdc58967e 764 */
wim 4:b2bbdc58967e 765 int putc(int c);
wim 4:b2bbdc58967e 766
wim 4:b2bbdc58967e 767 /** Write a formatted string to the Display
wim 4:b2bbdc58967e 768 *
wim 4:b2bbdc58967e 769 * @param format A printf-style format string, followed by the
wim 4:b2bbdc58967e 770 * variables to use in formatting the string.
wim 4:b2bbdc58967e 771 */
wim 4:b2bbdc58967e 772 int printf(const char* format, ...);
wim 4:b2bbdc58967e 773 #endif
wim 4:b2bbdc58967e 774 #endif
wim 4:b2bbdc58967e 775
wim 4:b2bbdc58967e 776 /** Locate cursor to a screen column
wim 4:b2bbdc58967e 777 *
wim 4:b2bbdc58967e 778 * @param column The horizontal position from the left, indexed from 0
wim 4:b2bbdc58967e 779 */
wim 4:b2bbdc58967e 780 void TM1638_LKM1638::locate(int column) {
wim 4:b2bbdc58967e 781 //sanity check
wim 4:b2bbdc58967e 782 if (column < 0) {column = 0;}
wim 4:b2bbdc58967e 783 if (column > (_columns - 1)) {column = _columns - 1;}
wim 4:b2bbdc58967e 784
wim 4:b2bbdc58967e 785 _column = column;
wim 4:b2bbdc58967e 786 }
wim 4:b2bbdc58967e 787
wim 4:b2bbdc58967e 788
wim 4:b2bbdc58967e 789 /** Number of screen columns
wim 4:b2bbdc58967e 790 *
wim 4:b2bbdc58967e 791 * @param none
wim 4:b2bbdc58967e 792 * @return columns
wim 4:b2bbdc58967e 793 */
wim 4:b2bbdc58967e 794 int TM1638_LKM1638::columns() {
wim 4:b2bbdc58967e 795 return _columns;
wim 4:b2bbdc58967e 796 }
wim 4:b2bbdc58967e 797
wim 4:b2bbdc58967e 798
wim 4:b2bbdc58967e 799 /** Clear the screen and locate to 0
wim 4:b2bbdc58967e 800 * @param bool clrAll Clear Icons also (default = false)
wim 4:b2bbdc58967e 801 */
wim 4:b2bbdc58967e 802 void TM1638_LKM1638::cls(bool clrAll) {
wim 4:b2bbdc58967e 803
wim 4:b2bbdc58967e 804 if (clrAll) {
wim 4:b2bbdc58967e 805 //clear local buffer (including Icons)
wim 4:b2bbdc58967e 806 for (int idx=0; idx < (LKM1638_NR_GRIDS << 1); idx++) {
wim 4:b2bbdc58967e 807 _displaybuffer[idx] = 0x00;
wim 4:b2bbdc58967e 808 }
wim 4:b2bbdc58967e 809 }
wim 4:b2bbdc58967e 810 else {
wim 4:b2bbdc58967e 811 //clear local buffer (preserving Icons)
wim 4:b2bbdc58967e 812 for (int idx=0; idx < LKM1638_NR_GRIDS; idx++) {
wim 4:b2bbdc58967e 813 _displaybuffer[(idx<<1)] = _displaybuffer[(idx<<1)] & MASK_ICON_GRID[idx][0];
wim 4:b2bbdc58967e 814 _displaybuffer[(idx<<1) + 1] = _displaybuffer[(idx<<1) + 1] & MASK_ICON_GRID[idx][1];
wim 4:b2bbdc58967e 815 }
wim 4:b2bbdc58967e 816 }
wim 4:b2bbdc58967e 817
wim 4:b2bbdc58967e 818 writeData(_displaybuffer, (LKM1638_NR_GRIDS * TM1638_BYTES_PER_GRID));
wim 4:b2bbdc58967e 819
wim 4:b2bbdc58967e 820 _column = 0;
wim 4:b2bbdc58967e 821 }
wim 4:b2bbdc58967e 822
wim 4:b2bbdc58967e 823 /** Set Icon
wim 4:b2bbdc58967e 824 *
wim 4:b2bbdc58967e 825 * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 16 LSBs
wim 4:b2bbdc58967e 826 * @return none
wim 4:b2bbdc58967e 827 */
wim 4:b2bbdc58967e 828 void TM1638_LKM1638::setIcon(Icon icon) {
wim 4:b2bbdc58967e 829 int addr, icn;
wim 4:b2bbdc58967e 830
wim 4:b2bbdc58967e 831 icn = icon & 0xFFFF;
wim 4:b2bbdc58967e 832 addr = (icon >> 24) & 0xFF;
wim 4:b2bbdc58967e 833 addr = (addr - 1) << 1; // * TM1638_BYTES_PER_GRID
wim 4:b2bbdc58967e 834
wim 4:b2bbdc58967e 835 //Save char...and set bits for icon to write
wim 4:b2bbdc58967e 836 _displaybuffer[addr] = _displaybuffer[addr] | LO(icn);
wim 4:b2bbdc58967e 837 _displaybuffer[addr+1] = _displaybuffer[addr+1] | HI(icn);
wim 4:b2bbdc58967e 838 // writeData(_displaybuffer, (LKM1638_NR_GRIDS * TM1638_BYTES_PER_GRID));
wim 4:b2bbdc58967e 839 writeData(_displaybuffer, TM1638_BYTES_PER_GRID, addr);
wim 4:b2bbdc58967e 840 }
wim 4:b2bbdc58967e 841
wim 4:b2bbdc58967e 842 /** Clr Icon
wim 4:b2bbdc58967e 843 *
wim 4:b2bbdc58967e 844 * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 16 LSBs
wim 4:b2bbdc58967e 845 * @return none
wim 4:b2bbdc58967e 846 */
wim 4:b2bbdc58967e 847 void TM1638_LKM1638::clrIcon(Icon icon) {
wim 4:b2bbdc58967e 848 int addr, icn;
wim 4:b2bbdc58967e 849
wim 4:b2bbdc58967e 850 icn = icon & 0xFFFF;
wim 4:b2bbdc58967e 851 addr = (icon >> 24) & 0xFF;
wim 4:b2bbdc58967e 852 addr = (addr - 1) << 1; // * TM1638_BYTES_PER_GRID
wim 4:b2bbdc58967e 853
wim 4:b2bbdc58967e 854 //Save char...and clr bits for icon to write
wim 4:b2bbdc58967e 855 _displaybuffer[addr] = _displaybuffer[addr] & ~LO(icn);
wim 4:b2bbdc58967e 856 _displaybuffer[addr+1] = _displaybuffer[addr+1] & ~HI(icn);
wim 4:b2bbdc58967e 857 // writeData(_displaybuffer, (LKM1638_NR_GRIDS * TM1638_BYTES_PER_GRID));
wim 4:b2bbdc58967e 858 writeData(_displaybuffer, TM1638_BYTES_PER_GRID, addr);
wim 4:b2bbdc58967e 859 }
wim 4:b2bbdc58967e 860
wim 4:b2bbdc58967e 861
wim 4:b2bbdc58967e 862 /** Set User Defined Characters (UDC)
wim 4:b2bbdc58967e 863 *
wim 4:b2bbdc58967e 864 * @param unsigned char udc_idx The Index of the UDC (0..7)
wim 4:b2bbdc58967e 865 * @param int udc_data The bitpattern for the UDC (8 bits)
wim 4:b2bbdc58967e 866 */
wim 4:b2bbdc58967e 867 void TM1638_LKM1638::setUDC(unsigned char udc_idx, int udc_data) {
wim 4:b2bbdc58967e 868
wim 4:b2bbdc58967e 869 //Sanity check
wim 4:b2bbdc58967e 870 if (udc_idx > (LKM1638_NR_UDC-1)) {
wim 4:b2bbdc58967e 871 return;
wim 4:b2bbdc58967e 872 }
wim 4:b2bbdc58967e 873 // Mask out Icon bits?
wim 4:b2bbdc58967e 874
wim 4:b2bbdc58967e 875 _UDC_7S[udc_idx] = LO(udc_data);
wim 4:b2bbdc58967e 876 }
wim 4:b2bbdc58967e 877
wim 4:b2bbdc58967e 878
wim 4:b2bbdc58967e 879 /** Write a single character (Stream implementation)
wim 4:b2bbdc58967e 880 */
wim 4:b2bbdc58967e 881 int TM1638_LKM1638::_putc(int value) {
wim 4:b2bbdc58967e 882 int addr;
wim 4:b2bbdc58967e 883 bool validChar = false;
wim 4:b2bbdc58967e 884 char pattern = 0x00;
wim 4:b2bbdc58967e 885
wim 4:b2bbdc58967e 886 if ((value == '\n') || (value == '\r')) {
wim 4:b2bbdc58967e 887 //No character to write
wim 4:b2bbdc58967e 888 validChar = false;
wim 4:b2bbdc58967e 889
wim 4:b2bbdc58967e 890 //Update Cursor
wim 4:b2bbdc58967e 891 _column = 0;
wim 4:b2bbdc58967e 892 }
wim 4:b2bbdc58967e 893 else if ((value == '.') || (value == ',')) {
wim 4:b2bbdc58967e 894 //No character to write
wim 4:b2bbdc58967e 895 validChar = false;
wim 4:b2bbdc58967e 896 pattern = S7_DP; // placeholder for all DPs
wim 4:b2bbdc58967e 897
wim 4:b2bbdc58967e 898 // Check to see that DP can be shown for current column
wim 4:b2bbdc58967e 899 if (_column > 0) {
wim 4:b2bbdc58967e 900 //Translate between _column and displaybuffer entries
wim 4:b2bbdc58967e 901 //Add DP to bitpattern of digit left of current column.
wim 4:b2bbdc58967e 902 addr = (_column - 1) << 1; // * TM1638_BYTES_PER_GRID
wim 4:b2bbdc58967e 903
wim 4:b2bbdc58967e 904 //Save icons...and set bits for decimal point to write
wim 4:b2bbdc58967e 905 _displaybuffer[addr] = _displaybuffer[addr] | pattern;
wim 4:b2bbdc58967e 906 // _displaybuffer[addr+1] = _displaybuffer[addr+1] | pattern;
wim 4:b2bbdc58967e 907
wim 4:b2bbdc58967e 908 // writeData(_displaybuffer, (LKM1638_NR_GRIDS * TM1638_BYTES_PER_GRID));
wim 4:b2bbdc58967e 909 writeData(_displaybuffer, TM1638_BYTES_PER_GRID, addr);
wim 4:b2bbdc58967e 910
wim 4:b2bbdc58967e 911 //No Cursor Update
wim 4:b2bbdc58967e 912 }
wim 4:b2bbdc58967e 913 }
wim 4:b2bbdc58967e 914 else if ((value >= 0) && (value < LKM1638_NR_UDC)) {
wim 4:b2bbdc58967e 915 //Character to write
wim 4:b2bbdc58967e 916 validChar = true;
wim 4:b2bbdc58967e 917 pattern = _UDC_7S[value];
wim 4:b2bbdc58967e 918 }
wim 4:b2bbdc58967e 919
wim 4:b2bbdc58967e 920 #if (SHOW_ASCII == 1)
wim 4:b2bbdc58967e 921 //display all ASCII characters
wim 4:b2bbdc58967e 922 else if ((value >= FONT_7S_START) && (value <= FONT_7S_END)) {
wim 4:b2bbdc58967e 923 //Character to write
wim 4:b2bbdc58967e 924 validChar = true;
wim 4:b2bbdc58967e 925 pattern = FONT_7S[value - FONT_7S_START];
wim 4:b2bbdc58967e 926 } // else
wim 4:b2bbdc58967e 927 #else
wim 4:b2bbdc58967e 928 //display only digits and hex characters
wim 4:b2bbdc58967e 929 else if (value == '-') {
wim 4:b2bbdc58967e 930 //Character to write
wim 4:b2bbdc58967e 931 validChar = true;
wim 4:b2bbdc58967e 932 pattern = C7_MIN;
wim 4:b2bbdc58967e 933 }
wim 4:b2bbdc58967e 934 else if ((value >= (int)'0') && (value <= (int) '9')) {
wim 4:b2bbdc58967e 935 //Character to write
wim 4:b2bbdc58967e 936 validChar = true;
wim 4:b2bbdc58967e 937 pattern = FONT_7S[value - (int) '0'];
wim 4:b2bbdc58967e 938 }
wim 4:b2bbdc58967e 939 else if ((value >= (int) 'A') && (value <= (int) 'F')) {
wim 4:b2bbdc58967e 940 //Character to write
wim 4:b2bbdc58967e 941 validChar = true;
wim 4:b2bbdc58967e 942 pattern = FONT_7S[10 + value - (int) 'A'];
wim 4:b2bbdc58967e 943 }
wim 4:b2bbdc58967e 944 else if ((value >= (int) 'a') && (value <= (int) 'f')) {
wim 4:b2bbdc58967e 945 //Character to write
wim 4:b2bbdc58967e 946 validChar = true;
wim 4:b2bbdc58967e 947 pattern = FONT_7S[10 + value - (int) 'a'];
wim 4:b2bbdc58967e 948 } //else
wim 4:b2bbdc58967e 949 #endif
wim 4:b2bbdc58967e 950
wim 4:b2bbdc58967e 951 if (validChar) {
wim 4:b2bbdc58967e 952 //Character to write
wim 4:b2bbdc58967e 953
wim 4:b2bbdc58967e 954 //Translate between _column and displaybuffer entries
wim 4:b2bbdc58967e 955 addr = _column << 1; // * TM1638_BYTES_PER_GRID
wim 4:b2bbdc58967e 956
wim 4:b2bbdc58967e 957 //Save icons...and set bits for character to write
wim 4:b2bbdc58967e 958 _displaybuffer[addr] = (_displaybuffer[addr] & MASK_ICON_GRID[_column][0]) | pattern;
wim 4:b2bbdc58967e 959 // _displaybuffer[addr+1] = (_displaybuffer[addr+1] & MASK_ICON_GRID[_column][0]) | pattern;
wim 4:b2bbdc58967e 960
wim 4:b2bbdc58967e 961 // writeData(_displaybuffer, (LKM1638_NR_GRIDS * TM1638_BYTES_PER_GRID));
wim 4:b2bbdc58967e 962 writeData(_displaybuffer, TM1638_BYTES_PER_GRID, addr);
wim 4:b2bbdc58967e 963
wim 4:b2bbdc58967e 964 //Update Cursor
wim 4:b2bbdc58967e 965 _column++;
wim 4:b2bbdc58967e 966 if (_column > (LKM1638_NR_DIGITS - 1)) {
wim 4:b2bbdc58967e 967 _column = 0;
wim 4:b2bbdc58967e 968 }
wim 4:b2bbdc58967e 969
wim 4:b2bbdc58967e 970 } // if validChar
wim 4:b2bbdc58967e 971
wim 4:b2bbdc58967e 972 return value;
wim 4:b2bbdc58967e 973 }
wim 4:b2bbdc58967e 974
wim 4:b2bbdc58967e 975
wim 4:b2bbdc58967e 976 // get a single character (Stream implementation)
wim 4:b2bbdc58967e 977 int TM1638_LKM1638::_getc() {
wim 4:b2bbdc58967e 978 return -1;
wim 4:b2bbdc58967e 979 }
wim 4:b2bbdc58967e 980
wim 4:b2bbdc58967e 981 #endif
wim 4:b2bbdc58967e 982