Mobile Security System - Revision 1.0

Dependencies:   FXOS8700Q N5110 SDFileSystem SRF02 mbed

Revision:
5:41cb88f47f42
Parent:
4:9cac024c057c
Child:
6:dc1229224d8e
--- a/main.cpp	Sun Apr 17 14:23:00 2016 +0000
+++ b/main.cpp	Sun May 01 16:53:56 2016 +0000
@@ -15,42 +15,30 @@
 
 int main()
 {
+    
     init_serial();
     init_K64F();
     init_buttons();
+    init_variables();
     lcd.init();
-
-    wait(3);
     
     read_pin();
     
-    led_alarm = 0;
-
-    one_second_distance = 0;
-    one_second_avg_distance = 0;
-    initial_setting_distance = 0;
-
-    pin_counter = 0;
-    incorrect_pin_flag = 0;
+    buzzer.period(1.0/500.0); // 500 Hz
     
-    setting_distance_counter = 0;
-    intruder_distance_counter = 0;
+    buzzer.write(0.5); 
 
-    g_button_0_flag = 0;
-    g_button_1_flag = 0;
-    g_button_c_flag = 0;
-    g_setting_distance_flag = 0;
-    g_intruder_distance_flag = 0;
-
-    setting_distance_ticker_status = 0;
-
-    g_current_state = 0;
-
+    wait(2);
+    
     screen_selection();
 
     wait(4);
 
     g_next_state = 1;
+    
+    screen_selection();
+      
+    g_current_state = 1;
 
 
     while (1) {
@@ -63,6 +51,9 @@
             g_button_0_flag = 0;
 
             button_0_protocol();
+            
+            pc.printf("Current State = %d \n",g_current_state);
+            pc.printf("Next State = %d \n",g_next_state);
 
         }
 
@@ -74,6 +65,9 @@
             
             button_1_protocol();
 
+            pc.printf("Current State = %d \n",g_current_state);
+            pc.printf("Next State = %d \n",g_next_state);
+
         }
 
         if (g_button_c_flag) {
@@ -81,70 +75,87 @@
             pc.printf("g_button_c_flag = %d \n", g_button_c_flag);
 
             g_button_c_flag = 0;
-    
+
             button_c_protocol();
-            
+
+            pc.printf("Current State = %d \n",g_current_state);
+            pc.printf("Next State = %d \n",g_next_state);
+
         }
 
         if (g_setting_distance_flag) {
 
-            //pc.printf("g_setting_distance_flag = %d \n", g_setting_distance_flag);
-
             g_setting_distance_flag = 0;
 
-            //pc.printf("g_current_state = %d (setting calibration) \n", g_current_state);
-
             get_setting_distance();
 
-
-
         }
 
         if (g_intruder_distance_flag) {
 
-            //pc.printf("g_intruder_distance_flag = %d \n", g_intruder_distance_flag);
-
             g_setting_distance_flag = 0;
 
-            //pc.printf("g_current_state = %d (intruder detection) \n", g_current_state);
-
             get_intruder_distance();
 
+        }
+        
+        if (g_setting_screen_flag) {
+            
+            g_setting_screen_flag = 0;
+            
+            loading_bar();
+            
         }    
 
-        if (setting_distance_counter == 10) {
+        if (g_led_buzzer_flag) {
+
+            g_led_buzzer_flag = 0;
 
-            calculate_setting_distance();
-        }
-
-        if (intruder_distance_counter == 10) {
-
-            calculate_intruder_distance();
+            led_alarm = !led_alarm;
+            
+            if (buzzer == 0) {
+                
+                buzzer = 0.5;
+                
+            }
+            
+            else {
+                
+                buzzer = 0;
+                
+            }        
 
         }
 
-        //pc.printf("g_current_state = %d \n", g_current_state);
+        if (g_pin_timeout_flag) {
 
-        //pc.printf("g_next_state = %d \n", g_next_state);
+            g_pin_timeout_flag = 0;
 
+            timeout_protocol();
+            
+            
+        }
+            
         if (g_next_state != g_current_state) {
+            
+            pc.printf("TRANSITION \n");
 
-            screen_selection(); //  the screen is determined by the current value of g_current_state
-
+            screen_selection(); //  the screen is determined by the current value of g_next_state
+            
+            g_current_state = g_next_state;
+            
+            pc.printf("Current State = %d \n",g_current_state);
+            
         }
 
-        g_current_state = g_next_state;
-
-        //pc.printf("g_current_state_after_transition = %d \n", g_current_state);
+        
 
         if (g_current_state == 4) {
-
+            
             if (setting_distance_ticker_status == 0) {
 
                 setting_distance_ticker_status = 1;
 
-                //pc.printf("g_current_state = %d (attach setting ticker) \n", g_current_state);
-
                 setting_distance.attach(&setting_distance_isr,0.1);
                 
             }
@@ -153,11 +164,7 @@
 
         if (g_current_state == 5) {
 
-                //pc.printf("g_current_state = %d (attach intruder ticker) \n", g_current_state);
-
                 intruder_distance.attach(&intruder_distance_isr,0.1);
-                
-                led_alarm = 1;
 
         }
         
@@ -165,11 +172,11 @@
             
             intruder_distance.detach();
             
-            led_alarm = 0;
-            
         }    
+        
 
         sleep();
+        
 
     }
 
