Code written by Paul Rabbat, Angel Daruna, Jarel Hawkins, and Cordel Williams

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

Fork of ECE4180_FinalProject by Angel Daruna

Revision:
8:548f413118e9
Parent:
7:b0f2169bdf48
Child:
9:add6ce18f1b7
--- a/main.cpp	Tue Dec 02 14:18:16 2014 +0000
+++ b/main.cpp	Tue Dec 02 17:12:59 2014 +0000
@@ -1,8 +1,11 @@
 // Includes
+#include "SDFileSystem.h"
+#include "PinDetect.h"
 #include "mbed.h"
 #include "NTPClient.h"
 #include "uLCD_4DGL.h"
 #include "EthernetInterface.h"
+#include <vector>
 #include <string>
 #include "mbed_rpc.h"
 #include "RPCCommand.h"
@@ -11,6 +14,7 @@
 #include "RequestHandler.h"
 #include "RPCType.h"
 #include "alarmContainer.h"
+#include "wave_player.h"
 
 // Defines
 #define SERVER_PORT 80 //HTTPSERVER
@@ -25,7 +29,27 @@
 DigitalOut led2(LED2); // led 2 indicates delay time for interrupts
 DigitalOut led3(LED3); // Indicates the alarm has been set 
 DigitalOut led4(LED4);  // Indiciates the alarm is going off
+
+//Jarel's globals
 int alarmSignaled = 0;
+PwmOut alarm(p25);
+AnalogOut DACout(p18);
+PinDetect snooze(p23);
+PinDetect stop(p24);
+SDFileSystem sd(p5, p6, p7, p8, p12, "sd"); //SD card
+FILE *wave_file;
+string filepath = "/sd/mymusic/";
+
+
+volatile bool t = true;
+volatile bool play = true;
+volatile float vol = .52;
+volatile bool insert;
+volatile int index;
+wave_player waver(&DACout);
+int num = 0;
+vector<string> filenames; //filenames are stored in a vector string
+
 
 // pin for ZeroCross tail input
 // An external 1K pullup required
@@ -48,27 +72,50 @@
 HTTPServer create_simple_server();
 HTTPServer create_interactive_server();
 
+//New Jarel's
+void play_wake_up(void);
+void stop_hit_callback(void);
+void snooze_hit_callback(void);
+void read_file_names(char *dir);
+
 void thread_server(void const *args);
 void thread_display(void const *args);
 void thread_alarm(void const *args);
 void triggerOn();
 void dimmer();
 void turn_on();
+void getAndParseWeather();
 
 /** Weather **/
 #include "HTTPClient.h"
-#include "spdomparser.hpp"
-#include "spxmlnode.hpp"
-#include "spxmlhandle.hpp"
-void getWeather();
+//#include "spdomparser.hpp"
+//#include "spxmlnode.hpp"
+//#include "spxmlhandle.hpp"
+char* getWeather();
 void getTime();
-void parseResults();
-char buf[3000];
+void parseResults(char*);
 string weatherCondition = "";
 string lowTemperature = "";
 string highTemperature = "";
 
 int main() {
+    snooze.mode(PullUp);
+    stop.mode(PullUp);
+    wait(.01);
+    snooze.attach_deasserted(&snooze_hit_callback);
+    stop.attach_deasserted(&stop_hit_callback);
+    stop.setSampleFrequency(); 
+
+    if(insert = sd.SD_inserted()){
+        printf("Inserted worked.");
+    }
+    printf("Attempting to read file names");
+    read_file_names("/sd/myMusic");
+    printf("File names read");
+    
+    wave_file = fopen( (filepath+filenames[index]).c_str(), "r");
+    printf("Wave file opened");
+    
     dim = -1.0;
     printf("Getting IP Address.. ");
     uLCD.baudrate(2000000);
@@ -99,9 +146,7 @@
     
     printf("IP Address is %s\n", eth.getIPAddress());
     
-    getWeather();
-    wait(1);
-    parseResults();
+    getAndParseWeather();
     wait(1);
     getTime();
     wait(1);
@@ -123,8 +168,15 @@
     }
 }
 
-void getWeather(){
+void getAndParseWeather(){
+    char* buf = getWeather();
+    wait(1);
+    parseResults(buf);    
+}
+
+char* getWeather(){
     /*** WEATHER****/
+    char buf[3000];
     printf("Getting weather..\n");
     HTTPClient http; 
     int retHttp = http.get("http://weather.yahooapis.com/forecastrss?w=2357024&u=f", buf, sizeof(buf));
@@ -141,19 +193,19 @@
         break;
     default:
         printf("Error\n");                        
-    }        
+    }
+    
+    return buf;        
 }
 
