ECE 4180 Final
Dependencies: mbed wave_player mbed-rtos C12832_lcd 4DGL-uLCD-SE LCD_fonts SDFileSystem
main.cpp@15:ebe9fb3aaf0f, 2019-12-06 (annotated)
- Committer:
- yqin70
- Date:
- Fri Dec 06 02:44:52 2019 +0000
- Revision:
- 15:ebe9fb3aaf0f
- Parent:
- 14:6f5432bb44c0
- Child:
- 16:13b65f6139be
time changed to sample index for first two songs
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 | 14:6f5432bb44c0 | 143 | lcd_mutex.lock(); |
jcrane32 | 7:15fdc55dbf66 | 144 | uLCD.baudrate(3000000); |
yqin70 | 14:6f5432bb44c0 | 145 | uLCD.cls(); |
yqin70 | 14:6f5432bb44c0 | 146 | uLCD.media_init(); |
yqin70 | 14:6f5432bb44c0 | 147 | uLCD.set_sector_address(0x0077, 0x4002); |
yqin70 | 14:6f5432bb44c0 | 148 | uLCD.display_video(0,0); |
yqin70 | 14:6f5432bb44c0 | 149 | wait(4); |
yqin70 | 14:6f5432bb44c0 | 150 | lcd_mutex.unlock(); |
jcrane32 | 6:cd24147b5e50 | 151 | thread1.start(homescreen_thread); |
jcrane32 | 7:15fdc55dbf66 | 152 | thread2.start(joystick_thread); |
jcrane32 | 6:cd24147b5e50 | 153 | thread3.start(pbcontrol_thread); |
jcrane32 | 7:15fdc55dbf66 | 154 | // nextsample.attach(&playsound, 1.0/8000.0); |
ShelbyC22 | 5:b2cf15651d4e | 155 | //startup sound |
ShelbyC22 | 5:b2cf15651d4e | 156 | myRGBled = blue; //tested to make sure led works, we can use whatever color(s) here |
ShelbyC22 | 5:b2cf15651d4e | 157 | FILE *wave_file; |
ShelbyC22 | 5:b2cf15651d4e | 158 | wave_file=fopen("/sd/cheer.wav","r"); |
ShelbyC22 | 5:b2cf15651d4e | 159 | waver.play(wave_file); |
ShelbyC22 | 5:b2cf15651d4e | 160 | fclose(wave_file); |
yqin70 | 0:4f77ae831ee7 | 161 | while(1) |
yqin70 | 0:4f77ae831ee7 | 162 | { |
yqin70 | 0:4f77ae831ee7 | 163 | |
yqin70 | 1:a1aa9a79070a | 164 | if (songselect){ |
yqin70 | 1:a1aa9a79070a | 165 | myled = 0; |
yqin70 | 1:a1aa9a79070a | 166 | homescreen = false; |
yqin70 | 1:a1aa9a79070a | 167 | lcd_mutex.lock(); |
yqin70 | 1:a1aa9a79070a | 168 | uLCD.cls(); |
yqin70 | 1:a1aa9a79070a | 169 | uLCD.printf("You selected song %2d",songnum); |
yqin70 | 1:a1aa9a79070a | 170 | lcd_mutex.unlock(); |
yqin70 | 1:a1aa9a79070a | 171 | //add case statement based on songnum or something |
yqin70 | 1:a1aa9a79070a | 172 | //code for playing song from sd |
ShelbyC22 | 4:59b73f321c0e | 173 | switch (songnum) |
ShelbyC22 | 4:59b73f321c0e | 174 | { |
ShelbyC22 | 4:59b73f321c0e | 175 | case 1: |
ShelbyC22 | 4:59b73f321c0e | 176 | { |
ShelbyC22 | 4:59b73f321c0e | 177 | FILE *wave_file; |
ShelbyC22 | 4:59b73f321c0e | 178 | wave_file =fopen("/sd/4180 final project/Fireflies.wav", "r"); |
ShelbyC22 | 4:59b73f321c0e | 179 | waver.play(wave_file); |
ShelbyC22 | 4:59b73f321c0e | 180 | fclose(wave_file); |
ShelbyC22 | 4:59b73f321c0e | 181 | break; |
ShelbyC22 | 4:59b73f321c0e | 182 | } |
ShelbyC22 | 4:59b73f321c0e | 183 | case 2: |
ShelbyC22 | 4:59b73f321c0e | 184 | { |
ShelbyC22 | 4:59b73f321c0e | 185 | FILE *wave_file; |
ShelbyC22 | 4:59b73f321c0e | 186 | wave_file =fopen("/sd/4180 final project/The_Middle.wav", "r"); |
ShelbyC22 | 4:59b73f321c0e | 187 | waver.play(wave_file); |
ShelbyC22 | 4:59b73f321c0e | 188 | fclose(wave_file); |
ShelbyC22 | 4:59b73f321c0e | 189 | break; |
ShelbyC22 | 4:59b73f321c0e | 190 | } |
ShelbyC22 | 4:59b73f321c0e | 191 | case 3: |
ShelbyC22 | 4:59b73f321c0e | 192 | { |
ShelbyC22 | 4:59b73f321c0e | 193 | FILE *wave_file; |
ShelbyC22 | 4:59b73f321c0e | 194 | wave_file =fopen("/sd/4180 final project/Stacy's_Mom.wav", "r"); |
ShelbyC22 | 4:59b73f321c0e | 195 | waver.play(wave_file); |
ShelbyC22 | 4:59b73f321c0e | 196 | fclose(wave_file); |
ShelbyC22 | 4:59b73f321c0e | 197 | break; |
ShelbyC22 | 4:59b73f321c0e | 198 | } |
ShelbyC22 | 4:59b73f321c0e | 199 | case 4: |
ShelbyC22 | 4:59b73f321c0e | 200 | { |
ShelbyC22 | 4:59b73f321c0e | 201 | FILE *wave_file; |
ShelbyC22 | 4:59b73f321c0e | 202 | wave_file =fopen("/sd/4180 final project/Sins_!_Tragedies.wav", "r"); |
ShelbyC22 | 4:59b73f321c0e | 203 | waver.play(wave_file); |
ShelbyC22 | 4:59b73f321c0e | 204 | fclose(wave_file); |
ShelbyC22 | 4:59b73f321c0e | 205 | break; |
ShelbyC22 | 4:59b73f321c0e | 206 | } |
ShelbyC22 | 4:59b73f321c0e | 207 | default: |
ShelbyC22 | 4:59b73f321c0e | 208 | break; |
ShelbyC22 | 4:59b73f321c0e | 209 | } |
ShelbyC22 | 4:59b73f321c0e | 210 | |
yqin70 | 1:a1aa9a79070a | 211 | } |
yqin70 | 0:4f77ae831ee7 | 212 | Thread::wait(100); |
yqin70 | 0:4f77ae831ee7 | 213 | } |
yqin70 | 0:4f77ae831ee7 | 214 | } |
yqin70 | 0:4f77ae831ee7 | 215 | |
yqin70 | 0:4f77ae831ee7 | 216 |