Test for TM1651 LED controller library Initial version, Battery monitor

Dependencies:   TM1651 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed TM1651 Test program, for TM1651 LED controller
00002  * Copyright (c) 2017, v01: WH, Initial version
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, including 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, INCLUDING 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 #include "mbed.h"
00023 #include "TM1651.h"
00024 
00025 #if (OPENSMART_TEST == 1)
00026 // OPENSMART TM1651 battery level display test
00027 
00028 Serial pc(USBTX, USBRX);
00029 DigitalOut myled(LED1);  //NOTE: On F401 LED1 is Pin D13, which is SCK!
00030 
00031 // DisplayData_t size is 4 bytes (4 Grids @ 7 Segments) 
00032 TM1651::DisplayData_t all_str   = {0x7F, 0x7F, 0x7F, 0x7F};  
00033 TM1651::DisplayData_t cls_str   = {0x00, 0x00, 0x00, 0x00};  
00034 
00035 // KeyData_t size is 1 bytes  
00036 TM1651::KeyData_t keydata; 
00037 
00038 //TM1651_OPENSMART declaration
00039 TM1651_OPENSMART OPENSMART(p6, p7);       //LPC1768
00040 //TM1651_OPENSMART OPENSMART(D9, D10);      //F401
00041 
00042 void show_menu() {
00043 //    pc.printf("0:   Exit\n\r");
00044     pc.printf("1:   All\n\r");    
00045     pc.printf("2:   Show all segs\r\n");
00046     pc.printf("3:   Show all leds\n\r");
00047     pc.printf("4:   Show all levels\n\r");    
00048     pc.printf("5:   Kitt\n\r");
00049     pc.printf("6:   Cls\n\r");    
00050     pc.printf("k:   Key\n\r");
00051 }
00052 
00053 
00054 char cmd, bits;
00055 int main() {
00056     
00057     pc.printf("Hello World\r\n"); //    
00058     
00059     OPENSMART.cls(); 
00060     OPENSMART.writeData(all_str);
00061     wait(2);
00062     OPENSMART.setBrightness(TM1651_BRT3);    
00063     wait(1);
00064     OPENSMART.setBrightness(TM1651_BRT0);        
00065     wait(1);
00066     OPENSMART.setBrightness(TM1651_BRT4);            
00067 
00068     wait(1);
00069     OPENSMART.cls(); 
00070     
00071     char cmd2 = '0';
00072     while (1) {
00073       
00074       show_menu();
00075       cmd2 = pc.getc();
00076 
00077       switch (cmd2) {
00078         case '1' : {
00079                       pc.printf("all\r\n");
00080                       OPENSMART.cls(); 
00081                       OPENSMART.writeData(all_str);
00082                       break;
00083         }
00084 
00085         case '2' : {
00086 #if(1)
00087 //test to show all segs
00088           pc.printf("Show all segs\r\n");
00089           wait(1);          
00090           OPENSMART.cls(); 
00091          
00092           for (int i=0; i<TM1651_DISPLAY_MEM; i++) {            
00093             for (int bit=0; bit<8; bit++) {
00094               OPENSMART.cls(); 
00095 
00096               bits = 0x01 << bit;              
00097               OPENSMART.writeData(bits, i);
00098 
00099               pc.printf("Idx = %d, Bits = 0x%02x\r\n", i, bits);              
00100 //              wait(0.5);
00101               cmd = pc.getc(); // wait for key
00102             }  
00103           }
00104           pc.printf("\r\nShow all segs done\r\n");                    
00105 #endif 
00106           break;          
00107         }
00108 
00109         case '3':  { 
00110 #if(1)
00111 //test to show all LEDs
00112           pc.printf("Show all Leds\r\n");
00113           OPENSMART.cls();
00114           
00115           float delay=0.1;
00116           // Leds on
00117           OPENSMART.setIcon(TM1651_OPENSMART::LD12); wait(delay);
00118           OPENSMART.setIcon(TM1651_OPENSMART::LD3);  wait(delay);
00119           OPENSMART.setIcon(TM1651_OPENSMART::LD4);  wait(delay);
00120           OPENSMART.setIcon(TM1651_OPENSMART::LD5);  wait(delay);
00121           OPENSMART.setIcon(TM1651_OPENSMART::LD67); wait(delay);
00122           OPENSMART.setIcon(TM1651_OPENSMART::LD89); wait(delay);
00123           OPENSMART.setIcon(TM1651_OPENSMART::LD10); wait(delay);
00124 
00125           wait(delay);
00126  
00127           // Leds off
00128           OPENSMART.clrIcon(TM1651_OPENSMART::LD10); wait(delay);
00129           OPENSMART.clrIcon(TM1651_OPENSMART::LD89); wait(delay);
00130           OPENSMART.clrIcon(TM1651_OPENSMART::LD67); wait(delay);
00131           OPENSMART.clrIcon(TM1651_OPENSMART::LD5);  wait(delay);
00132           OPENSMART.clrIcon(TM1651_OPENSMART::LD4);  wait(delay);
00133           OPENSMART.clrIcon(TM1651_OPENSMART::LD3);  wait(delay);
00134           OPENSMART.clrIcon(TM1651_OPENSMART::LD12); wait(delay);
00135 
00136 //          wait(1);
00137           pc.printf("Show all Leds done\r\n");                    
00138 #endif           
00139           break;
00140         }                          
00141 
00142         case '4':  { 
00143 #if(1)
00144 //test to show all Levels
00145           pc.printf("Show all Levels\r\n");
00146           OPENSMART.cls();
00147           
00148           float delay=0.1;
00149           // Levels
00150           OPENSMART.setLevel(TM1651_OPENSMART::LVL_0); wait(delay);
00151           OPENSMART.setLevel(TM1651_OPENSMART::LVL_1); wait(delay);
00152           OPENSMART.setLevel(TM1651_OPENSMART::LVL_2); wait(delay);
00153           OPENSMART.setLevel(TM1651_OPENSMART::LVL_3); wait(delay);
00154           OPENSMART.setLevel(TM1651_OPENSMART::LVL_4); wait(delay);
00155           OPENSMART.setLevel(TM1651_OPENSMART::LVL_5); wait(delay);
00156           OPENSMART.setLevel(TM1651_OPENSMART::LVL_6); wait(delay);
00157 
00158           wait(delay);
00159  
00160           // Levels off
00161           OPENSMART.setLevel(TM1651_OPENSMART::LVL_5); wait(delay);
00162           OPENSMART.setLevel(TM1651_OPENSMART::LVL_4); wait(delay);
00163           OPENSMART.setLevel(TM1651_OPENSMART::LVL_3); wait(delay);
00164 //          OPENSMART.setLevel(TM1651_OPENSMART::LVL_2); wait(delay);
00165 //          OPENSMART.setLevel(TM1651_OPENSMART::LVL_1); wait(delay);
00166 //          OPENSMART.setLevel(TM1651_OPENSMART::LVL_0); wait(delay);
00167 
00168 //          wait(1);
00169           pc.printf("Show all Levels done\r\n");
00170 #endif           
00171           break;
00172         }                          
00173 
00174         case '5': {
00175 
00176 #if(0)
00177 //test to show KITT
00178           pc.printf("Show KITT scanner\r\n");
00179           OPENSMART.cls(); // clear all
00180 
00181           float delay=0.1;
00182           while (!pc.readable()) { // wait for key
00183             // Levels
00184             OPENSMART.setLevel(TM1651_OPENSMART::LVL_0); wait(delay);
00185             OPENSMART.setLevel(TM1651_OPENSMART::LVL_1); wait(delay);
00186             OPENSMART.setLevel(TM1651_OPENSMART::LVL_2); wait(delay);
00187             OPENSMART.setLevel(TM1651_OPENSMART::LVL_3); wait(delay);
00188             OPENSMART.setLevel(TM1651_OPENSMART::LVL_4); wait(delay);
00189             OPENSMART.setLevel(TM1651_OPENSMART::LVL_5); wait(delay);
00190             OPENSMART.setLevel(TM1651_OPENSMART::LVL_6); wait(delay);
00191             // Levels off
00192             OPENSMART.setLevel(TM1651_OPENSMART::LVL_5); wait(delay);
00193             OPENSMART.setLevel(TM1651_OPENSMART::LVL_4); wait(delay);
00194             OPENSMART.setLevel(TM1651_OPENSMART::LVL_3); wait(delay);
00195             OPENSMART.setLevel(TM1651_OPENSMART::LVL_2); wait(delay);
00196             OPENSMART.setLevel(TM1651_OPENSMART::LVL_1); wait(delay);
00197             OPENSMART.setLevel(TM1651_OPENSMART::LVL_0); wait(delay);
00198             OPENSMART.cls(); ; wait(delay) // clear all            
00199           }
00200           cmd = pc.getc(); // read key           
00201           pc.printf("Show KITT done\r\n");
00202 #endif
00203 
00204 #if(1)
00205 //test to show KITT
00206           pc.printf("Show KITT scanner\r\n");
00207           OPENSMART.cls(); // clear all
00208 
00209           float delay=0.05;
00210           while (!pc.readable()) { // wait for key
00211             // Leds
00212             OPENSMART.setIcon(TM1651_OPENSMART::LD12); wait(delay);
00213             OPENSMART.setIcon(TM1651_OPENSMART::LD3); wait(delay);
00214             
00215             OPENSMART.setIcon(TM1651_OPENSMART::LD4); wait(delay);
00216             OPENSMART.clrIcon(TM1651_OPENSMART::LD12); wait(delay);
00217                         
00218             OPENSMART.setIcon(TM1651_OPENSMART::LD5); wait(delay);
00219             OPENSMART.clrIcon(TM1651_OPENSMART::LD3); wait(delay);
00220 
00221             OPENSMART.setIcon(TM1651_OPENSMART::LD67); wait(delay);
00222             OPENSMART.clrIcon(TM1651_OPENSMART::LD4); wait(delay);
00223             
00224             OPENSMART.setIcon(TM1651_OPENSMART::LD89); wait(delay);
00225             OPENSMART.clrIcon(TM1651_OPENSMART::LD5); wait(delay);
00226                         
00227             OPENSMART.setIcon(TM1651_OPENSMART::LD10); wait(delay);
00228             OPENSMART.clrIcon(TM1651_OPENSMART::LD67); wait(delay);
00229             
00230             OPENSMART.clrIcon(TM1651_OPENSMART::LD89); wait(delay);
00231 
00232             OPENSMART.clrIcon(TM1651_OPENSMART::LD10); wait(delay);
00233             
00234             OPENSMART.setIcon(TM1651_OPENSMART::LD10); wait(delay);
00235             OPENSMART.setIcon(TM1651_OPENSMART::LD89); wait(delay);
00236 
00237             OPENSMART.setIcon(TM1651_OPENSMART::LD67); wait(delay);
00238             OPENSMART.clrIcon(TM1651_OPENSMART::LD10); wait(delay);
00239 
00240             OPENSMART.setIcon(TM1651_OPENSMART::LD5); wait(delay);
00241             OPENSMART.clrIcon(TM1651_OPENSMART::LD89); wait(delay);
00242 
00243             OPENSMART.setIcon(TM1651_OPENSMART::LD4); wait(delay);
00244             OPENSMART.clrIcon(TM1651_OPENSMART::LD67); wait(delay);
00245 
00246             OPENSMART.setIcon(TM1651_OPENSMART::LD3); wait(delay);
00247             OPENSMART.clrIcon(TM1651_OPENSMART::LD5); wait(delay);
00248 
00249             OPENSMART.setIcon(TM1651_OPENSMART::LD12); wait(delay);
00250             OPENSMART.clrIcon(TM1651_OPENSMART::LD4); wait(delay);
00251 
00252             OPENSMART.clrIcon(TM1651_OPENSMART::LD3); wait(delay);
00253             
00254             OPENSMART.clrIcon(TM1651_OPENSMART::LD12); wait(delay);
00255           }
00256           cmd = pc.getc(); // read key           
00257           pc.printf("Show KITT done\r\n");          
00258 #endif
00259           break;
00260         }  
00261 
00262         case '6': {
00263           OPENSMART.cls(); // clear all
00264           break;
00265         }  
00266 
00267         case 'k': {
00268           if (OPENSMART.getKeys(&keydata)) {
00269             pc.printf("Keydata = 0x%02x\r\n", keydata);
00270           } 
00271           break;
00272         }
00273           
00274         default : {
00275           break;
00276         }         
00277      
00278       } //switch cmd
00279 
00280       myled = !myled;
00281       wait(0.3);      
00282     } //while
00283 }
00284 #endif
00285 
00286 
00287 #if (TM1651_TEST == 1)
00288 // Direct TM1651 Test
00289 
00290 Serial pc(USBTX, USBRX);
00291 DigitalOut myled(LED1);
00292 
00293 // DisplayData_t size is 4 bytes (4 Grids @ 7 Segments) 
00294 TM1651::DisplayData_t all_str   = {0x7F, 0x7F, 0x7F, 0x7F};  
00295 TM1651::DisplayData_t cls_str   = {0x00, 0x00, 0x00, 0x00};  
00296 
00297 // KeyData_t size is 1 bytes  
00298 TM1651::KeyData_t keydata; 
00299 
00300 // TM1651 declaration
00301 TM1651 TM1651(p6,p7);     //LPC1768
00302 
00303 void show_menu() {
00304 //    pc.printf("0:   Exit\n\r");
00305     pc.printf("1:   All\n\r");    
00306     pc.printf("2:   Cls\n\r");        
00307     pc.printf("3:   Show all segs\r\n");
00308 }
00309 
00310 
00311 char cmd, bits;
00312 int main() {
00313     
00314     pc.printf("Hello World\r\n"); //    
00315     
00316     TM1651.cls(); 
00317     TM1651.writeData(all_str);
00318     wait(2);
00319     TM1651.setBrightness(TM1651_BRT3);    
00320     wait(1);
00321     TM1651.setBrightness(TM1651_BRT0);        
00322     wait(1);
00323     TM1651.setBrightness(TM1651_BRT4);            
00324     
00325     while (1) {
00326      
00327       cmd = pc.getc();
00328 
00329       switch (cmd) {
00330         case '1' : 
00331           TM1651.cls(); 
00332           TM1651.writeData(all_str);
00333          break;    
00334                
00335         case '2' :       
00336           TM1651.cls(); 
00337           TM1651.writeData(cls_str);
00338          break;    
00339                
00340         case '3' :
00341 
00342 #if(1)
00343 //test to show all segs
00344           pc.printf("Show all segs\r\n");
00345           wait(1);          
00346           TM1651.cls(); 
00347          
00348           for (int i=0; i<TM1651_DISPLAY_MEM; i++) {            
00349             for (int bit=0; bit<8; bit++) {
00350               TM1651.cls(); 
00351 
00352               bits = 0x01 << bit;              
00353               TM1651.writeData(bits, i);
00354 
00355               pc.printf("Idx = %d, Bits = 0x%02x\r\n", i, bits);              
00356 //              wait(0.5);
00357               cmd = pc.getc(); // wait for key
00358             }  
00359           }
00360           pc.printf("Show all segs done\r\n");                    
00361 #endif 
00362          break;    
00363                        
00364         default : 
00365           break;          
00366      
00367       } //switch cmd
00368 
00369       // Check and read keydata
00370       if (TM1651.getKeys(&keydata)) {
00371         pc.printf("Keydata = 0x%02x\r\n", keydata);
00372       } // Check keydata
00373 
00374       myled = !myled;
00375       wait(0.3);      
00376     } //while
00377 }
00378 #endif