uwizeyimana abdulkarim / Mbed 2 deprecated mobileproject

Dependencies:   mbed

Revision:
1:2a3b76b2e4e5
Parent:
0:6e0a4dac6596
Child:
2:b990d41df49e
--- a/main.cpp	Fri Jul 09 14:28:43 2021 +0000
+++ b/main.cpp	Sat Jul 10 17:44:52 2021 +0000
@@ -9,8 +9,12 @@
 #include "C12832.h"
 #include "Sht31.h"
 #include <string>
-#include "Timer.h"
-Timer timer;
+#include "time.h"
+ time_t startTime;
+ time_t stopTime;
+ time_t startReading;
+ time_t  stopReading;
+ int  readingDuration;
 Serial device (p9,p10);
 C12832 lcd(SPI_MOSI, SPI_SCK, SPI_MISO, p8, p11);
 Sht31 theo(I2C_SDA, I2C_SCL);
@@ -19,16 +23,18 @@
 DigitalOut wed(p14);
 InterruptIn  btn(p15);
 InterruptIn  swt(p11);
+Timeout t;
+Ticker k;
+Ticker topicTicker;
+Ticker durationTime;
+Ticker resetted;
+Ticker backagain;
 int direction=0;
-bool forwards=true;
+int i=0;
+int pushDuration;
 int current=0;
-int mode=0;
-int btnState=1;
-int butnDuration = 0;
- int j;
- int k;
-time_t butnTimerStart;
-time_t butnTimerStop;
+int state=0;
+
 string str1 = "Menu of the Day";
 string str2 = "Rwanda Food Services Ltd";
 string str3="Rwanda Food Services Ltd,click Btn to browseMenu";
@@ -48,152 +54,118 @@
     lcd.printf(sms.c_str());
 }
 
-void displaycontent(){
-    mode=1;
-    display(content[current]);
-    wait_ms(10000);
+void welcomeMsg(){
+    display(str2);
 }
 
-void whiteled()
-{
-    wed=1;
-    wait_ms(2000);
-    wed=0;
-    wait_ms(2000);
-   
-}
-
-void scrol(){
-  
-int i;
+void displayState1(){
         float temp = theo.readTemperature();
         time_t seconds = time(NULL);
         char buffer[32];
         strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds));
         lcd.locate(5, 20); // get cursor to position x=3px and y=5px
-        whiteled();
-        wait(3);
         lcd.cls(); 
         lcd.set_auto_up(0);
-        lcd.setmode(XOR);
-    for (i = -20; i <5; i++) { // scrolling text
-        lcd.locate(5, i);
+        lcd.locate(5, 1);
         lcd.printf("Local Time: %s",buffer);
-        lcd.locate(5, i + 12);
+        lcd.locate(5, 9);
         lcd.printf("Temperature: %.2f C", temp);
-        lcd.copy_to_lcd();
-        lcd.locate(5, i);
-        wait(1);
-        lcd.printf("Local Time: %s",buffer);
-        lcd.locate(5, i + 12);
-        lcd.printf("Temperature: %.2f C", temp);
-         i = i + 1;
-    }
 }
 