@@ -209,6 +216,32 @@
 
 }
 
+void init_variables()
+{
+
+    one_second_distance = 0;
+    one_second_avg_distance = 0;
+    initial_setting_distance = 0;
+
+    pin_counter = 0;
+    incorrect_pin_flag = 0;
+
+    setting_distance_counter = 0;
+    intruder_distance_counter = 0;
+
+    g_button_0_flag = 0;
+    g_button_1_flag = 0;
+    g_button_c_flag = 0;
+    g_setting_distance_flag = 0;
+    g_intruder_distance_flag = 0;
+    g_pin_timeout_flag = 0;
+
+    setting_distance_ticker_status = 0;
+
+    g_current_state = 0;
+
+}
+
 void setting_distance_isr()
 {
     g_setting_distance_flag = 1;
@@ -221,9 +254,15 @@
 
 }
 
-void led_isr() 
+void led_buzzer_isr()
 {
-    g_led_flag = 0;
+    g_led_buzzer_flag = 1;
+
+}
+
+void setting_screen_isr()
+{
+    g_setting_screen_flag = 1;
     
 }    
 
@@ -245,6 +284,54 @@
 
 }
 
+void pin_timeout_isr()
+{
+    g_pin_timeout_flag = 1;
+
+}
+
+void loading_bar()
+{
+    setting_alarm_counter = setting_alarm_counter + 5;
+    
+    if (setting_alarm_counter < 101) {    
+                            
+        lcd.drawRect(0,20,setting_alarm_counter,5,1);
+        
+        if (setting_alarm_counter < 10) {
+            
+            length = sprintf(buffer,"0%d%%",setting_alarm_counter);
+            
+        }
+        
+        else {    
+            
+            length = sprintf(buffer,"%d%%",setting_alarm_counter);
+            
+        }    
+    
+        if (length <= 14) {
+            
+            if (setting_alarm_counter < 100) {
+                
+                lcd.printString(buffer,33,4);
+                
+            }
+            
+            else {
+                
+                lcd.printString(buffer,30,4);    
+                
+            }    
+        
+        }    
+    
+        lcd.refresh();
+        
+        
+    }    
+}    
+
 void button_0_protocol()
 {
     if (g_current_state == 2) {
@@ -256,7 +343,7 @@
             pin_counter++;
 
         }
-        
+
         enter_pin();
 
     }
@@ -270,7 +357,7 @@
             pin_counter++;
 
         }
-        
+
         enter_pin();
 
     }
@@ -284,7 +371,7 @@
             pin_counter++;
 
         }
-        
+
         enter_pin();
 
     }
@@ -298,7 +385,7 @@
             pin_counter++;
 
         }
-        
+
         enter_pin();
 
     }
@@ -323,7 +410,7 @@
             pin_counter++;
 
         }
-        
+
         enter_pin();
 
     }
@@ -337,7 +424,7 @@
             pin_counter++;
 
         }
-        
+
         enter_pin();
 
     }
@@ -351,7 +438,7 @@
             pin_counter++;
 
         }
-        
+
         enter_pin();
 
     }
@@ -365,9 +452,9 @@
             pin_counter++;
 
         }
-        
+
         enter_pin();
-        
+
     }
 
     else {
@@ -423,6 +510,46 @@
 
         setting_distance.detach();
 
+        calculate_setting_distance();
+
+    }
+
+}
+
+void timeout_protocol()
+{
+
+    pin_timeout_counter++;
+
+    seconds_till_timeout = 20 - pin_timeout_counter;
+
+    if (seconds_till_timeout == 0) {
+
+        g_next_state = 8;
+
+        pin_timeout_counter = 0;
+
+    }
+
+    else {
+
+        if (seconds_till_timeout >= 10) {
+
+            length = sprintf(buffer,"%d",seconds_till_timeout);
+
+        }
+
+        else {
+
+            length = sprintf(buffer,"0%d",seconds_till_timeout);
+
+        }
+
+        if (length <= 14)
+            lcd.printString(buffer,3,4);
+
+        lcd.refresh();
+
     }
 
 }
