inital commit

Dependencies:   mbed wave_player mbed-rtos 4DGL-uLCD-SE SDFileSystem2 PinDetect MMA8452

Committer:
lfink6
Date:
Sun Dec 12 23:11:48 2021 +0000
Revision:
9:08886cc06a5d
Parent:
7:71e9ee360b90
Child:
10:5bd6abd66d12
ver 5;

Who changed what in which revision?

UserRevisionLine numberNew 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 9:08886cc06a5d 12 RawSerial 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);
lfink6 6:6315ecdd0b47 32 bool playing = false;
lfink6 9:08886cc06a5d 33 bool play = 1;
lfink6 3:8a1fd3450cb5 34 bool menu1 = 0;
lfink6 6:6315ecdd0b47 35 bool inmenu=0;
lfink6 3:8a1fd3450cb5 36 int currentsong = 0;
lfink6 3:8a1fd3450cb5 37 int avgIMU;
lfink6 3:8a1fd3450cb5 38 int avgMic;
lfink6 3:8a1fd3450cb5 39 int songcount;
lfink6 3:8a1fd3450cb5 40 vector <string> songList; // vector of songs to index
lfink6 5:afdd7d5b432a 41 string dir = "/sd/myMusic/";
lfink6 3:8a1fd3450cb5 42
lfink6 3:8a1fd3450cb5 43 class microphone
lfink6 3:8a1fd3450cb5 44 {
lfink6 3:8a1fd3450cb5 45 public :
lfink6 3:8a1fd3450cb5 46 microphone(PinName pin);
lfink6 3:8a1fd3450cb5 47 float read();
lfink6 3:8a1fd3450cb5 48 operator float ();
lfink6 3:8a1fd3450cb5 49 private :
lfink6 3:8a1fd3450cb5 50 AnalogIn _pin;
lfink6 3:8a1fd3450cb5 51 };
lfink6 3:8a1fd3450cb5 52 microphone::microphone (PinName pin):
lfink6 3:8a1fd3450cb5 53 _pin(pin)
lfink6 3:8a1fd3450cb5 54 {
lfink6 3:8a1fd3450cb5 55 }
lfink6 3:8a1fd3450cb5 56 float microphone::read()
lfink6 3:8a1fd3450cb5 57 {
lfink6 3:8a1fd3450cb5 58 return _pin.read();
lfink6 3:8a1fd3450cb5 59 }
lfink6 3:8a1fd3450cb5 60 inline microphone::operator float ()
lfink6 3:8a1fd3450cb5 61 {
lfink6 3:8a1fd3450cb5 62 return _pin.read();
lfink6 3:8a1fd3450cb5 63 }
lfink6 3:8a1fd3450cb5 64
lfink6 3:8a1fd3450cb5 65 microphone mymicrophone(p16);
lfink6 3:8a1fd3450cb5 66 void LCDThread(void const *argument)
lfink6 3:8a1fd3450cb5 67 {
lfink6 3:8a1fd3450cb5 68 while(1){
lfink6 6:6315ecdd0b47 69 Speaker.lock();
lfink6 3:8a1fd3450cb5 70 if(!menu1)
lfink6 3:8a1fd3450cb5 71 {
lfink6 6:6315ecdd0b47 72 Speaker.unlock();
lfink6 6:6315ecdd0b47 73 LCD.lock();
lfink6 6:6315ecdd0b47 74 uLCD.cls();
lfink6 6:6315ecdd0b47 75 uLCD.locate(1,1);
lfink6 6:6315ecdd0b47 76 uLCD.printf("%s",songList[currentsong].substr(0,songList[currentsong].find(".wav")));
lfink6 9:08886cc06a5d 77 if(play)
lfink6 6:6315ecdd0b47 78 {
lfink6 6:6315ecdd0b47 79 //play
lfink6 3:8a1fd3450cb5 80
lfink6 6:6315ecdd0b47 81 uLCD.filled_rectangle(0,118,280,40,BLACK);
lfink6 6:6315ecdd0b47 82 uLCD.triangle(120, 100, 40, 40, 10, 100, 0x0000FF);
lfink6 6:6315ecdd0b47 83 }
lfink6 6:6315ecdd0b47 84 else
lfink6 6:6315ecdd0b47 85 {
lfink6 3:8a1fd3450cb5 86 //pause
lfink6 6:6315ecdd0b47 87 uLCD.filled_rectangle(0,118,110,40,WHITE);
lfink6 6:6315ecdd0b47 88 uLCD.filled_rectangle(50,118,100,40,BLACK);
lfink6 6:6315ecdd0b47 89 uLCD.filled_rectangle(180,118,280,40,WHITE);
lfink6 6:6315ecdd0b47 90 }
lfink6 6:6315ecdd0b47 91 LCD.unlock();
lfink6 6:6315ecdd0b47 92 }
lfink6 6:6315ecdd0b47 93 else
lfink6 6:6315ecdd0b47 94 {
lfink6 6:6315ecdd0b47 95
lfink6 6:6315ecdd0b47 96 Speaker.unlock();
lfink6 6:6315ecdd0b47 97 mentex.lock();
lfink6 6:6315ecdd0b47 98 LCD.lock();
lfink6 6:6315ecdd0b47 99 uLCD.cls();
lfink6 6:6315ecdd0b47 100 LCD.unlock();
lfink6 6:6315ecdd0b47 101 for(int i=-1; i<2; i++)
lfink6 6:6315ecdd0b47 102 {
lfink6 6:6315ecdd0b47 103 //add code to display and scroll through menu here
lfink6 6:6315ecdd0b47 104
lfink6 6:6315ecdd0b47 105 mentex.lock();
lfink6 6:6315ecdd0b47 106 if(currentsong+i>=0)
lfink6 6:6315ecdd0b47 107 {
lfink6 6:6315ecdd0b47 108 if(currentsong+i<songcount)
lfink6 6:6315ecdd0b47 109 {
lfink6 6:6315ecdd0b47 110 LCD.lock();
lfink6 6:6315ecdd0b47 111 uLCD.printf("%s\r\n\r\n", songList[currentsong+i].substr(0,songList[currentsong + i].find(".wav")));
lfink6 6:6315ecdd0b47 112 LCD.unlock();
lfink6 6:6315ecdd0b47 113 }
lfink6 6:6315ecdd0b47 114 else
lfink6 6:6315ecdd0b47 115 {
lfink6 6:6315ecdd0b47 116 LCD.lock();
lfink6 6:6315ecdd0b47 117 uLCD.printf("%s\r\n\r\n", songList[0].substr(0,songList[0].find(".wav")));
lfink6 6:6315ecdd0b47 118 LCD.unlock();
lfink6 6:6315ecdd0b47 119 }
lfink6 6:6315ecdd0b47 120 }
lfink6 6:6315ecdd0b47 121 else if(currentsong+i<0)
lfink6 6:6315ecdd0b47 122 {
lfink6 6:6315ecdd0b47 123 LCD.lock();
lfink6 6:6315ecdd0b47 124 uLCD.printf("%s\r\n\r\n", songList[songcount-1].substr(0,songList[songcount-1].find(".wav")));
lfink6 6:6315ecdd0b47 125 LCD.unlock();
lfink6 6:6315ecdd0b47 126 }
lfink6 6:6315ecdd0b47 127
lfink6 6:6315ecdd0b47 128 }
lfink6 6:6315ecdd0b47 129 mentex.unlock();
lfink6 3:8a1fd3450cb5 130 LCD.lock();
lfink6 6:6315ecdd0b47 131 uLCD.locate(2,3);
lfink6 6:6315ecdd0b47 132 uLCD.printf("%s", "^^^");
lfink6 3:8a1fd3450cb5 133 LCD.unlock();
lfink6 6:6315ecdd0b47 134 }
lfink6 7:71e9ee360b90 135 Thread::wait(500);
lfink6 3:8a1fd3450cb5 136 }
lfink6 3:8a1fd3450cb5 137 }
lfink6 3:8a1fd3450cb5 138 void buttonThread()
lfink6 3:8a1fd3450cb5 139 {
lfink6 3:8a1fd3450cb5 140 //add playpause and skip features here
lfink6 3:8a1fd3450cb5 141 Speaker.lock();
lfink6 9:08886cc06a5d 142 if(!menu1)
lfink6 3:8a1fd3450cb5 143 {
lfink6 9:08886cc06a5d 144 playing=!playing;
lfink6 9:08886cc06a5d 145 play=!playing;
lfink6 6:6315ecdd0b47 146 }
lfink6 6:6315ecdd0b47 147 else
lfink6 6:6315ecdd0b47 148 {
lfink6 9:08886cc06a5d 149 playing=!playing;
lfink6 9:08886cc06a5d 150 Thread::wait(500);
lfink6 9:08886cc06a5d 151 playing=!playing;
lfink6 6:6315ecdd0b47 152 }
lfink6 3:8a1fd3450cb5 153 myled=play;
lfink6 3:8a1fd3450cb5 154 Speaker.unlock();
lfink6 7:71e9ee360b90 155 Thread::wait(10);
lfink6 3:8a1fd3450cb5 156 }
lfink6 3:8a1fd3450cb5 157 void skipThread()
lfink6 3:8a1fd3450cb5 158 {
lfink6 3:8a1fd3450cb5 159 //add skip features
lfink6 9:08886cc06a5d 160
lfink6 9:08886cc06a5d 161 Speaker.lock();
lfink6 6:6315ecdd0b47 162 if(currentsong<songcount-1)
lfink6 3:8a1fd3450cb5 163 {
lfink6 3:8a1fd3450cb5 164 currentsong++;
lfink6 3:8a1fd3450cb5 165 }
lfink6 3:8a1fd3450cb5 166 else
lfink6 3:8a1fd3450cb5 167 {
lfink6 3:8a1fd3450cb5 168 currentsong=0;
lfink6 3:8a1fd3450cb5 169 }
lfink6 9:08886cc06a5d 170 if(!menu1)
lfink6 7:71e9ee360b90 171 {
lfink6 7:71e9ee360b90 172 playing=true;
lfink6 7:71e9ee360b90 173 play=true;
lfink6 7:71e9ee360b90 174 }
lfink6 9:08886cc06a5d 175 Speaker.unlock();
lfink6 3:8a1fd3450cb5 176 }
lfink6 3:8a1fd3450cb5 177 void backThread()
lfink6 3:8a1fd3450cb5 178 {
lfink6 9:08886cc06a5d 179 Speaker.lock();
lfink6 3:8a1fd3450cb5 180 if(currentsong!=0)
lfink6 3:8a1fd3450cb5 181 {
lfink6 3:8a1fd3450cb5 182 currentsong--;
lfink6 3:8a1fd3450cb5 183 }
lfink6 3:8a1fd3450cb5 184 else
lfink6 3:8a1fd3450cb5 185 {
lfink6 6:6315ecdd0b47 186 currentsong=songcount-1;
lfink6 3:8a1fd3450cb5 187 }
lfink6 9:08886cc06a5d 188 if(!menu1)
lfink6 7:71e9ee360b90 189 {
lfink6 7:71e9ee360b90 190 playing=true;
lfink6 7:71e9ee360b90 191 play=true;
lfink6 7:71e9ee360b90 192 }
lfink6 9:08886cc06a5d 193 Speaker.unlock();
lfink6 3:8a1fd3450cb5 194 }
lfink6 3:8a1fd3450cb5 195 void menuThread()
lfink6 3:8a1fd3450cb5 196 {
lfink6 3:8a1fd3450cb5 197 menu1=!menu1;
lfink6 3:8a1fd3450cb5 198 }
lfink6 3:8a1fd3450cb5 199 void BlueThread(void const *argument)
lfink6 3:8a1fd3450cb5 200 {
lfink6 3:8a1fd3450cb5 201 //add bluetooth control code here
lfink6 9:08886cc06a5d 202 char bnum =0;
lfink6 9:08886cc06a5d 203 char bhit=0;
lfink6 3:8a1fd3450cb5 204 while(1)
lfink6 3:8a1fd3450cb5 205 {
lfink6 9:08886cc06a5d 206
lfink6 9:08886cc06a5d 207 if(blue.readable()&&blue.writeable())
lfink6 3:8a1fd3450cb5 208 {
lfink6 9:08886cc06a5d 209 if (blue.getc()=='!') {
lfink6 9:08886cc06a5d 210 if (blue.getc()=='B') { //button data
lfink6 9:08886cc06a5d 211 bnum = blue.getc(); //button number
lfink6 9:08886cc06a5d 212 bhit=blue.getc();
lfink6 9:08886cc06a5d 213 if ((bnum>='1')&&(bnum<='4')) //is a number button 1..4
lfink6 9:08886cc06a5d 214 {
lfink6 9:08886cc06a5d 215 LCD.lock();
lfink6 9:08886cc06a5d 216 uLCD.printf("%s", "test lol");
lfink6 9:08886cc06a5d 217 LCD.unlock();
lfink6 9:08886cc06a5d 218 switch (bnum)
lfink6 9:08886cc06a5d 219 {
lfink6 9:08886cc06a5d 220 case '1': //number button 1 //add playpause and skip features here
lfink6 9:08886cc06a5d 221 if (bhit=='1') {
lfink6 9:08886cc06a5d 222 Speaker.lock();
lfink6 9:08886cc06a5d 223 if(play)
lfink6 9:08886cc06a5d 224 {
lfink6 9:08886cc06a5d 225 playing = false;
lfink6 9:08886cc06a5d 226 }
lfink6 9:08886cc06a5d 227 else
lfink6 9:08886cc06a5d 228 {
lfink6 9:08886cc06a5d 229 playing = true;
lfink6 9:08886cc06a5d 230 }
lfink6 9:08886cc06a5d 231 play=!play;
lfink6 9:08886cc06a5d 232 myled=play;
lfink6 9:08886cc06a5d 233 Speaker.unlock();
lfink6 9:08886cc06a5d 234 Thread::wait(10);
lfink6 9:08886cc06a5d 235 }
lfink6 9:08886cc06a5d 236 break;
lfink6 9:08886cc06a5d 237 case '2': //number button 2
lfink6 9:08886cc06a5d 238 if (bhit=='1') {
lfink6 9:08886cc06a5d 239 mentex.lock();
lfink6 9:08886cc06a5d 240 if(currentsong<songcount-1)
lfink6 9:08886cc06a5d 241 {
lfink6 9:08886cc06a5d 242 currentsong++;
lfink6 9:08886cc06a5d 243 }
lfink6 9:08886cc06a5d 244 else
lfink6 9:08886cc06a5d 245 {
lfink6 9:08886cc06a5d 246 currentsong=0;
lfink6 9:08886cc06a5d 247 }
lfink6 9:08886cc06a5d 248 if(!menu)
lfink6 9:08886cc06a5d 249 {
lfink6 9:08886cc06a5d 250 playing=true;
lfink6 9:08886cc06a5d 251 play=true;
lfink6 9:08886cc06a5d 252 }
lfink6 9:08886cc06a5d 253 mentex.unlock();
lfink6 9:08886cc06a5d 254 }
lfink6 9:08886cc06a5d 255 break;
lfink6 9:08886cc06a5d 256 case '3': //number button 3
lfink6 9:08886cc06a5d 257 if (bhit=='1') {
lfink6 9:08886cc06a5d 258 mentex.lock();
lfink6 9:08886cc06a5d 259 if(currentsong!=0)
lfink6 9:08886cc06a5d 260 {
lfink6 9:08886cc06a5d 261 currentsong--;
lfink6 9:08886cc06a5d 262 }
lfink6 9:08886cc06a5d 263 else
lfink6 9:08886cc06a5d 264 {
lfink6 9:08886cc06a5d 265 currentsong=songcount-1;
lfink6 9:08886cc06a5d 266 }
lfink6 9:08886cc06a5d 267 if(!menu)
lfink6 9:08886cc06a5d 268 {
lfink6 9:08886cc06a5d 269 playing=true;
lfink6 9:08886cc06a5d 270 play=true;
lfink6 9:08886cc06a5d 271 }
lfink6 9:08886cc06a5d 272 mentex.unlock();
lfink6 9:08886cc06a5d 273 }
lfink6 9:08886cc06a5d 274 break;
lfink6 9:08886cc06a5d 275 case '4': //number button 4
lfink6 9:08886cc06a5d 276 if (bhit=='1') {
lfink6 9:08886cc06a5d 277 mentex.lock();
lfink6 9:08886cc06a5d 278 menu1=!menu1;
lfink6 9:08886cc06a5d 279 mentex.unlock();
lfink6 9:08886cc06a5d 280 }
lfink6 9:08886cc06a5d 281 break;
lfink6 9:08886cc06a5d 282 default:
lfink6 9:08886cc06a5d 283 break;
lfink6 9:08886cc06a5d 284 }
lfink6 9:08886cc06a5d 285 }
lfink6 9:08886cc06a5d 286
lfink6 9:08886cc06a5d 287 }
lfink6 3:8a1fd3450cb5 288 }
lfink6 9:08886cc06a5d 289 }
lfink6 9:08886cc06a5d 290 Thread::wait(1000);
lfink6 3:8a1fd3450cb5 291 }
lfink6 3:8a1fd3450cb5 292 }
lfink6 3:8a1fd3450cb5 293 void IMUThread(void const *argument)
lfink6 3:8a1fd3450cb5 294 {
lfink6 3:8a1fd3450cb5 295 while(1){
lfink6 3:8a1fd3450cb5 296 avgs.lock();
lfink6 3:8a1fd3450cb5 297 //put imu averaging and next track selection code here if that is selected
lfink6 3:8a1fd3450cb5 298 avgMic = int(((abs((mymicrophone - (0.67/3.3)))*500.0)+avgMic)/2);
lfink6 3:8a1fd3450cb5 299 imu.readAccel();
lfink6 3:8a1fd3450cb5 300 avgIMU=int((((imu.ax+imu.az+imu.ay)/3.0)+avgIMU)/2);
lfink6 3:8a1fd3450cb5 301 avgs.unlock();
lfink6 7:71e9ee360b90 302 Thread::wait(5000);
lfink6 3:8a1fd3450cb5 303 }
lfink6 3:8a1fd3450cb5 304
lfink6 3:8a1fd3450cb5 305 }
lfink6 3:8a1fd3450cb5 306
4180_1 1:5b8e223e983d 307 int main()
4180_1 1:5b8e223e983d 308 {
lfink6 3:8a1fd3450cb5 309 uLCD.cls();
lfink6 3:8a1fd3450cb5 310 uLCD.baudrate(3000000);
lfink6 3:8a1fd3450cb5 311 uLCD.background_color(BLACK);
lfink6 3:8a1fd3450cb5 312 uLCD.text_width(1);
lfink6 3:8a1fd3450cb5 313 uLCD.text_height(1);
lfink6 3:8a1fd3450cb5 314 imu.begin();
lfink6 3:8a1fd3450cb5 315 if (!imu.begin()) {
lfink6 3:8a1fd3450cb5 316 //set fail flag for imu here
lfink6 3:8a1fd3450cb5 317 }
lfink6 3:8a1fd3450cb5 318 imu.calibrate();
lfink6 3:8a1fd3450cb5 319 blue.baud(9600);
lfink6 3:8a1fd3450cb5 320
lfink6 3:8a1fd3450cb5 321 DIR *dp;
lfink6 3:8a1fd3450cb5 322 struct dirent *dirp;
lfink6 3:8a1fd3450cb5 323 dp = opendir("/sd/myMusic");
lfink6 3:8a1fd3450cb5 324 songcount = 0;
lfink6 3:8a1fd3450cb5 325 if(dp !=NULL)
lfink6 3:8a1fd3450cb5 326 {
lfink6 3:8a1fd3450cb5 327 while ((dirp = readdir(dp)) != NULL) {
lfink6 3:8a1fd3450cb5 328 songList.push_back(string(dirp->d_name));
lfink6 3:8a1fd3450cb5 329 //uLCD.printf("\r%s\r\n", string(dirp->d_name));
lfink6 3:8a1fd3450cb5 330 songcount++;
lfink6 3:8a1fd3450cb5 331 }
lfink6 3:8a1fd3450cb5 332 }
lfink6 3:8a1fd3450cb5 333 playpause.mode(PullUp);
lfink6 3:8a1fd3450cb5 334 playpause.attach_deasserted(&buttonThread);
lfink6 3:8a1fd3450cb5 335 playpause.setSampleFrequency();
lfink6 3:8a1fd3450cb5 336
lfink6 3:8a1fd3450cb5 337 menu.mode(PullUp);
lfink6 3:8a1fd3450cb5 338 menu.attach_deasserted(&menuThread);
lfink6 3:8a1fd3450cb5 339 menu.setSampleFrequency();
lfink6 3:8a1fd3450cb5 340 skip.mode(PullUp);
lfink6 3:8a1fd3450cb5 341 skip.attach_deasserted(&skipThread);
lfink6 3:8a1fd3450cb5 342 skip.setSampleFrequency();
lfink6 3:8a1fd3450cb5 343 back.mode(PullUp);
lfink6 3:8a1fd3450cb5 344 back.attach_deasserted(&backThread);
lfink6 3:8a1fd3450cb5 345 back.setSampleFrequency();
lfink6 3:8a1fd3450cb5 346 //LCD, Player, button Interrupt, bluetooth, imu+mic
lfink6 3:8a1fd3450cb5 347 Thread thread1(LCDThread);
lfink6 3:8a1fd3450cb5 348 Thread thread4(BlueThread);
lfink6 3:8a1fd3450cb5 349 Thread thread5(IMUThread);
lfink6 7:71e9ee360b90 350 while(1){
4180_1 1:5b8e223e983d 351 FILE *wave_file;
4180_1 2:c2afd0c426af 352 Thread::wait(1000);
lfink6 6:6315ecdd0b47 353 mentex.lock();
lfink6 6:6315ecdd0b47 354 string selectedSong= "/sd/myMusic/" + songList[currentsong];
lfink6 6:6315ecdd0b47 355 mentex.unlock();
lfink6 5:afdd7d5b432a 356 const char* song = selectedSong.c_str();
lfink6 5:afdd7d5b432a 357 wave_file=fopen(song,"r");
lfink6 6:6315ecdd0b47 358 if(wave_file==NULL) uLCD.printf("file open error!\n\n\r");
4180_1 1:5b8e223e983d 359 waver.play(wave_file);
4180_1 1:5b8e223e983d 360 fclose(wave_file);
lfink6 3:8a1fd3450cb5 361 }
4180_1 1:5b8e223e983d 362 }