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:
deronmai
Date:
Thu Apr 28 17:00:55 2016 +0000
Revision:
12:27470e8c0556
Parent:
11:89e778e985d5
Child:
13:34dc6730e791
added sd

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
agamemaker 11:89e778e985d5 18 Serial pc(USBTX, USBRX);
agamemaker 11:89e778e985d5 19 Serial time_weth(p9, p10);
4180_1 1:5b8e223e983d 20 AnalogOut DACout(p18);
4180_1 1:5b8e223e983d 21 wave_player waver(&DACout);
ashea6 3:5f0b28699a67 22 PinDetect snooze(p19); //snooze button
ashea6 3:5f0b28699a67 23 PinDetect off(p20); //turn alarm off
agamemaker 11:89e778e985d5 24 PinDetect settings(p17); //turn alarm off
ashea6 3:5f0b28699a67 25 DigitalOut myled1(LED1);
ashea6 3:5f0b28699a67 26 DigitalOut myled2(LED2);
ashea6 5:3c880df67e2e 27 DigitalOut myled3(LED3);
agamemaker 8:4750e5fd45c1 28 //Mutex lcd_mutex;
4180_1 1:5b8e223e983d 29
ashea6 2:4d370e3f4618 30 // Parameters
agamemaker 8:4750e5fd45c1 31 char* time_domain_name = "0.uk.pool.ntp.org";
agamemaker 9:fbb5f22fc299 32 //char* weth_domain_name = "http://weather.yahooapis.com/forecastrss?w=2502265";
agamemaker 10:6b27fafad047 33 //char* weth_domain_name = "http://developer.mbed.org/media/uploads/mbed_official/hello.txt";
agamemaker 11:89e778e985d5 34 //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 35 int port_number = 123;
ashea6 2:4d370e3f4618 36
ashea6 2:4d370e3f4618 37 // Networking
ashea6 2:4d370e3f4618 38 EthernetInterface eth;
ashea6 5:3c880df67e2e 39 #define snoozeTime 10
agamemaker 11:89e778e985d5 40 #define locationLength 50
ashea6 5:3c880df67e2e 41
ashea6 2:4d370e3f4618 42 //global variables
ashea6 2:4d370e3f4618 43 time_t ct_time;
ashea6 5:3c880df67e2e 44 // Base alarm time-24 hour clock
ashea6 5:3c880df67e2e 45 int baseAlarmHour = 0; //0-23
ashea6 5:3c880df67e2e 46 int baseAlarmMin = 0;
ashea6 5:3c880df67e2e 47 // Current alarm time
ashea6 5:3c880df67e2e 48 int curAlarmHour = 18; //0-23
ashea6 5:3c880df67e2e 49 int curAlarmMin = 9;
ashea6 6:8e926bbfaec0 50 bool play = true;
agamemaker 11:89e778e985d5 51 //traffic
agamemaker 11:89e778e985d5 52 char location[locationLength];
agamemaker 11:89e778e985d5 53 char destination[locationLength];
agamemaker 11:89e778e985d5 54
ashea6 2:4d370e3f4618 55
ashea6 3:5f0b28699a67 56 //time thread
ashea6 2:4d370e3f4618 57 void time_thread(void const *args)
ashea6 2:4d370e3f4618 58 {
ashea6 2:4d370e3f4618 59 char time_buffer[80];
ashea6 5:3c880df67e2e 60 // Loop and update clock
ashea6 2:4d370e3f4618 61 while (1) {
agamemaker 8:4750e5fd45c1 62 //lcd_mutex.lock();
ashea6 2:4d370e3f4618 63 uLCD.locate(0, 1);
ashea6 2:4d370e3f4618 64 ct_time = time(NULL);
ashea6 2:4d370e3f4618 65 strftime(time_buffer, 80, " %a %b %d\n %T %p %z\n %Z\n", \
ashea6 2:4d370e3f4618 66 localtime(&ct_time));
ashea6 2:4d370e3f4618 67 uLCD.printf(" UTC/GMT:\n%s", time_buffer);
agamemaker 8:4750e5fd45c1 68 //lcd_mutex.unlock();
ashea6 2:4d370e3f4618 69 Thread::wait(100);
ashea6 2:4d370e3f4618 70 }
ashea6 2:4d370e3f4618 71 }
agamemaker 11:89e778e985d5 72 /*
agamemaker 11:89e778e985d5 73 //communication thread
agamemaker 11:89e778e985d5 74 void comm_thread(void const *args)
agamemaker 8:4750e5fd45c1 75 {
agamemaker 11:89e778e985d5 76 time_weth.putc("w");
agamemaker 11:89e778e985d5 77 time_weth.putc("e");
agamemaker 11:89e778e985d5 78 time_weth.putc("t");
agamemaker 11:89e778e985d5 79 time_weth.putc("h");
agamemaker 11:89e778e985d5 80 for(i = 0; i < locationLength; i++){
agamemaker 11:89e778e985d5 81 time_weth.putc(location[i]);
agamemaker 11:89e778e985d5 82 time_weth.putc(destination[i]);
agamemaker 11:89e778e985d5 83 }
agamemaker 8:4750e5fd45c1 84 }
agamemaker 11:89e778e985d5 85 */
agamemaker 8:4750e5fd45c1 86
ashea6 3:5f0b28699a67 87 //pushbutton (p19)
ashea6 3:5f0b28699a67 88 void snooze_hit_callback (void)
ashea6 3:5f0b28699a67 89 {
ashea6 5:3c880df67e2e 90 myled1 = !myled1;
ashea6 6:8e926bbfaec0 91 play = false;
ashea6 5:3c880df67e2e 92 time_t newtime;
ashea6 5:3c880df67e2e 93 struct tm * timeinfo;
ashea6 5:3c880df67e2e 94 newtime = ct_time + snoozeTime;
ashea6 5:3c880df67e2e 95 //time (&newtime);
ashea6 5:3c880df67e2e 96 timeinfo = localtime (&newtime);
ashea6 5:3c880df67e2e 97 curAlarmMin = timeinfo->tm_min;
ashea6 5:3c880df67e2e 98 curAlarmHour = timeinfo->tm_hour;
ashea6 3:5f0b28699a67 99 }
ashea6 3:5f0b28699a67 100
ashea6 3:5f0b28699a67 101 void off_hit_callback (void)
ashea6 3:5f0b28699a67 102 {
ashea6 3:5f0b28699a67 103 myled2 = !myled2;
ashea6 6:8e926bbfaec0 104 play = false;
ashea6 6:8e926bbfaec0 105 curAlarmMin = baseAlarmMin;
ashea6 6:8e926bbfaec0 106 curAlarmHour = baseAlarmHour;
ashea6 3:5f0b28699a67 107 }
ashea6 3:5f0b28699a67 108
agamemaker 11:89e778e985d5 109 void settings_hit_callback (void)
agamemaker 11:89e778e985d5 110 {
agamemaker 11:89e778e985d5 111 while(1) {
agamemaker 11:89e778e985d5 112 char buff[100];
agamemaker 11:89e778e985d5 113 int i = 0;
agamemaker 11:89e778e985d5 114 bool flag = false;
agamemaker 11:89e778e985d5 115 while(!flag) {
agamemaker 11:89e778e985d5 116 buff[i] = pc.getc();
agamemaker 11:89e778e985d5 117 pc.putc(buff[i]);
agamemaker 11:89e778e985d5 118 flag = buff[i] == '^';
agamemaker 11:89e778e985d5 119 i++;
agamemaker 11:89e778e985d5 120 }
agamemaker 11:89e778e985d5 121 uLCD.printf("%s", buff);
agamemaker 11:89e778e985d5 122
agamemaker 11:89e778e985d5 123 mkdir("/sd/mydir", 0777);
agamemaker 11:89e778e985d5 124 FILE *fp = fopen("/sd/mydir/sdtest.txt", "w");
agamemaker 11:89e778e985d5 125 if(fp == NULL) {
agamemaker 11:89e778e985d5 126 error("Could not open file for write\n");
agamemaker 11:89e778e985d5 127 }
agamemaker 11:89e778e985d5 128 fprintf(fp, buff);
agamemaker 11:89e778e985d5 129 fclose(fp);
agamemaker 11:89e778e985d5 130 }
agamemaker 11:89e778e985d5 131 }
agamemaker 11:89e778e985d5 132
ashea6 4:55035e20ae61 133 void play_file()
ashea6 4:55035e20ae61 134 {
ashea6 6:8e926bbfaec0 135 bool* play_point = &play;
ashea6 4:55035e20ae61 136 FILE *wave_file;
ashea6 4:55035e20ae61 137 printf("\n\n\nHello, wave world!\n");
ashea6 4:55035e20ae61 138 wave_file=fopen("/sd/bob.wav","r");
ashea6 6:8e926bbfaec0 139 waver.play(wave_file, play_point);
ashea6 4:55035e20ae61 140 fclose(wave_file);
ashea6 4:55035e20ae61 141 }
ashea6 5:3c880df67e2e 142
ashea6 5:3c880df67e2e 143 void timeCompare()
ashea6 5:3c880df67e2e 144 {
ashea6 5:3c880df67e2e 145 struct tm * timeinfo;
ashea6 5:3c880df67e2e 146 timeinfo = localtime (&ct_time);
ashea6 5:3c880df67e2e 147 if (timeinfo->tm_min == curAlarmMin && timeinfo->tm_hour == curAlarmHour) {
ashea6 6:8e926bbfaec0 148 play = true;
ashea6 6:8e926bbfaec0 149 myled3 = true;
ashea6 5:3c880df67e2e 150 play_file();
ashea6 5:3c880df67e2e 151 }
ashea6 5:3c880df67e2e 152 }
ashea6 5:3c880df67e2e 153
deronmai 12:27470e8c0556 154 void getSDInfo()
deronmai 12:27470e8c0556 155 {
deronmai 12:27470e8c0556 156 char c;
deronmai 12:27470e8c0556 157 FILE *fp = fopen("/sd/mydir/sdtest.txt", "r");
deronmai 12:27470e8c0556 158 if(fp == NULL) {
deronmai 12:27470e8c0556 159 error("Could not open file for read\n");
deronmai 12:27470e8c0556 160 }
deronmai 12:27470e8c0556 161 int count = 0;
deronmai 12:27470e8c0556 162 int i;
deronmai 12:27470e8c0556 163 char arr_time[8] = "";
deronmai 12:27470e8c0556 164 char hour[3], min[3];
deronmai 12:27470e8c0556 165 int arrival_hour;
deronmai 12:27470e8c0556 166 int arrival_min;
deronmai 12:27470e8c0556 167 char r_time[8] = "";
deronmai 12:27470e8c0556 168 int ready_time = 0;
deronmai 12:27470e8c0556 169 char start_add[32] = ""; // start location address
deronmai 12:27470e8c0556 170 char dest_add[32] = ""; // destination address
deronmai 12:27470e8c0556 171
deronmai 12:27470e8c0556 172 while (!feof(fp)){ // while not end of file
deronmai 12:27470e8c0556 173 c=fgetc(fp); // get a character/byte from the file
deronmai 12:27470e8c0556 174 uLCD.printf("%c",c);
deronmai 12:27470e8c0556 175 if (c == '@') {
deronmai 12:27470e8c0556 176 count++; // specifies what data (eg. arrival time, ready time, etc)
deronmai 12:27470e8c0556 177 i = 0;
deronmai 12:27470e8c0556 178 }
deronmai 12:27470e8c0556 179 else {
deronmai 12:27470e8c0556 180 //uLCD.printf("%d",count);
deronmai 12:27470e8c0556 181 switch (count) {
deronmai 12:27470e8c0556 182 case 0: // arrival time
deronmai 12:27470e8c0556 183 arr_time[i] = c;
deronmai 12:27470e8c0556 184 i++;
deronmai 12:27470e8c0556 185 break;
deronmai 12:27470e8c0556 186 case 1: // ready time (min)
deronmai 12:27470e8c0556 187 r_time[i] = c;
deronmai 12:27470e8c0556 188 i++;
deronmai 12:27470e8c0556 189 break;
deronmai 12:27470e8c0556 190 case 2: // start address
deronmai 12:27470e8c0556 191 start_add[i] = c;
deronmai 12:27470e8c0556 192 i++;
deronmai 12:27470e8c0556 193 break;
deronmai 12:27470e8c0556 194 case 3: // destination address
deronmai 12:27470e8c0556 195 dest_add[i] = c;
deronmai 12:27470e8c0556 196 i++;
deronmai 12:27470e8c0556 197 break;
deronmai 12:27470e8c0556 198 default:
deronmai 12:27470e8c0556 199 error("too many & detected\n");
deronmai 12:27470e8c0556 200 break;
deronmai 12:27470e8c0556 201 }
deronmai 12:27470e8c0556 202
deronmai 12:27470e8c0556 203 //uLCD.printf("Read from file %02x\n\r",c); // and show it in hex format
deronmai 12:27470e8c0556 204 }
deronmai 12:27470e8c0556 205 }
deronmai 12:27470e8c0556 206 memcpy( hour, &arr_time[0], 2 );
deronmai 12:27470e8c0556 207 memcpy( min, &arr_time[3], 2 );
deronmai 12:27470e8c0556 208
deronmai 12:27470e8c0556 209 arrival_hour = atoi(hour);
deronmai 12:27470e8c0556 210 arrival_min = atoi(min);
deronmai 12:27470e8c0556 211 uLCD.printf("\narrival hour is %d\n",arrival_hour);
deronmai 12:27470e8c0556 212 uLCD.printf("arrival min is %d\n",arrival_min);
deronmai 12:27470e8c0556 213
deronmai 12:27470e8c0556 214 ready_time = atoi(r_time);
deronmai 12:27470e8c0556 215 //uLCD.printf("\nString ready time is %s",r_time);
deronmai 12:27470e8c0556 216 uLCD.printf("Ready Time is %d\n",ready_time);
deronmai 12:27470e8c0556 217 uLCD.printf("start address is %s\n", start_add);
deronmai 12:27470e8c0556 218 uLCD.printf("dest address is %s\n", dest_add);
deronmai 12:27470e8c0556 219
deronmai 12:27470e8c0556 220 fclose(fp);
deronmai 12:27470e8c0556 221 }
deronmai 12:27470e8c0556 222
4180_1 1:5b8e223e983d 223 int main()
4180_1 1:5b8e223e983d 224 {
ashea6 3:5f0b28699a67 225 snooze.mode(PullUp);
ashea6 3:5f0b28699a67 226 off.mode(PullUp);
agamemaker 11:89e778e985d5 227 settings.mode(PullUp);
ashea6 3:5f0b28699a67 228 wait(0.01);
ashea6 3:5f0b28699a67 229 snooze.attach_deasserted(&snooze_hit_callback);
ashea6 3:5f0b28699a67 230 off.attach_deasserted(&off_hit_callback);
agamemaker 11:89e778e985d5 231 settings.attach_deasserted(&off_hit_callback);
ashea6 3:5f0b28699a67 232 snooze.setSampleFrequency();
ashea6 3:5f0b28699a67 233 off.setSampleFrequency();
agamemaker 11:89e778e985d5 234 settings.setSampleFrequency();
ashea6 2:4d370e3f4618 235
agamemaker 7:456add5c89e1 236 //play_file();
ashea6 2:4d370e3f4618 237
ashea6 2:4d370e3f4618 238
ashea6 2:4d370e3f4618 239 // Initialize LCD
ashea6 2:4d370e3f4618 240 uLCD.baudrate(115200);
ashea6 2:4d370e3f4618 241 uLCD.background_color(BLACK);
ashea6 2:4d370e3f4618 242 uLCD.cls();
ashea6 2:4d370e3f4618 243
ashea6 2:4d370e3f4618 244 // Connect to network and wait for DHCP
ashea6 2:4d370e3f4618 245 uLCD.locate(0,0);
ashea6 2:4d370e3f4618 246 uLCD.printf("Getting IP Address\n");
ashea6 2:4d370e3f4618 247 eth.init();
ashea6 2:4d370e3f4618 248 if ( eth.connect(60000) == -1 ) {
ashea6 2:4d370e3f4618 249 uLCD.printf("ERROR: Could not\nget IP address");
ashea6 2:4d370e3f4618 250 return -1;
ashea6 2:4d370e3f4618 251 }
ashea6 2:4d370e3f4618 252 uLCD.printf("IP address is \n%s\n\n",eth.getIPAddress());
ashea6 2:4d370e3f4618 253 // Read time from server
agamemaker 10:6b27fafad047 254 {
agamemaker 10:6b27fafad047 255 uLCD.printf("Reading time...\n\r");
agamemaker 10:6b27fafad047 256 NTPClient ntp_client;
agamemaker 10:6b27fafad047 257 ntp_client.setTime(time_domain_name, port_number);
agamemaker 10:6b27fafad047 258 uLCD.printf("Time set\n");
agamemaker 10:6b27fafad047 259 }
ashea6 2:4d370e3f4618 260 Thread::wait(2000);
ashea6 2:4d370e3f4618 261 eth.disconnect();
ashea6 2:4d370e3f4618 262
ashea6 2:4d370e3f4618 263 // Reset LCD
ashea6 2:4d370e3f4618 264 uLCD.background_color(WHITE);
ashea6 2:4d370e3f4618 265 uLCD.textbackground_color(WHITE);
ashea6 2:4d370e3f4618 266 uLCD.color(RED);
ashea6 2:4d370e3f4618 267 uLCD.cls();
ashea6 2:4d370e3f4618 268 uLCD.text_height(2);
ashea6 2:4d370e3f4618 269
agamemaker 11:89e778e985d5 270 Thread t1(time_thread);
agamemaker 11:89e778e985d5 271 //Thread t2(comm_thread);
agamemaker 7:456add5c89e1 272 while(true) {
ashea6 5:3c880df67e2e 273 timeCompare();
ashea6 5:3c880df67e2e 274 Thread::wait(100);
ashea6 5:3c880df67e2e 275 }
ashea6 4:55035e20ae61 276
4180_1 1:5b8e223e983d 277 }