![](/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@11:362e25f659a5, 2019-12-05 (annotated)
- Committer:
- yqin70
- Date:
- Thu Dec 05 04:38:33 2019 +0000
- Revision:
- 11:362e25f659a5
- Parent:
- 7:15fdc55dbf66
- Child:
- 14:6f5432bb44c0
- Child:
- 19:d65f9fb1023b
Songs in separate header files
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yqin70 | 0:4f77ae831ee7 | 1 | #include "mbed.h" |
jcrane32 | 7:15fdc55dbf66 | 2 | #include "rtos.h" |
yqin70 | 0:4f77ae831ee7 | 3 | #include "SDFileSystem.h" |
yqin70 | 0:4f77ae831ee7 | 4 | #include "wave_player.h" |
yqin70 | 0:4f77ae831ee7 | 5 | #include "uLCD_4DGL.h" |
jcrane32 | 6:cd24147b5e50 | 6 | |
yqin70 | 0:4f77ae831ee7 | 7 | //setup some color objects in flash using const's |
yqin70 | 0:4f77ae831ee7 | 8 | |
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" |
yqin70 | 11:362e25f659a5 | 16 | #include "stacys_mom.h" |
yqin70 | 11:362e25f659a5 | 17 | #include "the_middle.h" |
yqin70 | 11:362e25f659a5 | 18 | #include "fireflies.h" |
yqin70 | 11:362e25f659a5 | 19 | #include "sins.h" |
jcrane32 | 7:15fdc55dbf66 | 20 | #include "LED.hpp" |
jcrane32 | 6:cd24147b5e50 | 21 | |
yqin70 | 0:4f77ae831ee7 | 22 | |
yqin70 | 0:4f77ae831ee7 | 23 | //Setup RGB led using PWM pins and class |
ShelbyC22 | 4:59b73f321c0e | 24 | RGBLed myRGBled(p24,p23,p22); //RGB PWM pins |
jcrane32 | 7:15fdc55dbf66 | 25 | |
yqin70 | 0:4f77ae831ee7 | 26 | char bred=0; |
yqin70 | 0:4f77ae831ee7 | 27 | char bgreen=0; |
yqin70 | 0:4f77ae831ee7 | 28 | char bblue=0; |
yqin70 | 0:4f77ae831ee7 | 29 | |
yqin70 | 0:4f77ae831ee7 | 30 | volatile bool songselect = false; |
yqin70 | 0:4f77ae831ee7 | 31 | volatile bool homescreen = true; |
ShelbyC22 | 5:b2cf15651d4e | 32 | volatile bool songchange = false; |
jcrane32 | 7:15fdc55dbf66 | 33 | volatile bool playing = false; |
jcrane32 | 7:15fdc55dbf66 | 34 | uLCD_4DGL uLCD(p28,p27,p30); |
yqin70 | 0:4f77ae831ee7 | 35 | SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card |
yqin70 | 0:4f77ae831ee7 | 36 | DigitalOut myled(LED1); |
yqin70 | 0:4f77ae831ee7 | 37 | DigitalIn pb1(p20); |
yqin70 | 0:4f77ae831ee7 | 38 | DigitalIn pb2(p19); |
yqin70 | 1:a1aa9a79070a | 39 | AnalogOut DACout(p18); |
yqin70 | 1:a1aa9a79070a | 40 | wave_player waver(&DACout); |
jcrane32 | 6:cd24147b5e50 | 41 | Thread thread1, thread2, thread3; |
jcrane32 | 7:15fdc55dbf66 | 42 | Ticker nextsample; |
jcrane32 | 7:15fdc55dbf66 | 43 | int songNdx = 0; |
yqin70 | 0:4f77ae831ee7 | 44 | |
yqin70 | 0:4f77ae831ee7 | 45 | // mutex to make the lcd lib thread safe |
yqin70 | 0:4f77ae831ee7 | 46 | Mutex lcd_mutex; |
yqin70 | 0:4f77ae831ee7 | 47 | int songnum = 1; |
yqin70 | 0:4f77ae831ee7 | 48 | AnalogIn joy_pot(p16); |
jcrane32 | 6:cd24147b5e50 | 49 | |
yqin70 | 0:4f77ae831ee7 | 50 | // Thread 1 |
yqin70 | 1:a1aa9a79070a | 51 | // print homescreen to LCD |
ShelbyC22 | 5:b2cf15651d4e | 52 | //new homescreen that doesn't have the flashing |
jcrane32 | 6:cd24147b5e50 | 53 | void homescreen_thread() |
yqin70 | 0:4f77ae831ee7 | 54 | { |
yqin70 | 0:4f77ae831ee7 | 55 | while(true) { // thread loop |
ShelbyC22 | 5:b2cf15651d4e | 56 | lcd_mutex.lock(); |
yqin70 | 0:4f77ae831ee7 | 57 | if (homescreen){ |
yqin70 | 0:4f77ae831ee7 | 58 | uLCD.cls(); |
ShelbyC22 | 5:b2cf15651d4e | 59 | uLCD.text_height(1.3); |
yqin70 | 2:765774781bb1 | 60 | uLCD.text_width(1.9); |
yqin70 | 0:4f77ae831ee7 | 61 | uLCD.color(WHITE); |
ShelbyC22 | 5:b2cf15651d4e | 62 | uLCD.locate(5,0); |
ShelbyC22 | 5:b2cf15651d4e | 63 | uLCD.printf("Tap Tap"); |
ShelbyC22 | 5:b2cf15651d4e | 64 | uLCD.locate(4,1); |
ShelbyC22 | 5:b2cf15651d4e | 65 | uLCD.printf("Revolution"); |
ShelbyC22 | 5:b2cf15651d4e | 66 | uLCD.locate(0,3); |
jcrane32 | 7:15fdc55dbf66 | 67 | uLCD.printf("Pick a song!"); |
ShelbyC22 | 5:b2cf15651d4e | 68 | uLCD.text_height(1.3); |
yqin70 | 2:765774781bb1 | 69 | uLCD.text_width(1.9); |
ShelbyC22 | 5:b2cf15651d4e | 70 | uLCD.locate(3,5); |
ShelbyC22 | 4:59b73f321c0e | 71 | uLCD.printf("Fireflies"); |
ShelbyC22 | 5:b2cf15651d4e | 72 | uLCD.locate(3,7); |
ShelbyC22 | 4:59b73f321c0e | 73 | uLCD.printf("The Middle"); |
ShelbyC22 | 5:b2cf15651d4e | 74 | uLCD.locate(3,9); |
ShelbyC22 | 4:59b73f321c0e | 75 | uLCD.printf("Stacy's Mom"); |
ShelbyC22 | 5:b2cf15651d4e | 76 | uLCD.locate(3,11); |
jcrane32 | 7:15fdc55dbf66 | 77 | uLCD.printf("I Write Sins \n \t Not Tragedies"); |
ShelbyC22 | 5:b2cf15651d4e | 78 | uLCD.filled_circle(6, 43, 4, GREEN); //new selection with circle instead of rectangle |
ShelbyC22 | 5:b2cf15651d4e | 79 | homescreen = false;} |
ShelbyC22 | 5:b2cf15651d4e | 80 | if (songchange){ |
ShelbyC22 | 5:b2cf15651d4e | 81 | for (int i = 0; i < 4; i++){ |
ShelbyC22 | 5:b2cf15651d4e | 82 | uLCD.filled_circle(6, i*15+43, 4,BLACK); |
ShelbyC22 | 5:b2cf15651d4e | 83 | } |
ShelbyC22 | 5:b2cf15651d4e | 84 | uLCD.filled_circle(6, (songnum-1)*15+43, 4,GREEN); |
ShelbyC22 | 5:b2cf15651d4e | 85 | songchange = false; |
ShelbyC22 | 5:b2cf15651d4e | 86 | } |
yqin70 | 0:4f77ae831ee7 | 87 | lcd_mutex.unlock(); |
ShelbyC22 | 5:b2cf15651d4e | 88 | Thread::wait(200); |
yqin70 | 0:4f77ae831ee7 | 89 | } |
yqin70 | 0:4f77ae831ee7 | 90 | } |
yqin70 | 0:4f77ae831ee7 | 91 | |
jcrane32 | 6:cd24147b5e50 | 92 | // Thread 2 |
jcrane32 | 7:15fdc55dbf66 | 93 | // joystick control for song selection during homescreen |
jcrane32 | 6:cd24147b5e50 | 94 | void joystick_thread() |
yqin70 | 0:4f77ae831ee7 | 95 | { |
yqin70 | 0:4f77ae831ee7 | 96 | while(1){ |
yqin70 | 0:4f77ae831ee7 | 97 | if ((joy_pot <= (1.4/3.3)) && songnum>1) { |
yqin70 | 0:4f77ae831ee7 | 98 | songnum--; |
ShelbyC22 | 5:b2cf15651d4e | 99 | songchange = true; |
ShelbyC22 | 5:b2cf15651d4e | 100 | } |
yqin70 | 2:765774781bb1 | 101 | else if ((joy_pot >= (1.8/3.3)) && songnum<4){ |
yqin70 | 0:4f77ae831ee7 | 102 | songnum++; |
ShelbyC22 | 5:b2cf15651d4e | 103 | songchange = true; |
yqin70 | 0:4f77ae831ee7 | 104 | } |
yqin70 | 0:4f77ae831ee7 | 105 | Thread::wait(250); |
yqin70 | 0:4f77ae831ee7 | 106 | } |
yqin70 | 0:4f77ae831ee7 | 107 | } |
yqin70 | 0:4f77ae831ee7 | 108 | |
yqin70 | 0:4f77ae831ee7 | 109 | // Thread 3 |
yqin70 | 1:a1aa9a79070a | 110 | //pb1 is to select song |
yqin70 | 1:a1aa9a79070a | 111 | //pb2 is to return to homescreen |
jcrane32 | 6:cd24147b5e50 | 112 | void pbcontrol_thread() |
yqin70 | 0:4f77ae831ee7 | 113 | { |
yqin70 | 0:4f77ae831ee7 | 114 | pb1.mode(PullUp); |
yqin70 | 0:4f77ae831ee7 | 115 | pb2.mode(PullUp); |
yqin70 | 0:4f77ae831ee7 | 116 | while(true) { // thread loop |
yqin70 | 0:4f77ae831ee7 | 117 | if (!pb2) |
yqin70 | 0:4f77ae831ee7 | 118 | { |
yqin70 | 0:4f77ae831ee7 | 119 | homescreen = true; |
yqin70 | 0:4f77ae831ee7 | 120 | songselect = false; |
yqin70 | 0:4f77ae831ee7 | 121 | } |
yqin70 | 0:4f77ae831ee7 | 122 | if (!pb1) |
yqin70 | 0:4f77ae831ee7 | 123 | { |
yqin70 | 0:4f77ae831ee7 | 124 | songselect = true; |
yqin70 | 0:4f77ae831ee7 | 125 | } |
yqin70 | 0:4f77ae831ee7 | 126 | Thread::wait(100); // value of pot1 / 100 |
yqin70 | 0:4f77ae831ee7 | 127 | } |
yqin70 | 0:4f77ae831ee7 | 128 | } |
yqin70 | 0:4f77ae831ee7 | 129 | |
jcrane32 | 7:15fdc55dbf66 | 130 | // Thread 4 |
jcrane32 | 7:15fdc55dbf66 | 131 | // this thread plays music using the ticker class |
jcrane32 | 7:15fdc55dbf66 | 132 | void playsound() { |
jcrane32 | 7:15fdc55dbf66 | 133 | if (playing) { |
jcrane32 | 7:15fdc55dbf66 | 134 | //DACout = song[songNdx++]; |
jcrane32 | 7:15fdc55dbf66 | 135 | //if (i>**songlength**) i |
jcrane32 | 7:15fdc55dbf66 | 136 | } else { |
jcrane32 | 7:15fdc55dbf66 | 137 | DACout = 0; |
jcrane32 | 7:15fdc55dbf66 | 138 | songNdx = 0; |
jcrane32 | 7:15fdc55dbf66 | 139 | } |
jcrane32 | 7:15fdc55dbf66 | 140 | } |
jcrane32 | 7:15fdc55dbf66 | 141 | |
jcrane32 | 7:15fdc55dbf66 | 142 | int main() { |
yqin70 | 3:431877852f14 | 143 | |
jcrane32 | 7:15fdc55dbf66 | 144 | uLCD.baudrate(3000000); |
jcrane32 | 6:cd24147b5e50 | 145 | thread1.start(homescreen_thread); |
jcrane32 | 7:15fdc55dbf66 | 146 | thread2.start(joystick_thread); |
jcrane32 | 6:cd24147b5e50 | 147 | thread3.start(pbcontrol_thread); |
jcrane32 | 7:15fdc55dbf66 | 148 | // nextsample.attach(&playsound, 1.0/8000.0); |
ShelbyC22 | 5:b2cf15651d4e | 149 | //startup sound |
ShelbyC22 | 5:b2cf15651d4e | 150 | myRGBled = blue; //tested to make sure led works, we can use whatever color(s) here |
ShelbyC22 | 5:b2cf15651d4e | 151 | FILE *wave_file; |
ShelbyC22 | 5:b2cf15651d4e | 152 | wave_file=fopen("/sd/cheer.wav","r"); |
ShelbyC22 | 5:b2cf15651d4e | 153 | waver.play(wave_file); |
ShelbyC22 | 5:b2cf15651d4e | 154 | fclose(wave_file); |
yqin70 | 0:4f77ae831ee7 | 155 | while(1) |
yqin70 | 0:4f77ae831ee7 | 156 | { |
yqin70 | 0:4f77ae831ee7 | 157 | |
yqin70 | 1:a1aa9a79070a | 158 | if (songselect){ |
yqin70 | 1:a1aa9a79070a | 159 | myled = 0; |
yqin70 | 1:a1aa9a79070a | 160 | homescreen = false; |
yqin70 | 1:a1aa9a79070a | 161 | lcd_mutex.lock(); |
yqin70 | 1:a1aa9a79070a | 162 | uLCD.cls(); |
yqin70 | 1:a1aa9a79070a | 163 | uLCD.printf("You selected song %2d",songnum); |
yqin70 | 1:a1aa9a79070a | 164 | lcd_mutex.unlock(); |
yqin70 | 1:a1aa9a79070a | 165 | //add case statement based on songnum or something |
yqin70 | 1:a1aa9a79070a | 166 | //code for playing song from sd |
ShelbyC22 | 4:59b73f321c0e | 167 | switch (songnum) |
ShelbyC22 | 4:59b73f321c0e | 168 | { |
ShelbyC22 | 4:59b73f321c0e | 169 | case 1: |
ShelbyC22 | 4:59b73f321c0e | 170 | { |
ShelbyC22 | 4:59b73f321c0e | 171 | FILE *wave_file; |
ShelbyC22 | 4:59b73f321c0e | 172 | wave_file =fopen("/sd/4180 final project/Fireflies.wav", "r"); |
ShelbyC22 | 4:59b73f321c0e | 173 | waver.play(wave_file); |
ShelbyC22 | 4:59b73f321c0e | 174 | fclose(wave_file); |
ShelbyC22 | 4:59b73f321c0e | 175 | break; |
ShelbyC22 | 4:59b73f321c0e | 176 | } |
ShelbyC22 | 4:59b73f321c0e | 177 | case 2: |
ShelbyC22 | 4:59b73f321c0e | 178 | { |
ShelbyC22 | 4:59b73f321c0e | 179 | FILE *wave_file; |
ShelbyC22 | 4:59b73f321c0e | 180 | wave_file =fopen("/sd/4180 final project/The_Middle.wav", "r"); |
ShelbyC22 | 4:59b73f321c0e | 181 | waver.play(wave_file); |
ShelbyC22 | 4:59b73f321c0e | 182 | fclose(wave_file); |
ShelbyC22 | 4:59b73f321c0e | 183 | break; |
ShelbyC22 | 4:59b73f321c0e | 184 | } |
ShelbyC22 | 4:59b73f321c0e | 185 | case 3: |
ShelbyC22 | 4:59b73f321c0e | 186 | { |
ShelbyC22 | 4:59b73f321c0e | 187 | FILE *wave_file; |
ShelbyC22 | 4:59b73f321c0e | 188 | wave_file =fopen("/sd/4180 final project/Stacy's_Mom.wav", "r"); |
ShelbyC22 | 4:59b73f321c0e | 189 | waver.play(wave_file); |
ShelbyC22 | 4:59b73f321c0e | 190 | fclose(wave_file); |
ShelbyC22 | 4:59b73f321c0e | 191 | break; |
ShelbyC22 | 4:59b73f321c0e | 192 | } |
ShelbyC22 | 4:59b73f321c0e | 193 | case 4: |
ShelbyC22 | 4:59b73f321c0e | 194 | { |
ShelbyC22 | 4:59b73f321c0e | 195 | FILE *wave_file; |
ShelbyC22 | 4:59b73f321c0e | 196 | wave_file =fopen("/sd/4180 final project/Sins_!_Tragedies.wav", "r"); |
ShelbyC22 | 4:59b73f321c0e | 197 | waver.play(wave_file); |
ShelbyC22 | 4:59b73f321c0e | 198 | fclose(wave_file); |
ShelbyC22 | 4:59b73f321c0e | 199 | break; |
ShelbyC22 | 4:59b73f321c0e | 200 | } |
ShelbyC22 | 4:59b73f321c0e | 201 | default: |
ShelbyC22 | 4:59b73f321c0e | 202 | break; |
ShelbyC22 | 4:59b73f321c0e | 203 | } |
ShelbyC22 | 4:59b73f321c0e | 204 | |
yqin70 | 1:a1aa9a79070a | 205 | } |
yqin70 | 0:4f77ae831ee7 | 206 | Thread::wait(100); |
yqin70 | 0:4f77ae831ee7 | 207 | } |
yqin70 | 0:4f77ae831ee7 | 208 | } |
yqin70 | 0:4f77ae831ee7 | 209 | |
yqin70 | 0:4f77ae831ee7 | 210 |