test menu

Dependencies:   Menu RPG SMARTGPU TextLCD Ton mbed

Fork of lcd_menu by Peihsun Yeh

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 // #include "TextLCD.h"
00003 #include "RPG.h"
00004 #include "Selection.h"
00005 #include "Menu.h"
00006 #include "Navigator.h"
00007 #include <vector>
00008 #include <string>
00009 #include "SMARTGPU.h"
00010 #include "Ton.h"
00011 Ton t1(11000);
00012 Ton t2(60000);
00013 Ton t3(840000);
00014 char Rtions[5];
00015 char p;
00016 DigitalOut Out1(LED1);
00017 DigitalOut Out2(LED2);
00018 DigitalOut out3(LED3);
00019 DigitalOut Out4(LED4);
00020 InterruptIn pb(p24);
00021 InterruptIn bt1(p26);
00022 InterruptIn bt2(p28);
00023 int waiting=0; 
00024 int n;
00025 //int MTR = 0;
00026 DigitalOut MTR(p19);
00027 int volatile countT=0;
00028 char counter = 0;
00029 //const char Cpass[10]= {0x00};
00030 char Cpass[10];
00031 //int volatile old=0;
00032 int  old = 0;
00033 int  bt1o = 1;
00034 int oldbt2=1;
00035 int Mn1 = 1;
00036 int Mn2 = 3;
00037 
00038 void pb_hit_interrupt (void) {
00039     countT++;
00040     if( (old==0 ) & (waiting == 0) &( oldbt2 == 0)&(  bt1o == 0)   )    {
00041       MTR = 1;
00042      Out4 = !Out4;
00043       Out1=1;
00044       old = 1;
00045       }
00046       }
00047       
00048 void bt1_hit_interrupt (void) {
00049    if( (bt1o==0 )& (old == 0)    )    {
00050      bt1o = 1;
00051     }  
00052      } 
00053       
00054       
00055 void bt2_hit_interrupt (void) {
00056    if( (oldbt2==0 )& (old == 0) )    {
00057    oldbt2 =1;
00058    
00059      }}    
00060       
00061 //#include "rpc.h"
00062 Serial pc(USBTX, USBRX);
00063 SMARTGPU lcd(p13,p14,p15);        //(TX,RX,Reset);
00064   
00065 DigitalOut led1(LED1), led2(LED2), led3(LED3), led4(LED4);
00066 //TextLCD lcd(p15, p16, p17, p18, p19, p20); // rs, e, d4-d7
00067 RPG rpg(p21,p22,p23); 
00068 
00069 
00070 int oldpass;
00071 int pass;
00072 int oldCons;
00073 int Cons;
00074 
00075 int oldcourbe;
00076 int courbe;
00077 int oldHor;
00078 int Hor;
00079 
00080 int oldJour;
00081 int Jour;
00082 
00083 
00084 
00085 using namespace std;
00086 int prevpass;
00087 
00088 char bufCons[3];
00089 char oldbufCons[3];
00090 char bufPass[1];
00091 char oldbufpass[1];
00092 
00093 char bufcourbe[1];
00094 char oldbufcourbe[1];
00095 
00096 char bufJour[1];
00097 char oldbufJour[1];
00098 
00099 char bufHor[1];
00100 char oldbufHor[1];
00101 
00102 
00103 // some functions to tie to selections
00104 void toggleLED1() { led1 = !led1; }
00105 void toggleLED2() { led2 = !led2; }
00106 void toggleLED3() { led3 = !led3; }
00107 void toggleLED4() { led4 = !led4; }
00108 void incpass() { 
00109 oldpass = pass;
00110 pass++; 
00111 
00112 sprintf(bufPass, "%d", pass);
00113 }
00114 
00115 void decpass() { 
00116 oldpass = pass;
00117 pass--;
00118 sprintf(bufPass, "%d", pass);
00119  }
00120                              
00121 void incCons() { 
00122 oldCons = Cons;
00123 Cons++;
00124 sprintf(bufCons, "%d", Cons);
00125  }
00126  
00127 void decCons() { 
00128 oldCons = Cons;
00129 Cons--;
00130 sprintf(bufCons, "%d", Cons);
00131  }
00132 
00133 
00134 // Horaire
00135 void incHor() { 
00136 oldHor = Hor;
00137 Hor++;
00138 sprintf(bufHor, "%d", Hor);
00139  }
00140  void decHor() { 
00141 oldHor = Hor;
00142 Hor--;
00143 sprintf(bufHor, "%d", Hor);
00144  }
00145 
00146  // Jour
00147 void incJour() { 
00148 oldJour = Jour;
00149 Jour++;
00150 sprintf(bufJour, "%d", Jour);
00151  }
00152 void decJour() { 
00153 oldJour = Jour;
00154 Jour--;
00155 sprintf(bufJour, "%d", Jour);
00156  }
00157  
00158  // Courbe
00159 void incCourbe() { 
00160 oldcourbe = courbe;
00161 courbe++;
00162 sprintf(bufcourbe, "%d", courbe);
00163  }
00164 void decCourbe() { 
00165 oldcourbe = courbe;
00166 courbe--;
00167 sprintf(bufcourbe, "%d", courbe);
00168  }
00169 
00170 
00171 void resetLED()
00172 {
00173     led1 = 0;
00174     led2 = 0;
00175     led3 = 0;
00176     led4 = 0;
00177 }
00178 
00179 int main() {
00180         Cons = 0;
00181           pass = 2;
00182           Hor = 1;
00183           courbe = 3;
00184           Jour = 7;
00185      lcd.reset();                    //physically reset SMARTGPU
00186      lcd.start();                    //initialize the SMARTGPU processor
00187  toggleLED2();
00188  pass = 1;
00189  set_time(1309776930); 
00190 pb.mode(PullUp);
00191 bt1.mode(PullUp);
00192 bt2.mode(PullUp);
00193 wait(1);
00194 pb.fall(&pb_hit_interrupt);
00195 
00196 bt1.rise(&bt1_hit_interrupt);
00197 bt2.rise(&bt2_hit_interrupt);
00198 
00199 Rtions[1]=11;
00200  Rtions[2]=12;
00201  Rtions[3]=13;
00202  Rtions[4]=14;
00203  time_t rawtime = time(NULL);
00204 
00205 
00206     // In using this library, the user declares "Menus", which are essentially arrays of "Selections".
00207     // Selections describe each individual selectable item. 
00208     // Selections can be tied to functions. Functions must output void and take in no arguments.
00209     // Selections can also have references to child menus.
00210  
00211     // It makes sense to declare a root menu first, but you don't have to. 
00212     // Menus should have an identifier (the argument in constructor). 
00213        Menu rootMenu("Menu Principale"); 
00214     
00215     // Selections are added to menus through the Menu's "add" method.
00216     // If a function is to be executed when the RPG is depressed, make a REFERENCE to it. Otherwise null.
00217     // The second argument is its position - the will be deprecated soon.
00218     // The last is the text to display. *INCLUDE A SPACE IN THE BEGINNING, this is where the cursor goes. 
00219     // *This means the text is limited to 14 characters (not counting the space) with this display. 
00220     // **It is 14 not 15 for reasons pertaining to the implementation of the TextLCD library. 
00221     Menu ledMenu("Options"); 
00222     ledMenu.add(Selection(&toggleLED1, 0, NULL," Toggle LED1")); // The function argument of selection can be added directly
00223     ledMenu.add(Selection(&toggleLED2, 1, NULL, " Toggle LED2"));  
00224     ledMenu.add(Selection(&toggleLED3, 2, NULL, " Toggle LED3"));
00225     ledMenu.add(Selection(&resetLED, 3, &rootMenu, " Retour"));
00226     ledMenu.add(Selection(NULL, 4, NULL, "Options"));  // always add a Selection at the end to point to the parent
00227     
00228     Menu AlimMenu("Alimenter"); // about menu crediting us :)
00229     AlimMenu.add(Selection(NULL, 0, NULL, " Voir Courbe"));
00230     AlimMenu.add(Selection(&incpass, 1, NULL, " Augmenter"));
00231     AlimMenu.add(Selection(&decpass, 2, NULL, " Diminuer"));
00232     AlimMenu.add(Selection(NULL, 3, &rootMenu, " Retour"));
00233     AlimMenu.add(Selection(NULL, 4, NULL, "Alimenter"));
00234          
00235     Menu ledCourbe("Courbe"); // about menu crediting us :)
00236     ledCourbe.add(Selection(&incCourbe, 1, NULL, " Augmenter"));
00237     ledCourbe.add(Selection(&decCourbe, 2, NULL, " Diminuer"));
00238     ledCourbe.add(Selection(NULL, 2, NULL, " "));
00239     ledCourbe.add(Selection(NULL, 3, &rootMenu, " Retour"));
00240     ledCourbe.add(Selection(NULL, 4, NULL, "Courbe"));
00241     
00242     Menu ledHoraire("Horaire"); // about menu crediting us :)
00243     ledHoraire.add(Selection(&incHor, 1, NULL, " Augmenter"));
00244     ledHoraire.add(Selection(&decHor, 2, NULL, " Diminuer"));
00245     ledHoraire.add(Selection(NULL, 2, NULL, " "));
00246     ledHoraire.add(Selection(NULL, 3, &rootMenu, " Retour"));
00247     ledHoraire.add(Selection(NULL, 4, NULL, "Horaire"));
00248       
00249     Menu ledJour("Jour"); // about menu crediting us :)
00250     ledJour.add(Selection(&incJour, 1, NULL, " Augmenter"));
00251     ledJour.add(Selection(&decJour, 2, NULL, " Diminuer"));
00252     ledJour.add(Selection(NULL, 2, NULL, " "));
00253     ledJour.add(Selection(NULL, 3, &rootMenu, " Retour"));
00254     ledJour.add(Selection(NULL, 4, NULL, "Jour"));
00255     
00256     Menu GestMenu("Gestation"); // about menu crediting us :)
00257     GestMenu.add(Selection(NULL, 0,&ledCourbe, " Courbe"));
00258     GestMenu.add(Selection(NULL, 1, &ledHoraire, " Horaire"));
00259     GestMenu.add(Selection(NULL, 2, &ledJour, " Journee Depart"));
00260     GestMenu.add(Selection(NULL, 3, &rootMenu, " Retour"));
00261     GestMenu.add(Selection(NULL, 4, NULL, "Gestation"));
00262    
00263     Menu ledCons("Consom"); // about menu crediting us :)
00264     ledCons.add(Selection(&incCons, 1, NULL, " Augmenter"));
00265     ledCons.add(Selection(&decCons, 2, NULL, " Diminuer"));
00266     ledCons.add(Selection(NULL, 2, NULL, " "));
00267     ledCons.add(Selection(NULL, 3, &rootMenu, " Retour"));
00268     ledCons.add(Selection(NULL, 4, NULL, "Consom"));
00269 
00270     
00271     Menu MBMenu("Mise Bas"); // about menu crediting us :)
00272     MBMenu.add(Selection(NULL, 0, NULL, " Courbe Parite"));
00273     MBMenu.add(Selection(NULL, 1, NULL, " Courbe Horaire"));
00274     MBMenu.add(Selection(NULL, 2, NULL, " Journee Depart"));
00275     MBMenu.add(Selection(NULL, 3, &rootMenu, " Retour"));
00276     MBMenu.add(Selection(NULL, 4, NULL, "Mise Bas"));
00277 
00278     rootMenu.add(Selection(NULL, 0, &AlimMenu, " Alimenter"));
00279     rootMenu.add(Selection(NULL, 1, &GestMenu, " Gestation"));  // a dummy menu, doesn't do anything
00280     rootMenu.add(Selection(NULL, 2, &MBMenu, " Mise Bas "));   
00281      rootMenu.add(Selection(NULL, 3, &ledMenu, " Options "));
00282  //  rootMenu.add(Selection(NULL, 4, &ledCourbe, " Courbe "));
00283   // rootMenu.add(Selection(NULL, 5, &ledHoraire, " Horaire "));
00284   // rootMenu.add(Selection(NULL, 6, &ledJour, " Jour "));
00285 //  rootMenu.add(Selection(NULL, 7, &ledCons, " Consom "));
00286     rootMenu.add(Selection(NULL, 4, NULL, "Menu Principale"));
00287     // Here is the heart of the system: the navigator. 
00288     // The navigator takes in a reference to the root, an interface, and a reference to an lcd
00289     Navigator navigator(&rootMenu, rpg, &lcd);
00290   prevpass = 0; 
00291     while(1){
00292   //       navigator.poll();
00293         Out2=1;
00294 
00295 
00296 if ( bt1o == 1){
00297 //++menu   if menu > 4 then menu = 0;
00298             Mn1 = ++ Mn1 % 9;
00299 //            Mn1 = Menu1(Mn1);
00300             wait(.1);
00301             bt1o = 0;
00302 }
00303 
00304 if ( oldbt2 == 1){
00305 //++menu   if menu2 > 4 then menu2 = 0;
00306              Mn2 = ++ Mn2 % 5;
00307 //             Mn2=Menu2(Mn2);
00308              wait(.1);
00309             oldbt2 = 0;
00310 }
00311          //  lcd.locate(0,1);
00312          //     lcd.printf("  bp out5\n"); 
00313          //     wait(.5); 
00314 if( (out3==1) &  (waiting == 840)     ){
00315            
00316            Out1=0;
00317 //           lcd.locate(0,1); lcd.printf("Out3-1\n");
00318             wait(1);
00319    if(   t3         ){
00320   //          navigator.poll();
00321            t3=0;                                                                  
00322  //           lcd.locate(0,1); lcd.printf("Out3wa\n");
00323             //   wait(1);
00324        out3=0;
00325        waiting = 0;
00326         counter = 0;
00327 }
00328 }
00329 if((Out1==1) & (waiting ==11)){
00330               
00331                                                         
00332 //       lcd.locate(0,1); 
00333 //       lcd.printf("Alimente      \n");
00334         wait(.1);
00335   if(   t1          ){
00336        
00337        Out1=0;
00338        t1=0;
00339        MTR=0;
00340             //lcd.locate(0,1); 
00341             //lcd.printf("Out11wait11E   \n");
00342               wait(.1);
00343        waiting = 60;
00344         t2=1;
00345       old=0;
00346        counter ++;
00347         Rtions[p]--;
00348 
00349 }
00350 }
00351 
00352 
00353 if (   (old==1)&(MTR==1)  & (waiting == 0) )  {
00354                         
00355                                                                        
00356 //                     lcd.locate(0,1);
00357  //                     lcd.printf("alimente   \n");
00358                        wait( 1);
00359            rawtime = time(NULL);
00360         waiting = 11;
00361         t1=1;
00362                      //  lcd.locate(0,1);
00363                      //   lcd.printf("nwait w1     \n");
00364                          wait( 1);
00365        
00366          if (counter >= 6) {
00367             
00368             out3=1;
00369             waiting = 840;t3=1;
00370               }
00371        }
00372 
00373 if((Out1==0) & (waiting ==60)){
00374          
00375           out3=1; 
00376 //                  lcd.locate(0,1); 
00377 //                  lcd.printf("Attend 60 sec  "); 
00378                   wait(61);
00379   if(   t2         ){
00380       
00381        out3=0;
00382          t2=0;  
00383 //        lcd.locate(0,1); 
00384 //         lcd.printf("             "); 
00385           wait(1);
00386        waiting = 0;
00387     //  old=0;
00388        
00389  // lcd.locate(0,1); lcd.printf("apres old-0    \n"); 
00390  // wait(1);
00391 }
00392 }
00393   //            lcd.string(100,200,300,220,WHITE,FONT6,TRANS," ");  //
00394      //    oldHor = Hor;  oldJour = Jour;  oldcourbe = courbe;
00395      
00396    if (  (oldpass != pass)| ( oldCons != Cons) | ( oldHor != Hor) | ( oldcourbe != courbe) | ( oldJour != Jour)){
00397        
00398         n=sprintf (Cpass, "Pa:%d Cs:%d H:%d Cb:%d Jr:%d", pass,Cons,Hor,courbe,Jour);
00399         lcd.string(15,210,300,220,WHITE,FONT3,TRANS,Cpass); 
00400         oldCons = Cons;
00401           oldpass = pass;
00402           oldHor = Hor;
00403           oldcourbe = courbe;
00404           oldJour = Jour;
00405        }
00406   //              lcd.string(100,200,300,220,WHITE,FONT6,TRANS,bufPass);  //
00407             
00408              //   lcd.string(100,200,300,220,WHITE,FONT6,TRANS,bufPass);  //
00409  
00410     //          lcd.string(150,200,300,220,WHITE,FONT6,TRANS,bufPass);  //
00411      
00412         navigator.poll(); // In a loop, call navigator's poll method to determine if the user is interacting with the rpg. 
00413 
00414     }
00415 }