![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Final project
Dependencies: mbed wave_player mbed-rtos 4DGL-uLCD-SE SDFileSystem 4180_Lab3_rtos_basic
Diff: main.cpp
- Revision:
- 10:a644e4e16cf4
- Parent:
- 9:1b37fd4bf5ea
--- a/main.cpp Mon Mar 01 22:25:07 2021 +0000 +++ b/main.cpp Mon May 03 23:01:08 2021 +0000 @@ -1,105 +1,491 @@ -#include "mbed.h" -#include "rtos.h" -#include "uLCD_4DGL.h" -#include "SDFileSystem.h" -#include "wave_player.h" -#include "PlatformMutex.h" +/* +-------------------AUTHORS----------------------- +\\\\\\\\\\\\\\\\\\\\\\/////////////////////////// + +JACQUES CRAWFORD +ALTON STOVALL +JAYLA WILLIAMS +MILES BARROS + +-----------------DEPENDECIES--------------------- +\\\\\\\\\\\\\\\\\\\\\\/////////////////////////// + +---HARDWARE--- + + - TEMPERATURE SENSOR: TMP36 + - LIDAR SENSOR: VL53L0X + - MICRPHONE: SPW2430 + - CLASS D AUIO AMP: TPA2005D1 + - SPEAKER: NaN + - SHIFTBRITE: COM-10075 + - SD CARD READER: NaN + - PUSHBUTTON: NaN + - BARREL JACK (5V): NaN + +---I/O PIN ASSIGMENTS--- +TMP36 +-------------------------------- +mbed | TMP36 +-------------------------------- +GND | GND +Vout(3.3V) | Vs +p19 | Vout + + +VL53L0X +---------------- +Mbed | VL53L0X +---------------- +3.3V | VIN +GND | GND +P28 | SDA +P27 | SCL +P26 | SHDN + +SPW2430 +--------------------------------- +mbed | microphone +--------------------------------- +gnd | gnd +VU(+5VDC) | Vin +p16 | (AnalogIn) DC -uLCD_4DGL myLCD(p28, p27, p30); -Mutex lcd_mutex; -RawSerial pc(USBTX, USBRX); -Serial bluemod(p13,p14); +TPA2005D1 +-------------------------------------------------------- +mbed | TPA2005D1 | Speaker +-------------------------------------------------------- +gnd | pwr - (gnd), in - | +Vout (3.3V) or 5V | pwr + | +p18 (A/D) | in + | + | out + | + + | out - | - + +SHIFTBRITE +-------------------- +mbed | ShiftBrite +-------------------- +gnd | Gnd +p11 | DI +p15 | LI +p20 | EI +p13 | CI +VU(5V)| V+* + +SD CARD READER +----------------------------------- +mbed | sd card reader +----------------------------------- +p8 | CS +p5 | DI +VOUT | VCC +p7 | SCK +gnd | GND +p6 | DO +NC | CD -PwmOut red(p23); -PwmOut green(p22); -PwmOut blue(p21); +PUHBUTTON +--------------------------- +mbed | pushbutton +--------------------------- +gnd | - +p22 | + + +*/ + +//required libraries +#include "mbed.h" +#include <stdio.h> +#include "rtos.h" +#include "VL53L0X.h" +#include "wave_player.h" +#include "SDFileSystem.h" +#include "DebounceIn.h" +#include <string> + +using namespace std; + +//ShiftBrite pin assignments +DigitalOut latch(p15); +DigitalOut enable(p20); +SPI SB_LED(p11, p12, p13); + +//onboard led +DigitalOut myled(LED1); + +//pushbutton pin assignments +InterruptIn pb(p22); + +//sdcard pin assignments SDFileSystem sd(p5, p6, p7, p8, "sd"); +//speaker pin assignment AnalogOut Speaker(p18); + +//wave player pin assignments and instantiation wave_player waver(&Speaker); bool PLAY = true; bool *PlayStopPtr = &PLAY; +FILE *wave_file; -// Thread 1 -// print counter into first line and wait for 1 s -void thread1(void const *args) + +//pc terminal communication +Serial pc(USBTX,USBRX); +DigitalOut shdn(p26); + +//thread mutexes +Mutex waver_mutex; +Mutex led_mutex; + +// Lidar sensor +I2C i2c(p28, p27); +VL53L0X lidar(&i2c); + +// connect p19(WIZwiki-W7500) to Vout(Temp36) +AnalogIn temp(p19); + +//bluetooth serial connection +Serial blue(p9,p10); + + +//boolean trigger variables (intuitively named) +bool soundAlarm = false; +bool tempAlarm = false; +bool distAlarm = false; +bool ledStatus = false; +bool armed = false; + +bool distNotified = false; +bool soundNotified = false; +bool tempNotified = false; + + +/////////////////////////////////// +// Microphone class for SPW2430 +/////////////////////////////////// +class microphone +{ +public : + microphone(PinName pin); + float read(); + operator float (); +private : + AnalogIn _pin; +}; +microphone::microphone (PinName pin): + _pin(pin) +{ +} +float microphone::read() +{ + return _pin.read(); +} +inline microphone::operator float () +{ + return _pin.read(); +} + + +// microphone pin assignment and object creation +microphone mymicrophone(p16); + +//shiftbrite RBG function. this function controls the color of the led based on the alarm trigger +void RGB_LED(int red, int green, int blue) +{ + unsigned int low_color=0; + unsigned int high_color=0; + high_color=(blue<<4)|((red&0x3C0)>>6); + low_color=(((red&0x3F)<<10)|(green)); + SB_LED.write(high_color); + SB_LED.write(low_color); + latch=1; + latch=0; +} + +//Reset all parameter i.e. reset alarm device +void reset(void) { - int i; - while(true) { // thread loop - lcd_mutex.lock(); - myLCD.locate(0,0); - myLCD.printf("Weeeee Woooo"); - lcd_mutex.unlock(); - i++; + soundAlarm = false; + tempAlarm = false; + distAlarm = false; + ledStatus = false; + armed = false; + + bool distNotified = false; + bool soundNotified = false; + bool tempNotified = false; + + pc.printf("LED STATUS: %d\r\nTEMP STATUS: %d\r\nDIST STATUS: %d\r\nLED STATUS: %d\r\n", soundAlarm, tempAlarm, distAlarm, ledStatus); +} + +//Handle motion detected flag +bool distanceTrigger(uint16_t dist) +{ + if (dist < 400 && dist != 20 && armed) + { + distAlarm = true; + } + + if (distAlarm && armed) + { + pc.printf("DIST_EVENT_STATUS: %d\r\n", distAlarm); // 1 for true, 0 for false + pc.printf("Distance: %d mm\r\n\n", dist); + + if (distNotified == false) + { + blue.printf("DISTANCE ALARM HAS BEEN TRIGGERED! Type \"stop\" to disarm.\n"); + } + distNotified = true; + + } + return distAlarm; +} + +//Handle temperature warning flag +bool tempTrigger(float temp) +{ + pc.printf("TEMP_EVENT_STATUS: %d\r\n", tempAlarm);// 1 for true, 0 for false + pc.printf("Temperature: %5.2f F \r\n\n", temp); + if (temp > 60.0 && armed) + { + tempAlarm = true; + } + + if (tempAlarm && armed) + { + pc.printf("TEMP_EVENT_STATUS: %d\r\n", tempAlarm);// 1 for true, 0 for false + pc.printf("Temperature: %5.2f F \r\n\n", temp); + if (tempNotified == false) + { + blue.printf("TEMPERATURE ALARM HAS BEEN TRIGGERED! Type \"stop\" to disarm.\n"); + } + tempNotified = true; + } + return tempAlarm; +} + +//Handle sound detected flag +bool soundTrigger(int snd) +{ + if (snd > 10 && armed) + { + soundAlarm = true; + } + + if (soundAlarm && armed) + { + pc.printf("SOUND_EVENT_STATUS: %d\r\n", soundAlarm);// 1 for true, 0 for false + pc.printf("Sound level: %d\r\n\n", snd); + if (soundNotified == false) { + blue.printf("SOUND ALARM HAS BEEN TRIGGERED! Type \"stop\" to disarm.\n"); + } + soundNotified = true; + + + } + + return soundAlarm; + +} + +//Thread for continuos microphone reading and flag setting --> (soundTrigger(sound)) +void SOUND_THREAD() +{ + float sound; + + while (1) + { + sound = int(abs((mymicrophone - (0.67/3.3)))*500.0); + soundTrigger(sound); + Thread::wait(1000); + + } +} + +//Thread for continuos temperature reading and flag setting --> (tempTrigger(distance)) +void TEMP_THREAD() +{ + while(1) + { + float V = temp.read() * 3.3; // connect Vs(Temp36) to 3.3V(WIZwiki-W7500) + float tempC = (V - 0.5) * 100; // calculate temperature C + float tempF = (tempC * 9 / 5) + 32.0; // calculate temperature F + tempTrigger(tempF); Thread::wait(1000); } } -// Thread 2 -// print counter into third line and wait for 0,5s -void thread2(void const *args) +//Thread for playing wave file based on respective alarm triggers +void WAVE_THREAD() { - int k; - while(true) { // thread loop - lcd_mutex.lock(); - myLCD.locate(0,20); - myLCD.printf("Pull Over."); - lcd_mutex.unlock(); - k++; - Thread::wait(500); // wait 0.5s + while (1) + { + if (soundAlarm && armed) + { + waver_mutex.lock(); + wave_file=fopen("/sd/sound_new_speed.wav","r"); + waver.play(wave_file, PlayStopPtr); + fclose(wave_file); + waver_mutex.unlock(); + Thread::wait(500); + } + if (distAlarm && armed) + { + waver_mutex.lock(); + wave_file=fopen("/sd/motion_new_speed.wav","r"); + waver.play(wave_file, PlayStopPtr); + fclose(wave_file); + waver_mutex.unlock(); + Thread::wait(500); + } + if (tempAlarm && armed) + { + waver_mutex.lock(); + wave_file=fopen("/sd/temp_new_speed.wav","r"); + waver.play(wave_file, PlayStopPtr); + fclose(wave_file); + waver_mutex.unlock(); + Thread::wait(500); + } + Thread::wait(1000); + } +} + +//Thread for checking any and all bluetooth commands +void BLUETOOTH_THREAD() +{ + char request[10]; + string alarm_state = "disarmed"; + + while(1) { + //check for user input to bluetooth + if(blue.readable()) + { + blue.scanf("%s", request); + if (strcmp(request, "stop") == 0) + { + alarm_state = "armed"; + reset(); + blue.printf("Alarm has been terminated.\n"); + } + else if (strcmp(request, "disarm") == 0) + { + alarm_state = "disarmed"; + armed = false; + blue.printf("Alarm has been disarmed.\n"); + } + else if (strcmp(request, "arm") == 0) + { + alarm_state = "armed"; + armed = true; + blue.printf("Alarm has been armed!\n"); + } + else if (strcmp(request, "status") == 0) + { + blue.printf("Alarm's current state is: %s\n", alarm_state); + } + } } } -void thread4(void const *args) { - while(true) { - FILE *wave_file; - wave_file=fopen("/sd/Siren.wav","r"); - waver.play(wave_file,PlayStopPtr); - fclose(wave_file); - Thread::wait(1000); // wait 1.0s + +//Thread for turning on/changing color of shiftbrite led based on respective alarm triggers +//red for sound +//green for motion +//blue for temperature +void LED_THREAD() +{ + SB_LED.format(16,0); + SB_LED.frequency(500000); + enable=0; + latch=0; + while (1) + { + if (soundAlarm && armed) + { + ledStatus = true; + led_mutex.lock(); + RGB_LED(100, 0, 0); + led_mutex.unlock(); + Thread::wait(100); + } + if (distAlarm && armed) + { + ledStatus = true; + led_mutex.lock(); + RGB_LED(0, 100, 0); + led_mutex.unlock(); + Thread::wait(100); + } + if (tempAlarm && armed) + { + ledStatus = true; + led_mutex.lock(); + RGB_LED(0, 0, 100); + led_mutex.unlock(); + Thread::wait(100); + } + + if (ledStatus == false) + { + RGB_LED(0, 0, 0); + } + RGB_LED(0, 0, 0); + + pc.printf("LED TRIGGER: %d\r\n\n", ledStatus); + Thread::wait(500); + } +} + +//Thread for detecting pushbutton intterupts thus triggering reset +void PB_THREAD() +{ + //set mode PullUp + pb.mode(PullUp); + + // Delay for initial pullup to take effect + Thread:: wait(10); + + while (1) + { + if (pb == 0) + { + reset(); + Thread::wait(10); + } } } - -int main() { - - myLCD.cls(); + +int main() +{ + Thread sound_sensor(SOUND_THREAD); //start microphone thread + Thread temp_sensor(TEMP_THREAD); //start temperature sensor thread + Thread play_alarm(WAVE_THREAD); //start wave file alarm thread + Thread led_light(LED_THREAD); //start shiftbrite led thread + Thread pushbutton(PB_THREAD); //start pushbutton interrupt thread + Thread bluetooth(BLUETOOTH_THREAD); //start bluetooth thread - Thread t1(thread1); - Thread t2(thread2); - // Thread t3(thread3); - Thread t4(thread4); + uint16_t distance; //for VL53L0X - Timer t; - t.start(); - while (true) { - lcd_mutex.lock(); - myLCD.locate(0,9); - myLCD.printf("The time taken was %0.2f seconds\n", t.read()); - char bred=0; - char bgreen=0; - char bblue=0; - red = green = blue = 0; - while(1) { - if (bluemod.getc()=='!') { - if (bluemod.getc()=='C') { //color data packet - bred = bluemod.getc(); // RGB color values - bgreen = bluemod.getc(); - bblue = bluemod.getc(); - if (bluemod.getc()==char(~('!' + 'C' + bred + bgreen + bblue))) { //checksum OK? - red = bred/255.0; //send new color to RGB LED PWM outputs - green = bgreen/255.0; - blue = bblue/255.0; - } - } - } + shdn = 0; //must reset sensor for an mbed reset to work + wait(0.1); + shdn = 1; + wait(0.1); + + //initializations for lidar sensor (VL53L0X) + lidar.init(); + lidar.setModeContinuous(); + lidar.startContinuous(); + + while (1) + { + // read distance for Lidar + distance = lidar.getRangeMillimeters(); + distanceTrigger(distance); + wait(1.0); } - - - lcd_mutex.unlock(); - Thread::wait(500); - } + }