Clock

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

Fork of Internet_LCD_Clock by jim hamblen

Revision:
5:818735a07b88
Parent:
3:b4e7f126c80a
Child:
6:e08b5dbdfbcf
--- a/Internet_LCD_Clock.cpp	Mon Apr 11 17:58:47 2016 +0000
+++ b/Internet_LCD_Clock.cpp	Wed Apr 20 20:28:20 2016 +0000
@@ -1,52 +1,105 @@
 #include "mbed.h"
-#include "EthernetNetIf.h"
+//#include "EthernetNetIf.h"
 #include "NTPClient.h"
+#include "EthernetInterface.h"
 #include "uLCD_4DGL.h"
 #include "Speaker.h"
 #include "SDFileSystem.h"
 #include "wave_player.h"
+#include "PinDetect.h"
+
+
 #include <vector>
 #include <string>
 
+// Parameters
+char* domain_name = "0.uk.pool.ntp.org";
+int port_number = 123;
+
+// Networking
+EthernetInterface eth;
+NTPClient ntp_client;
+
 // Internet of Things clock example: LCD time is set via internet NTP time server
-uLCD_4DGL lcd(p28,p27,p29); // create a global LCD object
-EthernetNetIf eth;
-NTPClient ntp;
-SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card
+uLCD_4DGL uLCD(p28,p27,p29); // create a global LCD object
+//EthernetNetIf eth;
+//NTPClient ntp;
+SDFileSystem sd(p11, p12, p13, p14, "sd"); //SD card
 AnalogOut DACout(p18);  //speaker
 wave_player waver(&DACout);
-PinDetect snooze(p15);  //snooze button
-PinDetect off(p16);     //turn alarm off
+PinDetect snooze(p19);  //snooze button
+PinDetect off(p20);     //turn alarm off
+DigitalIn sdd(p12);
+DigitalOut myled1(LED1);
+DigitalOut myled2(LED2);
 
-#define snoozeTime 420
+#define snoozeTime 60
 
 //Global Variables
 //variables for SD sound
-vector <string> filenames; 
+vector <string> filenames;
 int current = 0;
-bool play = false; sd_insert = false;
+bool playing = false;
+bool sd_insert = false;
 static int veclen = 5;
 FILE *wave_file;
-//variables for alarm 
+//variables for alarm
 //system time structure
 time_t ctTime; //ctTime = current time
-// Base alarm time-24 hour clock 
+// Base alarm time-24 hour clock
 int baseAlarmHour = 0; //0-23
-int baseAlarmMin = 0; 
+int baseAlarmMin = 0;
 // Current alarm time
-int curAlarmHour = 0; //0-23
-int curAlarmMin = 0; 
+int curAlarmHour = 18; //0-23
+int curAlarmMin = 9;
+//SongPlayer mySpeaker(p26);
+Ticker flippera;
+
+//alarm tone
+float note[17]= {1568.0,1396.9,1244.5,1244.5,1396.9,1568.0,1568.0,1568.0,1396.9,
+                 1244.5,1396.9,1568.0,1396.9,1244.5,1174.7,1244.5,1244.5,
+                };
+float duration[17]= {0.48,0.24,0.72,0.48,0.24,0.48,0.24,0.24,0.24,
+                     0.24,0.24,0.24,0.24,0.48,0.24,0.48,0.48
+                    };
+
+void flip_time()
+{
+    myled1 = !myled1;
+    char time_buffer[80];
+    uLCD.locate(0, 1);
+    ctTime = time(NULL);
+    strftime(time_buffer, 80, "    %a %b %d\n    %T %p %z\n    %Z\n", \
+             localtime(&ctTime));
+    uLCD.printf("    UTC/GMT:\n%s", time_buffer);
+}
+
+void thread_time(void const *args)
+{
+    while(1) {
+        myled1 = !myled1;
+        char time_buffer[80];
+        uLCD.locate(0, 1);
+        ctTime = time(NULL);
+        strftime(time_buffer, 80, "    %a %b %d\n    %T %p %z\n    %Z\n", \
+                 localtime(&ctTime));
+        uLCD.printf("    UTC/GMT:\n%s", time_buffer);
+        Thread::wait(1000);
+    }
+}
+
 
 //Check for SD Card
