Library for Siemens SDA5708 8 digit LED matrix display. The control interface is SPI.
Revision 0:5265413226e5, committed 2014-09-29
- Comitter:
- wim
- Date:
- Mon Sep 29 19:39:18 2014 +0000
- Child:
- 1:d8a07b8468f6
- Commit message:
- Library for Siemens SDA5708 LED matrix display. The display has 8 digits, each comprising of a 5x7 LED matrix.The control interface is SPI.
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SDA5708.cpp Mon Sep 29 19:39:18 2014 +0000 @@ -0,0 +1,214 @@ +/* mbed SDA5708 LED matrix display Library + * Copyright (c) 2014, v01: WH, Initial release + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +#include "mbed.h" +#include "SDA5708.h" +#include "font_5x7.h" + +SDA5708::SDA5708(SPI *spi, PinName cs, PinName rst) : _spi(spi), _cs(cs), _rst(rst) { + _init(); +} + +#if(SDA5708_PRINTF != 1) +/** Write a character to the LCD + * + * @param c The character to write to the display + */ +int SDA5708::putc(int c){ + return _putc(c); +} + +/** Write a raw string to the LCD + * + * @param string text, may be followed by variables to emulate formatting the string. + * However, printf formatting is NOT supported and variables will be ignored! + */ +int SDA5708::printf(const char* text, ...) { + + while (*text !=0) { + _putc(*text); + text++; + } + return 0; +} +#else +#if DOXYGEN_ONLY +/** Write a character to the LCD + * + * @param c The character to write to the display + */ +int SDA5708::putc(int c){ + return _putc(c); +} + +/** Write a formatted string to the LCD + * + * @param format A printf-style format string, followed by the + * variables to use in formatting the string. + */ +int SDA5708::printf(const char* format, ...){ +} +#endif + +#endif + +/** Clear the screen and locate to 0,0 + */ +void SDA5708::cls(){ + //Clear display + _write(SDA5708_CMD_CLR); + + //Set display to normal + _write(SDA5708_CMD_NORMAL | _peak | _brightness); + +} + +/** Locate cursor to a screen column and row + * + * @param column The horizontal position from the left, indexed from 0 + * @param row The vertical position from the top, indexed from 0 + */ +void SDA5708::locate(int column, int row){ + _column = column % 8; +} + +/** Set Brightness + * + * @param brightness The brightness level (valid range 0..7) + */ +void SDA5708::set_brightness(uint8_t brightness){ + + _brightness = brightness & 0x07; + + //Set brightness + _write(SDA5708_CMD_NORMAL | _peak | _brightness); +} + +/** Low level Reset method for controller + */ +void SDA5708::_reset(){ + + // Reset + _rst=0; + wait_us(500); + _rst=1; +} + +/** Low level Init method for controller + */ +void SDA5708::_init(){ + + // Hard Reset + _reset(); + + // Init CS + _cs = 1; + + // Setup the spi for 8 bit data, high steady state clock, + // rising edge capture, with a 500KHz or 1MHz clock rate + _spi->format(8,3); + _spi->frequency(1000000); // Max SCL is 5 MHz for SDA5708 + + // default display brightness + _brightness = SDA5708_DEF_BRIGHT; + _peak = SDA5708_MAX_PEAK; + + //Clear display + _write(SDA5708_CMD_CLR); + + //Set display to normal + _write(SDA5708_CMD_NORMAL | _peak | _brightness); + + //Cursor + _column=0; +} + +/** Low level command byte write operation. + */ +void SDA5708::_write(uint8_t data){ + int flip; + + // Enable CS + _cs = 0; + wait_us(1); + + //flip the bits around cause SDA5708 expects LSB first. + flip = 0; + if (data & 0x01) flip |= 0x80; + if (data & 0x02) flip |= 0x40; + if (data & 0x04) flip |= 0x20; + if (data & 0x08) flip |= 0x10; + if (data & 0x10) flip |= 0x08; + if (data & 0x20) flip |= 0x04; + if (data & 0x40) flip |= 0x02; + if (data & 0x80) flip |= 0x01; + + _spi->write(flip); + wait_us(1); // Min setup time is 50ns for SDA5708 + + // Disable CS + _cs = 1; + +} + +// Stream implementation functions +int SDA5708::_putc(int value){ + char *fnt_ptr; + int idx; + + //Set Digit address to current cursor position + _write(SDA5708_CMD_DIG_ADDR | _column); + + if ((value == '\n') || (value == '\r')) { + _column = 0; + return 0; + } + else + { + // Sanity check + if ((value < 0x20) || (value > 0x7F)) { + //Pointer to char pattern for 'space' + fnt_ptr = (char *) font_5x7[0]; + } + else { + //Pointer to char pattern + fnt_ptr = (char *) font_5x7[value - 0x20]; + } + + //Write char pattern + for (idx=0; idx < 7; idx++) { + _write(*fnt_ptr); + fnt_ptr++; + } + + // Next cursor position + _column = (_column + 1) % 8; + + return 0; + } + +} + +// Stream implementation functions +int SDA5708::_getc(){ + return -1; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SDA5708.h Mon Sep 29 19:39:18 2014 +0000 @@ -0,0 +1,186 @@ +/* mbed SDA5708 LED matrix display Library + * Copyright (c) 2014, v01: WH, Initial release + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +#ifndef _SDA5708_H +#define _SDA5708_H + +#include "mbed.h" + +//Enable Stream printf or minimalistic printf +#define SDA5708_PRINTF 1 + +// display brightness definitions, indicating percentage brightness +#define SDA5708_BRIGHT_100 0x00 +#define SDA5708_BRIGHT_53 0x01 +#define SDA5708_BRIGHT_40 0x02 +#define SDA5708_BRIGHT_27 0x03 +#define SDA5708_BRIGHT_20 0x04 +#define SDA5708_BRIGHT_13 0x05 +#define SDA5708_BRIGHT_6_6 0x06 +#define SDA5708_BRIGHT_0 0x07 + +#define SDA5708_MAX_PEAK 0x00 +#define SDA5708_12_5_PEAK 0x08 + +// default display brightness +#define SDA5708_DEF_BRIGHT SDA5708_BRIGHT_27 + +//Commands +//Clear display. D2..D0 select brightness. D3 selects max peak current +#define SDA5708_CMD_CLR 0xC0 +//Normal display. D2..D0 select brightness. D3 selects max peak current +#define SDA5708_CMD_NORMAL 0xE0 +//Select Digit Address. D2..D0 select digit. Digit 0 is leftmost, Digit 7 is rightmost. +#define SDA5708_CMD_DIG_ADDR 0xA0 +//Write Digit row data as 8 sequential bytes. +// First byte defines top row. D7..D5 are always 0, D4..D0 define bitpattern. D4 is leftmost, D0 is rightmost pixel. +// + +/** A library for driving SDA5708 LED matrix displays + * + * @code + * #include "mbed.h" + * #include "SDA5708.h" + * + * // SPI Communication + * SPI spi_led(p5, NC, p7); // MOSI, MISO, SCLK + * + * //Display + * SDA5708 led(&spi_led, p8, p9); // SPI bus, CS pin, RST pin + * + * int main() { + * int cnt; + * + * led.locate(0, 0); + * + * // 12345678 + * led.printf("Hi mbed "); + * wait(2); + * + * cnt=0x20; + * while(1) { + * wait(0.5); + * + * led.putc(cnt); + * cnt++; + * if (cnt == 0x80) cnt=0x20; + * } + * } + * @endcode + */ + +/** Create an SDA5708 Display object connected to the proper pins + * + * @param *spi SPI port + * @param cs PinName for Chip Select (active low) + * @param rst PinName for Reset (active low) +*/ +#if(SDA5708_PRINTF == 1) +class SDA5708 : public Stream { +#else +class SDA5708 { +#endif + +public: + /** Create an SDA5708 Display object connected to the proper pins + * + * @param *spi SPI port + * @param cs PinName for Chip Select (active low) + * @param rst PinName for Reset (active low) + */ + SDA5708(SPI *spi, PinName cs, PinName rst); + +#if(SDA5708_PRINTF != 1) + /** Write a character to the LCD + * + * @param c The character to write to the display + */ + int putc(int c); + + /** Write a raw string to the LCD + * + * @param string text, may be followed by variables to emulate formatting the string. + * However, printf formatting is NOT supported and variables will be ignored! + */ + int printf(const char* text, ...); +#else +#if DOXYGEN_ONLY + /** Write a character to the LCD + * + * @param c The character to write to the display + */ + int putc(int c); + + /** Write a formatted string to the LCD + * + * @param format A printf-style format string, followed by the + * variables to use in formatting the string. + */ + int printf(const char* format, ...); +#endif + +#endif + + /** Clear the screen and locate to 0,0 + */ + void cls(); + + /** Locate cursor to a screen column and row + * + * @param column The horizontal position from the left, indexed from 0 + * @param row The vertical position from the top, indexed from 0 + */ + void locate(int column=0, int row=0); + + /** Set Brightness + * + * @param brightness The brightness level (valid range 0..7) + */ + void set_brightness(uint8_t brightness); + +protected: + + /** Low level Reset method for controller + */ + void _reset(); + + /** Low level Init method for LCD controller + */ + void _init(); + + /** Low level command byte write operation. + */ + void _write(uint8_t data); + + // Stream implementation functions + virtual int _putc(int value); + virtual int _getc(); + +// int _row; + int _column; + int _peak, _brightness; + + SPI *_spi; + DigitalOut _cs, _rst; +}; + + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/font_5x7.h Mon Sep 29 19:39:18 2014 +0000 @@ -0,0 +1,124 @@ + +/* mbed SDA5708 LED matrix display Library + * Copyright (c) 2014, v01: Initial release (internet source) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +#ifndef _SDA5708_FONT_5x7_H +#define _SDA5708_FONT_5x7_H + +const char font_5x7[][7] = {{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, //Space $20 + {0x04, 0x04, 0x04, 0x04, 0x04, 0x00, 0x04}, // ! $21 + {0x0A, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00}, // " $22 + {0x0A, 0x0A, 0x3F, 0x0A, 0x3F, 0x0A, 0x0A}, // # $23 + {0x04, 0x0F, 0x14, 0x0E, 0x05, 0x1E, 0x04}, // $ $24 + {0x18, 0x19, 0x02, 0x04, 0x08, 0x13, 0x03}, // % $25 + {0x00, 0x08, 0x14, 0x08, 0x16, 0x12, 0x0D}, // & $26 + {0x04, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00}, // ' $27 + {0x02, 0x04, 0x08, 0x08, 0x08, 0x04, 0x02}, // ( $28 + {0x08, 0x04, 0x02, 0x02, 0x02, 0x04, 0x08}, // ) $29 + {0x04, 0x15, 0x0E, 0x04, 0x0E, 0x15, 0x04}, // * $2A + {0x00, 0x04, 0x04, 0x1F, 0x04, 0x04, 0x00}, // + $2B + {0x00, 0x00, 0x00, 0x00, 0x0C, 0x04, 0x08}, // , $2C + {0x00, 0x00, 0x00, 0x1F, 0x00, 0x00, 0x00}, // - $2D + {0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x0C}, // . $2E + {0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x00}, // / $2F + {0x04, 0x0A, 0x11, 0x11, 0x11, 0x0A, 0x04}, // 0 $30 + {0x04, 0x0C, 0x04, 0x04, 0x04, 0x04, 0x0E}, // 1 $31 + {0x0E, 0x11, 0x02, 0x04, 0x08, 0x10, 0x1F}, // 2 $32 + {0x1F, 0x01, 0x02, 0x06, 0x01, 0x11, 0x0E}, // 3 $33 + {0x02, 0x06, 0x0A, 0x12, 0x1F, 0x02, 0x02}, // 4 $34 + {0x1F, 0x10, 0x10, 0x1E, 0x01, 0x11, 0x0E}, // 5 $35 + {0x0E, 0x10, 0x10, 0x1E, 0x11, 0x11, 0x0E}, // 6 $36 + {0x1F, 0x01, 0x02, 0x04, 0x04, 0x04, 0x04}, // 7 $37 + {0x0E, 0x11, 0x11, 0x0E, 0x11, 0x11, 0x0E}, // 8 $38 + {0x0E, 0x11, 0x11, 0x0F, 0x01, 0x01, 0x1E}, // 9 $39 + {0x00, 0x0C, 0x0C, 0x00, 0x0C, 0x0C, 0x00}, // : $3A + {0x00, 0x0C, 0x0C, 0x00, 0x0C, 0x04, 0x08}, // ; $3B + {0x02, 0x04, 0x08, 0x10, 0x08, 0x04, 0x02}, // < $3C + {0x00, 0x00, 0x1F, 0x00, 0x1F, 0x00, 0x00}, // = $3D + {0x08, 0x04, 0x02, 0x01, 0x02, 0x04, 0x08}, // > $3E + {0x0E, 0x11, 0x02, 0x04, 0x04, 0x00, 0x04}, // ? $3F + {0x0E, 0x11, 0x15, 0x17, 0x10, 0x11, 0x0E}, // @ $40 + {0x04, 0x0A, 0x11, 0x1F, 0x11, 0x11, 0x11}, // A $41 + {0x1E, 0x09, 0x09, 0x0E, 0x09, 0x09, 0x1E}, // B $42 + {0x0E, 0x11, 0x10, 0x10, 0x10, 0x11, 0x0E}, // C $43 + {0x1E, 0x09, 0x09, 0x09, 0x09, 0x09, 0x1E}, // D $44 + {0x1F, 0x10, 0x10, 0x1C, 0x10, 0x10, 0x1F}, // E $45 + {0x1F, 0x10, 0x10, 0x1C, 0x10, 0x10, 0x10}, // F $46 + {0x0F, 0x10, 0x10, 0x10, 0x17, 0x11, 0x0E}, // G $47 + {0x11, 0x11, 0x11, 0x1F, 0x11, 0x11, 0x11}, // H $48 + {0x0E, 0x04, 0x04, 0x04, 0x04, 0x04, 0x0E}, // I $49 + {0x0E, 0x04, 0x04, 0x04, 0x04, 0x14, 0x08}, // J $4A + {0x11, 0x12, 0x14, 0x18, 0x14, 0x12, 0x11}, // K $4B + {0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x1F}, // L $4C + {0x11, 0x1B, 0x15, 0x15, 0x11, 0x11, 0x11}, // M $4D + {0x11, 0x11, 0x19, 0x15, 0x13, 0x11, 0x11}, // N $4E + {0x0E, 0x11, 0x11, 0x11, 0x11, 0x11, 0x0E}, // O $4F + {0x1E, 0x11, 0x11, 0x1E, 0x10, 0x10, 0x10}, // P $50 + {0x0E, 0x11, 0x11, 0x11, 0x15, 0x12, 0x0D}, // Q $51 + {0x1E, 0x11, 0x11, 0x1E, 0x14, 0x12, 0x11}, // R $52 + {0x0E, 0x11, 0x10, 0x0E, 0x01, 0x11, 0x0E}, // S $53 + {0x1F, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04}, // T $54 + {0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x0E}, // U $55 + {0x11, 0x11, 0x11, 0x11, 0x0A, 0x0A, 0x04}, // V $56 + {0x11, 0x11, 0x11, 0x11, 0x15, 0x15, 0x0A}, // W $57 + {0x11, 0x11, 0x0A, 0x04, 0x0A, 0x11, 0x11}, // X $58 + {0x11, 0x11, 0x0A, 0x0A, 0x04, 0x04, 0x04}, // Y $59 + {0x1F, 0x01, 0x02, 0x04, 0x08, 0x10, 0x1F}, // Z $5A + {0x0F, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0F}, // [ $5B + {0x00, 0x10, 0x08, 0x04, 0x02, 0x01, 0x00}, // \ $5C + {0x1E, 0x02, 0x02, 0x02, 0x02, 0x02, 0x1E}, // ] $5D + {0x04, 0x0A, 0x11, 0x00, 0x00, 0x00, 0x00}, // ^ $5E + {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1F}, // _ $5F + {0x04, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00}, // ' $60 + {0x00, 0x0E, 0x01, 0x0D, 0x13, 0x11, 0x0F}, // a $61 + {0x10, 0x10, 0x10, 0x16, 0x19, 0x11, 0x1E}, // b $62 + {0x00, 0x00, 0x0F, 0x10, 0x10, 0x10, 0x0F}, // c $63 + {0x01, 0x01, 0x01, 0x0D, 0x13, 0x11, 0x0F}, // d $64 + {0x00, 0x00, 0x0E, 0x11, 0x1E, 0x10, 0x0E}, // e $65 + {0x06, 0x09, 0x08, 0x1C, 0x08, 0x08, 0x08}, // f $66 + {0x00, 0x0F, 0x11, 0x13, 0x0D, 0x01, 0x0E}, // g $67 + {0x10, 0x10, 0x10, 0x16, 0x19, 0x11, 0x11}, // h $68 + {0x00, 0x04, 0x00, 0x04, 0x04, 0x04, 0x04}, // i $69 + {0x00, 0x02, 0x00, 0x02, 0x02, 0x12, 0x0C}, // j $6A + {0x10, 0x10, 0x11, 0x12, 0x1C, 0x12, 0x11}, // k $6B + {0x0C, 0x04, 0x04, 0x04, 0x04, 0x04, 0x0E}, // l $6C + {0x00, 0x00, 0x1A, 0x15, 0x15, 0x15, 0x15}, // m $6D + {0x00, 0x00, 0x16, 0x19, 0x11, 0x11, 0x11}, // n $6E + {0x00, 0x00, 0x0E, 0x11, 0x11, 0x11, 0x0E}, // o $6F + {0x00, 0x1E, 0x11, 0x19, 0x16, 0x10, 0x10}, // p $70 + {0x00, 0x0F, 0x11, 0x13, 0x0D, 0x01, 0x01}, // q $71 + {0x00, 0x00, 0x16, 0x19, 0x10, 0x10, 0x10}, // r $72 + {0x00, 0x00, 0x0F, 0x10, 0x0E, 0x01, 0x1E}, // s $73 + {0x04, 0x04, 0x0E, 0x04, 0x04, 0x05, 0x02}, // t $74 + {0x00, 0x00, 0x11, 0x11, 0x11, 0x13, 0x0D}, // u $75 + {0x00, 0x00, 0x11, 0x11, 0x11, 0x0A, 0x04}, // v $76 + {0x00, 0x00, 0x11, 0x11, 0x15, 0x15, 0x0A}, // w $77 + {0x00, 0x00, 0x11, 0x0A, 0x04, 0x0A, 0x11}, // x $78 + {0x00, 0x00, 0x11, 0x11, 0x0F, 0x02, 0x0C}, // y $79 + {0x00, 0x00, 0x1F, 0x02, 0x04, 0x08, 0x1F}, // z $7A + {0x03, 0x04, 0x04, 0x08, 0x04, 0x04, 0x03}, // { $7B + {0x04, 0x04, 0x04, 0x00, 0x04, 0x04, 0x04}, // | $7C + {0x18, 0x04, 0x04, 0x02, 0x04, 0x04, 0x18}, // } $7D + {0x08, 0x15, 0x02, 0x00, 0x00, 0x00, 0x00}, // ~ $7E + {0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F} // square $7F + }; + +#endif