Program code for the project

Dependencies:   4DGL-uLCD-SE SDAlarm mbed SDFileSystem SoundAPI mbed-rtos

Revision:
1:049d9c16aa35
Parent:
0:ecc736db2e40
Child:
2:15ba42aea6c7
--- a/main.cpp	Wed Dec 06 16:41:55 2017 +0000
+++ b/main.cpp	Mon Dec 11 22:58:04 2017 +0000
@@ -1,25 +1,42 @@
 #include "mbed.h"
-#include <ttmath.h>
-#include "PinDetect.h"
 #include "uLCD_4DGL.h"
+#include "rtos.h"
+#include "SDFileSystem.h"
+#include "SDAlarm.h"
+#include "wave_player.h"
 
 Serial pc(USBTX, USBRX);
 Serial esp(p28, p27); // tx, rx
+SDFileSystem sd(p5, p6, p7, p8, "sd");
+
+RawSerial  blue(p13,p14);
 DigitalOut reset(p26);
 DigitalOut led1(LED1);
 DigitalOut led4(LED4);
 Timer t;
+SDAlarm sdalarm;
 
+AnalogOut ao(p18);
+wave_player wp(&ao);
+
+time_t global_time;
 uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin;
+Mutex lcd_mutex;
+
+Thread alarmThread;
+Thread alarmSound;
+
  
-PinDetect pb1(p17);
-PinDetect pb2(p18);
-PinDetect pb3(p19);
-PinDetect pb4(p20);
+bool alarmWentOff = false;
+bool play = false;
 
+DigitalOut one(LED1);
+DigitalOut two(LED2);
+DigitalOut three(LED3);
+DigitalOut four(LED4);
 // Global count variable
 
-int volatile hours=12;
+int volatile hours=0;
 int volatile minutes=0;
 bool volatile alarmSet=false;
 int volatile alarmTimeSet = 0;
@@ -29,8 +46,8 @@
 int  count,ended,timeout;
 char buf[2024];
 char snd[1024];
-char ssid[32] = "MEwifi";     // enter WiFi router ssid inside the quotes
-char pwd [32] = "fxyh5453"; // enter WiFi router password inside the quotes
+char ssid[32] = "Ben’s iPhone";     // enter WiFi router ssid inside the quotes
+char pwd [32] = "bensiphonepassword96"; // enter WiFi router password inside the quotes
 bool timeReady = false;
 
 char curTime[20];
@@ -53,55 +70,99 @@
     }
 }
  
