Part 2 of our ECE 4180 Final Project

Dependencies:   4DGL-uLCD-SE MODSERIAL USBHost wave_player

Committer:
mohit1234
Date:
Fri May 01 18:10:34 2015 +0000
Revision:
0:25c861ee0c38
;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mohit1234 0:25c861ee0c38 1 #include <vector>
mohit1234 0:25c861ee0c38 2 #include <string>
mohit1234 0:25c861ee0c38 3 #include "mbed.h"
mohit1234 0:25c861ee0c38 4 #include "wave_player.h"
mohit1234 0:25c861ee0c38 5 #include "Speaker.h"
mohit1234 0:25c861ee0c38 6 #include "uLCD_4DGL.h"
mohit1234 0:25c861ee0c38 7 #include "USBHostMSD.h"
mohit1234 0:25c861ee0c38 8 #include "rtos.h"
mohit1234 0:25c861ee0c38 9
mohit1234 0:25c861ee0c38 10 #define MODSERIAL_DEFAULT_RX_BUFFER_SIZE 512
mohit1234 0:25c861ee0c38 11 #define MODSERIAL_DEFAULT_TX_BUFFER_SIZE 1024
mohit1234 0:25c861ee0c38 12
mohit1234 0:25c861ee0c38 13 #include "MODSERIAL.h"
mohit1234 0:25c861ee0c38 14
mohit1234 0:25c861ee0c38 15 using namespace std;
mohit1234 0:25c861ee0c38 16
mohit1234 0:25c861ee0c38 17 MODSERIAL device(p13,p14);
mohit1234 0:25c861ee0c38 18 MODSERIAL pc(USBTX, USBRX);
mohit1234 0:25c861ee0c38 19
mohit1234 0:25c861ee0c38 20 AnalogOut DACout(p18);
mohit1234 0:25c861ee0c38 21 wave_player waver(&DACout);
mohit1234 0:25c861ee0c38 22
mohit1234 0:25c861ee0c38 23 uLCD_4DGL screen1(p28, p27, p11);
mohit1234 0:25c861ee0c38 24 uLCD_4DGL screen2(p9, p10, p11);
mohit1234 0:25c861ee0c38 25
mohit1234 0:25c861ee0c38 26 Mutex mutex;
mohit1234 0:25c861ee0c38 27
mohit1234 0:25c861ee0c38 28 volatile bool playAlarm = false;
mohit1234 0:25c861ee0c38 29
mohit1234 0:25c861ee0c38 30 void musicThread(void const*);
mohit1234 0:25c861ee0c38 31
mohit1234 0:25c861ee0c38 32 void getDate()
mohit1234 0:25c861ee0c38 33 {
mohit1234 0:25c861ee0c38 34 //mutex.lock();
mohit1234 0:25c861ee0c38 35 device.printf("date\n");
mohit1234 0:25c861ee0c38 36 //pc.printf("Requesting date\n");
mohit1234 0:25c861ee0c38 37 //mutex.unlock();
mohit1234 0:25c861ee0c38 38 }
mohit1234 0:25c861ee0c38 39
mohit1234 0:25c861ee0c38 40 void getTime()
mohit1234 0:25c861ee0c38 41 {
mohit1234 0:25c861ee0c38 42 //mutex.lock();
mohit1234 0:25c861ee0c38 43 device.printf("time\n");
mohit1234 0:25c861ee0c38 44 //pc.printf("Requesting time\n");
mohit1234 0:25c861ee0c38 45 //mutex.unlock();
mohit1234 0:25c861ee0c38 46 }
mohit1234 0:25c861ee0c38 47
mohit1234 0:25c861ee0c38 48 void getAlarm()
mohit1234 0:25c861ee0c38 49 {
mohit1234 0:25c861ee0c38 50 //mutex.lock();
mohit1234 0:25c861ee0c38 51 device.printf("alarm\n");
mohit1234 0:25c861ee0c38 52 //pc.printf("Requesting Alarm\n");
mohit1234 0:25c861ee0c38 53 //mutex.unlock();
mohit1234 0:25c861ee0c38 54 }
mohit1234 0:25c861ee0c38 55
mohit1234 0:25c861ee0c38 56 string date;
mohit1234 0:25c861ee0c38 57 string t;
mohit1234 0:25c861ee0c38 58 volatile bool playingMusic = false;
mohit1234 0:25c861ee0c38 59
mohit1234 0:25c861ee0c38 60 void printDate()
mohit1234 0:25c861ee0c38 61 {
mohit1234 0:25c861ee0c38 62 // TODO - IMPLEMENT THIS SHIT
mohit1234 0:25c861ee0c38 63 }
mohit1234 0:25c861ee0c38 64
mohit1234 0:25c861ee0c38 65 void printTime()
mohit1234 0:25c861ee0c38 66 {
mohit1234 0:25c861ee0c38 67 // TODO - IMPLEMENT THIS SHIT
mohit1234 0:25c861ee0c38 68 }
mohit1234 0:25c861ee0c38 69
mohit1234 0:25c861ee0c38 70 /*void playAlarm()
mohit1234 0:25c861ee0c38 71 {
mohit1234 0:25c861ee0c38 72 //if (playingMusic) return;
mohit1234 0:25c861ee0c38 73 //Thread music(musicThread, NULL, osPriorityNormal, 1024 * 4);
mohit1234 0:25c861ee0c38 74
mohit1234 0:25c861ee0c38 75 }*/
mohit1234 0:25c861ee0c38 76
mohit1234 0:25c861ee0c38 77 void timeThread(void const* args)
mohit1234 0:25c861ee0c38 78 {
mohit1234 0:25c861ee0c38 79 //screen1.locate(0,5);
mohit1234 0:25c861ee0c38 80 //screen1.color(WHITE);
mohit1234 0:25c861ee0c38 81 //screen1.textbackground_color(RED);
mohit1234 0:25c861ee0c38 82 //screen1.printf("I AM IN THE THREAD\n");
mohit1234 0:25c861ee0c38 83
mohit1234 0:25c861ee0c38 84 getDate();
mohit1234 0:25c861ee0c38 85 while (!device.readable());
mohit1234 0:25c861ee0c38 86 char buffer[80];
mohit1234 0:25c861ee0c38 87 device.scanf("%s", buffer);
mohit1234 0:25c861ee0c38 88 date = string(buffer);
mohit1234 0:25c861ee0c38 89 char buffer2[80];
mohit1234 0:25c861ee0c38 90 device.scanf("%s", buffer2);
mohit1234 0:25c861ee0c38 91 date += (" " + string(buffer2));
mohit1234 0:25c861ee0c38 92 char buffer3[80];
mohit1234 0:25c861ee0c38 93 device.scanf("%s", buffer3);
mohit1234 0:25c861ee0c38 94 date += (" " + string(buffer3));
mohit1234 0:25c861ee0c38 95
mohit1234 0:25c861ee0c38 96 printDate();
mohit1234 0:25c861ee0c38 97
mohit1234 0:25c861ee0c38 98 while (1) {
mohit1234 0:25c861ee0c38 99 getTime();
mohit1234 0:25c861ee0c38 100 while (!device.readable());
mohit1234 0:25c861ee0c38 101 char buffert[80];
mohit1234 0:25c861ee0c38 102 device.scanf("%s", buffert);
mohit1234 0:25c861ee0c38 103 t = string(buffert);
mohit1234 0:25c861ee0c38 104
mohit1234 0:25c861ee0c38 105 printTime();
mohit1234 0:25c861ee0c38 106
mohit1234 0:25c861ee0c38 107 getAlarm();
mohit1234 0:25c861ee0c38 108 while (!device.readable());
mohit1234 0:25c861ee0c38 109 char buffera[20];
mohit1234 0:25c861ee0c38 110 device.scanf("%s", buffera);
mohit1234 0:25c861ee0c38 111 if (buffera[0] == '1') {
mohit1234 0:25c861ee0c38 112 playAlarm = true;
mohit1234 0:25c861ee0c38 113 }
mohit1234 0:25c861ee0c38 114
mohit1234 0:25c861ee0c38 115 Thread::wait(6000);
mohit1234 0:25c861ee0c38 116 }
mohit1234 0:25c861ee0c38 117 }
mohit1234 0:25c861ee0c38 118
mohit1234 0:25c861ee0c38 119 void musicThread(void const *args)
mohit1234 0:25c861ee0c38 120 {
mohit1234 0:25c861ee0c38 121 if (playingMusic) return;
mohit1234 0:25c861ee0c38 122 USBHostMSD msd("usb");
mohit1234 0:25c861ee0c38 123 while (1) {
mohit1234 0:25c861ee0c38 124 while (!playAlarm);
mohit1234 0:25c861ee0c38 125
mohit1234 0:25c861ee0c38 126 playingMusic = true;
mohit1234 0:25c861ee0c38 127 //pc.printf("Playing\n");
mohit1234 0:25c861ee0c38 128
mohit1234 0:25c861ee0c38 129 //pc.printf("USB\n");
mohit1234 0:25c861ee0c38 130 // try to connect a MSD device
mohit1234 0:25c861ee0c38 131 while(!msd.connect()) {
mohit1234 0:25c861ee0c38 132 Thread::wait(500);
mohit1234 0:25c861ee0c38 133 }
mohit1234 0:25c861ee0c38 134 //pc.printf("Connected\n");
mohit1234 0:25c861ee0c38 135 FILE * fp = fopen("/usb/sample.wav", "r");
mohit1234 0:25c861ee0c38 136
mohit1234 0:25c861ee0c38 137 if (fp != NULL) {
mohit1234 0:25c861ee0c38 138 waver.play(fp);
mohit1234 0:25c861ee0c38 139 fclose(fp);
mohit1234 0:25c861ee0c38 140 } else {
mohit1234 0:25c861ee0c38 141 }
mohit1234 0:25c861ee0c38 142
mohit1234 0:25c861ee0c38 143 playAlarm = false;
mohit1234 0:25c861ee0c38 144 playingMusic = false;
mohit1234 0:25c861ee0c38 145 }
mohit1234 0:25c861ee0c38 146 }
mohit1234 0:25c861ee0c38 147
mohit1234 0:25c861ee0c38 148
mohit1234 0:25c861ee0c38 149 void setupScreens()
mohit1234 0:25c861ee0c38 150 {
mohit1234 0:25c861ee0c38 151 screen1.baudrate(3000000);
mohit1234 0:25c861ee0c38 152 screen1.cls();
mohit1234 0:25c861ee0c38 153 screen2.baudrate(3000000);
mohit1234 0:25c861ee0c38 154 screen2.cls();
mohit1234 0:25c861ee0c38 155 }
mohit1234 0:25c861ee0c38 156
mohit1234 0:25c861ee0c38 157
mohit1234 0:25c861ee0c38 158 int main()
mohit1234 0:25c861ee0c38 159 {
mohit1234 0:25c861ee0c38 160 setupScreens();
mohit1234 0:25c861ee0c38 161
mohit1234 0:25c861ee0c38 162 screen1.locate(0,5);
mohit1234 0:25c861ee0c38 163 screen1.color(WHITE);
mohit1234 0:25c861ee0c38 164 screen1.textbackground_color(RED);
mohit1234 0:25c861ee0c38 165 screen1.printf("Song Playing\n");
mohit1234 0:25c861ee0c38 166
mohit1234 0:25c861ee0c38 167 Thread thread(musicThread, NULL, osPriorityNormal, 1024 * 4);
mohit1234 0:25c861ee0c38 168 Thread tThread(timeThread);
mohit1234 0:25c861ee0c38 169
mohit1234 0:25c861ee0c38 170 while (1);
mohit1234 0:25c861ee0c38 171 }