wave cancel
Dependencies: 4DGL-uLCD-SE EthernetInterface HTTPClient NTPClient PinDetect SDFileSystem mbed-rtos mbed wave_player
Fork of 4180_Final_Project by
main.cpp@11:89e778e985d5, 2016-04-28 (annotated)
- Committer:
- agamemaker
- Date:
- Thu Apr 28 16:52:38 2016 +0000
- Revision:
- 11:89e778e985d5
- Parent:
- 10:6b27fafad047
- Child:
- 12:27470e8c0556
serial;
Who changed what in which revision?
User | Revision | Line number | New 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 | |
4180_1 | 1:5b8e223e983d | 154 | int main() |
4180_1 | 1:5b8e223e983d | 155 | { |
ashea6 | 3:5f0b28699a67 | 156 | snooze.mode(PullUp); |
ashea6 | 3:5f0b28699a67 | 157 | off.mode(PullUp); |
agamemaker | 11:89e778e985d5 | 158 | settings.mode(PullUp); |
ashea6 | 3:5f0b28699a67 | 159 | wait(0.01); |
ashea6 | 3:5f0b28699a67 | 160 | snooze.attach_deasserted(&snooze_hit_callback); |
ashea6 | 3:5f0b28699a67 | 161 | off.attach_deasserted(&off_hit_callback); |
agamemaker | 11:89e778e985d5 | 162 | settings.attach_deasserted(&off_hit_callback); |
ashea6 | 3:5f0b28699a67 | 163 | snooze.setSampleFrequency(); |
ashea6 | 3:5f0b28699a67 | 164 | off.setSampleFrequency(); |
agamemaker | 11:89e778e985d5 | 165 | settings.setSampleFrequency(); |
ashea6 | 2:4d370e3f4618 | 166 | |
agamemaker | 7:456add5c89e1 | 167 | //play_file(); |
ashea6 | 2:4d370e3f4618 | 168 | |
ashea6 | 2:4d370e3f4618 | 169 | |
ashea6 | 2:4d370e3f4618 | 170 | // Initialize LCD |
ashea6 | 2:4d370e3f4618 | 171 | uLCD.baudrate(115200); |
ashea6 | 2:4d370e3f4618 | 172 | uLCD.background_color(BLACK); |
ashea6 | 2:4d370e3f4618 | 173 | uLCD.cls(); |
ashea6 | 2:4d370e3f4618 | 174 | |
ashea6 | 2:4d370e3f4618 | 175 | // Connect to network and wait for DHCP |
ashea6 | 2:4d370e3f4618 | 176 | uLCD.locate(0,0); |
ashea6 | 2:4d370e3f4618 | 177 | uLCD.printf("Getting IP Address\n"); |
ashea6 | 2:4d370e3f4618 | 178 | eth.init(); |
ashea6 | 2:4d370e3f4618 | 179 | if ( eth.connect(60000) == -1 ) { |
ashea6 | 2:4d370e3f4618 | 180 | uLCD.printf("ERROR: Could not\nget IP address"); |
ashea6 | 2:4d370e3f4618 | 181 | return -1; |
ashea6 | 2:4d370e3f4618 | 182 | } |
ashea6 | 2:4d370e3f4618 | 183 | uLCD.printf("IP address is \n%s\n\n",eth.getIPAddress()); |
ashea6 | 2:4d370e3f4618 | 184 | // Read time from server |
agamemaker | 10:6b27fafad047 | 185 | { |
agamemaker | 10:6b27fafad047 | 186 | uLCD.printf("Reading time...\n\r"); |
agamemaker | 10:6b27fafad047 | 187 | NTPClient ntp_client; |
agamemaker | 10:6b27fafad047 | 188 | ntp_client.setTime(time_domain_name, port_number); |
agamemaker | 10:6b27fafad047 | 189 | uLCD.printf("Time set\n"); |
agamemaker | 10:6b27fafad047 | 190 | } |
ashea6 | 2:4d370e3f4618 | 191 | Thread::wait(2000); |
ashea6 | 2:4d370e3f4618 | 192 | eth.disconnect(); |
ashea6 | 2:4d370e3f4618 | 193 | |
ashea6 | 2:4d370e3f4618 | 194 | // Reset LCD |
ashea6 | 2:4d370e3f4618 | 195 | uLCD.background_color(WHITE); |
ashea6 | 2:4d370e3f4618 | 196 | uLCD.textbackground_color(WHITE); |
ashea6 | 2:4d370e3f4618 | 197 | uLCD.color(RED); |
ashea6 | 2:4d370e3f4618 | 198 | uLCD.cls(); |
ashea6 | 2:4d370e3f4618 | 199 | uLCD.text_height(2); |
ashea6 | 2:4d370e3f4618 | 200 | |
agamemaker | 11:89e778e985d5 | 201 | Thread t1(time_thread); |
agamemaker | 11:89e778e985d5 | 202 | //Thread t2(comm_thread); |
agamemaker | 7:456add5c89e1 | 203 | while(true) { |
ashea6 | 5:3c880df67e2e | 204 | timeCompare(); |
ashea6 | 5:3c880df67e2e | 205 | Thread::wait(100); |
ashea6 | 5:3c880df67e2e | 206 | } |
ashea6 | 4:55035e20ae61 | 207 | |
4180_1 | 1:5b8e223e983d | 208 | } |