ECE 4180 Final

Dependencies:   mbed wave_player mbed-rtos C12832_lcd 4DGL-uLCD-SE LCD_fonts SDFileSystem

Committer:
ShelbyC22
Date:
Fri Dec 06 03:44:10 2019 +0000
Revision:
17:13e45fdcf0b0
Parent:
16:13b65f6139be
Child:
20:7d56cdcbc9a5
fixed touch keypad code;

Who changed what in which revision?

UserRevisionLine numberNew 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 17:13e45fdcf0b0 55 for (i=0; i<12; i++) {
ShelbyC22 17:13e45fdcf0b0 56 if (((value>>i)&0x01)==1) {
ShelbyC22 17:13e45fdcf0b0 57 key_code=i+1;
ShelbyC22 17:13e45fdcf0b0 58 }
ShelbyC22 17:13e45fdcf0b0 59 }
ShelbyC22 16:13b65f6139be 60 }
ShelbyC22 16:13b65f6139be 61
yqin70 1:a1aa9a79070a 62 wave_player waver(&DACout);
jcrane32 6:cd24147b5e50 63 Thread thread1, thread2, thread3;
jcrane32 7:15fdc55dbf66 64 Ticker nextsample;
jcrane32 7:15fdc55dbf66 65 int songNdx = 0;
yqin70 0:4f77ae831ee7 66
yqin70 0:4f77ae831ee7 67 // mutex to make the lcd lib thread safe
yqin70 0:4f77ae831ee7 68 Mutex lcd_mutex;
yqin70 0:4f77ae831ee7 69 int songnum = 1;
yqin70 0:4f77ae831ee7 70 AnalogIn joy_pot(p16);
jcrane32 6:cd24147b5e50 71
yqin70 0:4f77ae831ee7 72 // Thread 1
yqin70 1:a1aa9a79070a 73 // print homescreen to LCD
ShelbyC22 5:b2cf15651d4e 74 //new homescreen that doesn't have the flashing
jcrane32 6:cd24147b5e50 75 void homescreen_thread()
yqin70 0:4f77ae831ee7 76 {
yqin70 0:4f77ae831ee7 77 while(true) { // thread loop
ShelbyC22 5:b2cf15651d4e 78 lcd_mutex.lock();
yqin70 0:4f77ae831ee7 79 if (homescreen){
yqin70 0:4f77ae831ee7 80 uLCD.cls();
ShelbyC22 5:b2cf15651d4e 81 uLCD.text_height(1.3);
yqin70 2:765774781bb1 82 uLCD.text_width(1.9);
yqin70 0:4f77ae831ee7 83 uLCD.color(WHITE);
ShelbyC22 5:b2cf15651d4e 84 uLCD.locate(5,0);
ShelbyC22 5:b2cf15651d4e 85 uLCD.printf("Tap Tap");
ShelbyC22 5:b2cf15651d4e 86 uLCD.locate(4,1);
ShelbyC22 5:b2cf15651d4e 87 uLCD.printf("Revolution");
ShelbyC22 5:b2cf15651d4e 88 uLCD.locate(0,3);
jcrane32 7:15fdc55dbf66 89 uLCD.printf("Pick a song!");
ShelbyC22 5:b2cf15651d4e 90 uLCD.text_height(1.3);
yqin70 2:765774781bb1 91 uLCD.text_width(1.9);
ShelbyC22 5:b2cf15651d4e 92 uLCD.locate(3,5);
ShelbyC22 4:59b73f321c0e 93 uLCD.printf("Fireflies");
ShelbyC22 5:b2cf15651d4e 94 uLCD.locate(3,7);
ShelbyC22 4:59b73f321c0e 95 uLCD.printf("The Middle");
ShelbyC22 5:b2cf15651d4e 96 uLCD.locate(3,9);
ShelbyC22 4:59b73f321c0e 97 uLCD.printf("Stacy's Mom");
ShelbyC22 5:b2cf15651d4e 98 uLCD.locate(3,11);
jcrane32 7:15fdc55dbf66 99 uLCD.printf("I Write Sins \n \t Not Tragedies");
ShelbyC22 5:b2cf15651d4e 100 uLCD.filled_circle(6, 43, 4, GREEN); //new selection with circle instead of rectangle
ShelbyC22 5:b2cf15651d4e 101 homescreen = false;}
ShelbyC22 5:b2cf15651d4e 102 if (songchange){
ShelbyC22 5:b2cf15651d4e 103 for (int i = 0; i < 4; i++){
ShelbyC22 5:b2cf15651d4e 104 uLCD.filled_circle(6, i*15+43, 4,BLACK);
ShelbyC22 5:b2cf15651d4e 105 }
ShelbyC22 5:b2cf15651d4e 106 uLCD.filled_circle(6, (songnum-1)*15+43, 4,GREEN);
ShelbyC22 5:b2cf15651d4e 107 songchange = false;
ShelbyC22 5:b2cf15651d4e 108 }
yqin70 0:4f77ae831ee7 109 lcd_mutex.unlock();
ShelbyC22 5:b2cf15651d4e 110 Thread::wait(200);
yqin70 0:4f77ae831ee7 111 }
yqin70 0:4f77ae831ee7 112 }
yqin70 0:4f77ae831ee7 113
jcrane32 6:cd24147b5e50 114 // Thread 2
jcrane32 7:15fdc55dbf66 115 // joystick control for song selection during homescreen
jcrane32 6:cd24147b5e50 116 void joystick_thread()
yqin70 0:4f77ae831ee7 117 {
yqin70 0:4f77ae831ee7 118 while(1){
yqin70 0:4f77ae831ee7 119 if ((joy_pot <= (1.4/3.3)) && songnum>1) {
yqin70 0:4f77ae831ee7 120 songnum--;
ShelbyC22 5:b2cf15651d4e 121 songchange = true;
ShelbyC22 5:b2cf15651d4e 122 }
yqin70 2:765774781bb1 123 else if ((joy_pot >= (1.8/3.3)) && songnum<4){
yqin70 0:4f77ae831ee7 124 songnum++;
ShelbyC22 5:b2cf15651d4e 125 songchange = true;
yqin70 0:4f77ae831ee7 126 }
yqin70 0:4f77ae831ee7 127 Thread::wait(250);
yqin70 0:4f77ae831ee7 128 }
yqin70 0:4f77ae831ee7 129 }
yqin70 0:4f77ae831ee7 130
yqin70 0:4f77ae831ee7 131 // Thread 3
yqin70 1:a1aa9a79070a 132 //pb1 is to select song
yqin70 1:a1aa9a79070a 133 //pb2 is to return to homescreen
jcrane32 6:cd24147b5e50 134 void pbcontrol_thread()
yqin70 0:4f77ae831ee7 135 {
yqin70 0:4f77ae831ee7 136 pb1.mode(PullUp);
yqin70 0:4f77ae831ee7 137 pb2.mode(PullUp);
yqin70 0:4f77ae831ee7 138 while(true) { // thread loop
yqin70 0:4f77ae831ee7 139 if (!pb2)
yqin70 0:4f77ae831ee7 140 {
yqin70 0:4f77ae831ee7 141 homescreen = true;
yqin70 0:4f77ae831ee7 142 songselect = false;
yqin70 0:4f77ae831ee7 143 }
yqin70 0:4f77ae831ee7 144 if (!pb1)
yqin70 0:4f77ae831ee7 145 {
yqin70 0:4f77ae831ee7 146 songselect = true;
yqin70 0:4f77ae831ee7 147 }
yqin70 0:4f77ae831ee7 148 Thread::wait(100); // value of pot1 / 100
yqin70 0:4f77ae831ee7 149 }
yqin70 0:4f77ae831ee7 150 }
yqin70 0:4f77ae831ee7 151
jcrane32 7:15fdc55dbf66 152 // Thread 4
jcrane32 7:15fdc55dbf66 153 // this thread plays music using the ticker class
jcrane32 7:15fdc55dbf66 154 void playsound() {
jcrane32 7:15fdc55dbf66 155 if (playing) {
jcrane32 7:15fdc55dbf66 156 //DACout = song[songNdx++];
jcrane32 7:15fdc55dbf66 157 //if (i>**songlength**) i
jcrane32 7:15fdc55dbf66 158 } else {
jcrane32 7:15fdc55dbf66 159 DACout = 0;
jcrane32 7:15fdc55dbf66 160 songNdx = 0;
jcrane32 7:15fdc55dbf66 161 }
jcrane32 7:15fdc55dbf66 162 }
jcrane32 7:15fdc55dbf66 163
jcrane32 7:15fdc55dbf66 164 int main() {
ShelbyC22 17:13e45fdcf0b0 165 interrupt.fall(&fallInterrupt);
ShelbyC22 17:13e45fdcf0b0 166 interrupt.mode(PullUp); //for touch keypad
yqin70 14:6f5432bb44c0 167 lcd_mutex.lock();
jcrane32 7:15fdc55dbf66 168 uLCD.baudrate(3000000);
yqin70 14:6f5432bb44c0 169 uLCD.cls();
yqin70 14:6f5432bb44c0 170 uLCD.media_init();
yqin70 14:6f5432bb44c0 171 uLCD.set_sector_address(0x0077, 0x4002);
yqin70 14:6f5432bb44c0 172 uLCD.display_video(0,0);
yqin70 14:6f5432bb44c0 173 wait(4);
yqin70 14:6f5432bb44c0 174 lcd_mutex.unlock();
jcrane32 6:cd24147b5e50 175 thread1.start(homescreen_thread);
jcrane32 7:15fdc55dbf66 176 thread2.start(joystick_thread);
jcrane32 6:cd24147b5e50 177 thread3.start(pbcontrol_thread);
jcrane32 7:15fdc55dbf66 178 // nextsample.attach(&playsound, 1.0/8000.0);
ShelbyC22 5:b2cf15651d4e 179 //startup sound
ShelbyC22 5:b2cf15651d4e 180 myRGBled = blue; //tested to make sure led works, we can use whatever color(s) here
ShelbyC22 5:b2cf15651d4e 181 FILE *wave_file;
ShelbyC22 5:b2cf15651d4e 182 wave_file=fopen("/sd/cheer.wav","r");
ShelbyC22 5:b2cf15651d4e 183 waver.play(wave_file);
ShelbyC22 5:b2cf15651d4e 184 fclose(wave_file);
yqin70 0:4f77ae831ee7 185 while(1)
yqin70 0:4f77ae831ee7 186 {
yqin70 0:4f77ae831ee7 187
yqin70 1:a1aa9a79070a 188 if (songselect){
yqin70 1:a1aa9a79070a 189 myled = 0;
yqin70 1:a1aa9a79070a 190 homescreen = false;
yqin70 1:a1aa9a79070a 191 lcd_mutex.lock();
yqin70 1:a1aa9a79070a 192 uLCD.cls();
yqin70 1:a1aa9a79070a 193 uLCD.printf("You selected song %2d",songnum);
yqin70 1:a1aa9a79070a 194 lcd_mutex.unlock();
yqin70 1:a1aa9a79070a 195 //add case statement based on songnum or something
yqin70 1:a1aa9a79070a 196 //code for playing song from sd
ShelbyC22 4:59b73f321c0e 197 switch (songnum)
ShelbyC22 4:59b73f321c0e 198 {
ShelbyC22 4:59b73f321c0e 199 case 1:
ShelbyC22 4:59b73f321c0e 200 {
ShelbyC22 4:59b73f321c0e 201 FILE *wave_file;
ShelbyC22 4:59b73f321c0e 202 wave_file =fopen("/sd/4180 final project/Fireflies.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 case 2:
ShelbyC22 4:59b73f321c0e 208 {
ShelbyC22 4:59b73f321c0e 209 FILE *wave_file;
ShelbyC22 4:59b73f321c0e 210 wave_file =fopen("/sd/4180 final project/The_Middle.wav", "r");
ShelbyC22 4:59b73f321c0e 211 waver.play(wave_file);
ShelbyC22 4:59b73f321c0e 212 fclose(wave_file);
ShelbyC22 4:59b73f321c0e 213 break;
ShelbyC22 4:59b73f321c0e 214 }
ShelbyC22 4:59b73f321c0e 215 case 3:
ShelbyC22 4:59b73f321c0e 216 {
ShelbyC22 4:59b73f321c0e 217 FILE *wave_file;
ShelbyC22 4:59b73f321c0e 218 wave_file =fopen("/sd/4180 final project/Stacy's_Mom.wav", "r");
ShelbyC22 4:59b73f321c0e 219 waver.play(wave_file);
ShelbyC22 4:59b73f321c0e 220 fclose(wave_file);
ShelbyC22 4:59b73f321c0e 221 break;
ShelbyC22 4:59b73f321c0e 222 }
ShelbyC22 4:59b73f321c0e 223 case 4:
ShelbyC22 4:59b73f321c0e 224 {
ShelbyC22 4:59b73f321c0e 225 FILE *wave_file;
ShelbyC22 4:59b73f321c0e 226 wave_file =fopen("/sd/4180 final project/Sins_!_Tragedies.wav", "r");
ShelbyC22 4:59b73f321c0e 227 waver.play(wave_file);
ShelbyC22 4:59b73f321c0e 228 fclose(wave_file);
ShelbyC22 4:59b73f321c0e 229 break;
ShelbyC22 4:59b73f321c0e 230 }
ShelbyC22 4:59b73f321c0e 231 default:
ShelbyC22 4:59b73f321c0e 232 break;
ShelbyC22 4:59b73f321c0e 233 }
ShelbyC22 4:59b73f321c0e 234
yqin70 1:a1aa9a79070a 235 }
yqin70 0:4f77ae831ee7 236 Thread::wait(100);
yqin70 0:4f77ae831ee7 237 }
yqin70 0:4f77ae831ee7 238 }
yqin70 0:4f77ae831ee7 239
yqin70 0:4f77ae831ee7 240