Mobile Security System - Revision 1.0
Dependencies: FXOS8700Q N5110 SDFileSystem SRF02 mbed
main.cpp@4:9cac024c057c, 2016-04-17 (annotated)
- Committer:
- el14dg
- Date:
- Sun Apr 17 14:23:00 2016 +0000
- Revision:
- 4:9cac024c057c
- Parent:
- 3:70a7b64fbd98
- Child:
- 5:41cb88f47f42
Pin works, all bugs fixed.; Improve aesthetic of screen and add LED and buzzer
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el14dg | 0:12ae42019e9f | 1 | /* ELEC2645 Project |
el14dg | 0:12ae42019e9f | 2 | |
el14dg | 2:e504a3cfe113 | 3 | Week 19 - Take distance readings from the sensor and limited ticker interrupts |
el14dg | 2:e504a3cfe113 | 4 | Week 20 - Added the SD library |
el14dg | 2:e504a3cfe113 | 5 | Week 21 - Existing code has been edited to get consistent distance readings from the sensor and alll inputs and outputs have been added in main.h |
el14dg | 0:12ae42019e9f | 6 | Week Easter - |
el14dg | 0:12ae42019e9f | 7 | Week 22 - |
el14dg | 0:12ae42019e9f | 8 | Week 23 - |
el14dg | 2:e504a3cfe113 | 9 | Week 24 - |
el14dg | 0:12ae42019e9f | 10 | |
el14dg | 0:12ae42019e9f | 11 | */ |
el14dg | 0:12ae42019e9f | 12 | |
el14dg | 2:e504a3cfe113 | 13 | |
el14dg | 1:3ae4192d0c25 | 14 | #include "main.h" |
el14dg | 0:12ae42019e9f | 15 | |
el14dg | 0:12ae42019e9f | 16 | int main() |
el14dg | 0:12ae42019e9f | 17 | { |
el14dg | 2:e504a3cfe113 | 18 | init_serial(); |
el14dg | 0:12ae42019e9f | 19 | init_K64F(); |
el14dg | 2:e504a3cfe113 | 20 | init_buttons(); |
el14dg | 2:e504a3cfe113 | 21 | lcd.init(); |
el14dg | 2:e504a3cfe113 | 22 | |
el14dg | 2:e504a3cfe113 | 23 | wait(3); |
el14dg | 3:70a7b64fbd98 | 24 | |
el14dg | 3:70a7b64fbd98 | 25 | read_pin(); |
el14dg | 4:9cac024c057c | 26 | |
el14dg | 4:9cac024c057c | 27 | led_alarm = 0; |
el14dg | 3:70a7b64fbd98 | 28 | |
el14dg | 3:70a7b64fbd98 | 29 | one_second_distance = 0; |
el14dg | 3:70a7b64fbd98 | 30 | one_second_avg_distance = 0; |
el14dg | 3:70a7b64fbd98 | 31 | initial_setting_distance = 0; |
el14dg | 3:70a7b64fbd98 | 32 | |
el14dg | 3:70a7b64fbd98 | 33 | pin_counter = 0; |
el14dg | 4:9cac024c057c | 34 | incorrect_pin_flag = 0; |
el14dg | 4:9cac024c057c | 35 | |
el14dg | 3:70a7b64fbd98 | 36 | setting_distance_counter = 0; |
el14dg | 3:70a7b64fbd98 | 37 | intruder_distance_counter = 0; |
el14dg | 3:70a7b64fbd98 | 38 | |
el14dg | 3:70a7b64fbd98 | 39 | g_button_0_flag = 0; |
el14dg | 3:70a7b64fbd98 | 40 | g_button_1_flag = 0; |
el14dg | 2:e504a3cfe113 | 41 | g_button_c_flag = 0; |
el14dg | 3:70a7b64fbd98 | 42 | g_setting_distance_flag = 0; |
el14dg | 3:70a7b64fbd98 | 43 | g_intruder_distance_flag = 0; |
el14dg | 3:70a7b64fbd98 | 44 | |
el14dg | 3:70a7b64fbd98 | 45 | setting_distance_ticker_status = 0; |
el14dg | 3:70a7b64fbd98 | 46 | |
el14dg | 2:e504a3cfe113 | 47 | g_current_state = 0; |
el14dg | 2:e504a3cfe113 | 48 | |
el14dg | 2:e504a3cfe113 | 49 | screen_selection(); |
el14dg | 2:e504a3cfe113 | 50 | |
el14dg | 2:e504a3cfe113 | 51 | wait(4); |
el14dg | 2:e504a3cfe113 | 52 | |
el14dg | 3:70a7b64fbd98 | 53 | g_next_state = 1; |
el14dg | 3:70a7b64fbd98 | 54 | |
el14dg | 3:70a7b64fbd98 | 55 | |
el14dg | 0:12ae42019e9f | 56 | while (1) { |
el14dg | 0:12ae42019e9f | 57 | |
el14dg | 3:70a7b64fbd98 | 58 | |
el14dg | 2:e504a3cfe113 | 59 | if (g_button_0_flag) { |
el14dg | 3:70a7b64fbd98 | 60 | |
el14dg | 3:70a7b64fbd98 | 61 | pc.printf("g_button_0_flag = %d \n", g_button_0_flag); |
el14dg | 2:e504a3cfe113 | 62 | |
el14dg | 2:e504a3cfe113 | 63 | g_button_0_flag = 0; |
el14dg | 2:e504a3cfe113 | 64 | |
el14dg | 3:70a7b64fbd98 | 65 | button_0_protocol(); |
el14dg | 2:e504a3cfe113 | 66 | |
el14dg | 0:12ae42019e9f | 67 | } |
el14dg | 3:70a7b64fbd98 | 68 | |
el14dg | 2:e504a3cfe113 | 69 | if (g_button_1_flag) { |
el14dg | 3:70a7b64fbd98 | 70 | |
el14dg | 3:70a7b64fbd98 | 71 | pc.printf("g_button_1_flag = %d \n", g_button_1_flag); |
el14dg | 2:e504a3cfe113 | 72 | |
el14dg | 2:e504a3cfe113 | 73 | g_button_1_flag = 0; |
el14dg | 4:9cac024c057c | 74 | |
el14dg | 3:70a7b64fbd98 | 75 | button_1_protocol(); |
el14dg | 0:12ae42019e9f | 76 | |
el14dg | 2:e504a3cfe113 | 77 | } |
el14dg | 2:e504a3cfe113 | 78 | |
el14dg | 2:e504a3cfe113 | 79 | if (g_button_c_flag) { |
el14dg | 3:70a7b64fbd98 | 80 | |
el14dg | 3:70a7b64fbd98 | 81 | pc.printf("g_button_c_flag = %d \n", g_button_c_flag); |
el14dg | 2:e504a3cfe113 | 82 | |
el14dg | 2:e504a3cfe113 | 83 | g_button_c_flag = 0; |
el14dg | 4:9cac024c057c | 84 | |
el14dg | 3:70a7b64fbd98 | 85 | button_c_protocol(); |
el14dg | 4:9cac024c057c | 86 | |
el14dg | 3:70a7b64fbd98 | 87 | } |
el14dg | 3:70a7b64fbd98 | 88 | |
el14dg | 3:70a7b64fbd98 | 89 | if (g_setting_distance_flag) { |
el14dg | 3:70a7b64fbd98 | 90 | |
el14dg | 4:9cac024c057c | 91 | //pc.printf("g_setting_distance_flag = %d \n", g_setting_distance_flag); |
el14dg | 3:70a7b64fbd98 | 92 | |
el14dg | 3:70a7b64fbd98 | 93 | g_setting_distance_flag = 0; |
el14dg | 3:70a7b64fbd98 | 94 | |
el14dg | 4:9cac024c057c | 95 | //pc.printf("g_current_state = %d (setting calibration) \n", g_current_state); |
el14dg | 3:70a7b64fbd98 | 96 | |
el14dg | 3:70a7b64fbd98 | 97 | get_setting_distance(); |
el14dg | 3:70a7b64fbd98 | 98 | |
el14dg | 3:70a7b64fbd98 | 99 | |
el14dg | 3:70a7b64fbd98 | 100 | |
el14dg | 3:70a7b64fbd98 | 101 | } |
el14dg | 3:70a7b64fbd98 | 102 | |
el14dg | 3:70a7b64fbd98 | 103 | if (g_intruder_distance_flag) { |
el14dg | 3:70a7b64fbd98 | 104 | |
el14dg | 4:9cac024c057c | 105 | //pc.printf("g_intruder_distance_flag = %d \n", g_intruder_distance_flag); |
el14dg | 3:70a7b64fbd98 | 106 | |
el14dg | 3:70a7b64fbd98 | 107 | g_setting_distance_flag = 0; |
el14dg | 3:70a7b64fbd98 | 108 | |
el14dg | 4:9cac024c057c | 109 | //pc.printf("g_current_state = %d (intruder detection) \n", g_current_state); |
el14dg | 3:70a7b64fbd98 | 110 | |
el14dg | 3:70a7b64fbd98 | 111 | get_intruder_distance(); |
el14dg | 3:70a7b64fbd98 | 112 | |
el14dg | 4:9cac024c057c | 113 | } |
el14dg | 3:70a7b64fbd98 | 114 | |
el14dg | 3:70a7b64fbd98 | 115 | if (setting_distance_counter == 10) { |
el14dg | 3:70a7b64fbd98 | 116 | |
el14dg | 3:70a7b64fbd98 | 117 | calculate_setting_distance(); |
el14dg | 3:70a7b64fbd98 | 118 | } |
el14dg | 3:70a7b64fbd98 | 119 | |
el14dg | 3:70a7b64fbd98 | 120 | if (intruder_distance_counter == 10) { |
el14dg | 3:70a7b64fbd98 | 121 | |
el14dg | 3:70a7b64fbd98 | 122 | calculate_intruder_distance(); |
el14dg | 0:12ae42019e9f | 123 | |
el14dg | 2:e504a3cfe113 | 124 | } |
el14dg | 3:70a7b64fbd98 | 125 | |
el14dg | 4:9cac024c057c | 126 | //pc.printf("g_current_state = %d \n", g_current_state); |
el14dg | 3:70a7b64fbd98 | 127 | |
el14dg | 4:9cac024c057c | 128 | //pc.printf("g_next_state = %d \n", g_next_state); |
el14dg | 3:70a7b64fbd98 | 129 | |
el14dg | 3:70a7b64fbd98 | 130 | if (g_next_state != g_current_state) { |
el14dg | 3:70a7b64fbd98 | 131 | |
el14dg | 3:70a7b64fbd98 | 132 | screen_selection(); // the screen is determined by the current value of g_current_state |
el14dg | 3:70a7b64fbd98 | 133 | |
el14dg | 3:70a7b64fbd98 | 134 | } |
el14dg | 3:70a7b64fbd98 | 135 | |
el14dg | 3:70a7b64fbd98 | 136 | g_current_state = g_next_state; |
el14dg | 3:70a7b64fbd98 | 137 | |
el14dg | 4:9cac024c057c | 138 | //pc.printf("g_current_state_after_transition = %d \n", g_current_state); |
el14dg | 3:70a7b64fbd98 | 139 | |
el14dg | 3:70a7b64fbd98 | 140 | if (g_current_state == 4) { |
el14dg | 3:70a7b64fbd98 | 141 | |
el14dg | 3:70a7b64fbd98 | 142 | if (setting_distance_ticker_status == 0) { |
el14dg | 3:70a7b64fbd98 | 143 | |
el14dg | 3:70a7b64fbd98 | 144 | setting_distance_ticker_status = 1; |
el14dg | 3:70a7b64fbd98 | 145 | |
el14dg | 4:9cac024c057c | 146 | //pc.printf("g_current_state = %d (attach setting ticker) \n", g_current_state); |
el14dg | 3:70a7b64fbd98 | 147 | |
el14dg | 3:70a7b64fbd98 | 148 | setting_distance.attach(&setting_distance_isr,0.1); |
el14dg | 4:9cac024c057c | 149 | |
el14dg | 3:70a7b64fbd98 | 150 | } |
el14dg | 3:70a7b64fbd98 | 151 | |
el14dg | 3:70a7b64fbd98 | 152 | } |
el14dg | 3:70a7b64fbd98 | 153 | |
el14dg | 3:70a7b64fbd98 | 154 | if (g_current_state == 5) { |
el14dg | 3:70a7b64fbd98 | 155 | |
el14dg | 4:9cac024c057c | 156 | //pc.printf("g_current_state = %d (attach intruder ticker) \n", g_current_state); |
el14dg | 3:70a7b64fbd98 | 157 | |
el14dg | 3:70a7b64fbd98 | 158 | intruder_distance.attach(&intruder_distance_isr,0.1); |
el14dg | 4:9cac024c057c | 159 | |
el14dg | 4:9cac024c057c | 160 | led_alarm = 1; |
el14dg | 3:70a7b64fbd98 | 161 | |
el14dg | 3:70a7b64fbd98 | 162 | } |
el14dg | 4:9cac024c057c | 163 | |
el14dg | 4:9cac024c057c | 164 | else { |
el14dg | 4:9cac024c057c | 165 | |
el14dg | 3:70a7b64fbd98 | 166 | intruder_distance.detach(); |
el14dg | 4:9cac024c057c | 167 | |
el14dg | 4:9cac024c057c | 168 | led_alarm = 0; |
el14dg | 4:9cac024c057c | 169 | |
el14dg | 4:9cac024c057c | 170 | } |
el14dg | 3:70a7b64fbd98 | 171 | |
el14dg | 2:e504a3cfe113 | 172 | sleep(); |
el14dg | 2:e504a3cfe113 | 173 | |
el14dg | 0:12ae42019e9f | 174 | } |
el14dg | 2:e504a3cfe113 | 175 | |
el14dg | 0:12ae42019e9f | 176 | } |
el14dg | 0:12ae42019e9f | 177 | |
el14dg | 2:e504a3cfe113 | 178 | |
el14dg | 0:12ae42019e9f | 179 | void init_serial() |
el14dg | 0:12ae42019e9f | 180 | { |
el14dg | 2:e504a3cfe113 | 181 | // Ensure terminal software matches |
el14dg | 3:70a7b64fbd98 | 182 | pc.baud(115200); |
el14dg | 0:12ae42019e9f | 183 | } |
el14dg | 0:12ae42019e9f | 184 | |
el14dg | 0:12ae42019e9f | 185 | void init_K64F() |
el14dg | 0:12ae42019e9f | 186 | { |
el14dg | 0:12ae42019e9f | 187 | // on-board LEDs are active-low, so set pin high to turn them off. |
el14dg | 0:12ae42019e9f | 188 | r_led = 1; |
el14dg | 0:12ae42019e9f | 189 | g_led = 1; |
el14dg | 0:12ae42019e9f | 190 | b_led = 1; |
el14dg | 0:12ae42019e9f | 191 | |
el14dg | 0:12ae42019e9f | 192 | // since the on-board switches have external pull-ups, we should disable the internal pull-down |
el14dg | 0:12ae42019e9f | 193 | // resistors that are enabled by default using InterruptIn |
el14dg | 0:12ae42019e9f | 194 | sw2.mode(PullNone); |
el14dg | 0:12ae42019e9f | 195 | sw3.mode(PullNone); |
el14dg | 0:12ae42019e9f | 196 | |
el14dg | 0:12ae42019e9f | 197 | } |
el14dg | 2:e504a3cfe113 | 198 | |
el14dg | 2:e504a3cfe113 | 199 | void init_buttons() |
el14dg | 2:e504a3cfe113 | 200 | { |
el14dg | 2:e504a3cfe113 | 201 | |
el14dg | 2:e504a3cfe113 | 202 | button_0.fall(&button_0_isr); |
el14dg | 2:e504a3cfe113 | 203 | button_1.fall(&button_1_isr); |
el14dg | 2:e504a3cfe113 | 204 | button_c.fall(&button_c_isr); |
el14dg | 2:e504a3cfe113 | 205 | |
el14dg | 2:e504a3cfe113 | 206 | button_0.mode(PullDown); |
el14dg | 2:e504a3cfe113 | 207 | button_1.mode(PullDown); |
el14dg | 2:e504a3cfe113 | 208 | button_c.mode(PullDown); |
el14dg | 2:e504a3cfe113 | 209 | |
el14dg | 2:e504a3cfe113 | 210 | } |
el14dg | 2:e504a3cfe113 | 211 | |
el14dg | 3:70a7b64fbd98 | 212 | void setting_distance_isr() |
el14dg | 2:e504a3cfe113 | 213 | { |
el14dg | 3:70a7b64fbd98 | 214 | g_setting_distance_flag = 1; |
el14dg | 3:70a7b64fbd98 | 215 | |
el14dg | 3:70a7b64fbd98 | 216 | } |
el14dg | 3:70a7b64fbd98 | 217 | |
el14dg | 3:70a7b64fbd98 | 218 | void intruder_distance_isr() |
el14dg | 3:70a7b64fbd98 | 219 | { |
el14dg | 3:70a7b64fbd98 | 220 | g_intruder_distance_flag = 1; |
el14dg | 2:e504a3cfe113 | 221 | |
el14dg | 2:e504a3cfe113 | 222 | } |
el14dg | 2:e504a3cfe113 | 223 | |
el14dg | 4:9cac024c057c | 224 | void led_isr() |
el14dg | 4:9cac024c057c | 225 | { |
el14dg | 4:9cac024c057c | 226 | g_led_flag = 0; |
el14dg | 4:9cac024c057c | 227 | |
el14dg | 4:9cac024c057c | 228 | } |
el14dg | 4:9cac024c057c | 229 | |
el14dg | 2:e504a3cfe113 | 230 | void button_0_isr() |
el14dg | 2:e504a3cfe113 | 231 | { |
el14dg | 2:e504a3cfe113 | 232 | g_button_0_flag = 1; |
el14dg | 2:e504a3cfe113 | 233 | |
el14dg | 2:e504a3cfe113 | 234 | } |
el14dg | 2:e504a3cfe113 | 235 | |
el14dg | 2:e504a3cfe113 | 236 | void button_1_isr() |
el14dg | 2:e504a3cfe113 | 237 | { |
el14dg | 2:e504a3cfe113 | 238 | g_button_1_flag = 1; |
el14dg | 2:e504a3cfe113 | 239 | |
el14dg | 2:e504a3cfe113 | 240 | } |
el14dg | 2:e504a3cfe113 | 241 | |
el14dg | 2:e504a3cfe113 | 242 | void button_c_isr() |
el14dg | 2:e504a3cfe113 | 243 | { |
el14dg | 2:e504a3cfe113 | 244 | g_button_c_flag = 1; |
el14dg | 2:e504a3cfe113 | 245 | |
el14dg | 2:e504a3cfe113 | 246 | } |
el14dg | 2:e504a3cfe113 | 247 | |
el14dg | 3:70a7b64fbd98 | 248 | void button_0_protocol() |
el14dg | 2:e504a3cfe113 | 249 | { |
el14dg | 3:70a7b64fbd98 | 250 | if (g_current_state == 2) { |
el14dg | 3:70a7b64fbd98 | 251 | |
el14dg | 3:70a7b64fbd98 | 252 | if (pin_counter < 4) { |
el14dg | 3:70a7b64fbd98 | 253 | |
el14dg | 3:70a7b64fbd98 | 254 | entered_pin[pin_counter] = 0; |
el14dg | 3:70a7b64fbd98 | 255 | |
el14dg | 3:70a7b64fbd98 | 256 | pin_counter++; |
el14dg | 3:70a7b64fbd98 | 257 | |
el14dg | 3:70a7b64fbd98 | 258 | } |
el14dg | 4:9cac024c057c | 259 | |
el14dg | 4:9cac024c057c | 260 | enter_pin(); |
el14dg | 3:70a7b64fbd98 | 261 | |
el14dg | 3:70a7b64fbd98 | 262 | } |
el14dg | 3:70a7b64fbd98 | 263 | |
el14dg | 3:70a7b64fbd98 | 264 | else if (g_current_state == 3) { |
el14dg | 3:70a7b64fbd98 | 265 | |
el14dg | 3:70a7b64fbd98 | 266 | if (pin_counter < 4) { |
el14dg | 3:70a7b64fbd98 | 267 | |
el14dg | 3:70a7b64fbd98 | 268 | entered_pin[pin_counter] = 0; |
el14dg | 3:70a7b64fbd98 | 269 | |
el14dg | 3:70a7b64fbd98 | 270 | pin_counter++; |
el14dg | 3:70a7b64fbd98 | 271 | |
el14dg | 3:70a7b64fbd98 | 272 | } |
el14dg | 4:9cac024c057c | 273 | |
el14dg | 4:9cac024c057c | 274 | enter_pin(); |
el14dg | 3:70a7b64fbd98 | 275 | |
el14dg | 3:70a7b64fbd98 | 276 | } |
el14dg | 2:e504a3cfe113 | 277 | |
el14dg | 3:70a7b64fbd98 | 278 | else if (g_current_state == 6) { |
el14dg | 3:70a7b64fbd98 | 279 | |
el14dg | 3:70a7b64fbd98 | 280 | if (pin_counter < 4) { |
el14dg | 3:70a7b64fbd98 | 281 | |
el14dg | 3:70a7b64fbd98 | 282 | entered_pin[pin_counter] = 0; |
el14dg | 3:70a7b64fbd98 | 283 | |
el14dg | 3:70a7b64fbd98 | 284 | pin_counter++; |
el14dg | 3:70a7b64fbd98 | 285 | |
el14dg | 3:70a7b64fbd98 | 286 | } |
el14dg | 4:9cac024c057c | 287 | |
el14dg | 4:9cac024c057c | 288 | enter_pin(); |
el14dg | 3:70a7b64fbd98 | 289 | |
el14dg | 3:70a7b64fbd98 | 290 | } |
el14dg | 3:70a7b64fbd98 | 291 | |
el14dg | 3:70a7b64fbd98 | 292 | else if (g_current_state == 7) { |
el14dg | 3:70a7b64fbd98 | 293 | |
el14dg | 3:70a7b64fbd98 | 294 | if (pin_counter < 4) { |
el14dg | 3:70a7b64fbd98 | 295 | |
el14dg | 3:70a7b64fbd98 | 296 | entered_pin[pin_counter] = 0; |
el14dg | 3:70a7b64fbd98 | 297 | |
el14dg | 3:70a7b64fbd98 | 298 | pin_counter++; |
el14dg | 3:70a7b64fbd98 | 299 | |
el14dg | 3:70a7b64fbd98 | 300 | } |
el14dg | 4:9cac024c057c | 301 | |
el14dg | 4:9cac024c057c | 302 | enter_pin(); |
el14dg | 3:70a7b64fbd98 | 303 | |
el14dg | 3:70a7b64fbd98 | 304 | } |
el14dg | 3:70a7b64fbd98 | 305 | |
el14dg | 3:70a7b64fbd98 | 306 | else { |
el14dg | 2:e504a3cfe113 | 307 | |
el14dg | 3:70a7b64fbd98 | 308 | g_next_state = fsm[g_current_state].nextState[0]; |
el14dg | 3:70a7b64fbd98 | 309 | |
el14dg | 3:70a7b64fbd98 | 310 | } |
el14dg | 3:70a7b64fbd98 | 311 | |
el14dg | 3:70a7b64fbd98 | 312 | } |
el14dg | 3:70a7b64fbd98 | 313 | |
el14dg | 3:70a7b64fbd98 | 314 | void button_1_protocol() |
el14dg | 3:70a7b64fbd98 | 315 | { |
el14dg | 3:70a7b64fbd98 | 316 | |
el14dg | 3:70a7b64fbd98 | 317 | if (g_current_state == 2) { |
el14dg | 3:70a7b64fbd98 | 318 | |
el14dg | 3:70a7b64fbd98 | 319 | if (pin_counter < 4) { |
el14dg | 3:70a7b64fbd98 | 320 | |
el14dg | 3:70a7b64fbd98 | 321 | entered_pin[pin_counter] = 1; |
el14dg | 3:70a7b64fbd98 | 322 | |
el14dg | 3:70a7b64fbd98 | 323 | pin_counter++; |
el14dg | 3:70a7b64fbd98 | 324 | |
el14dg | 3:70a7b64fbd98 | 325 | } |
el14dg | 4:9cac024c057c | 326 | |
el14dg | 4:9cac024c057c | 327 | enter_pin(); |
el14dg | 3:70a7b64fbd98 | 328 | |
el14dg | 3:70a7b64fbd98 | 329 | } |
el14dg | 3:70a7b64fbd98 | 330 | |
el14dg | 3:70a7b64fbd98 | 331 | else if (g_current_state == 3) { |
el14dg | 3:70a7b64fbd98 | 332 | |
el14dg | 3:70a7b64fbd98 | 333 | if (pin_counter < 4) { |
el14dg | 2:e504a3cfe113 | 334 | |
el14dg | 3:70a7b64fbd98 | 335 | entered_pin[pin_counter] = 1; |
el14dg | 3:70a7b64fbd98 | 336 | |
el14dg | 3:70a7b64fbd98 | 337 | pin_counter++; |
el14dg | 3:70a7b64fbd98 | 338 | |
el14dg | 3:70a7b64fbd98 | 339 | } |
el14dg | 4:9cac024c057c | 340 | |
el14dg | 4:9cac024c057c | 341 | enter_pin(); |
el14dg | 3:70a7b64fbd98 | 342 | |
el14dg | 3:70a7b64fbd98 | 343 | } |
el14dg | 3:70a7b64fbd98 | 344 | |
el14dg | 3:70a7b64fbd98 | 345 | else if (g_current_state == 6) { |
el14dg | 3:70a7b64fbd98 | 346 | |
el14dg | 3:70a7b64fbd98 | 347 | if (pin_counter < 4) { |
el14dg | 3:70a7b64fbd98 | 348 | |
el14dg | 3:70a7b64fbd98 | 349 | entered_pin[pin_counter] = 1; |
el14dg | 2:e504a3cfe113 | 350 | |
el14dg | 3:70a7b64fbd98 | 351 | pin_counter++; |
el14dg | 3:70a7b64fbd98 | 352 | |
el14dg | 3:70a7b64fbd98 | 353 | } |
el14dg | 4:9cac024c057c | 354 | |
el14dg | 4:9cac024c057c | 355 | enter_pin(); |
el14dg | 3:70a7b64fbd98 | 356 | |
el14dg | 3:70a7b64fbd98 | 357 | } |
el14dg | 3:70a7b64fbd98 | 358 | |
el14dg | 3:70a7b64fbd98 | 359 | else if (g_current_state == 7) { |
el14dg | 3:70a7b64fbd98 | 360 | |
el14dg | 3:70a7b64fbd98 | 361 | if (pin_counter < 4) { |
el14dg | 3:70a7b64fbd98 | 362 | |
el14dg | 3:70a7b64fbd98 | 363 | entered_pin[pin_counter] = 1; |
el14dg | 3:70a7b64fbd98 | 364 | |
el14dg | 3:70a7b64fbd98 | 365 | pin_counter++; |
el14dg | 2:e504a3cfe113 | 366 | |
el14dg | 2:e504a3cfe113 | 367 | } |
el14dg | 4:9cac024c057c | 368 | |
el14dg | 4:9cac024c057c | 369 | enter_pin(); |
el14dg | 4:9cac024c057c | 370 | |
el14dg | 3:70a7b64fbd98 | 371 | } |
el14dg | 3:70a7b64fbd98 | 372 | |
el14dg | 3:70a7b64fbd98 | 373 | else { |
el14dg | 3:70a7b64fbd98 | 374 | |
el14dg | 4:9cac024c057c | 375 | g_next_state = fsm[g_current_state].nextState[1]; |
el14dg | 3:70a7b64fbd98 | 376 | |
el14dg | 3:70a7b64fbd98 | 377 | } |
el14dg | 3:70a7b64fbd98 | 378 | |
el14dg | 3:70a7b64fbd98 | 379 | } |
el14dg | 3:70a7b64fbd98 | 380 | |
el14dg | 3:70a7b64fbd98 | 381 | void button_c_protocol() |
el14dg | 3:70a7b64fbd98 | 382 | { |
el14dg | 3:70a7b64fbd98 | 383 | |
el14dg | 3:70a7b64fbd98 | 384 | if (g_current_state == 2) { |
el14dg | 3:70a7b64fbd98 | 385 | |
el14dg | 4:9cac024c057c | 386 | screen_progression(); |
el14dg | 3:70a7b64fbd98 | 387 | |
el14dg | 3:70a7b64fbd98 | 388 | } |
el14dg | 3:70a7b64fbd98 | 389 | |
el14dg | 3:70a7b64fbd98 | 390 | else if (g_current_state == 3) { |
el14dg | 3:70a7b64fbd98 | 391 | |
el14dg | 4:9cac024c057c | 392 | screen_progression(); |
el14dg | 3:70a7b64fbd98 | 393 | |
el14dg | 3:70a7b64fbd98 | 394 | } |
el14dg | 3:70a7b64fbd98 | 395 | |
el14dg | 3:70a7b64fbd98 | 396 | else if (g_current_state == 6) { |
el14dg | 3:70a7b64fbd98 | 397 | |
el14dg | 4:9cac024c057c | 398 | screen_progression(); |
el14dg | 2:e504a3cfe113 | 399 | |
el14dg | 3:70a7b64fbd98 | 400 | } |
el14dg | 3:70a7b64fbd98 | 401 | |
el14dg | 3:70a7b64fbd98 | 402 | else if (g_current_state == 7) { |
el14dg | 3:70a7b64fbd98 | 403 | |
el14dg | 4:9cac024c057c | 404 | screen_progression(); |
el14dg | 3:70a7b64fbd98 | 405 | |
el14dg | 3:70a7b64fbd98 | 406 | } |
el14dg | 3:70a7b64fbd98 | 407 | |
el14dg | 3:70a7b64fbd98 | 408 | else { |
el14dg | 3:70a7b64fbd98 | 409 | |
el14dg | 4:9cac024c057c | 410 | g_next_state = fsm[g_current_state].nextState[2]; |
el14dg | 3:70a7b64fbd98 | 411 | |
el14dg | 3:70a7b64fbd98 | 412 | } |
el14dg | 2:e504a3cfe113 | 413 | |
el14dg | 3:70a7b64fbd98 | 414 | } |
el14dg | 3:70a7b64fbd98 | 415 | |
el14dg | 3:70a7b64fbd98 | 416 | void get_setting_distance() |
el14dg | 3:70a7b64fbd98 | 417 | { |
el14dg | 3:70a7b64fbd98 | 418 | distance[setting_distance_counter] = srf02.getDistanceCm(); |
el14dg | 3:70a7b64fbd98 | 419 | |
el14dg | 3:70a7b64fbd98 | 420 | setting_distance_counter++; |
el14dg | 3:70a7b64fbd98 | 421 | |
el14dg | 3:70a7b64fbd98 | 422 | if (setting_distance_counter == 10) { |
el14dg | 3:70a7b64fbd98 | 423 | |
el14dg | 3:70a7b64fbd98 | 424 | setting_distance.detach(); |
el14dg | 3:70a7b64fbd98 | 425 | |
el14dg | 2:e504a3cfe113 | 426 | } |
el14dg | 2:e504a3cfe113 | 427 | |
el14dg | 2:e504a3cfe113 | 428 | } |
el14dg | 2:e504a3cfe113 | 429 | |
el14dg | 3:70a7b64fbd98 | 430 | void get_intruder_distance() |
el14dg | 3:70a7b64fbd98 | 431 | { |
el14dg | 3:70a7b64fbd98 | 432 | |
el14dg | 3:70a7b64fbd98 | 433 | distance[intruder_distance_counter] = srf02.getDistanceCm(); |
el14dg | 3:70a7b64fbd98 | 434 | |
el14dg | 3:70a7b64fbd98 | 435 | intruder_distance_counter++; |
el14dg | 3:70a7b64fbd98 | 436 | |
el14dg | 3:70a7b64fbd98 | 437 | if (intruder_distance_counter == 10) { |
el14dg | 3:70a7b64fbd98 | 438 | |
el14dg | 3:70a7b64fbd98 | 439 | intruder_distance.detach(); |
el14dg | 3:70a7b64fbd98 | 440 | |
el14dg | 3:70a7b64fbd98 | 441 | } |
el14dg | 3:70a7b64fbd98 | 442 | |
el14dg | 3:70a7b64fbd98 | 443 | } |
el14dg | 3:70a7b64fbd98 | 444 | |
el14dg | 3:70a7b64fbd98 | 445 | void calculate_setting_distance() |
el14dg | 3:70a7b64fbd98 | 446 | { |
el14dg | 3:70a7b64fbd98 | 447 | |
el14dg | 3:70a7b64fbd98 | 448 | for (int i = 0; i < 10; i++) { |
el14dg | 3:70a7b64fbd98 | 449 | |
el14dg | 3:70a7b64fbd98 | 450 | one_second_distance = one_second_distance + distance[i]; |
el14dg | 3:70a7b64fbd98 | 451 | |
el14dg | 3:70a7b64fbd98 | 452 | } |
el14dg | 3:70a7b64fbd98 | 453 | |
el14dg | 3:70a7b64fbd98 | 454 | initial_setting_distance = (one_second_distance / 10); |
el14dg | 4:9cac024c057c | 455 | |
el14dg | 4:9cac024c057c | 456 | pc.printf("Initial Setting Distance = %.2f cm\n",initial_setting_distance); |
el14dg | 3:70a7b64fbd98 | 457 | |
el14dg | 3:70a7b64fbd98 | 458 | setting_distance_counter = 0; |
el14dg | 3:70a7b64fbd98 | 459 | |
el14dg | 3:70a7b64fbd98 | 460 | one_second_distance = 0; |
el14dg | 3:70a7b64fbd98 | 461 | |
el14dg | 3:70a7b64fbd98 | 462 | transition.attach(&screen_5_transition,5); |
el14dg | 3:70a7b64fbd98 | 463 | |
el14dg | 3:70a7b64fbd98 | 464 | } |
el14dg | 3:70a7b64fbd98 | 465 | |
el14dg | 3:70a7b64fbd98 | 466 | void calculate_intruder_distance() |
el14dg | 3:70a7b64fbd98 | 467 | { |
el14dg | 3:70a7b64fbd98 | 468 | |
el14dg | 3:70a7b64fbd98 | 469 | for (int i = 0; i < 10; i++) { |
el14dg | 3:70a7b64fbd98 | 470 | |
el14dg | 3:70a7b64fbd98 | 471 | one_second_distance = one_second_distance + distance[i]; |
el14dg | 3:70a7b64fbd98 | 472 | |
el14dg | 3:70a7b64fbd98 | 473 | } |
el14dg | 3:70a7b64fbd98 | 474 | |
el14dg | 3:70a7b64fbd98 | 475 | one_second_avg_distance = one_second_distance / 10; |
el14dg | 4:9cac024c057c | 476 | |
el14dg | 4:9cac024c057c | 477 | pc.printf("Intruder Distance = %.2f cm\n",one_second_avg_distance); |
el14dg | 4:9cac024c057c | 478 | |
el14dg | 3:70a7b64fbd98 | 479 | intruder_distance_counter = 0; |
el14dg | 3:70a7b64fbd98 | 480 | |
el14dg | 3:70a7b64fbd98 | 481 | one_second_distance = 0; |
el14dg | 3:70a7b64fbd98 | 482 | |
el14dg | 3:70a7b64fbd98 | 483 | |
el14dg | 3:70a7b64fbd98 | 484 | if (one_second_avg_distance > (1.5*initial_setting_distance)) { |
el14dg | 3:70a7b64fbd98 | 485 | |
el14dg | 3:70a7b64fbd98 | 486 | g_next_state = 7; |
el14dg | 3:70a7b64fbd98 | 487 | |
el14dg | 3:70a7b64fbd98 | 488 | g_current_state = 7; |
el14dg | 3:70a7b64fbd98 | 489 | |
el14dg | 3:70a7b64fbd98 | 490 | screen_selection(); |
el14dg | 3:70a7b64fbd98 | 491 | |
el14dg | 3:70a7b64fbd98 | 492 | } |
el14dg | 3:70a7b64fbd98 | 493 | |
el14dg | 3:70a7b64fbd98 | 494 | else if (one_second_avg_distance < (0.5 * initial_setting_distance)) { |
el14dg | 3:70a7b64fbd98 | 495 | |
el14dg | 3:70a7b64fbd98 | 496 | g_next_state = 7; |
el14dg | 3:70a7b64fbd98 | 497 | |
el14dg | 3:70a7b64fbd98 | 498 | g_current_state = 7; |
el14dg | 3:70a7b64fbd98 | 499 | |
el14dg | 3:70a7b64fbd98 | 500 | screen_selection(); |
el14dg | 3:70a7b64fbd98 | 501 | |
el14dg | 3:70a7b64fbd98 | 502 | } |
el14dg | 3:70a7b64fbd98 | 503 | |
el14dg | 3:70a7b64fbd98 | 504 | else { |
el14dg | 3:70a7b64fbd98 | 505 | |
el14dg | 3:70a7b64fbd98 | 506 | intruder_distance.attach(&intruder_distance_isr,0.1); |
el14dg | 3:70a7b64fbd98 | 507 | |
el14dg | 3:70a7b64fbd98 | 508 | } |
el14dg | 3:70a7b64fbd98 | 509 | |
el14dg | 3:70a7b64fbd98 | 510 | |
el14dg | 3:70a7b64fbd98 | 511 | |
el14dg | 3:70a7b64fbd98 | 512 | } |
el14dg | 3:70a7b64fbd98 | 513 | |
el14dg | 3:70a7b64fbd98 | 514 | |
el14dg | 2:e504a3cfe113 | 515 | void state_0_screen() |
el14dg | 2:e504a3cfe113 | 516 | { |
el14dg | 2:e504a3cfe113 | 517 | lcd.clear(); |
el14dg | 2:e504a3cfe113 | 518 | |
el14dg | 2:e504a3cfe113 | 519 | lcd.printString("MobileSecurity",0,0); |
el14dg | 2:e504a3cfe113 | 520 | |
el14dg | 2:e504a3cfe113 | 521 | lcd.printString("System",27,1); |
el14dg | 2:e504a3cfe113 | 522 | |
el14dg | 2:e504a3cfe113 | 523 | lcd.printString("Daniel Gibbons",0,4); |
el14dg | 2:e504a3cfe113 | 524 | |
el14dg | 2:e504a3cfe113 | 525 | lcd.refresh(); |
el14dg | 2:e504a3cfe113 | 526 | |
el14dg | 2:e504a3cfe113 | 527 | } |
el14dg | 2:e504a3cfe113 | 528 | |
el14dg | 2:e504a3cfe113 | 529 | void state_1_screen() |
el14dg | 2:e504a3cfe113 | 530 | { |
el14dg | 2:e504a3cfe113 | 531 | lcd.clear(); |
el14dg | 2:e504a3cfe113 | 532 | |
el14dg | 2:e504a3cfe113 | 533 | lcd.printString("Set Alarm",15,1); |
el14dg | 2:e504a3cfe113 | 534 | |
el14dg | 2:e504a3cfe113 | 535 | lcd.printString("Set New Pin",9,4); |
el14dg | 2:e504a3cfe113 | 536 | |
el14dg | 2:e504a3cfe113 | 537 | lcd.refresh(); |
el14dg | 2:e504a3cfe113 | 538 | |
el14dg | 2:e504a3cfe113 | 539 | } |
el14dg | 2:e504a3cfe113 | 540 | |
el14dg | 3:70a7b64fbd98 | 541 | void state_2_6_7_screen() |
el14dg | 2:e504a3cfe113 | 542 | { |
el14dg | 2:e504a3cfe113 | 543 | lcd.clear(); |
el14dg | 2:e504a3cfe113 | 544 | |
el14dg | 2:e504a3cfe113 | 545 | lcd.printString("Enter 4 Digit",3,1); |
el14dg | 2:e504a3cfe113 | 546 | |
el14dg | 2:e504a3cfe113 | 547 | lcd.printString("Pin Below",15,2); |
el14dg | 2:e504a3cfe113 | 548 | |
el14dg | 2:e504a3cfe113 | 549 | lcd.refresh(); |
el14dg | 2:e504a3cfe113 | 550 | |
el14dg | 2:e504a3cfe113 | 551 | } |
el14dg | 2:e504a3cfe113 | 552 | |
el14dg | 3:70a7b64fbd98 | 553 | void state_3_screen() |
el14dg | 3:70a7b64fbd98 | 554 | { |
el14dg | 3:70a7b64fbd98 | 555 | lcd.clear(); |
el14dg | 3:70a7b64fbd98 | 556 | |
el14dg | 3:70a7b64fbd98 | 557 | lcd.printString("Enter New 4",9,1); |
el14dg | 3:70a7b64fbd98 | 558 | |
el14dg | 3:70a7b64fbd98 | 559 | lcd.printString("Digit Pin",15,2); |
el14dg | 3:70a7b64fbd98 | 560 | |
el14dg | 3:70a7b64fbd98 | 561 | lcd.refresh(); |
el14dg | 3:70a7b64fbd98 | 562 | |
el14dg | 3:70a7b64fbd98 | 563 | } |
el14dg | 3:70a7b64fbd98 | 564 | |
el14dg | 2:e504a3cfe113 | 565 | void state_4_screen() |
el14dg | 2:e504a3cfe113 | 566 | { |
el14dg | 2:e504a3cfe113 | 567 | lcd.clear(); |
el14dg | 2:e504a3cfe113 | 568 | |
el14dg | 2:e504a3cfe113 | 569 | lcd.printString("Setting",21,1); |
el14dg | 2:e504a3cfe113 | 570 | |
el14dg | 2:e504a3cfe113 | 571 | lcd.refresh(); |
el14dg | 2:e504a3cfe113 | 572 | |
el14dg | 2:e504a3cfe113 | 573 | } |
el14dg | 2:e504a3cfe113 | 574 | |
el14dg | 2:e504a3cfe113 | 575 | void state_5_screen() |
el14dg | 2:e504a3cfe113 | 576 | { |
el14dg | 2:e504a3cfe113 | 577 | lcd.clear(); |
el14dg | 2:e504a3cfe113 | 578 | |
el14dg | 2:e504a3cfe113 | 579 | lcd.printString("Alarm Set",15,1); |
el14dg | 2:e504a3cfe113 | 580 | |
el14dg | 2:e504a3cfe113 | 581 | lcd.printString("DEACTIVATE?",9,4); |
el14dg | 2:e504a3cfe113 | 582 | |
el14dg | 2:e504a3cfe113 | 583 | lcd.refresh(); |
el14dg | 2:e504a3cfe113 | 584 | |
el14dg | 2:e504a3cfe113 | 585 | } |
el14dg | 2:e504a3cfe113 | 586 | |
el14dg | 2:e504a3cfe113 | 587 | void state_8_screen() |
el14dg | 2:e504a3cfe113 | 588 | { |
el14dg | 2:e504a3cfe113 | 589 | lcd.clear(); |
el14dg | 2:e504a3cfe113 | 590 | |
el14dg | 2:e504a3cfe113 | 591 | lcd.printString("ALARM",27,1); |
el14dg | 2:e504a3cfe113 | 592 | |
el14dg | 2:e504a3cfe113 | 593 | lcd.printString("TRIGGERED",15,2); |
el14dg | 2:e504a3cfe113 | 594 | |
el14dg | 2:e504a3cfe113 | 595 | lcd.refresh(); |
el14dg | 2:e504a3cfe113 | 596 | } |
el14dg | 2:e504a3cfe113 | 597 | |
el14dg | 2:e504a3cfe113 | 598 | void screen_selection() |
el14dg | 2:e504a3cfe113 | 599 | { |
el14dg | 3:70a7b64fbd98 | 600 | |
el14dg | 3:70a7b64fbd98 | 601 | pin_counter = 0; |
el14dg | 3:70a7b64fbd98 | 602 | |
el14dg | 3:70a7b64fbd98 | 603 | if (g_next_state == 0) { |
el14dg | 2:e504a3cfe113 | 604 | |
el14dg | 2:e504a3cfe113 | 605 | state_0_screen(); |
el14dg | 2:e504a3cfe113 | 606 | |
el14dg | 2:e504a3cfe113 | 607 | lcd.printString("0",0,0); |
el14dg | 2:e504a3cfe113 | 608 | |
el14dg | 2:e504a3cfe113 | 609 | } |
el14dg | 3:70a7b64fbd98 | 610 | |
el14dg | 3:70a7b64fbd98 | 611 | else if (g_next_state == 1) { |
el14dg | 2:e504a3cfe113 | 612 | |
el14dg | 2:e504a3cfe113 | 613 | state_1_screen(); |
el14dg | 2:e504a3cfe113 | 614 | |
el14dg | 2:e504a3cfe113 | 615 | lcd.printString("1",0,0); |
el14dg | 2:e504a3cfe113 | 616 | |
el14dg | 2:e504a3cfe113 | 617 | } |
el14dg | 2:e504a3cfe113 | 618 | |
el14dg | 3:70a7b64fbd98 | 619 | else if (g_next_state == 2) { |
el14dg | 2:e504a3cfe113 | 620 | |
el14dg | 3:70a7b64fbd98 | 621 | state_2_6_7_screen(); |
el14dg | 2:e504a3cfe113 | 622 | |
el14dg | 2:e504a3cfe113 | 623 | lcd.printString("2",0,0); |
el14dg | 2:e504a3cfe113 | 624 | |
el14dg | 2:e504a3cfe113 | 625 | } |
el14dg | 2:e504a3cfe113 | 626 | |
el14dg | 3:70a7b64fbd98 | 627 | else if (g_next_state == 3) { |
el14dg | 2:e504a3cfe113 | 628 | |
el14dg | 3:70a7b64fbd98 | 629 | state_3_screen(); |
el14dg | 2:e504a3cfe113 | 630 | |
el14dg | 2:e504a3cfe113 | 631 | lcd.printString("3",0,0); |
el14dg | 2:e504a3cfe113 | 632 | |
el14dg | 2:e504a3cfe113 | 633 | } |
el14dg | 2:e504a3cfe113 | 634 | |
el14dg | 3:70a7b64fbd98 | 635 | else if (g_next_state == 4) { |
el14dg | 2:e504a3cfe113 | 636 | |
el14dg | 2:e504a3cfe113 | 637 | state_4_screen(); |
el14dg | 2:e504a3cfe113 | 638 | |
el14dg | 2:e504a3cfe113 | 639 | lcd.printString("4",0,0); |
el14dg | 2:e504a3cfe113 | 640 | |
el14dg | 2:e504a3cfe113 | 641 | } |
el14dg | 2:e504a3cfe113 | 642 | |
el14dg | 3:70a7b64fbd98 | 643 | else if (g_next_state == 5) { |
el14dg | 2:e504a3cfe113 | 644 | |
el14dg | 2:e504a3cfe113 | 645 | state_5_screen(); |
el14dg | 2:e504a3cfe113 | 646 | |
el14dg | 2:e504a3cfe113 | 647 | lcd.printString("5",0,0); |
el14dg | 2:e504a3cfe113 | 648 | |
el14dg | 2:e504a3cfe113 | 649 | } |
el14dg | 2:e504a3cfe113 | 650 | |
el14dg | 3:70a7b64fbd98 | 651 | else if (g_next_state == 6) { |
el14dg | 2:e504a3cfe113 | 652 | |
el14dg | 3:70a7b64fbd98 | 653 | state_2_6_7_screen(); |
el14dg | 2:e504a3cfe113 | 654 | |
el14dg | 2:e504a3cfe113 | 655 | lcd.printString("6",0,0); |
el14dg | 2:e504a3cfe113 | 656 | |
el14dg | 2:e504a3cfe113 | 657 | } |
el14dg | 2:e504a3cfe113 | 658 | |
el14dg | 3:70a7b64fbd98 | 659 | else if (g_next_state == 7) { |
el14dg | 2:e504a3cfe113 | 660 | |
el14dg | 3:70a7b64fbd98 | 661 | state_2_6_7_screen(); |
el14dg | 2:e504a3cfe113 | 662 | |
el14dg | 2:e504a3cfe113 | 663 | lcd.printString("7",0,0); |
el14dg | 2:e504a3cfe113 | 664 | |
el14dg | 2:e504a3cfe113 | 665 | } |
el14dg | 2:e504a3cfe113 | 666 | |
el14dg | 2:e504a3cfe113 | 667 | else { |
el14dg | 2:e504a3cfe113 | 668 | |
el14dg | 2:e504a3cfe113 | 669 | state_8_screen(); |
el14dg | 2:e504a3cfe113 | 670 | |
el14dg | 2:e504a3cfe113 | 671 | lcd.printString("8",0,0); |
el14dg | 2:e504a3cfe113 | 672 | |
el14dg | 2:e504a3cfe113 | 673 | } |
el14dg | 2:e504a3cfe113 | 674 | |
el14dg | 2:e504a3cfe113 | 675 | |
el14dg | 2:e504a3cfe113 | 676 | |
el14dg | 2:e504a3cfe113 | 677 | } |
el14dg | 3:70a7b64fbd98 | 678 | |
el14dg | 3:70a7b64fbd98 | 679 | void screen_5_transition() |
el14dg | 3:70a7b64fbd98 | 680 | { |
el14dg | 4:9cac024c057c | 681 | g_next_state = 5; |
el14dg | 4:9cac024c057c | 682 | |
el14dg | 3:70a7b64fbd98 | 683 | screen_selection(); |
el14dg | 3:70a7b64fbd98 | 684 | |
el14dg | 3:70a7b64fbd98 | 685 | g_current_state = 5; |
el14dg | 4:9cac024c057c | 686 | |
el14dg | 4:9cac024c057c | 687 | setting_distance_ticker_status = 0; |
el14dg | 4:9cac024c057c | 688 | |
el14dg | 4:9cac024c057c | 689 | led.detach(); |
el14dg | 3:70a7b64fbd98 | 690 | |
el14dg | 3:70a7b64fbd98 | 691 | intruder_distance.attach(&intruder_distance_isr,0.1); |
el14dg | 4:9cac024c057c | 692 | |
el14dg | 4:9cac024c057c | 693 | led_alarm = 1; |
el14dg | 4:9cac024c057c | 694 | |
el14dg | 3:70a7b64fbd98 | 695 | } |
el14dg | 3:70a7b64fbd98 | 696 | |
el14dg | 4:9cac024c057c | 697 | void screen_progression() |
el14dg | 4:9cac024c057c | 698 | { |
el14dg | 4:9cac024c057c | 699 | if (g_current_state == 3) { |
el14dg | 4:9cac024c057c | 700 | |
el14dg | 4:9cac024c057c | 701 | if (pin_counter > 3) { |
el14dg | 4:9cac024c057c | 702 | |
el14dg | 4:9cac024c057c | 703 | change_pin(); |
el14dg | 4:9cac024c057c | 704 | |
el14dg | 4:9cac024c057c | 705 | read_pin(); |
el14dg | 4:9cac024c057c | 706 | |
el14dg | 4:9cac024c057c | 707 | g_next_state = fsm[g_current_state].nextState[2]; |
el14dg | 4:9cac024c057c | 708 | |
el14dg | 4:9cac024c057c | 709 | } |
el14dg | 4:9cac024c057c | 710 | else { |
el14dg | 4:9cac024c057c | 711 | |
el14dg | 4:9cac024c057c | 712 | g_next_state = fsm[g_current_state].nextState[3]; |
el14dg | 4:9cac024c057c | 713 | |
el14dg | 4:9cac024c057c | 714 | } |
el14dg | 4:9cac024c057c | 715 | } |
el14dg | 4:9cac024c057c | 716 | |
el14dg | 4:9cac024c057c | 717 | else { |
el14dg | 4:9cac024c057c | 718 | |
el14dg | 4:9cac024c057c | 719 | check_pin(); |
el14dg | 4:9cac024c057c | 720 | |
el14dg | 4:9cac024c057c | 721 | if (incorrect_pin_flag == 1) { |
el14dg | 4:9cac024c057c | 722 | |
el14dg | 4:9cac024c057c | 723 | incorrect_pin_flag = 0; |
el14dg | 4:9cac024c057c | 724 | |
el14dg | 4:9cac024c057c | 725 | g_next_state = fsm[g_current_state].nextState[3]; |
el14dg | 4:9cac024c057c | 726 | |
el14dg | 4:9cac024c057c | 727 | pc.printf("g_next_state = %d\n",g_next_state); |
el14dg | 4:9cac024c057c | 728 | |
el14dg | 4:9cac024c057c | 729 | } |
el14dg | 4:9cac024c057c | 730 | |
el14dg | 4:9cac024c057c | 731 | else { |
el14dg | 4:9cac024c057c | 732 | |
el14dg | 4:9cac024c057c | 733 | g_next_state = fsm[g_current_state].nextState[2]; |
el14dg | 4:9cac024c057c | 734 | |
el14dg | 4:9cac024c057c | 735 | pc.printf("g_next_state = %d\n",g_next_state); |
el14dg | 4:9cac024c057c | 736 | |
el14dg | 4:9cac024c057c | 737 | } |
el14dg | 4:9cac024c057c | 738 | |
el14dg | 4:9cac024c057c | 739 | } |
el14dg | 4:9cac024c057c | 740 | |
el14dg | 4:9cac024c057c | 741 | } |
el14dg | 4:9cac024c057c | 742 | |
el14dg | 4:9cac024c057c | 743 | void enter_pin() |
el14dg | 4:9cac024c057c | 744 | { |
el14dg | 4:9cac024c057c | 745 | if (pin_counter == 1) { |
el14dg | 4:9cac024c057c | 746 | |
el14dg | 4:9cac024c057c | 747 | lcd.printString("*",19,4); |
el14dg | 4:9cac024c057c | 748 | |
el14dg | 4:9cac024c057c | 749 | } |
el14dg | 4:9cac024c057c | 750 | |
el14dg | 4:9cac024c057c | 751 | else if (pin_counter == 2) { |
el14dg | 4:9cac024c057c | 752 | |
el14dg | 4:9cac024c057c | 753 | lcd.printString("*",34,4); |
el14dg | 4:9cac024c057c | 754 | |
el14dg | 4:9cac024c057c | 755 | } |
el14dg | 4:9cac024c057c | 756 | |
el14dg | 4:9cac024c057c | 757 | else if (pin_counter == 3) { |
el14dg | 4:9cac024c057c | 758 | |
el14dg | 4:9cac024c057c | 759 | lcd.printString("*",49,4); |
el14dg | 4:9cac024c057c | 760 | |
el14dg | 4:9cac024c057c | 761 | } |
el14dg | 4:9cac024c057c | 762 | |
el14dg | 4:9cac024c057c | 763 | else if (pin_counter == 4) { |
el14dg | 4:9cac024c057c | 764 | |
el14dg | 4:9cac024c057c | 765 | lcd.printString("*",64,4); |
el14dg | 4:9cac024c057c | 766 | |
el14dg | 4:9cac024c057c | 767 | } |
el14dg | 4:9cac024c057c | 768 | |
el14dg | 4:9cac024c057c | 769 | } |
el14dg | 4:9cac024c057c | 770 | |
el14dg | 4:9cac024c057c | 771 | void check_pin() |
el14dg | 4:9cac024c057c | 772 | { |
el14dg | 4:9cac024c057c | 773 | for (int i = 0; i < 4; i++) { |
el14dg | 4:9cac024c057c | 774 | |
el14dg | 4:9cac024c057c | 775 | if (entered_pin[i] != set_pin[i]) { |
el14dg | 4:9cac024c057c | 776 | |
el14dg | 4:9cac024c057c | 777 | pc.printf("entered pin doesn't match set pin..."); |
el14dg | 4:9cac024c057c | 778 | |
el14dg | 4:9cac024c057c | 779 | incorrect_pin_flag = 1; |
el14dg | 4:9cac024c057c | 780 | |
el14dg | 4:9cac024c057c | 781 | break; |
el14dg | 4:9cac024c057c | 782 | |
el14dg | 4:9cac024c057c | 783 | } |
el14dg | 4:9cac024c057c | 784 | |
el14dg | 4:9cac024c057c | 785 | entered_pin[i] = -1; |
el14dg | 4:9cac024c057c | 786 | |
el14dg | 4:9cac024c057c | 787 | } |
el14dg | 4:9cac024c057c | 788 | |
el14dg | 4:9cac024c057c | 789 | } |
el14dg | 4:9cac024c057c | 790 | |
el14dg | 3:70a7b64fbd98 | 791 | void change_pin() |
el14dg | 3:70a7b64fbd98 | 792 | { |
el14dg | 3:70a7b64fbd98 | 793 | delete_file("/sd/test.txt"); |
el14dg | 3:70a7b64fbd98 | 794 | |
el14dg | 3:70a7b64fbd98 | 795 | pin = fopen("/sd/test.txt", "a"); |
el14dg | 3:70a7b64fbd98 | 796 | |
el14dg | 3:70a7b64fbd98 | 797 | if (pin == NULL) { // if it can't open the file then print error message |
el14dg | 3:70a7b64fbd98 | 798 | |
el14dg | 3:70a7b64fbd98 | 799 | pc.printf("Error! Unable to open file!\n"); |
el14dg | 3:70a7b64fbd98 | 800 | |
el14dg | 3:70a7b64fbd98 | 801 | } |
el14dg | 3:70a7b64fbd98 | 802 | |
el14dg | 3:70a7b64fbd98 | 803 | else { |
el14dg | 3:70a7b64fbd98 | 804 | |
el14dg | 4:9cac024c057c | 805 | pc.printf("Writing to file....\n"); |
el14dg | 3:70a7b64fbd98 | 806 | |
el14dg | 3:70a7b64fbd98 | 807 | for(int i = 0; i < 4; i++) { |
el14dg | 3:70a7b64fbd98 | 808 | |
el14dg | 3:70a7b64fbd98 | 809 | int pin_element = entered_pin[i];; // pin_element variable |
el14dg | 3:70a7b64fbd98 | 810 | |
el14dg | 3:70a7b64fbd98 | 811 | fprintf(pin, "%d,%d\n",i,pin_element); // print formatted string to file (CSV) |
el14dg | 3:70a7b64fbd98 | 812 | } |
el14dg | 3:70a7b64fbd98 | 813 | |
el14dg | 3:70a7b64fbd98 | 814 | for(int i = 0; i < 4; i++) { |
el14dg | 3:70a7b64fbd98 | 815 | |
el14dg | 3:70a7b64fbd98 | 816 | pc.printf("[%d] %d\n",i,entered_pin[i]); |
el14dg | 3:70a7b64fbd98 | 817 | } |
el14dg | 3:70a7b64fbd98 | 818 | |
el14dg | 3:70a7b64fbd98 | 819 | pc.printf("Done.\n"); |
el14dg | 3:70a7b64fbd98 | 820 | |
el14dg | 3:70a7b64fbd98 | 821 | fclose(pin); // ensure you close the file after writing |
el14dg | 3:70a7b64fbd98 | 822 | |
el14dg | 3:70a7b64fbd98 | 823 | } |
el14dg | 3:70a7b64fbd98 | 824 | |
el14dg | 3:70a7b64fbd98 | 825 | } |
el14dg | 3:70a7b64fbd98 | 826 | |
el14dg | 3:70a7b64fbd98 | 827 | void read_pin() |
el14dg | 3:70a7b64fbd98 | 828 | { |
el14dg | 3:70a7b64fbd98 | 829 | pin = fopen("/sd/test.txt", "r"); |
el14dg | 3:70a7b64fbd98 | 830 | |
el14dg | 3:70a7b64fbd98 | 831 | int i = 0; |
el14dg | 3:70a7b64fbd98 | 832 | |
el14dg | 3:70a7b64fbd98 | 833 | pc.printf("Reading into set_pin array...\n"); |
el14dg | 3:70a7b64fbd98 | 834 | |
el14dg | 3:70a7b64fbd98 | 835 | while (fscanf(pin, "%d,%d",&index_array[i],&set_pin[i]) != EOF) { |
el14dg | 3:70a7b64fbd98 | 836 | i++; // read data into array and increment index |
el14dg | 3:70a7b64fbd98 | 837 | } |
el14dg | 3:70a7b64fbd98 | 838 | |
el14dg | 3:70a7b64fbd98 | 839 | fclose(pin); // ensure you close the file after reading |
el14dg | 3:70a7b64fbd98 | 840 | |
el14dg | 3:70a7b64fbd98 | 841 | for(int i = 0; i < 4 ; i++) { |
el14dg | 3:70a7b64fbd98 | 842 | |
el14dg | 3:70a7b64fbd98 | 843 | pc.printf("[%d] %d\n",i,set_pin[i]); |
el14dg | 3:70a7b64fbd98 | 844 | |
el14dg | 3:70a7b64fbd98 | 845 | } |
el14dg | 3:70a7b64fbd98 | 846 | |
el14dg | 3:70a7b64fbd98 | 847 | pc.printf("Done.\n"); |
el14dg | 3:70a7b64fbd98 | 848 | |
el14dg | 3:70a7b64fbd98 | 849 | } |
el14dg | 3:70a7b64fbd98 | 850 | |
el14dg | 3:70a7b64fbd98 | 851 | void delete_file(char filename[]) |
el14dg | 3:70a7b64fbd98 | 852 | { |
el14dg | 3:70a7b64fbd98 | 853 | pc.printf("Deleting file '%s'...",filename); |
el14dg | 3:70a7b64fbd98 | 854 | FILE *fp = fopen(filename, "r"); // try and open file |
el14dg | 3:70a7b64fbd98 | 855 | if (fp != NULL) { // if it does open... |
el14dg | 3:70a7b64fbd98 | 856 | fclose(fp); // close it |
el14dg | 3:70a7b64fbd98 | 857 | remove(filename); // and then delete |
el14dg | 3:70a7b64fbd98 | 858 | pc.printf("Done!\n"); |
el14dg | 3:70a7b64fbd98 | 859 | } |
el14dg | 3:70a7b64fbd98 | 860 | // if we can't open it, it doesn't exist and so we can't delete it |
el14dg | 3:70a7b64fbd98 | 861 | } |