TEST

Dependents:   ADXL345Test ADXL345Test1

Fork of TM1638 by Wim Huiskamp

Committer:
jack__zen
Date:
Mon Sep 04 01:39:58 2017 +0000
Revision:
5:a4634cc2ab71
Parent:
4:b2bbdc58967e
TSET

Who changed what in which revision?

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