Mobile Security System - Revision 1.0

Dependencies:   FXOS8700Q N5110 SDFileSystem SRF02 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* ELEC2645 Project */
00002 
00003 #include "main.h"
00004 
00005 int main()
00006 {
00007 
00008     init_serial(); // set up UART connection with PC for debugging
00009     init_K64F(); // set up K64F on board LEDs and switches
00010     init_buttons(); // set up the three external buttons
00011     init_variables(); // initialise all variables to 0
00012     lcd.init(); // initialise the Nokia 5110 LCD
00013 
00014     read_pin(); // read current pin from the SD card to the set_pin array
00015 
00016     buzzer.period(1.0/500.0); // set the buzzer period - 500 Hz
00017 
00018     buzzer.write(0.5); // duty cycle = 0.5 ----> Square Wave
00019 
00020     wait(2); // wait two seconds to allow LCD to initialise
00021 
00022     screen_selection(); // initial screen
00023 
00024     wait(4);
00025 
00026     g_next_state  = 1;
00027 
00028     screen_selection(); // menu screen
00029 
00030     g_current_state  = 1;
00031 
00032 
00033     while (1) {
00034 
00035 
00036         if (g_button_0_flag ) { // if button_0 is pressed
00037 
00038             pc.printf("g_button_0_flag = %d \n", g_button_0_flag );
00039 
00040             g_button_0_flag  = 0;
00041 
00042             button_0_protocol();
00043 
00044         }
00045 
00046         if (g_button_1_flag ) { // if button_1 is pressed
00047 
00048             pc.printf("g_button_1_flag = %d \n", g_button_1_flag );
00049 
00050             g_button_1_flag  = 0;
00051 
00052             button_1_protocol();
00053 
00054         }
00055 
00056         if (g_button_c_flag ) { // if button_c is pressed
00057 
00058             pc.printf("g_button_c_flag = %d \n", g_button_c_flag );
00059 
00060             g_button_c_flag  = 0;
00061 
00062             button_c_protocol();
00063 
00064         }
00065 
00066         if (g_setting_distance_flag ) { // state 4 - initial setting distance
00067 
00068             g_setting_distance_flag  = 0;
00069 
00070             get_setting_distance();
00071 
00072         }
00073 
00074         if (g_intruder_distance_flag ) { // state 5 - current distance compared to initial setting distance
00075             
00076             if (g_current_state  == 5) {
00077                 
00078                 g_setting_distance_flag  = 0;
00079 
00080                 get_intruder_distance();
00081                 
00082             }    
00083 
00084         }
00085 
00086         if (g_setting_screen_flag ) { // state 4 loading bar and percentage animation
00087 
00088             g_setting_screen_flag  = 0;
00089 
00090             setting_animation();
00091 
00092         }
00093 
00094         if (g_led_buzzer_flag ) { // toggles LED and buzzer
00095         
00096             g_led_buzzer_flag  = 0;
00097 
00098             led_alarm = !led_alarm;
00099 
00100             if (buzzer == 0) {
00101 
00102                 buzzer = 0.5;
00103 
00104             }
00105 
00106             else {
00107 
00108                 buzzer = 0;
00109 
00110             }                   
00111 
00112         }
00113         
00114         if (g_acc_flag ) {
00115             
00116             g_acc_flag  = 0;
00117             
00118             if (g_current_state  == 4) {
00119                 
00120                 compare_axis_data();
00121                 
00122             }
00123             
00124         }    
00125 
00126         if (g_pin_timeout_flag ) { // state 7 - 20 second timeout
00127 
00128             g_pin_timeout_flag  = 0;
00129 
00130             timeout_protocol();
00131 
00132         }
00133 
00134         if (g_next_state  != g_current_state ) {
00135 
00136             pc.printf("TRANSITION \n");
00137 
00138             screen_selection(); //  the screen is determined by the value of g_next_state
00139 
00140             g_current_state  = g_next_state ;
00141 
00142             pc.printf("Current State = %d \n",g_current_state );
00143 
00144         }
00145 
00146         sleep();
00147 
00148 
00149     }
00150 
00151 }
00152 
00153 
00154 // Initialisation Functions
00155 
00156 void init_serial()
00157 {
00158     // Ensure terminal software matches
00159     pc.baud(115200);
00160 }
00161 
00162 void init_K64F()
00163 {
00164     // on-board LEDs are active-low, so they are set high to turn them off.
00165     r_led = 1;
00166     g_led = 1;
00167     b_led = 1;
00168 
00169     // since the on-board switches have external pull-ups, we should disable the internal pull-down
00170     // resistors that are enabled by default using InterruptIn
00171     sw2.mode(PullNone);
00172     sw3.mode(PullNone);
00173 
00174 }
00175 
00176 void init_buttons()
00177 {
00178     // all external buttons trigger the relevant ISR when the voltage at the K64F pin falls
00179     button_0.fall(&button_0_isr);
00180     button_1.fall(&button_1_isr);
00181     button_c.fall(&button_c_isr);
00182 
00183     // enable the internal pull-down resistors
00184     button_0.mode(PullDown);
00185     button_1.mode(PullDown);
00186     button_c.mode(PullDown);
00187 
00188 }
00189 
00190 void init_variables()
00191 {
00192 
00193     one_second_distance  = 0;
00194     one_second_avg_distance  = 0;
00195     initial_setting_distance  = 0;
00196 
00197     pin_counter  = 0;
00198     incorrect_pin_flag  = 0;
00199 
00200     setting_distance_counter  = 0;
00201     intruder_distance_counter  = 0;
00202 
00203     g_button_0_flag  = 0;
00204     g_button_1_flag  = 0;
00205     g_button_c_flag  = 0;
00206     g_setting_distance_flag  = 0;
00207     g_intruder_distance_flag  = 0;
00208     g_pin_timeout_flag  = 0;
00209 
00210     g_current_state  = 0;
00211 
00212     reset_entered_pin();
00213     
00214     acc.enable();
00215 
00216 }
00217 
00218 // Interrupt Service Routines (ISRs)
00219 
00220 void setting_distance_isr()
00221 {
00222     g_setting_distance_flag  = 1;
00223 
00224 }
00225 
00226 void intruder_distance_isr()
00227 {
00228     g_intruder_distance_flag  = 1;
00229 
00230 }
00231 
00232 void button_0_isr()
00233 {
00234     g_button_0_flag  = 1;
00235 
00236 }
00237 
00238 void button_1_isr()
00239 {
00240     g_button_1_flag  = 1;
00241 
00242 }
00243 
00244 void button_c_isr()
00245 {
00246     g_button_c_flag  = 1;
00247 
00248 }
00249 
00250 void led_buzzer_isr()
00251 {
00252     g_led_buzzer_flag  = 1;
00253 
00254 }
00255 
00256 void acc_isr()
00257 {
00258     g_acc_flag  = 1;
00259 
00260 }
00261 
00262 void pin_timeout_isr()
00263 {
00264     g_pin_timeout_flag  = 1;
00265 
00266 }
00267 
00268 void setting_screen_isr()
00269 {
00270     g_setting_screen_flag  = 1;
00271 
00272 }
00273 
00274 // Button Functions
00275 
00276 void button_0_protocol()
00277 {
00278 
00279     // states 2, 3, 6 and 7 require a pin to be entered
00280 
00281     if (g_current_state  == 2) {
00282 
00283         if (pin_counter  < 4) { // entered_pin has four elements
00284 
00285             entered_pin [pin_counter ] = 0;
00286 
00287             pin_counter ++;
00288 
00289         }
00290 
00291         enter_pin(); // enter_pin() prints '*' to the LCD
00292 
00293     }
00294 
00295     else if (g_current_state  == 3) {
00296 
00297         if (pin_counter  < 4) { // entered_pin has four elements
00298 
00299             entered_pin [pin_counter ] = 0;
00300 
00301             pin_counter ++;
00302 
00303         }
00304 
00305         enter_pin(); // Prints '*' to the LCD
00306 
00307     }
00308 
00309     else if (g_current_state  == 6) {
00310 
00311         if (pin_counter  < 4) { // entered_pin has four elements
00312 
00313             entered_pin [pin_counter ] = 0;
00314 
00315             pin_counter ++;
00316 
00317         }
00318 
00319         enter_pin(); // Prints '*' to the LCD
00320 
00321     }
00322 
00323     else if (g_current_state  == 7) { // entered_pin has four elements
00324 
00325         if (pin_counter  < 4) {
00326 
00327             entered_pin [pin_counter ] = 0;
00328 
00329             pin_counter ++;
00330 
00331         }
00332 
00333         enter_pin(); // Prints '*' to the LCD
00334 
00335     }
00336 
00337     else {
00338 
00339         g_next_state  = fsm[g_current_state ].nextState[0]; // Filters through fsm to find the next state
00340 
00341     }
00342 
00343 }
00344 
00345 void button_1_protocol()
00346 {
00347 
00348     // States 2, 3, 6 and 7 require a pin to be entered
00349 
00350     if (g_current_state  == 2) {
00351 
00352         if (pin_counter  < 4) {  // entered_pin has four elements
00353 
00354             entered_pin [pin_counter ] = 1;
00355 
00356             pin_counter ++;
00357 
00358         }
00359 
00360         enter_pin(); // enter_pin() prints '*' to the LCD
00361 
00362     }
00363 
00364     else if (g_current_state  == 3) {
00365 
00366         if (pin_counter  < 4) {  // entered_pin has four elements
00367 
00368             entered_pin [pin_counter ] = 1;
00369 
00370             pin_counter ++;
00371 
00372         }
00373 
00374         enter_pin(); // Prints '*' to the LCD
00375 
00376     }
00377 
00378     else if (g_current_state  == 6) {
00379 
00380         if (pin_counter  < 4) {  // entered_pin has four elements
00381 
00382             entered_pin [pin_counter ] = 1;
00383 
00384             pin_counter ++;
00385 
00386         }
00387 
00388         enter_pin(); // Prints '*' to the LCD
00389 
00390     }
00391 
00392     else if (g_current_state  == 7) {
00393 
00394         if (pin_counter  < 4) {  // entered_pin has four elements
00395 
00396             entered_pin [pin_counter ] = 1;
00397 
00398             pin_counter ++;
00399 
00400         }
00401 
00402         enter_pin(); // Prints '*' to the LCD
00403 
00404     }
00405 
00406     else {
00407 
00408         g_next_state  = fsm[g_current_state ].nextState[1]; // Filters through fsm to find the next state
00409 
00410     }
00411 
00412 }
00413 
00414 void button_c_protocol()
00415 {
00416 
00417     // States 2, 3, 6 and 7 require a pin to be entered
00418     // The screen only progresses if the correct pin is entered
00419 
00420     if (g_current_state  == 2) {
00421 
00422         screen_progression();
00423 
00424     }
00425 
00426     else if (g_current_state  == 3) {
00427 
00428         screen_progression();
00429 
00430     }
00431 
00432     else if (g_current_state  == 6) {
00433 
00434         screen_progression();
00435 
00436     }
00437 
00438     else if (g_current_state  == 7) {
00439 
00440         screen_progression();
00441 
00442     }
00443 
00444     else {
00445 
00446         g_next_state  = fsm[g_current_state ].nextState[2]; // filters through fsm to find the next state
00447 
00448     }
00449 
00450 }
00451 
00452 // State Functions
00453 
00454 void state_0_screen()
00455 {
00456     lcd.clear();
00457 
00458     // print the screen
00459 
00460     lcd.printString("Mobile",27,1);
00461 
00462     lcd.printString("Security",18,2);
00463 
00464     lcd.printString("System",27,3);
00465 
00466     lcd_border();
00467 
00468     // set LED and buzzer
00469 
00470     led_alarm = 0;
00471 
00472     buzzer.write(0.0);
00473 
00474     lcd.refresh();
00475 
00476 }
00477 
00478 void state_1_screen()
00479 {
00480     lcd.clear();
00481 
00482     // print the screen
00483 
00484     lcd.printString("Set Alarm",15,1);
00485 
00486     lcd.printString("Set New Pin",9,4);
00487 
00488     lcd_border();
00489 
00490     for (int i = 0; i < WIDTH; i++) {
00491 
00492         lcd.setPixel(i,24);
00493 
00494     }
00495 
00496     // detach tickers
00497 
00498     pin_timeout.detach();
00499     
00500     accelerometer.detach();
00501     
00502     intruder_distance.detach();
00503     
00504     alerts.detach();
00505 
00506     // set LED and buzzer
00507 
00508     led_alarm = 0;
00509 
00510     buzzer.write(0.0);
00511 
00512     // reset counter
00513 
00514     pin_timeout_counter  = 0;
00515 
00516     lcd.refresh();
00517 
00518 }
00519 
00520 void state_2_screen()
00521 {
00522     lcd.clear();
00523 
00524     // print the screen
00525 
00526     lcd.printString("Enter 4 Digit",3,1);
00527 
00528     lcd.printString("Pin Below",15,2);
00529 
00530     pin_text_box();
00531 
00532     lcd_border();
00533 
00534     // set the LED and buzzer
00535 
00536     led_alarm = 0;
00537 
00538     buzzer.write(0.0);
00539 
00540     lcd.refresh();
00541 
00542 }
00543 
00544 void state_3_screen()
00545 {
00546     lcd.clear();
00547 
00548     // print the screen
00549 
00550     lcd.printString("Enter New 4",9,1);
00551 
00552     lcd.printString("Digit Pin",15,2);
00553 
00554     pin_text_box();
00555 
00556     lcd_border();
00557 
00558     // set the LED and buzzer
00559 
00560     led_alarm = 0;
00561 
00562     buzzer.write(0.0);
00563 
00564     lcd.refresh();
00565 
00566 }
00567 
00568 void state_4_screen()
00569 {
00570     lcd.clear();
00571 
00572     // print the screen
00573 
00574     lcd.printString("Setting",21,1);
00575 
00576     lcd_border();
00577 
00578     lcd.drawRect(0,20,84,5,0); // blank setting bar
00579 
00580     lcd.printString("0%",41,4); // 0% of setting complete
00581 
00582     // reset counter
00583 
00584     setting_alarm_counter  = 0;
00585 
00586     // set LED and buzzer
00587 
00588     led_alarm = 0;
00589 
00590     buzzer.write(0.0);
00591 
00592     // attach tickers
00593 
00594     alerts.attach(&led_buzzer_isr,0.5);
00595     
00596     setting_distance.attach(&setting_distance_isr,0.1);
00597     
00598     accelerometer.attach(&acc_isr,0.1);
00599 
00600     setting_screen.attach(&setting_screen_isr,0.28);
00601     
00602     // get setting accelerometer data
00603     
00604     get_axis_data();
00605     
00606     pc.printf("Printing setting accelerometer data....\n");    
00607     
00608     setting_acc_X  = acc_X ;
00609     
00610     setting_acc_Y  = acc_Y ;
00611     
00612     setting_acc_Z  = acc_Z ;
00613     
00614     pc.printf("X = %.4f \nY = %.4f \nZ = %.4f \n",setting_acc_X ,setting_acc_Y ,setting_acc_Z );
00615     
00616     lcd.refresh();
00617 
00618 }
00619 
00620 void state_5_screen()
00621 {
00622     lcd.clear();
00623 
00624     // print the screen
00625 
00626     lcd.printString("Alarm Set",15,1);
00627 
00628     lcd.printString("DEACTIVATE?",9,4);
00629 
00630     lcd_border();
00631 
00632     for (int i = 0; i < WIDTH; i++) {
00633 
00634         lcd.setPixel(i,20);
00635 
00636     }
00637 
00638     // detach tickers
00639 
00640     alerts.detach();
00641     
00642     accelerometer.detach();
00643 
00644     setting_screen.detach();
00645     
00646     // attach ticker
00647     
00648     intruder_distance.attach(&intruder_distance_isr,0.1);
00649 
00650     // set LED and buzzer
00651 
00652     led_alarm = 1;
00653 
00654     buzzer.write(0.5);
00655 
00656     // turn off buzzer after 1 second
00657 
00658     buzz.attach(&alarm_setting_buzz,1);
00659 
00660     lcd.refresh();
00661 
00662 }
00663 
00664 void state_6_screen()
00665 {
00666     lcd.clear();
00667 
00668     // print the screen
00669 
00670     lcd.printString("DEACTIVATE",12,1);
00671 
00672     lcd.printString("Enter Pin",15,2);
00673 
00674     lcd_border();
00675 
00676     pin_text_box();
00677 
00678     // set the LED and buzzer
00679 
00680     led_alarm = 0;
00681 
00682     buzzer.write(0.0);
00683 
00684     // attach ticker
00685 
00686     alerts.attach(&led_buzzer_isr,0.5);
00687     
00688     // detach ticker
00689     
00690     intruder_distance.detach();
00691     lcd.refresh();
00692 
00693 }
00694 
00695 void state_7_screen()
00696 {
00697     lcd.clear();
00698 
00699     // print the screen
00700 
00701     lcd.printString("INTRUDER",18,1);
00702 
00703     lcd.printString("Enter Pin",15,2);
00704 
00705     lcd_border();
00706 
00707     pin_text_box();
00708 
00709     // set the LED and buzzer
00710 
00711     led_alarm = 0;
00712 
00713     buzzer.write(0.0);
00714 
00715     // attach tickers
00716 
00717     alerts.attach(&led_buzzer_isr,0.5);
00718 
00719     pin_timeout.attach(&pin_timeout_isr,1);
00720     
00721     // detach ticker
00722     
00723     intruder_distance.detach();
00724 
00725     lcd.refresh();
00726 
00727 }
00728 
00729 void state_8_screen()
00730 {
00731     lcd.clear();
00732 
00733     // print the screen
00734 
00735     lcd.printString("ALARM",27,1);
00736 
00737     lcd.printString("TRIGGERED",15,2);
00738 
00739     lcd.printString("Menu?",27,4);
00740 
00741     lcd_border();
00742 
00743     // set the LED and buzzer
00744 
00745     led_alarm = 0;
00746 
00747     buzzer.write(0.0);
00748 
00749     // detach tickers
00750 
00751     alerts.detach();
00752 
00753     pin_timeout.detach();
00754 
00755     // attach ticker
00756 
00757     alerts.attach(&led_buzzer_isr,0.2);
00758 
00759     lcd.refresh();
00760 }
00761 
00762 void lcd_border()
00763 {
00764     lcd.drawRect(0,0,83,47,0);
00765 
00766     lcd.drawRect(1,1,81,45,0);
00767 
00768 }
00769 
00770 void pin_text_box()
00771 {
00772     if (g_current_state  == 7) {
00773         
00774         lcd.drawRect(1,30,15,10,0);     // transparent box
00775         lcd.drawRect(15,30,15,10,0);    // transparent box
00776         lcd.drawRect(29,30,15,10,0);    // transparent box
00777         lcd.drawRect(43,30,15,10,0);    // transparent box
00778         lcd.drawRect(57,30,15,10,0);    // transparent box
00779         lcd.drawRect(71,30,15,10,1);    // filled box
00780         
00781     }
00782     
00783     else {
00784         
00785         lcd.drawRect(1,30,15,10,1);     // filled box
00786         lcd.drawRect(15,30,15,10,0);    // transparent box
00787         lcd.drawRect(29,30,15,10,0);    // transparent box
00788         lcd.drawRect(43,30,15,10,0);    // transparent box
00789         lcd.drawRect(57,30,15,10,0);    // transparent box
00790         lcd.drawRect(71,30,15,10,1);    // filled box
00791         
00792     }        
00793         
00794 }
00795 
00796 void screen_progression()
00797 {
00798     if (g_current_state  == 3) { // set new pin state
00799 
00800         if (pin_counter  > 3) { // if a four digit pin has been entered
00801 
00802             change_pin(); // save entered_pin to the SD card
00803 
00804             read_pin(); // read the pin from the SD card to the set_pin array
00805 
00806             g_next_state  = fsm[g_current_state ].nextState[2];
00807 
00808         }
00809 
00810         else {
00811 
00812             g_next_state  = fsm[g_current_state ].nextState[3];
00813 
00814         }
00815 
00816     }
00817 
00818     else { // if the current state is either 2, 6 or 7
00819 
00820         check_pin(); // sets incorrect_pin_flag to 1 if entered pin doesn't match set_pin
00821 
00822         if (incorrect_pin_flag  == 1) {
00823 
00824             incorrect_pin_flag  = 0;
00825 
00826             g_next_state  = fsm[g_current_state ].nextState[3]; // go to previous state or alarm triggered state
00827 
00828             pc.printf("g_next_state = %d\n",g_next_state );
00829 
00830         }
00831 
00832         else {
00833 
00834             g_next_state  = fsm[g_current_state ].nextState[2]; // proceed
00835 
00836             pc.printf("g_next_state = %d\n",g_next_state );
00837 
00838         }
00839 
00840     }
00841 
00842 }
00843 
00844 void screen_selection()
00845 {
00846 
00847     reset_entered_pin(); // re-set each element of the entered pin array to -1
00848 
00849     if (g_next_state  == 0) {
00850 
00851         state_0_screen();
00852 
00853     }
00854 
00855     else if (g_next_state  == 1) {
00856 
00857         state_1_screen();
00858 
00859     }
00860 
00861     else if (g_next_state  == 2) {
00862 
00863         state_2_screen();
00864 
00865     }
00866 
00867     else if (g_next_state  == 3) {
00868 
00869         state_3_screen();
00870 
00871     }
00872 
00873     else if (g_next_state  == 4) {
00874 
00875         state_4_screen();
00876 
00877     }
00878 
00879     else if (g_next_state  == 5) {
00880 
00881         state_5_screen();
00882 
00883     }
00884 
00885     else if (g_next_state  == 6) {
00886 
00887         state_6_screen();
00888 
00889     }
00890 
00891     else if (g_next_state  == 7) {
00892 
00893         state_7_screen();
00894 
00895     }
00896 
00897     else {
00898 
00899         state_8_screen();
00900 
00901     }
00902 
00903 }
00904 
00905 // Screen Animation Functions
00906 
00907 void setting_animation()
00908 {
00909     setting_alarm_counter  = setting_alarm_counter  + 5; //  increment setting_alarm_counter in steps of five
00910 
00911     if (setting_alarm_counter  < 101) {
00912 
00913         lcd.drawRect(0,20,setting_alarm_counter ,5,1); // prints the loading bar to the LCD
00914 
00915         // format how the percentage is displayed depending on its value
00916         if (setting_alarm_counter  < 10) {
00917 
00918             length  = sprintf(buffer ,"0%d%%",setting_alarm_counter );
00919 
00920         }
00921 
00922         else {
00923 
00924             length  = sprintf(buffer ,"%d%%",setting_alarm_counter );
00925 
00926         }
00927 
00928         if (length  <= 14) { // if length of string will fit on the screen ( WIDTH = 6 * 24 = 84 pixels )
00929 
00930             if (setting_alarm_counter  < 100) {
00931 
00932                 lcd.printString(buffer ,33,4);
00933 
00934             }
00935 
00936             else {
00937 
00938                 lcd.printString(buffer ,30,4); // this string is four characters long and is therefore shifted to the left of the screen
00939 
00940             }
00941 
00942         }
00943 
00944         lcd.refresh(); // refresh screen
00945 
00946     }
00947 }
00948 
00949 void timeout_protocol()
00950 {
00951 
00952     pin_timeout_counter ++;
00953 
00954     seconds_till_timeout  = 21 - pin_timeout_counter ; // timeout counts down from 20
00955 
00956     if (seconds_till_timeout  == 0) { // if 20 seconds passes
00957 
00958         g_next_state  = 8;
00959 
00960         pin_timeout_counter  = 0;
00961 
00962     }
00963 
00964     else { // less than 20 seconds has passed
00965 
00966         // These if statements manage the formatting of the number of seconds remaining
00967 
00968         if (seconds_till_timeout  >= 10) {
00969 
00970             length  = sprintf(buffer ,"%d",seconds_till_timeout );
00971 
00972         }
00973 
00974         else {
00975 
00976             length  = sprintf(buffer ,"0%d",seconds_till_timeout );
00977 
00978         }
00979 
00980         // if the length of the formatted string will fit on the screen print it ( WIDTH = 14 * 6 = 84 pixels )
00981 
00982         if (length  <= 14)
00983             lcd.printString(buffer ,3,4);
00984 
00985         lcd.refresh();
00986 
00987     }
00988 
00989 }
00990 
00991 // Read Distance Functions
00992 
00993 void get_setting_distance()
00994 {
00995     distance [setting_distance_counter ] = srf02.getDistanceCm(); // distance array stores 10 distance readings
00996 
00997     setting_distance_counter ++;
00998 
00999     if (setting_distance_counter  == 10) { // if distance array has 10 new readings (arrays are zero indexed)
01000 
01001         setting_distance.detach();
01002 
01003         calculate_setting_distance(); // find the average of the 10 distance readings
01004 
01005     }
01006 
01007 }
01008 
01009 void get_intruder_distance()
01010 {
01011 
01012     distance [intruder_distance_counter ] = srf02.getDistanceCm(); // distance array stores 10 distance readings
01013 
01014     intruder_distance_counter ++;
01015 
01016     if (intruder_distance_counter  == 10) { // if distance array has 10 new readings (arrays are zero indexed)
01017 
01018         intruder_distance.detach();
01019 
01020         calculate_intruder_distance(); // find the average of the 10 distance readings
01021 
01022     }
01023 
01024 }
01025 
01026 void calculate_setting_distance()
01027 {
01028 
01029     for (int i = 0; i < 10; i++) {
01030 
01031         one_second_distance  = one_second_distance  + distance [i]; // add all 10 readings together
01032 
01033     }
01034 
01035     initial_setting_distance  = (one_second_distance  / 10);
01036 
01037     pc.printf("Initial Setting Distance = %.2f cm\n",initial_setting_distance );
01038 
01039     setting_distance_counter  = 0;
01040 
01041     one_second_distance  = 0;
01042 
01043     transition.attach(&screen_5_transition,5); // transition to the set screen in five seconds
01044 
01045 }
01046 
01047 void calculate_intruder_distance()
01048 {
01049 
01050     for (int i = 0; i < 10; i++) {
01051 
01052         one_second_distance  = one_second_distance  + distance [i]; // add all 10 distance readings together
01053 
01054     }
01055 
01056     one_second_avg_distance  = one_second_distance  / 10;
01057 
01058     pc.printf("Intruder Distance = %.2f cm\n",one_second_avg_distance );
01059 
01060     intruder_distance_counter  = 0;
01061 
01062     one_second_distance  = 0;
01063 
01064 
01065     if (one_second_avg_distance  > (1.1*initial_setting_distance )) { // if the current average distance is 10% greater than the initial_setting_distance set off the alarm
01066 
01067         g_next_state  = 7;
01068 
01069         g_current_state  = 7;
01070 
01071         screen_selection();
01072 
01073     }
01074 
01075     else if (one_second_avg_distance  < (0.9 * initial_setting_distance )) { // if the current average distance is 10% smaller than the initial_setting_distance set off the alarm
01076 
01077         g_next_state  = 7;
01078 
01079         g_current_state  = 7;
01080 
01081         screen_selection();
01082 
01083     }
01084 
01085     else { // if the current distance is within 10% of the initial_setting_distance carry on reading distance
01086 
01087         intruder_distance.attach(&intruder_distance_isr,0.1);
01088 
01089     }
01090 
01091 }
01092 
01093 // Timeout Functions
01094 
01095 void screen_5_transition()
01096 {
01097     g_next_state  = 5;
01098     
01099     accelerometer.detach();
01100 
01101     screen_selection(); // sets the LCD to screen 5
01102 
01103     g_current_state  = 5;
01104 
01105 }
01106 
01107 void pin_confirm()
01108 {
01109     lcd.clear();
01110 
01111     lcd_border();
01112 
01113     lcd.printString("Press C",24,1);
01114 
01115     lcd.printString("to",36,2);
01116 
01117     lcd.printString("Confirm",24,3);
01118 
01119     if (g_current_state  == 7) {
01120 
01121         lcd.drawRect(1,30,15,10,0);
01122 
01123     }
01124 
01125     lcd.refresh();
01126 
01127 }
01128 
01129 void state_1_transition()
01130 {
01131     g_next_state  = 1;
01132 
01133     screen_selection();
01134 
01135     g_current_state  = 1;
01136     
01137     alerts.detach();
01138 
01139 }
01140 
01141 void alarm_setting_buzz()
01142 {
01143 
01144     buzzer.write(0.0); // turn off buzzer
01145 
01146 }
01147 
01148 // Accelerometer Function
01149 
01150 void get_axis_data()
01151 {
01152 
01153     acc.getX(acc_X );
01154     acc.getY(acc_Y );
01155     acc.getZ(acc_Z );
01156 
01157 }
01158 
01159 void compare_axis_data()
01160 {
01161     get_axis_data();
01162 
01163     pc.printf("X1 = %.4f \nY1 = %.4f \nZ1 = %.4f \n",acc_X ,acc_Y ,acc_Z );
01164 
01165     if (abs(acc_X ) < 0.2f*abs(setting_acc_X )) { // Uses abs() due to possibility of axis dat being negative
01166         
01167         device_tampered_protocol();
01168         
01169         pc.printf("acc_X < setting_acc_X\n");
01170         
01171     }
01172     
01173     else if (abs(acc_X ) > 5.0f*abs(setting_acc_X )) { // Uses abs() due to possibility of axis dat being negative
01174         
01175         device_tampered_protocol();
01176         
01177         pc.printf("acc_X > setting_acc_X\n");
01178         
01179     }    
01180     
01181     else if (abs(acc_Y ) < 0.2f*abs(setting_acc_Y )) { // Uses abs() due to possibility of axis dat being negative
01182         
01183         device_tampered_protocol();
01184         
01185         pc.printf("acc_Y < setting_acc_Y\n");
01186 
01187     }
01188     
01189     else if (abs(acc_Y ) > 5.0f*abs(setting_acc_Y )) { // Uses abs() due to possibility of axis dat being negative
01190         
01191         device_tampered_protocol();
01192         
01193         pc.printf("acc_Y > setting_acc_Y\n");
01194         
01195     } 
01196     
01197     else if (abs(acc_Z ) < 0.2f*abs(setting_acc_Z )) { // Uses abs() due to possibility of axis dat being negative
01198         
01199         device_tampered_protocol();
01200         
01201         pc.printf("acc_Z < setting_acc_Z\n");
01202 
01203     }
01204     
01205     else if (abs(acc_Z ) > 5.0f*abs(setting_acc_Z )) { // Uses abs() due to possibility of axis dat being negative
01206         
01207         device_tampered_protocol();
01208         
01209         pc.printf("acc_Z > setting_acc_Z\n");
01210         
01211     } 
01212     
01213     else {
01214         
01215         // Do nothing
01216         
01217     }    
01218 
01219 }
01220 
01221 void device_tampered_protocol()
01222 {
01223     setting_distance.detach(); // stop reading distance
01224 
01225     setting_screen.detach(); // stop setting screen animation
01226     
01227     accelerometer.detach(); // stop taking readings from the accelerometer
01228 
01229     transition.detach(); // cancel scheduled transition to state 5
01230     
01231     alerts.detach(); // turn off LED and Buzzer
01232 
01233     lcd.clear();
01234 
01235     lcd.printString("DEVICE MOVED",6,1);
01236 
01237     lcd.printString("SETTING",21,3);
01238 
01239     lcd.printString("INCOMPLETE",12,4);
01240 
01241     lcd_border();
01242 
01243     lcd.refresh();
01244     
01245     alerts.attach(&led_buzzer_isr,0.35);
01246     
01247     tamper_transition.attach(&state_1_transition,3); // transition to state 1 due to the device been tampered with whilst setting
01248 
01249 }
01250 
01251 // Pin Functions
01252 
01253 void enter_pin()
01254 {
01255 
01256     // current value of the pin_counter determines where '*' is printed
01257 
01258     if (pin_counter  == 1) {
01259 
01260         lcd.printString("*",20,4);
01261 
01262     }
01263 
01264     else if (pin_counter  == 2) {
01265 
01266         lcd.printString("*",34,4);
01267 
01268     }
01269 
01270     else if (pin_counter  == 3) {
01271 
01272         lcd.printString("*",48,4);
01273 
01274     }
01275 
01276     else if (pin_counter  == 4) {
01277 
01278         lcd.printString("*",62,4);
01279 
01280         confirm.attach(&pin_confirm,0.5); // 'Press C to Confirm' screen
01281 
01282     }
01283 
01284 }
01285 
01286 void reset_entered_pin()
01287 {
01288     for (int i = 0; i < 4; i++) {
01289 
01290         entered_pin [i] = -1; // sets each element of entered_pin to -1
01291 
01292     }
01293 
01294     pin_counter  = 0;
01295 
01296 }
01297 
01298 void check_pin()
01299 {
01300     for (int i = 0; i < 4; i++) {
01301 
01302         if (entered_pin [i] != set_pin [i]) { // if any element of entered_pin doesn't match set_pin
01303 
01304             pc.printf("entered pin doesn't match set pin...");
01305 
01306             incorrect_pin_flag  = 1;
01307 
01308             break;
01309 
01310         }
01311 
01312     }
01313 
01314 }
01315 
01316 void change_pin()
01317 {
01318     delete_file("/sd/test.txt");
01319 
01320     pin  = fopen("/sd/test.txt", "a");
01321 
01322     if (pin  == NULL) {  // if it can't open the file then print error message
01323 
01324         pc.printf("Error! Unable to open file!\n");
01325 
01326     }
01327 
01328     else {
01329 
01330         pc.printf("Writing to file....\n");
01331 
01332         for(int i = 0; i < 4; i++) {
01333 
01334             int pin_element = entered_pin [i];;  // pin_element takes on the value of each element in the entered_pin array
01335 
01336             fprintf(pin , "%d,%d\n",i,pin_element);  // pin element is printed to SD card file (CSV)
01337         }
01338 
01339         for(int i = 0; i < 4; i++) {
01340 
01341             pc.printf("[%d] %d\n",i,entered_pin [i]); // print to PC for debugging purposes
01342         }
01343 
01344         pc.printf("Done.\n");
01345 
01346         fclose(pin );  // close the file after writing
01347 
01348     }
01349 
01350 }
01351 
01352 void read_pin()
01353 {
01354     pin  = fopen("/sd/test.txt", "r");
01355 
01356     int i = 0;
01357 
01358     pc.printf("Reading into set_pin array...\n");
01359 
01360     while (fscanf(pin , "%d,%d",&index_array [i],&set_pin [i]) != EOF) { // EOF ---> End of File
01361         i++;  // read data into array and increment index
01362     }
01363 
01364     fclose(pin );  // close the file after reading
01365 
01366     for(int i = 0; i < 4 ; i++) {
01367 
01368         pc.printf("[%d] %d\n",i,set_pin [i]); // print to PC for debugging purposes
01369 
01370     }
01371 
01372     pc.printf("Done.\n");
01373 
01374 }
01375 
01376 void delete_file(char filename[])
01377 {
01378     pc.printf("Deleting file '%s'...",filename);
01379 
01380     FILE *fp = fopen(filename, "r");  // try and open file
01381 
01382     if (fp != NULL) {  // if it does open...
01383 
01384         fclose(fp);    // close it
01385 
01386         remove(filename);  // delete the file
01387 
01388         pc.printf("Done!\n");
01389 
01390     }
01391 
01392     // if it can't be opened, it doesn't exist and can't be deleted
01393 
01394 }