inital commit

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

Committer:
lfink6
Date:
Sun Dec 12 20:08:18 2021 +0000
Revision:
6:6315ecdd0b47
Parent:
5:afdd7d5b432a
Child:
7:71e9ee360b90
ver 3;

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 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);
lfink6 6:6315ecdd0b47 32 bool playing = false;
lfink6 3:8a1fd3450cb5 33 bool play = 0;
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 6:6315ecdd0b47 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 3:8a1fd3450cb5 135 Thread::wait(1000);
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 3:8a1fd3450cb5 142 if(play)
lfink6 3:8a1fd3450cb5 143 {
lfink6 6:6315ecdd0b47 144 playing = false;
lfink6 6:6315ecdd0b47 145 }
lfink6 6:6315ecdd0b47 146 else
lfink6 6:6315ecdd0b47 147 {
lfink6 6:6315ecdd0b47 148 playing = true;
lfink6 6:6315ecdd0b47 149 }
lfink6 3:8a1fd3450cb5 150 play=!play;
lfink6 3:8a1fd3450cb5 151 myled=play;
lfink6 3:8a1fd3450cb5 152 Speaker.unlock();
lfink6 3:8a1fd3450cb5 153 Thread::wait(100);
lfink6 3:8a1fd3450cb5 154 }
lfink6 3:8a1fd3450cb5 155 void skipThread()
lfink6 3:8a1fd3450cb5 156 {
lfink6 3:8a1fd3450cb5 157 //add skip features
lfink6 6:6315ecdd0b47 158 if(currentsong<songcount-1)
lfink6 3:8a1fd3450cb5 159 {
lfink6 3:8a1fd3450cb5 160 currentsong++;
lfink6 3:8a1fd3450cb5 161 }
lfink6 3:8a1fd3450cb5 162 else
lfink6 3:8a1fd3450cb5 163 {
lfink6 3:8a1fd3450cb5 164 currentsong=0;
lfink6 3:8a1fd3450cb5 165 }
lfink6 3:8a1fd3450cb5 166 }
lfink6 3:8a1fd3450cb5 167 void backThread()
lfink6 3:8a1fd3450cb5 168 {
lfink6 3:8a1fd3450cb5 169 if(currentsong!=0)
lfink6 3:8a1fd3450cb5 170 {
lfink6 3:8a1fd3450cb5 171 currentsong--;
lfink6 3:8a1fd3450cb5 172 }
lfink6 3:8a1fd3450cb5 173 else
lfink6 3:8a1fd3450cb5 174 {
lfink6 6:6315ecdd0b47 175 currentsong=songcount-1;
lfink6 3:8a1fd3450cb5 176 }
lfink6 3:8a1fd3450cb5 177 }
lfink6 3:8a1fd3450cb5 178 void menuThread()
lfink6 3:8a1fd3450cb5 179 {
lfink6 3:8a1fd3450cb5 180 menu1=!menu1;
lfink6 3:8a1fd3450cb5 181 }
lfink6 3:8a1fd3450cb5 182 void BlueThread(void const *argument)
lfink6 3:8a1fd3450cb5 183 {
lfink6 3:8a1fd3450cb5 184 //add bluetooth control code here
lfink6 3:8a1fd3450cb5 185 while(1)
lfink6 3:8a1fd3450cb5 186 {
lfink6 3:8a1fd3450cb5 187 if(blue.readable())
lfink6 3:8a1fd3450cb5 188 {
lfink6 3:8a1fd3450cb5 189
lfink6 3:8a1fd3450cb5 190 }
lfink6 3:8a1fd3450cb5 191 }
lfink6 3:8a1fd3450cb5 192 }
lfink6 3:8a1fd3450cb5 193 void IMUThread(void const *argument)
lfink6 3:8a1fd3450cb5 194 {
lfink6 3:8a1fd3450cb5 195 while(1){
lfink6 3:8a1fd3450cb5 196 avgs.lock();
lfink6 3:8a1fd3450cb5 197 //put imu averaging and next track selection code here if that is selected
lfink6 3:8a1fd3450cb5 198 avgMic = int(((abs((mymicrophone - (0.67/3.3)))*500.0)+avgMic)/2);
lfink6 3:8a1fd3450cb5 199 imu.readAccel();
lfink6 3:8a1fd3450cb5 200 avgIMU=int((((imu.ax+imu.az+imu.ay)/3.0)+avgIMU)/2);
lfink6 3:8a1fd3450cb5 201 avgs.unlock();
lfink6 3:8a1fd3450cb5 202 Thread::wait(2500);
lfink6 3:8a1fd3450cb5 203 }
lfink6 3:8a1fd3450cb5 204
lfink6 3:8a1fd3450cb5 205 }
lfink6 3:8a1fd3450cb5 206
4180_1 1:5b8e223e983d 207 int main()
4180_1 1:5b8e223e983d 208 {
lfink6 3:8a1fd3450cb5 209 uLCD.cls();
lfink6 3:8a1fd3450cb5 210 uLCD.baudrate(3000000);
lfink6 3:8a1fd3450cb5 211 uLCD.background_color(BLACK);
lfink6 3:8a1fd3450cb5 212 uLCD.text_width(1);
lfink6 3:8a1fd3450cb5 213 uLCD.text_height(1);
lfink6 3:8a1fd3450cb5 214 imu.begin();
lfink6 3:8a1fd3450cb5 215 if (!imu.begin()) {
lfink6 3:8a1fd3450cb5 216 //set fail flag for imu here
lfink6 3:8a1fd3450cb5 217 }
lfink6 3:8a1fd3450cb5 218 imu.calibrate();
lfink6 3:8a1fd3450cb5 219 blue.baud(9600);
lfink6 3:8a1fd3450cb5 220
lfink6 3:8a1fd3450cb5 221 DIR *dp;
lfink6 3:8a1fd3450cb5 222 struct dirent *dirp;
lfink6 3:8a1fd3450cb5 223 dp = opendir("/sd/myMusic");
lfink6 3:8a1fd3450cb5 224 songcount = 0;
lfink6 3:8a1fd3450cb5 225 if(dp !=NULL)
lfink6 3:8a1fd3450cb5 226 {
lfink6 3:8a1fd3450cb5 227 while ((dirp = readdir(dp)) != NULL) {
lfink6 3:8a1fd3450cb5 228 songList.push_back(string(dirp->d_name));
lfink6 3:8a1fd3450cb5 229 //uLCD.printf("\r%s\r\n", string(dirp->d_name));
lfink6 3:8a1fd3450cb5 230 songcount++;
lfink6 3:8a1fd3450cb5 231 }
lfink6 3:8a1fd3450cb5 232 }
lfink6 3:8a1fd3450cb5 233 playpause.mode(PullUp);
lfink6 3:8a1fd3450cb5 234 playpause.attach_deasserted(&buttonThread);
lfink6 3:8a1fd3450cb5 235 playpause.setSampleFrequency();
lfink6 3:8a1fd3450cb5 236
lfink6 3:8a1fd3450cb5 237 menu.mode(PullUp);
lfink6 3:8a1fd3450cb5 238 menu.attach_deasserted(&menuThread);
lfink6 3:8a1fd3450cb5 239 menu.setSampleFrequency();
lfink6 3:8a1fd3450cb5 240 skip.mode(PullUp);
lfink6 3:8a1fd3450cb5 241 skip.attach_deasserted(&skipThread);
lfink6 3:8a1fd3450cb5 242 skip.setSampleFrequency();
lfink6 3:8a1fd3450cb5 243 back.mode(PullUp);
lfink6 3:8a1fd3450cb5 244 back.attach_deasserted(&backThread);
lfink6 3:8a1fd3450cb5 245 back.setSampleFrequency();
lfink6 3:8a1fd3450cb5 246 //LCD, Player, button Interrupt, bluetooth, imu+mic
lfink6 3:8a1fd3450cb5 247 Thread thread1(LCDThread);
lfink6 3:8a1fd3450cb5 248 Thread thread4(BlueThread);
lfink6 3:8a1fd3450cb5 249 Thread thread5(IMUThread);
lfink6 6:6315ecdd0b47 250 while(1)
lfink6 6:6315ecdd0b47 251 {
lfink6 6:6315ecdd0b47 252 while(playing==true){
4180_1 1:5b8e223e983d 253 FILE *wave_file;
4180_1 2:c2afd0c426af 254 Thread::wait(1000);
lfink6 6:6315ecdd0b47 255 mentex.lock();
lfink6 6:6315ecdd0b47 256 string selectedSong= "/sd/myMusic/" + songList[currentsong];
lfink6 6:6315ecdd0b47 257 mentex.unlock();
lfink6 5:afdd7d5b432a 258 const char* song = selectedSong.c_str();
lfink6 6:6315ecdd0b47 259 uLCD.printf("%s",song);
lfink6 5:afdd7d5b432a 260 wave_file=fopen(song,"r");
lfink6 6:6315ecdd0b47 261 if(wave_file==NULL) uLCD.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 }
lfink6 6:6315ecdd0b47 265 }
4180_1 1:5b8e223e983d 266 }