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:15:19 2016 +0000
Revision:
3:5f0b28699a67
Parent:
2:4d370e3f4618
Child:
4:55035e20ae61
added pushbuttons

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 3:5f0b28699a67 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);
4180_1 1:5b8e223e983d 21
ashea6 2:4d370e3f4618 22 // Parameters
ashea6 2:4d370e3f4618 23 char* domain_name = "0.uk.pool.ntp.org";
ashea6 2:4d370e3f4618 24 int port_number = 123;
ashea6 2:4d370e3f4618 25
ashea6 2:4d370e3f4618 26 // Networking
ashea6 2:4d370e3f4618 27 EthernetInterface eth;
ashea6 2:4d370e3f4618 28 NTPClient ntp_client;
ashea6 2:4d370e3f4618 29
ashea6 2:4d370e3f4618 30 // Graphic LCD - TX, RX, and RES pins
ashea6 2:4d370e3f4618 31 uLCD_4DGL uLCD(p28,p27,p29);
ashea6 2:4d370e3f4618 32
ashea6 2:4d370e3f4618 33 //global variables
ashea6 2:4d370e3f4618 34 time_t ct_time;
ashea6 2:4d370e3f4618 35
ashea6 3:5f0b28699a67 36 //time thread
ashea6 2:4d370e3f4618 37 void time_thread(void const *args)
ashea6 2:4d370e3f4618 38 {
ashea6 2:4d370e3f4618 39
ashea6 2:4d370e3f4618 40 char time_buffer[80];
ashea6 2:4d370e3f4618 41 // Loop and update clock
ashea6 2:4d370e3f4618 42 while (1) {
ashea6 2:4d370e3f4618 43 uLCD.locate(0, 1);
ashea6 2:4d370e3f4618 44 ct_time = time(NULL);
ashea6 2:4d370e3f4618 45 strftime(time_buffer, 80, " %a %b %d\n %T %p %z\n %Z\n", \
ashea6 2:4d370e3f4618 46 localtime(&ct_time));
ashea6 2:4d370e3f4618 47 uLCD.printf(" UTC/GMT:\n%s", time_buffer);
ashea6 2:4d370e3f4618 48 Thread::wait(100);
ashea6 2:4d370e3f4618 49 }
ashea6 2:4d370e3f4618 50 }
ashea6 2:4d370e3f4618 51
ashea6 3:5f0b28699a67 52 //pushbutton (p19)
ashea6 3:5f0b28699a67 53 void snooze_hit_callback (void)
ashea6 3:5f0b28699a67 54 {
ashea6 3:5f0b28699a67 55 myled1 = !myled1;
ashea6 3:5f0b28699a67 56 }
ashea6 3:5f0b28699a67 57
ashea6 3:5f0b28699a67 58 void off_hit_callback (void)
ashea6 3:5f0b28699a67 59 {
ashea6 3:5f0b28699a67 60 myled2 = !myled2;
ashea6 3:5f0b28699a67 61 }
ashea6 3:5f0b28699a67 62
4180_1 1:5b8e223e983d 63 int main()
4180_1 1:5b8e223e983d 64 {
ashea6 3:5f0b28699a67 65 snooze.mode(PullUp);
ashea6 3:5f0b28699a67 66 off.mode(PullUp);
ashea6 3:5f0b28699a67 67 wait(0.01);
ashea6 3:5f0b28699a67 68 snooze.attach_deasserted(&snooze_hit_callback);
ashea6 3:5f0b28699a67 69 off.attach_deasserted(&off_hit_callback);
ashea6 3:5f0b28699a67 70 snooze.setSampleFrequency();
ashea6 3:5f0b28699a67 71 off.setSampleFrequency();
ashea6 2:4d370e3f4618 72
ashea6 2:4d370e3f4618 73
ashea6 2:4d370e3f4618 74
ashea6 2:4d370e3f4618 75 // Initialize LCD
ashea6 2:4d370e3f4618 76 uLCD.baudrate(115200);
ashea6 2:4d370e3f4618 77 uLCD.background_color(BLACK);
ashea6 2:4d370e3f4618 78 uLCD.cls();
ashea6 2:4d370e3f4618 79
ashea6 2:4d370e3f4618 80 // Connect to network and wait for DHCP
ashea6 2:4d370e3f4618 81 uLCD.locate(0,0);
ashea6 2:4d370e3f4618 82 uLCD.printf("Getting IP Address\n");
ashea6 2:4d370e3f4618 83 eth.init();
ashea6 2:4d370e3f4618 84 if ( eth.connect(60000) == -1 ) {
ashea6 2:4d370e3f4618 85 uLCD.printf("ERROR: Could not\nget IP address");
ashea6 2:4d370e3f4618 86 return -1;
ashea6 2:4d370e3f4618 87 }
ashea6 2:4d370e3f4618 88 uLCD.printf("IP address is \n%s\n\n",eth.getIPAddress());
ashea6 2:4d370e3f4618 89 Thread::wait(1000);
ashea6 2:4d370e3f4618 90
ashea6 2:4d370e3f4618 91 // Read time from server
ashea6 2:4d370e3f4618 92 uLCD.printf("Reading time...\n\r");
ashea6 2:4d370e3f4618 93 ntp_client.setTime(domain_name, port_number);
ashea6 2:4d370e3f4618 94 uLCD.printf("Time set\n");
ashea6 2:4d370e3f4618 95 Thread::wait(2000);
ashea6 2:4d370e3f4618 96 eth.disconnect();
ashea6 2:4d370e3f4618 97
ashea6 2:4d370e3f4618 98 // Reset LCD
ashea6 2:4d370e3f4618 99 uLCD.background_color(WHITE);
ashea6 2:4d370e3f4618 100 uLCD.textbackground_color(WHITE);
ashea6 2:4d370e3f4618 101 uLCD.color(RED);
ashea6 2:4d370e3f4618 102 uLCD.cls();
ashea6 2:4d370e3f4618 103 uLCD.text_height(2);
ashea6 2:4d370e3f4618 104
ashea6 2:4d370e3f4618 105 Thread thread_time(time_thread);
ashea6 2:4d370e3f4618 106
4180_1 1:5b8e223e983d 107 FILE *wave_file;
4180_1 1:5b8e223e983d 108 printf("\n\n\nHello, wave world!\n");
ashea6 2:4d370e3f4618 109 wave_file=fopen("/sd/bob.wav","r");
4180_1 1:5b8e223e983d 110 waver.play(wave_file);
4180_1 1:5b8e223e983d 111 fclose(wave_file);
4180_1 1:5b8e223e983d 112 }