Final project

Dependencies:   mbed wave_player mbed-rtos 4DGL-uLCD-SE SDFileSystem 4180_Lab3_rtos_basic

Committer:
astovall21
Date:
Mon May 03 23:01:08 2021 +0000
Revision:
10:a644e4e16cf4
Parent:
9:1b37fd4bf5ea
Final Project Spr 2021

Who changed what in which revision?

UserRevisionLine numberNew contents of line
astovall21 10:a644e4e16cf4 1 /*
astovall21 10:a644e4e16cf4 2 -------------------AUTHORS-----------------------
astovall21 10:a644e4e16cf4 3 \\\\\\\\\\\\\\\\\\\\\\///////////////////////////
astovall21 10:a644e4e16cf4 4
astovall21 10:a644e4e16cf4 5 JACQUES CRAWFORD
astovall21 10:a644e4e16cf4 6 ALTON STOVALL
astovall21 10:a644e4e16cf4 7 JAYLA WILLIAMS
astovall21 10:a644e4e16cf4 8 MILES BARROS
astovall21 10:a644e4e16cf4 9
astovall21 10:a644e4e16cf4 10 -----------------DEPENDECIES---------------------
astovall21 10:a644e4e16cf4 11 \\\\\\\\\\\\\\\\\\\\\\///////////////////////////
astovall21 10:a644e4e16cf4 12
astovall21 10:a644e4e16cf4 13 ---HARDWARE---
astovall21 10:a644e4e16cf4 14
astovall21 10:a644e4e16cf4 15 - TEMPERATURE SENSOR: TMP36
astovall21 10:a644e4e16cf4 16 - LIDAR SENSOR: VL53L0X
astovall21 10:a644e4e16cf4 17 - MICRPHONE: SPW2430
astovall21 10:a644e4e16cf4 18 - CLASS D AUIO AMP: TPA2005D1
astovall21 10:a644e4e16cf4 19 - SPEAKER: NaN
astovall21 10:a644e4e16cf4 20 - SHIFTBRITE: COM-10075
astovall21 10:a644e4e16cf4 21 - SD CARD READER: NaN
astovall21 10:a644e4e16cf4 22 - PUSHBUTTON: NaN
astovall21 10:a644e4e16cf4 23 - BARREL JACK (5V): NaN
astovall21 10:a644e4e16cf4 24
astovall21 10:a644e4e16cf4 25 ---I/O PIN ASSIGMENTS---
ldeng31 7:49bfd42cbf6f 26
astovall21 10:a644e4e16cf4 27 TMP36
astovall21 10:a644e4e16cf4 28 --------------------------------
astovall21 10:a644e4e16cf4 29 mbed | TMP36
astovall21 10:a644e4e16cf4 30 --------------------------------
astovall21 10:a644e4e16cf4 31 GND | GND
astovall21 10:a644e4e16cf4 32 Vout(3.3V) | Vs
astovall21 10:a644e4e16cf4 33 p19 | Vout
astovall21 10:a644e4e16cf4 34
astovall21 10:a644e4e16cf4 35
astovall21 10:a644e4e16cf4 36 VL53L0X
astovall21 10:a644e4e16cf4 37 ----------------
astovall21 10:a644e4e16cf4 38 Mbed | VL53L0X
astovall21 10:a644e4e16cf4 39 ----------------
astovall21 10:a644e4e16cf4 40 3.3V | VIN
astovall21 10:a644e4e16cf4 41 GND | GND
astovall21 10:a644e4e16cf4 42 P28 | SDA
astovall21 10:a644e4e16cf4 43 P27 | SCL
astovall21 10:a644e4e16cf4 44 P26 | SHDN
astovall21 10:a644e4e16cf4 45
astovall21 10:a644e4e16cf4 46 SPW2430
astovall21 10:a644e4e16cf4 47 ---------------------------------
astovall21 10:a644e4e16cf4 48 mbed | microphone
astovall21 10:a644e4e16cf4 49 ---------------------------------
astovall21 10:a644e4e16cf4 50 gnd | gnd
astovall21 10:a644e4e16cf4 51 VU(+5VDC) | Vin
astovall21 10:a644e4e16cf4 52 p16 | (AnalogIn) DC
mbarros7 9:1b37fd4bf5ea 53
ycai47 8:8eb653dc6571 54
astovall21 10:a644e4e16cf4 55 TPA2005D1
astovall21 10:a644e4e16cf4 56 --------------------------------------------------------
astovall21 10:a644e4e16cf4 57 mbed | TPA2005D1 | Speaker
astovall21 10:a644e4e16cf4 58 --------------------------------------------------------
astovall21 10:a644e4e16cf4 59 gnd | pwr - (gnd), in - |
astovall21 10:a644e4e16cf4 60 Vout (3.3V) or 5V | pwr + |
astovall21 10:a644e4e16cf4 61 p18 (A/D) | in + |
astovall21 10:a644e4e16cf4 62 | out + | +
astovall21 10:a644e4e16cf4 63 | out - | -
astovall21 10:a644e4e16cf4 64
astovall21 10:a644e4e16cf4 65 SHIFTBRITE
astovall21 10:a644e4e16cf4 66 --------------------
astovall21 10:a644e4e16cf4 67 mbed | ShiftBrite
astovall21 10:a644e4e16cf4 68 --------------------
astovall21 10:a644e4e16cf4 69 gnd | Gnd
astovall21 10:a644e4e16cf4 70 p11 | DI
astovall21 10:a644e4e16cf4 71 p15 | LI
astovall21 10:a644e4e16cf4 72 p20 | EI
astovall21 10:a644e4e16cf4 73 p13 | CI
astovall21 10:a644e4e16cf4 74 VU(5V)| V+*
astovall21 10:a644e4e16cf4 75
astovall21 10:a644e4e16cf4 76 SD CARD READER
astovall21 10:a644e4e16cf4 77 -----------------------------------
astovall21 10:a644e4e16cf4 78 mbed | sd card reader
astovall21 10:a644e4e16cf4 79 -----------------------------------
astovall21 10:a644e4e16cf4 80 p8 | CS
astovall21 10:a644e4e16cf4 81 p5 | DI
astovall21 10:a644e4e16cf4 82 VOUT | VCC
astovall21 10:a644e4e16cf4 83 p7 | SCK
astovall21 10:a644e4e16cf4 84 gnd | GND
astovall21 10:a644e4e16cf4 85 p6 | DO
astovall21 10:a644e4e16cf4 86 NC | CD
mbarros7 9:1b37fd4bf5ea 87
astovall21 10:a644e4e16cf4 88 PUHBUTTON
astovall21 10:a644e4e16cf4 89 ---------------------------
astovall21 10:a644e4e16cf4 90 mbed | pushbutton
astovall21 10:a644e4e16cf4 91 ---------------------------
astovall21 10:a644e4e16cf4 92 gnd | -
astovall21 10:a644e4e16cf4 93 p22 | +
astovall21 10:a644e4e16cf4 94
astovall21 10:a644e4e16cf4 95 */
astovall21 10:a644e4e16cf4 96
ldeng31 7:49bfd42cbf6f 97
astovall21 10:a644e4e16cf4 98 //required libraries
astovall21 10:a644e4e16cf4 99 #include "mbed.h"
astovall21 10:a644e4e16cf4 100 #include <stdio.h>
astovall21 10:a644e4e16cf4 101 #include "rtos.h"
astovall21 10:a644e4e16cf4 102 #include "VL53L0X.h"
astovall21 10:a644e4e16cf4 103 #include "wave_player.h"
astovall21 10:a644e4e16cf4 104 #include "SDFileSystem.h"
astovall21 10:a644e4e16cf4 105 #include "DebounceIn.h"
astovall21 10:a644e4e16cf4 106 #include <string>
astovall21 10:a644e4e16cf4 107
astovall21 10:a644e4e16cf4 108 using namespace std;
astovall21 10:a644e4e16cf4 109
astovall21 10:a644e4e16cf4 110 //ShiftBrite pin assignments
astovall21 10:a644e4e16cf4 111 DigitalOut latch(p15);
astovall21 10:a644e4e16cf4 112 DigitalOut enable(p20);
astovall21 10:a644e4e16cf4 113 SPI SB_LED(p11, p12, p13);
astovall21 10:a644e4e16cf4 114
astovall21 10:a644e4e16cf4 115 //onboard led
astovall21 10:a644e4e16cf4 116 DigitalOut myled(LED1);
astovall21 10:a644e4e16cf4 117
astovall21 10:a644e4e16cf4 118 //pushbutton pin assignments
astovall21 10:a644e4e16cf4 119 InterruptIn pb(p22);
astovall21 10:a644e4e16cf4 120
astovall21 10:a644e4e16cf4 121 //sdcard pin assignments
ycai47 8:8eb653dc6571 122 SDFileSystem sd(p5, p6, p7, p8, "sd");
ycai47 8:8eb653dc6571 123
astovall21 10:a644e4e16cf4 124 //speaker pin assignment
ycai47 8:8eb653dc6571 125 AnalogOut Speaker(p18);
astovall21 10:a644e4e16cf4 126
astovall21 10:a644e4e16cf4 127 //wave player pin assignments and instantiation
ycai47 8:8eb653dc6571 128 wave_player waver(&Speaker);
ycai47 8:8eb653dc6571 129 bool PLAY = true;
ycai47 8:8eb653dc6571 130 bool *PlayStopPtr = &PLAY;
astovall21 10:a644e4e16cf4 131 FILE *wave_file;
ycai47 8:8eb653dc6571 132
astovall21 10:a644e4e16cf4 133
astovall21 10:a644e4e16cf4 134 //pc terminal communication
astovall21 10:a644e4e16cf4 135 Serial pc(USBTX,USBRX);
astovall21 10:a644e4e16cf4 136 DigitalOut shdn(p26);
astovall21 10:a644e4e16cf4 137
astovall21 10:a644e4e16cf4 138 //thread mutexes
astovall21 10:a644e4e16cf4 139 Mutex waver_mutex;
astovall21 10:a644e4e16cf4 140 Mutex led_mutex;
astovall21 10:a644e4e16cf4 141
astovall21 10:a644e4e16cf4 142 // Lidar sensor
astovall21 10:a644e4e16cf4 143 I2C i2c(p28, p27);
astovall21 10:a644e4e16cf4 144 VL53L0X lidar(&i2c);
astovall21 10:a644e4e16cf4 145
astovall21 10:a644e4e16cf4 146 // connect p19(WIZwiki-W7500) to Vout(Temp36)
astovall21 10:a644e4e16cf4 147 AnalogIn temp(p19);
astovall21 10:a644e4e16cf4 148
astovall21 10:a644e4e16cf4 149 //bluetooth serial connection
astovall21 10:a644e4e16cf4 150 Serial blue(p9,p10);
astovall21 10:a644e4e16cf4 151
astovall21 10:a644e4e16cf4 152
astovall21 10:a644e4e16cf4 153 //boolean trigger variables (intuitively named)
astovall21 10:a644e4e16cf4 154 bool soundAlarm = false;
astovall21 10:a644e4e16cf4 155 bool tempAlarm = false;
astovall21 10:a644e4e16cf4 156 bool distAlarm = false;
astovall21 10:a644e4e16cf4 157 bool ledStatus = false;
astovall21 10:a644e4e16cf4 158 bool armed = false;
astovall21 10:a644e4e16cf4 159
astovall21 10:a644e4e16cf4 160 bool distNotified = false;
astovall21 10:a644e4e16cf4 161 bool soundNotified = false;
astovall21 10:a644e4e16cf4 162 bool tempNotified = false;
astovall21 10:a644e4e16cf4 163
astovall21 10:a644e4e16cf4 164
astovall21 10:a644e4e16cf4 165 ///////////////////////////////////
astovall21 10:a644e4e16cf4 166 // Microphone class for SPW2430
astovall21 10:a644e4e16cf4 167 ///////////////////////////////////
astovall21 10:a644e4e16cf4 168 class microphone
astovall21 10:a644e4e16cf4 169 {
astovall21 10:a644e4e16cf4 170 public :
astovall21 10:a644e4e16cf4 171 microphone(PinName pin);
astovall21 10:a644e4e16cf4 172 float read();
astovall21 10:a644e4e16cf4 173 operator float ();
astovall21 10:a644e4e16cf4 174 private :
astovall21 10:a644e4e16cf4 175 AnalogIn _pin;
astovall21 10:a644e4e16cf4 176 };
astovall21 10:a644e4e16cf4 177 microphone::microphone (PinName pin):
astovall21 10:a644e4e16cf4 178 _pin(pin)
astovall21 10:a644e4e16cf4 179 {
astovall21 10:a644e4e16cf4 180 }
astovall21 10:a644e4e16cf4 181 float microphone::read()
astovall21 10:a644e4e16cf4 182 {
astovall21 10:a644e4e16cf4 183 return _pin.read();
astovall21 10:a644e4e16cf4 184 }
astovall21 10:a644e4e16cf4 185 inline microphone::operator float ()
astovall21 10:a644e4e16cf4 186 {
astovall21 10:a644e4e16cf4 187 return _pin.read();
astovall21 10:a644e4e16cf4 188 }
astovall21 10:a644e4e16cf4 189
astovall21 10:a644e4e16cf4 190
astovall21 10:a644e4e16cf4 191 // microphone pin assignment and object creation
astovall21 10:a644e4e16cf4 192 microphone mymicrophone(p16);
astovall21 10:a644e4e16cf4 193
astovall21 10:a644e4e16cf4 194 //shiftbrite RBG function. this function controls the color of the led based on the alarm trigger
astovall21 10:a644e4e16cf4 195 void RGB_LED(int red, int green, int blue)
astovall21 10:a644e4e16cf4 196 {
astovall21 10:a644e4e16cf4 197 unsigned int low_color=0;
astovall21 10:a644e4e16cf4 198 unsigned int high_color=0;
astovall21 10:a644e4e16cf4 199 high_color=(blue<<4)|((red&0x3C0)>>6);
astovall21 10:a644e4e16cf4 200 low_color=(((red&0x3F)<<10)|(green));
astovall21 10:a644e4e16cf4 201 SB_LED.write(high_color);
astovall21 10:a644e4e16cf4 202 SB_LED.write(low_color);
astovall21 10:a644e4e16cf4 203 latch=1;
astovall21 10:a644e4e16cf4 204 latch=0;
astovall21 10:a644e4e16cf4 205 }
astovall21 10:a644e4e16cf4 206
astovall21 10:a644e4e16cf4 207 //Reset all parameter i.e. reset alarm device
astovall21 10:a644e4e16cf4 208 void reset(void)
mbarros7 9:1b37fd4bf5ea 209 {
astovall21 10:a644e4e16cf4 210 soundAlarm = false;
astovall21 10:a644e4e16cf4 211 tempAlarm = false;
astovall21 10:a644e4e16cf4 212 distAlarm = false;
astovall21 10:a644e4e16cf4 213 ledStatus = false;
astovall21 10:a644e4e16cf4 214 armed = false;
astovall21 10:a644e4e16cf4 215
astovall21 10:a644e4e16cf4 216 bool distNotified = false;
astovall21 10:a644e4e16cf4 217 bool soundNotified = false;
astovall21 10:a644e4e16cf4 218 bool tempNotified = false;
astovall21 10:a644e4e16cf4 219
astovall21 10:a644e4e16cf4 220 pc.printf("LED STATUS: %d\r\nTEMP STATUS: %d\r\nDIST STATUS: %d\r\nLED STATUS: %d\r\n", soundAlarm, tempAlarm, distAlarm, ledStatus);
astovall21 10:a644e4e16cf4 221 }
astovall21 10:a644e4e16cf4 222
astovall21 10:a644e4e16cf4 223 //Handle motion detected flag
astovall21 10:a644e4e16cf4 224 bool distanceTrigger(uint16_t dist)
astovall21 10:a644e4e16cf4 225 {
astovall21 10:a644e4e16cf4 226 if (dist < 400 && dist != 20 && armed)
astovall21 10:a644e4e16cf4 227 {
astovall21 10:a644e4e16cf4 228 distAlarm = true;
astovall21 10:a644e4e16cf4 229 }
astovall21 10:a644e4e16cf4 230
astovall21 10:a644e4e16cf4 231 if (distAlarm && armed)
astovall21 10:a644e4e16cf4 232 {
astovall21 10:a644e4e16cf4 233 pc.printf("DIST_EVENT_STATUS: %d\r\n", distAlarm); // 1 for true, 0 for false
astovall21 10:a644e4e16cf4 234 pc.printf("Distance: %d mm\r\n\n", dist);
astovall21 10:a644e4e16cf4 235
astovall21 10:a644e4e16cf4 236 if (distNotified == false)
astovall21 10:a644e4e16cf4 237 {
astovall21 10:a644e4e16cf4 238 blue.printf("DISTANCE ALARM HAS BEEN TRIGGERED! Type \"stop\" to disarm.\n");
astovall21 10:a644e4e16cf4 239 }
astovall21 10:a644e4e16cf4 240 distNotified = true;
astovall21 10:a644e4e16cf4 241
astovall21 10:a644e4e16cf4 242 }
astovall21 10:a644e4e16cf4 243 return distAlarm;
astovall21 10:a644e4e16cf4 244 }
astovall21 10:a644e4e16cf4 245
astovall21 10:a644e4e16cf4 246 //Handle temperature warning flag
astovall21 10:a644e4e16cf4 247 bool tempTrigger(float temp)
astovall21 10:a644e4e16cf4 248 {
astovall21 10:a644e4e16cf4 249 pc.printf("TEMP_EVENT_STATUS: %d\r\n", tempAlarm);// 1 for true, 0 for false
astovall21 10:a644e4e16cf4 250 pc.printf("Temperature: %5.2f F \r\n\n", temp);
astovall21 10:a644e4e16cf4 251 if (temp > 60.0 && armed)
astovall21 10:a644e4e16cf4 252 {
astovall21 10:a644e4e16cf4 253 tempAlarm = true;
astovall21 10:a644e4e16cf4 254 }
astovall21 10:a644e4e16cf4 255
astovall21 10:a644e4e16cf4 256 if (tempAlarm && armed)
astovall21 10:a644e4e16cf4 257 {
astovall21 10:a644e4e16cf4 258 pc.printf("TEMP_EVENT_STATUS: %d\r\n", tempAlarm);// 1 for true, 0 for false
astovall21 10:a644e4e16cf4 259 pc.printf("Temperature: %5.2f F \r\n\n", temp);
astovall21 10:a644e4e16cf4 260 if (tempNotified == false)
astovall21 10:a644e4e16cf4 261 {
astovall21 10:a644e4e16cf4 262 blue.printf("TEMPERATURE ALARM HAS BEEN TRIGGERED! Type \"stop\" to disarm.\n");
astovall21 10:a644e4e16cf4 263 }
astovall21 10:a644e4e16cf4 264 tempNotified = true;
astovall21 10:a644e4e16cf4 265 }
astovall21 10:a644e4e16cf4 266 return tempAlarm;
astovall21 10:a644e4e16cf4 267 }
astovall21 10:a644e4e16cf4 268
astovall21 10:a644e4e16cf4 269 //Handle sound detected flag
astovall21 10:a644e4e16cf4 270 bool soundTrigger(int snd)
astovall21 10:a644e4e16cf4 271 {
astovall21 10:a644e4e16cf4 272 if (snd > 10 && armed)
astovall21 10:a644e4e16cf4 273 {
astovall21 10:a644e4e16cf4 274 soundAlarm = true;
astovall21 10:a644e4e16cf4 275 }
astovall21 10:a644e4e16cf4 276
astovall21 10:a644e4e16cf4 277 if (soundAlarm && armed)
astovall21 10:a644e4e16cf4 278 {
astovall21 10:a644e4e16cf4 279 pc.printf("SOUND_EVENT_STATUS: %d\r\n", soundAlarm);// 1 for true, 0 for false
astovall21 10:a644e4e16cf4 280 pc.printf("Sound level: %d\r\n\n", snd);
astovall21 10:a644e4e16cf4 281 if (soundNotified == false) {
astovall21 10:a644e4e16cf4 282 blue.printf("SOUND ALARM HAS BEEN TRIGGERED! Type \"stop\" to disarm.\n");
astovall21 10:a644e4e16cf4 283 }
astovall21 10:a644e4e16cf4 284 soundNotified = true;
astovall21 10:a644e4e16cf4 285
astovall21 10:a644e4e16cf4 286
astovall21 10:a644e4e16cf4 287 }
astovall21 10:a644e4e16cf4 288
astovall21 10:a644e4e16cf4 289 return soundAlarm;
astovall21 10:a644e4e16cf4 290
astovall21 10:a644e4e16cf4 291 }
astovall21 10:a644e4e16cf4 292
astovall21 10:a644e4e16cf4 293 //Thread for continuos microphone reading and flag setting --> (soundTrigger(sound))
astovall21 10:a644e4e16cf4 294 void SOUND_THREAD()
astovall21 10:a644e4e16cf4 295 {
astovall21 10:a644e4e16cf4 296 float sound;
astovall21 10:a644e4e16cf4 297
astovall21 10:a644e4e16cf4 298 while (1)
astovall21 10:a644e4e16cf4 299 {
astovall21 10:a644e4e16cf4 300 sound = int(abs((mymicrophone - (0.67/3.3)))*500.0);
astovall21 10:a644e4e16cf4 301 soundTrigger(sound);
astovall21 10:a644e4e16cf4 302 Thread::wait(1000);
astovall21 10:a644e4e16cf4 303
astovall21 10:a644e4e16cf4 304 }
astovall21 10:a644e4e16cf4 305 }
astovall21 10:a644e4e16cf4 306
astovall21 10:a644e4e16cf4 307 //Thread for continuos temperature reading and flag setting --> (tempTrigger(distance))
astovall21 10:a644e4e16cf4 308 void TEMP_THREAD()
astovall21 10:a644e4e16cf4 309 {
astovall21 10:a644e4e16cf4 310 while(1)
astovall21 10:a644e4e16cf4 311 {
astovall21 10:a644e4e16cf4 312 float V = temp.read() * 3.3; // connect Vs(Temp36) to 3.3V(WIZwiki-W7500)
astovall21 10:a644e4e16cf4 313 float tempC = (V - 0.5) * 100; // calculate temperature C
astovall21 10:a644e4e16cf4 314 float tempF = (tempC * 9 / 5) + 32.0; // calculate temperature F
astovall21 10:a644e4e16cf4 315 tempTrigger(tempF);
mbarros7 9:1b37fd4bf5ea 316 Thread::wait(1000);
ycai47 8:8eb653dc6571 317 }
ldeng31 7:49bfd42cbf6f 318 }
ldeng31 7:49bfd42cbf6f 319
astovall21 10:a644e4e16cf4 320 //Thread for playing wave file based on respective alarm triggers
astovall21 10:a644e4e16cf4 321 void WAVE_THREAD()
mbarros7 9:1b37fd4bf5ea 322 {
astovall21 10:a644e4e16cf4 323 while (1)
astovall21 10:a644e4e16cf4 324 {
astovall21 10:a644e4e16cf4 325 if (soundAlarm && armed)
astovall21 10:a644e4e16cf4 326 {
astovall21 10:a644e4e16cf4 327 waver_mutex.lock();
astovall21 10:a644e4e16cf4 328 wave_file=fopen("/sd/sound_new_speed.wav","r");
astovall21 10:a644e4e16cf4 329 waver.play(wave_file, PlayStopPtr);
astovall21 10:a644e4e16cf4 330 fclose(wave_file);
astovall21 10:a644e4e16cf4 331 waver_mutex.unlock();
astovall21 10:a644e4e16cf4 332 Thread::wait(500);
astovall21 10:a644e4e16cf4 333 }
astovall21 10:a644e4e16cf4 334 if (distAlarm && armed)
astovall21 10:a644e4e16cf4 335 {
astovall21 10:a644e4e16cf4 336 waver_mutex.lock();
astovall21 10:a644e4e16cf4 337 wave_file=fopen("/sd/motion_new_speed.wav","r");
astovall21 10:a644e4e16cf4 338 waver.play(wave_file, PlayStopPtr);
astovall21 10:a644e4e16cf4 339 fclose(wave_file);
astovall21 10:a644e4e16cf4 340 waver_mutex.unlock();
astovall21 10:a644e4e16cf4 341 Thread::wait(500);
astovall21 10:a644e4e16cf4 342 }
astovall21 10:a644e4e16cf4 343 if (tempAlarm && armed)
astovall21 10:a644e4e16cf4 344 {
astovall21 10:a644e4e16cf4 345 waver_mutex.lock();
astovall21 10:a644e4e16cf4 346 wave_file=fopen("/sd/temp_new_speed.wav","r");
astovall21 10:a644e4e16cf4 347 waver.play(wave_file, PlayStopPtr);
astovall21 10:a644e4e16cf4 348 fclose(wave_file);
astovall21 10:a644e4e16cf4 349 waver_mutex.unlock();
astovall21 10:a644e4e16cf4 350 Thread::wait(500);
astovall21 10:a644e4e16cf4 351 }
astovall21 10:a644e4e16cf4 352 Thread::wait(1000);
astovall21 10:a644e4e16cf4 353 }
astovall21 10:a644e4e16cf4 354 }
astovall21 10:a644e4e16cf4 355
astovall21 10:a644e4e16cf4 356 //Thread for checking any and all bluetooth commands
astovall21 10:a644e4e16cf4 357 void BLUETOOTH_THREAD()
astovall21 10:a644e4e16cf4 358 {
astovall21 10:a644e4e16cf4 359 char request[10];
astovall21 10:a644e4e16cf4 360 string alarm_state = "disarmed";
astovall21 10:a644e4e16cf4 361
astovall21 10:a644e4e16cf4 362 while(1) {
astovall21 10:a644e4e16cf4 363 //check for user input to bluetooth
astovall21 10:a644e4e16cf4 364 if(blue.readable())
astovall21 10:a644e4e16cf4 365 {
astovall21 10:a644e4e16cf4 366 blue.scanf("%s", request);
astovall21 10:a644e4e16cf4 367 if (strcmp(request, "stop") == 0)
astovall21 10:a644e4e16cf4 368 {
astovall21 10:a644e4e16cf4 369 alarm_state = "armed";
astovall21 10:a644e4e16cf4 370 reset();
astovall21 10:a644e4e16cf4 371 blue.printf("Alarm has been terminated.\n");
astovall21 10:a644e4e16cf4 372 }
astovall21 10:a644e4e16cf4 373 else if (strcmp(request, "disarm") == 0)
astovall21 10:a644e4e16cf4 374 {
astovall21 10:a644e4e16cf4 375 alarm_state = "disarmed";
astovall21 10:a644e4e16cf4 376 armed = false;
astovall21 10:a644e4e16cf4 377 blue.printf("Alarm has been disarmed.\n");
astovall21 10:a644e4e16cf4 378 }
astovall21 10:a644e4e16cf4 379 else if (strcmp(request, "arm") == 0)
astovall21 10:a644e4e16cf4 380 {
astovall21 10:a644e4e16cf4 381 alarm_state = "armed";
astovall21 10:a644e4e16cf4 382 armed = true;
astovall21 10:a644e4e16cf4 383 blue.printf("Alarm has been armed!\n");
astovall21 10:a644e4e16cf4 384 }
astovall21 10:a644e4e16cf4 385 else if (strcmp(request, "status") == 0)
astovall21 10:a644e4e16cf4 386 {
astovall21 10:a644e4e16cf4 387 blue.printf("Alarm's current state is: %s\n", alarm_state);
astovall21 10:a644e4e16cf4 388 }
astovall21 10:a644e4e16cf4 389 }
ycai47 8:8eb653dc6571 390 }
ldeng31 7:49bfd42cbf6f 391 }
ldeng31 7:49bfd42cbf6f 392
ldeng31 7:49bfd42cbf6f 393
astovall21 10:a644e4e16cf4 394
astovall21 10:a644e4e16cf4 395 //Thread for turning on/changing color of shiftbrite led based on respective alarm triggers
astovall21 10:a644e4e16cf4 396 //red for sound
astovall21 10:a644e4e16cf4 397 //green for motion
astovall21 10:a644e4e16cf4 398 //blue for temperature
astovall21 10:a644e4e16cf4 399 void LED_THREAD()
astovall21 10:a644e4e16cf4 400 {
astovall21 10:a644e4e16cf4 401 SB_LED.format(16,0);
astovall21 10:a644e4e16cf4 402 SB_LED.frequency(500000);
astovall21 10:a644e4e16cf4 403 enable=0;
astovall21 10:a644e4e16cf4 404 latch=0;
astovall21 10:a644e4e16cf4 405 while (1)
astovall21 10:a644e4e16cf4 406 {
astovall21 10:a644e4e16cf4 407 if (soundAlarm && armed)
astovall21 10:a644e4e16cf4 408 {
astovall21 10:a644e4e16cf4 409 ledStatus = true;
astovall21 10:a644e4e16cf4 410 led_mutex.lock();
astovall21 10:a644e4e16cf4 411 RGB_LED(100, 0, 0);
astovall21 10:a644e4e16cf4 412 led_mutex.unlock();
astovall21 10:a644e4e16cf4 413 Thread::wait(100);
astovall21 10:a644e4e16cf4 414 }
astovall21 10:a644e4e16cf4 415 if (distAlarm && armed)
astovall21 10:a644e4e16cf4 416 {
astovall21 10:a644e4e16cf4 417 ledStatus = true;
astovall21 10:a644e4e16cf4 418 led_mutex.lock();
astovall21 10:a644e4e16cf4 419 RGB_LED(0, 100, 0);
astovall21 10:a644e4e16cf4 420 led_mutex.unlock();
astovall21 10:a644e4e16cf4 421 Thread::wait(100);
astovall21 10:a644e4e16cf4 422 }
astovall21 10:a644e4e16cf4 423 if (tempAlarm && armed)
astovall21 10:a644e4e16cf4 424 {
astovall21 10:a644e4e16cf4 425 ledStatus = true;
astovall21 10:a644e4e16cf4 426 led_mutex.lock();
astovall21 10:a644e4e16cf4 427 RGB_LED(0, 0, 100);
astovall21 10:a644e4e16cf4 428 led_mutex.unlock();
astovall21 10:a644e4e16cf4 429 Thread::wait(100);
astovall21 10:a644e4e16cf4 430 }
astovall21 10:a644e4e16cf4 431
astovall21 10:a644e4e16cf4 432 if (ledStatus == false)
astovall21 10:a644e4e16cf4 433 {
astovall21 10:a644e4e16cf4 434 RGB_LED(0, 0, 0);
astovall21 10:a644e4e16cf4 435 }
astovall21 10:a644e4e16cf4 436 RGB_LED(0, 0, 0);
astovall21 10:a644e4e16cf4 437
astovall21 10:a644e4e16cf4 438 pc.printf("LED TRIGGER: %d\r\n\n", ledStatus);
astovall21 10:a644e4e16cf4 439 Thread::wait(500);
astovall21 10:a644e4e16cf4 440 }
astovall21 10:a644e4e16cf4 441 }
astovall21 10:a644e4e16cf4 442
astovall21 10:a644e4e16cf4 443 //Thread for detecting pushbutton intterupts thus triggering reset
astovall21 10:a644e4e16cf4 444 void PB_THREAD()
astovall21 10:a644e4e16cf4 445 {
astovall21 10:a644e4e16cf4 446 //set mode PullUp
astovall21 10:a644e4e16cf4 447 pb.mode(PullUp);
astovall21 10:a644e4e16cf4 448
astovall21 10:a644e4e16cf4 449 // Delay for initial pullup to take effect
astovall21 10:a644e4e16cf4 450 Thread:: wait(10);
astovall21 10:a644e4e16cf4 451
astovall21 10:a644e4e16cf4 452 while (1)
astovall21 10:a644e4e16cf4 453 {
astovall21 10:a644e4e16cf4 454 if (pb == 0)
astovall21 10:a644e4e16cf4 455 {
astovall21 10:a644e4e16cf4 456 reset();
astovall21 10:a644e4e16cf4 457 Thread::wait(10);
astovall21 10:a644e4e16cf4 458 }
ycai47 8:8eb653dc6571 459 }
ldeng31 7:49bfd42cbf6f 460 }
astovall21 10:a644e4e16cf4 461
astovall21 10:a644e4e16cf4 462 int main()
astovall21 10:a644e4e16cf4 463 {
astovall21 10:a644e4e16cf4 464 Thread sound_sensor(SOUND_THREAD); //start microphone thread
astovall21 10:a644e4e16cf4 465 Thread temp_sensor(TEMP_THREAD); //start temperature sensor thread
astovall21 10:a644e4e16cf4 466 Thread play_alarm(WAVE_THREAD); //start wave file alarm thread
astovall21 10:a644e4e16cf4 467 Thread led_light(LED_THREAD); //start shiftbrite led thread
astovall21 10:a644e4e16cf4 468 Thread pushbutton(PB_THREAD); //start pushbutton interrupt thread
astovall21 10:a644e4e16cf4 469 Thread bluetooth(BLUETOOTH_THREAD); //start bluetooth thread
ycai47 8:8eb653dc6571 470
astovall21 10:a644e4e16cf4 471 uint16_t distance; //for VL53L0X
emilmont 1:491820ee784d 472
astovall21 10:a644e4e16cf4 473 shdn = 0; //must reset sensor for an mbed reset to work
astovall21 10:a644e4e16cf4 474 wait(0.1);
astovall21 10:a644e4e16cf4 475 shdn = 1;
astovall21 10:a644e4e16cf4 476 wait(0.1);
astovall21 10:a644e4e16cf4 477
astovall21 10:a644e4e16cf4 478 //initializations for lidar sensor (VL53L0X)
astovall21 10:a644e4e16cf4 479 lidar.init();
astovall21 10:a644e4e16cf4 480 lidar.setModeContinuous();
astovall21 10:a644e4e16cf4 481 lidar.startContinuous();
astovall21 10:a644e4e16cf4 482
astovall21 10:a644e4e16cf4 483 while (1)
astovall21 10:a644e4e16cf4 484 {
astovall21 10:a644e4e16cf4 485 // read distance for Lidar
astovall21 10:a644e4e16cf4 486 distance = lidar.getRangeMillimeters();
astovall21 10:a644e4e16cf4 487 distanceTrigger(distance);
astovall21 10:a644e4e16cf4 488 wait(1.0);
mbarros7 9:1b37fd4bf5ea 489 }
astovall21 10:a644e4e16cf4 490
emilmont 1:491820ee784d 491 }