TEST

Dependents:   ADXL345Test ADXL345Test1

Fork of TM1638 by Wim Huiskamp

Committer:
wim
Date:
Fri Jan 15 19:09:59 2016 +0000
Revision:
2:532ce15ea9ec
Parent:
1:1f2453ed85d7
Child:
3:25ddabfadc8c
Refactored display and keyboard defines, added ASCII font.

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 0:201dfacbe0e5 193 char TM1638::_flip(char data) {
wim 0:201dfacbe0e5 194 char value=0;
wim 0:201dfacbe0e5 195
wim 0:201dfacbe0e5 196 if (data & 0x01) {value |= 0x80;} ;
wim 0:201dfacbe0e5 197 if (data & 0x02) {value |= 0x40;} ;
wim 0:201dfacbe0e5 198 if (data & 0x04) {value |= 0x20;} ;
wim 0:201dfacbe0e5 199 if (data & 0x08) {value |= 0x10;} ;
wim 0:201dfacbe0e5 200 if (data & 0x10) {value |= 0x08;} ;
wim 0:201dfacbe0e5 201 if (data & 0x20) {value |= 0x04;} ;
wim 0:201dfacbe0e5 202 if (data & 0x40) {value |= 0x02;} ;
wim 0:201dfacbe0e5 203 if (data & 0x80) {value |= 0x01;} ;
wim 0:201dfacbe0e5 204 return value;
wim 0:201dfacbe0e5 205 }
wim 0:201dfacbe0e5 206
wim 0:201dfacbe0e5 207
wim 0:201dfacbe0e5 208 /** Write command and parameter to TM1638
wim 0:201dfacbe0e5 209 * @param int cmd Command byte
wim 0:201dfacbe0e5 210 * &Param int data Parameters for command
wim 0:201dfacbe0e5 211 * @return none
wim 0:201dfacbe0e5 212 */
wim 0:201dfacbe0e5 213 void TM1638::_writeCmd(int cmd, int data){
wim 0:201dfacbe0e5 214
wim 0:201dfacbe0e5 215 _cs=0;
wim 0:201dfacbe0e5 216 wait_us(1);
wim 0:201dfacbe0e5 217 // _spi.write(_flip( (cmd & 0xF0) | (data & 0x0F)));
wim 0:201dfacbe0e5 218 _spi.write(_flip( (cmd & TM1638_CMD_MSK) | (data & ~TM1638_CMD_MSK)));
wim 0:201dfacbe0e5 219
wim 0:201dfacbe0e5 220 wait_us(1);
wim 0:201dfacbe0e5 221 _cs=1;
wim 0:201dfacbe0e5 222 }
wim 0:201dfacbe0e5 223
wim 0:201dfacbe0e5 224
wim 0:201dfacbe0e5 225 #if (LEDKEY8_TEST == 1)
wim 0:201dfacbe0e5 226 // Derived class for TM1638 used in LED&KEY display unit
wim 0:201dfacbe0e5 227 //
wim 0:201dfacbe0e5 228
wim 0:201dfacbe0e5 229 /** Constructor for class for driving TM1638 LED controller as used in LEDKEY8
wim 0:201dfacbe0e5 230 *
wim 0:201dfacbe0e5 231 * @brief Supports 8 Digits of 7 Segments + DP + LED Icons. Also supports a scanned keyboard of 8.
wim 0:201dfacbe0e5 232 *
wim 0:201dfacbe0e5 233 * @param PinName mosi, miso, sclk, cs SPI bus pins
wim 0:201dfacbe0e5 234 */
wim 0:201dfacbe0e5 235 TM1638_LEDKEY8::TM1638_LEDKEY8(PinName mosi, PinName miso, PinName sclk, PinName cs) : TM1638(mosi, miso, sclk, cs) {
wim 0:201dfacbe0e5 236 _column = 0;
wim 0:201dfacbe0e5 237 _columns = LEDKEY8_NR_DIGITS;
wim 0:201dfacbe0e5 238 }
wim 0:201dfacbe0e5 239
wim 0:201dfacbe0e5 240 #if(0)
wim 0:201dfacbe0e5 241 #if DOXYGEN_ONLY
wim 0:201dfacbe0e5 242 /** Write a character to the Display
wim 0:201dfacbe0e5 243 *
wim 0:201dfacbe0e5 244 * @param c The character to write to the display
wim 0:201dfacbe0e5 245 */
wim 0:201dfacbe0e5 246 int putc(int c);
wim 0:201dfacbe0e5 247
wim 0:201dfacbe0e5 248 /** Write a formatted string to the Display
wim 0:201dfacbe0e5 249 *
wim 0:201dfacbe0e5 250 * @param format A printf-style format string, followed by the
wim 0:201dfacbe0e5 251 * variables to use in formatting the string.
wim 0:201dfacbe0e5 252 */
wim 0:201dfacbe0e5 253 int printf(const char* format, ...);
wim 0:201dfacbe0e5 254 #endif
wim 0:201dfacbe0e5 255 #endif
wim 0:201dfacbe0e5 256
wim 0:201dfacbe0e5 257 /** Locate cursor to a screen column
wim 0:201dfacbe0e5 258 *
wim 0:201dfacbe0e5 259 * @param column The horizontal position from the left, indexed from 0
wim 0:201dfacbe0e5 260 */
wim 0:201dfacbe0e5 261 void TM1638_LEDKEY8::locate(int column) {
wim 0:201dfacbe0e5 262 //sanity check
wim 0:201dfacbe0e5 263 if (column < 0) {column = 0;}
wim 0:201dfacbe0e5 264 if (column > (_columns - 1)) {column = _columns - 1;}
wim 0:201dfacbe0e5 265
wim 0:201dfacbe0e5 266 _column = column;
wim 0:201dfacbe0e5 267 }
wim 0:201dfacbe0e5 268
wim 0:201dfacbe0e5 269
wim 0:201dfacbe0e5 270 /** Number of screen columns
wim 0:201dfacbe0e5 271 *
wim 0:201dfacbe0e5 272 * @param none
wim 0:201dfacbe0e5 273 * @return columns
wim 0:201dfacbe0e5 274 */
wim 0:201dfacbe0e5 275 int TM1638_LEDKEY8::columns() {
wim 0:201dfacbe0e5 276 return _columns;
wim 0:201dfacbe0e5 277 }
wim 0:201dfacbe0e5 278
wim 0:201dfacbe0e5 279
wim 0:201dfacbe0e5 280 /** Clear the screen and locate to 0
wim 0:201dfacbe0e5 281 * @param bool clrAll Clear Icons also (default = false)
wim 0:201dfacbe0e5 282 */
wim 0:201dfacbe0e5 283 void TM1638_LEDKEY8::cls(bool clrAll) {
wim 0:201dfacbe0e5 284
wim 0:201dfacbe0e5 285 if (clrAll) {
wim 0:201dfacbe0e5 286 //clear local buffer (including Icons)
wim 0:201dfacbe0e5 287 for (int idx=0; idx < (LEDKEY8_NR_GRIDS << 1); idx++) {
wim 0:201dfacbe0e5 288 _displaybuffer[idx] = 0x00;
wim 0:201dfacbe0e5 289 }
wim 0:201dfacbe0e5 290 }
wim 0:201dfacbe0e5 291 else {
wim 0:201dfacbe0e5 292 //clear local buffer (preserving Icons)
wim 0:201dfacbe0e5 293 for (int idx=0; idx < LEDKEY8_NR_GRIDS; idx++) {
wim 0:201dfacbe0e5 294 _displaybuffer[(idx<<1)] = _displaybuffer[(idx<<1)] & MASK_ICON_GRID[idx][0];
wim 0:201dfacbe0e5 295 _displaybuffer[(idx<<1) + 1] = _displaybuffer[(idx<<1) + 1] & MASK_ICON_GRID[idx][1];
wim 0:201dfacbe0e5 296 }
wim 0:201dfacbe0e5 297 }
wim 0:201dfacbe0e5 298
wim 2:532ce15ea9ec 299 writeData(_displaybuffer, (LEDKEY8_NR_GRIDS * TM1638_BYTES_PER_GRID));
wim 0:201dfacbe0e5 300
wim 0:201dfacbe0e5 301 _column = 0;
wim 0:201dfacbe0e5 302 }
wim 0:201dfacbe0e5 303
wim 0:201dfacbe0e5 304 /** Set Icon
wim 0:201dfacbe0e5 305 *
wim 1:1f2453ed85d7 306 * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 16 LSBs
wim 0:201dfacbe0e5 307 * @return none
wim 0:201dfacbe0e5 308 */
wim 0:201dfacbe0e5 309 void TM1638_LEDKEY8::setIcon(Icon icon) {
wim 0:201dfacbe0e5 310 int addr, icn;
wim 0:201dfacbe0e5 311
wim 1:1f2453ed85d7 312 icn = icon & 0xFFFF;
wim 0:201dfacbe0e5 313 addr = (icon >> 24) & 0xFF;
wim 2:532ce15ea9ec 314 addr = (addr - 1) << 1; // * PT1638_BYTES_PER_GRID
wim 0:201dfacbe0e5 315
wim 0:201dfacbe0e5 316 //Save char...and set bits for icon to write
wim 0:201dfacbe0e5 317 _displaybuffer[addr] = _displaybuffer[addr] | LO(icn);
wim 0:201dfacbe0e5 318 _displaybuffer[addr+1] = _displaybuffer[addr+1] | HI(icn);
wim 2:532ce15ea9ec 319 writeData(_displaybuffer, (LEDKEY8_NR_GRIDS * TM1638_BYTES_PER_GRID));
wim 0:201dfacbe0e5 320 }
wim 0:201dfacbe0e5 321
wim 0:201dfacbe0e5 322 /** Clr Icon
wim 0:201dfacbe0e5 323 *
wim 1:1f2453ed85d7 324 * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 16 LSBs
wim 0:201dfacbe0e5 325 * @return none
wim 0:201dfacbe0e5 326 */
wim 0:201dfacbe0e5 327 void TM1638_LEDKEY8::clrIcon(Icon icon) {
wim 0:201dfacbe0e5 328 int addr, icn;
wim 0:201dfacbe0e5 329
wim 1:1f2453ed85d7 330 icn = icon & 0xFFFF;
wim 0:201dfacbe0e5 331 addr = (icon >> 24) & 0xFF;
wim 2:532ce15ea9ec 332 addr = (addr - 1) << 1; // * TM1638_BYTES_PER_GRID
wim 0:201dfacbe0e5 333
wim 0:201dfacbe0e5 334 //Save char...and clr bits for icon to write
wim 0:201dfacbe0e5 335 _displaybuffer[addr] = _displaybuffer[addr] & ~LO(icn);
wim 0:201dfacbe0e5 336 _displaybuffer[addr+1] = _displaybuffer[addr+1] & ~HI(icn);
wim 2:532ce15ea9ec 337 writeData(_displaybuffer, (LEDKEY8_NR_GRIDS * TM1638_BYTES_PER_GRID));
wim 0:201dfacbe0e5 338 }
wim 0:201dfacbe0e5 339
wim 0:201dfacbe0e5 340
wim 0:201dfacbe0e5 341 /** Set User Defined Characters (UDC)
wim 0:201dfacbe0e5 342 *
wim 0:201dfacbe0e5 343 * @param unsigned char udc_idx The Index of the UDC (0..7)
wim 0:201dfacbe0e5 344 * @param int udc_data The bitpattern for the UDC (8 bits)
wim 0:201dfacbe0e5 345 */
wim 0:201dfacbe0e5 346 void TM1638_LEDKEY8::setUDC(unsigned char udc_idx, int udc_data) {
wim 0:201dfacbe0e5 347
wim 0:201dfacbe0e5 348 //Sanity check
wim 0:201dfacbe0e5 349 if (udc_idx > (LEDKEY8_NR_UDC-1)) {
wim 0:201dfacbe0e5 350 return;
wim 0:201dfacbe0e5 351 }
wim 0:201dfacbe0e5 352 // Mask out Icon bits?
wim 0:201dfacbe0e5 353
wim 0:201dfacbe0e5 354 _UDC_7S[udc_idx] = LO(udc_data);
wim 0:201dfacbe0e5 355 }
wim 0:201dfacbe0e5 356
wim 0:201dfacbe0e5 357
wim 0:201dfacbe0e5 358 /** Write a single character (Stream implementation)
wim 0:201dfacbe0e5 359 */
wim 0:201dfacbe0e5 360 int TM1638_LEDKEY8::_putc(int value) {
wim 0:201dfacbe0e5 361 int addr;
wim 0:201dfacbe0e5 362 bool validChar = false;
wim 0:201dfacbe0e5 363 char pattern = 0x00;
wim 0:201dfacbe0e5 364
wim 0:201dfacbe0e5 365 if ((value == '\n') || (value == '\r')) {
wim 0:201dfacbe0e5 366 //No character to write
wim 0:201dfacbe0e5 367 validChar = false;
wim 0:201dfacbe0e5 368
wim 0:201dfacbe0e5 369 //Update Cursor
wim 0:201dfacbe0e5 370 _column = 0;
wim 0:201dfacbe0e5 371 }
wim 1:1f2453ed85d7 372 else if ((value == '.') || (value == ',')) {
wim 1:1f2453ed85d7 373 //No character to write
wim 1:1f2453ed85d7 374 validChar = false;
wim 1:1f2453ed85d7 375 pattern = S7_DP; // placeholder for all DPs
wim 1:1f2453ed85d7 376
wim 1:1f2453ed85d7 377 // Check to see that DP can be shown for current column
wim 1:1f2453ed85d7 378 if (_column > 0) {
wim 1:1f2453ed85d7 379 //Translate between _column and displaybuffer entries
wim 1:1f2453ed85d7 380 //Add DP to bitpattern of digit left of current column.
wim 2:532ce15ea9ec 381 addr = (_column - 1) << 1; // * PT1638_BYTES_PER_GRID
wim 1:1f2453ed85d7 382
wim 1:1f2453ed85d7 383 //Save icons...and set bits for decimal point to write
wim 1:1f2453ed85d7 384 _displaybuffer[addr] = _displaybuffer[addr] | pattern;
wim 1:1f2453ed85d7 385 // _displaybuffer[addr+1] = _displaybuffer[addr+1] | pattern;
wim 1:1f2453ed85d7 386
wim 2:532ce15ea9ec 387 writeData(_displaybuffer, (LEDKEY8_NR_GRIDS * TM1638_BYTES_PER_GRID));
wim 1:1f2453ed85d7 388
wim 1:1f2453ed85d7 389 //No Cursor Update
wim 1:1f2453ed85d7 390 }
wim 1:1f2453ed85d7 391 }
wim 0:201dfacbe0e5 392 else if ((value >= 0) && (value < LEDKEY8_NR_UDC)) {
wim 0:201dfacbe0e5 393 //Character to write
wim 0:201dfacbe0e5 394 validChar = true;
wim 0:201dfacbe0e5 395 pattern = _UDC_7S[value];
wim 0:201dfacbe0e5 396 }
wim 2:532ce15ea9ec 397
wim 2:532ce15ea9ec 398 #if (SHOW_ASCII == 1)
wim 2:532ce15ea9ec 399 //display all ASCII characters
wim 2:532ce15ea9ec 400 else if ((value >= FONT_7S_START) && (value <= FONT_7S_END)) {
wim 2:532ce15ea9ec 401 //Character to write
wim 2:532ce15ea9ec 402 validChar = true;
wim 2:532ce15ea9ec 403 pattern = FONT_7S[value - FONT_7S_START];
wim 2:532ce15ea9ec 404 } // else
wim 2:532ce15ea9ec 405 #else
wim 2:532ce15ea9ec 406 //display only digits and hex characters
wim 2:532ce15ea9ec 407 else if (value == '-') {
wim 2:532ce15ea9ec 408 //Character to write
wim 2:532ce15ea9ec 409 validChar = true;
wim 2:532ce15ea9ec 410 pattern = C7_MIN;
wim 2:532ce15ea9ec 411 }
wim 0:201dfacbe0e5 412 else if ((value >= (int)'0') && (value <= (int) '9')) {
wim 0:201dfacbe0e5 413 //Character to write
wim 0:201dfacbe0e5 414 validChar = true;
wim 0:201dfacbe0e5 415 pattern = FONT_7S[value - (int) '0'];
wim 0:201dfacbe0e5 416 }
wim 0:201dfacbe0e5 417 else if ((value >= (int) 'A') && (value <= (int) 'F')) {
wim 0:201dfacbe0e5 418 //Character to write
wim 0:201dfacbe0e5 419 validChar = true;
wim 0:201dfacbe0e5 420 pattern = FONT_7S[10 + value - (int) 'A'];
wim 0:201dfacbe0e5 421 }
wim 0:201dfacbe0e5 422 else if ((value >= (int) 'a') && (value <= (int) 'f')) {
wim 0:201dfacbe0e5 423 //Character to write
wim 0:201dfacbe0e5 424 validChar = true;
wim 0:201dfacbe0e5 425 pattern = FONT_7S[10 + value - (int) 'a'];
wim 0:201dfacbe0e5 426 } //else
wim 2:532ce15ea9ec 427 #endif
wim 0:201dfacbe0e5 428
wim 0:201dfacbe0e5 429 if (validChar) {
wim 0:201dfacbe0e5 430 //Character to write
wim 0:201dfacbe0e5 431
wim 0:201dfacbe0e5 432 //Translate between _column and displaybuffer entries
wim 2:532ce15ea9ec 433 addr = _column << 1; // * TM1638_BYTES_PER_GRID
wim 0:201dfacbe0e5 434
wim 0:201dfacbe0e5 435 //Save icons...and set bits for character to write
wim 0:201dfacbe0e5 436 _displaybuffer[addr] = (_displaybuffer[addr] & MASK_ICON_GRID[_column][0]) | pattern;
wim 0:201dfacbe0e5 437 // _displaybuffer[addr+1] = (_displaybuffer[addr+1] & MASK_ICON_GRID[_column][0]) | pattern;
wim 0:201dfacbe0e5 438
wim 2:532ce15ea9ec 439 writeData(_displaybuffer, (LEDKEY8_NR_GRIDS * TM1638_BYTES_PER_GRID));
wim 0:201dfacbe0e5 440
wim 0:201dfacbe0e5 441 //Update Cursor
wim 0:201dfacbe0e5 442 _column++;
wim 0:201dfacbe0e5 443 if (_column > (LEDKEY8_NR_DIGITS - 1)) {
wim 0:201dfacbe0e5 444 _column = 0;
wim 0:201dfacbe0e5 445 }
wim 0:201dfacbe0e5 446
wim 0:201dfacbe0e5 447 } // if validChar
wim 0:201dfacbe0e5 448
wim 0:201dfacbe0e5 449 return value;
wim 0:201dfacbe0e5 450 }
wim 0:201dfacbe0e5 451
wim 0:201dfacbe0e5 452
wim 0:201dfacbe0e5 453 // get a single character (Stream implementation)
wim 0:201dfacbe0e5 454 int TM1638_LEDKEY8::_getc() {
wim 0:201dfacbe0e5 455 return -1;
wim 0:201dfacbe0e5 456 }
wim 0:201dfacbe0e5 457
wim 0:201dfacbe0e5 458 #endif