4180 / Mbed 2 deprecated 4180_Final_Project_WaveProblems

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

Fork of 4180_Final_Project by 4180

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 //WavePlayer_HelloWorld4180
00002 //internet_clock
00003 
00004 #include "mbed.h"
00005 #include "SDFileSystem.h"
00006 #include "wave_player.h"
00007 #include "EthernetInterface.h"
00008 #include "NTPClient.h"
00009 #include "uLCD_4DGL.h"
00010 #include "rtos.h"
00011 #include "PinDetect.h"
00012 
00013 //pinouts
00014 SDFileSystem sd(p11, p12, p13, p14, "sd"); //SD card
00015 AnalogOut DACout(p18);
00016 wave_player waver(&DACout);
00017 PinDetect snooze(p19);  //snooze button
00018 PinDetect off(p20);     //turn alarm off
00019 DigitalOut myled1(LED1);
00020 DigitalOut myled2(LED2);
00021 DigitalOut myled3(LED3);
00022 
00023 // Parameters
00024 char* domain_name = "0.uk.pool.ntp.org";
00025 int port_number = 123;
00026 
00027 // Networking
00028 EthernetInterface eth;
00029 NTPClient ntp_client;
00030 
00031 // Graphic LCD - TX, RX, and RES pins
00032 uLCD_4DGL uLCD(p28,p27,p29);
00033 
00034 #define snoozeTime 10
00035 
00036 //global variables
00037 time_t ct_time;
00038 // Base alarm time-24 hour clock
00039 int baseAlarmHour = 0; //0-23
00040 int baseAlarmMin = 0;
00041 // Current alarm time
00042 int curAlarmHour = 18; //0-23
00043 int curAlarmMin = 9;
00044 bool play = true;
00045 
00046 //time thread
00047 void time_thread(void const *args)
00048 {
00049     char time_buffer[80];
00050     // Loop and update clock
00051     while (1) {
00052         uLCD.locate(0, 1);
00053         ct_time = time(NULL);
00054         strftime(time_buffer, 80, "    %a %b %d\n    %T %p %z\n    %Z\n", \
00055                  localtime(&ct_time));
00056         uLCD.printf("    UTC/GMT:\n%s", time_buffer);
00057         Thread::wait(100);
00058     }
00059 }
00060 
00061 //pushbutton (p19)
00062 void snooze_hit_callback (void)
00063 {
00064     myled1 = !myled1;
00065     play = false;
00066     time_t newtime;
00067     struct tm * timeinfo;
00068     newtime = ct_time + snoozeTime;
00069 //time (&newtime);
00070     timeinfo = localtime (&newtime);
00071     curAlarmMin = timeinfo->tm_min;
00072     curAlarmHour = timeinfo->tm_hour;
00073 }
00074 
00075 void off_hit_callback (void)
00076 {
00077     myled2 = !myled2;
00078     play = false;
00079     curAlarmMin = baseAlarmMin;
00080     curAlarmHour = baseAlarmHour;
00081 }
00082 
00083 void play_file()
00084 {
00085     bool* play_point = &play;
00086     FILE *wave_file;
00087     printf("\n\n\nHello, wave world!\n");
00088     wave_file=fopen("/sd/bob.wav","r");
00089     waver.play(wave_file, play_point);
00090     fclose(wave_file);
00091 }
00092 
00093 void timeCompare()
00094 {
00095     struct tm * timeinfo;
00096     timeinfo = localtime (&ct_time);
00097     if (timeinfo->tm_min == curAlarmMin && timeinfo->tm_hour == curAlarmHour) {
00098         play = true;
00099         myled3 = true;
00100         play_file();
00101     }
00102 }
00103 
00104 int main()
00105 {
00106     snooze.mode(PullUp);
00107     off.mode(PullUp);
00108     wait(0.01);
00109     snooze.attach_deasserted(&snooze_hit_callback);
00110     off.attach_deasserted(&off_hit_callback);
00111     snooze.setSampleFrequency();
00112     off.setSampleFrequency();
00113 
00114     play_file();
00115 
00116 
00117     // Initialize LCD
00118     uLCD.baudrate(115200);
00119     uLCD.background_color(BLACK);
00120     uLCD.cls();
00121 
00122     // Connect to network and wait for DHCP
00123     uLCD.locate(0,0);
00124     uLCD.printf("Getting IP Address\n");
00125     eth.init();
00126     if ( eth.connect(60000) == -1 ) {
00127         uLCD.printf("ERROR: Could not\nget IP address");
00128         return -1;
00129     }
00130     uLCD.printf("IP address is \n%s\n\n",eth.getIPAddress());
00131     Thread::wait(1000);
00132 
00133     // Read time from server
00134     uLCD.printf("Reading time...\n\r");
00135     ntp_client.setTime(domain_name, port_number);
00136     uLCD.printf("Time set\n");
00137     Thread::wait(2000);
00138     eth.disconnect();
00139 
00140     // Reset LCD
00141     uLCD.background_color(WHITE);
00142     uLCD.textbackground_color(WHITE);
00143     uLCD.color(RED);
00144     uLCD.cls();
00145     uLCD.text_height(2);
00146 
00147     Thread thread_time(time_thread);
00148     while(!myled3) {
00149         timeCompare();
00150         Thread::wait(100);
00151     }
00152 
00153 }