Benjamin Leverett / Mbed 2 deprecated Final_Project_2

Dependencies:   mbed mbed-rtos 4DGL-uLCD-SE SDFileSystem ATParser

Files at this revision

API Documentation at this revision

Comitter:
chenchen2020
Date:
Fri Apr 24 18:55:46 2020 +0000
Parent:
1:8eb0ee57df9f
Child:
3:2a4bee05d0c1
Commit message:
Working Code V2;

Changed in this revision

Speaker.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Speaker.h	Fri Apr 24 18:55:46 2020 +0000
@@ -0,0 +1,52 @@
+class Speaker
+{
+public:
+    Speaker(PinName pin) : _pin(pin) {
+// _pin(pin) means pass pin to the Speaker Constructor
+// precompute 32 sample points on one sine wave cycle
+// used for continuous sine wave output later
+        for(int k=0; k<32; k++) {
+            Analog_out_data[k] = int (65536.0 * ((1.0 + sin((float(k)/32.0*6.28318530717959)))/2.0));
+            // scale the sine wave to 16-bits - as needed for AnalogOut write_u16 arg
+        }
+
+    }
+// class method to play a note based on AnalogOut class
+    void PlayNote(float frequency, float duration, float volume) {
+        // scale samples using current volume level arg
+        for(int k=0; k<32; k++) {
+            Analog_scaled_data[k] = Analog_out_data[k] * volume;
+        }
+        // reset to start of sample array
+        i=0;
+        // turn on timer interrupts to start sine wave output
+        Sample_Period.attach(this, &Speaker::Sample_timer_interrupt, 1.0/(frequency*32.0));
+        // play note for specified time
+        wait(duration);
+        // turns off timer interrupts
+        Sample_Period.detach();
+        // sets output to mid range - analog zero
+        this->_pin.write_u16(32768);
+
+    }
+private:
+// sets up specified pin for analog using AnalogOut class
+    AnalogOut _pin;
+    // set up a timer to be used for sample rate interrupts
+    Ticker Sample_Period;
+
+    //variables used by interrupt routine and PlayNote
+    volatile int i;
+    short unsigned Analog_out_data[32];
+    short unsigned Analog_scaled_data[32];
+
+// Interrupt routine
+// used to output next analog sample whenever a timer interrupt occurs
+    void Sample_timer_interrupt(void) {
+        // send next analog sample out to D to A
+        this->_pin.write_u16(Analog_scaled_data[i]);
+        // increment pointer and wrap around back to 0 at 32
+        i = (i+1) & 0x01F;
+    }
+};
+
--- a/main.cpp	Mon Apr 20 04:30:14 2020 +0000
+++ b/main.cpp	Fri Apr 24 18:55:46 2020 +0000
@@ -3,9 +3,10 @@
 #include "ATParser.h"
 #include "uLCD_4DGL.h"
 #include "SDFileSystem.h"
+#include  "Speaker.h"
 //#include "wave_player.h"
 #include <string>
-#include <iostream> 
+#include <iostream>
 using namespace std;
 
 //General setups
@@ -13,10 +14,15 @@
 Serial pc(USBTX, USBRX);
 BufferedSerial ble(p13,p14);
 DigitalOut cmdstuff(p18);
+DigitalOut greenLED(p24);
+DigitalOut yellowLED(p25);
+DigitalOut redLED(p26);
 uLCD_4DGL uLCD(p28,p27,p30);
 SDFileSystem sd(p5, p6, p7, p8, "sd");
+//Speaker mySpeaker(p21);
 
 //AT command data handlers
+bool datalogged = 0;
 char delimiter[] = "\r\n";
 int buffer_size = 256;
 int timeout = 100;
@@ -25,26 +31,31 @@
 char buffer[10];
 volatile int risk_level = 0;
 
-//RTOS
+//RTOS Mutex Lock
 Mutex mutex_lock;
 
 //Global Data points and arrays
 int averageCount = 0;
 volatile int RSSI_array[15];
 
+//This portion of the code handles RSSI readings
 
