TEST

Dependents:   ADXL345Test ADXL345Test1

Fork of TM1638 by Wim Huiskamp

Committer:
wim
Date:
Tue Jan 19 18:58:22 2016 +0000
Revision:
3:25ddabfadc8c
Parent:
2:532ce15ea9ec
Child:
4:b2bbdc58967e
Added animation demo for Jeroen

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