@@ -437,6 +564,8 @@
     if (intruder_distance_counter == 10) {
 
         intruder_distance.detach();
+        
+        calculate_intruder_distance();
 
     }
 
@@ -481,7 +610,7 @@
     one_second_distance = 0;
 
 
-    if (one_second_avg_distance > (1.5*initial_setting_distance)) {
+    if (one_second_avg_distance > (1.05*initial_setting_distance)) {
 
         g_next_state = 7;
 
@@ -491,7 +620,7 @@
 
     }
 
-    else if (one_second_avg_distance < (0.5 * initial_setting_distance)) {
+    else if (one_second_avg_distance < (0.95 * initial_setting_distance)) {
 
         g_next_state = 7;
 
@@ -511,17 +640,22 @@
 
 }
 
-
 void state_0_screen()
 {
     lcd.clear();
 
-    lcd.printString("MobileSecurity",0,0);
+    lcd.printString("Mobile",27,1);
+    
+    lcd.printString("Security",18,2);
 
-    lcd.printString("System",27,1);
-
-    lcd.printString("Daniel Gibbons",0,4);
-
+    lcd.printString("System",27,3);
+    
+    lcd_border();
+    
+    led_alarm = 0;  
+    
+    buzzer.write(0.0);
+    
     lcd.refresh();
 
 }
@@ -533,19 +667,45 @@
     lcd.printString("Set Alarm",15,1);
 
     lcd.printString("Set New Pin",9,4);
-
+    
+    lcd_border();
+    
+    for (int i = 0; i < WIDTH; i++) {
+        
+        lcd.setPixel(i,24);
+        
+    } 
+            
+    pin_timeout.detach(); 
+    
+    alerts.detach();
+    
+    led_alarm = 0;  
+    
+    buzzer.write(0);
+    
+    pin_timeout_counter = 0;
+    
     lcd.refresh();
 
 }
 
-void state_2_6_7_screen()
+void state_2_screen()
 {
     lcd.clear();
 
     lcd.printString("Enter 4 Digit",3,1);
 
     lcd.printString("Pin Below",15,2);
-
+    
+    pin_text_box();
+    
+    lcd_border();
+    
+    led_alarm = 0;  
+    
+    buzzer.write(0.0);
+    
     lcd.refresh();
 
 }
@@ -557,7 +717,15 @@
     lcd.printString("Enter New 4",9,1);
 
     lcd.printString("Digit Pin",15,2);
+    
+    pin_text_box();
 
+    lcd_border();
+    
+    led_alarm = 0;  
+    
+    buzzer.write(0.0);
+    
     lcd.refresh();
 
 }
@@ -567,6 +735,22 @@
     lcd.clear();
 
     lcd.printString("Setting",21,1);
+    
+    lcd_border();
+    
+    lcd.drawRect(0,20,84,5,0);
+        
+    lcd.printString("0%",41,4);   
+    
+    setting_alarm_counter = 0;
+    
+    led_alarm = 0;
+    
+    buzzer.write(0.0);
+    
+    alerts.attach(&led_buzzer_isr,0.5);
+    
+    setting_screen.attach(&setting_screen_isr,0.25);
 
     lcd.refresh();
 
@@ -579,6 +763,74 @@
     lcd.printString("Alarm Set",15,1);
 
     lcd.printString("DEACTIVATE?",9,4);
