ECE 4180 Final

Dependencies:   mbed wave_player mbed-rtos C12832_lcd 4DGL-uLCD-SE LCD_fonts SDFileSystem

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "SDFileSystem.h"
00003 #include "wave_player.h"
00004 #include "uLCD_4DGL.h"
00005 //setup some color objects in flash using const's
00006 
00007 #include "rtos.h"
00008 #include "Small_6.h"
00009 #include "Small_7.h"
00010 #include "Arial_9.h"
00011 #include "stdio.h"
00012 #include "C12832_lcd.h"
00013 
00014 //class for 3 PWM color values for RGBLED
00015 class LEDColor
00016 {
00017 public:
00018     LEDColor(float r, float g, float b);
00019     float red;
00020     float green;
00021     float blue;
00022 };
00023 LEDColor:: LEDColor(float r, float g, float b)
00024     : red(r), green(g), blue(b)
00025 {
00026 }
00027 //Operator overload to adjust brightness with no color change
00028 LEDColor operator * (const LEDColor& x, const float& b)
00029 {
00030     return LEDColor(x.red*b,x.green*b,x.blue*b);
00031 }
00032 //Operator overload to add colors
00033 LEDColor operator + (const LEDColor& x, const LEDColor& y)
00034 {
00035     return LEDColor(x.red+y.red,x.green+y.green,x.blue+y.blue);
00036 }
00037  
00038 //Class to control an RGB LED using three PWM pins
00039 class RGBLed
00040 {
00041 public:
00042     RGBLed(PinName redpin, PinName greenpin, PinName bluepin);
00043     void write(float red,float green, float blue);
00044     void write(LEDColor c);
00045     RGBLed operator = (LEDColor c) {
00046         write(c);
00047         return *this;
00048     };
00049 private:
00050     PwmOut _redpin;
00051     PwmOut _greenpin;
00052     PwmOut _bluepin;
00053 };
00054  
00055 RGBLed::RGBLed (PinName redpin, PinName greenpin, PinName bluepin)
00056     : _redpin(redpin), _greenpin(greenpin), _bluepin(bluepin)
00057 {
00058     //50Hz PWM clock default a bit too low, go to 2000Hz (less flicker)
00059     _redpin.period(0.0005);
00060 }
00061  
00062 void RGBLed::write(float red,float green, float blue)
00063 {
00064     _redpin = red;
00065     _greenpin = green;
00066     _bluepin = blue;
00067 }
00068 void RGBLed::write(LEDColor c)
00069 {
00070     _redpin = c.red;
00071     _greenpin = c.green;
00072     _bluepin = c.blue;
00073 }
00074  
00075 //classes could be moved to include file
00076  
00077 //Setup RGB led using PWM pins and class
00078 RGBLed myRGBled(p23,p22,p21); //RGB PWM pins
00079 const LEDColor red(1.0,0.0,0.0);
00080 const LEDColor green(0.0,0.2,0.0);
00081 //brighter green LED is scaled down to same as red and
00082 //blue LED outputs on Sparkfun RGBLED
00083 const LEDColor blue(0.0,0.0,1.0);
00084 const LEDColor yellow(1.0,0.2,0.0);
00085 const LEDColor white(1.0,0.2,1.0);
00086 const LEDColor black(0.0,0.0,0.0);
00087 char bred=0;
00088 char bgreen=0;
00089 char bblue=0; 
00090 
00091 volatile bool songselect = false;
00092 volatile bool homescreen = true;
00093 uLCD_4DGL uLCD(p28,p27,p30); 
00094 SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card
00095 DigitalOut myled(LED1);
00096 DigitalIn pb1(p20);
00097 DigitalIn pb2(p19);
00098 AnalogOut DACout(p18);
00099 wave_player waver(&DACout);
00100 
00101 // mutex to make the lcd lib thread safe
00102 Mutex lcd_mutex;
00103 int songnum = 1;
00104 AnalogIn joy_pot(p16);
00105 // Thread 1
00106 // print homescreen to LCD
00107 void thread1(void const *args)
00108 {
00109     
00110     while(true) {       // thread loop
00111         
00112         if (homescreen){
00113             lcd_mutex.lock();
00114             uLCD.cls();
00115             uLCD.text_height(1.9);
00116             uLCD.text_width(1.9);
00117             uLCD.color(WHITE);
00118             uLCD.locate(0,0);
00119             uLCD.printf("Pick a song");
00120             uLCD.text_height(1.9);
00121             uLCD.text_width(1.9);
00122             uLCD.locate(1,2);
00123             uLCD.printf("Song1");
00124             uLCD.locate(1,4);
00125             uLCD.printf("Song2");
00126             uLCD.locate(1,6);
00127             uLCD.printf("Song3");
00128             uLCD.locate(1,8);
00129             uLCD.printf("Song4");
00130             uLCD.rectangle(5, songnum*16-2, 100, songnum*16+8 ,GREEN);
00131             lcd_mutex.unlock();
00132             
00133                 }
00134             Thread::wait(200);    
00135     }
00136 }
00137 
00138 //for song selection during homescreen
00139 void thread2(void const *args)
00140 {
00141 
00142    while(1){
00143         if (homescreen){
00144             if ((joy_pot <= (1.4/3.3)) && songnum>1) {
00145                 songnum--;
00146                 } 
00147             else if ((joy_pot >= (1.8/3.3)) && songnum<4){
00148                 songnum++;
00149                 }
00150             }
00151         Thread::wait(250);
00152         
00153     }
00154 }
00155 
00156 // Thread 3
00157 //pb1 is to select song
00158 //pb2 is to return to homescreen
00159 void thread3(void const *args)
00160 {
00161     pb1.mode(PullUp);
00162     pb2.mode(PullUp);
00163     while(true) {       // thread loop
00164         if (!pb2)
00165             {
00166             homescreen = true;
00167             songselect = false;
00168             }
00169         if (!pb1)
00170             {
00171             songselect = true;
00172             }
00173         Thread::wait(100);   // value of pot1 / 100
00174     }
00175 }
00176 
00177 int main()
00178 {
00179     //doesnt work yet, need to ask hamblen
00180 
00181     uLCD.media_init();
00182     uLCD.set_sector_address(0x001D, 0x4C01);
00183     uLCD.display_image(0,0);
00184 //    t1.start(thread1);
00185 //   t2.start(thread2);
00186 //    t3.start(thread3);
00187     Thread t1(thread1);
00188     Thread t2(thread2);
00189     Thread t3(thread3);
00190     //startup sound. Commented out for testing without SD card
00191     //FILE *wave_file;
00192     //wave_file=fopen("/sd/cheer.wav","r");
00193     //waver.play(wave_file);
00194     //fclose(wave_file);
00195     while(1) 
00196     {
00197         
00198         if (songselect){
00199                 myled = 0;
00200                 homescreen = false;
00201                 lcd_mutex.lock();
00202                 uLCD.cls();
00203                 uLCD.printf("You selected song %2d",songnum);
00204                 lcd_mutex.unlock();
00205                 //add case statement based on songnum or something
00206                 //code for playing song from sd
00207                 //FILE *wave_file;
00208                 //wave_file=fopen("/sd/sample1.wav","r");
00209                 //waver.play(wave_file);
00210                 //fclose(wave_file);
00211                 //end
00212                 }
00213         Thread::wait(100);    
00214         }
00215 }
00216         
00217