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