Library for TM1640 LED controller Initial version
See here for more information.
Revision 0:7e120ffa56d3, committed 2016-01-28
- Comitter:
- wim
- Date:
- Thu Jan 28 19:25:49 2016 +0000
- Commit message:
- Library for TM1640 LED controller; Initial version
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Font_7Seg.cpp Thu Jan 28 19:25:49 2016 +0000
@@ -0,0 +1,177 @@
+/* mbed LED Font Library, for TM1640 LED controller
+ * Copyright (c) 2016, v01: WH, Initial version, Test in LM1640
+ *
+ * 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 "Font_7Seg.h"
+
+// Select one of the testboards for TM1640 LED controller
+#if (LM1640_TEST == 1)
+
+//Mask for blending out and restoring Icons
+const char MASK_ICON_GRID[] = {
+ LO(S7_ICON_GR1),
+ LO(S7_ICON_GR2),
+ LO(S7_ICON_GR3),
+ LO(S7_ICON_GR4),
+ LO(S7_ICON_GR5),
+ LO(S7_ICON_GR6),
+ LO(S7_ICON_GR7),
+ LO(S7_ICON_GR8),
+
+ LO(S7_ICON_GR9),
+ LO(S7_ICON_GR10),
+ LO(S7_ICON_GR11),
+ LO(S7_ICON_GR12),
+ LO(S7_ICON_GR13),
+ LO(S7_ICON_GR14),
+ LO(S7_ICON_GR15),
+ LO(S7_ICON_GR16)
+ };
+
+// ASCII Font definition table for transmission to TM1640
+//
+//#define FONT_7S_START 0x20
+//#define FONT_7S_END 0x7F
+//#define FONT_7S_NR_CHARS (FONT_7_END - FONT_7S_START + 1)
+
+#if (SHOW_ASCII == 1)
+//display all ASCII characters
+const short FONT_7S[] = {
+ C7_SPC, //32 0x20, Space
+ C7_EXC,
+ C7_QTE,
+ C7_HSH,
+ C7_DLR,
+ C7_PCT,
+ C7_AMP,
+ C7_ACC,
+ C7_LBR,
+ C7_RBR,
+ C7_MLT,
+ C7_PLS,
+ C7_CMA,
+ C7_MIN,
+ C7_DPT,
+ C7_RS,
+ C7_0, //48 0x30
+ C7_1,
+ C7_2,
+ C7_3,
+ C7_4,
+ C7_5,
+ C7_6,
+ C7_7,
+ C7_8,
+ C7_9,
+ C7_COL, //58 0x3A
+ C7_SCL,
+ C7_LT,
+ C7_EQ,
+ C7_GT,
+ C7_QM,
+ C7_AT, //64 0x40
+ C7_A, //65 0x41, A
+ C7_B,
+ C7_C,
+ C7_D,
+ C7_E,
+ C7_F,
+ C7_G,
+ C7_H,
+ C7_I,
+ C7_J,
+ C7_K,
+ C7_L,
+ C7_M,
+ C7_N,
+ C7_O,
+ C7_P,
+ C7_Q,
+ C7_R,
+ C7_S,
+ C7_T,
+ C7_U,
+ C7_V,
+ C7_W,
+ C7_X,
+ C7_Y,
+ C7_Z, //90 0x5A, Z
+ C7_SBL, //91 0x5B
+ C7_LS,
+ C7_SBR,
+ C7_PWR,
+ C7_UDS,
+ C7_ACC,
+ C7_A, //97 0x61, A replacing a
+ C7_B,
+ C7_C,
+ C7_D,
+ C7_E,
+ C7_F,
+ C7_G,
+ C7_H,
+ C7_I,
+ C7_J,
+ C7_K,
+ C7_L,
+ C7_M,
+ C7_N,
+ C7_O,
+ C7_P,
+ C7_Q,
+ C7_R,
+ C7_S,
+ C7_T,
+ C7_U,
+ C7_V,
+ C7_W,
+ C7_X,
+ C7_Y,
+ C7_Z, // 122 0x7A, Z replacing z
+ C7_CBL, // 123 0x7B
+ C7_OR,
+ C7_CBR,
+ C7_TLD,
+ C7_DEL // 127
+ };
+
+#else
+//display only digits and hex characters
+const short FONT_7S[] = {
+ C7_0, //48 0x30
+ C7_1,
+ C7_2,
+ C7_3,
+ C7_4,
+ C7_5,
+ C7_6,
+ C7_7,
+ C7_8,
+ C7_9,
+ C7_A, //65 0x41, A
+ C7_B,
+ C7_C,
+ C7_D,
+ C7_E,
+ C7_F
+ };// 127
+#endif
+
+#endif
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Font_7Seg.h Thu Jan 28 19:25:49 2016 +0000
@@ -0,0 +1,276 @@
+/* mbed LED Font Library, for TM1640 LED Controller
+ * Copyright (c) 2016, v01: WH, Initial version, Test in LM1640
+ *
+ * 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 MBED_FONT_7SEG_H
+#define MBED_FONT_7SEG_H
+
+// Select one of the testboards for TM1640 controller
+#include "TM1640_Config.h"
+
+#if ((LM1640_TEST == 1) || (TM1640_TEST == 1))
+// Segment bit positions for 7 Segment display using the LM1640 mapping for TM1640
+// Modify this table for different 'bit-to-segment' mappings. The ASCII character defines and the FONT_7S const table below
+// will be adapted automatically according to the bit-to-segment mapping. Obviously this will only work when the segment
+// mapping is identical for every digit position. This will be the case unless the hardware designer really hates software developers.
+//
+// A
+// -----
+// | |
+// F | | B
+// | G |
+// -----
+// | |
+// E | | C
+// | |
+// ----- * DP
+// D
+//
+#define S7_A 0x0001
+#define S7_B 0x0002
+#define S7_C 0x0004
+#define S7_D 0x0008
+#define S7_E 0x0010
+#define S7_F 0x0020
+#define S7_G 0x0040
+#define S7_DP 0x0080
+
+//Mask for blending out and setting 7 segments digits
+#define MASK_7S_ALL = (S7_A | S7_B | S7_C | S7_D | S7_E | S7_F | S7_G}
+
+//Icons Grid 1
+#define S7_DP1 0x0080
+#define S7_ICON_GR1 (0x0000)
+//#define S7_ICON_GR1 (S7_DP1)
+
+//Icons Grid 2
+#define S7_DP2 0x0080
+#define S7_ICON_GR2 (0x0000)
+//#define S7_ICON_GR2 (S7_DP2)
+
+//Icons Grid 3
+#define S7_DP3 0x0080
+#define S7_ICON_GR3 (0x0000)
+//#define S7_ICON_GR3 (S7_LD3 | S7_DP3)
+
+//Icons Grid 4
+#define S7_DP4 0x0080
+#define S7_ICON_GR4 (0x0000)
+//#define S7_ICON_GR4 (S7_DP4)
+
+//Icons Grid 5
+#define S7_DP5 0x0080
+#define S7_ICON_GR5 (0x0000)
+//#define S7_ICON_GR5 (S7_DP5)
+
+//Icons Grid 6
+#define S7_DP6 0x0080
+#define S7_ICON_GR6 (0x0000)
+//#define S7_ICON_GR6 (S7_DP6)
+
+//Icons Grid 7
+#define S7_DP7 0x0080
+#define S7_ICON_GR7 (0x0000)
+//#define S7_ICON_GR7 (S7_DP7)
+
+//Icons Grid 8
+#define S7_DP8 0x0080
+#define S7_ICON_GR8 (0x0000)
+//#define S7_ICON_GR8 (S7_DP8)
+
+//Icons Grid 9
+#define S7_DP9 0x0080
+#define S7_ICON_GR9 (0x0000)
+//#define S7_ICON_GR9 (S7_DP9)
+
+//Icons Grid 10
+#define S7_DP10 0x0080
+#define S7_ICON_GR10 (0x0000)
+//#define S7_ICON_GR10 (S7_DP10)
+
+//Icons Grid 11
+#define S7_DP11 0x0080
+#define S7_ICON_GR11 (0x0000)
+//#define S7_ICON_GR11 (S7_DP11)
+
+//Icons Grid 12
+#define S7_DP12 0x0080
+#define S7_ICON_GR12 (0x0000)
+//#define S7_ICON_GR12 (S7_DP12)
+
+//Icons Grid 13
+#define S7_DP13 0x0080
+#define S7_ICON_GR13 (0x0000)
+//#define S7_ICON_GR13 (S7_DP13)
+
+//Icons Grid 14
+#define S7_DP14 0x0080
+#define S7_ICON_GR14 (0x0000)
+//#define S7_ICON_GR14 (S7_DP14)
+
+//Icons Grid 15
+#define S7_DP15 0x0080
+#define S7_ICON_GR15 (0x0000)
+//#define S7_ICON_GR15 (S7_DP15)
+
+//Icons Grid 16
+#define S7_DP16 0x0080
+#define S7_ICON_GR16 (0x0000)
+//#define S7_ICON_GR16 (S7_DP16)
+
+//Mask for blending out and restoring Icons
+extern const char MASK_ICON_GRID[];
+#endif
+
+
+// ASCII Font definitions for segments in each character
+//
+//32 0x20 Symbols
+#define C7_SPC (0x0000)
+#define C7_EXC (S7_B | S7_C) //!
+#define C7_QTE (S7_B | S7_F) //"
+#define C7_HSH (S7_C | S7_D | S7_E | S7_G) //#
+#define C7_DLR (S7_A | S7_C | S7_D | S7_F | S7_G) //$
+#define C7_PCT (S7_C | S7_F) //%
+#define C7_AMP (S7_A | S7_C | S7_D | S7_E | S7_F | S7_G) //&
+#define C7_ACC (S7_B) //'
+#define C7_LBR (S7_A | S7_D | S7_E | S7_F) //(
+#define C7_RBR (S7_A | S7_B | S7_C | S7_D) //)
+#define C7_MLT (S7_B | S7_C | S7_E | S7_F | S7_G) //*
+#define C7_PLS (S7_B | S7_C | S7_G) //+
+#define C7_CMA (S7_DP)
+#define C7_MIN (S7_G)
+#define C7_DPT (S7_DP)
+#define C7_RS (S7_B | S7_E | S7_G) // /
+
+//48 0x30 Digits
+#define C7_0 (S7_A | S7_B | S7_C | S7_D | S7_E | S7_F)
+#define C7_1 (S7_B | S7_C)
+#define C7_2 (S7_A | S7_B | S7_D | S7_E | S7_G)
+#define C7_3 (S7_A | S7_B | S7_C | S7_D | S7_G)
+#define C7_4 (S7_B | S7_C | S7_F | S7_G)
+#define C7_5 (S7_A | S7_C | S7_D | S7_F | S7_G)
+#define C7_6 (S7_A | S7_C | S7_D | S7_E | S7_F | S7_G)
+#define C7_7 (S7_A | S7_B | S7_C)
+#define C7_8 (S7_A | S7_B | S7_C | S7_D | S7_E | S7_F | S7_G)
+#define C7_9 (S7_A | S7_B | S7_C | S7_D | S7_F | S7_G)
+
+//58 0x3A
+#define C7_COL (S7_D | S7_G) // :
+#define C7_SCL (S7_D | S7_G) // ;
+#define C7_LT (S7_D | S7_E | S7_G) // <
+#define C7_EQ (S7_D | S7_G) // =
+#define C7_GT (S7_C | S7_D | S7_G) // >
+#define C7_QM (S7_A | S7_B | S7_E | S7_G) // ?
+#define C7_AT (S7_A | S7_B | S7_C | S7_D | S7_E | S7_G) // @
+
+//65 0x41 Upper case alphabet
+#define C7_A (S7_A | S7_B | S7_C | S7_E | S7_F | S7_G )
+#define C7_B (S7_C | S7_D | S7_E | S7_F | S7_G)
+#define C7_C (S7_A | S7_D | S7_E | S7_F)
+#define C7_D (S7_B | S7_C | S7_D | S7_E | S7_G)
+#define C7_E (S7_A | S7_D | S7_E | S7_F | S7_G)
+#define C7_F (S7_A | S7_E | S7_F | S7_G)
+
+#define C7_G (S7_A | S7_C | S7_D | S7_E | S7_F)
+#define C7_H (S7_B | S7_C | S7_E | S7_F | S7_G)
+#define C7_I (S7_B | S7_C)
+#define C7_J (S7_B | S7_C | S7_D | S7_E)
+#define C7_K (S7_B | S7_C | S7_E | S7_F | S7_G)
+#define C7_L (S7_D | S7_E | S7_F)
+#define C7_M (S7_A | S7_C | S7_E)
+#define C7_N (S7_A | S7_B | S7_C | S7_E | S7_F)
+#define C7_O (S7_A | S7_B | S7_C | S7_D | S7_E | S7_F)
+#define C7_P (S7_A | S7_B | S7_E | S7_F | S7_G)
+#define C7_Q (S7_A | S7_B | S7_C | S7_F | S7_G)
+#define C7_R (S7_E | S7_G )
+#define C7_S (S7_A | S7_C | S7_D | S7_F | S7_G)
+#define C7_T (S7_D | S7_E | S7_F | S7_G)
+#define C7_U (S7_B | S7_C | S7_D | S7_E | S7_F)
+#define C7_V (S7_B | S7_C | S7_D | S7_E | S7_F)
+#define C7_W (S7_B | S7_D | S7_F)
+#define C7_X (S7_B | S7_C | S7_E | S7_F | S7_G)
+#define C7_Y (S7_B | S7_C | S7_D | S7_F | S7_G)
+#define C7_Z (S7_A | S7_B | S7_D | S7_E | S7_G)
+
+//91 0x5B
+#define C7_SBL (S7_A | S7_D | S7_E | S7_F) // [
+#define C7_LS (S7_C | S7_F | S7_G) // left slash
+#define C7_SBR (S7_A | S7_B | S7_C | S7_D) // ]
+#define C7_PWR (S7_A | S7_B | S7_F) // ^
+#define C7_UDS (S7_D) // _
+#define C7_DSH (S7_F) // `
+
+//97 0x61 Lower case alphabet
+#define C7_a C7_A
+#define C7_b C7_B
+#define C7_c C7_C
+#define C7_d C7_D
+#define C7_e C7_E
+#define C7_f C7_H
+
+#define C7_g C7_G
+#define C7_h C7_H
+#define C7_i C7_I
+#define C7_j C7_J
+#define C7_k C7_K
+#define C7_l C7_L
+#define C7_m C7_M
+//#define C7_n C7_N
+#define C7_n (S7_C | S7_E | S7_G)
+//#define C7_o C7_O
+#define C7_o (S7_C | S7_D | S7_E | S7_G)
+#define C7_p C7_P
+#define C7_q C7_Q
+//#define C7_r C7_R
+#define C7_r (S7_E | S7_G)
+#define C7_s C7_S
+#define C7_t C7_T
+#define C7_u C7_U
+#define C7_v C7_V
+#define C7_w C7_W
+#define C7_x C7_X
+#define C7_y C7_Y
+#define C7_z C7_Z
+
+//123 0x7B
+#define C7_CBL (S7_A | S7_D | S7_E | S7_F) // {
+#define C7_OR (S7_B | S7_C) // |
+#define C7_CBR (S7_A | S7_B | S7_C | S7_D) // }
+#define C7_TLD (S7_B | S7_E | S7_G ) // ~
+#define C7_DEL (0x0000)
+
+
+//User Defined Characters (some examples)
+#define C7_DGR (S7_A | S7_B | S7_F | S7_G) //Degrees
+
+// Font data selection for transmission to TM1640 memory
+#define LO(x) ( x & 0xFF)
+#define HI(x) ((x >> 8) & 0xFF)
+
+
+// ASCII Font definition table
+//
+#define FONT_7S_START 0x20
+#define FONT_7S_END 0x7F
+//#define FONT_7S_NR_CHARS (FONT_7S_END - FONT_7S_START + 1)
+extern const short FONT_7S[];
+
+#endif
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/TM1640.cpp Thu Jan 28 19:25:49 2016 +0000
@@ -0,0 +1,436 @@
+/* mbed TM1640 Library, for TM1640 LED controller
+ * Copyright (c) 2016, v01: WH, Initial version
+ *
+ * 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, inclumosig 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, INCLUmosiG 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 "TM1640.h"
+
+/** Constructor for class for driving TM1640 LED controller with Serial bus interface device.
+ * @brief Supports 16 digits @ 8 segments.
+ *
+ * @param PinName mosi Serial bus MOSI pin
+ * @param PinName sclk Serial bus SCLK pin
+*/
+TM1640::TM1640(PinName mosi, PinName sclk) : _mosi(mosi), _sclk(sclk) {
+
+ _init();
+}
+
+/** Init the SPI interface and the controller
+ * @param none
+ * @return none
+ */
+void TM1640::_init(){
+
+//TM1640 uses Serial bus that looks like SPI, but uses Start and Stop conditions like I2C instead of CS
+//init Serial bus
+ _mosi=1;
+ _sclk=1;
+
+//init controller
+ _display = TM1640_DSP_ON;
+ _bright = TM1640_BRT_DEF;
+ _writeCmd(TM1640_DSP_CTRL_CMD, _display | _bright ); // Display control cmd, display on/off, brightness
+
+ _writeCmd(TM1640_DATA_SET_CMD, TM1640_DATA_WR | TM1640_ADDR_INC | TM1640_MODE_NORM); // Data set cmd, normal mode, auto incr, write data
+}
+
+
+/** Clear the screen and locate to 0
+ */
+void TM1640::cls() {
+
+ _start();
+
+ _write(TM1640_ADDR_SET_CMD | 0x00); // Address set cmd, 0
+ for (int cnt=0; cnt<TM1640_DISPLAY_MEM; cnt++) {
+ _write(0x00); // data
+ }
+
+ _stop();
+}
+
+/** Set Brightness
+ *
+ * @param char brightness (3 significant bits, valid range 0..7 (1/16 .. 14/14 dutycycle)
+ * @return none
+ */
+void TM1640::setBrightness(char brightness){
+
+ _bright = brightness & TM1640_BRT_MSK; // mask invalid bits
+
+ _writeCmd(TM1640_DSP_CTRL_CMD, _display | _bright ); // Display control cmd, display on/off, brightness
+}
+
+/** Set the Display mode On/off
+ *
+ * @param bool display mode
+ */
+void TM1640::setDisplay(bool on) {
+
+ if (on) {
+ _display = TM1640_DSP_ON;
+ }
+ else {
+ _display = TM1640_DSP_OFF;
+ }
+
+ _writeCmd(TM1640_DSP_CTRL_CMD, _display | _bright ); // Display control cmd, display on/off, brightness
+}
+
+/** Write databyte to TM1640
+ * @param int address display memory location to write byte
+ * @param char data byte written at given address
+ * @return none
+ */
+void TM1640::writeData(char data, int address) {
+
+ _start();
+
+ _write(TM1640_ADDR_SET_CMD | (address & TM1640_ADDR_MSK)); // Set Address cmd
+ _write(data); // data
+
+ _stop();
+}
+
+/** Write Display datablock to TM1640
+ * @param DisplayData_t data Array of TM1640_DISPLAY_MEM (=16) bytes for displaydata
+ * @param length number bytes to write (valid range 0..(TM1640_MAX_NR_GRIDS * TM1640_BYTES_PER_GRID) (=16), when starting at address 0)
+ * @param int address display memory location to write bytes (default = 0)
+ * @return none
+ */
+void TM1640::writeData(DisplayData_t data, int length, int address) {
+
+ _start();
+
+// sanity check
+ address &= TM1640_ADDR_MSK;
+ if (length < 0) {length = 0;}
+ if ((length + address) > TM1640_DISPLAY_MEM) {length = (TM1640_DISPLAY_MEM - address);}
+
+// _write(TM1640_ADDR_SET_CMD | 0x00); // Set Address at 0
+ _write(TM1640_ADDR_SET_CMD | address); // Set Address
+
+ for (int idx=0; idx<length; idx++) {
+// _write(data[idx]); // data
+ _write(data[address + idx]); // data
+ }
+
+ _stop();
+}
+
+
+/** Generate Start condition for TM1640
+ * @param none
+ * @return none
+ */
+void TM1640::_start() {
+
+ _mosi=0;
+ wait_us(1);
+ _sclk=0;
+ wait_us(1);
+}
+
+/** Generate Stop condition for TM1640
+ * @param none
+ * @return none
+ */
+void TM1640::_stop() {
+
+ _mosi=0;
+ wait_us(1);
+ _sclk=1;
+ wait_us(1);
+ _mosi=1;
+ wait_us(1);
+}
+
+/** Send byte to TM1640
+ * @param int data
+ * @return none
+ */
+void TM1640::_write(int data) {
+
+ for (int bit=0; bit<8; bit++) {
+ //The TM1640 expects LSB first, whereas SPI is MSB first
+ if (((data >> bit) & 0x01) == 0x01) {
+ _mosi=1;
+ }
+ else {
+ _mosi=0;
+ }
+ wait_us(1);
+ _sclk=1;
+ wait_us(1);
+ _sclk=0;
+ wait_us(1);
+ }
+}
+
+
+/** Write command and parameter to TM1640
+ * @param int cmd Command byte
+ * &Param int data Parameters for command
+ * @return none
+ */
+void TM1640::_writeCmd(int cmd, int data){
+
+ _start();
+
+ _write((cmd & TM1640_CMD_MSK) | (data & ~TM1640_CMD_MSK));
+
+ _stop();
+}
+
+
+#if (LM1640_TEST == 1)
+// Derived class for TM1640 used in LED&KEY display unit
+//
+
+/** Constructor for class for driving TM1640 LED controller as used in LM1640
+ *
+ * @brief Supports 8 Digits of 7 Segments + DP.
+ *
+ * @param PinName mosi Serial bus MOSI pin
+ * @param PinName sclk Serial bus SCLK pin
+ */
+TM1640_LM1640::TM1640_LM1640(PinName mosi, PinName sclk) : TM1640(mosi, sclk) {
+ _column = 0;
+ _columns = LM1640_NR_DIGITS;
+}
+
+#if(0)
+#if DOXYGEN_ONLY
+ /** Write a character to the Display
+ *
+ * @param c The character to write to the display
+ */
+ int putc(int c);
+
+ /** Write a formatted string to the Display
+ *
+ * @param format A printf-style format string, followed by the
+ * variables to use in formatting the string.
+ */
+ int printf(const char* format, ...);
+#endif
+#endif
+
+/** Locate cursor to a screen column
+ *
+ * @param column The horizontal position from the left, indexed from 0
+ */
+void TM1640_LM1640::locate(int column) {
+ //sanity check
+ if (column < 0) {column = 0;}
+ if (column > (_columns - 1)) {column = _columns - 1;}
+
+ _column = column;
+}
+
+
+/** Number of screen columns
+ *
+ * @param none
+ * @return columns
+ */
+int TM1640_LM1640::columns() {
+ return _columns;
+}
+
+
+/** Clear the screen and locate to 0
+ * @param bool clrAll Clear Icons also (default = false)
+ */
+void TM1640_LM1640::cls(bool clrAll) {
+
+ if (clrAll) {
+ //clear local buffer (inclumosig Icons)
+ for (int idx=0; idx < LM1640_NR_GRIDS; idx++) {
+ _displaybuffer[idx] = 0x00;
+ }
+ }
+ else {
+ //clear local buffer (preserving Icons)
+ for (int idx=0; idx < LM1640_NR_GRIDS; idx++) {
+ _displaybuffer[idx] = _displaybuffer[idx] & MASK_ICON_GRID[idx];
+ }
+ }
+
+ writeData(_displaybuffer, (LM1640_NR_GRIDS * TM1640_BYTES_PER_GRID));
+
+ _column = 0;
+}
+
+/** Set Icon
+ *
+ * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 16 LSBs
+ * @return none
+ */
+void TM1640_LM1640::setIcon(Icon icon) {
+ int addr, icn;
+
+ icn = icon & 0xFFFF;
+ addr = (icon >> 24) & 0xFF;
+ addr = (addr - 1);
+
+ //Save char...and set bits for icon to write
+ _displaybuffer[addr] = _displaybuffer[addr] | LO(icn);
+// writeData(_displaybuffer, (LM1640_NR_GRIDS * TM1640_BYTES_PER_GRID));
+ writeData(_displaybuffer, TM1640_BYTES_PER_GRID, addr);
+}
+
+/** Clr Icon
+ *
+ * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 16 LSBs
+ * @return none
+ */
+void TM1640_LM1640::clrIcon(Icon icon) {
+ int addr, icn;
+
+ icn = icon & 0xFFFF;
+ addr = (icon >> 24) & 0xFF;
+ addr = (addr - 1);
+
+ //Save char...and clr bits for icon to write
+ _displaybuffer[addr] = _displaybuffer[addr] & ~LO(icn);
+// writeData(_displaybuffer, (LM1640_NR_GRIDS * TM1640_BYTES_PER_GRID));
+ writeData(_displaybuffer, TM1640_BYTES_PER_GRID, addr);
+}
+
+
+/** Set User Defined Characters (UDC)
+ *
+ * @param unsigned char udc_idx The Index of the UDC (0..7)
+ * @param int udc_data The bitpattern for the UDC (8 bits)
+ */
+void TM1640_LM1640::setUDC(unsigned char udc_idx, int udc_data) {
+
+ //Sanity check
+ if (udc_idx > (LM1640_NR_UDC-1)) {
+ return;
+ }
+ // Mask out Icon bits?
+
+ _UDC_7S[udc_idx] = LO(udc_data);
+}
+
+
+/** Write a single character (Stream implementation)
+ */
+int TM1640_LM1640::_putc(int value) {
+ int addr;
+ bool validChar = false;
+ char pattern = 0x00;
+
+ if ((value == '\n') || (value == '\r')) {
+ //No character to write
+ validChar = false;
+
+ //Update Cursor
+ _column = 0;
+ }
+ else if ((value == '.') || (value == ',')) {
+ //No character to write
+ validChar = false;
+ pattern = S7_DP; // placeholder for all DPs
+
+ // Check to see that DP can be shown for current column
+ if (_column > 0) {
+ //Translate between _column and displaybuffer entries
+ //Add DP to bitpattern of digit left of current column.
+ addr = (_column - 1);
+
+ //Save icons...and set bits for decimal point to write
+ _displaybuffer[addr] = _displaybuffer[addr] | pattern;
+// writeData(_displaybuffer, (LM1640_NR_GRIDS * TM1640_BYTES_PER_GRID));
+ writeData(_displaybuffer, TM1640_BYTES_PER_GRID, addr);
+
+ //No Cursor Update
+ }
+ }
+ else if ((value >= 0) && (value < LM1640_NR_UDC)) {
+ //Character to write
+ validChar = true;
+ pattern = _UDC_7S[value];
+ }
+
+#if (SHOW_ASCII == 1)
+ //display all ASCII characters
+ else if ((value >= FONT_7S_START) && (value <= FONT_7S_END)) {
+ //Character to write
+ validChar = true;
+ pattern = FONT_7S[value - FONT_7S_START];
+ } // else
+#else
+ //display only digits and hex characters
+ else if (value == '-') {
+ //Character to write
+ validChar = true;
+ pattern = C7_MIN;
+ }
+ else if ((value >= (int)'0') && (value <= (int) '9')) {
+ //Character to write
+ validChar = true;
+ pattern = FONT_7S[value - (int) '0'];
+ }
+ else if ((value >= (int) 'A') && (value <= (int) 'F')) {
+ //Character to write
+ validChar = true;
+ pattern = FONT_7S[10 + value - (int) 'A'];
+ }
+ else if ((value >= (int) 'a') && (value <= (int) 'f')) {
+ //Character to write
+ validChar = true;
+ pattern = FONT_7S[10 + value - (int) 'a'];
+ } //else
+#endif
+
+ if (validChar) {
+ //Character to write
+
+ //Translate between _column and displaybuffer entries
+ addr = _column;
+
+ //Save icons...and set bits for character to write
+ _displaybuffer[addr] = (_displaybuffer[addr] & MASK_ICON_GRID[_column]) | pattern;
+
+// writeData(_displaybuffer, (LM1640_NR_GRIDS * TM1640_BYTES_PER_GRID));
+ writeData(_displaybuffer, TM1640_BYTES_PER_GRID, addr);
+
+ //Update Cursor
+ _column++;
+ if (_column > (LM1640_NR_DIGITS - 1)) {
+ _column = 0;
+ }
+
+ } // if validChar
+
+ return value;
+}
+
+
+// get a single character (Stream implementation)
+int TM1640_LM1640::_getc() {
+ return -1;
+}
+
+#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/TM1640.h Thu Jan 28 19:25:49 2016 +0000
@@ -0,0 +1,334 @@
+/* mbed TM1640 Library, for TM1640 LED controller
+ * Copyright (c) 2016, v01: WH, Initial version
+ *
+ * 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, inclumosig 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, INCLUmosiG 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 TM1640_H
+#define TM1640_H
+
+// Select one of the testboards for TM1640 LED controller
+#include "TM1640_Config.h"
+
+/** An interface for driving TM1640 LED controller
+ *
+ * @code
+ * #include "mbed.h"
+ * #include "TM1640.h"
+ *
+ * DisplayData_t size is 16 bytes (16 grids @ 8 segments)
+ * TM1640::DisplayData_t all_str = {0xFF,0xFF, 0xFF,0xFF, 0xFF,0xFF, 0xFF,0xFF, 0xFF,0xFF, 0xFF,0xFF, 0xFF,0xFF, 0xFF,0xFF};
+ *
+ * // TM1640 declaration
+ * TM1640 TM1640(p5,p7);
+ *
+ * int main() {
+ * TM1640.cls();
+ * TM1640.writeData(all_str);
+ * wait(1);
+ * TM1640.setBrightness(TM1640_BRT0);
+ * wait(1);
+ * TM1640.setBrightness(TM1640_BRT3);
+ *
+ * while (1) {
+ * TM1640.cls();
+ * wait(0.5);
+ * TM1640.writeData(all_str);
+ * wait(0.5);
+ * }
+ * }
+ * @endcode
+ */
+
+
+//TM1640 Display data
+#define TM1640_MAX_NR_GRIDS 16
+#define TM1640_BYTES_PER_GRID 1
+
+//Memory size in bytes for Display
+#define TM1640_DISPLAY_MEM (TM1640_MAX_NR_GRIDS * TM1640_BYTES_PER_GRID)
+
+//Reserved bits for commands
+#define TM1640_CMD_MSK 0xC0
+
+//Data setting commands
+#define TM1640_DATA_SET_CMD 0x40
+#define TM1640_DATA_WR 0x00
+#define TM1640_ADDR_INC 0x00
+#define TM1640_ADDR_FIXED 0x04
+#define TM1640_MODE_NORM 0x00
+#define TM1640_MODE_TEST 0x08
+
+//Address setting commands
+#define TM1640_ADDR_SET_CMD 0xC0
+#define TM1640_ADDR_MSK 0x0F
+
+//Display control commands
+#define TM1640_DSP_CTRL_CMD 0x80
+#define TM1640_BRT_MSK 0x07
+#define TM1640_BRT0 0x00 //Pulsewidth 1/16
+#define TM1640_BRT1 0x01
+#define TM1640_BRT2 0x02
+#define TM1640_BRT3 0x03
+#define TM1640_BRT4 0x04
+#define TM1640_BRT5 0x05
+#define TM1640_BRT6 0x06
+#define TM1640_BRT7 0x07 //Pulsewidth 14/16
+
+#define TM1640_BRT_DEF TM1640_BRT3
+
+#define TM1640_DSP_OFF 0x00
+#define TM1640_DSP_ON 0x08
+
+
+/** A class for driving TM1640 LED controller
+ *
+ * @brief Supports 16 Grids @ 8 Segments.
+ * Serial bus interface device.
+ */
+class TM1640 {
+ public:
+
+ /** Datatype for displaydata */
+ typedef char DisplayData_t[TM1640_DISPLAY_MEM];
+
+ /** Constructor for class for driving TM1640 LED controller
+ *
+ * @brief Supports 16 Grids @ 8 segments.
+ * Serial bus interface device.
+ *
+ * @param PinName mosi Serial bus MOSI pin
+ * @param PinName sclk Serial bus SCLK pin
+ */
+ TM1640(PinName mosi, PinName sclk);
+
+ /** Clear the screen and locate to 0
+ */
+ void cls();
+
+ /** Write databyte to TM1640
+ * @param char data byte written at given address
+ * @param int address display memory location to write byte
+ * @return none
+ */
+ void writeData(char data, int address);
+
+ /** Write Display datablock to TM1640
+ * @param DisplayData_t data Array of TM1640_DISPLAY_MEM (=16) bytes for displaydata
+ * @param length number bytes to write (valid range 0..(TM1640_MAX_NR_GRIDS * TM1640_BYTES_PER_GRID) (=16), when starting at address 0)
+ * @param int address display memory location to write bytes (default = 0)
+ * @return none
+ */
+ void writeData(DisplayData_t data, int length = (TM1640_MAX_NR_GRIDS * TM1640_BYTES_PER_GRID), int address = 0);
+
+ /** Set Brightness
+ *
+ * @param char brightness (3 significant bits, valid range 0..7 (1/16 .. 14/14 dutycycle)
+ * @return none
+ */
+ void setBrightness(char brightness = TM1640_BRT_DEF);
+
+ /** Set the Display mode On/off
+ *
+ * @param bool display mode
+ */
+ void setDisplay(bool on);
+
+ private:
+ DigitalOut _mosi;
+ DigitalOut _sclk;
+ char _display;
+ char _bright;
+
+ /** Init the SPI interface and the controller
+ * @param none
+ * @return none
+ */
+ void _init();
+
+
+ /** Generate Start condition for TM1640
+ * @param none
+ * @return none
+ */
+ void _start();
+
+ /** Generate Stop condition for TM1640
+ * @param none
+ * @return none
+ */
+ void _stop();
+
+ /** Send byte to TM1640
+ * @param int data
+ * @return none
+ */
+ void _write(int data);
+
+ /** Write command and parameter to TM1640
+ * @param int cmd Command byte
+ * &Param int data Parameters for command
+ * @return none
+ */
+ void _writeCmd(int cmd, int data);
+};
+
+
+#if (LM1640_TEST == 1)
+// Derived class for TM1640 used in LM1640 display unit
+//
+
+#include "Font_7Seg.h"
+
+#define LM1640_NR_GRIDS 16
+#define LM1640_NR_DIGITS 16
+#define LM1640_NR_UDC 8
+
+/** Constructor for class for driving TM1640 controller as used in LM1640
+ *
+ * @brief Supports 16 Digits of 7 Segments + DP.
+ *
+ * @param PinName mosi Serial bus MOSI pin
+ * @param PinName sclk Serial bus SCLK pin
+ */
+class TM1640_LM1640 : public TM1640, public Stream {
+ public:
+
+ /** Enums for Icons */
+ // Grid encoded in 8 MSBs, Icon pattern encoded in 16 LSBs
+ enum Icon {
+ DP1 = ( 1<<24) | S7_DP1, /**< Decimal Point 1 */
+ DP2 = ( 2<<24) | S7_DP2, /**< Decimal Point 2 */
+ DP3 = ( 3<<24) | S7_DP3, /**< Decimal Point 3 */
+ DP4 = ( 4<<24) | S7_DP4, /**< Decimal Point 4 */
+ DP5 = ( 5<<24) | S7_DP5, /**< Decimal Point 5 */
+ DP6 = ( 6<<24) | S7_DP6, /**< Decimal Point 6 */
+ DP7 = ( 7<<24) | S7_DP7, /**< Decimal Point 7 */
+ DP8 = ( 8<<24) | S7_DP8, /**< Decimal Point 8 */
+ DP9 = ( 9<<24) | S7_DP9, /**< Decimal Point 9 */
+ DP10 = (10<<24) | S7_DP10, /**< Decimal Point 10 */
+ DP11 = (11<<24) | S7_DP11, /**< Decimal Point 11 */
+ DP12 = (12<<24) | S7_DP12, /**< Decimal Point 12 */
+ DP13 = (13<<24) | S7_DP13, /**< Decimal Point 13 */
+ DP14 = (14<<24) | S7_DP14, /**< Decimal Point 14 */
+ DP15 = (15<<24) | S7_DP15, /**< Decimal Point 15 */
+ DP16 = (16<<24) | S7_DP16 /**< Decimal Point 16 */
+
+ };
+
+ typedef char UDCData_t[LM1640_NR_UDC];
+
+ /** Constructor for class for driving TM1640 LED controller as used in LM1640
+ *
+ * @brief Supports 16 Digits of 7 Segments + DP.
+ *
+ * @param PinName mosi Serial bus MOSI pin
+ * @param PinName sclk Serial bus SCLK pin
+ */
+ TM1640_LM1640(PinName mosi, PinName sclk);
+
+#if DOXYGEN_ONLY
+ /** Write a character to the Display
+ *
+ * @param c The character to write to the display
+ */
+ int putc(int c);
+
+ /** Write a formatted string to the Display
+ *
+ * @param format A printf-style format string, followed by the
+ * variables to use in formatting the string.
+ */
+ int printf(const char* format, ...);
+#endif
+
+ /** Locate cursor to a screen column
+ *
+ * @param column The horizontal position from the left, indexed from 0
+ */
+ void locate(int column);
+
+ /** Clear the screen and locate to 0
+ * @param bool clrAll Clear Icons also (default = false)
+ */
+ void cls(bool clrAll = false);
+
+ /** Set Icon
+ *
+ * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 16 LSBs
+ * @return none
+ */
+ void setIcon(Icon icon);
+
+ /** Clr Icon
+ *
+ * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 16 LSBs
+ * @return none
+ */
+ void clrIcon(Icon icon);
+
+ /** Set User Defined Characters (UDC)
+ *
+ * @param unsigned char udc_idx The Index of the UDC (0..7)
+ * @param int udc_data The bitpattern for the UDC (16 bits)
+ */
+ void setUDC(unsigned char udc_idx, int udc_data);
+
+
+ /** Number of screen columns
+ *
+ * @param none
+ * @return columns
+ */
+ int columns();
+
+ /** Write databyte to TM1640
+ * @param char data byte written at given address
+ * @param int address display memory location to write byte
+ * @return none
+ */
+ void writeData(char data, int address){
+ TM1640::writeData(data, address);
+ }
+
+ /** Write Display datablock to TM1640
+ * @param DisplayData_t data Array of TM1640_DISPLAY_MEM (=16) bytes for displaydata
+ * @param length number bytes to write (valid range 0..(LM1640_NR_GRIDS * TM1640_BYTES_PER_GRID) (=16), when starting at address 0)
+ * @param int address display memory location to write bytes (default = 0)
+ * @return none
+ */
+ void writeData(DisplayData_t data, int length = (LM1640_NR_GRIDS * TM1640_BYTES_PER_GRID), int address = 0) {
+ TM1640::writeData(data, length, address);
+ }
+
+protected:
+ // Stream implementation functions
+ virtual int _putc(int value);
+ virtual int _getc();
+
+private:
+ int _column;
+ int _columns;
+
+ DisplayData_t _displaybuffer;
+ UDCData_t _UDC_7S;
+};
+#endif
+
+#endif
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TM1640_Config.h Thu Jan 28 19:25:49 2016 +0000 @@ -0,0 +1,33 @@ +/* mbed TM1640 Library, for TM1640 LEDcontroller + * Copyright (c) 2016, v01: WH, Initial version + * + * 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 TM1640_CONFIG_H +#define TM1640_CONFIG_H + +// Select one of the testboards for TM1640 LED controller +#define TM1640_TEST 0 +#define LM1640_TEST 1 + +// Select the display mode: only digits and hex or ASCII +#define SHOW_ASCII 1 + +#endif \ No newline at end of file
TM1640 LED controller (128 LEDs max).