inital commit
Dependencies: mbed wave_player mbed-rtos 4DGL-uLCD-SE SDFileSystem2 PinDetect MMA8452
main.cpp@3:8a1fd3450cb5, 2021-12-12 (annotated)
- Committer:
- lfink6
- Date:
- Sun Dec 12 00:15:41 2021 +0000
- Revision:
- 3:8a1fd3450cb5
- Parent:
- 2:c2afd0c426af
- Child:
- 5:afdd7d5b432a
Initial Commit;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
4180_1 | 1:5b8e223e983d | 1 | #include "mbed.h" |
4180_1 | 2:c2afd0c426af | 2 | #include "rtos.h" |
4180_1 | 1:5b8e223e983d | 3 | #include "SDFileSystem.h" |
lfink6 | 3:8a1fd3450cb5 | 4 | #include "uLCD_4DGL.h" |
4180_1 | 1:5b8e223e983d | 5 | #include "wave_player.h" |
lfink6 | 3:8a1fd3450cb5 | 6 | #include "LSM9DS1.h" |
lfink6 | 3:8a1fd3450cb5 | 7 | #include "PinDetect.h" |
lfink6 | 3:8a1fd3450cb5 | 8 | #include <string> |
lfink6 | 3:8a1fd3450cb5 | 9 | #include <vector> |
4180_1 | 1:5b8e223e983d | 10 | |
lfink6 | 3:8a1fd3450cb5 | 11 | DigitalOut myled(LED1); |
lfink6 | 3:8a1fd3450cb5 | 12 | Serial blue(p28,p27); |
4180_1 | 1:5b8e223e983d | 13 | |
4180_1 | 1:5b8e223e983d | 14 | SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card |
lfink6 | 3:8a1fd3450cb5 | 15 | uLCD_4DGL uLCD(p13,p14,p11); |
lfink6 | 3:8a1fd3450cb5 | 16 | |
lfink6 | 3:8a1fd3450cb5 | 17 | LSM9DS1 imu(p9, p10, 0xD6, 0x3C); |
lfink6 | 3:8a1fd3450cb5 | 18 | Serial pc(USBTX, USBRX); |
lfink6 | 3:8a1fd3450cb5 | 19 | |
lfink6 | 3:8a1fd3450cb5 | 20 | PinDetect playpause(p23); |
lfink6 | 3:8a1fd3450cb5 | 21 | PinDetect menu(p26); |
lfink6 | 3:8a1fd3450cb5 | 22 | PinDetect skip(p25); |
lfink6 | 3:8a1fd3450cb5 | 23 | PinDetect back(p30); |
lfink6 | 3:8a1fd3450cb5 | 24 | Mutex LCD; |
lfink6 | 3:8a1fd3450cb5 | 25 | Mutex Speaker; |
lfink6 | 3:8a1fd3450cb5 | 26 | Mutex avgs; |
lfink6 | 3:8a1fd3450cb5 | 27 | Mutex mentex; |
4180_1 | 1:5b8e223e983d | 28 | |
4180_1 | 1:5b8e223e983d | 29 | AnalogOut DACout(p18); |
4180_1 | 1:5b8e223e983d | 30 | |
4180_1 | 1:5b8e223e983d | 31 | wave_player waver(&DACout); |
4180_1 | 1:5b8e223e983d | 32 | |
lfink6 | 3:8a1fd3450cb5 | 33 | bool play = 0; |
lfink6 | 3:8a1fd3450cb5 | 34 | bool menu1 = 0; |
lfink6 | 3:8a1fd3450cb5 | 35 | int currentsong = 0; |
lfink6 | 3:8a1fd3450cb5 | 36 | int avgIMU; |
lfink6 | 3:8a1fd3450cb5 | 37 | int avgMic; |
lfink6 | 3:8a1fd3450cb5 | 38 | int songcount; |
lfink6 | 3:8a1fd3450cb5 | 39 | vector <string> songList; // vector of songs to index |
lfink6 | 3:8a1fd3450cb5 | 40 | |
lfink6 | 3:8a1fd3450cb5 | 41 | class microphone |
lfink6 | 3:8a1fd3450cb5 | 42 | { |
lfink6 | 3:8a1fd3450cb5 | 43 | public : |
lfink6 | 3:8a1fd3450cb5 | 44 | microphone(PinName pin); |
lfink6 | 3:8a1fd3450cb5 | 45 | float read(); |
lfink6 | 3:8a1fd3450cb5 | 46 | operator float (); |
lfink6 | 3:8a1fd3450cb5 | 47 | private : |
lfink6 | 3:8a1fd3450cb5 | 48 | AnalogIn _pin; |
lfink6 | 3:8a1fd3450cb5 | 49 | }; |
lfink6 | 3:8a1fd3450cb5 | 50 | microphone::microphone (PinName pin): |
lfink6 | 3:8a1fd3450cb5 | 51 | _pin(pin) |
lfink6 | 3:8a1fd3450cb5 | 52 | { |
lfink6 | 3:8a1fd3450cb5 | 53 | } |
lfink6 | 3:8a1fd3450cb5 | 54 | float microphone::read() |
lfink6 | 3:8a1fd3450cb5 | 55 | { |
lfink6 | 3:8a1fd3450cb5 | 56 | return _pin.read(); |
lfink6 | 3:8a1fd3450cb5 | 57 | } |
lfink6 | 3:8a1fd3450cb5 | 58 | inline microphone::operator float () |
lfink6 | 3:8a1fd3450cb5 | 59 | { |
lfink6 | 3:8a1fd3450cb5 | 60 | return _pin.read(); |
lfink6 | 3:8a1fd3450cb5 | 61 | } |
lfink6 | 3:8a1fd3450cb5 | 62 | |
lfink6 | 3:8a1fd3450cb5 | 63 | microphone mymicrophone(p16); |
lfink6 | 3:8a1fd3450cb5 | 64 | void TrackSelect() |
lfink6 | 3:8a1fd3450cb5 | 65 | { |
lfink6 | 3:8a1fd3450cb5 | 66 | //Add code here to use the avg to set the next track, or set the next track based on the menu. |
lfink6 | 3:8a1fd3450cb5 | 67 | } |
lfink6 | 3:8a1fd3450cb5 | 68 | void LCDThread(void const *argument) |
lfink6 | 3:8a1fd3450cb5 | 69 | { |
lfink6 | 3:8a1fd3450cb5 | 70 | while(1){ |
lfink6 | 3:8a1fd3450cb5 | 71 | if(!menu1) |
lfink6 | 3:8a1fd3450cb5 | 72 | { |
lfink6 | 3:8a1fd3450cb5 | 73 | LCD.lock(); |
lfink6 | 3:8a1fd3450cb5 | 74 | uLCD.locate(1,1); |
lfink6 | 3:8a1fd3450cb5 | 75 | uLCD.printf("Put Song Name Here"); |
lfink6 | 3:8a1fd3450cb5 | 76 | uLCD.printf("%4D", avgIMU); |
lfink6 | 3:8a1fd3450cb5 | 77 | uLCD.printf("%4D", avgMic); |
lfink6 | 3:8a1fd3450cb5 | 78 | if(!play) |
lfink6 | 3:8a1fd3450cb5 | 79 | { |
lfink6 | 3:8a1fd3450cb5 | 80 | //play |
lfink6 | 3:8a1fd3450cb5 | 81 | |
lfink6 | 3:8a1fd3450cb5 | 82 | uLCD.filled_rectangle(0,118,280,40,BLACK); |
lfink6 | 3:8a1fd3450cb5 | 83 | uLCD.triangle(120, 100, 40, 40, 10, 100, 0x0000FF); |
lfink6 | 3:8a1fd3450cb5 | 84 | } |
lfink6 | 3:8a1fd3450cb5 | 85 | else |
lfink6 | 3:8a1fd3450cb5 | 86 | { |
lfink6 | 3:8a1fd3450cb5 | 87 | //pause |
lfink6 | 3:8a1fd3450cb5 | 88 | uLCD.filled_rectangle(0,118,110,40,WHITE); |
lfink6 | 3:8a1fd3450cb5 | 89 | uLCD.filled_rectangle(50,118,100,40,BLACK); |
lfink6 | 3:8a1fd3450cb5 | 90 | uLCD.filled_rectangle(180,118,280,40,WHITE); |
lfink6 | 3:8a1fd3450cb5 | 91 | } |
lfink6 | 3:8a1fd3450cb5 | 92 | LCD.unlock(); |
lfink6 | 3:8a1fd3450cb5 | 93 | } |
lfink6 | 3:8a1fd3450cb5 | 94 | else |
lfink6 | 3:8a1fd3450cb5 | 95 | { |
lfink6 | 3:8a1fd3450cb5 | 96 | mentex.lock(); |
lfink6 | 3:8a1fd3450cb5 | 97 | if(currentsong<songcount+3) |
lfink6 | 3:8a1fd3450cb5 | 98 | { |
lfink6 | 3:8a1fd3450cb5 | 99 | for(int i=0; i<3; i++) |
lfink6 | 3:8a1fd3450cb5 | 100 | { |
lfink6 | 3:8a1fd3450cb5 | 101 | //add code to display and scroll through menu here |
lfink6 | 3:8a1fd3450cb5 | 102 | LCD.lock(); |
lfink6 | 3:8a1fd3450cb5 | 103 | uLCD.printf("\r%d. %s\r\n\r\n", (i+1), songList[currentsong+i].substr(0,songList[currentsong + i].find(".wav"))); |
lfink6 | 3:8a1fd3450cb5 | 104 | LCD.unlock(); |
lfink6 | 3:8a1fd3450cb5 | 105 | |
lfink6 | 3:8a1fd3450cb5 | 106 | } |
lfink6 | 3:8a1fd3450cb5 | 107 | currentsong= currentsong+3; |
lfink6 | 3:8a1fd3450cb5 | 108 | } |
lfink6 | 3:8a1fd3450cb5 | 109 | else |
lfink6 | 3:8a1fd3450cb5 | 110 | { |
lfink6 | 3:8a1fd3450cb5 | 111 | for(int i=0; i<songcount-currentsong; i++) |
lfink6 | 3:8a1fd3450cb5 | 112 | { |
lfink6 | 3:8a1fd3450cb5 | 113 | //add code to display and scroll through menu here |
lfink6 | 3:8a1fd3450cb5 | 114 | LCD.lock(); |
lfink6 | 3:8a1fd3450cb5 | 115 | uLCD.printf("\r%d. %s\r\n\r\n", (i+1), songList[currentsong+i].substr(0,songList[currentsong + i].find(".wav"))); |
lfink6 | 3:8a1fd3450cb5 | 116 | LCD.unlock(); |
lfink6 | 3:8a1fd3450cb5 | 117 | } |
lfink6 | 3:8a1fd3450cb5 | 118 | for(int i=0; i<3-(songcount-currentsong); i++) |
lfink6 | 3:8a1fd3450cb5 | 119 | { |
lfink6 | 3:8a1fd3450cb5 | 120 | //add code to display and scroll through menu here |
lfink6 | 3:8a1fd3450cb5 | 121 | LCD.lock(); |
lfink6 | 3:8a1fd3450cb5 | 122 | uLCD.printf("\r%d. %s\r\n\r\n", (i+1), songList[currentsong+i].substr(0,songList[currentsong + i].find(".wav"))); |
lfink6 | 3:8a1fd3450cb5 | 123 | LCD.unlock(); |
lfink6 | 3:8a1fd3450cb5 | 124 | } |
lfink6 | 3:8a1fd3450cb5 | 125 | } |
lfink6 | 3:8a1fd3450cb5 | 126 | mentex.unlock(); |
lfink6 | 3:8a1fd3450cb5 | 127 | } |
lfink6 | 3:8a1fd3450cb5 | 128 | |
lfink6 | 3:8a1fd3450cb5 | 129 | Thread::wait(1000); |
lfink6 | 3:8a1fd3450cb5 | 130 | } |
lfink6 | 3:8a1fd3450cb5 | 131 | |
lfink6 | 3:8a1fd3450cb5 | 132 | } |
lfink6 | 3:8a1fd3450cb5 | 133 | void buttonThread() |
lfink6 | 3:8a1fd3450cb5 | 134 | { |
lfink6 | 3:8a1fd3450cb5 | 135 | //add playpause and skip features here |
lfink6 | 3:8a1fd3450cb5 | 136 | Speaker.lock(); |
lfink6 | 3:8a1fd3450cb5 | 137 | if(play) |
lfink6 | 3:8a1fd3450cb5 | 138 | { |
lfink6 | 3:8a1fd3450cb5 | 139 | //playing = false; |
lfink6 | 3:8a1fd3450cb5 | 140 | } |
lfink6 | 3:8a1fd3450cb5 | 141 | else |
lfink6 | 3:8a1fd3450cb5 | 142 | { |
lfink6 | 3:8a1fd3450cb5 | 143 | //playing = true; |
lfink6 | 3:8a1fd3450cb5 | 144 | } |
lfink6 | 3:8a1fd3450cb5 | 145 | play=!play; |
lfink6 | 3:8a1fd3450cb5 | 146 | myled=play; |
lfink6 | 3:8a1fd3450cb5 | 147 | Speaker.unlock(); |
lfink6 | 3:8a1fd3450cb5 | 148 | Thread::wait(100); |
lfink6 | 3:8a1fd3450cb5 | 149 | } |
lfink6 | 3:8a1fd3450cb5 | 150 | void skipThread() |
lfink6 | 3:8a1fd3450cb5 | 151 | { |
lfink6 | 3:8a1fd3450cb5 | 152 | //add skip features |
lfink6 | 3:8a1fd3450cb5 | 153 | mentex.lock(); |
lfink6 | 3:8a1fd3450cb5 | 154 | if(currentsong<songcount) |
lfink6 | 3:8a1fd3450cb5 | 155 | { |
lfink6 | 3:8a1fd3450cb5 | 156 | currentsong++; |
lfink6 | 3:8a1fd3450cb5 | 157 | } |
lfink6 | 3:8a1fd3450cb5 | 158 | else |
lfink6 | 3:8a1fd3450cb5 | 159 | { |
lfink6 | 3:8a1fd3450cb5 | 160 | currentsong=0; |
lfink6 | 3:8a1fd3450cb5 | 161 | } |
lfink6 | 3:8a1fd3450cb5 | 162 | mentex.unlock(); |
lfink6 | 3:8a1fd3450cb5 | 163 | Thread::wait(100); |
lfink6 | 3:8a1fd3450cb5 | 164 | } |
lfink6 | 3:8a1fd3450cb5 | 165 | void backThread() |
lfink6 | 3:8a1fd3450cb5 | 166 | { |
lfink6 | 3:8a1fd3450cb5 | 167 | //add skip features |
lfink6 | 3:8a1fd3450cb5 | 168 | |
lfink6 | 3:8a1fd3450cb5 | 169 | mentex.lock(); |
lfink6 | 3:8a1fd3450cb5 | 170 | if(currentsong!=0) |
lfink6 | 3:8a1fd3450cb5 | 171 | { |
lfink6 | 3:8a1fd3450cb5 | 172 | currentsong--; |
lfink6 | 3:8a1fd3450cb5 | 173 | } |
lfink6 | 3:8a1fd3450cb5 | 174 | else |
lfink6 | 3:8a1fd3450cb5 | 175 | { |
lfink6 | 3:8a1fd3450cb5 | 176 | currentsong=songcount; |
lfink6 | 3:8a1fd3450cb5 | 177 | } |
lfink6 | 3:8a1fd3450cb5 | 178 | |
lfink6 | 3:8a1fd3450cb5 | 179 | mentex.unlock(); |
lfink6 | 3:8a1fd3450cb5 | 180 | Thread::wait(100); |
lfink6 | 3:8a1fd3450cb5 | 181 | } |
lfink6 | 3:8a1fd3450cb5 | 182 | void menuThread() |
lfink6 | 3:8a1fd3450cb5 | 183 | { |
lfink6 | 3:8a1fd3450cb5 | 184 | Speaker.lock(); |
lfink6 | 3:8a1fd3450cb5 | 185 | menu1=!menu1; |
lfink6 | 3:8a1fd3450cb5 | 186 | Speaker.unlock(); |
lfink6 | 3:8a1fd3450cb5 | 187 | Thread::wait(100); |
lfink6 | 3:8a1fd3450cb5 | 188 | } |
lfink6 | 3:8a1fd3450cb5 | 189 | void BlueThread(void const *argument) |
lfink6 | 3:8a1fd3450cb5 | 190 | { |
lfink6 | 3:8a1fd3450cb5 | 191 | //add bluetooth control code here |
lfink6 | 3:8a1fd3450cb5 | 192 | while(1) |
lfink6 | 3:8a1fd3450cb5 | 193 | { |
lfink6 | 3:8a1fd3450cb5 | 194 | if(blue.readable()) |
lfink6 | 3:8a1fd3450cb5 | 195 | { |
lfink6 | 3:8a1fd3450cb5 | 196 | |
lfink6 | 3:8a1fd3450cb5 | 197 | } |
lfink6 | 3:8a1fd3450cb5 | 198 | } |
lfink6 | 3:8a1fd3450cb5 | 199 | } |
lfink6 | 3:8a1fd3450cb5 | 200 | void IMUThread(void const *argument) |
lfink6 | 3:8a1fd3450cb5 | 201 | { |
lfink6 | 3:8a1fd3450cb5 | 202 | while(1){ |
lfink6 | 3:8a1fd3450cb5 | 203 | avgs.lock(); |
lfink6 | 3:8a1fd3450cb5 | 204 | //put imu averaging and next track selection code here if that is selected |
lfink6 | 3:8a1fd3450cb5 | 205 | avgMic = int(((abs((mymicrophone - (0.67/3.3)))*500.0)+avgMic)/2); |
lfink6 | 3:8a1fd3450cb5 | 206 | imu.readAccel(); |
lfink6 | 3:8a1fd3450cb5 | 207 | avgIMU=int((((imu.ax+imu.az+imu.ay)/3.0)+avgIMU)/2); |
lfink6 | 3:8a1fd3450cb5 | 208 | avgs.unlock(); |
lfink6 | 3:8a1fd3450cb5 | 209 | Thread::wait(2500); |
lfink6 | 3:8a1fd3450cb5 | 210 | } |
lfink6 | 3:8a1fd3450cb5 | 211 | |
lfink6 | 3:8a1fd3450cb5 | 212 | } |
lfink6 | 3:8a1fd3450cb5 | 213 | |
4180_1 | 1:5b8e223e983d | 214 | int main() |
4180_1 | 1:5b8e223e983d | 215 | { |
lfink6 | 3:8a1fd3450cb5 | 216 | uLCD.cls(); |
lfink6 | 3:8a1fd3450cb5 | 217 | uLCD.baudrate(3000000); |
lfink6 | 3:8a1fd3450cb5 | 218 | uLCD.background_color(BLACK); |
lfink6 | 3:8a1fd3450cb5 | 219 | uLCD.text_width(1); |
lfink6 | 3:8a1fd3450cb5 | 220 | uLCD.text_height(1); |
lfink6 | 3:8a1fd3450cb5 | 221 | imu.begin(); |
lfink6 | 3:8a1fd3450cb5 | 222 | if (!imu.begin()) { |
lfink6 | 3:8a1fd3450cb5 | 223 | //set fail flag for imu here |
lfink6 | 3:8a1fd3450cb5 | 224 | } |
lfink6 | 3:8a1fd3450cb5 | 225 | imu.calibrate(); |
lfink6 | 3:8a1fd3450cb5 | 226 | blue.baud(9600); |
lfink6 | 3:8a1fd3450cb5 | 227 | |
lfink6 | 3:8a1fd3450cb5 | 228 | DIR *dp; |
lfink6 | 3:8a1fd3450cb5 | 229 | struct dirent *dirp; |
lfink6 | 3:8a1fd3450cb5 | 230 | dp = opendir("/sd/myMusic"); |
lfink6 | 3:8a1fd3450cb5 | 231 | songcount = 0; |
lfink6 | 3:8a1fd3450cb5 | 232 | if(dp !=NULL) |
lfink6 | 3:8a1fd3450cb5 | 233 | { |
lfink6 | 3:8a1fd3450cb5 | 234 | while ((dirp = readdir(dp)) != NULL) { |
lfink6 | 3:8a1fd3450cb5 | 235 | songList.push_back(string(dirp->d_name)); |
lfink6 | 3:8a1fd3450cb5 | 236 | //uLCD.printf("\r%s\r\n", string(dirp->d_name)); |
lfink6 | 3:8a1fd3450cb5 | 237 | songcount++; |
lfink6 | 3:8a1fd3450cb5 | 238 | } |
lfink6 | 3:8a1fd3450cb5 | 239 | } |
lfink6 | 3:8a1fd3450cb5 | 240 | playpause.mode(PullUp); |
lfink6 | 3:8a1fd3450cb5 | 241 | playpause.attach_deasserted(&buttonThread); |
lfink6 | 3:8a1fd3450cb5 | 242 | playpause.setSampleFrequency(); |
lfink6 | 3:8a1fd3450cb5 | 243 | |
lfink6 | 3:8a1fd3450cb5 | 244 | menu.mode(PullUp); |
lfink6 | 3:8a1fd3450cb5 | 245 | menu.attach_deasserted(&menuThread); |
lfink6 | 3:8a1fd3450cb5 | 246 | menu.setSampleFrequency(); |
lfink6 | 3:8a1fd3450cb5 | 247 | skip.mode(PullUp); |
lfink6 | 3:8a1fd3450cb5 | 248 | skip.attach_deasserted(&skipThread); |
lfink6 | 3:8a1fd3450cb5 | 249 | skip.setSampleFrequency(); |
lfink6 | 3:8a1fd3450cb5 | 250 | back.mode(PullUp); |
lfink6 | 3:8a1fd3450cb5 | 251 | back.attach_deasserted(&backThread); |
lfink6 | 3:8a1fd3450cb5 | 252 | back.setSampleFrequency(); |
lfink6 | 3:8a1fd3450cb5 | 253 | //LCD, Player, button Interrupt, bluetooth, imu+mic |
lfink6 | 3:8a1fd3450cb5 | 254 | Thread thread1(LCDThread); |
lfink6 | 3:8a1fd3450cb5 | 255 | Thread thread4(BlueThread); |
lfink6 | 3:8a1fd3450cb5 | 256 | Thread thread5(IMUThread); |
lfink6 | 3:8a1fd3450cb5 | 257 | while(1){ |
4180_1 | 1:5b8e223e983d | 258 | FILE *wave_file; |
4180_1 | 2:c2afd0c426af | 259 | Thread::wait(1000); |
4180_1 | 1:5b8e223e983d | 260 | wave_file=fopen("/sd/sample.wav","r"); |
4180_1 | 2:c2afd0c426af | 261 | if(wave_file==NULL) printf("file open error!\n\n\r"); |
4180_1 | 1:5b8e223e983d | 262 | waver.play(wave_file); |
4180_1 | 1:5b8e223e983d | 263 | fclose(wave_file); |
lfink6 | 3:8a1fd3450cb5 | 264 | } |
4180_1 | 1:5b8e223e983d | 265 | } |