LED Driver, 6 digits @ 8 segm, 8 LEDs, 16 Keys. SPI Interface
See here for more information.
STLED316S.h@0:2c5311a4f6fa, 2016-10-01 (annotated)
- Committer:
- wim
- Date:
- Sat Oct 01 15:09:51 2016 +0000
- Revision:
- 0:2c5311a4f6fa
- Child:
- 1:7a845a89625f
ST316LED driver lib; First Release
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 | |
wim | 0:2c5311a4f6fa | 23 | #ifndef STLED316S_H |
wim | 0:2c5311a4f6fa | 24 | #define STLED316S_H |
wim | 0:2c5311a4f6fa | 25 | |
wim | 0:2c5311a4f6fa | 26 | // Select one of the testboards for the STM STLED316S LED controller |
wim | 0:2c5311a4f6fa | 27 | #include "STLED316S_Config.h" |
wim | 0:2c5311a4f6fa | 28 | |
wim | 0:2c5311a4f6fa | 29 | #include "Font_7Seg.h" |
wim | 0:2c5311a4f6fa | 30 | |
wim | 0:2c5311a4f6fa | 31 | /** An interface for driving STLED316S LED controller |
wim | 0:2c5311a4f6fa | 32 | * |
wim | 0:2c5311a4f6fa | 33 | * @code |
wim | 0:2c5311a4f6fa | 34 | * #include "mbed.h" |
wim | 0:2c5311a4f6fa | 35 | * #include "STLED316S.h" |
wim | 0:2c5311a4f6fa | 36 | * |
wim | 0:2c5311a4f6fa | 37 | * DisplayData_t size is 8 bytes (4 grids @ 13 segments) OR 10 bytes (5 grids @ 12 segments) OR |
wim | 0:2c5311a4f6fa | 38 | * 12 bytes (6 grids @ 11 segments) OR 14 bytes (7 grids @ 10 segments) |
wim | 0:2c5311a4f6fa | 39 | * STLED316S::DisplayData_t mbed_str = {0xDA,0x00, 0x7C,0x00, 0x3C,0x01, 0xF6,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00}; |
wim | 0:2c5311a4f6fa | 40 | * STLED316S::DisplayData_t all_str = {0xFF,0x0F, 0xFF,0x0F, 0xFF,0x0F, 0xFF,0x0F, 0xFF,0x0F, 0xFF,0x0F, 0xFF,0x0F}; |
wim | 0:2c5311a4f6fa | 41 | * |
wim | 0:2c5311a4f6fa | 42 | * // KeyData_t size is 5 bytes |
wim | 0:2c5311a4f6fa | 43 | * STLED316S::KeyData_t keydata; |
wim | 0:2c5311a4f6fa | 44 | * |
wim | 0:2c5311a4f6fa | 45 | * // STLED316S declaration, Default setting 7 Grids @ 10 Segments |
wim | 0:2c5311a4f6fa | 46 | * STLED316S STLED316S(p5,p6,p7, p8); |
wim | 0:2c5311a4f6fa | 47 | * |
wim | 0:2c5311a4f6fa | 48 | * int main() { |
wim | 0:2c5311a4f6fa | 49 | * STLED316S.cls(); |
wim | 0:2c5311a4f6fa | 50 | * STLED316S.writeData(all_str); |
wim | 0:2c5311a4f6fa | 51 | * wait(4); |
wim | 0:2c5311a4f6fa | 52 | * STLED316S.writeData(mbed_str); |
wim | 0:2c5311a4f6fa | 53 | * wait(1); |
wim | 0:2c5311a4f6fa | 54 | * STLED316S.setBrightness(STLED316S_BRT0); |
wim | 0:2c5311a4f6fa | 55 | * wait(1); |
wim | 0:2c5311a4f6fa | 56 | * STLED316S.setBrightness(STLED316S_BRT3); |
wim | 0:2c5311a4f6fa | 57 | * |
wim | 0:2c5311a4f6fa | 58 | * while (1) { |
wim | 0:2c5311a4f6fa | 59 | * // Check and read keydata |
wim | 0:2c5311a4f6fa | 60 | * if (STLED316S.getKeys(&keydata)) { |
wim | 0:2c5311a4f6fa | 61 | * pc.printf("Keydata 0..4 = 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\r\n", keydata[0], keydata[1], keydata[2], keydata[3], keydata[4]); |
wim | 0:2c5311a4f6fa | 62 | * |
wim | 0:2c5311a4f6fa | 63 | * if (keydata[0] == 0x10) { //sw2 |
wim | 0:2c5311a4f6fa | 64 | * STLED316S.cls(); |
wim | 0:2c5311a4f6fa | 65 | * STLED316S.writeData(all_str); |
wim | 0:2c5311a4f6fa | 66 | * } |
wim | 0:2c5311a4f6fa | 67 | * } |
wim | 0:2c5311a4f6fa | 68 | * } |
wim | 0:2c5311a4f6fa | 69 | * } |
wim | 0:2c5311a4f6fa | 70 | * @endcode |
wim | 0:2c5311a4f6fa | 71 | */ |
wim | 0:2c5311a4f6fa | 72 | |
wim | 0:2c5311a4f6fa | 73 | //STLED316S Display and Keymatrix data |
wim | 0:2c5311a4f6fa | 74 | #define STLED316S_MAX_NR_GRIDS 6 |
wim | 0:2c5311a4f6fa | 75 | #define STLED316S_BYTES_PER_GRID 1 |
wim | 0:2c5311a4f6fa | 76 | //Significant bits Keymatrix data |
wim | 0:2c5311a4f6fa | 77 | #define STLED316S_KEY_MSK 0xFF |
wim | 0:2c5311a4f6fa | 78 | |
wim | 0:2c5311a4f6fa | 79 | //Memory size in bytes for Display and Keymatrix |
wim | 0:2c5311a4f6fa | 80 | #define STLED316S_DISPLAY_MEM (STLED316S_MAX_NR_GRIDS * STLED316S_BYTES_PER_GRID) |
wim | 0:2c5311a4f6fa | 81 | #define STLED316S_KEY_MEM 2 |
wim | 0:2c5311a4f6fa | 82 | |
wim | 0:2c5311a4f6fa | 83 | //Address set commands |
wim | 0:2c5311a4f6fa | 84 | #define STLED316S_CMD_MSK 0x7F |
wim | 0:2c5311a4f6fa | 85 | #define STLED316S_DATA_WR 0x00 |
wim | 0:2c5311a4f6fa | 86 | #define STLED316S_DATA_RD 0x40 |
wim | 0:2c5311a4f6fa | 87 | #define STLED316S_ADDR_INC 0x00 |
wim | 0:2c5311a4f6fa | 88 | #define STLED316S_ADDR_FIXED 0x20 |
wim | 0:2c5311a4f6fa | 89 | #define STLED316S_PAGE_MSK 0x18 |
wim | 0:2c5311a4f6fa | 90 | #define STLED316S_PAGE_SHFT 3 |
wim | 0:2c5311a4f6fa | 91 | #define STLED316S_ADDR_MSK 0x07 |
wim | 0:2c5311a4f6fa | 92 | #define STLED316S_ADDR_SHFT 0 |
wim | 0:2c5311a4f6fa | 93 | #define STLED316S_IDX_MSK (STLED316S_PAGE_MSK | STLED316S_ADDR_MSK) |
wim | 0:2c5311a4f6fa | 94 | |
wim | 0:2c5311a4f6fa | 95 | //Read and Write commands |
wim | 0:2c5311a4f6fa | 96 | #define STLED316S_ADDR_WR_CMD (STLED316S_DATA_WR | STLED316S_ADDR_INC) |
wim | 0:2c5311a4f6fa | 97 | #define STLED316S_ADDR_RD_CMD (STLED316S_DATA_RD | STLED316S_ADDR_INC) |
wim | 0:2c5311a4f6fa | 98 | |
wim | 0:2c5311a4f6fa | 99 | //Combined Page and Address parameters |
wim | 0:2c5311a4f6fa | 100 | #define STLED316S_IDX(page, addr) ( ((page << STLED316S_PAGE_SHFT) & STLED316S_PAGE_MSK) | ((addr << STLED316S_ADDR_SHFT) & STLED316S_ADDR_MSK) ) |
wim | 0:2c5311a4f6fa | 101 | |
wim | 0:2c5311a4f6fa | 102 | |
wim | 0:2c5311a4f6fa | 103 | //Digit Data address (Digit2..Digit7) |
wim | 0:2c5311a4f6fa | 104 | #define STLED316S_DIG_PAGE 0 |
wim | 0:2c5311a4f6fa | 105 | #define STLED316S_DIG2_ADDR 0x00 |
wim | 0:2c5311a4f6fa | 106 | #define STLED316S_DIG3_ADDR 0x01 |
wim | 0:2c5311a4f6fa | 107 | #define STLED316S_DIG4_ADDR 0x02 |
wim | 0:2c5311a4f6fa | 108 | #define STLED316S_DIG5_ADDR 0x03 |
wim | 0:2c5311a4f6fa | 109 | #define STLED316S_DIG6_ADDR 0x04 |
wim | 0:2c5311a4f6fa | 110 | #define STLED316S_DIG7_ADDR 0x05 |
wim | 0:2c5311a4f6fa | 111 | |
wim | 0:2c5311a4f6fa | 112 | //LED Data address (also Digit1 Data address) |
wim | 0:2c5311a4f6fa | 113 | #define STLED316S_DIG1_LED_PAGE 1 |
wim | 0:2c5311a4f6fa | 114 | #define STLED316S_DIG1_LED_ADDR 0x00 |
wim | 0:2c5311a4f6fa | 115 | |
wim | 0:2c5311a4f6fa | 116 | //LED Data used to select individual LEDs |
wim | 0:2c5311a4f6fa | 117 | #define STLED316S_LED_L1 0x01 |
wim | 0:2c5311a4f6fa | 118 | #define STLED316S_LED_L2 0x02 |
wim | 0:2c5311a4f6fa | 119 | #define STLED316S_LED_L3 0x04 |
wim | 0:2c5311a4f6fa | 120 | #define STLED316S_LED_L4 0x08 |
wim | 0:2c5311a4f6fa | 121 | #define STLED316S_LED_L5 0x10 |
wim | 0:2c5311a4f6fa | 122 | #define STLED316S_LED_L6 0x20 |
wim | 0:2c5311a4f6fa | 123 | #define STLED316S_LED_L7 0x40 |
wim | 0:2c5311a4f6fa | 124 | #define STLED316S_LED_L8 0x80 |
wim | 0:2c5311a4f6fa | 125 | #define STLED316S_LED_ALL 0xFF |
wim | 0:2c5311a4f6fa | 126 | |
wim | 0:2c5311a4f6fa | 127 | //Digit Data used to select individual digits |
wim | 0:2c5311a4f6fa | 128 | //#define STLED316S_DIG_D1 0x01 |
wim | 0:2c5311a4f6fa | 129 | #define STLED316S_DIG_D2 0x02 |
wim | 0:2c5311a4f6fa | 130 | #define STLED316S_DIG_D3 0x04 |
wim | 0:2c5311a4f6fa | 131 | #define STLED316S_DIG_D4 0x08 |
wim | 0:2c5311a4f6fa | 132 | #define STLED316S_DIG_D5 0x10 |
wim | 0:2c5311a4f6fa | 133 | #define STLED316S_DIG_D6 0x20 |
wim | 0:2c5311a4f6fa | 134 | #define STLED316S_DIG_D7 0x40 |
wim | 0:2c5311a4f6fa | 135 | #define STLED316S_DIG_ALL 0x7E |
wim | 0:2c5311a4f6fa | 136 | |
wim | 0:2c5311a4f6fa | 137 | |
wim | 0:2c5311a4f6fa | 138 | //Key Data address |
wim | 0:2c5311a4f6fa | 139 | #define STLED316S_KEY_PAGE 1 |
wim | 0:2c5311a4f6fa | 140 | #define STLED316S_KEY1_ADDR 0x01 |
wim | 0:2c5311a4f6fa | 141 | #define STLED316S_KEY2_ADDR 0x02 |
wim | 0:2c5311a4f6fa | 142 | |
wim | 0:2c5311a4f6fa | 143 | //Display On_Off address |
wim | 0:2c5311a4f6fa | 144 | #define STLED316S_DSP_PAGE 1 |
wim | 0:2c5311a4f6fa | 145 | #define STLED316S_DSP_ON_ADDR 0x05 |
wim | 0:2c5311a4f6fa | 146 | #define STLED316S_DSP_OFF_ADDR 0x06 |
wim | 0:2c5311a4f6fa | 147 | |
wim | 0:2c5311a4f6fa | 148 | //Config set address |
wim | 0:2c5311a4f6fa | 149 | #define STLED316S_CONF_PAGE 2 |
wim | 0:2c5311a4f6fa | 150 | #define STLED316S_CONF_ADDR 0x00 |
wim | 0:2c5311a4f6fa | 151 | |
wim | 0:2c5311a4f6fa | 152 | //Config parameters |
wim | 0:2c5311a4f6fa | 153 | #define STLED316S_CONF_GRID_MSK 0x07 |
wim | 0:2c5311a4f6fa | 154 | #define STLED316S_CONF_GRID_SHFT 0 |
wim | 0:2c5311a4f6fa | 155 | #define STLED316S_CONF_BRT_MODE_MSK 0x18 |
wim | 0:2c5311a4f6fa | 156 | #define STLED316S_CONF_BRT_MODE_SHFT 3 |
wim | 0:2c5311a4f6fa | 157 | #define STLED316S_CONF_BRT_GLOB_MSK 0xE0 |
wim | 0:2c5311a4f6fa | 158 | #define STLED316S_CONF_BRT_GLOB_SHFT 5 |
wim | 0:2c5311a4f6fa | 159 | |
wim | 0:2c5311a4f6fa | 160 | //Grid Config parameters |
wim | 0:2c5311a4f6fa | 161 | #define STLED316S_GR1_SEG8 0x00 |
wim | 0:2c5311a4f6fa | 162 | #define STLED316S_GR2_SEG8 0x01 |
wim | 0:2c5311a4f6fa | 163 | #define STLED316S_GR3_SEG8 0x02 |
wim | 0:2c5311a4f6fa | 164 | #define STLED316S_GR4_SEG8 0x03 //default |
wim | 0:2c5311a4f6fa | 165 | #define STLED316S_GR5_SEG8 0x04 |
wim | 0:2c5311a4f6fa | 166 | #define STLED316S_GR6_SEG8 0x05 |
wim | 0:2c5311a4f6fa | 167 | |
wim | 0:2c5311a4f6fa | 168 | //Brightness Mode Config parameters |
wim | 0:2c5311a4f6fa | 169 | #define STLED316S_BRT_INDIV 0x00 |
wim | 0:2c5311a4f6fa | 170 | #define STLED316S_BRT_GLOB 0x03 //default |
wim | 0:2c5311a4f6fa | 171 | |
wim | 0:2c5311a4f6fa | 172 | //Digit Brightness address (used when Brightness mode is set as 'individual') |
wim | 0:2c5311a4f6fa | 173 | #define STLED316S_DIG_BRT_PAGE 2 |
wim | 0:2c5311a4f6fa | 174 | #define STLED316S_DIG2_3_BRT_ADDR 0x01 |
wim | 0:2c5311a4f6fa | 175 | #define STLED316S_DIG4_5_BRT_ADDR 0x02 |
wim | 0:2c5311a4f6fa | 176 | #define STLED316S_DIG6_7_BRT_ADDR 0x03 |
wim | 0:2c5311a4f6fa | 177 | |
wim | 0:2c5311a4f6fa | 178 | //LED Brightness address (used when Brightness mode is set as 'individual') |
wim | 0:2c5311a4f6fa | 179 | #define STLED316S_LED_BRT_PAGE 3 |
wim | 0:2c5311a4f6fa | 180 | #define STLED316S_LED1_2_BRT_ADDR 0x00 |
wim | 0:2c5311a4f6fa | 181 | #define STLED316S_LED3_4_BRT_ADDR 0x01 |
wim | 0:2c5311a4f6fa | 182 | #define STLED316S_LED5_6_BRT_ADDR 0x02 |
wim | 0:2c5311a4f6fa | 183 | #define STLED316S_LED7_8_BRT_ADDR 0x03 |
wim | 0:2c5311a4f6fa | 184 | |
wim | 0:2c5311a4f6fa | 185 | //Brightness control parameters (used for both Brightness modes 'global' and 'individual') |
wim | 0:2c5311a4f6fa | 186 | #define STLED316S_BRT_MSK 0x07 |
wim | 0:2c5311a4f6fa | 187 | |
wim | 0:2c5311a4f6fa | 188 | #define STLED316S_BRT0 0x00 //Pulsewidth 1/16 |
wim | 0:2c5311a4f6fa | 189 | #define STLED316S_BRT1 0x01 |
wim | 0:2c5311a4f6fa | 190 | #define STLED316S_BRT2 0x02 |
wim | 0:2c5311a4f6fa | 191 | #define STLED316S_BRT3 0x03 |
wim | 0:2c5311a4f6fa | 192 | #define STLED316S_BRT4 0x04 |
wim | 0:2c5311a4f6fa | 193 | #define STLED316S_BRT5 0x05 |
wim | 0:2c5311a4f6fa | 194 | #define STLED316S_BRT6 0x06 |
wim | 0:2c5311a4f6fa | 195 | #define STLED316S_BRT7 0x07 //Pulsewidth 14/16 (default) |
wim | 0:2c5311a4f6fa | 196 | |
wim | 0:2c5311a4f6fa | 197 | #define STLED316S_BRT_DEF STLED316S_BRT3 |
wim | 0:2c5311a4f6fa | 198 | |
wim | 0:2c5311a4f6fa | 199 | |
wim | 0:2c5311a4f6fa | 200 | |
wim | 0:2c5311a4f6fa | 201 | /** A class for driving STM STLED316S LED controller |
wim | 0:2c5311a4f6fa | 202 | * |
wim | 0:2c5311a4f6fa | 203 | * @brief Supports 1..6 Grids @ 8 Segments and 8 LEDs. |
wim | 0:2c5311a4f6fa | 204 | * Also supports a scanned keyboard of upto 16 keys. |
wim | 0:2c5311a4f6fa | 205 | * SPI bus interface device. |
wim | 0:2c5311a4f6fa | 206 | */ |
wim | 0:2c5311a4f6fa | 207 | class STLED316S { |
wim | 0:2c5311a4f6fa | 208 | public: |
wim | 0:2c5311a4f6fa | 209 | |
wim | 0:2c5311a4f6fa | 210 | /** Enums for Display mode */ |
wim | 0:2c5311a4f6fa | 211 | enum Mode { |
wim | 0:2c5311a4f6fa | 212 | Grid1_Seg8 = STLED316S_GR1_SEG8, |
wim | 0:2c5311a4f6fa | 213 | Grid2_Seg8 = STLED316S_GR2_SEG8, |
wim | 0:2c5311a4f6fa | 214 | Grid3_Seg8 = STLED316S_GR3_SEG8, |
wim | 0:2c5311a4f6fa | 215 | Grid4_Seg8 = STLED316S_GR4_SEG8, |
wim | 0:2c5311a4f6fa | 216 | Grid5_Seg8 = STLED316S_GR5_SEG8, |
wim | 0:2c5311a4f6fa | 217 | Grid6_Seg8 = STLED316S_GR6_SEG8 |
wim | 0:2c5311a4f6fa | 218 | }; |
wim | 0:2c5311a4f6fa | 219 | |
wim | 0:2c5311a4f6fa | 220 | /** Enums for Brightness mode */ |
wim | 0:2c5311a4f6fa | 221 | enum BrightMode { |
wim | 0:2c5311a4f6fa | 222 | IndivBright = STLED316S_BRT_INDIV, |
wim | 0:2c5311a4f6fa | 223 | GlobalBright = STLED316S_BRT_GLOB |
wim | 0:2c5311a4f6fa | 224 | }; |
wim | 0:2c5311a4f6fa | 225 | |
wim | 0:2c5311a4f6fa | 226 | /** Datatypes for display and keymatrix data */ |
wim | 0:2c5311a4f6fa | 227 | typedef char DisplayData_t[STLED316S_DISPLAY_MEM]; |
wim | 0:2c5311a4f6fa | 228 | typedef char LedData_t; |
wim | 0:2c5311a4f6fa | 229 | typedef char KeyData_t[STLED316S_KEY_MEM]; |
wim | 0:2c5311a4f6fa | 230 | |
wim | 0:2c5311a4f6fa | 231 | /** Constructor for class for driving STLED316S LED controller |
wim | 0:2c5311a4f6fa | 232 | * |
wim | 0:2c5311a4f6fa | 233 | * @brief Supports 1..6 Grids @ 8 Segments and 8 LEDs. |
wim | 0:2c5311a4f6fa | 234 | * Also supports a scanned keyboard of upto 16 keys. |
wim | 0:2c5311a4f6fa | 235 | * SPI bus interface device. |
wim | 0:2c5311a4f6fa | 236 | * |
wim | 0:2c5311a4f6fa | 237 | * @param PinName mosi, miso, sclk, cs SPI bus pins |
wim | 0:2c5311a4f6fa | 238 | * @param Mode selects either Grids/Segments (default 6 Grids @ 8 Segments) |
wim | 0:2c5311a4f6fa | 239 | */ |
wim | 0:2c5311a4f6fa | 240 | STLED316S(PinName mosi, PinName miso, PinName sclk, PinName cs, Mode mode=Grid6_Seg8); |
wim | 0:2c5311a4f6fa | 241 | |
wim | 0:2c5311a4f6fa | 242 | /** Clear the screen and locate to 0 |
wim | 0:2c5311a4f6fa | 243 | */ |
wim | 0:2c5311a4f6fa | 244 | void cls(); |
wim | 0:2c5311a4f6fa | 245 | |
wim | 0:2c5311a4f6fa | 246 | /** Write databyte to STLED316S |
wim | 0:2c5311a4f6fa | 247 | * @param int address display memory location to write byte |
wim | 0:2c5311a4f6fa | 248 | * @param char data byte written at given address |
wim | 0:2c5311a4f6fa | 249 | * @return none |
wim | 0:2c5311a4f6fa | 250 | */ |
wim | 0:2c5311a4f6fa | 251 | void writeData(int address, char data); |
wim | 0:2c5311a4f6fa | 252 | |
wim | 0:2c5311a4f6fa | 253 | /** Write Display datablock to STLED316S |
wim | 0:2c5311a4f6fa | 254 | * @param DisplayData_t data Array of STLED316S_DISPLAY_MEM (=6) bytes for displaydata (starting at address 0) |
wim | 0:2c5311a4f6fa | 255 | * @param length number bytes to write (valid range 0..STLED316S_DISPLAY_MEM (=6), starting at address 0) |
wim | 0:2c5311a4f6fa | 256 | * @return none |
wim | 0:2c5311a4f6fa | 257 | */ |
wim | 0:2c5311a4f6fa | 258 | void writeData(DisplayData_t data, int length = STLED316S_DISPLAY_MEM); |
wim | 0:2c5311a4f6fa | 259 | |
wim | 0:2c5311a4f6fa | 260 | /** Write LED data to STLED316S |
wim | 0:2c5311a4f6fa | 261 | * @param LedData_t leds LED data |
wim | 0:2c5311a4f6fa | 262 | * @return none |
wim | 0:2c5311a4f6fa | 263 | */ |
wim | 0:2c5311a4f6fa | 264 | void writeLedData(LedData_t leds); |
wim | 0:2c5311a4f6fa | 265 | |
wim | 0:2c5311a4f6fa | 266 | /** Set LED |
wim | 0:2c5311a4f6fa | 267 | * |
wim | 0:2c5311a4f6fa | 268 | * @param LedData_t leds pattern of LED data |
wim | 0:2c5311a4f6fa | 269 | * @return none |
wim | 0:2c5311a4f6fa | 270 | */ |
wim | 0:2c5311a4f6fa | 271 | void setLed(LedData_t leds); |
wim | 0:2c5311a4f6fa | 272 | |
wim | 0:2c5311a4f6fa | 273 | /** Clr LED |
wim | 0:2c5311a4f6fa | 274 | * |
wim | 0:2c5311a4f6fa | 275 | * @param LedData_t leds pattern of LED data |
wim | 0:2c5311a4f6fa | 276 | * @return none |
wim | 0:2c5311a4f6fa | 277 | */ |
wim | 0:2c5311a4f6fa | 278 | void clrLed(LedData_t leds); |
wim | 0:2c5311a4f6fa | 279 | |
wim | 0:2c5311a4f6fa | 280 | |
wim | 0:2c5311a4f6fa | 281 | /** Read keydata block from STLED316S |
wim | 0:2c5311a4f6fa | 282 | * @param *keydata Ptr to Array of STLED316S_KEY_MEM (=2) bytes for keydata |
wim | 0:2c5311a4f6fa | 283 | * @return bool keypress True when at least one key was pressed |
wim | 0:2c5311a4f6fa | 284 | * |
wim | 0:2c5311a4f6fa | 285 | * Note: Due to the hardware configuration the STLED316S key matrix scanner will detect multiple keys pressed at same time, |
wim | 0:2c5311a4f6fa | 286 | * but this may result in some spurious keys also being set in keypress data array. |
wim | 0:2c5311a4f6fa | 287 | * 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 | 288 | */ |
wim | 0:2c5311a4f6fa | 289 | bool getKeys(KeyData_t *keydata); |
wim | 0:2c5311a4f6fa | 290 | |
wim | 0:2c5311a4f6fa | 291 | /** Set Brightness for all Digits and LEDs (value is used in GlobalBrightness mode) |
wim | 0:2c5311a4f6fa | 292 | * |
wim | 0:2c5311a4f6fa | 293 | * @param char brightness (3 significant bits, valid range 0..7 (1/16 .. 14/14 dutycycle) |
wim | 0:2c5311a4f6fa | 294 | * @return none |
wim | 0:2c5311a4f6fa | 295 | */ |
wim | 0:2c5311a4f6fa | 296 | void setBrightness(char brightness = STLED316S_BRT_DEF); |
wim | 0:2c5311a4f6fa | 297 | |
wim | 0:2c5311a4f6fa | 298 | /** Set Individual LED Brightness (value is used in IndivBright mode) |
wim | 0:2c5311a4f6fa | 299 | * |
wim | 0:2c5311a4f6fa | 300 | * @param LedData_t leds pattern of LED data |
wim | 0:2c5311a4f6fa | 301 | * @param char led_brt (3 significant bits, valid range 0..7 (1/16 .. 14/14 dutycycle) |
wim | 0:2c5311a4f6fa | 302 | * @return none |
wim | 0:2c5311a4f6fa | 303 | */ |
wim | 0:2c5311a4f6fa | 304 | void setLedBrightness(LedData_t leds = STLED316S_LED_ALL, char led_brt = STLED316S_BRT_DEF); |
wim | 0:2c5311a4f6fa | 305 | |
wim | 0:2c5311a4f6fa | 306 | /** Set Individual Digit Brightness (value is used in IndivBright mode) |
wim | 0:2c5311a4f6fa | 307 | * |
wim | 0:2c5311a4f6fa | 308 | * @param LedData_t digits pattern of Digit data |
wim | 0:2c5311a4f6fa | 309 | * @param char dig_brt (3 significant bits, valid range 0..7 (1/16 .. 14/14 dutycycle) |
wim | 0:2c5311a4f6fa | 310 | * @return none |
wim | 0:2c5311a4f6fa | 311 | */ |
wim | 0:2c5311a4f6fa | 312 | void setDigitBrightness(LedData_t digits = STLED316S_DIG_ALL, char dig_brt = STLED316S_BRT_DEF); |
wim | 0:2c5311a4f6fa | 313 | |
wim | 0:2c5311a4f6fa | 314 | /** Set Brightness mode |
wim | 0:2c5311a4f6fa | 315 | * |
wim | 0:2c5311a4f6fa | 316 | * @param BrightMode brt_mode (value is IndivBright or GlobalBright) |
wim | 0:2c5311a4f6fa | 317 | * @return none |
wim | 0:2c5311a4f6fa | 318 | */ |
wim | 0:2c5311a4f6fa | 319 | void setBrightMode(BrightMode brt_mode = GlobalBright); |
wim | 0:2c5311a4f6fa | 320 | |
wim | 0:2c5311a4f6fa | 321 | |
wim | 0:2c5311a4f6fa | 322 | /** Set the Display mode On/off |
wim | 0:2c5311a4f6fa | 323 | * |
wim | 0:2c5311a4f6fa | 324 | * @param bool display mode |
wim | 0:2c5311a4f6fa | 325 | */ |
wim | 0:2c5311a4f6fa | 326 | void setDisplay(bool on); |
wim | 0:2c5311a4f6fa | 327 | |
wim | 0:2c5311a4f6fa | 328 | private: |
wim | 0:2c5311a4f6fa | 329 | SPI _spi; |
wim | 0:2c5311a4f6fa | 330 | DigitalOut _cs; |
wim | 0:2c5311a4f6fa | 331 | Mode _mode; |
wim | 0:2c5311a4f6fa | 332 | BrightMode _brt_mode; |
wim | 0:2c5311a4f6fa | 333 | char _bright; |
wim | 0:2c5311a4f6fa | 334 | LedData_t _leds; |
wim | 0:2c5311a4f6fa | 335 | |
wim | 0:2c5311a4f6fa | 336 | /** Init the SPI interface and the controller |
wim | 0:2c5311a4f6fa | 337 | * @param none |
wim | 0:2c5311a4f6fa | 338 | * @return none |
wim | 0:2c5311a4f6fa | 339 | */ |
wim | 0:2c5311a4f6fa | 340 | void _init(); |
wim | 0:2c5311a4f6fa | 341 | |
wim | 0:2c5311a4f6fa | 342 | /** Helper to reverse all command or databits. The STLED316S expects LSB first, whereas SPI is MSB first |
wim | 0:2c5311a4f6fa | 343 | * @param char data |
wim | 0:2c5311a4f6fa | 344 | * @return bitreversed data |
wim | 0:2c5311a4f6fa | 345 | */ |
wim | 0:2c5311a4f6fa | 346 | char _flip(char data); |
wim | 0:2c5311a4f6fa | 347 | |
wim | 0:2c5311a4f6fa | 348 | /** Write parameter to STLED316S Register |
wim | 0:2c5311a4f6fa | 349 | * @param int idx Register address |
wim | 0:2c5311a4f6fa | 350 | * @param int data Parameter for Register |
wim | 0:2c5311a4f6fa | 351 | * @return none |
wim | 0:2c5311a4f6fa | 352 | */ |
wim | 0:2c5311a4f6fa | 353 | void _writeReg(int idx, int data); |
wim | 0:2c5311a4f6fa | 354 | |
wim | 0:2c5311a4f6fa | 355 | /** Write merged command and parameter to STLED316S |
wim | 0:2c5311a4f6fa | 356 | * @param int cmd Command & Parameter byte |
wim | 0:2c5311a4f6fa | 357 | * @return none |
wim | 0:2c5311a4f6fa | 358 | */ |
wim | 0:2c5311a4f6fa | 359 | void _writeReg(int cmd); |
wim | 0:2c5311a4f6fa | 360 | |
wim | 0:2c5311a4f6fa | 361 | /** Read parameter from STLED316S Register |
wim | 0:2c5311a4f6fa | 362 | * @param int idx Register address |
wim | 0:2c5311a4f6fa | 363 | * @return char data from Register |
wim | 0:2c5311a4f6fa | 364 | */ |
wim | 0:2c5311a4f6fa | 365 | char _readReg(int idx); |
wim | 0:2c5311a4f6fa | 366 | }; |
wim | 0:2c5311a4f6fa | 367 | |
wim | 0:2c5311a4f6fa | 368 | |
wim | 0:2c5311a4f6fa | 369 | |
wim | 0:2c5311a4f6fa | 370 | #if(ST316BOARD_TEST == 1) |
wim | 0:2c5311a4f6fa | 371 | // Derived class for STLED316S used in test display module |
wim | 0:2c5311a4f6fa | 372 | // |
wim | 0:2c5311a4f6fa | 373 | #include "Font_7Seg.h" |
wim | 0:2c5311a4f6fa | 374 | |
wim | 0:2c5311a4f6fa | 375 | #define ST316BOARD_NR_GRIDS 6 |
wim | 0:2c5311a4f6fa | 376 | #define ST316BOARD_NR_DIGITS 6 |
wim | 0:2c5311a4f6fa | 377 | //#define ST316BOARD_DIG1_IDX 0 |
wim | 0:2c5311a4f6fa | 378 | #define ST316BOARD_NR_UDC 8 |
wim | 0:2c5311a4f6fa | 379 | |
wim | 0:2c5311a4f6fa | 380 | /** Constructor for class for driving STM STLED316S controller as used in ST316S test display |
wim | 0:2c5311a4f6fa | 381 | * |
wim | 0:2c5311a4f6fa | 382 | * @brief Supports 6 Digits of 8 Segments, 1 Grid of 3 LEDs. Also supports a scanned keyboard of 3 keys. |
wim | 0:2c5311a4f6fa | 383 | * |
wim | 0:2c5311a4f6fa | 384 | * @param PinName mosi, miso, sclk, cs SPI bus pins |
wim | 0:2c5311a4f6fa | 385 | */ |
wim | 0:2c5311a4f6fa | 386 | class STLED316S_BOARD : public STLED316S, public Stream { |
wim | 0:2c5311a4f6fa | 387 | public: |
wim | 0:2c5311a4f6fa | 388 | |
wim | 0:2c5311a4f6fa | 389 | /** Enums for LEDs */ |
wim | 0:2c5311a4f6fa | 390 | // Grid encoded in 8 MSBs, LED pattern encoded in 16 LSBs |
wim | 0:2c5311a4f6fa | 391 | enum Icon { |
wim | 0:2c5311a4f6fa | 392 | LD1 = (0<<24) | STLED316S_LED_L1, |
wim | 0:2c5311a4f6fa | 393 | LD2 = (0<<24) | STLED316S_LED_L2, |
wim | 0:2c5311a4f6fa | 394 | LD3 = (0<<24) | STLED316S_LED_L3, |
wim | 0:2c5311a4f6fa | 395 | }; |
wim | 0:2c5311a4f6fa | 396 | |
wim | 0:2c5311a4f6fa | 397 | typedef short UDCData_t[ST316BOARD_NR_UDC]; |
wim | 0:2c5311a4f6fa | 398 | |
wim | 0:2c5311a4f6fa | 399 | /** Constructor for class for driving STLED316S controller as used in ST316S test display |
wim | 0:2c5311a4f6fa | 400 | * |
wim | 0:2c5311a4f6fa | 401 | * @brief Supports 6 Digits of 8 Segments, 1 Grid of 3 LEDs. Also supports a scanned keyboard of 3 keys. |
wim | 0:2c5311a4f6fa | 402 | * |
wim | 0:2c5311a4f6fa | 403 | * @param PinName mosi, miso, sclk, cs SPI bus pins |
wim | 0:2c5311a4f6fa | 404 | */ |
wim | 0:2c5311a4f6fa | 405 | STLED316S_BOARD(PinName mosi, PinName miso, PinName sclk, PinName cs); |
wim | 0:2c5311a4f6fa | 406 | |
wim | 0:2c5311a4f6fa | 407 | #if DOXYGEN_ONLY |
wim | 0:2c5311a4f6fa | 408 | /** Write a character to the Display |
wim | 0:2c5311a4f6fa | 409 | * |
wim | 0:2c5311a4f6fa | 410 | * @param c The character to write to the display |
wim | 0:2c5311a4f6fa | 411 | */ |
wim | 0:2c5311a4f6fa | 412 | int putc(int c); |
wim | 0:2c5311a4f6fa | 413 | |
wim | 0:2c5311a4f6fa | 414 | /** Write a formatted string to the Display |
wim | 0:2c5311a4f6fa | 415 | * |
wim | 0:2c5311a4f6fa | 416 | * @param format A printf-style format string, followed by the |
wim | 0:2c5311a4f6fa | 417 | * variables to use in formatting the string. |
wim | 0:2c5311a4f6fa | 418 | */ |
wim | 0:2c5311a4f6fa | 419 | int printf(const char* format, ...); |
wim | 0:2c5311a4f6fa | 420 | #endif |
wim | 0:2c5311a4f6fa | 421 | |
wim | 0:2c5311a4f6fa | 422 | /** Locate cursor to a screen column |
wim | 0:2c5311a4f6fa | 423 | * |
wim | 0:2c5311a4f6fa | 424 | * @param column The horizontal position from the left, indexed from 0 |
wim | 0:2c5311a4f6fa | 425 | */ |
wim | 0:2c5311a4f6fa | 426 | void locate(int column); |
wim | 0:2c5311a4f6fa | 427 | |
wim | 0:2c5311a4f6fa | 428 | /** Clear the screen and locate to 0 |
wim | 0:2c5311a4f6fa | 429 | * @param bool clrAll Clear Icons also (default = false) |
wim | 0:2c5311a4f6fa | 430 | */ |
wim | 0:2c5311a4f6fa | 431 | void cls(bool clrAll = false); |
wim | 0:2c5311a4f6fa | 432 | |
wim | 0:2c5311a4f6fa | 433 | /** Set Icon |
wim | 0:2c5311a4f6fa | 434 | * |
wim | 0:2c5311a4f6fa | 435 | * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 16 LSBs |
wim | 0:2c5311a4f6fa | 436 | * @return none |
wim | 0:2c5311a4f6fa | 437 | */ |
wim | 0:2c5311a4f6fa | 438 | void setIcon(Icon icon); |
wim | 0:2c5311a4f6fa | 439 | |
wim | 0:2c5311a4f6fa | 440 | /** Clr Icon |
wim | 0:2c5311a4f6fa | 441 | * |
wim | 0:2c5311a4f6fa | 442 | * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 16 LSBs |
wim | 0:2c5311a4f6fa | 443 | * @return none |
wim | 0:2c5311a4f6fa | 444 | */ |
wim | 0:2c5311a4f6fa | 445 | void clrIcon(Icon icon); |
wim | 0:2c5311a4f6fa | 446 | |
wim | 0:2c5311a4f6fa | 447 | /** Set User Defined Characters (UDC) |
wim | 0:2c5311a4f6fa | 448 | * |
wim | 0:2c5311a4f6fa | 449 | * @param unsigned char udc_idx The Index of the UDC (0..7) |
wim | 0:2c5311a4f6fa | 450 | * @param int udc_data The bitpattern for the UDC (16 bits) |
wim | 0:2c5311a4f6fa | 451 | */ |
wim | 0:2c5311a4f6fa | 452 | void setUDC(unsigned char udc_idx, int udc_data); |
wim | 0:2c5311a4f6fa | 453 | |
wim | 0:2c5311a4f6fa | 454 | |
wim | 0:2c5311a4f6fa | 455 | /** Number of screen columns |
wim | 0:2c5311a4f6fa | 456 | * |
wim | 0:2c5311a4f6fa | 457 | * @param none |
wim | 0:2c5311a4f6fa | 458 | * @return columns |
wim | 0:2c5311a4f6fa | 459 | */ |
wim | 0:2c5311a4f6fa | 460 | int columns(); |
wim | 0:2c5311a4f6fa | 461 | |
wim | 0:2c5311a4f6fa | 462 | /** Write databyte to STLED316S |
wim | 0:2c5311a4f6fa | 463 | * @param int address display memory location to write byte |
wim | 0:2c5311a4f6fa | 464 | * @param char data byte written at given address |
wim | 0:2c5311a4f6fa | 465 | * @return none |
wim | 0:2c5311a4f6fa | 466 | */ |
wim | 0:2c5311a4f6fa | 467 | void writeData(int address, char data){ |
wim | 0:2c5311a4f6fa | 468 | STLED316S::writeData(address, data); |
wim | 0:2c5311a4f6fa | 469 | } |
wim | 0:2c5311a4f6fa | 470 | |
wim | 0:2c5311a4f6fa | 471 | /** Write Display datablock to STLED316S |
wim | 0:2c5311a4f6fa | 472 | * @param DisplayData_t data Array of STLED316S_DISPLAY_MEM (=6) bytes for displaydata (starting at address 0) |
wim | 0:2c5311a4f6fa | 473 | * @param length number bytes to write (valid range 0..(ST316BOARD_NR_GRIDS) (=6), starting at address 0) |
wim | 0:2c5311a4f6fa | 474 | * @return none |
wim | 0:2c5311a4f6fa | 475 | */ |
wim | 0:2c5311a4f6fa | 476 | void writeData(DisplayData_t data, int length = (ST316BOARD_NR_GRIDS)) { |
wim | 0:2c5311a4f6fa | 477 | STLED316S::writeData(data, length); |
wim | 0:2c5311a4f6fa | 478 | } |
wim | 0:2c5311a4f6fa | 479 | |
wim | 0:2c5311a4f6fa | 480 | protected: |
wim | 0:2c5311a4f6fa | 481 | // Stream implementation functions |
wim | 0:2c5311a4f6fa | 482 | virtual int _putc(int value); |
wim | 0:2c5311a4f6fa | 483 | virtual int _getc(); |
wim | 0:2c5311a4f6fa | 484 | |
wim | 0:2c5311a4f6fa | 485 | private: |
wim | 0:2c5311a4f6fa | 486 | int _column; |
wim | 0:2c5311a4f6fa | 487 | int _columns; |
wim | 0:2c5311a4f6fa | 488 | |
wim | 0:2c5311a4f6fa | 489 | DisplayData_t _displaybuffer; |
wim | 0:2c5311a4f6fa | 490 | UDCData_t _UDC_7S; |
wim | 0:2c5311a4f6fa | 491 | }; |
wim | 0:2c5311a4f6fa | 492 | #endif |
wim | 0:2c5311a4f6fa | 493 | |
wim | 0:2c5311a4f6fa | 494 | |
wim | 0:2c5311a4f6fa | 495 | #endif |