![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
ECE 4180 Final
Dependencies: mbed wave_player mbed-rtos C12832_lcd 4DGL-uLCD-SE LCD_fonts SDFileSystem
main.cpp@2:765774781bb1, 2019-12-03 (annotated)
- Committer:
- yqin70
- Date:
- Tue Dec 03 22:57:12 2019 +0000
- Revision:
- 2:765774781bb1
- Parent:
- 1:a1aa9a79070a
- Child:
- 3:431877852f14
v3
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yqin70 | 0:4f77ae831ee7 | 1 | #include "mbed.h" |
yqin70 | 0:4f77ae831ee7 | 2 | #include "SDFileSystem.h" |
yqin70 | 0:4f77ae831ee7 | 3 | #include "wave_player.h" |
yqin70 | 0:4f77ae831ee7 | 4 | #include "uLCD_4DGL.h" |
yqin70 | 0:4f77ae831ee7 | 5 | //setup some color objects in flash using const's |
yqin70 | 0:4f77ae831ee7 | 6 | |
yqin70 | 0:4f77ae831ee7 | 7 | #include "rtos.h" |
yqin70 | 0:4f77ae831ee7 | 8 | #include "Small_6.h" |
yqin70 | 0:4f77ae831ee7 | 9 | #include "Small_7.h" |
yqin70 | 0:4f77ae831ee7 | 10 | #include "Arial_9.h" |
yqin70 | 0:4f77ae831ee7 | 11 | #include "stdio.h" |
yqin70 | 0:4f77ae831ee7 | 12 | #include "C12832_lcd.h" |
yqin70 | 0:4f77ae831ee7 | 13 | |
yqin70 | 0:4f77ae831ee7 | 14 | //class for 3 PWM color values for RGBLED |
yqin70 | 0:4f77ae831ee7 | 15 | class LEDColor |
yqin70 | 0:4f77ae831ee7 | 16 | { |
yqin70 | 0:4f77ae831ee7 | 17 | public: |
yqin70 | 0:4f77ae831ee7 | 18 | LEDColor(float r, float g, float b); |
yqin70 | 0:4f77ae831ee7 | 19 | float red; |
yqin70 | 0:4f77ae831ee7 | 20 | float green; |
yqin70 | 0:4f77ae831ee7 | 21 | float blue; |
yqin70 | 0:4f77ae831ee7 | 22 | }; |
yqin70 | 0:4f77ae831ee7 | 23 | LEDColor:: LEDColor(float r, float g, float b) |
yqin70 | 0:4f77ae831ee7 | 24 | : red(r), green(g), blue(b) |
yqin70 | 0:4f77ae831ee7 | 25 | { |
yqin70 | 0:4f77ae831ee7 | 26 | } |
yqin70 | 0:4f77ae831ee7 | 27 | //Operator overload to adjust brightness with no color change |
yqin70 | 0:4f77ae831ee7 | 28 | LEDColor operator * (const LEDColor& x, const float& b) |
yqin70 | 0:4f77ae831ee7 | 29 | { |
yqin70 | 0:4f77ae831ee7 | 30 | return LEDColor(x.red*b,x.green*b,x.blue*b); |
yqin70 | 0:4f77ae831ee7 | 31 | } |
yqin70 | 0:4f77ae831ee7 | 32 | //Operator overload to add colors |
yqin70 | 0:4f77ae831ee7 | 33 | LEDColor operator + (const LEDColor& x, const LEDColor& y) |
yqin70 | 0:4f77ae831ee7 | 34 | { |
yqin70 | 0:4f77ae831ee7 | 35 | return LEDColor(x.red+y.red,x.green+y.green,x.blue+y.blue); |
yqin70 | 0:4f77ae831ee7 | 36 | } |
yqin70 | 0:4f77ae831ee7 | 37 | |
yqin70 | 0:4f77ae831ee7 | 38 | //Class to control an RGB LED using three PWM pins |
yqin70 | 0:4f77ae831ee7 | 39 | class RGBLed |
yqin70 | 0:4f77ae831ee7 | 40 | { |
yqin70 | 0:4f77ae831ee7 | 41 | public: |
yqin70 | 0:4f77ae831ee7 | 42 | RGBLed(PinName redpin, PinName greenpin, PinName bluepin); |
yqin70 | 0:4f77ae831ee7 | 43 | void write(float red,float green, float blue); |
yqin70 | 0:4f77ae831ee7 | 44 | void write(LEDColor c); |
yqin70 | 0:4f77ae831ee7 | 45 | RGBLed operator = (LEDColor c) { |
yqin70 | 0:4f77ae831ee7 | 46 | write(c); |
yqin70 | 0:4f77ae831ee7 | 47 | return *this; |
yqin70 | 0:4f77ae831ee7 | 48 | }; |
yqin70 | 0:4f77ae831ee7 | 49 | private: |
yqin70 | 0:4f77ae831ee7 | 50 | PwmOut _redpin; |
yqin70 | 0:4f77ae831ee7 | 51 | PwmOut _greenpin; |
yqin70 | 0:4f77ae831ee7 | 52 | PwmOut _bluepin; |
yqin70 | 0:4f77ae831ee7 | 53 | }; |
yqin70 | 0:4f77ae831ee7 | 54 | |
yqin70 | 0:4f77ae831ee7 | 55 | RGBLed::RGBLed (PinName redpin, PinName greenpin, PinName bluepin) |
yqin70 | 0:4f77ae831ee7 | 56 | : _redpin(redpin), _greenpin(greenpin), _bluepin(bluepin) |
yqin70 | 0:4f77ae831ee7 | 57 | { |
yqin70 | 0:4f77ae831ee7 | 58 | //50Hz PWM clock default a bit too low, go to 2000Hz (less flicker) |
yqin70 | 0:4f77ae831ee7 | 59 | _redpin.period(0.0005); |
yqin70 | 0:4f77ae831ee7 | 60 | } |
yqin70 | 0:4f77ae831ee7 | 61 | |
yqin70 | 0:4f77ae831ee7 | 62 | void RGBLed::write(float red,float green, float blue) |
yqin70 | 0:4f77ae831ee7 | 63 | { |
yqin70 | 0:4f77ae831ee7 | 64 | _redpin = red; |
yqin70 | 0:4f77ae831ee7 | 65 | _greenpin = green; |
yqin70 | 0:4f77ae831ee7 | 66 | _bluepin = blue; |
yqin70 | 0:4f77ae831ee7 | 67 | } |
yqin70 | 0:4f77ae831ee7 | 68 | void RGBLed::write(LEDColor c) |
yqin70 | 0:4f77ae831ee7 | 69 | { |
yqin70 | 0:4f77ae831ee7 | 70 | _redpin = c.red; |
yqin70 | 0:4f77ae831ee7 | 71 | _greenpin = c.green; |
yqin70 | 0:4f77ae831ee7 | 72 | _bluepin = c.blue; |
yqin70 | 0:4f77ae831ee7 | 73 | } |
yqin70 | 0:4f77ae831ee7 | 74 | |
yqin70 | 0:4f77ae831ee7 | 75 | //classes could be moved to include file |
yqin70 | 0:4f77ae831ee7 | 76 | |
yqin70 | 0:4f77ae831ee7 | 77 | //Setup RGB led using PWM pins and class |
yqin70 | 0:4f77ae831ee7 | 78 | RGBLed myRGBled(p23,p22,p21); //RGB PWM pins |
yqin70 | 0:4f77ae831ee7 | 79 | const LEDColor red(1.0,0.0,0.0); |
yqin70 | 0:4f77ae831ee7 | 80 | const LEDColor green(0.0,0.2,0.0); |
yqin70 | 0:4f77ae831ee7 | 81 | //brighter green LED is scaled down to same as red and |
yqin70 | 0:4f77ae831ee7 | 82 | //blue LED outputs on Sparkfun RGBLED |
yqin70 | 0:4f77ae831ee7 | 83 | const LEDColor blue(0.0,0.0,1.0); |
yqin70 | 0:4f77ae831ee7 | 84 | const LEDColor yellow(1.0,0.2,0.0); |
yqin70 | 0:4f77ae831ee7 | 85 | const LEDColor white(1.0,0.2,1.0); |
yqin70 | 0:4f77ae831ee7 | 86 | const LEDColor black(0.0,0.0,0.0); |
yqin70 | 0:4f77ae831ee7 | 87 | char bred=0; |
yqin70 | 0:4f77ae831ee7 | 88 | char bgreen=0; |
yqin70 | 0:4f77ae831ee7 | 89 | char bblue=0; |
yqin70 | 0:4f77ae831ee7 | 90 | |
yqin70 | 0:4f77ae831ee7 | 91 | volatile bool songselect = false; |
yqin70 | 0:4f77ae831ee7 | 92 | volatile bool homescreen = true; |
yqin70 | 0:4f77ae831ee7 | 93 | uLCD_4DGL uLCD(p28,p27,p30); |
yqin70 | 0:4f77ae831ee7 | 94 | SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card |
yqin70 | 0:4f77ae831ee7 | 95 | DigitalOut myled(LED1); |
yqin70 | 0:4f77ae831ee7 | 96 | DigitalIn pb1(p20); |
yqin70 | 0:4f77ae831ee7 | 97 | DigitalIn pb2(p19); |
yqin70 | 1:a1aa9a79070a | 98 | AnalogOut DACout(p18); |
yqin70 | 1:a1aa9a79070a | 99 | wave_player waver(&DACout); |
yqin70 | 0:4f77ae831ee7 | 100 | |
yqin70 | 0:4f77ae831ee7 | 101 | // mutex to make the lcd lib thread safe |
yqin70 | 0:4f77ae831ee7 | 102 | Mutex lcd_mutex; |
yqin70 | 0:4f77ae831ee7 | 103 | int songnum = 1; |
yqin70 | 0:4f77ae831ee7 | 104 | AnalogIn joy_pot(p16); |
yqin70 | 0:4f77ae831ee7 | 105 | // Thread 1 |
yqin70 | 1:a1aa9a79070a | 106 | // print homescreen to LCD |
yqin70 | 0:4f77ae831ee7 | 107 | void thread1(void const *args) |
yqin70 | 0:4f77ae831ee7 | 108 | { |
yqin70 | 0:4f77ae831ee7 | 109 | |
yqin70 | 0:4f77ae831ee7 | 110 | while(true) { // thread loop |
yqin70 | 0:4f77ae831ee7 | 111 | |
yqin70 | 0:4f77ae831ee7 | 112 | if (homescreen){ |
yqin70 | 0:4f77ae831ee7 | 113 | lcd_mutex.lock(); |
yqin70 | 0:4f77ae831ee7 | 114 | uLCD.cls(); |
yqin70 | 2:765774781bb1 | 115 | uLCD.text_height(1.9); |
yqin70 | 2:765774781bb1 | 116 | uLCD.text_width(1.9); |
yqin70 | 0:4f77ae831ee7 | 117 | uLCD.color(WHITE); |
yqin70 | 0:4f77ae831ee7 | 118 | uLCD.locate(0,0); |
yqin70 | 0:4f77ae831ee7 | 119 | uLCD.printf("Pick a song"); |
yqin70 | 2:765774781bb1 | 120 | uLCD.text_height(1.9); |
yqin70 | 2:765774781bb1 | 121 | uLCD.text_width(1.9); |
yqin70 | 0:4f77ae831ee7 | 122 | uLCD.locate(1,2); |
yqin70 | 0:4f77ae831ee7 | 123 | uLCD.printf("Song1"); |
yqin70 | 0:4f77ae831ee7 | 124 | uLCD.locate(1,4); |
yqin70 | 0:4f77ae831ee7 | 125 | uLCD.printf("Song2"); |
yqin70 | 0:4f77ae831ee7 | 126 | uLCD.locate(1,6); |
yqin70 | 0:4f77ae831ee7 | 127 | uLCD.printf("Song3"); |
yqin70 | 2:765774781bb1 | 128 | uLCD.locate(1,8); |
yqin70 | 2:765774781bb1 | 129 | uLCD.printf("Song4"); |
yqin70 | 2:765774781bb1 | 130 | uLCD.rectangle(5, songnum*16-2, 100, songnum*16+8 ,GREEN); |
yqin70 | 0:4f77ae831ee7 | 131 | lcd_mutex.unlock(); |
yqin70 | 1:a1aa9a79070a | 132 | |
yqin70 | 0:4f77ae831ee7 | 133 | } |
yqin70 | 0:4f77ae831ee7 | 134 | Thread::wait(200); |
yqin70 | 0:4f77ae831ee7 | 135 | } |
yqin70 | 0:4f77ae831ee7 | 136 | } |
yqin70 | 0:4f77ae831ee7 | 137 | |
yqin70 | 1:a1aa9a79070a | 138 | //for song selection during homescreen |
yqin70 | 0:4f77ae831ee7 | 139 | void thread2(void const *args) |
yqin70 | 0:4f77ae831ee7 | 140 | { |
yqin70 | 0:4f77ae831ee7 | 141 | |
yqin70 | 0:4f77ae831ee7 | 142 | while(1){ |
yqin70 | 1:a1aa9a79070a | 143 | if (homescreen){ |
yqin70 | 0:4f77ae831ee7 | 144 | if ((joy_pot <= (1.4/3.3)) && songnum>1) { |
yqin70 | 0:4f77ae831ee7 | 145 | songnum--; |
yqin70 | 0:4f77ae831ee7 | 146 | } |
yqin70 | 2:765774781bb1 | 147 | else if ((joy_pot >= (1.8/3.3)) && songnum<4){ |
yqin70 | 0:4f77ae831ee7 | 148 | songnum++; |
yqin70 | 0:4f77ae831ee7 | 149 | } |
yqin70 | 1:a1aa9a79070a | 150 | } |
yqin70 | 0:4f77ae831ee7 | 151 | Thread::wait(250); |
yqin70 | 0:4f77ae831ee7 | 152 | |
yqin70 | 0:4f77ae831ee7 | 153 | } |
yqin70 | 0:4f77ae831ee7 | 154 | } |
yqin70 | 0:4f77ae831ee7 | 155 | |
yqin70 | 0:4f77ae831ee7 | 156 | // Thread 3 |
yqin70 | 1:a1aa9a79070a | 157 | //pb1 is to select song |
yqin70 | 1:a1aa9a79070a | 158 | //pb2 is to return to homescreen |
yqin70 | 0:4f77ae831ee7 | 159 | void thread3(void const *args) |
yqin70 | 0:4f77ae831ee7 | 160 | { |
yqin70 | 0:4f77ae831ee7 | 161 | pb1.mode(PullUp); |
yqin70 | 0:4f77ae831ee7 | 162 | pb2.mode(PullUp); |
yqin70 | 0:4f77ae831ee7 | 163 | while(true) { // thread loop |
yqin70 | 0:4f77ae831ee7 | 164 | if (!pb2) |
yqin70 | 0:4f77ae831ee7 | 165 | { |
yqin70 | 0:4f77ae831ee7 | 166 | homescreen = true; |
yqin70 | 0:4f77ae831ee7 | 167 | songselect = false; |
yqin70 | 0:4f77ae831ee7 | 168 | } |
yqin70 | 0:4f77ae831ee7 | 169 | if (!pb1) |
yqin70 | 0:4f77ae831ee7 | 170 | { |
yqin70 | 0:4f77ae831ee7 | 171 | songselect = true; |
yqin70 | 0:4f77ae831ee7 | 172 | } |
yqin70 | 0:4f77ae831ee7 | 173 | Thread::wait(100); // value of pot1 / 100 |
yqin70 | 0:4f77ae831ee7 | 174 | } |
yqin70 | 0:4f77ae831ee7 | 175 | } |
yqin70 | 0:4f77ae831ee7 | 176 | |
yqin70 | 0:4f77ae831ee7 | 177 | int main() |
yqin70 | 0:4f77ae831ee7 | 178 | { |
yqin70 | 1:a1aa9a79070a | 179 | //doesnt work yet, need to ask hamblen |
yqin70 | 1:a1aa9a79070a | 180 | |
yqin70 | 0:4f77ae831ee7 | 181 | uLCD.media_init(); |
yqin70 | 0:4f77ae831ee7 | 182 | uLCD.set_sector_address(0x001D, 0x4C01); |
yqin70 | 0:4f77ae831ee7 | 183 | uLCD.display_image(0,0); |
yqin70 | 0:4f77ae831ee7 | 184 | // t1.start(thread1); |
yqin70 | 0:4f77ae831ee7 | 185 | // t2.start(thread2); |
yqin70 | 0:4f77ae831ee7 | 186 | // t3.start(thread3); |
yqin70 | 0:4f77ae831ee7 | 187 | Thread t1(thread1); |
yqin70 | 0:4f77ae831ee7 | 188 | Thread t2(thread2); |
yqin70 | 0:4f77ae831ee7 | 189 | Thread t3(thread3); |
yqin70 | 1:a1aa9a79070a | 190 | //startup sound. Commented out for testing without SD card |
yqin70 | 1:a1aa9a79070a | 191 | //FILE *wave_file; |
yqin70 | 1:a1aa9a79070a | 192 | //wave_file=fopen("/sd/cheer.wav","r"); |
yqin70 | 1:a1aa9a79070a | 193 | //waver.play(wave_file); |
yqin70 | 1:a1aa9a79070a | 194 | //fclose(wave_file); |
yqin70 | 0:4f77ae831ee7 | 195 | while(1) |
yqin70 | 0:4f77ae831ee7 | 196 | { |
yqin70 | 0:4f77ae831ee7 | 197 | |
yqin70 | 1:a1aa9a79070a | 198 | if (songselect){ |
yqin70 | 1:a1aa9a79070a | 199 | myled = 0; |
yqin70 | 1:a1aa9a79070a | 200 | homescreen = false; |
yqin70 | 1:a1aa9a79070a | 201 | lcd_mutex.lock(); |
yqin70 | 1:a1aa9a79070a | 202 | uLCD.cls(); |
yqin70 | 1:a1aa9a79070a | 203 | uLCD.printf("You selected song %2d",songnum); |
yqin70 | 1:a1aa9a79070a | 204 | lcd_mutex.unlock(); |
yqin70 | 1:a1aa9a79070a | 205 | //add case statement based on songnum or something |
yqin70 | 1:a1aa9a79070a | 206 | //code for playing song from sd |
yqin70 | 1:a1aa9a79070a | 207 | //FILE *wave_file; |
yqin70 | 1:a1aa9a79070a | 208 | //wave_file=fopen("/sd/sample1.wav","r"); |
yqin70 | 1:a1aa9a79070a | 209 | //waver.play(wave_file); |
yqin70 | 1:a1aa9a79070a | 210 | //fclose(wave_file); |
yqin70 | 1:a1aa9a79070a | 211 | //end |
yqin70 | 1:a1aa9a79070a | 212 | } |
yqin70 | 0:4f77ae831ee7 | 213 | Thread::wait(100); |
yqin70 | 0:4f77ae831ee7 | 214 | } |
yqin70 | 0:4f77ae831ee7 | 215 | } |
yqin70 | 0:4f77ae831ee7 | 216 | |
yqin70 | 0:4f77ae831ee7 | 217 |