Level 2 Project Range Device

Dependencies:   N5110 SDFileSystem SRF02 TMP102 mbed

Fork of Ranger by Philip Thompson

Revision:
2:329597081c06
Parent:
1:5b991c2302e1
Child:
3:8782b8b8658b
--- a/main.cpp	Tue Mar 22 20:00:59 2016 +0000
+++ b/main.cpp	Fri Apr 01 23:09:07 2016 +0000
@@ -4,27 +4,27 @@
 Embedded System Project
 EL2645
 
-
- Have added the N5110 Libary and set up a a basic disply of the Range reading
- as well as added LED indictors to range
 */
 
-
 #include "mbed.h"
 #include "SRF02.h"
 #include "N5110.h"
 
-BusOut output(PTB2,PTB3,PTB10,PTB11,PTC11,PTC10);
+#define LOW 0
+#define HIGH 1
+
+DigitalOut rr_led (PTA1);
+DigitalOut a_led (PTC2);
+DigitalOut gg_led(PTB23);
 
 // Ranger object
 SRF02 srf02(I2C_SDA,I2C_SCL);
 
-// UART connection for PC
-Serial pc(USBTX,USBRX);
-
 //N5110 Object  VCC, SCE,RST, D/C, MOSI,SCLK, LED
 N5110           lcd(PTE26,PTA0,PTC4,PTD0,PTD2,PTD1,PTC3);
 
+Ticker ticker;
+
 // K64F on-board LEDs
 DigitalOut r_led(LED_RED);
 DigitalOut g_led(LED_GREEN);
@@ -34,81 +34,115 @@
 InterruptIn sw2(SW2);
 InterruptIn sw3(SW3);
 
+volatile int g_timer_flag = 0;
+
 int distance;
 int length;
-int alert
+int alert;
+
+char buffer[14];  // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
 
-// setup serial port
-void init_serial();
+// struct for state
+struct Alertlevel {
+    char srr_led;  // output value
+    char sa_led;    // time in state
+    char sgg_led;  // array of next states
+    char frr_led;
+    char fa_led;
+    char fgg_led;
+};
+typedef const struct Alertlevel STyp;
+
+STyp Alertlevel[8] = {
+    {LOW,LOW,LOW,LOW,LOW,LOW}, // no output
+    {LOW,LOW,LOW,LOW,LOW,HIGH}, //flash green
+    {LOW,LOW,HIGH,LOW,LOW,LOW}, //steady green
+    {LOW,LOW,HIGH,LOW,HIGH,LOW}, //flash amber
+    {LOW,HIGH,HIGH,LOW,LOW,LOW}, //steady amber
+    {LOW,HIGH,HIGH,HIGH,LOW,LOW}, //flash red
+    {HIGH,HIGH,HIGH,LOW,LOW,LOW},// steady red
+    {LOW,LOW,LOW,HIGH,HIGH,HIGH} // all flash
+};
+// timed interuprt
+void timer_isr();
 
 // set up board
 void init_K64F();
 
-char buffer[14];  // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
-
 int main()
 {
     lcd.init();
-    init_serial();
     init_K64F();
-
+    ticker.attach(&timer_isr,0.05);
 
     while(1) {
 
-        // read sensor distance in cm and print over serial port
+// read sensor distance in cm and print over serial port
         int distance = srf02.getDistanceCm();
+        length = sprintf(buffer,"D = %i Cm",distance);
+        if (length <= 14)
+            lcd.printString(buffer,0,2);
+// need to refresh display after setting pixels
+        lcd.refresh();
 
-        //serial link reading of range
-        pc.printf("Distance = %i Cm\n",distance);
-
-        // short delay before next measurement
+// short delay before next measurement
         wait(0.25);
 
-        length = sprintf(buffer,"D = %i Cm",distance);
-
-        if (length <= 14)
-
-            lcd.printString(buffer,0,2);
-        // need to refresh display after setting pixels
-        lcd.refresh();
+//Range Alert selection use to adjust alert boundarys
+        if (distance >= 200 && distance < 250) {
+            alert = 1; //flash green
+        } else if (distance >= 150 && distance  < 200) {
+            alert = 2; //steady green
+        } else if (distance >= 100 && distance < 150) {
+            alert = 3; //flashing amber
+        } else if (distance >= 50 && distance < 100) {
+            alert = 4; //steady amber
+        } else if ( distance > 10 && distance < 50) {
+            alert = 5; //flashing red
+        } else if (distance > 1 && distance <= 10) {
+            alert = 6; //steady red
+        } else if (distance <=1) {
+            alert = 7; //all flashing
+        } else {
+            alert = 0; //no output
+        }
+// Steady light outputs
+        rr_led = Alertlevel[alert].srr_led;
+        a_led = Alertlevel[alert].sa_led;
+        gg_led = Alertlevel[alert].sgg_led;
 
-//Range Alert selection
-        if (distance <= 200) {
-            alert = 6;
-        } else if (150 <= distance  < 200) {
-            alert = 6;
-        } else if (100 <=distance < 150) {
-            alert = 5;
-        } else if (50 <= distance < 100) {
-            alert = 4;
-        } else if ( 10 <=distance < 150) {
-            alert = 3;
-        } else if (distance <10) {
-            alert = 2;
-        } else {
-            output = 1;
+//Flashing light outputs
+        if (g_timer_flag) {
+            g_timer_flag = 0;  // if it has, clear the flag
+
+            if ( Alertlevel[alert].frr_led == HIGH) {
+                rr_led = !rr_led;
+            } else if ( Alertlevel[alert].fa_led == HIGH) {
+                a_led = !a_led;
+            } else if ( Alertlevel[alert].fgg_led == HIGH) {
+                gg_led = !gg_led;
+            }
         }
     }
 
 }
 
-
-// Used to return a range readig thru a serial connection
-    void init_serial() {
-        // set to highest baud - ensure terminal software matches
-        pc.baud(115200);
-    }
-
-
 //Set up board switches and LEDS
-    void init_K64F() {
+void init_K64F()
+{
 //on-board LEDs are active-low, so set pin high to turn them off.
-        r_led = 1;
-        g_led = 1;
-        b_led = 1;
+    r_led = 1;
+    g_led = 1;
+    b_led = 1;
 
 // since the on-board switches have external pull-ups, we should disable the internal pull-down
 // resistors that are enabled by default using InterruptIn
-        sw2.mode(PullNone);
-        sw3.mode(PullNone);
-    }
\ No newline at end of file
+    sw2.mode(PullNone);
+    sw3.mode(PullNone);
+}
+
+
+void timer_isr()
+{
+    g_timer_flag = 1;   // set flag in ISR
+}
\ No newline at end of file