Mobile Security System - Revision 1.0

Dependencies:   FXOS8700Q N5110 SDFileSystem SRF02 mbed

Committer:
el14dg
Date:
Mon May 02 14:28:32 2016 +0000
Revision:
7:8ac5aee0c13e
Parent:
6:dc1229224d8e
Child:
8:8a3d1c07bdec
Formatted Code.; ; Still need to investigate RTC and Accelerometer

Who changed what in which revision?

UserRevisionLine numberNew contents of line
el14dg 1:3ae4192d0c25 1 /**
el14dg 1:3ae4192d0c25 2 @file main.h
el14dg 1:3ae4192d0c25 3 @brief Header file containing functions prototypes, defines and global variables.
el14dg 1:3ae4192d0c25 4 @brief Revision 1.0.
el14dg 1:3ae4192d0c25 5 @author Daniel Gibbons
el14dg 1:3ae4192d0c25 6 @date March 2016
el14dg 1:3ae4192d0c25 7 */
el14dg 1:3ae4192d0c25 8
el14dg 1:3ae4192d0c25 9 #ifndef MAIN_H
el14dg 1:3ae4192d0c25 10 #define MAIN_H
el14dg 1:3ae4192d0c25 11
el14dg 1:3ae4192d0c25 12 #define PI 3.14159265359
el14dg 1:3ae4192d0c25 13
el14dg 1:3ae4192d0c25 14 #include "mbed.h"
el14dg 1:3ae4192d0c25 15 #include "N5110.h"
el14dg 1:3ae4192d0c25 16 #include "SRF02.h"
el14dg 3:70a7b64fbd98 17 #include "SDFileSystem.h"
el14dg 1:3ae4192d0c25 18
el14dg 2:e504a3cfe113 19
el14dg 1:3ae4192d0c25 20 /**
el14dg 2:e504a3cfe113 21 @namespace lcd
el14dg 2:e504a3cfe113 22 @brief N5110 LCD connections
el14dg 1:3ae4192d0c25 23 @namespace srf02
el14dg 2:e504a3cfe113 24 @brief SRF02 sensor connections
el14dg 6:dc1229224d8e 25 @namespace sd
el14dg 6:dc1229224d8e 26 @brief SD card connections
el14dg 1:3ae4192d0c25 27 @namespace pc
el14dg 2:e504a3cfe113 28 @brief UART connection to PC for debugging
el14dg 2:e504a3cfe113 29 @namespace led_alarm
el14dg 2:e504a3cfe113 30 @brief LED to indicate status of the alarm: flashing -> setting or triggered ; constant -> set
el14dg 2:e504a3cfe113 31 @namespace buzzer
el14dg 6:dc1229224d8e 32 @brief Indicates status of the alarm: buzzes when alarm is setting and when triggered
el14dg 6:dc1229224d8e 33 @namespace button_0
el14dg 6:dc1229224d8e 34 @brief First button interrupt
el14dg 6:dc1229224d8e 35 @namespace button_1
el14dg 6:dc1229224d8e 36 @brief Second button interrupt
el14dg 6:dc1229224d8e 37 @namespace button_c
el14dg 6:dc1229224d8e 38 @brief Third button interrupt
el14dg 2:e504a3cfe113 39 @namespace sw2
el14dg 2:e504a3cfe113 40 @brief K64F on-board switch
el14dg 2:e504a3cfe113 41 @namespace sw3
el14dg 2:e504a3cfe113 42 @brief K64F on-board switch
el14dg 6:dc1229224d8e 43 @namespace setting_distance
el14dg 6:dc1229224d8e 44 @brief This ticker fires to read the distance when the system is setting
el14dg 6:dc1229224d8e 45 @namespace intruder_distance
el14dg 6:dc1229224d8e 46 @brief This ticker fires to read the distance when the system is set
el14dg 6:dc1229224d8e 47 @namespace alerts
el14dg 6:dc1229224d8e 48 @brief This ticker fires to turn and off the led and buzzer
el14dg 6:dc1229224d8e 49 @namespace pin_timeout
el14dg 6:dc1229224d8e 50 @brief This ticker fires to timeout the user if they don't enter the pin within an alotted time of triggering the device
el14dg 6:dc1229224d8e 51 @namespace setting_screen
el14dg 6:dc1229224d8e 52 @brief This ticker fires to display the setting animation on the setting screen
el14dg 6:dc1229224d8e 53 @namespace transition
el14dg 6:dc1229224d8e 54 @brief This timeout transitions the system to the set state five seconds after entering the setting state
el14dg 6:dc1229224d8e 55 @namespace buzz
el14dg 6:dc1229224d8e 56 @brief This timeout turns off the buzzer one second after the device is set
el14dg 6:dc1229224d8e 57 @namespace confirm
el14dg 6:dc1229224d8e 58 @brief This timeout transitions to a screen asking the user to confirm the pin that they have just entered
el14dg 1:3ae4192d0c25 59 @namespace r_led
el14dg 2:e504a3cfe113 60 @brief K64F on-board red LED
el14dg 1:3ae4192d0c25 61 @namespace g_led
el14dg 2:e504a3cfe113 62 @brief K64F on-board green LED
el14dg 1:3ae4192d0c25 63 @namespace b_led
el14dg 2:e504a3cfe113 64 @brief K64F on-board blue LED
el14dg 2:e504a3cfe113 65
el14dg 1:3ae4192d0c25 66 */
el14dg 1:3ae4192d0c25 67
el14dg 2:e504a3cfe113 68 // VCC, SCE, RST, D/C, MOSI, SCLK, LED
el14dg 2:e504a3cfe113 69 N5110 lcd(PTE26,PTA0,PTC4,PTD0,PTD2,PTD1,PTC3);
el14dg 1:3ae4192d0c25 70 SRF02 srf02(I2C_SDA,I2C_SCL);
el14dg 3:70a7b64fbd98 71 // MOSI, MISO, SCK, CS
el14dg 3:70a7b64fbd98 72 SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd");
el14dg 1:3ae4192d0c25 73 Serial pc(USBTX,USBRX);
el14dg 2:e504a3cfe113 74 PwmOut led_alarm(PTC10);
el14dg 2:e504a3cfe113 75 PwmOut buzzer(PTC11);
el14dg 2:e504a3cfe113 76 InterruptIn button_0(PTB23);
el14dg 2:e504a3cfe113 77 InterruptIn button_1(PTA2);
el14dg 2:e504a3cfe113 78 InterruptIn button_c(PTC2);
el14dg 2:e504a3cfe113 79 InterruptIn sw2(SW2);
el14dg 2:e504a3cfe113 80 InterruptIn sw3(SW3);
el14dg 3:70a7b64fbd98 81 Ticker setting_distance;
el14dg 3:70a7b64fbd98 82 Ticker intruder_distance;
el14dg 5:41cb88f47f42 83 Ticker alerts;
el14dg 5:41cb88f47f42 84 Ticker pin_timeout;
el14dg 5:41cb88f47f42 85 Ticker setting_screen;
el14dg 3:70a7b64fbd98 86 Timeout transition;
el14dg 5:41cb88f47f42 87 Timeout buzz;
el14dg 6:dc1229224d8e 88 Timeout confirm;
el14dg 1:3ae4192d0c25 89 DigitalOut r_led(LED_RED);
el14dg 1:3ae4192d0c25 90 DigitalOut g_led(LED_GREEN);
el14dg 1:3ae4192d0c25 91 DigitalOut b_led(LED_BLUE);
el14dg 2:e504a3cfe113 92
el14dg 2:e504a3cfe113 93 struct State {
el14dg 6:dc1229224d8e 94
el14dg 6:dc1229224d8e 95 /* array of next states which depend on which button was pressed
el14dg 6:dc1229224d8e 96 - nextState[0] ----> button_0 pressed
el14dg 6:dc1229224d8e 97 - nextState[1] ----> button_1 pressed
el14dg 6:dc1229224d8e 98 - nextState[2] ----> button_c pressed (Option 1)
el14dg 6:dc1229224d8e 99 - nextState[3] ----> button_c pressed (Option 2)
el14dg 6:dc1229224d8e 100 */
el14dg 6:dc1229224d8e 101
el14dg 6:dc1229224d8e 102 int nextState[4];
el14dg 6:dc1229224d8e 103
el14dg 2:e504a3cfe113 104 };
el14dg 2:e504a3cfe113 105 typedef const struct State STyp;
el14dg 2:e504a3cfe113 106
el14dg 6:dc1229224d8e 107 // fsm stores all nine states that 'Mobile Security System' is comprised off
el14dg 2:e504a3cfe113 108 STyp fsm[9] = {
el14dg 3:70a7b64fbd98 109 {0,0,0,0}, // 0 - initialisation (title screen)
el14dg 5:41cb88f47f42 110 {2,3,1,1}, // 1 - main menu (Set alarm or set new password)
el14dg 4:9cac024c057c 111 {2,2,4,1}, // 2 - set alarm (enter password)
el14dg 4:9cac024c057c 112 {3,3,1,1}, // 3 - set new password (enter new password)
el14dg 3:70a7b64fbd98 113 {4,4,4,4}, // 4 - setting calibration
el14dg 6:dc1229224d8e 114 {5,6,5,5}, // 5 - alarm activated
el14dg 3:70a7b64fbd98 115 {6,6,1,5}, // 6 - deactivate without triggering (enter password)
el14dg 3:70a7b64fbd98 116 {7,7,1,8}, // 7 - alarm triggered (enter password)
el14dg 6:dc1229224d8e 117 {8,8,1,1} // 8 - alarm triggered
el14dg 2:e504a3cfe113 118 };
el14dg 2:e504a3cfe113 119
el14dg 6:dc1229224d8e 120 FILE *pin; /*!< file pointer */
el14dg 3:70a7b64fbd98 121
el14dg 6:dc1229224d8e 122 int entered_pin[4]; /*!< pin user enters */
el14dg 6:dc1229224d8e 123 int set_pin[4]; /*!< pin that is saved to SD card and this is the current pin for the system */
el14dg 6:dc1229224d8e 124 int index_array[4]; /*!< array to index the reading from the SD card */
el14dg 3:70a7b64fbd98 125
el14dg 6:dc1229224d8e 126 int pin_counter; /*!< increments to store the entered pin */
el14dg 6:dc1229224d8e 127 int incorrect_pin_flag; /*!< if the entered pin doesn't match the set pin this flag is incremented to 1 */
el14dg 3:70a7b64fbd98 128
el14dg 6:dc1229224d8e 129 int g_current_state; /*!< the current state of the system */
el14dg 6:dc1229224d8e 130 int g_next_state; /*!< the next state of the system */
el14dg 2:e504a3cfe113 131
el14dg 3:70a7b64fbd98 132 float distance[10]; /*!< Stores 10 distance readings from SRF02 */
el14dg 6:dc1229224d8e 133 float one_second_distance; /*!< the total of all 10 distance readings taken in a second */
el14dg 6:dc1229224d8e 134 double one_second_avg_distance; /*!< one_second_distance divided by 10 */
el14dg 6:dc1229224d8e 135 double initial_setting_distance; /*!< the one_second_avg_distance calculated when in the setting state (state 4) */
el14dg 2:e504a3cfe113 136
el14dg 6:dc1229224d8e 137 int setting_distance_counter; /*!< increments to 10 distance readings can be stored over 1 second */
el14dg 6:dc1229224d8e 138 int intruder_distance_counter; /*!< increments to 10 distance readings can be stored over 1 second */
el14dg 6:dc1229224d8e 139 int pin_timeout_counter; /*!< increments to 20 to enable the 20 second timeout when the alarm is triggered */
el14dg 6:dc1229224d8e 140 int setting_alarm_counter; /*!< increments in steps of five and is used for the setting screen animation */
el14dg 5:41cb88f47f42 141
el14dg 6:dc1229224d8e 142 int seconds_till_timeout; /*!< 21 - pin_timeout_counter */
el14dg 3:70a7b64fbd98 143
el14dg 3:70a7b64fbd98 144 volatile int g_setting_distance_flag; /*!< Flag in setting_distance_isr */
el14dg 3:70a7b64fbd98 145 volatile int g_intruder_distance_flag; /*!< Flag in intruder_distance_isr */
el14dg 5:41cb88f47f42 146 volatile int g_led_buzzer_flag; /*!< Flag in led_buzzer_isr */
el14dg 6:dc1229224d8e 147 volatile int g_pin_timeout_flag; /*!< Flag in pin_timeout_isr */
el14dg 6:dc1229224d8e 148 volatile int g_setting_screen_flag; /*!< Flag in setting_screen_isr */
el14dg 2:e504a3cfe113 149 volatile int g_button_0_flag; /*!< Flag in button_0_isr */
el14dg 2:e504a3cfe113 150 volatile int g_button_1_flag; /*!< Flag in button_1_isr */
el14dg 2:e504a3cfe113 151 volatile int g_button_c_flag; /*!< Flag in button_c_isr */
el14dg 2:e504a3cfe113 152
el14dg 6:dc1229224d8e 153 int setting_distance_ticker_status; /*!< if the setting_distance ticker has been attached this int is equal to 1 */
el14dg 3:70a7b64fbd98 154
el14dg 6:dc1229224d8e 155 char buffer[14]; /*!< Stores strings that are comprised of variables that are going to be displayed on the LCD */
el14dg 6:dc1229224d8e 156 int length; /*!< Stores the length of strings that are comprised of variables that are going to be displayed on the LCD */
el14dg 1:3ae4192d0c25 157
el14dg 2:e504a3cfe113 158
el14dg 7:8ac5aee0c13e 159 // Initialisation Functions
el14dg 7:8ac5aee0c13e 160
el14dg 2:e504a3cfe113 161 void error(); /*!< Hangs flashing on_board LED */
el14dg 2:e504a3cfe113 162
el14dg 2:e504a3cfe113 163 void init_serial(); /*!< Set-up the serial port */
el14dg 2:e504a3cfe113 164
el14dg 2:e504a3cfe113 165 void init_K64F(); /*!< Set-up the on-board LEDs and switches */
el14dg 2:e504a3cfe113 166
el14dg 2:e504a3cfe113 167 void init_buttons(); /*!< Set-up the three external buttons */
el14dg 2:e504a3cfe113 168
el14dg 6:dc1229224d8e 169 void init_variables(); /*!< Initialises all variables to zero */
el14dg 5:41cb88f47f42 170
el14dg 7:8ac5aee0c13e 171 // Interrupt Service Routines (ISRs)
el14dg 7:8ac5aee0c13e 172
el14dg 6:dc1229224d8e 173 void setting_distance_isr(); /*!< Interrupt Service Routine that triggers when setting_distance ticker fires */
el14dg 3:70a7b64fbd98 174
el14dg 6:dc1229224d8e 175 void intruder_distance_isr(); /*!< Interrupt Service Routine that triggers when intruder_distance ticker fires */
el14dg 2:e504a3cfe113 176
el14dg 6:dc1229224d8e 177 void button_0_isr(); /*!< Interrupt Service Routine that triggers when button_0 is pressed by the user */
el14dg 6:dc1229224d8e 178
el14dg 6:dc1229224d8e 179 void button_1_isr(); /*!< Interrupt Service Routine that triggers when button_1 is pressed by the user */
el14dg 2:e504a3cfe113 180
el14dg 6:dc1229224d8e 181 void button_c_isr(); /*!< Interrupt Service Routine that triggers when button_c is pressed by the user */
el14dg 1:3ae4192d0c25 182
el14dg 6:dc1229224d8e 183 void led_buzzer_isr(); /*!< Interrupt Service Routine that triggers when alerts ticker fires */
el14dg 2:e504a3cfe113 184
el14dg 6:dc1229224d8e 185 void pin_timeout_isr(); /*!< Interrupt Service Routine that triggers when pin_timeout ticker fires */
el14dg 5:41cb88f47f42 186
el14dg 6:dc1229224d8e 187 void setting_screen_isr(); /*!< Interrupt Service Routine that triggers when setting_screen ticker fires */
el14dg 5:41cb88f47f42 188
el14dg 7:8ac5aee0c13e 189 // Button Functions
el14dg 7:8ac5aee0c13e 190
el14dg 6:dc1229224d8e 191 void button_0_protocol(); /*!< Protocol when button_0 is pressed */
el14dg 5:41cb88f47f42 192
el14dg 6:dc1229224d8e 193 void button_1_protocol(); /*!< Protocol when button_1 is pressed */
el14dg 4:9cac024c057c 194
el14dg 6:dc1229224d8e 195 void button_c_protocol(); /*!< Protocol when button_c is pressed */
el14dg 3:70a7b64fbd98 196
el14dg 7:8ac5aee0c13e 197 // State Functions
el14dg 3:70a7b64fbd98 198
el14dg 6:dc1229224d8e 199 void state_0_screen(); /*!< Sets the screen and the necessary tickers and timeouts for state 0 */
el14dg 2:e504a3cfe113 200
el14dg 6:dc1229224d8e 201 void state_1_screen(); /*!< Sets the screen and the necessary tickers and timeouts for state 1 */
el14dg 5:41cb88f47f42 202
el14dg 6:dc1229224d8e 203 void state_2_screen(); /*!< Sets the screen and the necessary tickers and timeouts for state 2 */
el14dg 2:e504a3cfe113 204
el14dg 6:dc1229224d8e 205 void state_3_screen(); /*!< Sets the screen and the necessary tickers and timeouts for state 3 */
el14dg 2:e504a3cfe113 206
el14dg 6:dc1229224d8e 207 void state_4_screen(); /*!< Sets the screen and the necessary tickers and timeouts for state 4 */
el14dg 3:70a7b64fbd98 208
el14dg 6:dc1229224d8e 209 void state_5_screen(); /*!< Sets the screen and the necessary tickers and timeouts for state 5 */
el14dg 2:e504a3cfe113 210
el14dg 6:dc1229224d8e 211 void state_6_screen(); /*!< Sets the screen and the necessary tickers and timeouts for state 6 */
el14dg 2:e504a3cfe113 212
el14dg 6:dc1229224d8e 213 void state_7_screen(); /*!< Sets the screen and the necessary tickers and timeouts for state 7 */
el14dg 2:e504a3cfe113 214
el14dg 6:dc1229224d8e 215 void state_8_screen(); /*!< Sets the screen and the necessary tickers and timeouts for state 8 */
el14dg 5:41cb88f47f42 216
el14dg 7:8ac5aee0c13e 217 void lcd_border(); /*!< Prints a border to the Nokia 5110 LCD */
el14dg 7:8ac5aee0c13e 218
el14dg 7:8ac5aee0c13e 219 void pin_text_box(); /*!< Prints a box to the Nokia 5110 LCD for the states where a pin is required */
el14dg 7:8ac5aee0c13e 220
el14dg 6:dc1229224d8e 221 void screen_selection(); /*!< Calls the relevant state_X_screen() depending on the value of g_next_state */
el14dg 5:41cb88f47f42 222
el14dg 7:8ac5aee0c13e 223 // Screen Animation Functions
el14dg 7:8ac5aee0c13e 224
el14dg 6:dc1229224d8e 225 void setting_animation(); /*!< Prints the animation on the setting screen to the Nokia 5110 LCD */
el14dg 2:e504a3cfe113 226
el14dg 7:8ac5aee0c13e 227 void timeout_protocol(); /*!< Prints timeout counter when the alarm is triggered */
el14dg 1:3ae4192d0c25 228
el14dg 7:8ac5aee0c13e 229 // Read Distance Functions
el14dg 3:70a7b64fbd98 230
el14dg 6:dc1229224d8e 231 void get_setting_distance(); /*!< Gets the distance from the SRF02 and then increments the setting_distance_counter */
el14dg 3:70a7b64fbd98 232
el14dg 6:dc1229224d8e 233 void get_intruder_distance(); /*!< Gets the distance from the SRF02 and then increments the intruder_distance_counter */
el14dg 3:70a7b64fbd98 234
el14dg 6:dc1229224d8e 235 void calculate_setting_distance(); /*!< Averages the past 10 distance readings and stores them in the initial_setting_distance */
el14dg 3:70a7b64fbd98 236
el14dg 6:dc1229224d8e 237 void calculate_intruder_distance(); /*!< Averages the past 10 distance readings and compares this value to the initial_setting_distance to detect a potential intruder */
el14dg 3:70a7b64fbd98 238
el14dg 7:8ac5aee0c13e 239 // Timeout Functions
el14dg 7:8ac5aee0c13e 240
el14dg 6:dc1229224d8e 241 void screen_5_transition(); /*!< Called five seconds after the setting screen is called */
el14dg 4:9cac024c057c 242
el14dg 6:dc1229224d8e 243 void screen_progression(); /*!< Calls check pin to see if the entered pin is correct and from there either proceed or go back */
el14dg 5:41cb88f47f42 244
el14dg 6:dc1229224d8e 245 void alarm_setting_buzz(); /*!< Turns off buzzer one second after entering state 5 */
el14dg 5:41cb88f47f42 246
el14dg 7:8ac5aee0c13e 247 // Pin Functions
el14dg 7:8ac5aee0c13e 248
el14dg 6:dc1229224d8e 249 void pin_confirm(); /*!< Clears the screen when a pin is entered to tell the user to press button 'C' to confirm the entered pin */
el14dg 5:41cb88f47f42 250
el14dg 6:dc1229224d8e 251 void enter_pin(); /*!< Prints '*' to the Nokia 5110 LCD when a pin is being entered */
el14dg 4:9cac024c057c 252
el14dg 6:dc1229224d8e 253 void reset_entered_pin(); /*!< Resets the entered_pin array to have elements with a value of -1 */
el14dg 5:41cb88f47f42 254
el14dg 6:dc1229224d8e 255 void check_pin(); /*!< Compares the entered_pin to the set_pin */
el14dg 4:9cac024c057c 256
el14dg 6:dc1229224d8e 257 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 */
el14dg 3:70a7b64fbd98 258
el14dg 6:dc1229224d8e 259 void read_pin(); /*!< Reads pin from the SD card to the set_pin array - adapted from SD card program by Dr. C.A. Evans */
el14dg 3:70a7b64fbd98 260
el14dg 6:dc1229224d8e 261 void delete_file(char filename[]); /*!< Deletes the old pin from the SD card - taken from SD card program by Dr. C.A. Evans */
el14dg 3:70a7b64fbd98 262
el14dg 1:3ae4192d0c25 263 /**
el14dg 2:e504a3cfe113 264
el14dg 1:3ae4192d0c25 265 */
el14dg 1:3ae4192d0c25 266
el14dg 1:3ae4192d0c25 267 #endif