+    
+    lcd_border();
+    
+    for (int i = 0; i < WIDTH; i++) {
+        
+        lcd.setPixel(i,20);
+        
+    } 
+    
+    alerts.detach();
+    
+    setting_screen.detach();
+    
+    led_alarm = 1;
+    
+    buzzer.write(0.5);
+    
+    buzz.attach(&alarm_setting_buzz,1);
+
+    lcd.refresh();
+    
+}
+
+void state_6_screen()
+{
+    lcd.clear();
+
+    lcd.printString("DEACTIVATE",12,1);
+
+    lcd.printString("Enter Pin",15,2);
+    
+    lcd_border();
+    
+    pin_text_box();
+    
+    led_alarm = 0;
+    
+    buzzer.write(0.0);
+    
+    alerts.attach(&led_buzzer_isr,0.5);
+
+    lcd.refresh();
+
+}
+
+void state_7_screen()
+{
+    lcd.clear();
+
+    lcd.printString("INTRUDER",18,1);
+
+    lcd.printString("Enter Pin",15,2);
+    
+    lcd.printString("20",3,4);
+    
+    lcd_border();
+    
+    lcd.drawRect(1,30,15,10,0); 
+    
+    pin_text_box();
+    
+    led_alarm = 0;
+    
+    buzzer.write(0.0);
+    
+    alerts.attach(&led_buzzer_isr,0.5);
+    
+    pin_timeout.attach(&pin_timeout_isr,1);
 
     lcd.refresh();
 
@@ -591,36 +843,44 @@
     lcd.printString("ALARM",27,1);
 
     lcd.printString("TRIGGERED",15,2);
+    
+    lcd.printString("Menu?",27,4);
+    
+    lcd_border();
+    
+    led_alarm = 0;
+    
+    buzzer.write(0.0);
+    
+    alerts.detach();
+      
+    pin_timeout.detach();
+    
+    alerts.attach(&led_buzzer_isr,0.2);
 
     lcd.refresh();
 }
 
 void screen_selection()
 {
-
-    pin_counter = 0;
+    
+    reset_entered_pin();
 
     if (g_next_state == 0) {
 
         state_0_screen();
 
-        lcd.printString("0",0,0);
-
     }
 
     else if (g_next_state == 1) {
 
         state_1_screen();
 
-        lcd.printString("1",0,0);
-
     }
 
     else if (g_next_state == 2) {
 
-        state_2_6_7_screen();
-
-        lcd.printString("2",0,0);
+        state_2_screen();
 
     }
 
@@ -628,39 +888,29 @@
 
         state_3_screen();
 
-        lcd.printString("3",0,0);
-
     }
 
     else if (g_next_state == 4) {
 
         state_4_screen();
 
-        lcd.printString("4",0,0);
-
     }
 
     else if (g_next_state == 5) {
 
         state_5_screen();
 
-        lcd.printString("5",0,0);
-
     }
 
     else if (g_next_state == 6) {
 
-        state_2_6_7_screen();
-
-        lcd.printString("6",0,0);
+        state_6_screen();
 
     }
 
     else if (g_next_state == 7) {
 
-        state_2_6_7_screen();
-
-        lcd.printString("7",0,0);
+        state_7_screen();
 
     }
 
@@ -668,12 +918,8 @@
 
         state_8_screen();
 
-        lcd.printString("8",0,0);
-
     }
-
-
-
+    
 }
 
 void screen_5_transition()
@@ -685,13 +931,9 @@
     g_current_state = 5;
     
     setting_distance_ticker_status = 0;
-    
-    led.detach();
 
     intruder_distance.attach(&intruder_distance_isr,0.1);
     
-    led_alarm = 1;
-    
 }
 
 void screen_progression() 
@@ -738,13 +980,37 @@
     
     }
     
-}        
+} 
+
+void alarm_setting_buzz()
+{
+    
+    buzzer.write(0.0);
+    
+}    
+
+void lcd_border()
+{
+    lcd.drawRect(0,0,83,47,0);  
+    
+    lcd.drawRect(1,1,81,45,0); 
+   
+}
+
+void pin_text_box()
+{
+    lcd.drawRect(15,30,15,10,0);    // transparent, just outline
+    lcd.drawRect(29,30,15,10,0);    // transparent, just outline
+    lcd.drawRect(43,30,15,10,0);    // transparent, just outline
+    lcd.drawRect(57,30,15,10,0);    // transparent, just outline
+    
+}           
 
 void enter_pin()
 {
     if (pin_counter == 1) {
         
-        lcd.printString("*",19,4);
+        lcd.printString("*",20,4);
         
     }
     
@@ -756,13 +1022,13 @@
     
     else if (pin_counter == 3) {
         
-        lcd.printString("*",49,4);
+        lcd.printString("*",48,4);
         
     }
     
     else if (pin_counter == 4) {
         
-        lcd.printString("*",64,4);
+        lcd.printString("*",62,4);
         
     }                
     
@@ -782,11 +1048,21 @@
             
         }
         
-        entered_pin[i] = -1;
-        
     }
     
-}    
+}
+
+void reset_entered_pin()
+{
+        for (int i = 0; i < 4; i++) {
+            
+            entered_pin[i] = -1;
+            
+        }
+        
+         pin_counter = 0;
+        
+}                
         
 void change_pin()
 {