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

Dependents:   mbed_SDA5708

Committer:
wim
Date:
Tue Sep 30 17:36:32 2014 +0000
Revision:
2:966fca76e5aa
Parent:
1:d8a07b8468f6
Child:
3:7f65944fee37
Doxygen added

Who changed what in which revision?

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