done

Dependents:   AwesomeAlarm_BLE

Committer:
wim
Date:
Fri Oct 06 16:43:56 2017 +0000
Revision:
1:f63d87466f55
Parent:
0:dc8789a6e21f
Added RobotDyn 6 Digit module,; Added Eyewink 6 Digit + 5 Keys module,; Constructor adapted to 2 pins: dio, clk

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wim 0:dc8789a6e21f 1 /* mbed TM1637 Library, for TM1637 LED controller
wim 0:dc8789a6e21f 2 * Copyright (c) 2016, v01: WH, Initial version
wim 1:f63d87466f55 3 * 2017, v02: WH, Added RobotDyn 6 Digit module,
wim 1:f63d87466f55 4 * Added Eyewink 6 Digit + 6 Keys module,
wim 1:f63d87466f55 5 * Constructor adapted to 2 pins: dio, clk
wim 0:dc8789a6e21f 6 *
wim 0:dc8789a6e21f 7 * Permission is hereby granted, free of charge, to any person obtaining a copy
wim 0:dc8789a6e21f 8 * of this software and associated documentation files (the "Software"), to deal
wim 0:dc8789a6e21f 9 * in the Software without restriction, inclumosig without limitation the rights
wim 0:dc8789a6e21f 10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
wim 0:dc8789a6e21f 11 * copies of the Software, and to permit persons to whom the Software is
wim 0:dc8789a6e21f 12 * furnished to do so, subject to the following conditions:
wim 0:dc8789a6e21f 13 *
wim 0:dc8789a6e21f 14 * The above copyright notice and this permission notice shall be included in
wim 0:dc8789a6e21f 15 * all copies or substantial portions of the Software.
wim 0:dc8789a6e21f 16 *
wim 0:dc8789a6e21f 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
wim 0:dc8789a6e21f 18 * IMPLIED, INCLUmosiG BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
wim 0:dc8789a6e21f 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
wim 0:dc8789a6e21f 20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
wim 0:dc8789a6e21f 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
wim 0:dc8789a6e21f 22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
wim 0:dc8789a6e21f 23 * THE SOFTWARE.
wim 0:dc8789a6e21f 24 */
wim 0:dc8789a6e21f 25 #include "mbed.h"
wim 0:dc8789a6e21f 26 #include "TM1637.h"
wim 0:dc8789a6e21f 27
wim 1:f63d87466f55 28 #if(SPI==1)
wim 0:dc8789a6e21f 29 /** Constructor for class for driving TM1637 LED controller with Serial bus interface device.
wim 1:f63d87466f55 30 *
wim 1:f63d87466f55 31 * @brief Supports 6 Grids @ 8 segments. Also supports upto 16 Keys.
wim 1:f63d87466f55 32 * Serial bus interface device.
wim 1:f63d87466f55 33 * DEPRECATED version
wim 1:f63d87466f55 34 *
wim 1:f63d87466f55 35 * @param PinName mosi_nc Serial bus NC pin
wim 1:f63d87466f55 36 * @param PinName miso_dio Serial bus DIO pin
wim 1:f63d87466f55 37 * @param PinName sclk_clk Serial bus CLK pin
wim 1:f63d87466f55 38 */
wim 1:f63d87466f55 39 TM1637::TM1637(PinName mosi_nc, PinName miso_dio, PinName sclk_clk) : _mosi_nc(mosi_nc), _dio(miso_dio), _clk(sclk_clk) {
wim 0:dc8789a6e21f 40
wim 0:dc8789a6e21f 41 _init();
wim 0:dc8789a6e21f 42 }
wim 0:dc8789a6e21f 43
wim 1:f63d87466f55 44 /** Constructor for class for driving TM1637 LED controller with Serial bus interface device.
wim 1:f63d87466f55 45 * @brief Supports 6 digits @ 8 segments. Also supports upto 16 Keys.
wim 1:f63d87466f55 46 *
wim 1:f63d87466f55 47 * @param PinName dio Serial bus DIO pin
wim 1:f63d87466f55 48 * @param PinName clk Serial bus CLK pin
wim 1:f63d87466f55 49 */
wim 1:f63d87466f55 50 TM1637::TM1637(PinName dio, PinName clk) : _mosi_nc(NC), _dio(dio), _clk(clk) {
wim 1:f63d87466f55 51
wim 1:f63d87466f55 52 _init();
wim 1:f63d87466f55 53 }
wim 1:f63d87466f55 54 #else
wim 1:f63d87466f55 55
wim 1:f63d87466f55 56 /** Constructor for class for driving TM1637 LED controller with Serial bus interface device.
wim 1:f63d87466f55 57 * @brief Supports 6 digits @ 8 segments. Also supports upto 16 Keys.
wim 1:f63d87466f55 58 *
wim 1:f63d87466f55 59 * @param PinName dio Serial bus DIO pin
wim 1:f63d87466f55 60 * @param PinName clk Serial bus CLK pin
wim 1:f63d87466f55 61 */
wim 1:f63d87466f55 62 TM1637::TM1637(PinName dio, PinName clk) : _dio(dio), _clk(clk) {
wim 1:f63d87466f55 63
wim 1:f63d87466f55 64 _init();
wim 1:f63d87466f55 65 }
wim 1:f63d87466f55 66
wim 1:f63d87466f55 67 #endif
wim 0:dc8789a6e21f 68 /** Init the Serial interface and the controller
wim 0:dc8789a6e21f 69 * @param none
wim 0:dc8789a6e21f 70 * @return none
wim 0:dc8789a6e21f 71 */
wim 0:dc8789a6e21f 72 void TM1637::_init(){
wim 0:dc8789a6e21f 73
wim 0:dc8789a6e21f 74 //TM1637 uses a Serial bus that looks like I2C, but really is not.
wim 0:dc8789a6e21f 75 //It has Start and Stop conditions like I2C and an Ack pulse, but instead of slaveaddresses and a RW bit it just transmits commands and data.
wim 1:f63d87466f55 76
wim 0:dc8789a6e21f 77 //init Serial bus
wim 1:f63d87466f55 78 _dio.output();
wim 1:f63d87466f55 79 // _dio.mode(PullUp);
wim 1:f63d87466f55 80 wait_us(1);
wim 1:f63d87466f55 81
wim 1:f63d87466f55 82 _dio=1;
wim 1:f63d87466f55 83 _clk=1;
wim 0:dc8789a6e21f 84
wim 0:dc8789a6e21f 85 //init controller
wim 0:dc8789a6e21f 86 _display = TM1637_DSP_ON;
wim 0:dc8789a6e21f 87 _bright = TM1637_BRT_DEF;
wim 0:dc8789a6e21f 88 _writeCmd(TM1637_DSP_CTRL_CMD, _display | _bright ); // Display control cmd, display on/off, brightness
wim 0:dc8789a6e21f 89
wim 0:dc8789a6e21f 90 _writeCmd(TM1637_DATA_SET_CMD, TM1637_DATA_WR | TM1637_ADDR_INC | TM1637_MODE_NORM); // Data set cmd, normal mode, auto incr, write data
wim 0:dc8789a6e21f 91 }
wim 0:dc8789a6e21f 92
wim 0:dc8789a6e21f 93
wim 0:dc8789a6e21f 94 /** Clear the screen and locate to 0
wim 0:dc8789a6e21f 95 */
wim 0:dc8789a6e21f 96 void TM1637::cls() {
wim 0:dc8789a6e21f 97
wim 0:dc8789a6e21f 98 _start();
wim 0:dc8789a6e21f 99
wim 0:dc8789a6e21f 100 _write(TM1637_ADDR_SET_CMD | 0x00); // Address set cmd, 0
wim 0:dc8789a6e21f 101 for (int cnt=0; cnt<TM1637_DISPLAY_MEM; cnt++) {
wim 0:dc8789a6e21f 102 _write(0x00); // data
wim 0:dc8789a6e21f 103 }
wim 0:dc8789a6e21f 104
wim 0:dc8789a6e21f 105 _stop();
wim 0:dc8789a6e21f 106 }
wim 0:dc8789a6e21f 107
wim 0:dc8789a6e21f 108 /** Set Brightness
wim 0:dc8789a6e21f 109 *
wim 0:dc8789a6e21f 110 * @param char brightness (3 significant bits, valid range 0..7 (1/16 .. 14/14 dutycycle)
wim 0:dc8789a6e21f 111 * @return none
wim 0:dc8789a6e21f 112 */
wim 0:dc8789a6e21f 113 void TM1637::setBrightness(char brightness){
wim 0:dc8789a6e21f 114
wim 0:dc8789a6e21f 115 _bright = brightness & TM1637_BRT_MSK; // mask invalid bits
wim 0:dc8789a6e21f 116
wim 0:dc8789a6e21f 117 _writeCmd(TM1637_DSP_CTRL_CMD, _display | _bright ); // Display control cmd, display on/off, brightness
wim 0:dc8789a6e21f 118 }
wim 0:dc8789a6e21f 119
wim 0:dc8789a6e21f 120 /** Set the Display mode On/off
wim 0:dc8789a6e21f 121 *
wim 0:dc8789a6e21f 122 * @param bool display mode
wim 0:dc8789a6e21f 123 */
wim 0:dc8789a6e21f 124 void TM1637::setDisplay(bool on) {
wim 0:dc8789a6e21f 125
wim 0:dc8789a6e21f 126 if (on) {
wim 0:dc8789a6e21f 127 _display = TM1637_DSP_ON;
wim 0:dc8789a6e21f 128 }
wim 0:dc8789a6e21f 129 else {
wim 0:dc8789a6e21f 130 _display = TM1637_DSP_OFF;
wim 0:dc8789a6e21f 131 }
wim 0:dc8789a6e21f 132
wim 0:dc8789a6e21f 133 _writeCmd(TM1637_DSP_CTRL_CMD, _display | _bright ); // Display control cmd, display on/off, brightness
wim 0:dc8789a6e21f 134 }
wim 0:dc8789a6e21f 135
wim 0:dc8789a6e21f 136 /** Write databyte to TM1637
wim 0:dc8789a6e21f 137 * @param int address display memory location to write byte
wim 0:dc8789a6e21f 138 * @param char data byte written at given address
wim 0:dc8789a6e21f 139 * @return none
wim 0:dc8789a6e21f 140 */
wim 0:dc8789a6e21f 141 void TM1637::writeData(char data, int address) {
wim 0:dc8789a6e21f 142
wim 0:dc8789a6e21f 143 _start();
wim 0:dc8789a6e21f 144
wim 0:dc8789a6e21f 145 _write(TM1637_ADDR_SET_CMD | (address & TM1637_ADDR_MSK)); // Set Address cmd
wim 0:dc8789a6e21f 146 _write(data); // data
wim 0:dc8789a6e21f 147
wim 0:dc8789a6e21f 148 _stop();
wim 0:dc8789a6e21f 149 }
wim 0:dc8789a6e21f 150
wim 0:dc8789a6e21f 151 /** Write Display datablock to TM1637
wim 0:dc8789a6e21f 152 * @param DisplayData_t data Array of TM1637_DISPLAY_MEM (=16) bytes for displaydata
wim 0:dc8789a6e21f 153 * @param length number bytes to write (valid range 0..(TM1637_MAX_NR_GRIDS * TM1637_BYTES_PER_GRID) (=16), when starting at address 0)
wim 0:dc8789a6e21f 154 * @param int address display memory location to write bytes (default = 0)
wim 0:dc8789a6e21f 155 * @return none
wim 0:dc8789a6e21f 156 */
wim 0:dc8789a6e21f 157 void TM1637::writeData(DisplayData_t data, int length, int address) {
wim 0:dc8789a6e21f 158
wim 0:dc8789a6e21f 159 _start();
wim 0:dc8789a6e21f 160
wim 0:dc8789a6e21f 161 // sanity check
wim 0:dc8789a6e21f 162 address &= TM1637_ADDR_MSK;
wim 0:dc8789a6e21f 163 if (length < 0) {length = 0;}
wim 0:dc8789a6e21f 164 if ((length + address) > TM1637_DISPLAY_MEM) {length = (TM1637_DISPLAY_MEM - address);}
wim 0:dc8789a6e21f 165
wim 0:dc8789a6e21f 166 // _write(TM1637_ADDR_SET_CMD | 0x00); // Set Address at 0
wim 0:dc8789a6e21f 167 _write(TM1637_ADDR_SET_CMD | address); // Set Address
wim 0:dc8789a6e21f 168
wim 0:dc8789a6e21f 169 for (int idx=0; idx<length; idx++) {
wim 0:dc8789a6e21f 170 // _write(data[idx]); // data
wim 0:dc8789a6e21f 171 _write(data[address + idx]); // data
wim 0:dc8789a6e21f 172 }
wim 0:dc8789a6e21f 173
wim 0:dc8789a6e21f 174 _stop();
wim 0:dc8789a6e21f 175 }
wim 0:dc8789a6e21f 176
wim 0:dc8789a6e21f 177 /** Read keydata block from TM1637
wim 0:dc8789a6e21f 178 * @param *keydata Ptr to bytes for keydata
wim 0:dc8789a6e21f 179 * @return bool keypress True when at least one key was pressed
wim 0:dc8789a6e21f 180 */
wim 0:dc8789a6e21f 181 bool TM1637::getKeys(KeyData_t *keydata) {
wim 0:dc8789a6e21f 182
wim 0:dc8789a6e21f 183 _start();
wim 0:dc8789a6e21f 184
wim 0:dc8789a6e21f 185 // Enable Key Read mode
wim 0:dc8789a6e21f 186 _write(TM1637_DATA_SET_CMD | TM1637_KEY_RD | TM1637_ADDR_INC | TM1637_MODE_NORM); // Data set cmd, normal mode, auto incr, read data
wim 0:dc8789a6e21f 187
wim 0:dc8789a6e21f 188 // Read keys
wim 1:f63d87466f55 189 // Bitpattern S0 S1 S2 K1 K2 1 1 1
wim 1:f63d87466f55 190 *keydata = _read();
wim 1:f63d87466f55 191 // printf("Key = 0x%02x\r\n", *keydata);
wim 0:dc8789a6e21f 192
wim 0:dc8789a6e21f 193 _stop();
wim 0:dc8789a6e21f 194
wim 0:dc8789a6e21f 195 // Restore Data Write mode
wim 0:dc8789a6e21f 196 _writeCmd(TM1637_DATA_SET_CMD, TM1637_DATA_WR | TM1637_ADDR_INC | TM1637_MODE_NORM); // Data set cmd, normal mode, auto incr, write data
wim 0:dc8789a6e21f 197
wim 0:dc8789a6e21f 198 return (*keydata != TM1637_SW_NONE);
wim 0:dc8789a6e21f 199 }
wim 0:dc8789a6e21f 200
wim 0:dc8789a6e21f 201
wim 0:dc8789a6e21f 202 /** Generate Start condition for TM1637
wim 0:dc8789a6e21f 203 * @param none
wim 0:dc8789a6e21f 204 * @return none
wim 0:dc8789a6e21f 205 */
wim 0:dc8789a6e21f 206 void TM1637::_start() {
wim 0:dc8789a6e21f 207
wim 1:f63d87466f55 208 _dio=0;
wim 0:dc8789a6e21f 209 wait_us(1);
wim 1:f63d87466f55 210 _clk=0;
wim 0:dc8789a6e21f 211 wait_us(1);
wim 0:dc8789a6e21f 212 }
wim 0:dc8789a6e21f 213
wim 0:dc8789a6e21f 214 /** Generate Stop condition for TM1637
wim 0:dc8789a6e21f 215 * @param none
wim 0:dc8789a6e21f 216 * @return none
wim 0:dc8789a6e21f 217 */
wim 0:dc8789a6e21f 218 void TM1637::_stop() {
wim 0:dc8789a6e21f 219
wim 1:f63d87466f55 220 _dio=0;
wim 0:dc8789a6e21f 221 wait_us(1);
wim 1:f63d87466f55 222 _clk=1;
wim 0:dc8789a6e21f 223 wait_us(1);
wim 1:f63d87466f55 224 _dio=1;
wim 0:dc8789a6e21f 225 wait_us(1);
wim 0:dc8789a6e21f 226 }
wim 0:dc8789a6e21f 227
wim 0:dc8789a6e21f 228 /** Send byte to TM1637
wim 0:dc8789a6e21f 229 * @param int data
wim 0:dc8789a6e21f 230 * @return none
wim 0:dc8789a6e21f 231 */
wim 0:dc8789a6e21f 232 void TM1637::_write(int data) {
wim 0:dc8789a6e21f 233
wim 0:dc8789a6e21f 234 for (int bit=0; bit<8; bit++) {
wim 0:dc8789a6e21f 235 //The TM1637 expects LSB first
wim 0:dc8789a6e21f 236 if (((data >> bit) & 0x01) == 0x01) {
wim 1:f63d87466f55 237 _dio=1;
wim 0:dc8789a6e21f 238 }
wim 0:dc8789a6e21f 239 else {
wim 1:f63d87466f55 240 _dio=0;
wim 0:dc8789a6e21f 241 }
wim 0:dc8789a6e21f 242 wait_us(1);
wim 1:f63d87466f55 243 _clk=1;
wim 0:dc8789a6e21f 244 wait_us(1);
wim 1:f63d87466f55 245 _clk=0;
wim 0:dc8789a6e21f 246 wait_us(1);
wim 0:dc8789a6e21f 247 }
wim 1:f63d87466f55 248
wim 1:f63d87466f55 249 _dio=1;
wim 0:dc8789a6e21f 250
wim 1:f63d87466f55 251 // Prepare DIO to read data
wim 1:f63d87466f55 252 _dio.input();
wim 1:f63d87466f55 253 wait_us(3);
wim 1:f63d87466f55 254
wim 0:dc8789a6e21f 255 // dummy Ack
wim 1:f63d87466f55 256 _clk=1;
wim 0:dc8789a6e21f 257 wait_us(1);
wim 1:f63d87466f55 258 // _ack = _dio;
wim 1:f63d87466f55 259 _clk=0;
wim 0:dc8789a6e21f 260 wait_us(1);
wim 1:f63d87466f55 261
wim 1:f63d87466f55 262 // Return DIO to output mode
wim 1:f63d87466f55 263 _dio.output();
wim 1:f63d87466f55 264 wait_us(3);
wim 1:f63d87466f55 265
wim 1:f63d87466f55 266 _dio=1; //idle
wim 0:dc8789a6e21f 267 }
wim 0:dc8789a6e21f 268
wim 0:dc8789a6e21f 269 /** Read byte from TM1637
wim 0:dc8789a6e21f 270 * @return read byte
wim 0:dc8789a6e21f 271 */
wim 1:f63d87466f55 272 char TM1637::_read() {
wim 0:dc8789a6e21f 273 char keycode = 0;
wim 1:f63d87466f55 274
wim 1:f63d87466f55 275 // Prepare DIO to read data
wim 1:f63d87466f55 276 _dio.input();
wim 1:f63d87466f55 277 wait_us(3);
wim 0:dc8789a6e21f 278
wim 1:f63d87466f55 279 for (int bit=0; bit<8; bit++) {
wim 1:f63d87466f55 280
wim 1:f63d87466f55 281 //The TM1637 sends bitpattern: S0 S1 S2 K1 K2 1 1 1
wim 1:f63d87466f55 282 //Data is shifted out by the TM1637 on the falling edge of CLK
wim 1:f63d87466f55 283 //Observe sufficient delay to allow the Open Drain DIO to rise to H levels
wim 1:f63d87466f55 284 // Prepare to read next bit, LSB (ie S0) first.
wim 1:f63d87466f55 285 // The code below flips bits for easier matching with datasheet
wim 1:f63d87466f55 286 keycode = keycode << 1;
wim 1:f63d87466f55 287
wim 1:f63d87466f55 288 _clk=1;
wim 0:dc8789a6e21f 289 wait_us(1);
wim 1:f63d87466f55 290
wim 1:f63d87466f55 291 // Read next bit
wim 1:f63d87466f55 292 if (_dio) { keycode |= 0x01; }
wim 0:dc8789a6e21f 293
wim 1:f63d87466f55 294 _clk=0;
wim 1:f63d87466f55 295 wait_us(5); // Delay to allow for slow risetime
wim 0:dc8789a6e21f 296 }
wim 0:dc8789a6e21f 297
wim 1:f63d87466f55 298 // Return DIO to output mode
wim 1:f63d87466f55 299 _dio.output();
wim 1:f63d87466f55 300 wait_us(3);
wim 1:f63d87466f55 301
wim 0:dc8789a6e21f 302 // dummy Ack
wim 1:f63d87466f55 303 _dio=0; //Ack
wim 0:dc8789a6e21f 304 wait_us(1);
wim 1:f63d87466f55 305
wim 1:f63d87466f55 306 _clk=1;
wim 1:f63d87466f55 307 wait_us(1);
wim 1:f63d87466f55 308 _clk=0;
wim 0:dc8789a6e21f 309 wait_us(1);
wim 0:dc8789a6e21f 310
wim 1:f63d87466f55 311 _dio=1; //idle
wim 1:f63d87466f55 312
wim 0:dc8789a6e21f 313 return keycode;
wim 0:dc8789a6e21f 314 }
wim 0:dc8789a6e21f 315
wim 0:dc8789a6e21f 316 /** Write command and parameter to TM1637
wim 0:dc8789a6e21f 317 * @param int cmd Command byte
wim 0:dc8789a6e21f 318 * &Param int data Parameters for command
wim 0:dc8789a6e21f 319 * @return none
wim 0:dc8789a6e21f 320 */
wim 0:dc8789a6e21f 321 void TM1637::_writeCmd(int cmd, int data){
wim 0:dc8789a6e21f 322
wim 0:dc8789a6e21f 323 _start();
wim 0:dc8789a6e21f 324
wim 0:dc8789a6e21f 325 _write((cmd & TM1637_CMD_MSK) | (data & ~TM1637_CMD_MSK));
wim 0:dc8789a6e21f 326
wim 0:dc8789a6e21f 327 _stop();
wim 0:dc8789a6e21f 328 }
wim 0:dc8789a6e21f 329
wim 0:dc8789a6e21f 330
wim 0:dc8789a6e21f 331 #if (CATALEX_TEST == 1)
wim 0:dc8789a6e21f 332 // Derived class for TM1637 used in LED&KEY display unit
wim 0:dc8789a6e21f 333 //
wim 0:dc8789a6e21f 334
wim 1:f63d87466f55 335 #if(SPI==1)
wim 0:dc8789a6e21f 336 /** Constructor for class for driving TM1637 LED controller as used in CATALEX
wim 0:dc8789a6e21f 337 *
wim 1:f63d87466f55 338 * @brief Supports 4 Digits of 7 Segments + DP (or Colon for Digit2 on some models).
wim 1:f63d87466f55 339 * DEPRECATED version
wim 0:dc8789a6e21f 340 *
wim 1:f63d87466f55 341 * @param PinName mosi_nc Serial bus NC pin
wim 1:f63d87466f55 342 * @param PinName miso_dio Serial bus DIO pin
wim 1:f63d87466f55 343 * @param PinName sclk_clk Serial bus CLK pin
wim 0:dc8789a6e21f 344 */
wim 1:f63d87466f55 345 TM1637_CATALEX::TM1637_CATALEX(PinName mosi_nc, PinName miso_dio, PinName sclk_clk) : TM1637(mosi_nc, miso_dio, sclk_clk) {
wim 1:f63d87466f55 346 _column = 0;
wim 1:f63d87466f55 347 _columns = CATALEX_NR_DIGITS;
wim 1:f63d87466f55 348 }
wim 1:f63d87466f55 349 #endif
wim 1:f63d87466f55 350
wim 1:f63d87466f55 351 /** Constructor for class for driving TM1637 LED controller
wim 1:f63d87466f55 352 *
wim 1:f63d87466f55 353 * @brief Supports 4 Digits of 7 Segments + DP (or Colon for Digit2 on some models).
wim 1:f63d87466f55 354 * Serial bus interface device.
wim 1:f63d87466f55 355 *
wim 1:f63d87466f55 356 * @param PinName dio Serial bus DIO pin
wim 1:f63d87466f55 357 * @param PinName sck Serial bus CLK pin
wim 1:f63d87466f55 358 */
wim 1:f63d87466f55 359 TM1637_CATALEX::TM1637_CATALEX(PinName dio, PinName clk) : TM1637(dio, clk) {
wim 0:dc8789a6e21f 360 _column = 0;
wim 0:dc8789a6e21f 361 _columns = CATALEX_NR_DIGITS;
wim 0:dc8789a6e21f 362 }
wim 0:dc8789a6e21f 363
wim 0:dc8789a6e21f 364 #if(0)
wim 0:dc8789a6e21f 365 #if DOXYGEN_ONLY
wim 0:dc8789a6e21f 366 /** Write a character to the Display
wim 0:dc8789a6e21f 367 *
wim 0:dc8789a6e21f 368 * @param c The character to write to the display
wim 0:dc8789a6e21f 369 */
wim 0:dc8789a6e21f 370 int putc(int c);
wim 0:dc8789a6e21f 371
wim 0:dc8789a6e21f 372 /** Write a formatted string to the Display
wim 0:dc8789a6e21f 373 *
wim 0:dc8789a6e21f 374 * @param format A printf-style format string, followed by the
wim 0:dc8789a6e21f 375 * variables to use in formatting the string.
wim 0:dc8789a6e21f 376 */
wim 0:dc8789a6e21f 377 int printf(const char* format, ...);
wim 0:dc8789a6e21f 378 #endif
wim 0:dc8789a6e21f 379 #endif
wim 0:dc8789a6e21f 380
wim 0:dc8789a6e21f 381 /** Locate cursor to a screen column
wim 0:dc8789a6e21f 382 *
wim 0:dc8789a6e21f 383 * @param column The horizontal position from the left, indexed from 0
wim 0:dc8789a6e21f 384 */
wim 0:dc8789a6e21f 385 void TM1637_CATALEX::locate(int column) {
wim 0:dc8789a6e21f 386 //sanity check
wim 0:dc8789a6e21f 387 if (column < 0) {column = 0;}
wim 0:dc8789a6e21f 388 if (column > (_columns - 1)) {column = _columns - 1;}
wim 0:dc8789a6e21f 389
wim 0:dc8789a6e21f 390 _column = column;
wim 0:dc8789a6e21f 391 }
wim 0:dc8789a6e21f 392
wim 0:dc8789a6e21f 393
wim 0:dc8789a6e21f 394 /** Number of screen columns
wim 0:dc8789a6e21f 395 *
wim 0:dc8789a6e21f 396 * @param none
wim 0:dc8789a6e21f 397 * @return columns
wim 0:dc8789a6e21f 398 */
wim 0:dc8789a6e21f 399 int TM1637_CATALEX::columns() {
wim 0:dc8789a6e21f 400 return _columns;
wim 0:dc8789a6e21f 401 }
wim 0:dc8789a6e21f 402
wim 0:dc8789a6e21f 403
wim 0:dc8789a6e21f 404 /** Clear the screen and locate to 0
wim 0:dc8789a6e21f 405 * @param bool clrAll Clear Icons also (default = false)
wim 0:dc8789a6e21f 406 */
wim 0:dc8789a6e21f 407 void TM1637_CATALEX::cls(bool clrAll) {
wim 0:dc8789a6e21f 408
wim 0:dc8789a6e21f 409 if (clrAll) {
wim 0:dc8789a6e21f 410 //clear local buffer (inclumosig Icons)
wim 0:dc8789a6e21f 411 for (int idx=0; idx < CATALEX_NR_GRIDS; idx++) {
wim 0:dc8789a6e21f 412 _displaybuffer[idx] = 0x00;
wim 0:dc8789a6e21f 413 }
wim 0:dc8789a6e21f 414 }
wim 0:dc8789a6e21f 415 else {
wim 0:dc8789a6e21f 416 //clear local buffer (preserving Icons)
wim 0:dc8789a6e21f 417 for (int idx=0; idx < CATALEX_NR_GRIDS; idx++) {
wim 0:dc8789a6e21f 418 _displaybuffer[idx] = _displaybuffer[idx] & MASK_ICON_GRID[idx];
wim 0:dc8789a6e21f 419 }
wim 0:dc8789a6e21f 420 }
wim 0:dc8789a6e21f 421
wim 1:f63d87466f55 422 writeData(_displaybuffer, (CATALEX_NR_GRIDS * TM1637_BYTES_PER_GRID), 0);
wim 0:dc8789a6e21f 423
wim 0:dc8789a6e21f 424 _column = 0;
wim 0:dc8789a6e21f 425 }
wim 0:dc8789a6e21f 426
wim 0:dc8789a6e21f 427 /** Set Icon
wim 0:dc8789a6e21f 428 *
wim 0:dc8789a6e21f 429 * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 16 LSBs
wim 0:dc8789a6e21f 430 * @return none
wim 0:dc8789a6e21f 431 */
wim 0:dc8789a6e21f 432 void TM1637_CATALEX::setIcon(Icon icon) {
wim 0:dc8789a6e21f 433 int addr, icn;
wim 0:dc8789a6e21f 434
wim 0:dc8789a6e21f 435 icn = icon & 0xFFFF;
wim 0:dc8789a6e21f 436 addr = (icon >> 24) & 0xFF;
wim 0:dc8789a6e21f 437 addr = (addr - 1);
wim 0:dc8789a6e21f 438
wim 0:dc8789a6e21f 439 //Save char...and set bits for icon to write
wim 0:dc8789a6e21f 440 _displaybuffer[addr] = _displaybuffer[addr] | LO(icn);
wim 1:f63d87466f55 441 // writeData(_displaybuffer, (CATALEX_NR_GRIDS * TM1637_BYTES_PER_GRID), 0);
wim 0:dc8789a6e21f 442 writeData(_displaybuffer, TM1637_BYTES_PER_GRID, addr);
wim 0:dc8789a6e21f 443 }
wim 0:dc8789a6e21f 444
wim 0:dc8789a6e21f 445 /** Clr Icon
wim 0:dc8789a6e21f 446 *
wim 0:dc8789a6e21f 447 * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 16 LSBs
wim 0:dc8789a6e21f 448 * @return none
wim 0:dc8789a6e21f 449 */
wim 0:dc8789a6e21f 450 void TM1637_CATALEX::clrIcon(Icon icon) {
wim 0:dc8789a6e21f 451 int addr, icn;
wim 0:dc8789a6e21f 452
wim 0:dc8789a6e21f 453 icn = icon & 0xFFFF;
wim 0:dc8789a6e21f 454 addr = (icon >> 24) & 0xFF;
wim 0:dc8789a6e21f 455 addr = (addr - 1);
wim 0:dc8789a6e21f 456
wim 0:dc8789a6e21f 457 //Save char...and clr bits for icon to write
wim 0:dc8789a6e21f 458 _displaybuffer[addr] = _displaybuffer[addr] & ~LO(icn);
wim 1:f63d87466f55 459 // writeData(_displaybuffer, (CATALEX_NR_GRIDS * TM1637_BYTES_PER_GRID), 0);
wim 0:dc8789a6e21f 460 writeData(_displaybuffer, TM1637_BYTES_PER_GRID, addr);
wim 0:dc8789a6e21f 461 }
wim 0:dc8789a6e21f 462
wim 0:dc8789a6e21f 463
wim 0:dc8789a6e21f 464 /** Set User Defined Characters (UDC)
wim 0:dc8789a6e21f 465 *
wim 0:dc8789a6e21f 466 * @param unsigned char udc_idx The Index of the UDC (0..7)
wim 0:dc8789a6e21f 467 * @param int udc_data The bitpattern for the UDC (8 bits)
wim 0:dc8789a6e21f 468 */
wim 0:dc8789a6e21f 469 void TM1637_CATALEX::setUDC(unsigned char udc_idx, int udc_data) {
wim 0:dc8789a6e21f 470
wim 0:dc8789a6e21f 471 //Sanity check
wim 0:dc8789a6e21f 472 if (udc_idx > (CATALEX_NR_UDC-1)) {
wim 0:dc8789a6e21f 473 return;
wim 0:dc8789a6e21f 474 }
wim 0:dc8789a6e21f 475 // Mask out Icon bits?
wim 0:dc8789a6e21f 476
wim 0:dc8789a6e21f 477 _UDC_7S[udc_idx] = LO(udc_data);
wim 0:dc8789a6e21f 478 }
wim 0:dc8789a6e21f 479
wim 0:dc8789a6e21f 480
wim 0:dc8789a6e21f 481 /** Write a single character (Stream implementation)
wim 0:dc8789a6e21f 482 */
wim 0:dc8789a6e21f 483 int TM1637_CATALEX::_putc(int value) {
wim 1:f63d87466f55 484 //The CATALAX mapping between Digit positions (Left to Right) and Grids is:
wim 1:f63d87466f55 485 // GR1 GR2 GR3 GR4
wim 1:f63d87466f55 486 //The memory addresses or column numbers are:
wim 1:f63d87466f55 487 // 0 1 2 3
wim 1:f63d87466f55 488
wim 0:dc8789a6e21f 489 int addr;
wim 0:dc8789a6e21f 490 bool validChar = false;
wim 0:dc8789a6e21f 491 char pattern = 0x00;
wim 0:dc8789a6e21f 492
wim 0:dc8789a6e21f 493 if ((value == '\n') || (value == '\r')) {
wim 0:dc8789a6e21f 494 //No character to write
wim 0:dc8789a6e21f 495 validChar = false;
wim 0:dc8789a6e21f 496
wim 0:dc8789a6e21f 497 //Update Cursor
wim 0:dc8789a6e21f 498 _column = 0;
wim 0:dc8789a6e21f 499 }
wim 0:dc8789a6e21f 500 else if ((value == '.') || (value == ',')) {
wim 0:dc8789a6e21f 501 //No character to write
wim 0:dc8789a6e21f 502 validChar = false;
wim 0:dc8789a6e21f 503 pattern = S7_DP; // placeholder for all DPs
wim 0:dc8789a6e21f 504
wim 0:dc8789a6e21f 505 // Check to see that DP can be shown for current column
wim 0:dc8789a6e21f 506 if (_column > 0) {
wim 0:dc8789a6e21f 507 //Translate between _column and displaybuffer entries
wim 0:dc8789a6e21f 508 //Add DP to bitpattern of digit left of current column.
wim 0:dc8789a6e21f 509 addr = (_column - 1);
wim 0:dc8789a6e21f 510
wim 0:dc8789a6e21f 511 //Save icons...and set bits for decimal point to write
wim 0:dc8789a6e21f 512 _displaybuffer[addr] = _displaybuffer[addr] | pattern;
wim 0:dc8789a6e21f 513 // writeData(_displaybuffer, (CATALEX_NR_GRIDS * TM1637_BYTES_PER_GRID));
wim 0:dc8789a6e21f 514 writeData(_displaybuffer, TM1637_BYTES_PER_GRID, addr);
wim 0:dc8789a6e21f 515
wim 0:dc8789a6e21f 516 //No Cursor Update
wim 0:dc8789a6e21f 517 }
wim 0:dc8789a6e21f 518 }
wim 0:dc8789a6e21f 519 else if ((value >= 0) && (value < CATALEX_NR_UDC)) {
wim 0:dc8789a6e21f 520 //Character to write
wim 0:dc8789a6e21f 521 validChar = true;
wim 0:dc8789a6e21f 522 pattern = _UDC_7S[value];
wim 0:dc8789a6e21f 523 }
wim 0:dc8789a6e21f 524
wim 0:dc8789a6e21f 525 #if (SHOW_ASCII == 1)
wim 0:dc8789a6e21f 526 //display all ASCII characters
wim 0:dc8789a6e21f 527 else if ((value >= FONT_7S_START) && (value <= FONT_7S_END)) {
wim 0:dc8789a6e21f 528 //Character to write
wim 0:dc8789a6e21f 529 validChar = true;
wim 0:dc8789a6e21f 530 pattern = FONT_7S[value - FONT_7S_START];
wim 0:dc8789a6e21f 531 } // else
wim 0:dc8789a6e21f 532 #else
wim 0:dc8789a6e21f 533 //display only digits and hex characters
wim 0:dc8789a6e21f 534 else if (value == '-') {
wim 0:dc8789a6e21f 535 //Character to write
wim 0:dc8789a6e21f 536 validChar = true;
wim 0:dc8789a6e21f 537 pattern = C7_MIN;
wim 0:dc8789a6e21f 538 }
wim 0:dc8789a6e21f 539 else if ((value >= (int)'0') && (value <= (int) '9')) {
wim 0:dc8789a6e21f 540 //Character to write
wim 0:dc8789a6e21f 541 validChar = true;
wim 0:dc8789a6e21f 542 pattern = FONT_7S[value - (int) '0'];
wim 0:dc8789a6e21f 543 }
wim 0:dc8789a6e21f 544 else if ((value >= (int) 'A') && (value <= (int) 'F')) {
wim 0:dc8789a6e21f 545 //Character to write
wim 0:dc8789a6e21f 546 validChar = true;
wim 0:dc8789a6e21f 547 pattern = FONT_7S[10 + value - (int) 'A'];
wim 0:dc8789a6e21f 548 }
wim 0:dc8789a6e21f 549 else if ((value >= (int) 'a') && (value <= (int) 'f')) {
wim 0:dc8789a6e21f 550 //Character to write
wim 0:dc8789a6e21f 551 validChar = true;
wim 0:dc8789a6e21f 552 pattern = FONT_7S[10 + value - (int) 'a'];
wim 0:dc8789a6e21f 553 } //else
wim 0:dc8789a6e21f 554 #endif
wim 0:dc8789a6e21f 555
wim 0:dc8789a6e21f 556 if (validChar) {
wim 0:dc8789a6e21f 557 //Character to write
wim 0:dc8789a6e21f 558
wim 0:dc8789a6e21f 559 //Translate between _column and displaybuffer entries
wim 0:dc8789a6e21f 560 addr = _column;
wim 0:dc8789a6e21f 561
wim 0:dc8789a6e21f 562 //Save icons...and set bits for character to write
wim 0:dc8789a6e21f 563 _displaybuffer[addr] = (_displaybuffer[addr] & MASK_ICON_GRID[_column]) | pattern;
wim 0:dc8789a6e21f 564
wim 0:dc8789a6e21f 565 // writeData(_displaybuffer, (CATALEX_NR_GRIDS * TM1637_BYTES_PER_GRID));
wim 0:dc8789a6e21f 566 writeData(_displaybuffer, TM1637_BYTES_PER_GRID, addr);
wim 0:dc8789a6e21f 567
wim 0:dc8789a6e21f 568 //Update Cursor
wim 0:dc8789a6e21f 569 _column++;
wim 0:dc8789a6e21f 570 if (_column > (CATALEX_NR_DIGITS - 1)) {
wim 0:dc8789a6e21f 571 _column = 0;
wim 0:dc8789a6e21f 572 }
wim 0:dc8789a6e21f 573
wim 0:dc8789a6e21f 574 } // if validChar
wim 0:dc8789a6e21f 575
wim 0:dc8789a6e21f 576 return value;
wim 0:dc8789a6e21f 577 }
wim 0:dc8789a6e21f 578
wim 0:dc8789a6e21f 579
wim 0:dc8789a6e21f 580 // get a single character (Stream implementation)
wim 0:dc8789a6e21f 581 int TM1637_CATALEX::_getc() {
wim 0:dc8789a6e21f 582 return -1;
wim 0:dc8789a6e21f 583 }
wim 0:dc8789a6e21f 584
wim 0:dc8789a6e21f 585 #endif
wim 1:f63d87466f55 586
wim 1:f63d87466f55 587 #if (ROBOTDYN_TEST == 1)
wim 1:f63d87466f55 588 // Derived class for TM1637 used in ROBOTDYN display unit
wim 1:f63d87466f55 589 //
wim 1:f63d87466f55 590
wim 1:f63d87466f55 591 /** Constructor for class for driving TM1637 LED controller as used in ROBOTDYN
wim 1:f63d87466f55 592 *
wim 1:f63d87466f55 593 * @brief Supports 6 Digits of 7 Segments + DP.
wim 1:f63d87466f55 594 *
wim 1:f63d87466f55 595 * @param PinName dio Serial bus DIO pin
wim 1:f63d87466f55 596 * @param PinName clk Serial bus CLK pin
wim 1:f63d87466f55 597 */
wim 1:f63d87466f55 598 TM1637_ROBOTDYN::TM1637_ROBOTDYN(PinName dio, PinName clk) : TM1637(dio, clk) {
wim 1:f63d87466f55 599 _column = 0;
wim 1:f63d87466f55 600 _columns = ROBOTDYN_NR_DIGITS;
wim 1:f63d87466f55 601 }
wim 1:f63d87466f55 602
wim 1:f63d87466f55 603
wim 1:f63d87466f55 604 #if(0)
wim 1:f63d87466f55 605 #if DOXYGEN_ONLY
wim 1:f63d87466f55 606 /** Write a character to the Display
wim 1:f63d87466f55 607 *
wim 1:f63d87466f55 608 * @param c The character to write to the display
wim 1:f63d87466f55 609 */
wim 1:f63d87466f55 610 int putc(int c);
wim 1:f63d87466f55 611
wim 1:f63d87466f55 612 /** Write a formatted string to the Display
wim 1:f63d87466f55 613 *
wim 1:f63d87466f55 614 * @param format A printf-style format string, followed by the
wim 1:f63d87466f55 615 * variables to use in formatting the string.
wim 1:f63d87466f55 616 */
wim 1:f63d87466f55 617 int printf(const char* format, ...);
wim 1:f63d87466f55 618 #endif
wim 1:f63d87466f55 619 #endif
wim 1:f63d87466f55 620
wim 1:f63d87466f55 621 /** Locate cursor to a screen column
wim 1:f63d87466f55 622 *
wim 1:f63d87466f55 623 * @param column The horizontal position from the left, indexed from 0
wim 1:f63d87466f55 624 */
wim 1:f63d87466f55 625 void TM1637_ROBOTDYN::locate(int column) {
wim 1:f63d87466f55 626 //sanity check
wim 1:f63d87466f55 627 if (column < 0) {column = 0;}
wim 1:f63d87466f55 628 if (column > (_columns - 1)) {column = _columns - 1;}
wim 1:f63d87466f55 629
wim 1:f63d87466f55 630 _column = column;
wim 1:f63d87466f55 631 }
wim 1:f63d87466f55 632
wim 1:f63d87466f55 633
wim 1:f63d87466f55 634 /** Number of screen columns
wim 1:f63d87466f55 635 *
wim 1:f63d87466f55 636 * @param none
wim 1:f63d87466f55 637 * @return columns
wim 1:f63d87466f55 638 */
wim 1:f63d87466f55 639 int TM1637_ROBOTDYN::columns() {
wim 1:f63d87466f55 640 return _columns;
wim 1:f63d87466f55 641 }
wim 1:f63d87466f55 642
wim 1:f63d87466f55 643
wim 1:f63d87466f55 644 /** Clear the screen and locate to 0
wim 1:f63d87466f55 645 * @param bool clrAll Clear Icons also (default = false)
wim 1:f63d87466f55 646 */
wim 1:f63d87466f55 647 void TM1637_ROBOTDYN::cls(bool clrAll) {
wim 1:f63d87466f55 648
wim 1:f63d87466f55 649 if (clrAll) {
wim 1:f63d87466f55 650 //clear local buffer (including Icons)
wim 1:f63d87466f55 651 for (int idx=0; idx < ROBOTDYN_NR_GRIDS; idx++) {
wim 1:f63d87466f55 652 _displaybuffer[idx] = 0x00;
wim 1:f63d87466f55 653 }
wim 1:f63d87466f55 654 }
wim 1:f63d87466f55 655 else {
wim 1:f63d87466f55 656 //clear local buffer (preserving Icons)
wim 1:f63d87466f55 657 for (int idx=0; idx < ROBOTDYN_NR_GRIDS; idx++) {
wim 1:f63d87466f55 658 _displaybuffer[idx] = _displaybuffer[idx] & MASK_ICON_GRID[idx];
wim 1:f63d87466f55 659 }
wim 1:f63d87466f55 660 }
wim 1:f63d87466f55 661
wim 1:f63d87466f55 662 writeData(_displaybuffer, (ROBOTDYN_NR_GRIDS * TM1637_BYTES_PER_GRID), 0);
wim 1:f63d87466f55 663
wim 1:f63d87466f55 664 _column = 0;
wim 1:f63d87466f55 665 }
wim 1:f63d87466f55 666
wim 1:f63d87466f55 667 /** Set Icon
wim 1:f63d87466f55 668 *
wim 1:f63d87466f55 669 * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 16 LSBs
wim 1:f63d87466f55 670 * @return none
wim 1:f63d87466f55 671 */
wim 1:f63d87466f55 672 void TM1637_ROBOTDYN::setIcon(Icon icon) {
wim 1:f63d87466f55 673 int addr, icn;
wim 1:f63d87466f55 674
wim 1:f63d87466f55 675 icn = icon & 0xFFFF;
wim 1:f63d87466f55 676 addr = (icon >> 24) & 0xFF;
wim 1:f63d87466f55 677 addr = (addr - 1);
wim 1:f63d87466f55 678
wim 1:f63d87466f55 679 //Save char...and set bits for icon to write
wim 1:f63d87466f55 680 _displaybuffer[addr] = _displaybuffer[addr] | LO(icn);
wim 1:f63d87466f55 681 // writeData(_displaybuffer, (ROBOTDYN_NR_GRIDS * TM1637_BYTES_PER_GRID), 0);
wim 1:f63d87466f55 682 writeData(_displaybuffer, TM1637_BYTES_PER_GRID, addr);
wim 1:f63d87466f55 683 }
wim 1:f63d87466f55 684
wim 1:f63d87466f55 685 /** Clr Icon
wim 1:f63d87466f55 686 *
wim 1:f63d87466f55 687 * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 16 LSBs
wim 1:f63d87466f55 688 * @return none
wim 1:f63d87466f55 689 */
wim 1:f63d87466f55 690 void TM1637_ROBOTDYN::clrIcon(Icon icon) {
wim 1:f63d87466f55 691 int addr, icn;
wim 1:f63d87466f55 692
wim 1:f63d87466f55 693 icn = icon & 0xFFFF;
wim 1:f63d87466f55 694 addr = (icon >> 24) & 0xFF;
wim 1:f63d87466f55 695 addr = (addr - 1);
wim 1:f63d87466f55 696
wim 1:f63d87466f55 697 //Save char...and clr bits for icon to write
wim 1:f63d87466f55 698 _displaybuffer[addr] = _displaybuffer[addr] & ~LO(icn);
wim 1:f63d87466f55 699 // writeData(_displaybuffer, (ROBOTDYN_NR_GRIDS * TM1637_BYTES_PER_GRID), 0);
wim 1:f63d87466f55 700 writeData(_displaybuffer, TM1637_BYTES_PER_GRID, addr);
wim 1:f63d87466f55 701 }
wim 1:f63d87466f55 702
wim 1:f63d87466f55 703
wim 1:f63d87466f55 704 /** Set User Defined Characters (UDC)
wim 1:f63d87466f55 705 *
wim 1:f63d87466f55 706 * @param unsigned char udc_idx The Index of the UDC (0..7)
wim 1:f63d87466f55 707 * @param int udc_data The bitpattern for the UDC (8 bits)
wim 1:f63d87466f55 708 */
wim 1:f63d87466f55 709 void TM1637_ROBOTDYN::setUDC(unsigned char udc_idx, int udc_data) {
wim 1:f63d87466f55 710
wim 1:f63d87466f55 711 //Sanity check
wim 1:f63d87466f55 712 if (udc_idx > (ROBOTDYN_NR_UDC-1)) {
wim 1:f63d87466f55 713 return;
wim 1:f63d87466f55 714 }
wim 1:f63d87466f55 715 // Mask out Icon bits?
wim 1:f63d87466f55 716
wim 1:f63d87466f55 717 _UDC_7S[udc_idx] = LO(udc_data);
wim 1:f63d87466f55 718 }
wim 1:f63d87466f55 719
wim 1:f63d87466f55 720 /** Write a single character (Stream implementation)
wim 1:f63d87466f55 721 *
wim 1:f63d87466f55 722 */
wim 1:f63d87466f55 723 int TM1637_ROBOTDYN::_putc(int value) {
wim 1:f63d87466f55 724 //The ROBOTDYN mapping between Digit positions (Left to Right) and Grids is:
wim 1:f63d87466f55 725 // GR3 GR2 GR1 GR6 GR5 GR4
wim 1:f63d87466f55 726 //The memory addresses or column numbers are:
wim 1:f63d87466f55 727 // 2 1 0 5 4 3
wim 1:f63d87466f55 728 //The Grids are reversed for 2 sets of 3 digits:
wim 1:f63d87466f55 729 const int col2addr[] = {2, 1, 0, 5, 4, 3};
wim 1:f63d87466f55 730
wim 1:f63d87466f55 731 int addr;
wim 1:f63d87466f55 732 bool validChar = false;
wim 1:f63d87466f55 733 char pattern = 0x00;
wim 1:f63d87466f55 734
wim 1:f63d87466f55 735 if ((value == '\n') || (value == '\r')) {
wim 1:f63d87466f55 736 //No character to write
wim 1:f63d87466f55 737 validChar = false;
wim 1:f63d87466f55 738
wim 1:f63d87466f55 739 //Update Cursor
wim 1:f63d87466f55 740 _column = 0;
wim 1:f63d87466f55 741 }
wim 1:f63d87466f55 742 else if ((value == '.') || (value == ',')) {
wim 1:f63d87466f55 743 //No character to write
wim 1:f63d87466f55 744 validChar = false;
wim 1:f63d87466f55 745 pattern = S7_DP; // placeholder for all DPs
wim 1:f63d87466f55 746
wim 1:f63d87466f55 747 // Check to see that DP can be shown for current column
wim 1:f63d87466f55 748 if (_column > 0) {
wim 1:f63d87466f55 749 //Translate between _column and displaybuffer entries
wim 1:f63d87466f55 750 //Add DP to bitpattern of digit left of current column.
wim 1:f63d87466f55 751 addr = col2addr [_column - 1];
wim 1:f63d87466f55 752
wim 1:f63d87466f55 753 //Save icons...and set bits for decimal point to write
wim 1:f63d87466f55 754 _displaybuffer[addr] = _displaybuffer[addr] | pattern;
wim 1:f63d87466f55 755 // writeData(_displaybuffer, (ROBOTDYN_NR_GRIDS * TM1637_BYTES_PER_GRID));
wim 1:f63d87466f55 756 writeData(_displaybuffer, TM1637_BYTES_PER_GRID, addr);
wim 1:f63d87466f55 757
wim 1:f63d87466f55 758 //No Cursor Update
wim 1:f63d87466f55 759 }
wim 1:f63d87466f55 760 }
wim 1:f63d87466f55 761 else if ((value >= 0) && (value < ROBOTDYN_NR_UDC)) {
wim 1:f63d87466f55 762 //Character to write
wim 1:f63d87466f55 763 validChar = true;
wim 1:f63d87466f55 764 pattern = _UDC_7S[value];
wim 1:f63d87466f55 765 }
wim 1:f63d87466f55 766
wim 1:f63d87466f55 767 #if (SHOW_ASCII == 1)
wim 1:f63d87466f55 768 //display all ASCII characters
wim 1:f63d87466f55 769 else if ((value >= FONT_7S_START) && (value <= FONT_7S_END)) {
wim 1:f63d87466f55 770 //Character to write
wim 1:f63d87466f55 771 validChar = true;
wim 1:f63d87466f55 772 pattern = FONT_7S[value - FONT_7S_START];
wim 1:f63d87466f55 773 } // else
wim 1:f63d87466f55 774 #else
wim 1:f63d87466f55 775 //display only digits and hex characters
wim 1:f63d87466f55 776 else if (value == '-') {
wim 1:f63d87466f55 777 //Character to write
wim 1:f63d87466f55 778 validChar = true;
wim 1:f63d87466f55 779 pattern = C7_MIN;
wim 1:f63d87466f55 780 }
wim 1:f63d87466f55 781 else if ((value >= (int)'0') && (value <= (int) '9')) {
wim 1:f63d87466f55 782 //Character to write
wim 1:f63d87466f55 783 validChar = true;
wim 1:f63d87466f55 784 pattern = FONT_7S[value - (int) '0'];
wim 1:f63d87466f55 785 }
wim 1:f63d87466f55 786 else if ((value >= (int) 'A') && (value <= (int) 'F')) {
wim 1:f63d87466f55 787 //Character to write
wim 1:f63d87466f55 788 validChar = true;
wim 1:f63d87466f55 789 pattern = FONT_7S[10 + value - (int) 'A'];
wim 1:f63d87466f55 790 }
wim 1:f63d87466f55 791 else if ((value >= (int) 'a') && (value <= (int) 'f')) {
wim 1:f63d87466f55 792 //Character to write
wim 1:f63d87466f55 793 validChar = true;
wim 1:f63d87466f55 794 pattern = FONT_7S[10 + value - (int) 'a'];
wim 1:f63d87466f55 795 } //else
wim 1:f63d87466f55 796 #endif
wim 1:f63d87466f55 797
wim 1:f63d87466f55 798 if (validChar) {
wim 1:f63d87466f55 799 //Character to write
wim 1:f63d87466f55 800
wim 1:f63d87466f55 801 //Translate between _column and displaybuffer entries
wim 1:f63d87466f55 802 addr = col2addr[_column];
wim 1:f63d87466f55 803
wim 1:f63d87466f55 804 //Save icons...and set bits for character to write
wim 1:f63d87466f55 805 _displaybuffer[addr] = (_displaybuffer[addr] & MASK_ICON_GRID[_column]) | pattern;
wim 1:f63d87466f55 806
wim 1:f63d87466f55 807 // writeData(_displaybuffer, (ROBOTDYN_NR_GRIDS * TM1637_BYTES_PER_GRID));
wim 1:f63d87466f55 808 writeData(_displaybuffer, TM1637_BYTES_PER_GRID, addr);
wim 1:f63d87466f55 809
wim 1:f63d87466f55 810 //Update Cursor
wim 1:f63d87466f55 811 _column++;
wim 1:f63d87466f55 812 if (_column > (ROBOTDYN_NR_DIGITS - 1)) {
wim 1:f63d87466f55 813 _column = 0;
wim 1:f63d87466f55 814 }
wim 1:f63d87466f55 815
wim 1:f63d87466f55 816 } // if validChar
wim 1:f63d87466f55 817
wim 1:f63d87466f55 818 return value;
wim 1:f63d87466f55 819 }
wim 1:f63d87466f55 820
wim 1:f63d87466f55 821
wim 1:f63d87466f55 822 // get a single character (Stream implementation)
wim 1:f63d87466f55 823 int TM1637_ROBOTDYN::_getc() {
wim 1:f63d87466f55 824 return -1;
wim 1:f63d87466f55 825 }
wim 1:f63d87466f55 826
wim 1:f63d87466f55 827 #endif
wim 1:f63d87466f55 828
wim 1:f63d87466f55 829
wim 1:f63d87466f55 830 #if (EYEWINK_TEST == 1)
wim 1:f63d87466f55 831 // Derived class for TM1637 used in EYEWINK display unit
wim 1:f63d87466f55 832 //
wim 1:f63d87466f55 833
wim 1:f63d87466f55 834 /** Constructor for class for driving TM1637 LED controller as used in EYEWINK
wim 1:f63d87466f55 835 *
wim 1:f63d87466f55 836 * @brief Supports 6 Digits of 7 Segments + DP and 6 Keys.
wim 1:f63d87466f55 837 *
wim 1:f63d87466f55 838 * @param PinName dio Serial bus DIO pin
wim 1:f63d87466f55 839 * @param PinName clk Serial bus CLK pin
wim 1:f63d87466f55 840 */
wim 1:f63d87466f55 841 TM1637_EYEWINK::TM1637_EYEWINK(PinName dio, PinName clk) : TM1637(dio, clk) {
wim 1:f63d87466f55 842 _column = 0;
wim 1:f63d87466f55 843 _columns = EYEWINK_NR_DIGITS;
wim 1:f63d87466f55 844 }
wim 1:f63d87466f55 845
wim 1:f63d87466f55 846
wim 1:f63d87466f55 847 #if(0)
wim 1:f63d87466f55 848 #if DOXYGEN_ONLY
wim 1:f63d87466f55 849 /** Write a character to the Display
wim 1:f63d87466f55 850 *
wim 1:f63d87466f55 851 * @param c The character to write to the display
wim 1:f63d87466f55 852 */
wim 1:f63d87466f55 853 int putc(int c);
wim 1:f63d87466f55 854
wim 1:f63d87466f55 855 /** Write a formatted string to the Display
wim 1:f63d87466f55 856 *
wim 1:f63d87466f55 857 * @param format A printf-style format string, followed by the
wim 1:f63d87466f55 858 * variables to use in formatting the string.
wim 1:f63d87466f55 859 */
wim 1:f63d87466f55 860 int printf(const char* format, ...);
wim 1:f63d87466f55 861 #endif
wim 1:f63d87466f55 862 #endif
wim 1:f63d87466f55 863
wim 1:f63d87466f55 864 /** Locate cursor to a screen column
wim 1:f63d87466f55 865 *
wim 1:f63d87466f55 866 * @param column The horizontal position from the left, indexed from 0
wim 1:f63d87466f55 867 */
wim 1:f63d87466f55 868 void TM1637_EYEWINK::locate(int column) {
wim 1:f63d87466f55 869 //sanity check
wim 1:f63d87466f55 870 if (column < 0) {column = 0;}
wim 1:f63d87466f55 871 if (column > (_columns - 1)) {column = _columns - 1;}
wim 1:f63d87466f55 872
wim 1:f63d87466f55 873 _column = column;
wim 1:f63d87466f55 874 }
wim 1:f63d87466f55 875
wim 1:f63d87466f55 876
wim 1:f63d87466f55 877 /** Number of screen columns
wim 1:f63d87466f55 878 *
wim 1:f63d87466f55 879 * @param none
wim 1:f63d87466f55 880 * @return columns
wim 1:f63d87466f55 881 */
wim 1:f63d87466f55 882 int TM1637_EYEWINK::columns() {
wim 1:f63d87466f55 883 return _columns;
wim 1:f63d87466f55 884 }
wim 1:f63d87466f55 885
wim 1:f63d87466f55 886
wim 1:f63d87466f55 887 /** Clear the screen and locate to 0
wim 1:f63d87466f55 888 * @param bool clrAll Clear Icons also (default = false)
wim 1:f63d87466f55 889 */
wim 1:f63d87466f55 890 void TM1637_EYEWINK::cls(bool clrAll) {
wim 1:f63d87466f55 891
wim 1:f63d87466f55 892 if (clrAll) {
wim 1:f63d87466f55 893 //clear local buffer (including Icons)
wim 1:f63d87466f55 894 for (int idx=0; idx < EYEWINK_NR_GRIDS; idx++) {
wim 1:f63d87466f55 895 _displaybuffer[idx] = 0x00;
wim 1:f63d87466f55 896 }
wim 1:f63d87466f55 897 }
wim 1:f63d87466f55 898 else {
wim 1:f63d87466f55 899 //clear local buffer (preserving Icons)
wim 1:f63d87466f55 900 for (int idx=0; idx < EYEWINK_NR_GRIDS; idx++) {
wim 1:f63d87466f55 901 _displaybuffer[idx] = _displaybuffer[idx] & MASK_ICON_GRID[idx];
wim 1:f63d87466f55 902 }
wim 1:f63d87466f55 903 }
wim 1:f63d87466f55 904
wim 1:f63d87466f55 905 writeData(_displaybuffer, (EYEWINK_NR_GRIDS * TM1637_BYTES_PER_GRID), 0);
wim 1:f63d87466f55 906
wim 1:f63d87466f55 907 _column = 0;
wim 1:f63d87466f55 908 }
wim 1:f63d87466f55 909
wim 1:f63d87466f55 910 /** Set Icon
wim 1:f63d87466f55 911 *
wim 1:f63d87466f55 912 * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 16 LSBs
wim 1:f63d87466f55 913 * @return none
wim 1:f63d87466f55 914 */
wim 1:f63d87466f55 915 void TM1637_EYEWINK::setIcon(Icon icon) {
wim 1:f63d87466f55 916 int addr, icn;
wim 1:f63d87466f55 917
wim 1:f63d87466f55 918 icn = icon & 0xFFFF;
wim 1:f63d87466f55 919 addr = (icon >> 24) & 0xFF;
wim 1:f63d87466f55 920 addr = (addr - 1);
wim 1:f63d87466f55 921
wim 1:f63d87466f55 922 //Save char...and set bits for icon to write
wim 1:f63d87466f55 923 _displaybuffer[addr] = _displaybuffer[addr] | LO(icn);
wim 1:f63d87466f55 924 // writeData(_displaybuffer, (EYEWINK_NR_GRIDS * TM1637_BYTES_PER_GRID), 0);
wim 1:f63d87466f55 925 writeData(_displaybuffer, TM1637_BYTES_PER_GRID, addr);
wim 1:f63d87466f55 926 }
wim 1:f63d87466f55 927
wim 1:f63d87466f55 928 /** Clr Icon
wim 1:f63d87466f55 929 *
wim 1:f63d87466f55 930 * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 16 LSBs
wim 1:f63d87466f55 931 * @return none
wim 1:f63d87466f55 932 */
wim 1:f63d87466f55 933 void TM1637_EYEWINK::clrIcon(Icon icon) {
wim 1:f63d87466f55 934 int addr, icn;
wim 1:f63d87466f55 935
wim 1:f63d87466f55 936 icn = icon & 0xFFFF;
wim 1:f63d87466f55 937 addr = (icon >> 24) & 0xFF;
wim 1:f63d87466f55 938 addr = (addr - 1);
wim 1:f63d87466f55 939
wim 1:f63d87466f55 940 //Save char...and clr bits for icon to write
wim 1:f63d87466f55 941 _displaybuffer[addr] = _displaybuffer[addr] & ~LO(icn);
wim 1:f63d87466f55 942 // writeData(_displaybuffer, (EYEWINK_NR_GRIDS * TM1637_BYTES_PER_GRID), 0);
wim 1:f63d87466f55 943 writeData(_displaybuffer, TM1637_BYTES_PER_GRID, addr);
wim 1:f63d87466f55 944 }
wim 1:f63d87466f55 945
wim 1:f63d87466f55 946
wim 1:f63d87466f55 947 /** Set User Defined Characters (UDC)
wim 1:f63d87466f55 948 *
wim 1:f63d87466f55 949 * @param unsigned char udc_idx The Index of the UDC (0..7)
wim 1:f63d87466f55 950 * @param int udc_data The bitpattern for the UDC (8 bits)
wim 1:f63d87466f55 951 */
wim 1:f63d87466f55 952 void TM1637_EYEWINK::setUDC(unsigned char udc_idx, int udc_data) {
wim 1:f63d87466f55 953
wim 1:f63d87466f55 954 //Sanity check
wim 1:f63d87466f55 955 if (udc_idx > (EYEWINK_NR_UDC-1)) {
wim 1:f63d87466f55 956 return;
wim 1:f63d87466f55 957 }
wim 1:f63d87466f55 958 // Mask out Icon bits?
wim 1:f63d87466f55 959
wim 1:f63d87466f55 960 _UDC_7S[udc_idx] = LO(udc_data);
wim 1:f63d87466f55 961 }
wim 1:f63d87466f55 962
wim 1:f63d87466f55 963 /** Write a single character (Stream implementation)
wim 1:f63d87466f55 964 *
wim 1:f63d87466f55 965 */
wim 1:f63d87466f55 966 int TM1637_EYEWINK::_putc(int value) {
wim 1:f63d87466f55 967 //The EYEWINK mapping between Digit positions (Left to Right) and Grids is:
wim 1:f63d87466f55 968 // GR1 GR2 GR3 GR4 GR5 GR6
wim 1:f63d87466f55 969 //The memory addresses or column numbers are:
wim 1:f63d87466f55 970 // 0 1 2 3 4 5
wim 1:f63d87466f55 971 //The Grids for all digits:
wim 1:f63d87466f55 972 // const int col2addr[] = {0, 1, 2, 3, 4, 5};
wim 1:f63d87466f55 973
wim 1:f63d87466f55 974 int addr;
wim 1:f63d87466f55 975 bool validChar = false;
wim 1:f63d87466f55 976 char pattern = 0x00;
wim 1:f63d87466f55 977
wim 1:f63d87466f55 978 if ((value == '\n') || (value == '\r')) {
wim 1:f63d87466f55 979 //No character to write
wim 1:f63d87466f55 980 validChar = false;
wim 1:f63d87466f55 981
wim 1:f63d87466f55 982 //Update Cursor
wim 1:f63d87466f55 983 _column = 0;
wim 1:f63d87466f55 984 }
wim 1:f63d87466f55 985 else if ((value == '.') || (value == ',')) {
wim 1:f63d87466f55 986 //No character to write
wim 1:f63d87466f55 987 validChar = false;
wim 1:f63d87466f55 988 pattern = S7_DP; // placeholder for all DPs
wim 1:f63d87466f55 989
wim 1:f63d87466f55 990 // Check to see that DP can be shown for current column
wim 1:f63d87466f55 991 if (_column > 0) {
wim 1:f63d87466f55 992 //Translate between _column and displaybuffer entries
wim 1:f63d87466f55 993 //Add DP to bitpattern of digit left of current column.
wim 1:f63d87466f55 994 // addr = col2addr [_column - 1];
wim 1:f63d87466f55 995 addr = _column - 1;
wim 1:f63d87466f55 996
wim 1:f63d87466f55 997 //Save icons...and set bits for decimal point to write
wim 1:f63d87466f55 998 _displaybuffer[addr] = _displaybuffer[addr] | pattern;
wim 1:f63d87466f55 999 // writeData(_displaybuffer, (EYEWINK_NR_GRIDS * TM1637_BYTES_PER_GRID));
wim 1:f63d87466f55 1000 writeData(_displaybuffer, TM1637_BYTES_PER_GRID, addr);
wim 1:f63d87466f55 1001
wim 1:f63d87466f55 1002 //No Cursor Update
wim 1:f63d87466f55 1003 }
wim 1:f63d87466f55 1004 }
wim 1:f63d87466f55 1005 else if ((value >= 0) && (value < EYEWINK_NR_UDC)) {
wim 1:f63d87466f55 1006 //Character to write
wim 1:f63d87466f55 1007 validChar = true;
wim 1:f63d87466f55 1008 pattern = _UDC_7S[value];
wim 1:f63d87466f55 1009 }
wim 1:f63d87466f55 1010
wim 1:f63d87466f55 1011 #if (SHOW_ASCII == 1)
wim 1:f63d87466f55 1012 //display all ASCII characters
wim 1:f63d87466f55 1013 else if ((value >= FONT_7S_START) && (value <= FONT_7S_END)) {
wim 1:f63d87466f55 1014 //Character to write
wim 1:f63d87466f55 1015 validChar = true;
wim 1:f63d87466f55 1016 pattern = FONT_7S[value - FONT_7S_START];
wim 1:f63d87466f55 1017 } // else
wim 1:f63d87466f55 1018 #else
wim 1:f63d87466f55 1019 //display only digits and hex characters
wim 1:f63d87466f55 1020 else if (value == '-') {
wim 1:f63d87466f55 1021 //Character to write
wim 1:f63d87466f55 1022 validChar = true;
wim 1:f63d87466f55 1023 pattern = C7_MIN;
wim 1:f63d87466f55 1024 }
wim 1:f63d87466f55 1025 else if ((value >= (int)'0') && (value <= (int) '9')) {
wim 1:f63d87466f55 1026 //Character to write
wim 1:f63d87466f55 1027 validChar = true;
wim 1:f63d87466f55 1028 pattern = FONT_7S[value - (int) '0'];
wim 1:f63d87466f55 1029 }
wim 1:f63d87466f55 1030 else if ((value >= (int) 'A') && (value <= (int) 'F')) {
wim 1:f63d87466f55 1031 //Character to write
wim 1:f63d87466f55 1032 validChar = true;
wim 1:f63d87466f55 1033 pattern = FONT_7S[10 + value - (int) 'A'];
wim 1:f63d87466f55 1034 }
wim 1:f63d87466f55 1035 else if ((value >= (int) 'a') && (value <= (int) 'f')) {
wim 1:f63d87466f55 1036 //Character to write
wim 1:f63d87466f55 1037 validChar = true;
wim 1:f63d87466f55 1038 pattern = FONT_7S[10 + value - (int) 'a'];
wim 1:f63d87466f55 1039 } //else
wim 1:f63d87466f55 1040 #endif
wim 1:f63d87466f55 1041
wim 1:f63d87466f55 1042 if (validChar) {
wim 1:f63d87466f55 1043 //Character to write
wim 1:f63d87466f55 1044
wim 1:f63d87466f55 1045 //Translate between _column and displaybuffer entries
wim 1:f63d87466f55 1046 // addr = col2addr[_column];
wim 1:f63d87466f55 1047 addr = _column;
wim 1:f63d87466f55 1048
wim 1:f63d87466f55 1049 //Save icons...and set bits for character to write
wim 1:f63d87466f55 1050 _displaybuffer[addr] = (_displaybuffer[addr] & MASK_ICON_GRID[_column]) | pattern;
wim 1:f63d87466f55 1051
wim 1:f63d87466f55 1052 // writeData(_displaybuffer, (EYEWINK_NR_GRIDS * TM1637_BYTES_PER_GRID));
wim 1:f63d87466f55 1053 writeData(_displaybuffer, TM1637_BYTES_PER_GRID, addr);
wim 1:f63d87466f55 1054
wim 1:f63d87466f55 1055 //Update Cursor
wim 1:f63d87466f55 1056 _column++;
wim 1:f63d87466f55 1057 if (_column > (EYEWINK_NR_DIGITS - 1)) {
wim 1:f63d87466f55 1058 _column = 0;
wim 1:f63d87466f55 1059 }
wim 1:f63d87466f55 1060
wim 1:f63d87466f55 1061 } // if validChar
wim 1:f63d87466f55 1062
wim 1:f63d87466f55 1063 return value;
wim 1:f63d87466f55 1064 }
wim 1:f63d87466f55 1065
wim 1:f63d87466f55 1066
wim 1:f63d87466f55 1067 // get a single character (Stream implementation)
wim 1:f63d87466f55 1068 int TM1637_EYEWINK::_getc() {
wim 1:f63d87466f55 1069 return -1;
wim 1:f63d87466f55 1070 }
wim 1:f63d87466f55 1071
wim 1:f63d87466f55 1072 #endif