Mobile Security System - Revision 1.0

Dependencies:   FXOS8700Q N5110 SDFileSystem SRF02 mbed

Files at this revision

API Documentation at this revision

Comitter:
el14dg
Date:
Wed May 04 15:33:54 2016 +0000
Parent:
8:8a3d1c07bdec
Commit message:
FINISHED v1.0

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
main.h Show annotated file Show diff for this revision Revisions of this file
diff -r 8a3d1c07bdec -r a5614f53d435 main.cpp
--- a/main.cpp	Tue May 03 09:51:02 2016 +0000
+++ b/main.cpp	Wed May 04 15:33:54 2016 +0000
@@ -72,10 +72,14 @@
         }
 
         if (g_intruder_distance_flag) { // state 5 - current distance compared to initial setting distance
+            
+            if (g_current_state == 5) {
+                
+                g_setting_distance_flag = 0;
 
-            g_setting_distance_flag = 0;
-
-            get_intruder_distance();
+                get_intruder_distance();
+                
+            }    
 
         }
 
@@ -88,7 +92,7 @@
         }
 
         if (g_led_buzzer_flag) { // toggles LED and buzzer
-
+        
             g_led_buzzer_flag = 0;
 
             led_alarm = !led_alarm;
@@ -284,7 +288,7 @@
 
         }
 
-        enter_pin(); // prints '*' to the LCD
+        enter_pin(); // enter_pin() prints '*' to the LCD
 
     }
 
@@ -298,7 +302,7 @@
 
         }
 
-        enter_pin(); // prints '*' to the LCD
+        enter_pin(); // Prints '*' to the LCD
 
     }
 
@@ -312,7 +316,7 @@
 
         }
 
-        enter_pin(); // prints '*' to the LCD
+        enter_pin(); // Prints '*' to the LCD
 
     }
 
@@ -326,13 +330,13 @@
 
         }
 
-        enter_pin(); // prints '*' to the LCD
+        enter_pin(); // Prints '*' to the LCD
 
     }
 
     else {
 
-        g_next_state = fsm[g_current_state].nextState[0]; // filters through fsm to find the next state
+        g_next_state = fsm[g_current_state].nextState[0]; // Filters through fsm to find the next state
 
     }
 
@@ -341,7 +345,7 @@
 void button_1_protocol()
 {
 
-    // states 2, 3, 6 and 7 require a pin to be entered
+    // States 2, 3, 6 and 7 require a pin to be entered
 
     if (g_current_state == 2) {
 
@@ -353,7 +357,7 @@
 
         }
 
-        enter_pin(); // prints '*' to the LCD
+        enter_pin(); // enter_pin() prints '*' to the LCD
 
     }
 
@@ -367,7 +371,7 @@
 
         }
 
-        enter_pin(); // prints '*' to the LCD
+        enter_pin(); // Prints '*' to the LCD
 
     }
 
@@ -381,7 +385,7 @@
 
         }
 
-        enter_pin(); // prints '*' to the LCD
+        enter_pin(); // Prints '*' to the LCD
 
     }
 
@@ -395,13 +399,13 @@
 
         }
 
-        enter_pin(); // prints '*' to the LCD
+        enter_pin(); // Prints '*' to the LCD
 
     }
 
     else {
 
-        g_next_state = fsm[g_current_state].nextState[1]; // filters through fsm to find the next state
+        g_next_state = fsm[g_current_state].nextState[1]; // Filters through fsm to find the next state
 
     }
 
