![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
ECE 4180 Final Project MP3 player code
Dependencies: mbed mbed-rtos wave_player_appbd 4DGL-uLCD-SE SDFileSystem PinDetect
main.cpp@2:bb408ca8fddb, 2021-12-10 (annotated)
- Committer:
- lfink6
- Date:
- Fri Dec 10 17:57:49 2021 +0000
- Revision:
- 2:bb408ca8fddb
- Parent:
- 1:481169ca05e0
test commit;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lfink6 | 0:57a32b7102e8 | 1 | #include "mbed.h" |
lfink6 | 0:57a32b7102e8 | 2 | #include "rtos.h" |
lfink6 | 0:57a32b7102e8 | 3 | #include "SDFileSystem.h" |
lfink6 | 0:57a32b7102e8 | 4 | #include "uLCD_4DGL.h" |
lfink6 | 0:57a32b7102e8 | 5 | #include "wave_player.h" |
lfink6 | 0:57a32b7102e8 | 6 | #include "LSM9DS1.h" |
lfink6 | 1:481169ca05e0 | 7 | #include "PinDetect.h" |
lfink6 | 0:57a32b7102e8 | 8 | |
lfink6 | 0:57a32b7102e8 | 9 | DigitalOut myled(LED1); |
lfink6 | 0:57a32b7102e8 | 10 | Serial blue(p28,p27); |
lfink6 | 1:481169ca05e0 | 11 | |
lfink6 | 0:57a32b7102e8 | 12 | uLCD_4DGL uLCD(p13,p14,p11); |
lfink6 | 1:481169ca05e0 | 13 | |
lfink6 | 0:57a32b7102e8 | 14 | SDFileSystem sd(p5, p6, p7, p8, "sd"); |
lfink6 | 1:481169ca05e0 | 15 | |
lfink6 | 0:57a32b7102e8 | 16 | LSM9DS1 imu(p9, p10, 0xD6, 0x3C); |
lfink6 | 1:481169ca05e0 | 17 | Serial pc(USBTX, USBRX); |
lfink6 | 1:481169ca05e0 | 18 | |
lfink6 | 1:481169ca05e0 | 19 | PinDetect playpause(p23); |
lfink6 | 1:481169ca05e0 | 20 | Mutex LCD; |
lfink6 | 1:481169ca05e0 | 21 | Mutex Speaker; |
lfink6 | 1:481169ca05e0 | 22 | Mutex avgs; |
lfink6 | 1:481169ca05e0 | 23 | bool play = 0; |
lfink6 | 1:481169ca05e0 | 24 | |
lfink6 | 1:481169ca05e0 | 25 | |
lfink6 | 1:481169ca05e0 | 26 | AnalogOut DACout(p18); |
lfink6 | 1:481169ca05e0 | 27 | PwmOut PWMout(p26); |
lfink6 | 1:481169ca05e0 | 28 | wave_player waver(&DACout,&PWMout); |
lfink6 | 1:481169ca05e0 | 29 | |
lfink6 | 1:481169ca05e0 | 30 | int avgIMU; |
lfink6 | 1:481169ca05e0 | 31 | int avgMic; |
lfink6 | 1:481169ca05e0 | 32 | |
lfink6 | 0:57a32b7102e8 | 33 | |
lfink6 | 0:57a32b7102e8 | 34 | class microphone |
lfink6 | 0:57a32b7102e8 | 35 | { |
lfink6 | 0:57a32b7102e8 | 36 | public : |
lfink6 | 0:57a32b7102e8 | 37 | microphone(PinName pin); |
lfink6 | 0:57a32b7102e8 | 38 | float read(); |
lfink6 | 0:57a32b7102e8 | 39 | operator float (); |
lfink6 | 0:57a32b7102e8 | 40 | private : |
lfink6 | 0:57a32b7102e8 | 41 | AnalogIn _pin; |
lfink6 | 0:57a32b7102e8 | 42 | }; |
lfink6 | 0:57a32b7102e8 | 43 | microphone::microphone (PinName pin): |
lfink6 | 0:57a32b7102e8 | 44 | _pin(pin) |
lfink6 | 0:57a32b7102e8 | 45 | { |
lfink6 | 0:57a32b7102e8 | 46 | } |
lfink6 | 0:57a32b7102e8 | 47 | float microphone::read() |
lfink6 | 0:57a32b7102e8 | 48 | { |
lfink6 | 0:57a32b7102e8 | 49 | return _pin.read(); |
lfink6 | 0:57a32b7102e8 | 50 | } |
lfink6 | 0:57a32b7102e8 | 51 | inline microphone::operator float () |
lfink6 | 0:57a32b7102e8 | 52 | { |
lfink6 | 0:57a32b7102e8 | 53 | return _pin.read(); |
lfink6 | 0:57a32b7102e8 | 54 | } |
lfink6 | 0:57a32b7102e8 | 55 | |
lfink6 | 0:57a32b7102e8 | 56 | microphone mymicrophone(p16); |
lfink6 | 2:bb408ca8fddb | 57 | void TrackSelect() |
lfink6 | 2:bb408ca8fddb | 58 | { |
lfink6 | 2:bb408ca8fddb | 59 | //Add code here to use the avg to set the next track, or set the next track based on the menu. |
lfink6 | 2:bb408ca8fddb | 60 | } |
lfink6 | 2:bb408ca8fddb | 61 | |
lfink6 | 1:481169ca05e0 | 62 | void LCDThread(void const *argument) |
lfink6 | 1:481169ca05e0 | 63 | { |
lfink6 | 1:481169ca05e0 | 64 | while(1){ |
lfink6 | 1:481169ca05e0 | 65 | LCD.lock(); |
lfink6 | 1:481169ca05e0 | 66 | uLCD.locate(1,1); |
lfink6 | 1:481169ca05e0 | 67 | uLCD.printf("Put Song Name Here"); |
lfink6 | 1:481169ca05e0 | 68 | uLCD.printf("%4D", avgIMU); |
lfink6 | 1:481169ca05e0 | 69 | uLCD.printf("%4D", avgMic); |
lfink6 | 1:481169ca05e0 | 70 | if(!play) |
lfink6 | 1:481169ca05e0 | 71 | { |
lfink6 | 1:481169ca05e0 | 72 | //play |
lfink6 | 1:481169ca05e0 | 73 | |
lfink6 | 1:481169ca05e0 | 74 | uLCD.filled_rectangle(0,118,280,40,BLACK); |
lfink6 | 1:481169ca05e0 | 75 | uLCD.triangle(120, 100, 40, 40, 10, 100, 0x0000FF); |
lfink6 | 1:481169ca05e0 | 76 | } |
lfink6 | 1:481169ca05e0 | 77 | else |
lfink6 | 1:481169ca05e0 | 78 | { |
lfink6 | 1:481169ca05e0 | 79 | //pause |
lfink6 | 1:481169ca05e0 | 80 | uLCD.filled_rectangle(0,118,110,40,WHITE); |
lfink6 | 1:481169ca05e0 | 81 | uLCD.filled_rectangle(50,118,100,40,BLACK); |
lfink6 | 1:481169ca05e0 | 82 | uLCD.filled_rectangle(180,118,280,40,WHITE); |
lfink6 | 1:481169ca05e0 | 83 | } |
lfink6 | 1:481169ca05e0 | 84 | LCD.unlock(); |
lfink6 | 1:481169ca05e0 | 85 | Thread::wait(100); |
lfink6 | 1:481169ca05e0 | 86 | } |
lfink6 | 1:481169ca05e0 | 87 | |
lfink6 | 1:481169ca05e0 | 88 | } |
lfink6 | 1:481169ca05e0 | 89 | void PlayerThread(void const *argument) |
lfink6 | 1:481169ca05e0 | 90 | { |
lfink6 | 1:481169ca05e0 | 91 | Speaker.lock(); |
lfink6 | 1:481169ca05e0 | 92 | FILE *wave_file; |
lfink6 | 1:481169ca05e0 | 93 | wave_file=fopen("/sd/sample.wav","r"); |
lfink6 | 1:481169ca05e0 | 94 | waver.play(wave_file); |
lfink6 | 1:481169ca05e0 | 95 | fclose(wave_file); |
lfink6 | 1:481169ca05e0 | 96 | Speaker.unlock(); |
lfink6 | 1:481169ca05e0 | 97 | Thread::wait(1000); |
lfink6 | 1:481169ca05e0 | 98 | } |
lfink6 | 1:481169ca05e0 | 99 | void buttonThread() |
lfink6 | 1:481169ca05e0 | 100 | { |
lfink6 | 1:481169ca05e0 | 101 | //add playpause and skip features here |
lfink6 | 1:481169ca05e0 | 102 | Speaker.lock(); |
lfink6 | 1:481169ca05e0 | 103 | play=!play; |
lfink6 | 1:481169ca05e0 | 104 | myled=play; |
lfink6 | 1:481169ca05e0 | 105 | Speaker.unlock(); |
lfink6 | 1:481169ca05e0 | 106 | Thread::wait(100); |
lfink6 | 1:481169ca05e0 | 107 | } |
lfink6 | 1:481169ca05e0 | 108 | void BlueThread(void const *argument) |
lfink6 | 1:481169ca05e0 | 109 | { |
lfink6 | 1:481169ca05e0 | 110 | //add bluetooth control code here |
lfink6 | 1:481169ca05e0 | 111 | while(1) |
lfink6 | 1:481169ca05e0 | 112 | { |
lfink6 | 1:481169ca05e0 | 113 | if(blue.readable()) |
lfink6 | 1:481169ca05e0 | 114 | { |
lfink6 | 1:481169ca05e0 | 115 | |
lfink6 | 1:481169ca05e0 | 116 | } |
lfink6 | 1:481169ca05e0 | 117 | } |
lfink6 | 1:481169ca05e0 | 118 | } |
lfink6 | 1:481169ca05e0 | 119 | void IMUThread(void const *argument) |
lfink6 | 1:481169ca05e0 | 120 | { |
lfink6 | 1:481169ca05e0 | 121 | while(1){ |
lfink6 | 1:481169ca05e0 | 122 | avgs.lock(); |
lfink6 | 1:481169ca05e0 | 123 | //put imu averaging and next track selection code here if that is selected |
lfink6 | 1:481169ca05e0 | 124 | avgMic = int(((abs((mymicrophone - (0.67/3.3)))*500.0)+avgMic)/2); |
lfink6 | 1:481169ca05e0 | 125 | imu.readAccel(); |
lfink6 | 1:481169ca05e0 | 126 | avgIMU=int((((imu.ax+imu.az+imu.ay)/3.0)+avgIMU)/2); |
lfink6 | 1:481169ca05e0 | 127 | avgs.unlock(); |
lfink6 | 1:481169ca05e0 | 128 | Thread::wait(250); |
lfink6 | 1:481169ca05e0 | 129 | } |
lfink6 | 1:481169ca05e0 | 130 | |
lfink6 | 1:481169ca05e0 | 131 | } |
lfink6 | 0:57a32b7102e8 | 132 | |
lfink6 | 0:57a32b7102e8 | 133 | int main() { |
lfink6 | 1:481169ca05e0 | 134 | uLCD.cls(); |
lfink6 | 1:481169ca05e0 | 135 | uLCD.baudrate(3000000); |
lfink6 | 1:481169ca05e0 | 136 | uLCD.background_color(BLACK); |
lfink6 | 1:481169ca05e0 | 137 | uLCD.text_width(1); |
lfink6 | 1:481169ca05e0 | 138 | uLCD.text_height(1); |
lfink6 | 0:57a32b7102e8 | 139 | imu.begin(); |
lfink6 | 0:57a32b7102e8 | 140 | if (!imu.begin()) { |
lfink6 | 0:57a32b7102e8 | 141 | //set fail flag for imu here |
lfink6 | 0:57a32b7102e8 | 142 | } |
lfink6 | 0:57a32b7102e8 | 143 | imu.calibrate(); |
lfink6 | 1:481169ca05e0 | 144 | playpause.mode(PullUp); |
lfink6 | 1:481169ca05e0 | 145 | playpause.attach_deasserted(&buttonThread); |
lfink6 | 1:481169ca05e0 | 146 | playpause.setSampleFrequency(); |
lfink6 | 1:481169ca05e0 | 147 | //LCD, Player, button Interrupt, bluetooth, imu+mic |
lfink6 | 1:481169ca05e0 | 148 | Thread thread1(LCDThread); |
lfink6 | 1:481169ca05e0 | 149 | Thread thread2(PlayerThread); |
lfink6 | 1:481169ca05e0 | 150 | Thread thread4(BlueThread); |
lfink6 | 1:481169ca05e0 | 151 | Thread thread5(IMUThread); |
lfink6 | 0:57a32b7102e8 | 152 | while(1) { |
lfink6 | 1:481169ca05e0 | 153 | //add some code to show the menu of songs if the mbed pushbutton is pushed here |
lfink6 | 0:57a32b7102e8 | 154 | |
lfink6 | 0:57a32b7102e8 | 155 | } |
lfink6 | 0:57a32b7102e8 | 156 | } |