![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
IOT Cooler that has an integrated MP3 Player attached
Dependencies: 4DGL-uLCD-SE PinDetect SDFileSystem mbed wave_player
Revision 0:16db2db8886d, committed 2015-05-01
- Comitter:
- anevil14
- Date:
- Fri May 01 16:52:11 2015 +0000
- Commit message:
- IOT Cooler Code
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/4DGL-uLCD-SE.lib Fri May 01 16:52:11 2015 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/anevil14/code/4DGL-uLCD-SE/#df77db998389
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PinDetect.lib Fri May 01 16:52:11 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/AjK/code/PinDetect/#cb3afc45028b
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SDFileSystem.lib Fri May 01 16:52:11 2015 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/anevil14/code/SDFileSystem/#2cf87e7f54e7
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SystemState.h Fri May 01 16:52:11 2015 +0000 @@ -0,0 +1,10 @@ +#ifndef SystemState_H +#define SystemState_H +enum SystemState +{ + stop = 0, + play_state, + Need_SD +}; + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TMP36.h Fri May 01 16:52:11 2015 +0000 @@ -0,0 +1,31 @@ +#include "mbed.h" + +//Setup a new class for TMP36 sensor +class TMP36 +{ +public: + TMP36(PinName pin); + TMP36(); + operator float (); + float read(); +private: +//class sets up the AnalogIn pin + AnalogIn _pin; +}; + +TMP36::TMP36(PinName pin) : _pin(pin) +{ +// _pin(pin) means pass pin to the AnalogIn constructor +} + +float TMP36::read() +{ +//convert sensor reading to temperature in degrees C + return ((_pin.read()*3.3)-0.500)*100.0; +} +//overload of float conversion (avoids needing to type .read() in equations) +TMP36::operator float () +{ +//convert sensor reading to temperature in degrees C + return ((_pin.read()*3.3)-0.500)*100.0; +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WavDis.cpp Fri May 01 16:52:11 2015 +0000 @@ -0,0 +1,113 @@ +#include "WavDis.h" + +void WavDis::Update() +{ + UpdateLEDs(); + + string s = ""; + + switch (currentState) + { + case stop: + s = currentSong.substr(0,(currentSong.size() - 4)); + //FirstLine + if (screen_lock == false) { + screen_lock = true; + lcd.text_width(1.5); //4X size text + lcd.text_height(1.5); + lcd.color(GREEN); + lcd.locate(0,1); + lcd.printf("%s ", s.c_str()); + //SecondLine + lcd.locate(0,2); + lcd.printf("Stopped "); + screen_lock = false; + } + break; + case play_state: + s = currentSong.substr(0,(currentSong.size() - 4)); + //FirstLine + if (screen_lock == false) { + screen_lock = true; + lcd.text_width(1.5); //4X size text + lcd.text_height(1.5); + lcd.color(GREEN); + lcd.locate(0,1); + lcd.printf("%s ", s.c_str()); + //SecondLine + lcd.locate(0,2); + lcd.printf("Now Playing"); + screen_lock = false; + } + break; + case Need_SD: + lcd.locate(0,0); + lcd.printf("Insert SD Card"); + break; + } +} + +void WavDis::UpdateTemp(float temp) { + if (screen_lock == false) { + screen_lock = true; + lcd.text_width(1.5); //4X size text + lcd.text_height(1.5); + lcd.color(WHITE); + lcd.locate(0,7); + if (temp < 60.00) { + lcd.filled_circle(60, 100, 20, BLUE); + //lcd.locate(0,8); + lcd.printf("Temperature: \n%.2f", temp); + } else if (temp >= 60.00) { + lcd.filled_circle(60, 100, 20, RED); + //lcd.locate(0,8); + lcd.printf("Temperature: \n%.2f", temp); + } + screen_lock = false; + } + return; +} + +void WavDis::UpdateSong(string songName) +{ + currentSong = songName; + Update(); +} + +void WavDis::UpdateState(SystemState state) +{ + currentState = state; + Update(); +} + +void WavDis::UpdateVolume(int volume) +{ + if (screen_lock == false) { + screen_lock = true; + lcd.text_width(1); //4X size text + lcd.text_height(1); + lcd.color(GREEN); + lcd.locate(0,3); + lcd.printf("Volume: \n%d ", volume); + screen_lock = false; + } +} + +void WavDis::UpdateLEDs() +{ + led1 = 1; + switch (currentState) + { + case stop: + led2 = 0; + led3 = 0; + led4 = 0; + break; + case play_state: + led2 = 1; + led3 = 1; + led4 = 1; + break; + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WavDis.h Fri May 01 16:52:11 2015 +0000 @@ -0,0 +1,46 @@ +#ifndef WavDis_H +#define WavDis_H + +#include "mbed.h" +#include "uLCD_4DGL.h" +#include "SystemState.h" +#include <string> + + +using std::string; + +class WavDis +{ + +public: + WavDis(): led1(LED1) , led2(LED2), led3(LED3), led4(LED4), lcd(p28, p27, p30) + { + currentState = stop; + currentSong = ""; + screen_lock = false; + } + + void UpdateSong(string); + void UpdateState(SystemState); + void UpdateTemp(float); + void UpdateVolume(int); + +private: + + void Update(); + void UpdateLEDs(); + + DigitalOut led1; + DigitalOut led2; + DigitalOut led3; + DigitalOut led4; + + SystemState currentState; + string currentSong; + bool screen_lock; + //LCD + uLCD_4DGL lcd; + +}; + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Fri May 01 16:52:11 2015 +0000 @@ -0,0 +1,329 @@ +#include "mbed.h" +#include "TMP36.h" +#include "PinDetect.h" +#include "SDFileSystem.h" +#include "SystemState.h" +#include "wave_player.h" +#include "WavDis.h" +#include "time.h" +#include <vector> +#include <string> +#include <stdio.h> + +using std::string; + +PinDetect fwdButton(p17, PullUp); +PinDetect revButton(p16, PullUp); +PinDetect playButton(p12, PullUp); +PinDetect volumeButton(p20, PullUp); + +Serial rn42(p13,p14); // Bluetooth Module +Serial pc(USBTX, USBRX); + +SDFileSystem sd(p5, p6, p7, p8, p9, "sd"); //SD card extra pin p9 + +AnalogOut DACout(p18); +wave_player waver(&DACout); + +float Current_temp = 0.0; +float Current_temp1 = 0.0; +float Current_temp2 = 0.0; + +TMP36 myTMP36(p15); +Timer t; + +const char* cmds[] = {"MUSIC:UP", "MUSIC:STOP", "MUSIC:PLAY", "MUSIC:NEXT", "MUSIC:PREV", "MUSIC:DOWN"}; +SystemState currentState = stop; +string currentSong; +string fileDirectory; +int currentIndex; +int librarySize; +int volumeControl; +bool playStop; +float const rate = 1.0; +string fileName = ""; + +WavDis display; + + +vector<string> filenames; //filenames are stored in a vector string + +//PROTOCOL: +// ex) TEMPERATURE:30 --> reading of 30 degrees +// +bool writeLine (char* towrite, int length) +{ + if (rn42.writeable()) { + for (int i = 0; i < length; i++) { + if (towrite[i] == NULL ) { + break; + } + pc.printf("|%c",towrite[i]); + rn42.putc(towrite[i]); + } + rn42.putc('\n'); + rn42.putc(0); + return true; + } + + return false; +} + + +bool readLine (char* buffer, int sizebuffer) +{ + char tmpRead = 0; + + if (rn42.readable() == 0){ + return false; + } + int i; + for (i= 0 ; i< sizebuffer-1; i++) { + + //if (rn42.readable()) { + tmpRead = rn42.getc(); + //} else { + // break; + //} + + if (tmpRead == '\n' || tmpRead == 0) { + break; + } + buffer[i] = tmpRead; + if (i == 0 && tmpRead == 0) { + return false; + } + } + + buffer[i] = 0; + if (i == 0 ) { + return false; + } + + return true; + +} +/* +void Rx_interrupt(void) { + pc.printf("I blocked you bitch"); + pc.putc(rn42.getc()); + pc.printf("jk"); +}*/ + +//UPDATE NECESSARY VARIABLES EACH TIME A BUTTON EVEN OCCURS +void Update(int index, SystemState state) +{ + currentState = state; + currentIndex = index; + currentSong = filenames[currentIndex]; + display.UpdateState(currentState); + display.UpdateSong(currentSong); + display.UpdateVolume(15-volumeControl); + Current_temp = (1.8*myTMP36.read())+32; + display.UpdateTemp(Current_temp); + switch(currentState) { + case stop: + playStop = false; + break; + case play_state: + playStop = true; + break; + } +} + + +void getWaveFiles() +{ + if (!sd.SDInserted()) { + while (!sd.SDInserted()) { + //check for sd card until true + display.UpdateState(Need_SD); + printf("SD Card Not Found\n"); + wait(1); + } + wait(2); + sd.disk_initialize(); + wait(2); + } + + wait(2); + DIR *dp; + struct dirent *dirp; + dp = opendir(fileDirectory.c_str()); + + while((dirp = readdir(dp)) != NULL) { + filenames.push_back(string(dirp->d_name)); + } + // print filename strings from vector using an iterator + for(vector<string>::iterator it=filenames.begin(); it < filenames.end(); it++) { + printf("%s\n\r",(*it).c_str()); + } + + librarySize = filenames.size(); + if (librarySize == 0) { + printf("Library Empty\n\n"); + return; + } + + fileDirectory.append("/"); + + Update(0,stop); +} + + + +void fwdButton_Event(void) +{ + if (currentState == stop) { + currentIndex++; //INCREMENT CURRENT INDEX (0 TO NUMBER OF SONGS -1) + if(currentIndex >= librarySize) { + currentIndex = 0; + } + Update(currentIndex,currentState); + } +} +void revButton_Event(void) +{ + if (currentState == stop) { + currentIndex--; //DECREMENT CURRENT INDEX (0 TO NUMBER OF SONGS -1) + if(currentIndex < 0) { + currentIndex = (librarySize - 1); + } + Update(currentIndex,currentState); + } +} + +void playButton_Event(void) +{ + + switch (currentState) { + case stop: + Update(currentIndex, play_state); + break; + case play_state: + Update(currentIndex, stop); + break; + } +} +void volumeUpButton_Event(void) +{ + if ((volumeControl >= 0) && (volumeControl < 15)){ + volumeControl++; + } + display.UpdateVolume(15-volumeControl); +} +void volumeButton_Event(void) +{ + if(volumeControl >= 15) { + volumeControl = 0; + } else { + volumeControl++; + } + display.UpdateVolume(15-volumeControl); +} + +void volumeDownButton_Event(void) +{ + if(volumeControl > 0) { + volumeControl--; + } + display.UpdateVolume(15-volumeControl); +} + +int main() +{ + + // BlueTooth Module + rn42.baud(19600); + pc.baud(9600); + + // MP3 Player + currentState = stop; + currentSong = ""; + librarySize = 0; + volumeControl = 0; + currentIndex = -1; + fileDirectory = "/sd/myMusic"; + char output[50]; + string temp; + bool didRead = false; + char buf [100]; + + fwdButton.attach_asserted(&fwdButton_Event); + fwdButton.setSampleFrequency(); + revButton.attach_asserted(&revButton_Event); + revButton.setSampleFrequency(); + playButton.attach_asserted(&playButton_Event); + playButton.setSampleFrequency(); + volumeButton.attach_asserted(&volumeButton_Event); + volumeButton.setSampleFrequency(); + //rn42.attach(&Rx_interrupt, Serial::RxIrq); + + getWaveFiles(); + pc.printf("init"); + + while(1) { + switch (currentState) { + case stop: + break; + case play_state: + string fileName = ""; + fileName.append(fileDirectory + currentSong); + printf("%s\n\r",fileName.c_str()); + FILE *wave_file; + wave_file=fopen(fileName.c_str(),"r"); + + waver.play(wave_file, &playStop, &volumeControl); + + fclose(wave_file); + Update(currentIndex,stop); + break; + } + wait(0.1); + t.start(); + + if (t.read() >= rate) { + Current_temp = (1.8*myTMP36.read())+32; + Current_temp1 = (1.8*myTMP36.read())+32; + Current_temp2 = (1.8*myTMP36.read())+32; + Current_temp = (Current_temp + Current_temp1 + Current_temp2) / 3.00; + display.UpdateTemp(Current_temp); + snprintf(output,50,"%.2f", Current_temp); + writeLine (output, 50); + t.reset(); + t.start(); + } + + + didRead = readLine(buf, 100); + pc.printf("(%s)\n",buf); + if (didRead) { + if (strcmp(buf,cmds[0]) == 0) { + pc.printf("Volume Increase"); + volumeUpButton_Event(); + } + if (strcmp(buf,cmds[1]) == 0) { + pc.printf("STOP"); + playButton_Event(); + } + if (strcmp(buf,cmds[2]) == 0) { + pc.printf("PLAY"); + playButton_Event(); + } + if (strcmp(buf,cmds[3]) == 0) { + pc.printf("FORWARD"); + fwdButton_Event(); + } + if (strcmp(buf,cmds[4]) == 0) { + pc.printf("REVERSE"); + revButton_Event(); + } + if (strcmp(buf,cmds[5]) == 0) { + pc.printf("DOWN"); + volumeDownButton_Event(); + } + + } + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Fri May 01 16:52:11 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/433970e64889 \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/wave_player.lib Fri May 01 16:52:11 2015 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/anevil14/code/wave_player/#68f1136f2ea6