LED Driver, 6 digits @ 8 segm, 8 LEDs, 16 Keys. SPI Interface
See here for more information.
STLED316S.cpp@1:7a845a89625f, 2016-10-01 (annotated)
- Committer:
- wim
- Date:
- Sat Oct 01 15:58:18 2016 +0000
- Revision:
- 1:7a845a89625f
- Parent:
- 0:2c5311a4f6fa
Fixed example documentation
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wim | 0:2c5311a4f6fa | 1 | /* mbed STLED316S Library, for STLED316S LED controller |
wim | 0:2c5311a4f6fa | 2 | * Copyright (c) 2016, v01: WH, Initial version |
wim | 0:2c5311a4f6fa | 3 | * |
wim | 0:2c5311a4f6fa | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
wim | 0:2c5311a4f6fa | 5 | * of this software and associated documentation files (the "Software"), to deal |
wim | 0:2c5311a4f6fa | 6 | * in the Software without restriction, including without limitation the rights |
wim | 0:2c5311a4f6fa | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
wim | 0:2c5311a4f6fa | 8 | * copies of the Software, and to permit persons to whom the Software is |
wim | 0:2c5311a4f6fa | 9 | * furnished to do so, subject to the following conditions: |
wim | 0:2c5311a4f6fa | 10 | * |
wim | 0:2c5311a4f6fa | 11 | * The above copyright notice and this permission notice shall be included in |
wim | 0:2c5311a4f6fa | 12 | * all copies or substantial portions of the Software. |
wim | 0:2c5311a4f6fa | 13 | * |
wim | 0:2c5311a4f6fa | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
wim | 0:2c5311a4f6fa | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
wim | 0:2c5311a4f6fa | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
wim | 0:2c5311a4f6fa | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
wim | 0:2c5311a4f6fa | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
wim | 0:2c5311a4f6fa | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
wim | 0:2c5311a4f6fa | 20 | * THE SOFTWARE. |
wim | 0:2c5311a4f6fa | 21 | */ |
wim | 0:2c5311a4f6fa | 22 | #include "mbed.h" |
wim | 0:2c5311a4f6fa | 23 | #include "STLED316S.h" |
wim | 0:2c5311a4f6fa | 24 | #include "Font_7Seg.h" |
wim | 0:2c5311a4f6fa | 25 | |
wim | 0:2c5311a4f6fa | 26 | /** Constructor for class for driving STLED316S LED controller with SPI bus interface device. |
wim | 0:2c5311a4f6fa | 27 | * @brief Supports 1..6 digits @ 8 segments and 8 LEDs. |
wim | 0:2c5311a4f6fa | 28 | * Also supports a scanned keyboard of upto 16 keys. |
wim | 0:2c5311a4f6fa | 29 | * |
wim | 0:2c5311a4f6fa | 30 | * @param PinName mosi, miso, sclk, cs SPI bus pins |
wim | 0:2c5311a4f6fa | 31 | * @param Mode selects 1..6 Digits of 8 Segments (default 6 Digits of 8 Segments) |
wim | 0:2c5311a4f6fa | 32 | */ |
wim | 0:2c5311a4f6fa | 33 | STLED316S::STLED316S(PinName mosi, PinName miso, PinName sclk, PinName cs, Mode mode) : _spi(mosi,miso,sclk), _cs(cs), _mode(mode) { |
wim | 0:2c5311a4f6fa | 34 | |
wim | 0:2c5311a4f6fa | 35 | _init(); |
wim | 0:2c5311a4f6fa | 36 | } |
wim | 0:2c5311a4f6fa | 37 | |
wim | 0:2c5311a4f6fa | 38 | /** Init the SPI interface and the controller |
wim | 0:2c5311a4f6fa | 39 | * @param none |
wim | 0:2c5311a4f6fa | 40 | * @return none |
wim | 0:2c5311a4f6fa | 41 | */ |
wim | 0:2c5311a4f6fa | 42 | void STLED316S::_init(){ |
wim | 0:2c5311a4f6fa | 43 | char config; |
wim | 0:2c5311a4f6fa | 44 | |
wim | 0:2c5311a4f6fa | 45 | //init SPI |
wim | 0:2c5311a4f6fa | 46 | _cs=1; |
wim | 0:2c5311a4f6fa | 47 | _spi.format(8,3); //STLED316S uses mode 3 (Clock High on Idle, Data latched on second (=rising) edge) |
wim | 0:2c5311a4f6fa | 48 | _spi.frequency(500000); |
wim | 0:2c5311a4f6fa | 49 | |
wim | 0:2c5311a4f6fa | 50 | //init controller |
wim | 0:2c5311a4f6fa | 51 | _brt_mode = GlobalBright; //STLED316S_BRT_GLOB; |
wim | 0:2c5311a4f6fa | 52 | // _brt_mode = IndivBright; //STLED316S_BRT_INDIV; |
wim | 0:2c5311a4f6fa | 53 | _bright = STLED316S_BRT_DEF; |
wim | 0:2c5311a4f6fa | 54 | |
wim | 0:2c5311a4f6fa | 55 | config = ((_mode << STLED316S_CONF_GRID_SHFT) & STLED316S_CONF_GRID_MSK) | |
wim | 0:2c5311a4f6fa | 56 | ((_brt_mode << STLED316S_CONF_BRT_MODE_SHFT) & STLED316S_CONF_BRT_MODE_MSK) | |
wim | 0:2c5311a4f6fa | 57 | ((_bright << STLED316S_CONF_BRT_GLOB_SHFT) & STLED316S_CONF_BRT_GLOB_MSK); |
wim | 0:2c5311a4f6fa | 58 | |
wim | 0:2c5311a4f6fa | 59 | _writeReg(STLED316S_IDX(STLED316S_CONF_PAGE,STLED316S_CONF_ADDR), config); // Config set command |
wim | 0:2c5311a4f6fa | 60 | // printf("Cmd= 0x%02X, Conf= 0x%02X\r\n", STLED316S_ADDR_WR_CMD | STLED316S_IDX(STLED316S_CONF_PAGE,STLED316S_CONF_ADDR), config); // Debug |
wim | 0:2c5311a4f6fa | 61 | |
wim | 0:2c5311a4f6fa | 62 | _writeReg(STLED316S_IDX(STLED316S_DSP_PAGE,STLED316S_DSP_ON_ADDR)); // Display On command |
wim | 0:2c5311a4f6fa | 63 | } |
wim | 0:2c5311a4f6fa | 64 | |
wim | 0:2c5311a4f6fa | 65 | |
wim | 0:2c5311a4f6fa | 66 | /** Clear the screen and locate to 0 |
wim | 0:2c5311a4f6fa | 67 | */ |
wim | 0:2c5311a4f6fa | 68 | void STLED316S::cls() { |
wim | 0:2c5311a4f6fa | 69 | |
wim | 0:2c5311a4f6fa | 70 | _cs=0; |
wim | 0:2c5311a4f6fa | 71 | wait_us(1); |
wim | 0:2c5311a4f6fa | 72 | _spi.write(_flip(STLED316S_ADDR_WR_CMD | STLED316S_IDX(STLED316S_DIG_PAGE,STLED316S_DIG2_ADDR))); // Address set cmd, 0 |
wim | 0:2c5311a4f6fa | 73 | |
wim | 0:2c5311a4f6fa | 74 | for (int cnt=0; cnt<STLED316S_DISPLAY_MEM; cnt++) { |
wim | 0:2c5311a4f6fa | 75 | _spi.write(0x00); // data |
wim | 0:2c5311a4f6fa | 76 | } |
wim | 0:2c5311a4f6fa | 77 | |
wim | 0:2c5311a4f6fa | 78 | wait_us(1); |
wim | 0:2c5311a4f6fa | 79 | _cs=1; |
wim | 0:2c5311a4f6fa | 80 | } |
wim | 0:2c5311a4f6fa | 81 | |
wim | 0:2c5311a4f6fa | 82 | /** Set Brightness for all Digits and LEDs (value is used in GlobalBright mode) |
wim | 0:2c5311a4f6fa | 83 | * |
wim | 0:2c5311a4f6fa | 84 | * @param char brightness (3 significant bits, valid range 0..7 (1/16 .. 14/14 dutycycle) |
wim | 0:2c5311a4f6fa | 85 | * @return none |
wim | 0:2c5311a4f6fa | 86 | */ |
wim | 0:2c5311a4f6fa | 87 | void STLED316S::setBrightness(char brightness){ |
wim | 0:2c5311a4f6fa | 88 | char config; |
wim | 0:2c5311a4f6fa | 89 | |
wim | 0:2c5311a4f6fa | 90 | _bright = brightness & STLED316S_BRT_MSK; // mask invalid bits |
wim | 0:2c5311a4f6fa | 91 | |
wim | 0:2c5311a4f6fa | 92 | config = ((_mode << STLED316S_CONF_GRID_SHFT) & STLED316S_CONF_GRID_MSK) | |
wim | 0:2c5311a4f6fa | 93 | ((_brt_mode << STLED316S_CONF_BRT_MODE_SHFT) & STLED316S_CONF_BRT_MODE_MSK) | |
wim | 0:2c5311a4f6fa | 94 | ((_bright << STLED316S_CONF_BRT_GLOB_SHFT) & STLED316S_CONF_BRT_GLOB_MSK); |
wim | 0:2c5311a4f6fa | 95 | |
wim | 0:2c5311a4f6fa | 96 | _writeReg(STLED316S_IDX(STLED316S_CONF_PAGE,STLED316S_CONF_ADDR), config); // Config set command |
wim | 0:2c5311a4f6fa | 97 | // printf("Cmd= 0x%02X, Conf= 0x%02X\r\n", STLED316S_ADDR_WR_CMD | STLED316S_IDX(STLED316S_CONF_PAGE,STLED316S_CONF_ADDR), config); // Debug |
wim | 0:2c5311a4f6fa | 98 | } |
wim | 0:2c5311a4f6fa | 99 | |
wim | 0:2c5311a4f6fa | 100 | |
wim | 0:2c5311a4f6fa | 101 | /** Set Individual LED Brightness (value is used in IndivBright mode) |
wim | 0:2c5311a4f6fa | 102 | * |
wim | 0:2c5311a4f6fa | 103 | * @param LedData_t leds pattern of LED data |
wim | 0:2c5311a4f6fa | 104 | * @param char led_brt (3 significant bits, valid range 0..7 (1/16 .. 14/14 dutycycle) |
wim | 0:2c5311a4f6fa | 105 | * @return none |
wim | 0:2c5311a4f6fa | 106 | */ |
wim | 0:2c5311a4f6fa | 107 | void STLED316S::setLedBrightness(LedData_t leds, char led_brt) { |
wim | 0:2c5311a4f6fa | 108 | char brt_old; |
wim | 0:2c5311a4f6fa | 109 | |
wim | 0:2c5311a4f6fa | 110 | //Sanity check |
wim | 0:2c5311a4f6fa | 111 | led_brt &= STLED316S_BRT_MSK; |
wim | 0:2c5311a4f6fa | 112 | |
wim | 0:2c5311a4f6fa | 113 | if (leds & STLED316S_LED_L1) { // LED_1 |
wim | 0:2c5311a4f6fa | 114 | brt_old = _readReg(STLED316S_IDX(STLED316S_LED_BRT_PAGE,STLED316S_LED1_2_BRT_ADDR)); // LED Brt read command |
wim | 0:2c5311a4f6fa | 115 | _writeReg(STLED316S_IDX(STLED316S_LED_BRT_PAGE,STLED316S_LED1_2_BRT_ADDR), (brt_old & 0xF0) | led_brt); // LED Brt set command |
wim | 0:2c5311a4f6fa | 116 | } |
wim | 0:2c5311a4f6fa | 117 | |
wim | 0:2c5311a4f6fa | 118 | if (leds & STLED316S_LED_L2) { // LED_2 |
wim | 0:2c5311a4f6fa | 119 | brt_old = _readReg(STLED316S_IDX(STLED316S_LED_BRT_PAGE,STLED316S_LED1_2_BRT_ADDR)); // LED Brt read command |
wim | 0:2c5311a4f6fa | 120 | _writeReg(STLED316S_IDX(STLED316S_LED_BRT_PAGE,STLED316S_LED1_2_BRT_ADDR), (brt_old & 0x0F) | (led_brt << 4)); // LED Brt set command |
wim | 0:2c5311a4f6fa | 121 | } |
wim | 0:2c5311a4f6fa | 122 | |
wim | 0:2c5311a4f6fa | 123 | if (leds & STLED316S_LED_L3) { // LED_3 |
wim | 0:2c5311a4f6fa | 124 | brt_old = _readReg(STLED316S_IDX(STLED316S_LED_BRT_PAGE,STLED316S_LED3_4_BRT_ADDR)); // LED Brt read command |
wim | 0:2c5311a4f6fa | 125 | _writeReg(STLED316S_IDX(STLED316S_LED_BRT_PAGE,STLED316S_LED3_4_BRT_ADDR), (brt_old & 0xF0) | led_brt); // LED Brt set command |
wim | 0:2c5311a4f6fa | 126 | } |
wim | 0:2c5311a4f6fa | 127 | |
wim | 0:2c5311a4f6fa | 128 | if (leds & STLED316S_LED_L4) { // LED_4 |
wim | 0:2c5311a4f6fa | 129 | brt_old = _readReg(STLED316S_IDX(STLED316S_LED_BRT_PAGE,STLED316S_LED3_4_BRT_ADDR)); // LED Brt read command |
wim | 0:2c5311a4f6fa | 130 | _writeReg(STLED316S_IDX(STLED316S_LED_BRT_PAGE,STLED316S_LED3_4_BRT_ADDR), (brt_old & 0x0F) | (led_brt << 4)); // LED Brt set command |
wim | 0:2c5311a4f6fa | 131 | } |
wim | 0:2c5311a4f6fa | 132 | |
wim | 0:2c5311a4f6fa | 133 | if (leds & STLED316S_LED_L5) { // LED_5 |
wim | 0:2c5311a4f6fa | 134 | brt_old = _readReg(STLED316S_IDX(STLED316S_LED_BRT_PAGE,STLED316S_LED5_6_BRT_ADDR)); // LED Brt read command |
wim | 0:2c5311a4f6fa | 135 | _writeReg(STLED316S_IDX(STLED316S_LED_BRT_PAGE,STLED316S_LED5_6_BRT_ADDR), (brt_old & 0xF0) | led_brt); // LED Brt set command |
wim | 0:2c5311a4f6fa | 136 | } |
wim | 0:2c5311a4f6fa | 137 | |
wim | 0:2c5311a4f6fa | 138 | if (leds & STLED316S_LED_L6) { // LED_6 |
wim | 0:2c5311a4f6fa | 139 | brt_old = _readReg(STLED316S_IDX(STLED316S_LED_BRT_PAGE,STLED316S_LED5_6_BRT_ADDR)); // LED Brt read command |
wim | 0:2c5311a4f6fa | 140 | _writeReg(STLED316S_IDX(STLED316S_LED_BRT_PAGE,STLED316S_LED5_6_BRT_ADDR), (brt_old & 0x0F) | (led_brt << 4)); // LED Brt set command |
wim | 0:2c5311a4f6fa | 141 | } |
wim | 0:2c5311a4f6fa | 142 | |
wim | 0:2c5311a4f6fa | 143 | if (leds & STLED316S_LED_L7) { // LED_7 |
wim | 0:2c5311a4f6fa | 144 | brt_old = _readReg(STLED316S_IDX(STLED316S_LED_BRT_PAGE,STLED316S_LED7_8_BRT_ADDR)); // LED Brt read command |
wim | 0:2c5311a4f6fa | 145 | _writeReg(STLED316S_IDX(STLED316S_LED_BRT_PAGE,STLED316S_LED7_8_BRT_ADDR), (brt_old & 0xF0) | led_brt); // LED Brt set command |
wim | 0:2c5311a4f6fa | 146 | } |
wim | 0:2c5311a4f6fa | 147 | |
wim | 0:2c5311a4f6fa | 148 | if (leds & STLED316S_LED_L8) { // LED_8 |
wim | 0:2c5311a4f6fa | 149 | brt_old = _readReg(STLED316S_IDX(STLED316S_LED_BRT_PAGE,STLED316S_LED7_8_BRT_ADDR)); // LED Brt read command |
wim | 0:2c5311a4f6fa | 150 | _writeReg(STLED316S_IDX(STLED316S_LED_BRT_PAGE,STLED316S_LED7_8_BRT_ADDR), (brt_old & 0x0F) | (led_brt << 4)); // LED Brt set command |
wim | 0:2c5311a4f6fa | 151 | } |
wim | 0:2c5311a4f6fa | 152 | } |
wim | 0:2c5311a4f6fa | 153 | |
wim | 0:2c5311a4f6fa | 154 | |
wim | 0:2c5311a4f6fa | 155 | /** Set Individual Digit Brightness (value is used in IndivBright mode) |
wim | 0:2c5311a4f6fa | 156 | * |
wim | 0:2c5311a4f6fa | 157 | * @param LedData_t digits pattern of Digit data |
wim | 0:2c5311a4f6fa | 158 | * @param char dig_brt (3 significant bits, valid range 0..7 (1/16 .. 14/14 dutycycle) |
wim | 0:2c5311a4f6fa | 159 | * @return none |
wim | 0:2c5311a4f6fa | 160 | */ |
wim | 0:2c5311a4f6fa | 161 | void STLED316S::setDigitBrightness(LedData_t digits, char dig_brt) { |
wim | 0:2c5311a4f6fa | 162 | char brt_old; |
wim | 0:2c5311a4f6fa | 163 | |
wim | 0:2c5311a4f6fa | 164 | //Sanity check |
wim | 0:2c5311a4f6fa | 165 | dig_brt &= STLED316S_BRT_MSK; |
wim | 0:2c5311a4f6fa | 166 | |
wim | 0:2c5311a4f6fa | 167 | if (digits & STLED316S_DIG_D2) { // DIGIT_2 |
wim | 0:2c5311a4f6fa | 168 | brt_old = _readReg(STLED316S_IDX(STLED316S_DIG_BRT_PAGE,STLED316S_DIG2_3_BRT_ADDR)); // Digit Brt read command |
wim | 0:2c5311a4f6fa | 169 | _writeReg(STLED316S_IDX(STLED316S_DIG_BRT_PAGE,STLED316S_DIG2_3_BRT_ADDR), (brt_old & 0xF0) | dig_brt); // Digit Brt set command |
wim | 0:2c5311a4f6fa | 170 | } |
wim | 0:2c5311a4f6fa | 171 | |
wim | 0:2c5311a4f6fa | 172 | if (digits & STLED316S_DIG_D3) { // DIGIT_3 |
wim | 0:2c5311a4f6fa | 173 | brt_old = _readReg(STLED316S_IDX(STLED316S_DIG_BRT_PAGE,STLED316S_DIG2_3_BRT_ADDR)); // Digit Brt read command |
wim | 0:2c5311a4f6fa | 174 | _writeReg(STLED316S_IDX(STLED316S_DIG_BRT_PAGE,STLED316S_DIG2_3_BRT_ADDR), (brt_old & 0x0F) | (dig_brt << 4)); // Digit Brt set command |
wim | 0:2c5311a4f6fa | 175 | } |
wim | 0:2c5311a4f6fa | 176 | |
wim | 0:2c5311a4f6fa | 177 | if (digits & STLED316S_DIG_D4) { // DIGIT_4 |
wim | 0:2c5311a4f6fa | 178 | brt_old = _readReg(STLED316S_IDX(STLED316S_DIG_BRT_PAGE,STLED316S_DIG4_5_BRT_ADDR)); // Digit Brt read command |
wim | 0:2c5311a4f6fa | 179 | _writeReg(STLED316S_IDX(STLED316S_DIG_BRT_PAGE,STLED316S_DIG4_5_BRT_ADDR), (brt_old & 0xF0) | dig_brt); // Digit Brt set command |
wim | 0:2c5311a4f6fa | 180 | } |
wim | 0:2c5311a4f6fa | 181 | |
wim | 0:2c5311a4f6fa | 182 | if (digits & STLED316S_DIG_D5) { // DIGIT_5 |
wim | 0:2c5311a4f6fa | 183 | brt_old = _readReg(STLED316S_IDX(STLED316S_DIG_BRT_PAGE,STLED316S_DIG4_5_BRT_ADDR)); // Digit Brt read command |
wim | 0:2c5311a4f6fa | 184 | _writeReg(STLED316S_IDX(STLED316S_DIG_BRT_PAGE,STLED316S_DIG4_5_BRT_ADDR), (brt_old & 0x0F) | (dig_brt << 4)); // Digit Brt set command |
wim | 0:2c5311a4f6fa | 185 | } |
wim | 0:2c5311a4f6fa | 186 | |
wim | 0:2c5311a4f6fa | 187 | if (digits & STLED316S_DIG_D6) { // DIGIT_6 |
wim | 0:2c5311a4f6fa | 188 | brt_old = _readReg(STLED316S_IDX(STLED316S_DIG_BRT_PAGE,STLED316S_DIG6_7_BRT_ADDR)); // Digit Brt read command |
wim | 0:2c5311a4f6fa | 189 | _writeReg(STLED316S_IDX(STLED316S_DIG_BRT_PAGE,STLED316S_DIG6_7_BRT_ADDR), (brt_old & 0xF0) | dig_brt); // Digit Brt set command |
wim | 0:2c5311a4f6fa | 190 | } |
wim | 0:2c5311a4f6fa | 191 | |
wim | 0:2c5311a4f6fa | 192 | if (digits & STLED316S_DIG_D7) { // DIGIT_7 |
wim | 0:2c5311a4f6fa | 193 | brt_old = _readReg(STLED316S_IDX(STLED316S_DIG_BRT_PAGE,STLED316S_DIG6_7_BRT_ADDR)); // Digit Brt read command |
wim | 0:2c5311a4f6fa | 194 | _writeReg(STLED316S_IDX(STLED316S_DIG_BRT_PAGE,STLED316S_DIG6_7_BRT_ADDR), (brt_old & 0x0F) | (dig_brt << 4)); // Digit Brt set command |
wim | 0:2c5311a4f6fa | 195 | } |
wim | 0:2c5311a4f6fa | 196 | } |
wim | 0:2c5311a4f6fa | 197 | |
wim | 0:2c5311a4f6fa | 198 | |
wim | 0:2c5311a4f6fa | 199 | /** Set the Display mode On/off |
wim | 0:2c5311a4f6fa | 200 | * |
wim | 0:2c5311a4f6fa | 201 | * @param bool display mode |
wim | 0:2c5311a4f6fa | 202 | */ |
wim | 0:2c5311a4f6fa | 203 | void STLED316S::setDisplay(bool on) { |
wim | 0:2c5311a4f6fa | 204 | |
wim | 0:2c5311a4f6fa | 205 | if (on) { |
wim | 0:2c5311a4f6fa | 206 | _writeReg(STLED316S_IDX(STLED316S_DSP_PAGE,STLED316S_DSP_ON_ADDR)); // Display On command |
wim | 0:2c5311a4f6fa | 207 | } |
wim | 0:2c5311a4f6fa | 208 | else { |
wim | 0:2c5311a4f6fa | 209 | _writeReg(STLED316S_IDX(STLED316S_DSP_PAGE,STLED316S_DSP_OFF_ADDR)); // Display Off command |
wim | 0:2c5311a4f6fa | 210 | } |
wim | 0:2c5311a4f6fa | 211 | } |
wim | 0:2c5311a4f6fa | 212 | |
wim | 0:2c5311a4f6fa | 213 | /** Set Brightness mode |
wim | 0:2c5311a4f6fa | 214 | * |
wim | 0:2c5311a4f6fa | 215 | * @param BrightMode brt_mode (value is IndivBright or GlobalBright) |
wim | 0:2c5311a4f6fa | 216 | * @return none |
wim | 0:2c5311a4f6fa | 217 | */ |
wim | 0:2c5311a4f6fa | 218 | void STLED316S::setBrightMode(BrightMode brt_mode) { |
wim | 0:2c5311a4f6fa | 219 | char config; |
wim | 0:2c5311a4f6fa | 220 | |
wim | 0:2c5311a4f6fa | 221 | _brt_mode = brt_mode; // mask invalid bits |
wim | 0:2c5311a4f6fa | 222 | |
wim | 0:2c5311a4f6fa | 223 | config = ((_mode << STLED316S_CONF_GRID_SHFT) & STLED316S_CONF_GRID_MSK) | |
wim | 0:2c5311a4f6fa | 224 | ((_brt_mode << STLED316S_CONF_BRT_MODE_SHFT) & STLED316S_CONF_BRT_MODE_MSK) | |
wim | 0:2c5311a4f6fa | 225 | ((_bright << STLED316S_CONF_BRT_GLOB_SHFT) & STLED316S_CONF_BRT_GLOB_MSK); |
wim | 0:2c5311a4f6fa | 226 | |
wim | 0:2c5311a4f6fa | 227 | _writeReg(STLED316S_IDX(STLED316S_CONF_PAGE,STLED316S_CONF_ADDR), config); // Config set command |
wim | 0:2c5311a4f6fa | 228 | // printf("Cmd= 0x%02X, Conf= 0x%02X\r\n", STLED316S_ADDR_WR_CMD | STLED316S_IDX(STLED316S_CONF_PAGE,STLED316S_CONF_ADDR), config); // Debug |
wim | 0:2c5311a4f6fa | 229 | } |
wim | 0:2c5311a4f6fa | 230 | |
wim | 0:2c5311a4f6fa | 231 | |
wim | 0:2c5311a4f6fa | 232 | /** Write databyte to STLED316S |
wim | 0:2c5311a4f6fa | 233 | * @param int address display memory location to write byte |
wim | 0:2c5311a4f6fa | 234 | * @param char data byte written at given address |
wim | 0:2c5311a4f6fa | 235 | * @return none |
wim | 0:2c5311a4f6fa | 236 | */ |
wim | 0:2c5311a4f6fa | 237 | void STLED316S::writeData(int address, char data) { |
wim | 0:2c5311a4f6fa | 238 | _cs=0; |
wim | 0:2c5311a4f6fa | 239 | wait_us(1); |
wim | 0:2c5311a4f6fa | 240 | |
wim | 0:2c5311a4f6fa | 241 | _spi.write(_flip(STLED316S_ADDR_WR_CMD | STLED316S_IDX(STLED316S_DIG_PAGE, address))); // Address set cmd |
wim | 0:2c5311a4f6fa | 242 | _spi.write(_flip(data)); // data |
wim | 0:2c5311a4f6fa | 243 | |
wim | 0:2c5311a4f6fa | 244 | wait_us(1); |
wim | 0:2c5311a4f6fa | 245 | _cs=1; |
wim | 0:2c5311a4f6fa | 246 | } |
wim | 0:2c5311a4f6fa | 247 | |
wim | 0:2c5311a4f6fa | 248 | /** Write Display datablock to STLED316S |
wim | 0:2c5311a4f6fa | 249 | * @param DisplayData_t data Array of STLED316S_DISPLAY_MEM (=6) bytes for displaydata (starting at address 0) |
wim | 0:2c5311a4f6fa | 250 | * @param length number bytes to write (valide range 0..STLED316S_DISPLAY_MEM (=6), starting at address 0) |
wim | 0:2c5311a4f6fa | 251 | * @return none |
wim | 0:2c5311a4f6fa | 252 | */ |
wim | 0:2c5311a4f6fa | 253 | void STLED316S::writeData(DisplayData_t data, int length) { |
wim | 0:2c5311a4f6fa | 254 | _cs=0; |
wim | 0:2c5311a4f6fa | 255 | wait_us(1); |
wim | 0:2c5311a4f6fa | 256 | |
wim | 0:2c5311a4f6fa | 257 | _spi.write(_flip(STLED316S_ADDR_WR_CMD | STLED316S_IDX(STLED316S_DIG_PAGE, 0x00))); // Set Address at 0 |
wim | 0:2c5311a4f6fa | 258 | |
wim | 0:2c5311a4f6fa | 259 | // printf("Cmd= 0x%02X\r\n", STLED316S_ADDR_WR_CMD | STLED316S_IDX(STLED316S_DIG_PAGE, 0x00)); // Debug |
wim | 0:2c5311a4f6fa | 260 | |
wim | 0:2c5311a4f6fa | 261 | // sanity check |
wim | 0:2c5311a4f6fa | 262 | if (length < 0) {length = 0;} |
wim | 0:2c5311a4f6fa | 263 | if (length > STLED316S_DISPLAY_MEM) {length = STLED316S_DISPLAY_MEM;} |
wim | 0:2c5311a4f6fa | 264 | |
wim | 0:2c5311a4f6fa | 265 | // for (int idx=0; idx<STLED316S_DISPLAY_MEM; idx++) { |
wim | 0:2c5311a4f6fa | 266 | for (int idx=0; idx<length; idx++) { |
wim | 0:2c5311a4f6fa | 267 | _spi.write(_flip(data[idx])); // data |
wim | 0:2c5311a4f6fa | 268 | |
wim | 0:2c5311a4f6fa | 269 | // printf("Data= 0x%02X\r\n", data[idx]); // Debug |
wim | 0:2c5311a4f6fa | 270 | } |
wim | 0:2c5311a4f6fa | 271 | |
wim | 0:2c5311a4f6fa | 272 | wait_us(1); |
wim | 0:2c5311a4f6fa | 273 | _cs=1; |
wim | 0:2c5311a4f6fa | 274 | } |
wim | 0:2c5311a4f6fa | 275 | |
wim | 0:2c5311a4f6fa | 276 | |
wim | 0:2c5311a4f6fa | 277 | /** Write LED data to STLED316S |
wim | 0:2c5311a4f6fa | 278 | * @param LedData_t leds LED data |
wim | 0:2c5311a4f6fa | 279 | * @return none |
wim | 0:2c5311a4f6fa | 280 | */ |
wim | 0:2c5311a4f6fa | 281 | void STLED316S::writeLedData(LedData_t leds) { |
wim | 0:2c5311a4f6fa | 282 | _cs=0; |
wim | 0:2c5311a4f6fa | 283 | wait_us(1); |
wim | 0:2c5311a4f6fa | 284 | |
wim | 0:2c5311a4f6fa | 285 | _leds = leds; |
wim | 0:2c5311a4f6fa | 286 | |
wim | 0:2c5311a4f6fa | 287 | _spi.write(_flip(STLED316S_ADDR_WR_CMD | STLED316S_IDX(STLED316S_DIG1_LED_PAGE, STLED316S_DIG1_LED_ADDR))); // Set Address |
wim | 0:2c5311a4f6fa | 288 | // printf("Cmd= 0x%02Xr\n", STLED316S_ADDR_WR_CMD | STLED316S_IDX(STLED316S_DIG1_LED_PAGE, STLED316S_DIG1_LED_ADDR)); // Debug |
wim | 0:2c5311a4f6fa | 289 | |
wim | 0:2c5311a4f6fa | 290 | _spi.write(_flip(leds)); // data |
wim | 0:2c5311a4f6fa | 291 | // printf("Data 0x%02X\r\n", leds); // Debug |
wim | 0:2c5311a4f6fa | 292 | |
wim | 0:2c5311a4f6fa | 293 | wait_us(1); |
wim | 0:2c5311a4f6fa | 294 | _cs=1; |
wim | 0:2c5311a4f6fa | 295 | } |
wim | 0:2c5311a4f6fa | 296 | |
wim | 0:2c5311a4f6fa | 297 | |
wim | 0:2c5311a4f6fa | 298 | /** Set LED |
wim | 0:2c5311a4f6fa | 299 | * |
wim | 0:2c5311a4f6fa | 300 | * @param LedData_t leds pattern of LED data |
wim | 0:2c5311a4f6fa | 301 | * @return none |
wim | 0:2c5311a4f6fa | 302 | */ |
wim | 0:2c5311a4f6fa | 303 | void STLED316S::setLed(LedData_t leds){ |
wim | 0:2c5311a4f6fa | 304 | |
wim | 0:2c5311a4f6fa | 305 | _leds |= leds; |
wim | 0:2c5311a4f6fa | 306 | writeLedData(_leds); |
wim | 0:2c5311a4f6fa | 307 | } |
wim | 0:2c5311a4f6fa | 308 | |
wim | 0:2c5311a4f6fa | 309 | /** Clr LED |
wim | 0:2c5311a4f6fa | 310 | * |
wim | 0:2c5311a4f6fa | 311 | * @param LedData_t leds pattern of LED data |
wim | 0:2c5311a4f6fa | 312 | * @return none |
wim | 0:2c5311a4f6fa | 313 | */ |
wim | 0:2c5311a4f6fa | 314 | void STLED316S::clrLed(LedData_t leds){ |
wim | 0:2c5311a4f6fa | 315 | _leds &= ~leds; |
wim | 0:2c5311a4f6fa | 316 | writeLedData(_leds); |
wim | 0:2c5311a4f6fa | 317 | } |
wim | 0:2c5311a4f6fa | 318 | |
wim | 0:2c5311a4f6fa | 319 | /** Read keydata block from STLED316S |
wim | 0:2c5311a4f6fa | 320 | * @param *keydata Ptr to Array of STLED316S_KEY_MEM (=5) bytes for keydata |
wim | 0:2c5311a4f6fa | 321 | * @return bool keypress True when at least one key was pressed |
wim | 0:2c5311a4f6fa | 322 | * |
wim | 0:2c5311a4f6fa | 323 | * Note: Due to the hardware configuration the STLED316S key matrix scanner will detect multiple keys pressed at same time, |
wim | 0:2c5311a4f6fa | 324 | * but this may also result in some spurious keys being set in keypress data array. |
wim | 0:2c5311a4f6fa | 325 | * It may be best to ignore all keys in those situations. That option is implemented in this method depending on #define setting. |
wim | 0:2c5311a4f6fa | 326 | */ |
wim | 0:2c5311a4f6fa | 327 | bool STLED316S::getKeys(KeyData_t *keydata) { |
wim | 0:2c5311a4f6fa | 328 | int keypress = 0; |
wim | 0:2c5311a4f6fa | 329 | char data; |
wim | 0:2c5311a4f6fa | 330 | |
wim | 0:2c5311a4f6fa | 331 | // Read keys |
wim | 0:2c5311a4f6fa | 332 | _cs=0; |
wim | 0:2c5311a4f6fa | 333 | wait_us(1); |
wim | 0:2c5311a4f6fa | 334 | |
wim | 0:2c5311a4f6fa | 335 | // Enable Key Read mode |
wim | 0:2c5311a4f6fa | 336 | _spi.write(_flip(STLED316S_ADDR_RD_CMD | STLED316S_IDX(STLED316S_KEY_PAGE, STLED316S_KEY1_ADDR))); // Set Address |
wim | 0:2c5311a4f6fa | 337 | // printf("Cmd= 0x%02Xr\n", STLED316S_ADDR_RD_CMD | STLED316S_IDX(STLED316S_KEY_PAGE, STLED316S_KEY1_ADDR)); // Debug |
wim | 0:2c5311a4f6fa | 338 | |
wim | 0:2c5311a4f6fa | 339 | for (int idx=0; idx < STLED316S_KEY_MEM; idx++) { |
wim | 0:2c5311a4f6fa | 340 | data = _flip(_spi.write(0xFF)); // read keys and correct bitorder |
wim | 0:2c5311a4f6fa | 341 | // printf("KeyData 0x%02X\r\n", data); // Debug |
wim | 0:2c5311a4f6fa | 342 | |
wim | 0:2c5311a4f6fa | 343 | data = data & STLED316S_KEY_MSK; // Mask valid bits |
wim | 0:2c5311a4f6fa | 344 | if (data != 0) { // Check for any pressed key |
wim | 0:2c5311a4f6fa | 345 | for (int bit=0; bit < 8; bit++) { |
wim | 0:2c5311a4f6fa | 346 | if (data & (1 << bit)) {keypress++;} // Test all significant bits |
wim | 0:2c5311a4f6fa | 347 | } |
wim | 0:2c5311a4f6fa | 348 | } |
wim | 0:2c5311a4f6fa | 349 | |
wim | 0:2c5311a4f6fa | 350 | (*keydata)[idx] = data; // Store keydata after correcting bitorder |
wim | 0:2c5311a4f6fa | 351 | } |
wim | 0:2c5311a4f6fa | 352 | |
wim | 0:2c5311a4f6fa | 353 | wait_us(1); |
wim | 0:2c5311a4f6fa | 354 | _cs=1; |
wim | 0:2c5311a4f6fa | 355 | |
wim | 0:2c5311a4f6fa | 356 | #if(1) |
wim | 0:2c5311a4f6fa | 357 | // Dismiss multiple keypresses at same time |
wim | 0:2c5311a4f6fa | 358 | return (keypress == 1); |
wim | 0:2c5311a4f6fa | 359 | #else |
wim | 0:2c5311a4f6fa | 360 | // Allow multiple keypress and accept possible spurious keys |
wim | 0:2c5311a4f6fa | 361 | return (keypress > 0); |
wim | 0:2c5311a4f6fa | 362 | #endif |
wim | 0:2c5311a4f6fa | 363 | } |
wim | 0:2c5311a4f6fa | 364 | |
wim | 0:2c5311a4f6fa | 365 | |
wim | 0:2c5311a4f6fa | 366 | /** Helper to reverse all command or databits. The STLED316S expects LSB first, whereas SPI is MSB first |
wim | 0:2c5311a4f6fa | 367 | * @param char data |
wim | 0:2c5311a4f6fa | 368 | * @return bitreversed data |
wim | 0:2c5311a4f6fa | 369 | */ |
wim | 0:2c5311a4f6fa | 370 | char STLED316S::_flip(char data) { |
wim | 0:2c5311a4f6fa | 371 | char value=0; |
wim | 0:2c5311a4f6fa | 372 | |
wim | 0:2c5311a4f6fa | 373 | if (data & 0x01) {value |= 0x80;} ; |
wim | 0:2c5311a4f6fa | 374 | if (data & 0x02) {value |= 0x40;} ; |
wim | 0:2c5311a4f6fa | 375 | if (data & 0x04) {value |= 0x20;} ; |
wim | 0:2c5311a4f6fa | 376 | if (data & 0x08) {value |= 0x10;} ; |
wim | 0:2c5311a4f6fa | 377 | if (data & 0x10) {value |= 0x08;} ; |
wim | 0:2c5311a4f6fa | 378 | if (data & 0x20) {value |= 0x04;} ; |
wim | 0:2c5311a4f6fa | 379 | if (data & 0x40) {value |= 0x02;} ; |
wim | 0:2c5311a4f6fa | 380 | if (data & 0x80) {value |= 0x01;} ; |
wim | 0:2c5311a4f6fa | 381 | return value; |
wim | 0:2c5311a4f6fa | 382 | } |
wim | 0:2c5311a4f6fa | 383 | |
wim | 0:2c5311a4f6fa | 384 | |
wim | 0:2c5311a4f6fa | 385 | /** Write parameter to STLED316S Register |
wim | 0:2c5311a4f6fa | 386 | * @param int idx Register address |
wim | 0:2c5311a4f6fa | 387 | * @Param int data Parameter for Register |
wim | 0:2c5311a4f6fa | 388 | * @return none |
wim | 0:2c5311a4f6fa | 389 | */ |
wim | 0:2c5311a4f6fa | 390 | void STLED316S::_writeReg(int idx, int data){ |
wim | 0:2c5311a4f6fa | 391 | |
wim | 0:2c5311a4f6fa | 392 | _cs=0; |
wim | 0:2c5311a4f6fa | 393 | wait_us(1); |
wim | 0:2c5311a4f6fa | 394 | |
wim | 0:2c5311a4f6fa | 395 | _spi.write(_flip(STLED316S_ADDR_WR_CMD | (idx & STLED316S_IDX_MSK))); |
wim | 0:2c5311a4f6fa | 396 | _spi.write(_flip(data) ); |
wim | 0:2c5311a4f6fa | 397 | |
wim | 0:2c5311a4f6fa | 398 | // printf("Cmd= 0x%02X, Write= 0x%02X\r\n", STLED316S_ADDR_WR_CMD | (idx & STLED316S_IDX_MSK), data); // Debug |
wim | 0:2c5311a4f6fa | 399 | |
wim | 0:2c5311a4f6fa | 400 | wait_us(1); |
wim | 0:2c5311a4f6fa | 401 | _cs=1; |
wim | 0:2c5311a4f6fa | 402 | } |
wim | 0:2c5311a4f6fa | 403 | |
wim | 0:2c5311a4f6fa | 404 | |
wim | 0:2c5311a4f6fa | 405 | /** Write merged command and parameter to STLED316S |
wim | 0:2c5311a4f6fa | 406 | * @param int cmd Command & Parameter byte |
wim | 0:2c5311a4f6fa | 407 | * @return none |
wim | 0:2c5311a4f6fa | 408 | */ |
wim | 0:2c5311a4f6fa | 409 | void STLED316S::_writeReg(int cmd){ |
wim | 0:2c5311a4f6fa | 410 | |
wim | 0:2c5311a4f6fa | 411 | _cs=0; |
wim | 0:2c5311a4f6fa | 412 | wait_us(1); |
wim | 0:2c5311a4f6fa | 413 | _spi.write(_flip(STLED316S_ADDR_WR_CMD | (cmd & STLED316S_IDX_MSK))); |
wim | 0:2c5311a4f6fa | 414 | |
wim | 0:2c5311a4f6fa | 415 | // printf("Cmd= 0x%02X\r\n", STLED316S_ADDR_WR_CMD | (cmd & STLED316S_IDX_MSK)); // Debug |
wim | 0:2c5311a4f6fa | 416 | |
wim | 0:2c5311a4f6fa | 417 | wait_us(1); |
wim | 0:2c5311a4f6fa | 418 | _cs=1; |
wim | 0:2c5311a4f6fa | 419 | } |
wim | 0:2c5311a4f6fa | 420 | |
wim | 0:2c5311a4f6fa | 421 | |
wim | 0:2c5311a4f6fa | 422 | /** Read parameter from STLED316S Register |
wim | 0:2c5311a4f6fa | 423 | * @param int idx Register address |
wim | 0:2c5311a4f6fa | 424 | * @return char data from Register |
wim | 0:2c5311a4f6fa | 425 | */ |
wim | 0:2c5311a4f6fa | 426 | char STLED316S::_readReg(int idx){ |
wim | 0:2c5311a4f6fa | 427 | char data; |
wim | 0:2c5311a4f6fa | 428 | |
wim | 0:2c5311a4f6fa | 429 | _cs=0; |
wim | 0:2c5311a4f6fa | 430 | wait_us(1); |
wim | 0:2c5311a4f6fa | 431 | _spi.write(_flip(STLED316S_ADDR_RD_CMD | (idx & STLED316S_IDX_MSK))); |
wim | 0:2c5311a4f6fa | 432 | |
wim | 0:2c5311a4f6fa | 433 | data = _flip(_spi.write(0xFF)); // read data and correct bitorder |
wim | 0:2c5311a4f6fa | 434 | |
wim | 0:2c5311a4f6fa | 435 | // printf("Cmd= 0x%02X, Read= 0x%02X\r\n", STLED316S_ADDR_RD_CMD | (idx & STLED316S_IDX_MSK), data); // Debug |
wim | 0:2c5311a4f6fa | 436 | |
wim | 0:2c5311a4f6fa | 437 | wait_us(1); |
wim | 0:2c5311a4f6fa | 438 | _cs=1; |
wim | 0:2c5311a4f6fa | 439 | |
wim | 0:2c5311a4f6fa | 440 | return data; |
wim | 0:2c5311a4f6fa | 441 | } |
wim | 0:2c5311a4f6fa | 442 | |
wim | 0:2c5311a4f6fa | 443 | |
wim | 0:2c5311a4f6fa | 444 | |
wim | 0:2c5311a4f6fa | 445 | #if(ST316BOARD_TEST == 1) |
wim | 0:2c5311a4f6fa | 446 | // Derived class for STLED316S used in test display module |
wim | 0:2c5311a4f6fa | 447 | // |
wim | 0:2c5311a4f6fa | 448 | //#include "Font_7Seg.h" |
wim | 0:2c5311a4f6fa | 449 | |
wim | 0:2c5311a4f6fa | 450 | /** Constructor for class for driving STM STLED316S controller as used in ST316S test display |
wim | 0:2c5311a4f6fa | 451 | * |
wim | 0:2c5311a4f6fa | 452 | * @brief Supports 6 Digits of 7 Segments and 3 LEDs. Also supports a scanned keyboard of 3. |
wim | 0:2c5311a4f6fa | 453 | * |
wim | 0:2c5311a4f6fa | 454 | * @param PinName mosi, miso, sclk, cs SPI bus pins |
wim | 0:2c5311a4f6fa | 455 | */ |
wim | 0:2c5311a4f6fa | 456 | STLED316S_BOARD::STLED316S_BOARD(PinName mosi, PinName miso, PinName sclk, PinName cs) : STLED316S(mosi, miso, sclk, cs, Grid6_Seg8) { |
wim | 0:2c5311a4f6fa | 457 | _column = 0; |
wim | 0:2c5311a4f6fa | 458 | _columns = ST316BOARD_NR_DIGITS; |
wim | 0:2c5311a4f6fa | 459 | } |
wim | 0:2c5311a4f6fa | 460 | |
wim | 0:2c5311a4f6fa | 461 | #if(0) |
wim | 0:2c5311a4f6fa | 462 | #if DOXYGEN_ONLY |
wim | 0:2c5311a4f6fa | 463 | /** Write a character to the Display |
wim | 0:2c5311a4f6fa | 464 | * |
wim | 0:2c5311a4f6fa | 465 | * @param c The character to write to the display |
wim | 0:2c5311a4f6fa | 466 | */ |
wim | 0:2c5311a4f6fa | 467 | int putc(int c); |
wim | 0:2c5311a4f6fa | 468 | |
wim | 0:2c5311a4f6fa | 469 | /** Write a formatted string to the Display |
wim | 0:2c5311a4f6fa | 470 | * |
wim | 0:2c5311a4f6fa | 471 | * @param format A printf-style format string, followed by the |
wim | 0:2c5311a4f6fa | 472 | * variables to use in formatting the string. |
wim | 0:2c5311a4f6fa | 473 | */ |
wim | 0:2c5311a4f6fa | 474 | int printf(const char* format, ...); |
wim | 0:2c5311a4f6fa | 475 | #endif |
wim | 0:2c5311a4f6fa | 476 | #endif |
wim | 0:2c5311a4f6fa | 477 | |
wim | 0:2c5311a4f6fa | 478 | /** Locate cursor to a screen column |
wim | 0:2c5311a4f6fa | 479 | * |
wim | 0:2c5311a4f6fa | 480 | * @param column The horizontal position from the left, indexed from 0 |
wim | 0:2c5311a4f6fa | 481 | */ |
wim | 0:2c5311a4f6fa | 482 | void STLED316S_BOARD::locate(int column) { |
wim | 0:2c5311a4f6fa | 483 | //sanity check |
wim | 0:2c5311a4f6fa | 484 | if (column < 0) {column = 0;} |
wim | 0:2c5311a4f6fa | 485 | if (column > (_columns - 1)) {column = _columns - 1;} |
wim | 0:2c5311a4f6fa | 486 | |
wim | 0:2c5311a4f6fa | 487 | _column = column; |
wim | 0:2c5311a4f6fa | 488 | } |
wim | 0:2c5311a4f6fa | 489 | |
wim | 0:2c5311a4f6fa | 490 | |
wim | 0:2c5311a4f6fa | 491 | /** Number of screen columns |
wim | 0:2c5311a4f6fa | 492 | * |
wim | 0:2c5311a4f6fa | 493 | * @param none |
wim | 0:2c5311a4f6fa | 494 | * @return columns |
wim | 0:2c5311a4f6fa | 495 | */ |
wim | 0:2c5311a4f6fa | 496 | int STLED316S_BOARD::columns() { |
wim | 0:2c5311a4f6fa | 497 | return _columns; |
wim | 0:2c5311a4f6fa | 498 | } |
wim | 0:2c5311a4f6fa | 499 | |
wim | 0:2c5311a4f6fa | 500 | |
wim | 0:2c5311a4f6fa | 501 | /** Clear the screen and locate to 0 |
wim | 0:2c5311a4f6fa | 502 | * @param bool clrAll Clear Icons also (default = false) |
wim | 0:2c5311a4f6fa | 503 | */ |
wim | 0:2c5311a4f6fa | 504 | void STLED316S_BOARD::cls(bool clrAll) { |
wim | 0:2c5311a4f6fa | 505 | |
wim | 0:2c5311a4f6fa | 506 | if (clrAll) { |
wim | 0:2c5311a4f6fa | 507 | //clear local buffer for LEDs/Icons |
wim | 0:2c5311a4f6fa | 508 | setLed(0x00); |
wim | 0:2c5311a4f6fa | 509 | } |
wim | 0:2c5311a4f6fa | 510 | |
wim | 0:2c5311a4f6fa | 511 | //clear local buffer for digits |
wim | 0:2c5311a4f6fa | 512 | for (int idx=0; idx < (ST316BOARD_NR_GRIDS * STLED316S_BYTES_PER_GRID); idx++) { |
wim | 0:2c5311a4f6fa | 513 | _displaybuffer[idx] = 0x00; |
wim | 0:2c5311a4f6fa | 514 | } |
wim | 0:2c5311a4f6fa | 515 | writeData(_displaybuffer, (ST316BOARD_NR_GRIDS * STLED316S_BYTES_PER_GRID)); |
wim | 0:2c5311a4f6fa | 516 | |
wim | 0:2c5311a4f6fa | 517 | _column = 0; |
wim | 0:2c5311a4f6fa | 518 | } |
wim | 0:2c5311a4f6fa | 519 | |
wim | 0:2c5311a4f6fa | 520 | |
wim | 0:2c5311a4f6fa | 521 | /** Set Icon |
wim | 0:2c5311a4f6fa | 522 | * |
wim | 0:2c5311a4f6fa | 523 | * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 16 LSBs |
wim | 0:2c5311a4f6fa | 524 | * @return none |
wim | 0:2c5311a4f6fa | 525 | */ |
wim | 0:2c5311a4f6fa | 526 | void STLED316S_BOARD::setIcon(Icon icon) { |
wim | 0:2c5311a4f6fa | 527 | // int addr; |
wim | 0:2c5311a4f6fa | 528 | int icn; |
wim | 0:2c5311a4f6fa | 529 | |
wim | 0:2c5311a4f6fa | 530 | icn = icon & 0xFF; |
wim | 0:2c5311a4f6fa | 531 | // addr = (icon >> 24) & 0xFF; |
wim | 0:2c5311a4f6fa | 532 | |
wim | 0:2c5311a4f6fa | 533 | setLed(icn); |
wim | 0:2c5311a4f6fa | 534 | } |
wim | 0:2c5311a4f6fa | 535 | |
wim | 0:2c5311a4f6fa | 536 | /** Clr Icon |
wim | 0:2c5311a4f6fa | 537 | * |
wim | 0:2c5311a4f6fa | 538 | * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 16 LSBs |
wim | 0:2c5311a4f6fa | 539 | * @return none |
wim | 0:2c5311a4f6fa | 540 | */ |
wim | 0:2c5311a4f6fa | 541 | void STLED316S_BOARD::clrIcon(Icon icon) { |
wim | 0:2c5311a4f6fa | 542 | // int addr; |
wim | 0:2c5311a4f6fa | 543 | int icn; |
wim | 0:2c5311a4f6fa | 544 | |
wim | 0:2c5311a4f6fa | 545 | icn = icon & 0xFF; |
wim | 0:2c5311a4f6fa | 546 | // addr = (icon >> 24) & 0xFF; |
wim | 0:2c5311a4f6fa | 547 | |
wim | 0:2c5311a4f6fa | 548 | clrLed(icn); |
wim | 0:2c5311a4f6fa | 549 | } |
wim | 0:2c5311a4f6fa | 550 | |
wim | 0:2c5311a4f6fa | 551 | |
wim | 0:2c5311a4f6fa | 552 | /** Set User Defined Characters (UDC) |
wim | 0:2c5311a4f6fa | 553 | * |
wim | 0:2c5311a4f6fa | 554 | * @param unsigned char udc_idx The Index of the UDC (0..7) |
wim | 0:2c5311a4f6fa | 555 | * @param int udc_data The bitpattern for the UDC (16 bits) |
wim | 0:2c5311a4f6fa | 556 | */ |
wim | 0:2c5311a4f6fa | 557 | void STLED316S_BOARD::setUDC(unsigned char udc_idx, int udc_data) { |
wim | 0:2c5311a4f6fa | 558 | |
wim | 0:2c5311a4f6fa | 559 | //Sanity check |
wim | 0:2c5311a4f6fa | 560 | if (udc_idx > (ST316BOARD_NR_UDC-1)) { |
wim | 0:2c5311a4f6fa | 561 | return; |
wim | 0:2c5311a4f6fa | 562 | } |
wim | 0:2c5311a4f6fa | 563 | // Mask out Icon bits? |
wim | 0:2c5311a4f6fa | 564 | |
wim | 0:2c5311a4f6fa | 565 | _UDC_7S[udc_idx] = udc_data; |
wim | 0:2c5311a4f6fa | 566 | } |
wim | 0:2c5311a4f6fa | 567 | |
wim | 0:2c5311a4f6fa | 568 | |
wim | 0:2c5311a4f6fa | 569 | /** Write a single character (Stream implementation) |
wim | 0:2c5311a4f6fa | 570 | */ |
wim | 0:2c5311a4f6fa | 571 | int STLED316S_BOARD::_putc(int value) { |
wim | 0:2c5311a4f6fa | 572 | |
wim | 0:2c5311a4f6fa | 573 | int addr; |
wim | 0:2c5311a4f6fa | 574 | bool validChar = false; |
wim | 0:2c5311a4f6fa | 575 | char pattern = 0x00; |
wim | 0:2c5311a4f6fa | 576 | |
wim | 0:2c5311a4f6fa | 577 | if ((value == '\n') || (value == '\r')) { |
wim | 0:2c5311a4f6fa | 578 | //No character to write |
wim | 0:2c5311a4f6fa | 579 | validChar = false; |
wim | 0:2c5311a4f6fa | 580 | |
wim | 0:2c5311a4f6fa | 581 | //Update Cursor |
wim | 0:2c5311a4f6fa | 582 | _column = 0; |
wim | 0:2c5311a4f6fa | 583 | } |
wim | 0:2c5311a4f6fa | 584 | else if ((value == '.') || (value == ',')) { |
wim | 0:2c5311a4f6fa | 585 | //No character to write |
wim | 0:2c5311a4f6fa | 586 | validChar = false; |
wim | 0:2c5311a4f6fa | 587 | pattern = S7_DP; // placeholder for all DPs |
wim | 0:2c5311a4f6fa | 588 | |
wim | 0:2c5311a4f6fa | 589 | // Check to see that DP can be shown for current column |
wim | 0:2c5311a4f6fa | 590 | if (_column > 0) { |
wim | 0:2c5311a4f6fa | 591 | //Translate between _column and displaybuffer entries |
wim | 0:2c5311a4f6fa | 592 | //Add DP to bitpattern of digit left of current column. |
wim | 0:2c5311a4f6fa | 593 | addr = (_column - 1); |
wim | 0:2c5311a4f6fa | 594 | |
wim | 0:2c5311a4f6fa | 595 | //Set bits for decimal point to write |
wim | 0:2c5311a4f6fa | 596 | _displaybuffer[addr] = _displaybuffer[addr] | pattern; |
wim | 0:2c5311a4f6fa | 597 | writeData(_displaybuffer, (ST316BOARD_NR_GRIDS * STLED316S_BYTES_PER_GRID)); |
wim | 0:2c5311a4f6fa | 598 | |
wim | 0:2c5311a4f6fa | 599 | //No Cursor Update |
wim | 0:2c5311a4f6fa | 600 | } |
wim | 0:2c5311a4f6fa | 601 | } |
wim | 0:2c5311a4f6fa | 602 | else if ((value >= 0) && (value < ST316BOARD_NR_UDC)) { |
wim | 0:2c5311a4f6fa | 603 | //Character to write |
wim | 0:2c5311a4f6fa | 604 | validChar = true; |
wim | 0:2c5311a4f6fa | 605 | pattern = _UDC_7S[value]; |
wim | 0:2c5311a4f6fa | 606 | } |
wim | 0:2c5311a4f6fa | 607 | |
wim | 0:2c5311a4f6fa | 608 | #if (SHOW_ASCII == 1) |
wim | 0:2c5311a4f6fa | 609 | //display all ASCII characters |
wim | 0:2c5311a4f6fa | 610 | else if ((value >= FONT_7S_START) && (value <= FONT_7S_END)) { |
wim | 0:2c5311a4f6fa | 611 | //Character to write |
wim | 0:2c5311a4f6fa | 612 | validChar = true; |
wim | 0:2c5311a4f6fa | 613 | pattern = FONT_7S[value - FONT_7S_START]; |
wim | 0:2c5311a4f6fa | 614 | } // else |
wim | 0:2c5311a4f6fa | 615 | #else |
wim | 0:2c5311a4f6fa | 616 | //display only digits and hex characters |
wim | 0:2c5311a4f6fa | 617 | else if (value == '-') { |
wim | 0:2c5311a4f6fa | 618 | //Character to write |
wim | 0:2c5311a4f6fa | 619 | validChar = true; |
wim | 0:2c5311a4f6fa | 620 | pattern = C7_MIN; |
wim | 0:2c5311a4f6fa | 621 | } |
wim | 0:2c5311a4f6fa | 622 | else if ((value >= (int)'0') && (value <= (int) '9')) { |
wim | 0:2c5311a4f6fa | 623 | //Character to write |
wim | 0:2c5311a4f6fa | 624 | validChar = true; |
wim | 0:2c5311a4f6fa | 625 | pattern = FONT_7S[value - (int) '0']; |
wim | 0:2c5311a4f6fa | 626 | } |
wim | 0:2c5311a4f6fa | 627 | else if ((value >= (int) 'A') && (value <= (int) 'F')) { |
wim | 0:2c5311a4f6fa | 628 | //Character to write |
wim | 0:2c5311a4f6fa | 629 | validChar = true; |
wim | 0:2c5311a4f6fa | 630 | pattern = FONT_7S[10 + value - (int) 'A']; |
wim | 0:2c5311a4f6fa | 631 | } |
wim | 0:2c5311a4f6fa | 632 | else if ((value >= (int) 'a') && (value <= (int) 'f')) { |
wim | 0:2c5311a4f6fa | 633 | //Character to write |
wim | 0:2c5311a4f6fa | 634 | validChar = true; |
wim | 0:2c5311a4f6fa | 635 | pattern = FONT_7S[10 + value - (int) 'a']; |
wim | 0:2c5311a4f6fa | 636 | } //else |
wim | 0:2c5311a4f6fa | 637 | #endif |
wim | 0:2c5311a4f6fa | 638 | |
wim | 0:2c5311a4f6fa | 639 | if (validChar) { |
wim | 0:2c5311a4f6fa | 640 | //Character to write |
wim | 0:2c5311a4f6fa | 641 | |
wim | 0:2c5311a4f6fa | 642 | //Translate between _column and displaybuffer entries |
wim | 0:2c5311a4f6fa | 643 | //_column == 0 => Grid0 => addr = 0 |
wim | 0:2c5311a4f6fa | 644 | // |
wim | 0:2c5311a4f6fa | 645 | //_column == 5 => Grid5 => addr = 5 |
wim | 0:2c5311a4f6fa | 646 | addr = _column; |
wim | 0:2c5311a4f6fa | 647 | _displaybuffer[addr] = pattern; |
wim | 0:2c5311a4f6fa | 648 | |
wim | 0:2c5311a4f6fa | 649 | writeData(_displaybuffer, (ST316BOARD_NR_GRIDS * STLED316S_BYTES_PER_GRID)); |
wim | 0:2c5311a4f6fa | 650 | |
wim | 0:2c5311a4f6fa | 651 | //Update Cursor |
wim | 0:2c5311a4f6fa | 652 | _column++; |
wim | 0:2c5311a4f6fa | 653 | if (_column > (ST316BOARD_NR_DIGITS - 1)) { |
wim | 0:2c5311a4f6fa | 654 | _column = 0; |
wim | 0:2c5311a4f6fa | 655 | } |
wim | 0:2c5311a4f6fa | 656 | |
wim | 0:2c5311a4f6fa | 657 | } // if validChar |
wim | 0:2c5311a4f6fa | 658 | |
wim | 0:2c5311a4f6fa | 659 | return value; |
wim | 0:2c5311a4f6fa | 660 | } |
wim | 0:2c5311a4f6fa | 661 | |
wim | 0:2c5311a4f6fa | 662 | // get a single character (Stream implementation) |
wim | 0:2c5311a4f6fa | 663 | int STLED316S_BOARD::_getc() { |
wim | 0:2c5311a4f6fa | 664 | return -1; |
wim | 0:2c5311a4f6fa | 665 | } |
wim | 0:2c5311a4f6fa | 666 | #endif |