-    
-  
-/*}
  
-void StopBtnTimer(){
-    printf("Button timer stop \n");
-   // loraTimerStop =time(NULL); 
-    butnTimerStop = time(NULL);
-    printf("Button timer stop: %d \n",butnTimerStop);
-    wait(0.5);
-    
-    //loraDuration = loraTimerStop - loraTimerStart;
-    
-    butnDuration = butnTimerStop - butnTimerStart;
-    //printf("Duration Lora: %d \n", loraDuration); 
-    printf("Duration Button: %d \n", butnDuration); 
-    
-   // DurationCheck();
-  }*/
-
-void function(){
-   
-   j=0;
-    while(j<6 && mode==0){
-         whiteled();
-    display(topics[j]);
-    current=j;
-     wait_ms(2000);
-    j++;
-    }}
-
-void function2(){
-    k=5;
-    while(k>=0 && mode==0){
-         whiteled();
-    display(topics[k]);
-    current=k;
-     wait_ms(2000);
-    k--;
-    }}
-   
-void changedirection()
+ void whiteled()
 {
-   wait_ms(3000);
-  direction=0;
-  bed=1;
-  if(direction==0){
-       
-      bed=led;
-      led=!bed;
+    wed=!wed;
+}
+void forwards(){
+   display(topics[i]);
+   current=i;
+   i+=1;
+   if(i>4){
+       i=0;
+   }
+}
+void backwards(){
+  display(topics[i]);
+  current=i;
+  i-=1;
+  if(i<0){
+      i=4;
   }
-  if(forwards){
-      forwards=!forwards;
-      function();
-       display(str3);
-      
-      
-  }else{
-      forwards=!forwards;
-      function2();
-       display(str3);
-  }
-   
 }
-void StartButtonTimer(){
-     time_t seconds = time(NULL);
-        char buffer[32];
-        strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds));
-    printf("Button Timer start \n");
-    butnTimerStart = strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds));
-   //butnTimerStop=stoptime(buffer, 32, "%I:%M %p\n", localtime(&seconds));
-    printf("Button timer start: %d \n",butnTimerStart);
-    wait(0.5);
-    butnDuration =  butnTimerStart;
-     printf("Duration Button: %d \n", butnDuration); 
-      if(btnState==1 && butnDuration>=10) {
-          lcd.cls();
-          lcd.locate(0,0);
-        lcd.printf("Reset to factory ....");
-        wait(10);
-       display(str2);
-          function();
-      }
-       else{
-   changedirection();
-}
+void displayState2(){
+     if(direction==0){
+         i=0;
+          bed=1;
+    led=0;
+         topicTicker.attach(callback(&forwards),2.0f);
+     }
+     else{
+         i=4;
+  led=1;
+   bed=0;
+         topicTicker.attach(callback(& backwards),2.0f);
+     }
+ }
+
+  void fallBtn(){
+     state = 0;
+
+ }
+ void mode1(){
+    k.attach(callback(& whiteled),1.0f);
+     displayState1();
+     t.attach(callback(&displayState2),3.0f);
+    
+ }
+  
+void riseSwt(){
+    time(&startReading);
+     display(content[current]);
+    topicTicker.detach();
+    
     
 }
-void mode1(){
-    mode=0;
-    display(str3);
+void fallSwt(){
+    topicTicker.detach();
+    time(&stopReading);
+    readingDuration=stopReading-startReading;
+    printf("Reading time is:%d \n",readingDuration);
+    if(readingDuration>5){
+        printf("message sent,Item Id is:%d \n",current+1);
+    }
+    displayState2();    
 }
-void released(){
-    direction=1;
-}
+void countDuration(){
+     time(&stopTime);
+         pushDuration=stopTime-startTime;
+            if(pushDuration>5){
+            display("System resetting......\n");
+            durationTime.detach();
+             topicTicker.detach();
+            resetted.attach(callback(&welcomeMsg),10.0f);
+             direction=!direction;
+            backagain.attach(callback(&mode1),15.0f);
+            
+            }
+        else if(pushDuration>3 && state==0 ){
+             state =0;
+             direction=!direction;
+             displayState2();
+             
+         }
+ }
+ void riseBtn(){
+     time(&startTime);
+     durationTime.attach(callback(&countDuration),0.5f);
+
+ }
+
 int main() {
-    display(str2);
-    wait_ms(5000);
-    scrol();
-    btn.fall(callback(&released));
-   btn.rise(callback(&StartButtonTimer));
-   swt.rise(callback(&displaycontent));
-   swt.fall(callback(&mode1));
-    function();
-   display(str3);
+    welcomeMsg();
+    t.attach(callback(&mode1),5.0f);
+     btn.rise(callback(&riseBtn));
+     btn.fall(callback(&fallBtn));
+     swt.rise(callback(&riseSwt));
+   swt.fall(callback(&fallSwt));
 }