-void sd_check(){
+void sd_check()
+{
     int sdPre = sdd.read();
-    while (sdPre == 0){
-        lcd.locate(0,0);
-        lcd.printf("Insert SD card");
+    while (sdPre == 0) {
+        uLCD.locate(0,0);
+        uLCD.printf("Insert SD card");
         sdPre = sdd.read();
         wait (.5);
     }
-    lcd.cls();
+    uLCD.cls();
 }
 
 //Read File Names
@@ -64,12 +117,14 @@
 //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){
+    bool* play_point = &playing;
+    //string file_name("/sd/");
+    //file_name += filenames[current];
+    // wave_file = fopen(file_name.c_str(),"r");
+    wave_file=fopen("/sd/bob.wav","r");
+    while(playing) {
         waver.play(wave_file, play_point);
+       // mySpeaker.PlaySong(note,duration);
     }
     fclose(wave_file);
 }
@@ -77,75 +132,103 @@
 //Interrupt-Snooze Function
 void snooze_hit_callback (void)
 {
-    play = false;
-    time_t = newtime;
+    myled1 = !myled1;
+    playing = false;
+    time_t newtime;
     struct tm * timeinfo;
-    
+
     newtime = ctTime + snoozeTime;
+//    time (&newtime);
     timeinfo = localtime (&newtime);
-    curAlarmMin = timeinfo.tm_min;
-    curAlarmHour = timeinfo.tm_hour;
+    curAlarmMin = timeinfo->tm_min;
+    curAlarmHour = timeinfo->tm_hour;
 }
 
-//Interrupt- Off Function 
+//Interrupt- Off Function
 void off_hit_callback (void)
 {
-    play = false;
+    myled2 = !myled2;
+    playing = 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();
-  }
-}  
+    struct tm * timeinfo;
+
+    //time(&ctTime);
+    timeinfo = localtime (&ctTime);
+    if (timeinfo->tm_min == curAlarmMin && timeinfo->tm_hour == curAlarmHour) {
+        playing = true;
+        play_file();
+
+    }
+}
 
 
-int main() {
+int main()
+{
 
     snooze.mode(PullUp);
     off.mode(PullUp);
-    wait(.01);
+    wait(0.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;
-    lcd.printf("Get IP addr...");
-    EthernetErr ethErr = eth.setup();
-    //Get an Internet IP address using DHCP
-    if (ethErr) {
-        //error or timeout getting an IP address
-        lcd.cls();
-        lcd.printf("Network Error \n\r %d",ethErr);
+
+    //time_t ct_time;
+
+
+    // Initialize LCD
+    uLCD.baudrate(115200);
+    uLCD.background_color(BLACK);
+    uLCD.cls();
+
+    // Connect to network and wait for DHCP
+    uLCD.locate(0,0);
+    uLCD.printf("Getting IP Address\n");
+    eth.init();
+    if ( eth.connect(60000) == -1 ) {
+        uLCD.printf("ERROR: Could not\nget IP address");
         return -1;
     }
-    lcd.cls();
-    lcd.printf("Reading Time...\n\r");
-    //specify time server URL
-    Host server(IpAddr(), 123, "0.uk.pool.ntp.org");
-    //Read time from server
-    ntp.setTime(server);
-    lcd.printf("Time set");
-    //Delay for human time to read LCD display
+    uLCD.printf("IP address is \n%s\n\n",eth.getIPAddress());
     wait(1);
+
+    // Read time from server
+    uLCD.printf("Reading time...\n\r");
+    ntp_client.setTime(domain_name, port_number);
+    uLCD.printf("Time set\n");
+    wait(2);
+    eth.disconnect();
+
+    // Reset LCD
+    uLCD.background_color(WHITE);
+    uLCD.textbackground_color(WHITE);
+    uLCD.color(RED);
+    uLCD.cls();
+    uLCD.text_height(2);
+
+    // flippera.attach(&flip_time, 1.0);
+    Thread time_thread(thread_time);
+
+    // Loop and update clock
     while (1) {
-        // loop and periodically update the LCD's time display
-        lcd.cls();
+        /*
+        myled1 = !myled1;
+        char time_buffer[80];
+        uLCD.locate(0, 1);
         ctTime = time(NULL);
-        lcd.printf("UTC:  %s", ctime(&ctTime));
+        strftime(time_buffer, 80, "    %a %b %d\n    %T %p %z\n    %Z\n", \
+                 localtime(&ctTime));
+        uLCD.printf("    UTC/GMT:\n%s", time_buffer);
+        */
         timeCompare();
-        wait(.25);
+        wait(0.1);
+        
     }
+
+
 }