wave_player library problems

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

Fork of 4180_Final_Project by 4180

Committer:
ashea6
Date:
Fri Apr 22 17:59:29 2016 +0000
Revision:
6:8e926bbfaec0
Parent:
5:3c880df67e2e
wave player probs

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 6:8e926bbfaec0 44 bool play = true;
ashea6 2:4d370e3f4618 45
ashea6 3:5f0b28699a67 46 //time thread
ashea6 2:4d370e3f4618 47 void time_thread(void const *args)
ashea6 2:4d370e3f4618 48 {
ashea6 2:4d370e3f4618 49 char time_buffer[80];
ashea6 5:3c880df67e2e 50 // Loop and update clock
ashea6 2:4d370e3f4618 51 while (1) {
ashea6 2:4d370e3f4618 52 uLCD.locate(0, 1);
ashea6 2:4d370e3f4618 53 ct_time = time(NULL);
ashea6 2:4d370e3f4618 54 strftime(time_buffer, 80, " %a %b %d\n %T %p %z\n %Z\n", \
ashea6 2:4d370e3f4618 55 localtime(&ct_time));
ashea6 2:4d370e3f4618 56 uLCD.printf(" UTC/GMT:\n%s", time_buffer);
ashea6 2:4d370e3f4618 57 Thread::wait(100);
ashea6 2:4d370e3f4618 58 }
ashea6 2:4d370e3f4618 59 }
ashea6 2:4d370e3f4618 60
ashea6 3:5f0b28699a67 61 //pushbutton (p19)
ashea6 3:5f0b28699a67 62 void snooze_hit_callback (void)
ashea6 3:5f0b28699a67 63 {
ashea6 5:3c880df67e2e 64 myled1 = !myled1;
ashea6 6:8e926bbfaec0 65 play = false;
ashea6 5:3c880df67e2e 66 time_t newtime;
ashea6 5:3c880df67e2e 67 struct tm * timeinfo;
ashea6 5:3c880df67e2e 68 newtime = ct_time + snoozeTime;
ashea6 5:3c880df67e2e 69 //time (&newtime);
ashea6 5:3c880df67e2e 70 timeinfo = localtime (&newtime);
ashea6 5:3c880df67e2e 71 curAlarmMin = timeinfo->tm_min;
ashea6 5:3c880df67e2e 72 curAlarmHour = timeinfo->tm_hour;
ashea6 3:5f0b28699a67 73 }
ashea6 3:5f0b28699a67 74
ashea6 3:5f0b28699a67 75 void off_hit_callback (void)
ashea6 3:5f0b28699a67 76 {
ashea6 3:5f0b28699a67 77 myled2 = !myled2;
ashea6 6:8e926bbfaec0 78 play = false;
ashea6 6:8e926bbfaec0 79 curAlarmMin = baseAlarmMin;
ashea6 6:8e926bbfaec0 80 curAlarmHour = baseAlarmHour;
ashea6 3:5f0b28699a67 81 }
ashea6 3:5f0b28699a67 82
ashea6 4:55035e20ae61 83 void play_file()
ashea6 4:55035e20ae61 84 {
ashea6 6:8e926bbfaec0 85 bool* play_point = &play;
ashea6 4:55035e20ae61 86 FILE *wave_file;
ashea6 4:55035e20ae61 87 printf("\n\n\nHello, wave world!\n");
ashea6 4:55035e20ae61 88 wave_file=fopen("/sd/bob.wav","r");
ashea6 6:8e926bbfaec0 89 waver.play(wave_file, play_point);
ashea6 4:55035e20ae61 90 fclose(wave_file);
ashea6 4:55035e20ae61 91 }
ashea6 5:3c880df67e2e 92
ashea6 5:3c880df67e2e 93 void timeCompare()
ashea6 5:3c880df67e2e 94 {
ashea6 5:3c880df67e2e 95 struct tm * timeinfo;
ashea6 5:3c880df67e2e 96 timeinfo = localtime (&ct_time);
ashea6 5:3c880df67e2e 97 if (timeinfo->tm_min == curAlarmMin && timeinfo->tm_hour == curAlarmHour) {
ashea6 6:8e926bbfaec0 98 play = true;
ashea6 6:8e926bbfaec0 99 myled3 = true;
ashea6 5:3c880df67e2e 100 play_file();
ashea6 5:3c880df67e2e 101 }
ashea6 5:3c880df67e2e 102 }
ashea6 5:3c880df67e2e 103
4180_1 1:5b8e223e983d 104 int main()
4180_1 1:5b8e223e983d 105 {
ashea6 3:5f0b28699a67 106 snooze.mode(PullUp);
ashea6 3:5f0b28699a67 107 off.mode(PullUp);
ashea6 3:5f0b28699a67 108 wait(0.01);
ashea6 3:5f0b28699a67 109 snooze.attach_deasserted(&snooze_hit_callback);
ashea6 3:5f0b28699a67 110 off.attach_deasserted(&off_hit_callback);
ashea6 3:5f0b28699a67 111 snooze.setSampleFrequency();
ashea6 3:5f0b28699a67 112 off.setSampleFrequency();
ashea6 2:4d370e3f4618 113
ashea6 6:8e926bbfaec0 114 play_file();
ashea6 2:4d370e3f4618 115
ashea6 2:4d370e3f4618 116
ashea6 2:4d370e3f4618 117 // Initialize LCD
ashea6 2:4d370e3f4618 118 uLCD.baudrate(115200);
ashea6 2:4d370e3f4618 119 uLCD.background_color(BLACK);
ashea6 2:4d370e3f4618 120 uLCD.cls();
ashea6 2:4d370e3f4618 121
ashea6 2:4d370e3f4618 122 // Connect to network and wait for DHCP
ashea6 2:4d370e3f4618 123 uLCD.locate(0,0);
ashea6 2:4d370e3f4618 124 uLCD.printf("Getting IP Address\n");
ashea6 2:4d370e3f4618 125 eth.init();
ashea6 2:4d370e3f4618 126 if ( eth.connect(60000) == -1 ) {
ashea6 2:4d370e3f4618 127 uLCD.printf("ERROR: Could not\nget IP address");
ashea6 2:4d370e3f4618 128 return -1;
ashea6 2:4d370e3f4618 129 }
ashea6 2:4d370e3f4618 130 uLCD.printf("IP address is \n%s\n\n",eth.getIPAddress());
ashea6 2:4d370e3f4618 131 Thread::wait(1000);
ashea6 2:4d370e3f4618 132
ashea6 2:4d370e3f4618 133 // Read time from server
ashea6 2:4d370e3f4618 134 uLCD.printf("Reading time...\n\r");
ashea6 2:4d370e3f4618 135 ntp_client.setTime(domain_name, port_number);
ashea6 2:4d370e3f4618 136 uLCD.printf("Time set\n");
ashea6 2:4d370e3f4618 137 Thread::wait(2000);
ashea6 2:4d370e3f4618 138 eth.disconnect();
ashea6 2:4d370e3f4618 139
ashea6 2:4d370e3f4618 140 // Reset LCD
ashea6 2:4d370e3f4618 141 uLCD.background_color(WHITE);
ashea6 2:4d370e3f4618 142 uLCD.textbackground_color(WHITE);
ashea6 2:4d370e3f4618 143 uLCD.color(RED);
ashea6 2:4d370e3f4618 144 uLCD.cls();
ashea6 2:4d370e3f4618 145 uLCD.text_height(2);
ashea6 2:4d370e3f4618 146
ashea6 2:4d370e3f4618 147 Thread thread_time(time_thread);
ashea6 5:3c880df67e2e 148 while(!myled3) {
ashea6 5:3c880df67e2e 149 timeCompare();
ashea6 5:3c880df67e2e 150 Thread::wait(100);
ashea6 5:3c880df67e2e 151 }
ashea6 4:55035e20ae61 152
4180_1 1:5b8e223e983d 153 }