![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
t
Dependencies: mbed wave_player mbed-rtos C12832_lcd 4DGL-uLCD-SE LCD_fonts SDFileSystem
main.cpp@6:cd24147b5e50, 2019-12-05 (annotated)
- Committer:
- jcrane32
- Date:
- Thu Dec 05 00:17:49 2019 +0000
- Revision:
- 6:cd24147b5e50
- Parent:
- 5:b2cf15651d4e
Refactored most of the code. Created .h files for bubbles and songs to be added later.
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" |
jcrane32 | 6:cd24147b5e50 | 5 | |
yqin70 | 0:4f77ae831ee7 | 6 | //setup some color objects in flash using const's |
yqin70 | 0:4f77ae831ee7 | 7 | |
yqin70 | 0:4f77ae831ee7 | 8 | #include "rtos.h" |
yqin70 | 0:4f77ae831ee7 | 9 | #include "Small_6.h" |
yqin70 | 0:4f77ae831ee7 | 10 | #include "Small_7.h" |
yqin70 | 0:4f77ae831ee7 | 11 | #include "Arial_9.h" |
yqin70 | 0:4f77ae831ee7 | 12 | #include "stdio.h" |
yqin70 | 0:4f77ae831ee7 | 13 | #include "C12832_lcd.h" |
yqin70 | 0:4f77ae831ee7 | 14 | |
jcrane32 | 6:cd24147b5e50 | 15 | #include "bubbles.h" |
jcrane32 | 6:cd24147b5e50 | 16 | #include "songs.h" |
jcrane32 | 6:cd24147b5e50 | 17 | |
yqin70 | 0:4f77ae831ee7 | 18 | //class for 3 PWM color values for RGBLED |
yqin70 | 0:4f77ae831ee7 | 19 | class LEDColor |
yqin70 | 0:4f77ae831ee7 | 20 | { |
yqin70 | 0:4f77ae831ee7 | 21 | public: |
yqin70 | 0:4f77ae831ee7 | 22 | LEDColor(float r, float g, float b); |
yqin70 | 0:4f77ae831ee7 | 23 | float red; |
yqin70 | 0:4f77ae831ee7 | 24 | float green; |
yqin70 | 0:4f77ae831ee7 | 25 | float blue; |
yqin70 | 0:4f77ae831ee7 | 26 | }; |
yqin70 | 0:4f77ae831ee7 | 27 | LEDColor:: LEDColor(float r, float g, float b) |
yqin70 | 0:4f77ae831ee7 | 28 | : red(r), green(g), blue(b) |
yqin70 | 0:4f77ae831ee7 | 29 | { |
yqin70 | 0:4f77ae831ee7 | 30 | } |
yqin70 | 0:4f77ae831ee7 | 31 | //Operator overload to adjust brightness with no color change |
yqin70 | 0:4f77ae831ee7 | 32 | LEDColor operator * (const LEDColor& x, const float& b) |
yqin70 | 0:4f77ae831ee7 | 33 | { |
yqin70 | 0:4f77ae831ee7 | 34 | return LEDColor(x.red*b,x.green*b,x.blue*b); |
yqin70 | 0:4f77ae831ee7 | 35 | } |
yqin70 | 0:4f77ae831ee7 | 36 | //Operator overload to add colors |
yqin70 | 0:4f77ae831ee7 | 37 | LEDColor operator + (const LEDColor& x, const LEDColor& y) |
yqin70 | 0:4f77ae831ee7 | 38 | { |
yqin70 | 0:4f77ae831ee7 | 39 | return LEDColor(x.red+y.red,x.green+y.green,x.blue+y.blue); |
yqin70 | 0:4f77ae831ee7 | 40 | } |
yqin70 | 0:4f77ae831ee7 | 41 | |
yqin70 | 0:4f77ae831ee7 | 42 | //Class to control an RGB LED using three PWM pins |
yqin70 | 0:4f77ae831ee7 | 43 | class RGBLed |
yqin70 | 0:4f77ae831ee7 | 44 | { |
yqin70 | 0:4f77ae831ee7 | 45 | public: |
yqin70 | 0:4f77ae831ee7 | 46 | RGBLed(PinName redpin, PinName greenpin, PinName bluepin); |
yqin70 | 0:4f77ae831ee7 | 47 | void write(float red,float green, float blue); |
yqin70 | 0:4f77ae831ee7 | 48 | void write(LEDColor c); |
yqin70 | 0:4f77ae831ee7 | 49 | RGBLed operator = (LEDColor c) { |
yqin70 | 0:4f77ae831ee7 | 50 | write(c); |
yqin70 | 0:4f77ae831ee7 | 51 | return *this; |
yqin70 | 0:4f77ae831ee7 | 52 | }; |
yqin70 | 0:4f77ae831ee7 | 53 | private: |
yqin70 | 0:4f77ae831ee7 | 54 | PwmOut _redpin; |
yqin70 | 0:4f77ae831ee7 | 55 | PwmOut _greenpin; |
yqin70 | 0:4f77ae831ee7 | 56 | PwmOut _bluepin; |
yqin70 | 0:4f77ae831ee7 | 57 | }; |
yqin70 | 0:4f77ae831ee7 | 58 | |
yqin70 | 0:4f77ae831ee7 | 59 | RGBLed::RGBLed (PinName redpin, PinName greenpin, PinName bluepin) |
yqin70 | 0:4f77ae831ee7 | 60 | : _redpin(redpin), _greenpin(greenpin), _bluepin(bluepin) |
yqin70 | 0:4f77ae831ee7 | 61 | { |
yqin70 | 0:4f77ae831ee7 | 62 | //50Hz PWM clock default a bit too low, go to 2000Hz (less flicker) |
yqin70 | 0:4f77ae831ee7 | 63 | _redpin.period(0.0005); |
yqin70 | 0:4f77ae831ee7 | 64 | } |
yqin70 | 0:4f77ae831ee7 | 65 | |
yqin70 | 0:4f77ae831ee7 | 66 | void RGBLed::write(float red,float green, float blue) |
yqin70 | 0:4f77ae831ee7 | 67 | { |
yqin70 | 0:4f77ae831ee7 | 68 | _redpin = red; |
yqin70 | 0:4f77ae831ee7 | 69 | _greenpin = green; |
yqin70 | 0:4f77ae831ee7 | 70 | _bluepin = blue; |
yqin70 | 0:4f77ae831ee7 | 71 | } |
yqin70 | 0:4f77ae831ee7 | 72 | void RGBLed::write(LEDColor c) |
yqin70 | 0:4f77ae831ee7 | 73 | { |
yqin70 | 0:4f77ae831ee7 | 74 | _redpin = c.red; |
yqin70 | 0:4f77ae831ee7 | 75 | _greenpin = c.green; |
yqin70 | 0:4f77ae831ee7 | 76 | _bluepin = c.blue; |
yqin70 | 0:4f77ae831ee7 | 77 | } |
yqin70 | 0:4f77ae831ee7 | 78 | |
yqin70 | 0:4f77ae831ee7 | 79 | //classes could be moved to include file |
yqin70 | 0:4f77ae831ee7 | 80 | |
yqin70 | 0:4f77ae831ee7 | 81 | //Setup RGB led using PWM pins and class |
ShelbyC22 | 4:59b73f321c0e | 82 | RGBLed myRGBled(p24,p23,p22); //RGB PWM pins |
yqin70 | 0:4f77ae831ee7 | 83 | const LEDColor red(1.0,0.0,0.0); |
yqin70 | 0:4f77ae831ee7 | 84 | const LEDColor green(0.0,0.2,0.0); |
yqin70 | 0:4f77ae831ee7 | 85 | //brighter green LED is scaled down to same as red and |
yqin70 | 0:4f77ae831ee7 | 86 | //blue LED outputs on Sparkfun RGBLED |
yqin70 | 0:4f77ae831ee7 | 87 | const LEDColor blue(0.0,0.0,1.0); |
yqin70 | 0:4f77ae831ee7 | 88 | const LEDColor yellow(1.0,0.2,0.0); |
yqin70 | 0:4f77ae831ee7 | 89 | const LEDColor white(1.0,0.2,1.0); |
yqin70 | 0:4f77ae831ee7 | 90 | const LEDColor black(0.0,0.0,0.0); |
yqin70 | 0:4f77ae831ee7 | 91 | char bred=0; |
yqin70 | 0:4f77ae831ee7 | 92 | char bgreen=0; |
yqin70 | 0:4f77ae831ee7 | 93 | char bblue=0; |
yqin70 | 0:4f77ae831ee7 | 94 | |
yqin70 | 0:4f77ae831ee7 | 95 | volatile bool songselect = false; |
yqin70 | 0:4f77ae831ee7 | 96 | volatile bool homescreen = true; |
ShelbyC22 | 5:b2cf15651d4e | 97 | volatile bool songchange = false; |
yqin70 | 0:4f77ae831ee7 | 98 | uLCD_4DGL uLCD(p28,p27,p30); |
yqin70 | 0:4f77ae831ee7 | 99 | SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card |
yqin70 | 0:4f77ae831ee7 | 100 | DigitalOut myled(LED1); |
yqin70 | 0:4f77ae831ee7 | 101 | DigitalIn pb1(p20); |
yqin70 | 0:4f77ae831ee7 | 102 | DigitalIn pb2(p19); |
yqin70 | 1:a1aa9a79070a | 103 | AnalogOut DACout(p18); |
yqin70 | 1:a1aa9a79070a | 104 | wave_player waver(&DACout); |
jcrane32 | 6:cd24147b5e50 | 105 | Thread thread1, thread2, thread3; |
yqin70 | 0:4f77ae831ee7 | 106 | |
yqin70 | 0:4f77ae831ee7 | 107 | // mutex to make the lcd lib thread safe |
yqin70 | 0:4f77ae831ee7 | 108 | Mutex lcd_mutex; |
yqin70 | 0:4f77ae831ee7 | 109 | int songnum = 1; |
yqin70 | 0:4f77ae831ee7 | 110 | AnalogIn joy_pot(p16); |
jcrane32 | 6:cd24147b5e50 | 111 | |
yqin70 | 0:4f77ae831ee7 | 112 | // Thread 1 |
yqin70 | 1:a1aa9a79070a | 113 | // print homescreen to LCD |
ShelbyC22 | 5:b2cf15651d4e | 114 | //new homescreen that doesn't have the flashing |
jcrane32 | 6:cd24147b5e50 | 115 | void homescreen_thread() |
yqin70 | 0:4f77ae831ee7 | 116 | { |
yqin70 | 0:4f77ae831ee7 | 117 | while(true) { // thread loop |
ShelbyC22 | 5:b2cf15651d4e | 118 | lcd_mutex.lock(); |
yqin70 | 0:4f77ae831ee7 | 119 | if (homescreen){ |
yqin70 | 0:4f77ae831ee7 | 120 | uLCD.cls(); |
ShelbyC22 | 5:b2cf15651d4e | 121 | uLCD.text_height(1.3); |
yqin70 | 2:765774781bb1 | 122 | uLCD.text_width(1.9); |
yqin70 | 0:4f77ae831ee7 | 123 | uLCD.color(WHITE); |
ShelbyC22 | 5:b2cf15651d4e | 124 | uLCD.locate(5,0); |
ShelbyC22 | 5:b2cf15651d4e | 125 | uLCD.printf("Tap Tap"); |
ShelbyC22 | 5:b2cf15651d4e | 126 | uLCD.locate(4,1); |
ShelbyC22 | 5:b2cf15651d4e | 127 | uLCD.printf("Revolution"); |
ShelbyC22 | 5:b2cf15651d4e | 128 | uLCD.locate(0,3); |
yqin70 | 0:4f77ae831ee7 | 129 | uLCD.printf("Pick a song"); |
ShelbyC22 | 5:b2cf15651d4e | 130 | uLCD.text_height(1.3); |
yqin70 | 2:765774781bb1 | 131 | uLCD.text_width(1.9); |
ShelbyC22 | 5:b2cf15651d4e | 132 | uLCD.locate(3,5); |
ShelbyC22 | 4:59b73f321c0e | 133 | uLCD.printf("Fireflies"); |
ShelbyC22 | 5:b2cf15651d4e | 134 | uLCD.locate(3,7); |
ShelbyC22 | 4:59b73f321c0e | 135 | uLCD.printf("The Middle"); |
ShelbyC22 | 5:b2cf15651d4e | 136 | uLCD.locate(3,9); |
ShelbyC22 | 4:59b73f321c0e | 137 | uLCD.printf("Stacy's Mom"); |
ShelbyC22 | 5:b2cf15651d4e | 138 | uLCD.locate(3,11); |
ShelbyC22 | 5:b2cf15651d4e | 139 | uLCD.printf("I Write Sins \n \t Not Tragedies"); |
ShelbyC22 | 5:b2cf15651d4e | 140 | uLCD.filled_circle(6, 43, 4, GREEN); //new selection with circle instead of rectangle |
ShelbyC22 | 5:b2cf15651d4e | 141 | homescreen = false;} |
ShelbyC22 | 5:b2cf15651d4e | 142 | if (songchange){ |
ShelbyC22 | 5:b2cf15651d4e | 143 | for (int i = 0; i < 4; i++){ |
ShelbyC22 | 5:b2cf15651d4e | 144 | uLCD.filled_circle(6, i*15+43, 4,BLACK); |
ShelbyC22 | 5:b2cf15651d4e | 145 | } |
ShelbyC22 | 5:b2cf15651d4e | 146 | uLCD.filled_circle(6, (songnum-1)*15+43, 4,GREEN); |
ShelbyC22 | 5:b2cf15651d4e | 147 | songchange = false; |
ShelbyC22 | 5:b2cf15651d4e | 148 | } |
yqin70 | 0:4f77ae831ee7 | 149 | lcd_mutex.unlock(); |
ShelbyC22 | 5:b2cf15651d4e | 150 | Thread::wait(200); |
yqin70 | 0:4f77ae831ee7 | 151 | } |
yqin70 | 0:4f77ae831ee7 | 152 | } |
yqin70 | 0:4f77ae831ee7 | 153 | |
jcrane32 | 6:cd24147b5e50 | 154 | // Thread 2 |
yqin70 | 1:a1aa9a79070a | 155 | //for song selection during homescreen |
ShelbyC22 | 5:b2cf15651d4e | 156 | |
jcrane32 | 6:cd24147b5e50 | 157 | void joystick_thread() |
yqin70 | 0:4f77ae831ee7 | 158 | { |
yqin70 | 0:4f77ae831ee7 | 159 | while(1){ |
yqin70 | 0:4f77ae831ee7 | 160 | if ((joy_pot <= (1.4/3.3)) && songnum>1) { |
yqin70 | 0:4f77ae831ee7 | 161 | songnum--; |
ShelbyC22 | 5:b2cf15651d4e | 162 | songchange = true; |
ShelbyC22 | 5:b2cf15651d4e | 163 | } |
yqin70 | 2:765774781bb1 | 164 | else if ((joy_pot >= (1.8/3.3)) && songnum<4){ |
yqin70 | 0:4f77ae831ee7 | 165 | songnum++; |
ShelbyC22 | 5:b2cf15651d4e | 166 | songchange = true; |
yqin70 | 0:4f77ae831ee7 | 167 | } |
yqin70 | 0:4f77ae831ee7 | 168 | Thread::wait(250); |
yqin70 | 0:4f77ae831ee7 | 169 | } |
yqin70 | 0:4f77ae831ee7 | 170 | } |
yqin70 | 0:4f77ae831ee7 | 171 | |
yqin70 | 0:4f77ae831ee7 | 172 | // Thread 3 |
yqin70 | 1:a1aa9a79070a | 173 | //pb1 is to select song |
yqin70 | 1:a1aa9a79070a | 174 | //pb2 is to return to homescreen |
jcrane32 | 6:cd24147b5e50 | 175 | void pbcontrol_thread() |
yqin70 | 0:4f77ae831ee7 | 176 | { |
yqin70 | 0:4f77ae831ee7 | 177 | pb1.mode(PullUp); |
yqin70 | 0:4f77ae831ee7 | 178 | pb2.mode(PullUp); |
yqin70 | 0:4f77ae831ee7 | 179 | while(true) { // thread loop |
yqin70 | 0:4f77ae831ee7 | 180 | if (!pb2) |
yqin70 | 0:4f77ae831ee7 | 181 | { |
yqin70 | 0:4f77ae831ee7 | 182 | homescreen = true; |
yqin70 | 0:4f77ae831ee7 | 183 | songselect = false; |
yqin70 | 0:4f77ae831ee7 | 184 | } |
yqin70 | 0:4f77ae831ee7 | 185 | if (!pb1) |
yqin70 | 0:4f77ae831ee7 | 186 | { |
yqin70 | 0:4f77ae831ee7 | 187 | songselect = true; |
yqin70 | 0:4f77ae831ee7 | 188 | } |
yqin70 | 0:4f77ae831ee7 | 189 | Thread::wait(100); // value of pot1 / 100 |
yqin70 | 0:4f77ae831ee7 | 190 | } |
yqin70 | 0:4f77ae831ee7 | 191 | } |
yqin70 | 0:4f77ae831ee7 | 192 | |
yqin70 | 0:4f77ae831ee7 | 193 | int main() |
yqin70 | 0:4f77ae831ee7 | 194 | { |
yqin70 | 3:431877852f14 | 195 | //timestamp in terms of seconds |
yqin70 | 3:431877852f14 | 196 | |
yqin70 | 3:431877852f14 | 197 | //doesnt work yet, nee,d to ask hamblen |
yqin70 | 0:4f77ae831ee7 | 198 | uLCD.media_init(); |
yqin70 | 0:4f77ae831ee7 | 199 | uLCD.set_sector_address(0x001D, 0x4C01); |
yqin70 | 0:4f77ae831ee7 | 200 | uLCD.display_image(0,0); |
jcrane32 | 6:cd24147b5e50 | 201 | thread1.start(homescreen_thread); |
jcrane32 | 6:cd24147b5e50 | 202 | thread2.start(joystick_thread); |
jcrane32 | 6:cd24147b5e50 | 203 | thread3.start(pbcontrol_thread); |
ShelbyC22 | 5:b2cf15651d4e | 204 | //startup sound |
ShelbyC22 | 5:b2cf15651d4e | 205 | myRGBled = blue; //tested to make sure led works, we can use whatever color(s) here |
ShelbyC22 | 5:b2cf15651d4e | 206 | FILE *wave_file; |
ShelbyC22 | 5:b2cf15651d4e | 207 | wave_file=fopen("/sd/cheer.wav","r"); |
ShelbyC22 | 5:b2cf15651d4e | 208 | waver.play(wave_file); |
ShelbyC22 | 5:b2cf15651d4e | 209 | fclose(wave_file); |
yqin70 | 0:4f77ae831ee7 | 210 | while(1) |
yqin70 | 0:4f77ae831ee7 | 211 | { |
yqin70 | 0:4f77ae831ee7 | 212 | |
yqin70 | 1:a1aa9a79070a | 213 | if (songselect){ |
yqin70 | 1:a1aa9a79070a | 214 | myled = 0; |
yqin70 | 1:a1aa9a79070a | 215 | homescreen = false; |
yqin70 | 1:a1aa9a79070a | 216 | lcd_mutex.lock(); |
yqin70 | 1:a1aa9a79070a | 217 | uLCD.cls(); |
yqin70 | 1:a1aa9a79070a | 218 | uLCD.printf("You selected song %2d",songnum); |
yqin70 | 1:a1aa9a79070a | 219 | lcd_mutex.unlock(); |
yqin70 | 1:a1aa9a79070a | 220 | //add case statement based on songnum or something |
yqin70 | 1:a1aa9a79070a | 221 | //code for playing song from sd |
ShelbyC22 | 4:59b73f321c0e | 222 | switch (songnum) |
ShelbyC22 | 4:59b73f321c0e | 223 | { |
ShelbyC22 | 4:59b73f321c0e | 224 | case 1: |
ShelbyC22 | 4:59b73f321c0e | 225 | { |
ShelbyC22 | 4:59b73f321c0e | 226 | FILE *wave_file; |
ShelbyC22 | 4:59b73f321c0e | 227 | wave_file =fopen("/sd/4180 final project/Fireflies.wav", "r"); |
ShelbyC22 | 4:59b73f321c0e | 228 | waver.play(wave_file); |
ShelbyC22 | 4:59b73f321c0e | 229 | fclose(wave_file); |
ShelbyC22 | 4:59b73f321c0e | 230 | break; |
ShelbyC22 | 4:59b73f321c0e | 231 | } |
ShelbyC22 | 4:59b73f321c0e | 232 | case 2: |
ShelbyC22 | 4:59b73f321c0e | 233 | { |
ShelbyC22 | 4:59b73f321c0e | 234 | FILE *wave_file; |
ShelbyC22 | 4:59b73f321c0e | 235 | wave_file =fopen("/sd/4180 final project/The_Middle.wav", "r"); |
ShelbyC22 | 4:59b73f321c0e | 236 | waver.play(wave_file); |
ShelbyC22 | 4:59b73f321c0e | 237 | fclose(wave_file); |
ShelbyC22 | 4:59b73f321c0e | 238 | break; |
ShelbyC22 | 4:59b73f321c0e | 239 | } |
ShelbyC22 | 4:59b73f321c0e | 240 | case 3: |
ShelbyC22 | 4:59b73f321c0e | 241 | { |
ShelbyC22 | 4:59b73f321c0e | 242 | FILE *wave_file; |
ShelbyC22 | 4:59b73f321c0e | 243 | wave_file =fopen("/sd/4180 final project/Stacy's_Mom.wav", "r"); |
ShelbyC22 | 4:59b73f321c0e | 244 | waver.play(wave_file); |
ShelbyC22 | 4:59b73f321c0e | 245 | fclose(wave_file); |
ShelbyC22 | 4:59b73f321c0e | 246 | break; |
ShelbyC22 | 4:59b73f321c0e | 247 | } |
ShelbyC22 | 4:59b73f321c0e | 248 | case 4: |
ShelbyC22 | 4:59b73f321c0e | 249 | { |
ShelbyC22 | 4:59b73f321c0e | 250 | FILE *wave_file; |
ShelbyC22 | 4:59b73f321c0e | 251 | wave_file =fopen("/sd/4180 final project/Sins_!_Tragedies.wav", "r"); |
ShelbyC22 | 4:59b73f321c0e | 252 | waver.play(wave_file); |
ShelbyC22 | 4:59b73f321c0e | 253 | fclose(wave_file); |
ShelbyC22 | 4:59b73f321c0e | 254 | break; |
ShelbyC22 | 4:59b73f321c0e | 255 | } |
ShelbyC22 | 4:59b73f321c0e | 256 | default: |
ShelbyC22 | 4:59b73f321c0e | 257 | break; |
ShelbyC22 | 4:59b73f321c0e | 258 | } |
ShelbyC22 | 4:59b73f321c0e | 259 | |
yqin70 | 1:a1aa9a79070a | 260 | } |
yqin70 | 0:4f77ae831ee7 | 261 | Thread::wait(100); |
yqin70 | 0:4f77ae831ee7 | 262 | } |
yqin70 | 0:4f77ae831ee7 | 263 | } |
yqin70 | 0:4f77ae831ee7 | 264 | |
yqin70 | 0:4f77ae831ee7 | 265 |