Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
TM1651.h
00001 /* mbed TM1651 Library, for TM1651 LED controller 00002 * Copyright (c) 2017, v01: WH, Initial version, Battery monitor 00003 * 00004 * Permission is hereby granted, free of charge, to any person obtaining a copy 00005 * of this software and associated documentation files (the "Software"), to deal 00006 * in the Software without restriction, inclumosig without limitation the rights 00007 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00008 * copies of the Software, and to permit persons to whom the Software is 00009 * furnished to do so, subject to the following conditions: 00010 * 00011 * The above copyright notice and this permission notice shall be included in 00012 * all copies or substantial portions of the Software. 00013 * 00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00015 * IMPLIED, INCLUmosiG BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00017 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00019 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00020 * THE SOFTWARE. 00021 */ 00022 00023 #ifndef TM1651_H 00024 #define TM1651_H 00025 00026 // Select one of the testboards for TM1651 LED controller 00027 #include "TM1651_Config.h" 00028 00029 /** An interface for driving TM1651 LED controller 00030 * 00031 * @code 00032 * #include "mbed.h" 00033 * #include "TM1651.h" 00034 * 00035 * Serial pc(USBTX, USBRX); 00036 * 00037 * //DisplayData_t size is 4 bytes (4 grids @ 7 segments) 00038 * TM1651::DisplayData_t all_str = {0x7F, 0x7F, 0x7F, 0x7F}; 00039 * 00040 * // KeyData_t size is 1 bytes 00041 * TM1651::KeyData_t keydata; 00042 * 00043 * // TM1651 declaration, Select the desired type in TM1651_Config.h 00044 * TM1651_OPENSMART OPENSMART(p6, p7); //LPC1768 00045 * //TM1651_OPENSMART OPENSMART(D9, D10); //F401 00046 * 00047 * int main() { 00048 * OPENSMART.cls(); 00049 * OPENSMART.writeData(all_str); 00050 * wait(1); 00051 * OPENSMART.setBrightness(TM1651_BRT0); 00052 * wait(1); 00053 * OPENSMART.setBrightness(TM1651_BRT3); 00054 * 00055 * float delay=0.1; 00056 * while (1) { 00057 * OPENSMART.cls(); 00058 * wait(0.5); 00059 * 00060 * // Levels 00061 * OPENSMART.setLevel(TM1651_OPENSMART::LVL_0); wait(delay); 00062 * OPENSMART.setLevel(TM1651_OPENSMART::LVL_1); wait(delay); 00063 * OPENSMART.setLevel(TM1651_OPENSMART::LVL_2); wait(delay); 00064 * OPENSMART.setLevel(TM1651_OPENSMART::LVL_3); wait(delay); 00065 * OPENSMART.setLevel(TM1651_OPENSMART::LVL_4); wait(delay); 00066 * OPENSMART.setLevel(TM1651_OPENSMART::LVL_5); wait(delay); 00067 * OPENSMART.setLevel(TM1651_OPENSMART::LVL_6); wait(delay); 00068 * // Levels off 00069 * OPENSMART.setLevel(TM1651_OPENSMART::LVL_5); wait(delay); 00070 * OPENSMART.setLevel(TM1651_OPENSMART::LVL_4); wait(delay); 00071 * 00072 * // Check and read keydata 00073 * if (OPENSMART.getKeys(&keydata)) { 00074 * pc.printf("Keydata = 0x%02x\r\n", keydata); 00075 * 00076 * if (keydata == TM1651_SW9_BIT) { //sw9 00077 * OPENSMART.cls(); 00078 * OPENSMART.printf("--09"); 00079 * } 00080 * } // Check keydata 00081 * } // while 00082 * } 00083 * @endcode 00084 */ 00085 00086 00087 //TM1651 Display data 00088 #define TM1651_MAX_NR_GRIDS 4 00089 #define TM1651_BYTES_PER_GRID 1 00090 00091 //Significant bits Keymatrix data 00092 //#define TM1638_KEY_MSK 0xFF 00093 00094 //Memory size in bytes for Display and Keymatrix 00095 #define TM1651_DISPLAY_MEM (TM1651_MAX_NR_GRIDS * TM1651_BYTES_PER_GRID) 00096 #define TM1651_KEY_MEM 1 00097 00098 //Reserved bits for commands 00099 #define TM1651_CMD_MSK 0xC0 00100 00101 //Data setting commands 00102 #define TM1651_DATA_SET_CMD 0x40 00103 #define TM1651_DATA_WR 0x00 00104 #define TM1651_KEY_RD 0x02 00105 #define TM1651_ADDR_INC 0x00 00106 #define TM1651_ADDR_FIXED 0x04 00107 #define TM1651_MODE_NORM 0x00 00108 #define TM1651_MODE_TEST 0x08 00109 00110 //Address setting commands 00111 #define TM1651_ADDR_SET_CMD 0xC0 00112 #define TM1651_ADDR_MSK 0x03 //0..3 00113 00114 //Display control commands 00115 #define TM1651_DSP_CTRL_CMD 0x80 00116 #define TM1651_BRT_MSK 0x07 00117 #define TM1651_BRT0 0x00 //Pulsewidth 1/16 00118 #define TM1651_BRT1 0x01 00119 #define TM1651_BRT2 0x02 00120 #define TM1651_BRT3 0x03 00121 #define TM1651_BRT4 0x04 00122 #define TM1651_BRT5 0x05 00123 #define TM1651_BRT6 0x06 00124 #define TM1651_BRT7 0x07 //Pulsewidth 14/16 00125 00126 #define TM1651_BRT_DEF TM1651_BRT3 00127 00128 #define TM1651_DSP_OFF 0x00 00129 #define TM1651_DSP_ON 0x08 00130 00131 00132 //Access to 8 Switches 00133 //S0 S1 S2 K1 K2 1 1 1 00134 //K1 = 0 1 00135 //K2 = 1 00136 #define TM1651_SW1_BIT 0xEF 00137 #define TM1651_SW2_BIT 0x6F 00138 #define TM1651_SW3_BIT 0xAF 00139 #define TM1651_SW4_BIT 0x2F 00140 #define TM1651_SW5_BIT 0xCF 00141 #define TM1651_SW6_BIT 0x4F 00142 #define TM1651_SW7_BIT 0x8F 00143 00144 #define TM1651_SW_NONE 0xFF 00145 00146 00147 //Segments 00148 #define S7_S1 (1 << 0) 00149 #define S7_S2 (1 << 1) 00150 #define S7_S3 (1 << 2) 00151 #define S7_S4 (1 << 3) 00152 #define S7_S5 (1 << 4) 00153 #define S7_S6 (1 << 5) 00154 #define S7_S7 (1 << 6) 00155 00156 00157 /** A class for driving TM1651 LED controller 00158 * 00159 * @brief Supports 4 Grids @ 7 Segments and 7 Keys. 00160 * Serial bus interface device. 00161 */ 00162 class TM1651 { 00163 public: 00164 00165 /** Datatype for displaydata */ 00166 typedef char DisplayData_t[TM1651_DISPLAY_MEM]; 00167 00168 /** Datatypes for keymatrix data */ 00169 typedef char KeyData_t; 00170 00171 00172 /** Constructor for class for driving TM1651 LED controller 00173 * 00174 * @brief Supports 4 Grids @ 7 segments and 7 Keys. 00175 * Serial bus interface device. 00176 * 00177 * @param PinName dio Serial bus DIO pin 00178 * @param PinName sck Serial bus CLK pin 00179 */ 00180 TM1651(PinName dio, PinName clk); 00181 00182 00183 /** Clear the screen and locate to 0 00184 */ 00185 void cls(); 00186 00187 /** Write databyte to TM1651 00188 * @param char data byte written at given address 00189 * @param int address display memory location to write byte 00190 * @return none 00191 */ 00192 void writeData(char data, int address); 00193 00194 /** Write Display datablock to TM1651 00195 * @param DisplayData_t data Array of TM1651_DISPLAY_MEM (=4) bytes for displaydata 00196 * @param length number bytes to write (valid range 0..(TM1651_MAX_NR_GRIDS * TM1651_BYTES_PER_GRID) (=4), when starting at address 0) 00197 * @param int address display memory location to write bytes (default = 0) 00198 * @return none 00199 */ 00200 void writeData(DisplayData_t data, int length = (TM1651_MAX_NR_GRIDS * TM1651_BYTES_PER_GRID), int address = 0); 00201 00202 /** Read keydata block from TM1651 00203 * @param *keydata Ptr to bytes for keydata 00204 * @return bool keypress True when at least one key was pressed 00205 * 00206 */ 00207 bool getKeys(KeyData_t *keydata); 00208 00209 /** Set Brightness 00210 * 00211 * @param char brightness (3 significant bits, valid range 0..7 (1/16 .. 14/16 dutycycle) 00212 * @return none 00213 */ 00214 void setBrightness(char brightness = TM1651_BRT_DEF); 00215 00216 /** Set the Display mode On/off 00217 * 00218 * @param bool display mode 00219 */ 00220 void setDisplay(bool on); 00221 00222 private: 00223 DigitalInOut _dio; 00224 DigitalOut _clk; 00225 00226 char _display; 00227 char _bright; 00228 00229 /** Init the Serial interface and the controller 00230 * @param none 00231 * @return none 00232 */ 00233 void _init(); 00234 00235 00236 /** Generate Start condition for TM1651 00237 * @param none 00238 * @return none 00239 */ 00240 void _start(); 00241 00242 /** Generate Stop condition for TM1651 00243 * @param none 00244 * @return none 00245 */ 00246 void _stop(); 00247 00248 /** Send byte to TM1651 00249 * @param int data 00250 * @return none 00251 */ 00252 void _write(int data); 00253 00254 /** Read byte from TM1651 00255 * @return read byte 00256 */ 00257 char _read(); 00258 00259 /** Write command and parameter to TM1651 00260 * @param int cmd Command byte 00261 * &Param int data Parameters for command 00262 * @return none 00263 */ 00264 void _writeCmd(int cmd, int data); 00265 }; 00266 00267 #if (OPENSMART_TEST == 1) 00268 // Derived class for TM1651 used in OPEN-SMART battery display unit with 10 Segments 00269 // 00270 00271 #define OPENSMART_NR_GRIDS 4 00272 #define OPENSMART_NR_DIGITS 4 00273 #define OPENSMART_NR_UDC 0 00274 00275 //Battery level Icons mapping onto Segments 00276 #define R12 S7_S1 00277 #define Y3 S7_S2 00278 #define Y4 S7_S3 00279 #define Y5 S7_S4 00280 #define G67 S7_S5 00281 #define G89 S7_S6 00282 #define B10 S7_S7 00283 00284 00285 /** Constructor for class for driving TM1651 controller as used in OPENSMART 00286 * 00287 * @brief Supports battery display unit with 10 Segments. 00288 * 00289 * @param PinName dio Serial bus DIO pin 00290 * @param PinName clk Serial bus CLK pin 00291 */ 00292 class TM1651_OPENSMART : public TM1651 { 00293 public: 00294 00295 /** Enums for Icons */ 00296 // Grid encoded in 8 MSBs, Pattern encoded in 16 LSBs 00297 enum Icon { 00298 LD12 = ( 1<<24) | R12, /**< LED1 (Red) & LED2 (Red)*/ 00299 LD3 = ( 1<<24) | Y3, /**< LED3 (Yellow)*/ 00300 LD4 = ( 1<<24) | Y4, /**< LED4 (Yellow)*/ 00301 LD5 = ( 1<<24) | Y5, /**< LED5 (Yellow)*/ 00302 LD67 = ( 1<<24) | G67, /**< LED6 (Green) & LED7 (Green)*/ 00303 LD89 = ( 1<<24) | G89, /**< LED8 (Green) & LED9 (Green)*/ 00304 LD10 = ( 1<<24) | B10, /**< LED10 (Blue) */ 00305 }; 00306 00307 /** Enums for Batterylevels */ 00308 enum Level { 00309 LVL_0, /**< Level 0 = R12 */ 00310 LVL_1, /**< Level 1 = R12 - Y3 */ 00311 LVL_2, /**< Level 2 = R12 - Y4 */ 00312 LVL_3, /**< Level 3 = R12 - Y5 */ 00313 LVL_4, /**< Level 4 = R12 - G67 */ 00314 LVL_5, /**< Level 5 = R12 - G89 */ 00315 LVL_6 /**< Level 6 = R12 - B10 */ 00316 00317 #if(0) 00318 LVL_0 = ( 1<<24) | R12, /**< Level 0 */ 00319 LVL_1 = ( 1<<24) | R12 | Y3, /**< Level 1 */ 00320 LVL_2 = ( 1<<24) | R12 | Y3 | Y4, /**< Level 2 */ 00321 LVL_3 = ( 1<<24) | R12 | Y3 | Y4 | Y5, /**< Level 3 */ 00322 LVL_4 = ( 1<<24) | R12 | Y3 | Y4 | Y5 | G67, /**< Level 4 */ 00323 LVL_5 = ( 1<<24) | R12 | Y3 | Y4 | Y5 | G67 | G89, /**< Level 5 */ 00324 LVL_6 = ( 1<<24) | R12 | Y3 | Y4 | Y5 | G67 | G89 | B10, /**< Level 6 */ 00325 #endif 00326 }; 00327 00328 00329 /** Constructor for class for driving TM1651 LED controller 00330 * 00331 * @brief Supports battery display unit with 10 Segments. 00332 * Serial bus interface device. 00333 * 00334 * @param PinName dio Serial bus DIO pin 00335 * @param PinName sck Serial bus CLK pin 00336 */ 00337 TM1651_OPENSMART(PinName dio, PinName clk); 00338 00339 /** Clear the screen and locate to 0 00340 * @param none 00341 * @return none 00342 */ 00343 void cls(); 00344 00345 /** Set Icon 00346 * 00347 * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Pattern encoded in 16 LSBs 00348 * @return none 00349 */ 00350 void setIcon(Icon icon); 00351 00352 /** Clr Icon 00353 * 00354 * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Pattern encoded in 16 LSBs 00355 * @return none 00356 */ 00357 void clrIcon(Icon icon); 00358 00359 /** Set Level 00360 * 00361 * @param Level level Enums Level indicates the Battery level to be displayed 00362 * @return none 00363 */ 00364 void setLevel(Level level); 00365 00366 00367 /** Write databyte to TM1651 00368 * @param char data byte written at given address 00369 * @param int address display memory location to write byte 00370 * @return none 00371 */ 00372 void writeData(char data, int address){ 00373 TM1651::writeData(data, address); 00374 } 00375 00376 /** Write Display datablock to TM1651 00377 * @param DisplayData_t data Array of TM1651_DISPLAY_MEM (=4) bytes for displaydata 00378 * @param length number bytes to write (valid range 0..(OPENSMART_NR_GRIDS * TM1651_BYTES_PER_GRID) (=4), when starting at address 0) 00379 * @param int address display memory location to write bytes (default = 0) 00380 * @return none 00381 */ 00382 void writeData(DisplayData_t data, int length = (OPENSMART_NR_GRIDS * TM1651_BYTES_PER_GRID), int address = 0) { 00383 TM1651::writeData(data, length, address); 00384 } 00385 00386 protected: 00387 00388 private: 00389 DisplayData_t _displaybuffer; 00390 00391 }; 00392 #endif 00393 00394 #endif
Generated on Mon Jul 25 2022 00:01:41 by
1.7.2
TM1651 LED controller (28 LEDs max), Keyboard scan (7 keys max)