rev1

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

Fork of WavePlayer_HelloWorld by jim hamblen

Committer:
ashea6
Date:
Fri Apr 22 17:38:25 2016 +0000
Revision:
5:3c880df67e2e
Parent:
4:55035e20ae61
snooze, time compare

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"
4180_1 1:5b8e223e983d 12
ashea6 5:3c880df67e2e 13 //pinouts
ashea6 2:4d370e3f4618 14 SDFileSystem sd(p11, p12, p13, p14, "sd"); //SD card
4180_1 1:5b8e223e983d 15 AnalogOut DACout(p18);
4180_1 1:5b8e223e983d 16 wave_player waver(&DACout);
ashea6 3:5f0b28699a67 17 PinDetect snooze(p19); //snooze button
ashea6 3:5f0b28699a67 18 PinDetect off(p20); //turn alarm off
ashea6 3:5f0b28699a67 19 DigitalOut myled1(LED1);
ashea6 3:5f0b28699a67 20 DigitalOut myled2(LED2);
ashea6 5:3c880df67e2e 21 DigitalOut myled3(LED3);
4180_1 1:5b8e223e983d 22
ashea6 2:4d370e3f4618 23 // Parameters
ashea6 2:4d370e3f4618 24 char* domain_name = "0.uk.pool.ntp.org";
ashea6 2:4d370e3f4618 25 int port_number = 123;
ashea6 2:4d370e3f4618 26
ashea6 2:4d370e3f4618 27 // Networking
ashea6 2:4d370e3f4618 28 EthernetInterface eth;
ashea6 2:4d370e3f4618 29 NTPClient ntp_client;
ashea6 2:4d370e3f4618 30
ashea6 2:4d370e3f4618 31 // Graphic LCD - TX, RX, and RES pins
ashea6 2:4d370e3f4618 32 uLCD_4DGL uLCD(p28,p27,p29);
ashea6 2:4d370e3f4618 33
ashea6 5:3c880df67e2e 34 #define snoozeTime 10
ashea6 5:3c880df67e2e 35
ashea6 2:4d370e3f4618 36 //global variables
ashea6 2:4d370e3f4618 37 time_t ct_time;
ashea6 5:3c880df67e2e 38 // Base alarm time-24 hour clock
ashea6 5:3c880df67e2e 39 int baseAlarmHour = 0; //0-23
ashea6 5:3c880df67e2e 40 int baseAlarmMin = 0;
ashea6 5:3c880df67e2e 41 // Current alarm time
ashea6 5:3c880df67e2e 42 int curAlarmHour = 18; //0-23
ashea6 5:3c880df67e2e 43 int curAlarmMin = 9;
ashea6 2:4d370e3f4618 44
ashea6 3:5f0b28699a67 45 //time thread
ashea6 2:4d370e3f4618 46 void time_thread(void const *args)
ashea6 2:4d370e3f4618 47 {
ashea6 2:4d370e3f4618 48 char time_buffer[80];
ashea6 5:3c880df67e2e 49 // Loop and update clock
ashea6 2:4d370e3f4618 50 while (1) {
ashea6 2:4d370e3f4618 51 uLCD.locate(0, 1);
ashea6 2:4d370e3f4618 52 ct_time = time(NULL);
ashea6 2:4d370e3f4618 53 strftime(time_buffer, 80, " %a %b %d\n %T %p %z\n %Z\n", \
ashea6 2:4d370e3f4618 54 localtime(&ct_time));
ashea6 2:4d370e3f4618 55 uLCD.printf(" UTC/GMT:\n%s", time_buffer);
ashea6 2:4d370e3f4618 56 Thread::wait(100);
ashea6 2:4d370e3f4618 57 }
ashea6 2:4d370e3f4618 58 }
ashea6 2:4d370e3f4618 59
ashea6 3:5f0b28699a67 60 //pushbutton (p19)
ashea6 3:5f0b28699a67 61 void snooze_hit_callback (void)
ashea6 3:5f0b28699a67 62 {
ashea6 5:3c880df67e2e 63 myled1 = !myled1;
ashea6 5:3c880df67e2e 64 time_t newtime;
ashea6 5:3c880df67e2e 65 struct tm * timeinfo;
ashea6 5:3c880df67e2e 66 newtime = ct_time + snoozeTime;
ashea6 5:3c880df67e2e 67 //time (&newtime);
ashea6 5:3c880df67e2e 68 timeinfo = localtime (&newtime);
ashea6 5:3c880df67e2e 69 curAlarmMin = timeinfo->tm_min;
ashea6 5:3c880df67e2e 70 curAlarmHour = timeinfo->tm_hour;
ashea6 3:5f0b28699a67 71 }
ashea6 3:5f0b28699a67 72
ashea6 3:5f0b28699a67 73 void off_hit_callback (void)
ashea6 3:5f0b28699a67 74 {
ashea6 3:5f0b28699a67 75 myled2 = !myled2;
ashea6 3:5f0b28699a67 76 }
ashea6 3:5f0b28699a67 77
ashea6 4:55035e20ae61 78 void play_file()
ashea6 4:55035e20ae61 79 {
ashea6 4:55035e20ae61 80 FILE *wave_file;
ashea6 4:55035e20ae61 81 printf("\n\n\nHello, wave world!\n");
ashea6 4:55035e20ae61 82 wave_file=fopen("/sd/bob.wav","r");
ashea6 4:55035e20ae61 83 waver.play(wave_file);
ashea6 4:55035e20ae61 84 fclose(wave_file);
ashea6 4:55035e20ae61 85 }
ashea6 5:3c880df67e2e 86
ashea6 5:3c880df67e2e 87 void timeCompare()
ashea6 5:3c880df67e2e 88 {
ashea6 5:3c880df67e2e 89 struct tm * timeinfo;
ashea6 5:3c880df67e2e 90 timeinfo = localtime (&ct_time);
ashea6 5:3c880df67e2e 91 if (timeinfo->tm_min == curAlarmMin && timeinfo->tm_hour == curAlarmHour) {
ashea6 5:3c880df67e2e 92 //playing = true;
ashea6 5:3c880df67e2e 93 play_file();
ashea6 5:3c880df67e2e 94 myled3 = true;
ashea6 5:3c880df67e2e 95 }
ashea6 5:3c880df67e2e 96 }
ashea6 5:3c880df67e2e 97
4180_1 1:5b8e223e983d 98 int main()
4180_1 1:5b8e223e983d 99 {
ashea6 3:5f0b28699a67 100 snooze.mode(PullUp);
ashea6 3:5f0b28699a67 101 off.mode(PullUp);
ashea6 3:5f0b28699a67 102 wait(0.01);
ashea6 3:5f0b28699a67 103 snooze.attach_deasserted(&snooze_hit_callback);
ashea6 3:5f0b28699a67 104 off.attach_deasserted(&off_hit_callback);
ashea6 3:5f0b28699a67 105 snooze.setSampleFrequency();
ashea6 3:5f0b28699a67 106 off.setSampleFrequency();
ashea6 2:4d370e3f4618 107
ashea6 2:4d370e3f4618 108
ashea6 2:4d370e3f4618 109
ashea6 2:4d370e3f4618 110 // Initialize LCD
ashea6 2:4d370e3f4618 111 uLCD.baudrate(115200);
ashea6 2:4d370e3f4618 112 uLCD.background_color(BLACK);
ashea6 2:4d370e3f4618 113 uLCD.cls();
ashea6 2:4d370e3f4618 114
ashea6 2:4d370e3f4618 115 // Connect to network and wait for DHCP
ashea6 2:4d370e3f4618 116 uLCD.locate(0,0);
ashea6 2:4d370e3f4618 117 uLCD.printf("Getting IP Address\n");
ashea6 2:4d370e3f4618 118 eth.init();
ashea6 2:4d370e3f4618 119 if ( eth.connect(60000) == -1 ) {
ashea6 2:4d370e3f4618 120 uLCD.printf("ERROR: Could not\nget IP address");
ashea6 2:4d370e3f4618 121 return -1;
ashea6 2:4d370e3f4618 122 }
ashea6 2:4d370e3f4618 123 uLCD.printf("IP address is \n%s\n\n",eth.getIPAddress());
ashea6 2:4d370e3f4618 124 Thread::wait(1000);
ashea6 2:4d370e3f4618 125
ashea6 2:4d370e3f4618 126 // Read time from server
ashea6 2:4d370e3f4618 127 uLCD.printf("Reading time...\n\r");
ashea6 2:4d370e3f4618 128 ntp_client.setTime(domain_name, port_number);
ashea6 2:4d370e3f4618 129 uLCD.printf("Time set\n");
ashea6 2:4d370e3f4618 130 Thread::wait(2000);
ashea6 2:4d370e3f4618 131 eth.disconnect();
ashea6 2:4d370e3f4618 132
ashea6 2:4d370e3f4618 133 // Reset LCD
ashea6 2:4d370e3f4618 134 uLCD.background_color(WHITE);
ashea6 2:4d370e3f4618 135 uLCD.textbackground_color(WHITE);
ashea6 2:4d370e3f4618 136 uLCD.color(RED);
ashea6 2:4d370e3f4618 137 uLCD.cls();
ashea6 2:4d370e3f4618 138 uLCD.text_height(2);
ashea6 2:4d370e3f4618 139
ashea6 2:4d370e3f4618 140 Thread thread_time(time_thread);
ashea6 5:3c880df67e2e 141 while(!myled3) {
ashea6 5:3c880df67e2e 142 timeCompare();
ashea6 5:3c880df67e2e 143 Thread::wait(100);
ashea6 5:3c880df67e2e 144 }
ashea6 4:55035e20ae61 145
4180_1 1:5b8e223e983d 146 }