Library for Siemens SDA5708 8 digit LED matrix display. The control interface is SPI.

Dependents:   mbed_SDA5708

Committer:
wim
Date:
Tue Sep 30 17:13:47 2014 +0000
Revision:
1:d8a07b8468f6
Parent:
0:5265413226e5
Child:
2:966fca76e5aa
Added method setMode() to switch display on/off, added support for User Defined Characters (max. 8 UDCs, occupying character codes 0..7).

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wim 0:5265413226e5 1 /* mbed SDA5708 LED matrix display Library
wim 0:5265413226e5 2 * Copyright (c) 2014, v01: WH, Initial release
wim 0:5265413226e5 3 *
wim 0:5265413226e5 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
wim 0:5265413226e5 5 * of this software and associated documentation files (the "Software"), to deal
wim 0:5265413226e5 6 * in the Software without restriction, including without limitation the rights
wim 0:5265413226e5 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
wim 0:5265413226e5 8 * copies of the Software, and to permit persons to whom the Software is
wim 0:5265413226e5 9 * furnished to do so, subject to the following conditions:
wim 0:5265413226e5 10 *
wim 0:5265413226e5 11 * The above copyright notice and this permission notice shall be included in
wim 0:5265413226e5 12 * all copies or substantial portions of the Software.
wim 0:5265413226e5 13 *
wim 0:5265413226e5 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
wim 0:5265413226e5 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
wim 0:5265413226e5 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
wim 0:5265413226e5 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
wim 0:5265413226e5 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
wim 0:5265413226e5 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
wim 0:5265413226e5 20 * THE SOFTWARE.
wim 0:5265413226e5 21 */
wim 0:5265413226e5 22 #include "mbed.h"
wim 0:5265413226e5 23 #include "SDA5708.h"
wim 0:5265413226e5 24 #include "font_5x7.h"
wim 0:5265413226e5 25
wim 1:d8a07b8468f6 26 // User Defined Characters (UDCs) are defined by a 7 byte bitpattern. The P0..P5 form the character pattern.
wim 1:d8a07b8468f6 27 // P7 P6 P5 P4 P3 P2 P1 P0
wim 1:d8a07b8468f6 28 // 0 B1 B0 x 0 1 1 1 0
wim 1:d8a07b8468f6 29 // 1 B1 B0 x 1 0 0 0 1
wim 1:d8a07b8468f6 30 // . .............
wim 1:d8a07b8468f6 31 // 6 B1 B0 x 1 0 0 0 1
wim 1:d8a07b8468f6 32 //
wim 1:d8a07b8468f6 33
wim 1:d8a07b8468f6 34 /** Some sample User Defined Chars 5x7 dots */
wim 1:d8a07b8468f6 35 const char udc_0[] = {0x18, 0x14, 0x12, 0x11, 0x12, 0x14, 0x18}; // |>
wim 1:d8a07b8468f6 36 const char udc_1[] = {0x03, 0x05, 0x09, 0x11, 0x09, 0x05, 0x03}; // <|
wim 1:d8a07b8468f6 37 const char udc_2[] = {0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10}; // |
wim 1:d8a07b8468f6 38 const char udc_3[] = {0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14}; // ||
wim 1:d8a07b8468f6 39 const char udc_4[] = {0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15}; // |||
wim 1:d8a07b8468f6 40 const char udc_5[] = {0x00, 0x1f, 0x00, 0x1f, 0x00, 0x1f, 0x00}; // =
wim 1:d8a07b8468f6 41 const char udc_6[] = {0x15, 0x0a, 0x15, 0x0a, 0x15, 0x0a, 0x15}; // checkerboard
wim 1:d8a07b8468f6 42 const char udc_7[] = {0x10, 0x08, 0x04, 0x02, 0x01, 0x00, 0x10}; // \
wim 1:d8a07b8468f6 43
wim 1:d8a07b8468f6 44 const char udc_Bat_Hi[] = {0x0E, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F}; // Battery Full
wim 1:d8a07b8468f6 45 const char udc_Bat_Ha[] = {0x0E, 0x11, 0x11, 0x1F, 0x1F, 0x1F, 0x1F}; // Battery Half
wim 1:d8a07b8468f6 46 const char udc_Bat_Lo[] = {0x0E, 0x11, 0x11, 0x11, 0x11, 0x1F, 0x1F}; // Battery Low
wim 1:d8a07b8468f6 47 const char udc_AC[] = {0x0A, 0x0A, 0x1F, 0x11, 0x0E, 0x04, 0x04}; // AC Power
wim 1:d8a07b8468f6 48 const char udc_smiley[] = {0x00, 0x0A, 0x00, 0x04, 0x11, 0x0E, 0x00}; // Smiley
wim 1:d8a07b8468f6 49
wim 1:d8a07b8468f6 50 /** Create an SDA5708 Display object connected to the proper pins
wim 1:d8a07b8468f6 51 *
wim 1:d8a07b8468f6 52 * @param *spi SPI port
wim 1:d8a07b8468f6 53 * @param cs PinName for Chip Select (active low)
wim 1:d8a07b8468f6 54 * @param rst PinName for Reset (active low)
wim 1:d8a07b8468f6 55 */
wim 0:5265413226e5 56 SDA5708::SDA5708(SPI *spi, PinName cs, PinName rst) : _spi(spi), _cs(cs), _rst(rst) {
wim 0:5265413226e5 57 _init();
wim 0:5265413226e5 58 }
wim 0:5265413226e5 59
wim 0:5265413226e5 60 #if(SDA5708_PRINTF != 1)
wim 0:5265413226e5 61 /** Write a character to the LCD
wim 0:5265413226e5 62 *
wim 0:5265413226e5 63 * @param c The character to write to the display
wim 0:5265413226e5 64 */
wim 0:5265413226e5 65 int SDA5708::putc(int c){
wim 0:5265413226e5 66 return _putc(c);
wim 0:5265413226e5 67 }
wim 0:5265413226e5 68
wim 0:5265413226e5 69 /** Write a raw string to the LCD
wim 0:5265413226e5 70 *
wim 0:5265413226e5 71 * @param string text, may be followed by variables to emulate formatting the string.
wim 0:5265413226e5 72 * However, printf formatting is NOT supported and variables will be ignored!
wim 0:5265413226e5 73 */
wim 0:5265413226e5 74 int SDA5708::printf(const char* text, ...) {
wim 0:5265413226e5 75
wim 0:5265413226e5 76 while (*text !=0) {
wim 0:5265413226e5 77 _putc(*text);
wim 0:5265413226e5 78 text++;
wim 0:5265413226e5 79 }
wim 0:5265413226e5 80 return 0;
wim 0:5265413226e5 81 }
wim 0:5265413226e5 82 #else
wim 0:5265413226e5 83 #if DOXYGEN_ONLY
wim 0:5265413226e5 84 /** Write a character to the LCD
wim 0:5265413226e5 85 *
wim 0:5265413226e5 86 * @param c The character to write to the display
wim 0:5265413226e5 87 */
wim 0:5265413226e5 88 int SDA5708::putc(int c){
wim 0:5265413226e5 89 return _putc(c);
wim 0:5265413226e5 90 }
wim 0:5265413226e5 91
wim 0:5265413226e5 92 /** Write a formatted string to the LCD
wim 0:5265413226e5 93 *
wim 0:5265413226e5 94 * @param format A printf-style format string, followed by the
wim 0:5265413226e5 95 * variables to use in formatting the string.
wim 0:5265413226e5 96 */
wim 0:5265413226e5 97 int SDA5708::printf(const char* format, ...){
wim 0:5265413226e5 98 }
wim 0:5265413226e5 99 #endif
wim 0:5265413226e5 100
wim 0:5265413226e5 101 #endif
wim 0:5265413226e5 102
wim 0:5265413226e5 103 /** Clear the screen and locate to 0,0
wim 0:5265413226e5 104 */
wim 0:5265413226e5 105 void SDA5708::cls(){
wim 0:5265413226e5 106 //Clear display
wim 0:5265413226e5 107 _write(SDA5708_CMD_CLR);
wim 0:5265413226e5 108
wim 0:5265413226e5 109 //Set display to normal
wim 0:5265413226e5 110 _write(SDA5708_CMD_NORMAL | _peak | _brightness);
wim 0:5265413226e5 111
wim 0:5265413226e5 112 }
wim 0:5265413226e5 113
wim 0:5265413226e5 114 /** Locate cursor to a screen column and row
wim 0:5265413226e5 115 *
wim 0:5265413226e5 116 * @param column The horizontal position from the left, indexed from 0
wim 0:5265413226e5 117 * @param row The vertical position from the top, indexed from 0
wim 0:5265413226e5 118 */
wim 0:5265413226e5 119 void SDA5708::locate(int column, int row){
wim 0:5265413226e5 120 _column = column % 8;
wim 0:5265413226e5 121 }
wim 0:5265413226e5 122
wim 0:5265413226e5 123 /** Set Brightness
wim 0:5265413226e5 124 *
wim 0:5265413226e5 125 * @param brightness The brightness level (valid range 0..7)
wim 0:5265413226e5 126 */
wim 0:5265413226e5 127 void SDA5708::set_brightness(uint8_t brightness){
wim 0:5265413226e5 128
wim 0:5265413226e5 129 _brightness = brightness & 0x07;
wim 0:5265413226e5 130
wim 0:5265413226e5 131 //Set brightness
wim 0:5265413226e5 132 _write(SDA5708_CMD_NORMAL | _peak | _brightness);
wim 0:5265413226e5 133 }
wim 0:5265413226e5 134
wim 1:d8a07b8468f6 135 /** Set the Displaymode
wim 1:d8a07b8468f6 136 *
wim 1:d8a07b8468f6 137 * @param displayMode The Display mode (DispOff, DispOn)
wim 1:d8a07b8468f6 138 */
wim 1:d8a07b8468f6 139 void SDA5708::setMode(DisplayMode displayMode){
wim 1:d8a07b8468f6 140
wim 1:d8a07b8468f6 141 if (displayMode == DispOff) {
wim 1:d8a07b8468f6 142 //Set brightness to Zero
wim 1:d8a07b8468f6 143 _write(SDA5708_CMD_NORMAL | _peak | SDA5708_BRIGHT_0);
wim 1:d8a07b8468f6 144 }
wim 1:d8a07b8468f6 145 else {
wim 1:d8a07b8468f6 146 //Restore brightness
wim 1:d8a07b8468f6 147 _write(SDA5708_CMD_NORMAL | _peak | _brightness);
wim 1:d8a07b8468f6 148 }
wim 1:d8a07b8468f6 149 }
wim 1:d8a07b8468f6 150
wim 1:d8a07b8468f6 151 /** Set User Defined Characters (UDC)
wim 1:d8a07b8468f6 152 *
wim 1:d8a07b8468f6 153 * @param unsigned char c The Index of the UDC (0..7)
wim 1:d8a07b8468f6 154 * @param char *udc_data The bitpatterns for the UDC (7 bytes of 5 significant bits for bitpattern)
wim 1:d8a07b8468f6 155 */
wim 1:d8a07b8468f6 156 void SDA5708::setUDC(unsigned char c, char *udc_data){
wim 1:d8a07b8468f6 157 char *udc;
wim 1:d8a07b8468f6 158 int idx;
wim 1:d8a07b8468f6 159
wim 1:d8a07b8468f6 160 c = c & 0x07; // mask to valid range
wim 1:d8a07b8468f6 161
wim 1:d8a07b8468f6 162 udc = (char *) _udc[c];
wim 1:d8a07b8468f6 163
wim 1:d8a07b8468f6 164 // Copy UDC data to local UDC memory
wim 1:d8a07b8468f6 165 for (idx=0; idx < 7; idx++) {
wim 1:d8a07b8468f6 166 *udc++ = *udc_data++;
wim 1:d8a07b8468f6 167 }
wim 1:d8a07b8468f6 168 }
wim 1:d8a07b8468f6 169
wim 1:d8a07b8468f6 170
wim 0:5265413226e5 171 /** Low level Reset method for controller
wim 0:5265413226e5 172 */
wim 0:5265413226e5 173 void SDA5708::_reset(){
wim 0:5265413226e5 174
wim 0:5265413226e5 175 // Reset
wim 0:5265413226e5 176 _rst=0;
wim 0:5265413226e5 177 wait_us(500);
wim 0:5265413226e5 178 _rst=1;
wim 0:5265413226e5 179 }
wim 0:5265413226e5 180
wim 0:5265413226e5 181 /** Low level Init method for controller
wim 0:5265413226e5 182 */
wim 0:5265413226e5 183 void SDA5708::_init(){
wim 0:5265413226e5 184
wim 0:5265413226e5 185 // Hard Reset
wim 0:5265413226e5 186 _reset();
wim 0:5265413226e5 187
wim 0:5265413226e5 188 // Init CS
wim 0:5265413226e5 189 _cs = 1;
wim 0:5265413226e5 190
wim 0:5265413226e5 191 // Setup the spi for 8 bit data, high steady state clock,
wim 0:5265413226e5 192 // rising edge capture, with a 500KHz or 1MHz clock rate
wim 0:5265413226e5 193 _spi->format(8,3);
wim 0:5265413226e5 194 _spi->frequency(1000000); // Max SCL is 5 MHz for SDA5708
wim 0:5265413226e5 195
wim 0:5265413226e5 196 // default display brightness
wim 0:5265413226e5 197 _brightness = SDA5708_DEF_BRIGHT;
wim 0:5265413226e5 198 _peak = SDA5708_MAX_PEAK;
wim 0:5265413226e5 199
wim 0:5265413226e5 200 //Clear display
wim 0:5265413226e5 201 _write(SDA5708_CMD_CLR);
wim 0:5265413226e5 202
wim 0:5265413226e5 203 //Set display to normal
wim 0:5265413226e5 204 _write(SDA5708_CMD_NORMAL | _peak | _brightness);
wim 0:5265413226e5 205
wim 0:5265413226e5 206 //Cursor
wim 0:5265413226e5 207 _column=0;
wim 0:5265413226e5 208 }
wim 0:5265413226e5 209
wim 0:5265413226e5 210 /** Low level command byte write operation.
wim 0:5265413226e5 211 */
wim 0:5265413226e5 212 void SDA5708::_write(uint8_t data){
wim 0:5265413226e5 213 int flip;
wim 0:5265413226e5 214
wim 0:5265413226e5 215 // Enable CS
wim 0:5265413226e5 216 _cs = 0;
wim 0:5265413226e5 217 wait_us(1);
wim 0:5265413226e5 218
wim 0:5265413226e5 219 //flip the bits around cause SDA5708 expects LSB first.
wim 0:5265413226e5 220 flip = 0;
wim 0:5265413226e5 221 if (data & 0x01) flip |= 0x80;
wim 0:5265413226e5 222 if (data & 0x02) flip |= 0x40;
wim 0:5265413226e5 223 if (data & 0x04) flip |= 0x20;
wim 0:5265413226e5 224 if (data & 0x08) flip |= 0x10;
wim 0:5265413226e5 225 if (data & 0x10) flip |= 0x08;
wim 0:5265413226e5 226 if (data & 0x20) flip |= 0x04;
wim 0:5265413226e5 227 if (data & 0x40) flip |= 0x02;
wim 0:5265413226e5 228 if (data & 0x80) flip |= 0x01;
wim 0:5265413226e5 229
wim 0:5265413226e5 230 _spi->write(flip);
wim 0:5265413226e5 231 wait_us(1); // Min setup time is 50ns for SDA5708
wim 0:5265413226e5 232
wim 0:5265413226e5 233 // Disable CS
wim 0:5265413226e5 234 _cs = 1;
wim 0:5265413226e5 235
wim 0:5265413226e5 236 }
wim 0:5265413226e5 237
wim 0:5265413226e5 238 // Stream implementation functions
wim 0:5265413226e5 239 int SDA5708::_putc(int value){
wim 0:5265413226e5 240 char *fnt_ptr;
wim 0:5265413226e5 241 int idx;
wim 0:5265413226e5 242
wim 0:5265413226e5 243 //Set Digit address to current cursor position
wim 0:5265413226e5 244 _write(SDA5708_CMD_DIG_ADDR | _column);
wim 0:5265413226e5 245
wim 0:5265413226e5 246 if ((value == '\n') || (value == '\r')) {
wim 0:5265413226e5 247 _column = 0;
wim 0:5265413226e5 248 return 0;
wim 0:5265413226e5 249 }
wim 0:5265413226e5 250 else
wim 0:5265413226e5 251 {
wim 0:5265413226e5 252 // Sanity check
wim 1:d8a07b8468f6 253 if (value < 0x08) {
wim 1:d8a07b8468f6 254 //Pointer to char pattern for UDC
wim 1:d8a07b8468f6 255 fnt_ptr = (char *) _udc[value];
wim 1:d8a07b8468f6 256 }
wim 1:d8a07b8468f6 257 else if ((value < 0x20) || (value > 0x7F)) {
wim 0:5265413226e5 258 //Pointer to char pattern for 'space'
wim 0:5265413226e5 259 fnt_ptr = (char *) font_5x7[0];
wim 0:5265413226e5 260 }
wim 0:5265413226e5 261 else {
wim 0:5265413226e5 262 //Pointer to char pattern
wim 0:5265413226e5 263 fnt_ptr = (char *) font_5x7[value - 0x20];
wim 0:5265413226e5 264 }
wim 0:5265413226e5 265
wim 0:5265413226e5 266 //Write char pattern
wim 0:5265413226e5 267 for (idx=0; idx < 7; idx++) {
wim 0:5265413226e5 268 _write(*fnt_ptr);
wim 0:5265413226e5 269 fnt_ptr++;
wim 0:5265413226e5 270 }
wim 0:5265413226e5 271
wim 0:5265413226e5 272 // Next cursor position
wim 0:5265413226e5 273 _column = (_column + 1) % 8;
wim 0:5265413226e5 274
wim 0:5265413226e5 275 return 0;
wim 0:5265413226e5 276 }
wim 0:5265413226e5 277
wim 0:5265413226e5 278 }
wim 0:5265413226e5 279
wim 0:5265413226e5 280 // Stream implementation functions
wim 0:5265413226e5 281 int SDA5708::_getc(){
wim 0:5265413226e5 282 return -1;
wim 0:5265413226e5 283 }
wim 0:5265413226e5 284