wave cancel

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

Fork of 4180_Final_Project_WaveProblems by 4180

Committer:
agamemaker
Date:
Sun Apr 24 19:39:01 2016 +0000
Revision:
9:fbb5f22fc299
Parent:
8:4750e5fd45c1
kgh

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ashea6 2:4d370e3f4618 1 //WavePlayer_HelloWorld4180
ashea6 2:4d370e3f4618 2 //internet_clock
4180_1 1:5b8e223e983d 3
4180_1 1:5b8e223e983d 4 #include "mbed.h"
4180_1 1:5b8e223e983d 5 #include "SDFileSystem.h"
4180_1 1:5b8e223e983d 6 #include "wave_player.h"
ashea6 2:4d370e3f4618 7 #include "EthernetInterface.h"
ashea6 2:4d370e3f4618 8 #include "NTPClient.h"
ashea6 2:4d370e3f4618 9 #include "uLCD_4DGL.h"
ashea6 2:4d370e3f4618 10 #include "rtos.h"
ashea6 3:5f0b28699a67 11 #include "PinDetect.h"
agamemaker 8:4750e5fd45c1 12 #include "HTTPClient.h"
4180_1 1:5b8e223e983d 13
ashea6 5:3c880df67e2e 14 //pinouts
agamemaker 8:4750e5fd45c1 15 // Graphic LCD - TX, RX, and RES pins
agamemaker 8:4750e5fd45c1 16 uLCD_4DGL uLCD(p28,p27,p29);
ashea6 2:4d370e3f4618 17 SDFileSystem sd(p11, p12, p13, p14, "sd"); //SD card
4180_1 1:5b8e223e983d 18 AnalogOut DACout(p18);
4180_1 1:5b8e223e983d 19 wave_player waver(&DACout);
ashea6 3:5f0b28699a67 20 PinDetect snooze(p19); //snooze button
ashea6 3:5f0b28699a67 21 PinDetect off(p20); //turn alarm off
ashea6 3:5f0b28699a67 22 DigitalOut myled1(LED1);
ashea6 3:5f0b28699a67 23 DigitalOut myled2(LED2);
ashea6 5:3c880df67e2e 24 DigitalOut myled3(LED3);
agamemaker 8:4750e5fd45c1 25 //Mutex lcd_mutex;
4180_1 1:5b8e223e983d 26
ashea6 2:4d370e3f4618 27 // Parameters
agamemaker 8:4750e5fd45c1 28 char* time_domain_name = "0.uk.pool.ntp.org";
agamemaker 9:fbb5f22fc299 29 //char* weth_domain_name = "http://weather.yahooapis.com/forecastrss?w=2502265";
agamemaker 9:fbb5f22fc299 30 //char* weth_domain_name = "https://query.yahooapis.com/v1/public/yql?q=select%20item.condition%20from%20weather.forecast%20where%20woeid%20in%20%28select%20woeid%20from%20geo.places%281%29%20where%20text%3D%22atlanta%2C%20ga%22%29&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";
agamemaker 8:4750e5fd45c1 31 char* weth_domain_name = "https://query.yahooapis.com/v1/public/yql?q=select%20item.condition%20from%20weather.forecast%20where%20woeid%20in%20%28select%20woeid%20from%20geo.places%281%29%20where%20text%3D%22atlanta%2C%20ga%22%29&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";
ashea6 2:4d370e3f4618 32 int port_number = 123;
ashea6 2:4d370e3f4618 33
ashea6 2:4d370e3f4618 34 // Networking
ashea6 2:4d370e3f4618 35 EthernetInterface eth;
ashea6 2:4d370e3f4618 36 NTPClient ntp_client;
ashea6 2:4d370e3f4618 37
ashea6 5:3c880df67e2e 38 #define snoozeTime 10
ashea6 5:3c880df67e2e 39
ashea6 2:4d370e3f4618 40 //global variables
ashea6 2:4d370e3f4618 41 time_t ct_time;
ashea6 5:3c880df67e2e 42 // Base alarm time-24 hour clock
ashea6 5:3c880df67e2e 43 int baseAlarmHour = 0; //0-23
ashea6 5:3c880df67e2e 44 int baseAlarmMin = 0;
ashea6 5:3c880df67e2e 45 // Current alarm time
ashea6 5:3c880df67e2e 46 int curAlarmHour = 18; //0-23
ashea6 5:3c880df67e2e 47 int curAlarmMin = 9;
ashea6 6:8e926bbfaec0 48 bool play = true;
ashea6 2:4d370e3f4618 49
ashea6 3:5f0b28699a67 50 //time thread
ashea6 2:4d370e3f4618 51 void time_thread(void const *args)
ashea6 2:4d370e3f4618 52 {
ashea6 2:4d370e3f4618 53 char time_buffer[80];
ashea6 5:3c880df67e2e 54 // Loop and update clock
ashea6 2:4d370e3f4618 55 while (1) {
agamemaker 8:4750e5fd45c1 56 //lcd_mutex.lock();
ashea6 2:4d370e3f4618 57 uLCD.locate(0, 1);
ashea6 2:4d370e3f4618 58 ct_time = time(NULL);
ashea6 2:4d370e3f4618 59 strftime(time_buffer, 80, " %a %b %d\n %T %p %z\n %Z\n", \
ashea6 2:4d370e3f4618 60 localtime(&ct_time));
ashea6 2:4d370e3f4618 61 uLCD.printf(" UTC/GMT:\n%s", time_buffer);
agamemaker 8:4750e5fd45c1 62 //lcd_mutex.unlock();
ashea6 2:4d370e3f4618 63 Thread::wait(100);
ashea6 2:4d370e3f4618 64 }
ashea6 2:4d370e3f4618 65 }
ashea6 2:4d370e3f4618 66
agamemaker 8:4750e5fd45c1 67 void parseWeather(char* buf)
agamemaker 8:4750e5fd45c1 68 {
agamemaker 8:4750e5fd45c1 69 char * pch;
agamemaker 8:4750e5fd45c1 70 char * key;
agamemaker 8:4750e5fd45c1 71 pch = strtok (buf,"{");
agamemaker 8:4750e5fd45c1 72 while (pch != NULL) {
agamemaker 8:4750e5fd45c1 73 pch = strtok (NULL,"{");
agamemaker 8:4750e5fd45c1 74 }
agamemaker 8:4750e5fd45c1 75 pch = strtok (buf,",");
agamemaker 8:4750e5fd45c1 76 while (pch != NULL) {
agamemaker 8:4750e5fd45c1 77 key = strtok (pch,":");
agamemaker 8:4750e5fd45c1 78 if(strcmp(key, "\"text\"")) {
agamemaker 8:4750e5fd45c1 79 uLCD.printf("Condition = %s",pch);
agamemaker 8:4750e5fd45c1 80 } else if(strcmp(key, "\"temp\"")) {
agamemaker 8:4750e5fd45c1 81 uLCD.printf("temp = %s",pch);
agamemaker 8:4750e5fd45c1 82 }
agamemaker 8:4750e5fd45c1 83 }
agamemaker 8:4750e5fd45c1 84 }
agamemaker 8:4750e5fd45c1 85
agamemaker 8:4750e5fd45c1 86 void getWeather()
agamemaker 8:4750e5fd45c1 87 {
agamemaker 8:4750e5fd45c1 88 //lcd_mutex.lock();
agamemaker 8:4750e5fd45c1 89 /*** WEATHER****/
agamemaker 8:4750e5fd45c1 90 char buf[500];
agamemaker 8:4750e5fd45c1 91 uLCD.printf("Getting weather..\n");
agamemaker 8:4750e5fd45c1 92 HTTPClient http;
agamemaker 8:4750e5fd45c1 93 int retHttp = http.get(weth_domain_name, buf, sizeof(buf));
agamemaker 8:4750e5fd45c1 94 uLCD.printf("%d", retHttp);
agamemaker 8:4750e5fd45c1 95 switch(retHttp) {
agamemaker 8:4750e5fd45c1 96 case HTTP_OK:
agamemaker 8:4750e5fd45c1 97 uLCD.printf("Read completely\n");
agamemaker 8:4750e5fd45c1 98 uLCD.printf("%c",buf);
agamemaker 8:4750e5fd45c1 99 wait(2);
agamemaker 8:4750e5fd45c1 100 uLCD.printf("%s",buf);
agamemaker 8:4750e5fd45c1 101 wait(2);
agamemaker 8:4750e5fd45c1 102 parseWeather(buf);
agamemaker 8:4750e5fd45c1 103 break;
agamemaker 8:4750e5fd45c1 104 case HTTP_TIMEOUT:
agamemaker 8:4750e5fd45c1 105 uLCD.printf("Connection Timeout\n");
agamemaker 8:4750e5fd45c1 106 break;
agamemaker 8:4750e5fd45c1 107 case HTTP_CONN:
agamemaker 8:4750e5fd45c1 108 uLCD.printf("Connection Error\n");
agamemaker 8:4750e5fd45c1 109 break;
agamemaker 8:4750e5fd45c1 110 default:
agamemaker 8:4750e5fd45c1 111 uLCD.printf("Error\n");
agamemaker 8:4750e5fd45c1 112 }
agamemaker 8:4750e5fd45c1 113 //lcd_mutex.unlock();
agamemaker 8:4750e5fd45c1 114 }
agamemaker 8:4750e5fd45c1 115
agamemaker 8:4750e5fd45c1 116 void weth_thread(void const *args)
agamemaker 8:4750e5fd45c1 117 {
agamemaker 8:4750e5fd45c1 118
agamemaker 8:4750e5fd45c1 119 Thread::wait(3600000);
agamemaker 8:4750e5fd45c1 120 }
agamemaker 8:4750e5fd45c1 121
ashea6 3:5f0b28699a67 122 //pushbutton (p19)
ashea6 3:5f0b28699a67 123 void snooze_hit_callback (void)
ashea6 3:5f0b28699a67 124 {
ashea6 5:3c880df67e2e 125 myled1 = !myled1;
ashea6 6:8e926bbfaec0 126 play = false;
ashea6 5:3c880df67e2e 127 time_t newtime;
ashea6 5:3c880df67e2e 128 struct tm * timeinfo;
ashea6 5:3c880df67e2e 129 newtime = ct_time + snoozeTime;
ashea6 5:3c880df67e2e 130 //time (&newtime);
ashea6 5:3c880df67e2e 131 timeinfo = localtime (&newtime);
ashea6 5:3c880df67e2e 132 curAlarmMin = timeinfo->tm_min;
ashea6 5:3c880df67e2e 133 curAlarmHour = timeinfo->tm_hour;
ashea6 3:5f0b28699a67 134 }
ashea6 3:5f0b28699a67 135
ashea6 3:5f0b28699a67 136 void off_hit_callback (void)
ashea6 3:5f0b28699a67 137 {
ashea6 3:5f0b28699a67 138 myled2 = !myled2;
ashea6 6:8e926bbfaec0 139 play = false;
ashea6 6:8e926bbfaec0 140 curAlarmMin = baseAlarmMin;
ashea6 6:8e926bbfaec0 141 curAlarmHour = baseAlarmHour;
ashea6 3:5f0b28699a67 142 }
ashea6 3:5f0b28699a67 143
ashea6 4:55035e20ae61 144 void play_file()
ashea6 4:55035e20ae61 145 {
ashea6 6:8e926bbfaec0 146 bool* play_point = &play;
ashea6 4:55035e20ae61 147 FILE *wave_file;
ashea6 4:55035e20ae61 148 printf("\n\n\nHello, wave world!\n");
ashea6 4:55035e20ae61 149 wave_file=fopen("/sd/bob.wav","r");
ashea6 6:8e926bbfaec0 150 waver.play(wave_file, play_point);
ashea6 4:55035e20ae61 151 fclose(wave_file);
ashea6 4:55035e20ae61 152 }
ashea6 5:3c880df67e2e 153
ashea6 5:3c880df67e2e 154 void timeCompare()
ashea6 5:3c880df67e2e 155 {
ashea6 5:3c880df67e2e 156 struct tm * timeinfo;
ashea6 5:3c880df67e2e 157 timeinfo = localtime (&ct_time);
ashea6 5:3c880df67e2e 158 if (timeinfo->tm_min == curAlarmMin && timeinfo->tm_hour == curAlarmHour) {
ashea6 6:8e926bbfaec0 159 play = true;
ashea6 6:8e926bbfaec0 160 myled3 = true;
ashea6 5:3c880df67e2e 161 play_file();
ashea6 5:3c880df67e2e 162 }
ashea6 5:3c880df67e2e 163 }
ashea6 5:3c880df67e2e 164
4180_1 1:5b8e223e983d 165 int main()
4180_1 1:5b8e223e983d 166 {
ashea6 3:5f0b28699a67 167 snooze.mode(PullUp);
ashea6 3:5f0b28699a67 168 off.mode(PullUp);
ashea6 3:5f0b28699a67 169 wait(0.01);
ashea6 3:5f0b28699a67 170 snooze.attach_deasserted(&snooze_hit_callback);
ashea6 3:5f0b28699a67 171 off.attach_deasserted(&off_hit_callback);
ashea6 3:5f0b28699a67 172 snooze.setSampleFrequency();
ashea6 3:5f0b28699a67 173 off.setSampleFrequency();
ashea6 2:4d370e3f4618 174
agamemaker 7:456add5c89e1 175 //play_file();
ashea6 2:4d370e3f4618 176
ashea6 2:4d370e3f4618 177
ashea6 2:4d370e3f4618 178 // Initialize LCD
ashea6 2:4d370e3f4618 179 uLCD.baudrate(115200);
ashea6 2:4d370e3f4618 180 uLCD.background_color(BLACK);
ashea6 2:4d370e3f4618 181 uLCD.cls();
ashea6 2:4d370e3f4618 182
ashea6 2:4d370e3f4618 183 // Connect to network and wait for DHCP
ashea6 2:4d370e3f4618 184 uLCD.locate(0,0);
ashea6 2:4d370e3f4618 185 uLCD.printf("Getting IP Address\n");
ashea6 2:4d370e3f4618 186 eth.init();
ashea6 2:4d370e3f4618 187 if ( eth.connect(60000) == -1 ) {
ashea6 2:4d370e3f4618 188 uLCD.printf("ERROR: Could not\nget IP address");
ashea6 2:4d370e3f4618 189 return -1;
ashea6 2:4d370e3f4618 190 }
ashea6 2:4d370e3f4618 191 uLCD.printf("IP address is \n%s\n\n",eth.getIPAddress());
ashea6 2:4d370e3f4618 192 Thread::wait(1000);
agamemaker 8:4750e5fd45c1 193 getWeather();
agamemaker 9:fbb5f22fc299 194 wait(500);
ashea6 2:4d370e3f4618 195 // Read time from server
ashea6 2:4d370e3f4618 196 uLCD.printf("Reading time...\n\r");
agamemaker 8:4750e5fd45c1 197 ntp_client.setTime(time_domain_name, port_number);
ashea6 2:4d370e3f4618 198 uLCD.printf("Time set\n");
ashea6 2:4d370e3f4618 199 Thread::wait(2000);
ashea6 2:4d370e3f4618 200 eth.disconnect();
ashea6 2:4d370e3f4618 201
ashea6 2:4d370e3f4618 202 // Reset LCD
ashea6 2:4d370e3f4618 203 uLCD.background_color(WHITE);
ashea6 2:4d370e3f4618 204 uLCD.textbackground_color(WHITE);
ashea6 2:4d370e3f4618 205 uLCD.color(RED);
ashea6 2:4d370e3f4618 206 uLCD.cls();
ashea6 2:4d370e3f4618 207 uLCD.text_height(2);
ashea6 2:4d370e3f4618 208
agamemaker 8:4750e5fd45c1 209 //Thread t1(time_thread);
agamemaker 8:4750e5fd45c1 210 //Thread t2(weth_thread);
agamemaker 7:456add5c89e1 211 while(true) {
ashea6 5:3c880df67e2e 212 timeCompare();
ashea6 5:3c880df67e2e 213 Thread::wait(100);
ashea6 5:3c880df67e2e 214 }
ashea6 4:55035e20ae61 215
4180_1 1:5b8e223e983d 216 }