- void pb1_hit_callback (void) {
-    count++;
-    hours++;
-    if(hours>12){ hours= 1; AMPM=!AMPM;}
-   
-    
-    uLCD.cls();
-    uLCD.printf("\nAlarm Time %d : %d\nTime %d",hours, minutes, timeDisp);
+
+ 
+void bt_rec() {
+    while(blue.readable()) {
+        if (blue.getc()=='!') {
+            char c = blue.getc();
+            if (c=='B') { //button data
+                char bnum = blue.getc(); //button number
+                char pressedOrReleased = blue.getc();
+                
+                if (pressedOrReleased == '0') {
+                    if (bnum=='1')  {
+                        alarmSet = !alarmSet;
+                    }
+                }
+                if (pressedOrReleased == '1') {
+                    
+                    if (bnum=='2') {
+                        hours++;
+                        if(hours>24){ hours= 0;}
+                    }
+                    if (bnum=='3') {
+                            minutes++;
+                            if(minutes>59) minutes= 0;
+                    }
+                }
+                
+            }
+        }
+    }
 }
+void alarm_sound() {
+    while (true) {
+        if (play){
+            FILE *wave_file;
+            wave_file=fopen("/sd/alarm_beep.wav","r");
 
-void pb2_hit_callback (void) {
-    count++;
-    minutes++;
-    if(minutes>59) minutes= 0;
-    uLCD.cls();
-     uLCD.printf("\nAlarm Time %d : %d\nTime %d",hours, minutes, timeDisp);
+            wp.play(wave_file);
+            fclose(wave_file);
+        }
+    }
 }
+void alarm_thread() {
+    time_t lastalarm = time(NULL);
+    bool lastset = false;
+    while(1) {
+        time_t rawtime = hours*60*60 + minutes*60;
+        struct tm * timeinfo;
+        char buffer [80];
 
-void pb3_hit_callback (void) {
-    alarmSet=true;
-    alarmTimeSet=60*60*hours+60*minutes;
-    uLCD.cls();
-    uLCD.printf("\nAlarm Time %d : %d\nTime %d\nAlarm Turned On",hours, minutes, timeDisp);
-}
+        timeinfo = localtime (&rawtime);
+        strftime (buffer,80,"%I:%M %p",timeinfo);
+        //if (lastalarm != rawtime) {
+            lcd_mutex.lock();
+            uLCD.locate(3,11);
+            uLCD.color(WHITE);
+            uLCD.text_width(1); 
+            uLCD.text_height(1);
+            uLCD.printf("%s", buffer);
+            
+            lastalarm = rawtime;
+            
+        //}
+        if (lastset != alarmSet) {
+
 
-void pb4_hit_callback (void) {
-    alarmSet=false;
-    uLCD.cls();
-    uLCD.printf("\nAlarm Time %d : %d\nTime %d\nAlarm Turned Off",hours, minutes, timeDisp);
+            uLCD.color(WHITE);
+            uLCD.text_width(1); 
+            uLCD.text_height(1);
+            uLCD.locate(3,12);
+            if (alarmSet) {
+                
+                uLCD.color(WHITE);
+                uLCD.printf("Alarm Set");
+                sdalarm.setAlarm(rawtime);
+            } else {
+                
+                uLCD.color(BLACK);
+                uLCD.printf("Alarm Set");
+            }
+            lastset = alarmSet;
+            
+        }
+        
+        lcd_mutex.unlock();
+        Thread::wait(50);
+    }    
 }
- 
 int main()
 {
-    pb1.mode(PullUp);
-    pb2.mode(PullUp);
-    pb3.mode(PullUp);
-    pb4.mode(PullUp);
-   
-    wait(.001);
-    // Setup Interrupt callback function for a pb hit
-    pb1.attach_deasserted(&pb1_hit_callback);
-    pb2.attach_deasserted(&pb2_hit_callback);
-    pb3.attach_deasserted(&pb3_hit_callback);
-    pb4.attach_deasserted(&pb4_hit_callback);
-    
-    pb1.setSampleFrequency();
-    pb2.setSampleFrequency();
-    pb3.setSampleFrequency();
-    pb4.setSampleFrequency();
+    uLCD.baudrate(3000000);
+    blue.attach(&bt_rec,Serial::RxIrq);
+
     
     reset=0; //hardware reset for 8266
     pc.baud(9600);  // set what you want here depending on your terminal program speed
@@ -119,19 +180,77 @@
     getTime();
     pc.printf("%s", buf);
     
-    timeDisp = timeFromResponse();
+    timeDisp = timeFromResponse() + 18;
     set_time(timeDisp);
-    pc.printf("%d", timeDisp);
-    timeDisp = timeDisp%86400;
-        while (1) {
+    
+    
+    
+    time_t alarmTime = sdalarm.getAlarm();
+    char timebuffer[80];
+    struct tm * timeinfo;
+    timeinfo = localtime (&alarmTime);
+    
+    strftime (timebuffer,80,"%H",timeinfo);
+    
+    hours = atoi(timebuffer);
+    strftime (timebuffer,80,"%M",timeinfo);
+    minutes = atoi(timebuffer);
+    
+    alarmThread.start(alarm_thread);
+    alarmSound.start(alarm_sound);
+
+    
+    while (1) {
+        time_t rawtime;
+        struct tm * timeinfo;
+        char buffer [80];
+        time (&rawtime);
+        global_time = rawtime;
+        timeinfo = localtime (&rawtime);
+        strftime (buffer,80,"%I:%M %p",timeinfo);
+        
+        if ((hours*60*60 + minutes*60) < (rawtime % (60*60*24))) {
+            alarmWentOff = true;
+        } else {
+            alarmWentOff = false;
+        }
         
-        //timeDisp++;
-        if(alarmSet&&int(alarmTimeSet/60)==int(timeDisp/60)){
-            uLCD.cls();
+        lcd_mutex.lock();
+        uLCD.locate(0,0);
+        uLCD.color(WHITE);
+        uLCD.text_width(2); 
+        uLCD.text_height(2);
+        uLCD.printf("%s", buffer);
+        lcd_mutex.unlock();
+        Thread::wait(500);
+        
+        if (global_time % (60*60*24) == 0) {
+            alarmWentOff = false;
+        }
+        if ((((int)global_time) % (60*60*24)) >= (hours*60*60 + minutes*60) && alarmSet && !alarmWentOff) {
+            one = !one;
+            lcd_mutex.lock();
+            uLCD.locate(2,6);
+            uLCD.color(RED);
+            uLCD.text_width(2); 
+            uLCD.text_height(2);
             uLCD.printf("ALARM");
-            }
+            lcd_mutex.unlock();
+            alarmWentOff = true;
+            play = true;
+        } else {
+            play = false;
+            lcd_mutex.lock();
+            uLCD.locate(2,6);
+            uLCD.color(BLACK);
+            uLCD.text_width(2); 
+            uLCD.text_height(2);
+            uLCD.printf("ALARM");
+            lcd_mutex.unlock();
+        }
         
-        wait(.5);
+        
+        
     }
  
 }