Demo program of the wave_player library working with mbed-os v5. For ECE 4180 Lab 3 Extra Extra credit.

Dependencies:   wave_player

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "rtos.h"
00003 #include "SDBlockDevice.h"
00004 #include "FATFileSystem.h"
00005 #include "wave_player.h"
00006 
00007 // LED Components
00008 PwmOut led(LED1);
00009 
00010 // SD File System Components
00011 SDBlockDevice   sd(p5, p6, p7, p8);
00012 FATFileSystem   fs("sd");
00013 
00014 // Audio Components
00015 AnalogOut       dac(p18);
00016 PwmOut          speaker(p25);
00017 wave_player     wav(&dac, &speaker);
00018 
00019 Thread led_thread, audio_thread;
00020 
00021 void led_siren() {
00022     const int build_time = 100;
00023     while (1) {
00024         //LED warm up effect using PWM
00025         for(int i=0; i<build_time; i++) {
00026             led = i / (float) build_time;
00027             ThisThread::sleep_for(1000.0*0.02);
00028         }
00029         //LED at full brightness level
00030         led = 1.0;
00031         ThisThread::sleep_for(1000.0*0.25);
00032         //LED cool down effect using PWM
00033         for(int i=build_time-1; i>0; i--) {
00034             led = i/ (float) build_time;
00035             ThisThread::sleep_for(1000.0*0.02);
00036         }
00037         //LED off
00038         led = 0.0;
00039         ThisThread::sleep_for(1000.0*1.5);
00040     }
00041 }
00042 
00043 void play_audio() {
00044     FILE * wav_file;
00045     speaker.period(1.0/1000000.0);
00046     while (1) {
00047         sd.init();
00048         fs.mount(&sd);
00049         wav_file = fopen("/sd/siren.wav", "r");
00050         wav.play(wav_file);
00051         fclose(wav_file);
00052         sd.deinit();
00053         fs.unmount();
00054     }
00055 }
00056 
00057 // main() runs in its own thread in the OS
00058 int main()
00059 {   
00060     led_thread.start(led_siren);
00061     audio_thread.start(play_audio);
00062     while (true) {
00063         ThisThread::sleep_for(500);
00064     }
00065 }