-void parseResults(){
-    printf("Starting parser\n");
-    SP_XmlDomParser parser;    
+void parseResults(char* buf){
+    printf("Starting parser\n");  
     int i=0;
     //printf("\n----------%s----------\n",buf);
     char* chars_array = strtok(buf, "\"");
     while(chars_array)
     {
         //printf("%d: %s\n", i, chars_array);
-        
         if(i==63){
             lowTemperature = chars_array;
             printf("Low temp: %s F\n", lowTemperature);
@@ -171,34 +223,6 @@
         chars_array = strtok(NULL, "\"");
     }
     
-    
-    /*parser.append( buf, strlen(buf)); // stream current buffer data to the XML parser
-    printf("Parser appended\n");    
-
-    
-    SP_XmlHandle rootHandle( parser.getDocument()->getRootElement() );
-    
-    SP_XmlElementNode * location = rootHandle.getChild( "channel" ).getChild( "yweather:location" ).toElement();
-    if (location) {
-        printf("\r\n === Location:%s === \r\n",location->getAttrValue("city"));
-    }
-                 
-    SP_XmlElementNode * forecast;
-    
-    forecast = rootHandle.getChild( "channel" ).getChild("item").getChild( "yweather:forecast",0).toElement();
-    if (forecast) {
-        printf("\r\n ----- Date:%s(%s) ----- \r\n",forecast->getAttrValue("date"),forecast->getAttrValue("day"));
-        printf("Condition:%s \n",forecast->getAttrValue("text"));
-        printf("Temp:Low%sF High%sF\n",forecast->getAttrValue("low"),forecast->getAttrValue("high"));
-    }        
-    
-    forecast = rootHandle.getChild( "channel" ).getChild("item").getChild( "yweather:forecast",1).toElement();    
-    if (forecast) {
-        printf("\r\n ----- Date:%s(%s) ----- \r\n",forecast->getAttrValue("date"),forecast->getAttrValue("day"));        
-        printf("Condition:%s \n",forecast->getAttrValue("text"));
-        printf("Temp:Low%sF High%sF\n",forecast->getAttrValue("low"),forecast->getAttrValue("high"));
-    }      */
-    
     printf("Parser complete!\n");
 }
 
@@ -223,6 +247,7 @@
             if(_alarm.hours == hour && _alarm.minutes == minute && _alarm.amPm == ampm){
                 alarmSignaled = 1;
                 turn_on();
+                play_wake_up();
                 led4 = 1;
             }
         }
@@ -287,11 +312,11 @@
         
         uLCD.printf("    %s, %s %s\n    %02d:%02d:%02d %s\n", dayOfWeek, month, dayNum, hour, minute, sec, ampm);
         uLCD.color(BLUE);
-        uLCD.printf("    Low: %s F\n    High: %s F\n    Weather:\n  %s\n", lowTemperature, highTemperature, weatherCondition);
+        uLCD.printf("    Low: %s F\n    High: %s F\n    Weather:\n  %s", lowTemperature, highTemperature, weatherCondition);
         if(_alarm.alarmSet){
+            uLCD.locate(0,6);
             uLCD.color(GREEN);
-            if(_alarm.minutes < 10){ uLCD.printf("    Alarm:\n    %d:0%d %s", _alarm.hours, _alarm.minutes, _alarm.amPm); }
-            else{ uLCD.printf("\n\n    Alarm:\n    %d:%d %s", _alarm.hours, _alarm.minutes, _alarm.amPm); }
+            uLCD.printf("    Alarm:\n    %d:%02d %s", _alarm.hours, _alarm.minutes, _alarm.amPm);
         }
         
         Thread::wait(100);
@@ -355,4 +380,84 @@
         wait(0.10);
     }
     dim = 1.0;
+}
+
+void stop_hit_callback(void){
+    //alarm = 0.0;
+    //state = off;
+   vol = 0.0;
+   //wait(1000);
+   t = false;
+   if (insert == true){
+       play =! play;
+       }
+}
+
+void snooze_hit_callback(void) {
+    alarm = 0.0;
+    wait(20);
+    if (insert == true) {
+        play =! play;
+        wait(20);
+        play =! play;
+    }
+}
+
+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));
+        num++;
+    }
+    num= num;
+}
+
+void play_wake_up(void){
+    while(t == true) {
+        printf("play_wake_up started");
+        if ((insert == true) && (play ==true)) {
+                printf("File names read.");
+                //string str2 = filenames[index].substr(0,filenames[index].length()-4);
+                waver.play(wave_file, &play);
+                printf("Wav file playing");
+               if (feof(wave_file)) // when playback ends file will close and play will equal false
+                {
+                fclose(wave_file);
+                printf("Wav ended.");
+                //play=!play;
+                } 
+                 
+            }
+        if ((insert == false)&&(play == true)) {
+            // generate a 500Hz tone using PWM hardware output
+            printf("Playing tune sound.");
+            for (int j=0; j<26; j=j+2) {
+                alarm.period(1.0/500.0); // 500hz period
+                alarm =vol; //50% duty cycle - max volume
+                wait(.5);
+                //alarm=0.0; // turn off audio
+                alarm.period(0.0/500.0);
+                wait(.5);
+                } // end of for
+        
+            // two tone police siren effect -  two periods or two frequencies
+            // increase volume - by changing the PWM duty cycle
+            for (int i=0; i<26; i=i+2) {
+                alarm = vol;
+                alarm.period(1.0/969.0);
+                wait(.5);
+                alarm.period(1.0/800.0);
+                wait(.5);
+              } //end of for
+        
+            //alarm = 0.0;
+        } //end of if
+
+    }// end of while
+    alarm = 0.0;
+    printf("Alarm sound turned off.");
 }
\ No newline at end of file