@@ -410,8 +414,8 @@
 void button_c_protocol()
 {
 
-    // states 2, 3, 6 and 7 require a pin to be entered
-    // the screen only progresses if the correct pin is entered
+    // States 2, 3, 6 and 7 require a pin to be entered
+    // The screen only progresses if the correct pin is entered
 
     if (g_current_state == 2) {
 
@@ -503,7 +507,7 @@
 
     led_alarm = 0;
 
-    buzzer.write(0);
+    buzzer.write(0.0);
 
     // reset counter
 
@@ -700,8 +704,6 @@
 
     lcd_border();
 
-    lcd.drawRect(1,30,15,10,0);
-
     pin_text_box();
 
     // set the LED and buzzer
@@ -767,11 +769,28 @@
 
 void pin_text_box()
 {
-    lcd.drawRect(15,30,15,10,0);    // transparent box, just outline
-    lcd.drawRect(29,30,15,10,0);    // transparent box, just outline
-    lcd.drawRect(43,30,15,10,0);    // transparent box, just outline
-    lcd.drawRect(57,30,15,10,0);    // transparent boox, just outline
-
+    if (g_current_state == 7) {
+        
+        lcd.drawRect(1,30,15,10,0);     // transparent box
+        lcd.drawRect(15,30,15,10,0);    // transparent box
+        lcd.drawRect(29,30,15,10,0);    // transparent box
+        lcd.drawRect(43,30,15,10,0);    // transparent box
+        lcd.drawRect(57,30,15,10,0);    // transparent box
+        lcd.drawRect(71,30,15,10,1);    // filled box
+        
+    }
+    
+    else {
+        
+        lcd.drawRect(1,30,15,10,1);     // filled box
+        lcd.drawRect(15,30,15,10,0);    // transparent box
+        lcd.drawRect(29,30,15,10,0);    // transparent box
+        lcd.drawRect(43,30,15,10,0);    // transparent box
+        lcd.drawRect(57,30,15,10,0);    // transparent box
+        lcd.drawRect(71,30,15,10,1);    // filled box
+        
+    }        
+        
 }
 
 void screen_progression()
@@ -1107,13 +1126,15 @@
 
 }
 
-void device_tampered()
+void state_1_transition()
 {
     g_next_state = 1;
 
     screen_selection();
 
     g_current_state = 1;
+    
+    alerts.detach();
 
 }
 
@@ -1141,7 +1162,7 @@
 
     pc.printf("X1 = %.4f \nY1 = %.4f \nZ1 = %.4f \n",acc_X,acc_Y,acc_Z);
 
-    if (abs(acc_X) < 0.2f*abs(setting_acc_X)) {
+    if (abs(acc_X) < 0.2f*abs(setting_acc_X)) { // Uses abs() due to possibility of axis dat being negative
         
         device_tampered_protocol();
         
@@ -1149,7 +1170,7 @@
         
     }
     
-    else if (abs(acc_X) > 5.0f*abs(setting_acc_X)) {
+    else if (abs(acc_X) > 5.0f*abs(setting_acc_X)) { // Uses abs() due to possibility of axis dat being negative
         
         device_tampered_protocol();
         
@@ -1157,7 +1178,7 @@
         
     }    
     
-    else if (abs(acc_Y) < 0.2f*abs(setting_acc_Y)) {
+    else if (abs(acc_Y) < 0.2f*abs(setting_acc_Y)) { // Uses abs() due to possibility of axis dat being negative
         
         device_tampered_protocol();
         
@@ -1165,7 +1186,7 @@
 
     }
     
-    else if (abs(acc_Y) > 5.0f*abs(setting_acc_Y)) {
+    else if (abs(acc_Y) > 5.0f*abs(setting_acc_Y)) { // Uses abs() due to possibility of axis dat being negative
         
         device_tampered_protocol();
         
@@ -1173,7 +1194,7 @@
         
     } 
     
-    else if (abs(acc_Z) < 0.2f*abs(setting_acc_Z)) {
+    else if (abs(acc_Z) < 0.2f*abs(setting_acc_Z)) { // Uses abs() due to possibility of axis dat being negative
         
         device_tampered_protocol();
         
@@ -1181,7 +1202,7 @@
 
     }
     
-    else if (abs(acc_Z) > 5.0f*abs(setting_acc_Z)) {
+    else if (abs(acc_Z) > 5.0f*abs(setting_acc_Z)) { // Uses abs() due to possibility of axis dat being negative
         
         device_tampered_protocol();
         
@@ -1191,19 +1212,23 @@
     
     else {
         
+        // Do nothing
+        
     }    
 
 }
 
 void device_tampered_protocol()
 {
-    setting_distance.detach();
+    setting_distance.detach(); // stop reading distance
 
-    setting_screen.detach();
+    setting_screen.detach(); // stop setting screen animation
     
-    accelerometer.detach();
+    accelerometer.detach(); // stop taking readings from the accelerometer
 
-    transition.detach();
+    transition.detach(); // cancel scheduled transition to state 5
+    
+    alerts.detach(); // turn off LED and Buzzer
 
     lcd.clear();
 
@@ -1216,8 +1241,10 @@
     lcd_border();
 
     lcd.refresh();
-
-    tamper_transition.attach(&device_tampered,3);
+    
+    alerts.attach(&led_buzzer_isr,0.35);
+    
+    tamper_transition.attach(&state_1_transition,3); // transition to state 1 due to the device been tampered with whilst setting
 
 }
 
@@ -1260,7 +1287,7 @@
 {
     for (int i = 0; i < 4; i++) {
 
-        entered_pin[i] = -1;
+        entered_pin[i] = -1; // sets each element of entered_pin to -1
 
     }
 
diff -r 8a3d1c07bdec -r a5614f53d435 main.h
--- a/main.h	Tue May 03 09:51:02 2016 +0000
+++ b/main.h	Wed May 04 15:33:54 2016 +0000
@@ -3,7 +3,7 @@
 @brief Header file containing functions prototypes, defines and global variables.
 @brief Revision 1.0.
 @author Daniel Gibbons
-@date   March 2016
+@date   May 2016
 */
 
 #ifndef MAIN_H
@@ -127,7 +127,7 @@
     {5,6,5,5}, // 5 - alarm activated
     {6,6,1,5}, // 6 - deactivate without triggering (enter password)
     {7,7,1,8}, // 7 - alarm triggered (enter password)
-    {8,8,1,1}   // 8 - alarm triggered
+    {8,1,8,8}   // 8 - alarm triggered
 };
 
 FILE *pin; /*!< File pointer */
@@ -174,9 +174,9 @@
 char buffer[14]; /*!< Stores strings that are comprised of variables that are going to be displayed on the LCD */
 int length; /*!< Stores the length of strings that are comprised of variables that are going to be displayed on the LCD */
 
-
-// Initialisation Functions
-
+///
+/// Initialisation Functions
+///
 void error(); /*!< Hangs flashing on_board LED */
   
 void init_serial(); /*!< Set-up the serial port */
@@ -186,9 +186,9 @@
 void init_buttons(); /*!< Set-up the three external buttons */
 
 void init_variables(); /*!< Initialises all variables to zero */
-
-// Interrupt Service Routines (ISRs)
-
+///
+/// Interrupt Service Routines (ISRs)
+///
 void setting_distance_isr(); /*!< Interrupt Service Routine that triggers when setting_distance ticker fires */
 
 void intruder_distance_isr(); /*!< Interrupt Service Routine that triggers when intruder_distance ticker fires */
@@ -206,17 +206,17 @@
 void pin_timeout_isr(); /*!< Interrupt Service Routine that triggers when pin_timeout ticker fires */
 
 void setting_screen_isr(); /*!< Interrupt Service Routine that triggers when setting_screen ticker fires */
-
-// Button Functions
-
+///
+/// Button Functions
+///
 void button_0_protocol(); /*!< Protocol when button_0 is pressed */
 
 void button_1_protocol(); /*!< Protocol when button_1 is pressed */
 
 void button_c_protocol(); /*!< Protocol when button_c is pressed */
-
-// State Functions
-
+///
+/// State Functions
+///
 void state_0_screen(); /*!< Sets the screen and the necessary tickers and timeouts for state 0 */
 
 void state_1_screen(); /*!< Sets the screen and the necessary tickers and timeouts for state 1 */
@@ -242,15 +242,15 @@
 void screen_progression(); /*!< Calls check pin to see if the entered pin is correct and from there either proceed or go back */
 
 void screen_selection(); /*!< Calls the relevant state_X_screen() depending on the value of g_next_state */
-
-// Screen Animation Functions
-
+///
+/// Screen Animation Functions
+///
 void setting_animation(); /*!< Prints the animation on the setting screen to the Nokia 5110 LCD */
 
 void timeout_protocol(); /*!< Prints timeout counter when the alarm is triggered */
-
-// Read Distance Functions
-
+///
+/// Read Distance Functions
+///
 void get_setting_distance(); /*!< Gets the distance from the SRF02 and then increments the setting_distance_counter */
 
 void get_intruder_distance(); /*!< Gets the distance from the SRF02 and then increments the intruder_distance_counter */
@@ -258,40 +258,38 @@
 void calculate_setting_distance(); /*!< Averages the past 10 distance readings and stores them in the initial_setting_distance */
 
 void calculate_intruder_distance(); /*!< Averages the past 10 distance readings and compares this value to the initial_setting_distance to detect a potential intruder */
-
-// Timeout Functions
-
+///
+/// Timeout Functions
+///
 void screen_5_transition(); /*!< Called five seconds after the setting screen is called */
 
 void pin_confirm(); /*!< Clears the screen when a pin is entered to tell the user to press button 'C' to confirm the entered pin */
 
-void device_tampered(); /*!< Sends the user back to state 1 if the device is tampered in the setting state */
+void state_1_transition(); /*!< Sends the user back to state 1 if the device is tampered in the setting state */
 
 void alarm_setting_buzz(); /*!< Turns off buzzer one second after entering state 5 */
-
-// Accelerometer Function
-
+///
+/// Accelerometer Functions
+///
 void get_axis_data(); /*!< Gets the accelerometer data from the K64F on_board accelerometer and stores them in acc_X, acc_Y and acc_Z */
 
 void compare_axis_data(); /*!< Compares the accelerometer data from the K64F on-board accelerometer with the stored values of setting_acc_X, setting_acc_Y and setting_acc_Z */ 
 
-void device_tampered_protocol(); /*!< If the device is moved whilst in the setting state this function is called */
-
-// Pin Functions
-
-
-
+void device_tampered_protocol(); /*!< If the device is moved whilst in the setting state this function is called to stop the alarm from setting */
+///
+/// Pin Functions
+///
 void enter_pin(); /*!< Prints '*' to the Nokia 5110 LCD when a pin is being entered */
 
 void reset_entered_pin(); /*!< Resets the entered_pin array to have elements with a value of -1 */
 
 void check_pin(); /*!< Compares the entered_pin to the set_pin */
 
-void change_pin(); /*!< Writes entered_pin to the SD card when a pin is entered in state 3 - adapted from SD card program by Dr. C.A. Evans */
+void change_pin(); /*!< Writes entered_pin to the SD card when a pin is entered in state 3 - adapted from 2545_SD_Card Example by Dr. C.A. Evans */
 
-void read_pin(); /*!< Reads pin from the SD card to the set_pin array - adapted from SD card program by Dr. C.A. Evans */
+void read_pin(); /*!< Reads pin from the SD card to the set_pin array - adapted from 2545_SD_Card Example by Dr. C.A. Evans */
 
-void delete_file(char filename[]); /*!< Deletes the old pin from the SD card - taken from SD card program by Dr. C.A. Evans */
+void delete_file(char filename[]); /*!< Deletes the old pin from the SD card - taken from 2545_SD_Card Example by Dr. C.A. Evans */
 
 /**