-int calculate_average(volatile int *input, int size){
+int calculate_average(volatile int *input, int size)
+{
     int average;
-    for(int i = 0; i< size; i++){
+    for(int i = 0; i< size; i++) {
         average = average + input[i];
     }
     average = average/size;
     return average;
 }
 
-void parse_RSSI(){
+void parse_RSSI()
+{
+    mutex_lock.lock();
     at.send("AT+BLEGETRSSI") && at.read(buffer, 10);
-    if(buffer[0] == '-'){
+    if(buffer[0] == '-') {
+        datalogged = 1;
         pc.printf("RSSI: ");
         pc.putc(buffer[1]);
         pc.putc(buffer[2]);
@@ -52,47 +63,97 @@
         int digit1 = buffer[1] - 48;
         int digit2 = buffer[2] - 48;
         int total = 10*digit1 + digit2;
-        if (averageCount <= 15){
+        if (averageCount <= 15) {
             RSSI_array[averageCount] = total;
         }
-        averageCount++;        
-        if(averageCount > 15 && buffer[0] == '-'){
+        averageCount++;
+        if(averageCount > 15 && buffer[0] == '-') {
             averageCount = 0;
             int average = calculate_average(RSSI_array, 15);
-            if(average < 55){
+            if(average < 55) {
                 risk_level = 3;
-            }else if(average > 55 && average < 70) {
+            } else if(average > 55 && average < 70) {
                 risk_level = 2;
-            }else if(average > 70 && average < 90) {
+            } else if(average > 70 && average < 90) {
                 risk_level = 1;
-            }
-            else{
-               risk_level = 0; 
+            } else {
+                risk_level = 0;
             }
         }
         pc.printf("Risk level: ");
         pc.printf("%i\n", risk_level);
-    }else{
+    } else {
         pc.printf("Disconnected\n");
+        datalogged = 0;
+    }
+    mutex_lock.unlock();
+}
+
+//This portion of the code handles peripherals
+/*
+void speaker_alarm()
+{
+    while(1){
+        if(risk_level >= 2 && datalogged){
+            mySpeaker.PlayNote(969.0, 0.5, 1.0);
+            mySpeaker.PlayNote(800.0, 0.5, 1.0);
+        }else{
+            mySpeaker.PlayNote(0.0, 0.0, 0.0);
+        }
+    }
+}
+*/
+
+void logging_SD_card()
+{
+
+}
+
+void blink_leds()
+{
+    while(1){
+        if(risk_level <= 1 && datalogged){
+            greenLED = 1;
+            redLED = 0;
+            yellowLED = 0;
+        }else if(risk_level == 2 && datalogged){
+            yellowLED = 1;
+            greenLED = 0;
+            redLED = 0;
+        }else if(risk_level == 3 && datalogged){
+            redLED = 1;
+            yellowLED = 0;
+            greenLED = 0;
+        }else{
+            redLED = 0;
+            yellowLED = 0;
+            greenLED = 0;
+        }
     }
 }
 
-void speaker_alarm(){
- 
-}
+void display_ulcd()
+{
+    while(1){
+        mutex_lock.lock();
+        if(risk_level <= 1 && datalogged){
+            uLCD.background_color(BLACK);
+            uLCD.text_string("Safe", 1, 4, FONT_7X8, WHITE);
+        }else if(risk_level == 2 && datalogged){
+            uLCD.background_color(BLACK);
+            uLCD.text_string("Cautious 2", 1, 4, FONT_7X8, WHITE);
+        }else if(risk_level == 3 && datalogged){
+            uLCD.background_color(BLACK);
+            uLCD.text_string("Hazardous", 1, 4, FONT_7X8, WHITE);
+        }else{
+            uLCD.background_color(BLACK);
+            uLCD.text_string("ok", 1, 4, FONT_7X8, WHITE);
+        }
+        mutex_lock.unlock();
+    }
 
-void logging_SD_card(){
- 
 }
 
-void blink_leds(){
- 
-}
-
-void display_ulcd(){
-
-}
- 
 int main()
 {
     cmdstuff = 1;
@@ -100,11 +161,12 @@
     at.send("AT+AB ChangeDefaultBaud [9600]", 3) && at.recv("OK");
     pc.baud(9600);
     ble.baud(9600);
+
+    //Thread SD_Thread();
+    Thread ULCD_Thread(display_ulcd);
+    Thread LED_Thread(blink_leds);
+    //Thread Speaker_Thread(speaker_alarm);
     
-    Thread SD_Thread();
-    Thread ULCD_Thread();
-    Thread LED_Thread();
-    Thread Speaker_Thread();
     while(1) {
         parse_RSSI();
     }