Clock

Dependencies:   4DGL-uLCD-SE EthernetInterface NTPClient TextLCD mbed PinDetect SDFileSystem wave_player mbed-rtos

Fork of Internet_LCD_Clock by jim hamblen

Revision:
3:b4e7f126c80a
Parent:
1:09fcc9b81f23
Child:
5:818735a07b88
--- a/Internet_LCD_Clock.cpp	Mon Apr 11 16:23:49 2016 +0000
+++ b/Internet_LCD_Clock.cpp	Mon Apr 11 17:56:48 2016 +0000
@@ -1,15 +1,124 @@
 #include "mbed.h"
 #include "EthernetNetIf.h"
 #include "NTPClient.h"
-#include "TextLCD.h"
+#include "uLCD_4DGL.h"
+#include "Speaker.h"
+#include "SDFileSystem.h"
+#include "wave_player.h"
+#include <vector>
+#include <string>
+
 // Internet of Things clock example: LCD time is set via internet NTP time server
-TextLCD lcd(p15, p16, p17, p18, p19, p20); // rs, e, d0-d3
+uLCD_4DGL lcd(p28,p27,p29); // create a global LCD object
 EthernetNetIf eth;
 NTPClient ntp;
+SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card
+AnalogOut DACout(p18);  //speaker
+wave_player waver(&DACout);
+PinDetect snooze(p15);  //snooze button
+PinDetect off(p16);     //turn alarm off
+
+#define snoozeTime 420
+
+//Global Variables
+//variables for SD sound
+vector <string> filenames; 
+int current = 0;
+bool play = false; sd_insert = false;
+static int veclen = 5;
+FILE *wave_file;
+//variables for alarm 
+//system time structure
+time_t ctTime; //ctTime = current time
+// Base alarm time-24 hour clock 
+int baseAlarmHour = 0; //0-23
+int baseAlarmMin = 0; 
+// Current alarm time
+int curAlarmHour = 0; //0-23
+int curAlarmMin = 0; 
+
+//Check for SD Card
+void sd_check(){
+    int sdPre = sdd.read();
+    while (sdPre == 0){
+        lcd.locate(0,0);
+        lcd.printf("Insert SD card");
+        sdPre = sdd.read();
+        wait (.5);
+    }
+    lcd.cls();
+}
+
+//Read File Names
+void read_file_names(char *dir)
+{
+    DIR *dp;
+    struct dirent *dirp;
+    dp = opendir(dir);
+    //read all directory and file names in current directory into filename vector
+    while((dirp = readdir(dp)) != NULL) {
+        filenames.push_back(string(dirp->d_name));
+    }
+}
+
+//Play file from SD card
+void play_file()
+{
+    bool* play_point = &play;
+    string file_name("/sd/");
+    file_name += filenames[current];
+    wave_file = fopen(file_name.c_str(),"r");
+    while(play){
+        waver.play(wave_file, play_point);
+    }
+    fclose(wave_file);
+}
+
+//Interrupt-Snooze Function
+void snooze_hit_callback (void)
+{
+    play = false;
+    time_t = newtime;
+    struct tm * timeinfo;
+    
+    newtime = ctTime + snoozeTime;
+    timeinfo = localtime (&newtime);
+    curAlarmMin = timeinfo.tm_min;
+    curAlarmHour = timeinfo.tm_hour;
+}
+
+//Interrupt- Off Function 
+void off_hit_callback (void)
+{
+    play = false;
+    curAlarmMin = baseAlarmMin;
+    curAlarmHour = baseAlarmHour;
+}
+//Time Compare Function
+void timeCompare()
+{
+  struct tm * timeinfo;
+  
+  //time(&ctTime);
+  timeinfo = localtime (&ctTime);
+  if (timeinfo.tm_min == curAlarmMin && timeinfo.tm_hour == curAlarmHour)
+  {
+      play = true;
+      play_file();
+  }
+}  
+
 
 int main() {
-//system time structure
-    time_t ctTime;
+
+    snooze.mode(PullUp);
+    off.mode(PullUp);
+    wait(.01);
+    snooze.attach_deasserted(&snooze_hit_callback);
+    off.attach_deasserted(&off_hit_callback);
+    snooze.setSampleFrequency();
+    off.setSampleFrequency();
+    
     //clear LCD
     lcd.cls();
     // lcd.printf prints to LCD display;
@@ -36,6 +145,7 @@
         lcd.cls();
         ctTime = time(NULL);
         lcd.printf("UTC:  %s", ctime(&ctTime));
+        timeCompare();
         wait(.25);
     }
 }