wave cancel

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

Fork of 4180_Final_Project by Adam Zuravleff

Committer:
agamemaker
Date:
Mon Apr 25 15:48:53 2016 +0000
Revision:
10:6b27fafad047
Parent:
9:fbb5f22fc299
Child:
11:89e778e985d5
hellow;

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 10:6b27fafad047 30 //char* weth_domain_name = "http://developer.mbed.org/media/uploads/mbed_official/hello.txt";
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 5:3c880df67e2e 36 #define snoozeTime 10
ashea6 5:3c880df67e2e 37
ashea6 2:4d370e3f4618 38 //global variables
ashea6 2:4d370e3f4618 39 time_t ct_time;
ashea6 5:3c880df67e2e 40 // Base alarm time-24 hour clock
ashea6 5:3c880df67e2e 41 int baseAlarmHour = 0; //0-23
ashea6 5:3c880df67e2e 42 int baseAlarmMin = 0;
ashea6 5:3c880df67e2e 43 // Current alarm time
ashea6 5:3c880df67e2e 44 int curAlarmHour = 18; //0-23
ashea6 5:3c880df67e2e 45 int curAlarmMin = 9;
ashea6 6:8e926bbfaec0 46 bool play = true;
ashea6 2:4d370e3f4618 47
ashea6 3:5f0b28699a67 48 //time thread
ashea6 2:4d370e3f4618 49 void time_thread(void const *args)
ashea6 2:4d370e3f4618 50 {
ashea6 2:4d370e3f4618 51 char time_buffer[80];
ashea6 5:3c880df67e2e 52 // Loop and update clock
ashea6 2:4d370e3f4618 53 while (1) {
agamemaker 8:4750e5fd45c1 54 //lcd_mutex.lock();
ashea6 2:4d370e3f4618 55 uLCD.locate(0, 1);
ashea6 2:4d370e3f4618 56 ct_time = time(NULL);
ashea6 2:4d370e3f4618 57 strftime(time_buffer, 80, " %a %b %d\n %T %p %z\n %Z\n", \
ashea6 2:4d370e3f4618 58 localtime(&ct_time));
ashea6 2:4d370e3f4618 59 uLCD.printf(" UTC/GMT:\n%s", time_buffer);
agamemaker 8:4750e5fd45c1 60 //lcd_mutex.unlock();
ashea6 2:4d370e3f4618 61 Thread::wait(100);
ashea6 2:4d370e3f4618 62 }
ashea6 2:4d370e3f4618 63 }
ashea6 2:4d370e3f4618 64
agamemaker 8:4750e5fd45c1 65 void parseWeather(char* buf)
agamemaker 8:4750e5fd45c1 66 {
agamemaker 8:4750e5fd45c1 67 char * pch;
agamemaker 8:4750e5fd45c1 68 char * key;
agamemaker 8:4750e5fd45c1 69 pch = strtok (buf,"{");
agamemaker 8:4750e5fd45c1 70 while (pch != NULL) {
agamemaker 8:4750e5fd45c1 71 pch = strtok (NULL,"{");
agamemaker 8:4750e5fd45c1 72 }
agamemaker 8:4750e5fd45c1 73 pch = strtok (buf,",");
agamemaker 8:4750e5fd45c1 74 while (pch != NULL) {
agamemaker 8:4750e5fd45c1 75 key = strtok (pch,":");
agamemaker 8:4750e5fd45c1 76 if(strcmp(key, "\"text\"")) {
agamemaker 8:4750e5fd45c1 77 uLCD.printf("Condition = %s",pch);
agamemaker 8:4750e5fd45c1 78 } else if(strcmp(key, "\"temp\"")) {
agamemaker 8:4750e5fd45c1 79 uLCD.printf("temp = %s",pch);
agamemaker 8:4750e5fd45c1 80 }
agamemaker 8:4750e5fd45c1 81 }
agamemaker 8:4750e5fd45c1 82 }
agamemaker 8:4750e5fd45c1 83
agamemaker 8:4750e5fd45c1 84 void getWeather()
agamemaker 8:4750e5fd45c1 85 {
agamemaker 8:4750e5fd45c1 86 //lcd_mutex.lock();
agamemaker 8:4750e5fd45c1 87 /*** WEATHER****/
agamemaker 10:6b27fafad047 88 char buf[250];
agamemaker 8:4750e5fd45c1 89 uLCD.printf("Getting weather..\n");
agamemaker 8:4750e5fd45c1 90 HTTPClient http;
agamemaker 8:4750e5fd45c1 91 int retHttp = http.get(weth_domain_name, buf, sizeof(buf));
agamemaker 8:4750e5fd45c1 92 uLCD.printf("%d", retHttp);
agamemaker 8:4750e5fd45c1 93 switch(retHttp) {
agamemaker 8:4750e5fd45c1 94 case HTTP_OK:
agamemaker 8:4750e5fd45c1 95 uLCD.printf("Read completely\n");
agamemaker 8:4750e5fd45c1 96 uLCD.printf("%c",buf);
agamemaker 8:4750e5fd45c1 97 wait(2);
agamemaker 8:4750e5fd45c1 98 uLCD.printf("%s",buf);
agamemaker 8:4750e5fd45c1 99 wait(2);
agamemaker 8:4750e5fd45c1 100 parseWeather(buf);
agamemaker 8:4750e5fd45c1 101 break;
agamemaker 8:4750e5fd45c1 102 case HTTP_TIMEOUT:
agamemaker 8:4750e5fd45c1 103 uLCD.printf("Connection Timeout\n");
agamemaker 8:4750e5fd45c1 104 break;
agamemaker 8:4750e5fd45c1 105 case HTTP_CONN:
agamemaker 8:4750e5fd45c1 106 uLCD.printf("Connection Error\n");
agamemaker 8:4750e5fd45c1 107 break;
agamemaker 8:4750e5fd45c1 108 default:
agamemaker 8:4750e5fd45c1 109 uLCD.printf("Error\n");
agamemaker 8:4750e5fd45c1 110 }
agamemaker 8:4750e5fd45c1 111 //lcd_mutex.unlock();
agamemaker 8:4750e5fd45c1 112 }
agamemaker 8:4750e5fd45c1 113
agamemaker 8:4750e5fd45c1 114 void weth_thread(void const *args)
agamemaker 8:4750e5fd45c1 115 {
agamemaker 8:4750e5fd45c1 116
agamemaker 8:4750e5fd45c1 117 Thread::wait(3600000);
agamemaker 8:4750e5fd45c1 118 }
agamemaker 8:4750e5fd45c1 119
ashea6 3:5f0b28699a67 120 //pushbutton (p19)
ashea6 3:5f0b28699a67 121 void snooze_hit_callback (void)
ashea6 3:5f0b28699a67 122 {
ashea6 5:3c880df67e2e 123 myled1 = !myled1;
ashea6 6:8e926bbfaec0 124 play = false;
ashea6 5:3c880df67e2e 125 time_t newtime;
ashea6 5:3c880df67e2e 126 struct tm * timeinfo;
ashea6 5:3c880df67e2e 127 newtime = ct_time + snoozeTime;
ashea6 5:3c880df67e2e 128 //time (&newtime);
ashea6 5:3c880df67e2e 129 timeinfo = localtime (&newtime);
ashea6 5:3c880df67e2e 130 curAlarmMin = timeinfo->tm_min;
ashea6 5:3c880df67e2e 131 curAlarmHour = timeinfo->tm_hour;
ashea6 3:5f0b28699a67 132 }
ashea6 3:5f0b28699a67 133
ashea6 3:5f0b28699a67 134 void off_hit_callback (void)
ashea6 3:5f0b28699a67 135 {
ashea6 3:5f0b28699a67 136 myled2 = !myled2;
ashea6 6:8e926bbfaec0 137 play = false;
ashea6 6:8e926bbfaec0 138 curAlarmMin = baseAlarmMin;
ashea6 6:8e926bbfaec0 139 curAlarmHour = baseAlarmHour;
ashea6 3:5f0b28699a67 140 }
ashea6 3:5f0b28699a67 141
ashea6 4:55035e20ae61 142 void play_file()
ashea6 4:55035e20ae61 143 {
ashea6 6:8e926bbfaec0 144 bool* play_point = &play;
ashea6 4:55035e20ae61 145 FILE *wave_file;
ashea6 4:55035e20ae61 146 printf("\n\n\nHello, wave world!\n");
ashea6 4:55035e20ae61 147 wave_file=fopen("/sd/bob.wav","r");
ashea6 6:8e926bbfaec0 148 waver.play(wave_file, play_point);
ashea6 4:55035e20ae61 149 fclose(wave_file);
ashea6 4:55035e20ae61 150 }
ashea6 5:3c880df67e2e 151
ashea6 5:3c880df67e2e 152 void timeCompare()
ashea6 5:3c880df67e2e 153 {
ashea6 5:3c880df67e2e 154 struct tm * timeinfo;
ashea6 5:3c880df67e2e 155 timeinfo = localtime (&ct_time);
ashea6 5:3c880df67e2e 156 if (timeinfo->tm_min == curAlarmMin && timeinfo->tm_hour == curAlarmHour) {
ashea6 6:8e926bbfaec0 157 play = true;
ashea6 6:8e926bbfaec0 158 myled3 = true;
ashea6 5:3c880df67e2e 159 play_file();
ashea6 5:3c880df67e2e 160 }
ashea6 5:3c880df67e2e 161 }
ashea6 5:3c880df67e2e 162
4180_1 1:5b8e223e983d 163 int main()
4180_1 1:5b8e223e983d 164 {
ashea6 3:5f0b28699a67 165 snooze.mode(PullUp);
ashea6 3:5f0b28699a67 166 off.mode(PullUp);
ashea6 3:5f0b28699a67 167 wait(0.01);
ashea6 3:5f0b28699a67 168 snooze.attach_deasserted(&snooze_hit_callback);
ashea6 3:5f0b28699a67 169 off.attach_deasserted(&off_hit_callback);
ashea6 3:5f0b28699a67 170 snooze.setSampleFrequency();
ashea6 3:5f0b28699a67 171 off.setSampleFrequency();
ashea6 2:4d370e3f4618 172
agamemaker 7:456add5c89e1 173 //play_file();
ashea6 2:4d370e3f4618 174
ashea6 2:4d370e3f4618 175
ashea6 2:4d370e3f4618 176 // Initialize LCD
ashea6 2:4d370e3f4618 177 uLCD.baudrate(115200);
ashea6 2:4d370e3f4618 178 uLCD.background_color(BLACK);
ashea6 2:4d370e3f4618 179 uLCD.cls();
ashea6 2:4d370e3f4618 180
ashea6 2:4d370e3f4618 181 // Connect to network and wait for DHCP
ashea6 2:4d370e3f4618 182 uLCD.locate(0,0);
ashea6 2:4d370e3f4618 183 uLCD.printf("Getting IP Address\n");
ashea6 2:4d370e3f4618 184 eth.init();
ashea6 2:4d370e3f4618 185 if ( eth.connect(60000) == -1 ) {
ashea6 2:4d370e3f4618 186 uLCD.printf("ERROR: Could not\nget IP address");
ashea6 2:4d370e3f4618 187 return -1;
ashea6 2:4d370e3f4618 188 }
ashea6 2:4d370e3f4618 189 uLCD.printf("IP address is \n%s\n\n",eth.getIPAddress());
ashea6 2:4d370e3f4618 190 Thread::wait(1000);
agamemaker 8:4750e5fd45c1 191 getWeather();
agamemaker 9:fbb5f22fc299 192 wait(500);
ashea6 2:4d370e3f4618 193 // Read time from server
agamemaker 10:6b27fafad047 194 {
agamemaker 10:6b27fafad047 195 uLCD.printf("Reading time...\n\r");
agamemaker 10:6b27fafad047 196 NTPClient ntp_client;
agamemaker 10:6b27fafad047 197 ntp_client.setTime(time_domain_name, port_number);
agamemaker 10:6b27fafad047 198 uLCD.printf("Time set\n");
agamemaker 10:6b27fafad047 199 }
ashea6 2:4d370e3f4618 200 Thread::wait(2000);
ashea6 2:4d370e3f4618 201 eth.disconnect();
ashea6 2:4d370e3f4618 202
ashea6 2:4d370e3f4618 203 // Reset LCD
ashea6 2:4d370e3f4618 204 uLCD.background_color(WHITE);
ashea6 2:4d370e3f4618 205 uLCD.textbackground_color(WHITE);
ashea6 2:4d370e3f4618 206 uLCD.color(RED);
ashea6 2:4d370e3f4618 207 uLCD.cls();
ashea6 2:4d370e3f4618 208 uLCD.text_height(2);
ashea6 2:4d370e3f4618 209
agamemaker 8:4750e5fd45c1 210 //Thread t1(time_thread);
agamemaker 8:4750e5fd45c1 211 //Thread t2(weth_thread);
agamemaker 7:456add5c89e1 212 while(true) {
ashea6 5:3c880df67e2e 213 timeCompare();
ashea6 5:3c880df67e2e 214 Thread::wait(100);
ashea6 5:3c880df67e2e 215 }
ashea6 4:55035e20ae61 216
4180_1 1:5b8e223e983d 217 }