Distance Sensor Embedded Systems Project SID: 200864479 James Erringham-Bruce

Dependencies:   N5110 SRF02-JEB mbed

Committer:
ll13j7b
Date:
Thu May 05 14:22:27 2016 +0000
Revision:
3:ab75e6a12701
Parent:
2:01f697b856de
Final Code Submission

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ll13j7b 1:f82359c58eda 1 /**
ll13j7b 1:f82359c58eda 2 @file main.cpp
ll13j7b 1:f82359c58eda 3
ll13j7b 1:f82359c58eda 4 @brief Program implementation
ll13j7b 1:f82359c58eda 5 */
ll13j7b 1:f82359c58eda 6
ll13j7b 1:f82359c58eda 7 #include "MainMenu.h"
ll13j7b 1:f82359c58eda 8 #include "intro.h"
ll13j7b 1:f82359c58eda 9
ll13j7b 1:f82359c58eda 10 Menu menu; // passing in the class data for the main menu
ll13j7b 1:f82359c58eda 11
ll13j7b 1:f82359c58eda 12 // MAIN //
ll13j7b 1:f82359c58eda 13 int main()
ll13j7b 1:f82359c58eda 14 {
ll13j7b 2:01f697b856de 15 init_all(); /// initialise the screen, mbed, tickers
ll13j7b 2:01f697b856de 16 wait(2); // waiting for power to settle across the distance sensor
ll13j7b 2:01f697b856de 17 performAnimatedIntro(); ///animated introduction screen performed
ll13j7b 2:01f697b856de 18 menu.mainMenu(); /// load the menu screen
ll13j7b 1:f82359c58eda 19 }
ll13j7b 1:f82359c58eda 20
ll13j7b 1:f82359c58eda 21 // FUNCTION TO INITIALISE THE MBED
ll13j7b 1:f82359c58eda 22 void init_K64F()
ll13j7b 1:f82359c58eda 23 {
ll13j7b 1:f82359c58eda 24 /// on-board LEDs are active-low, so set pin high to turn them off.
ll13j7b 1:f82359c58eda 25 r_led = 1;
ll13j7b 1:f82359c58eda 26 g_led = 1;
ll13j7b 1:f82359c58eda 27 b_led = 1;
ll13j7b 1:f82359c58eda 28
ll13j7b 1:f82359c58eda 29 /// since the on-board switches have external pull-ups, we should disable the internal pull-down
ll13j7b 1:f82359c58eda 30 /// resistors that are enabled by default using InterruptIn
ll13j7b 1:f82359c58eda 31 sw2.mode(PullNone);
ll13j7b 1:f82359c58eda 32 sw3.mode(PullNone);
ll13j7b 1:f82359c58eda 33
ll13j7b 1:f82359c58eda 34 }
ll13j7b 1:f82359c58eda 35
ll13j7b 1:f82359c58eda 36 // TIMER FLAG 1
ll13j7b 1:f82359c58eda 37 void timer_isr()
ll13j7b 1:f82359c58eda 38 {
ll13j7b 1:f82359c58eda 39 g_timer_flag = 1; /// set flag in ISR
ll13j7b 1:f82359c58eda 40 }
ll13j7b 1:f82359c58eda 41
ll13j7b 1:f82359c58eda 42 // TIMER FLAG 2
ll13j7b 1:f82359c58eda 43 void timer_isr2()
ll13j7b 1:f82359c58eda 44 {
ll13j7b 1:f82359c58eda 45 g_timer_flag2 = 1; /// set flag in ISR
ll13j7b 1:f82359c58eda 46 }
ll13j7b 1:f82359c58eda 47
ll13j7b 1:f82359c58eda 48 // TIMER FLAG 2
ll13j7b 1:f82359c58eda 49 void settings_timer_isr()
ll13j7b 1:f82359c58eda 50 {
ll13j7b 1:f82359c58eda 51 settings_timer_flag = 1; /// set flag in ISR
ll13j7b 1:f82359c58eda 52 }
ll13j7b 1:f82359c58eda 53
ll13j7b 1:f82359c58eda 54
ll13j7b 1:f82359c58eda 55 // FUNCTION TO ELIMINATE AND REDUCE ERRORS WHEN OBTAINING THE AVERAGE READING
ll13j7b 1:f82359c58eda 56 void readingErrors()
ll13j7b 1:f82359c58eda 57 {
ll13j7b 2:01f697b856de 58 /// check if the distance is out of range
ll13j7b 2:01f697b856de 59 /// if so, display warning text and activate LED or Buzzer depending on the settings
ll13j7b 1:f82359c58eda 60 lcd.printString("distance is",15,2); // printing error message
ll13j7b 1:f82359c58eda 61 lcd.printString("unreadable",17,3);
ll13j7b 2:01f697b856de 62 if (LED_Toggle) {
ll13j7b 2:01f697b856de 63 LED = 1; // warning LED activated
ll13j7b 2:01f697b856de 64 } else {
ll13j7b 2:01f697b856de 65 LED = 0; // warning LED activated
ll13j7b 2:01f697b856de 66 }
ll13j7b 2:01f697b856de 67 if (buzzer_Toggle) {
ll13j7b 2:01f697b856de 68 buzzerAlert = 0.5; // 50% duty cycle
ll13j7b 2:01f697b856de 69 } else {
ll13j7b 2:01f697b856de 70 buzzerAlert = 0; // turn off buzzer (0% duty cycle)
ll13j7b 2:01f697b856de 71 }
ll13j7b 2:01f697b856de 72 // both can be disabled in the settings
ll13j7b 1:f82359c58eda 73 }
ll13j7b 1:f82359c58eda 74
ll13j7b 1:f82359c58eda 75 // FUNCTION TO SET UP THE SYSTEM
ll13j7b 1:f82359c58eda 76 void init_all()
ll13j7b 1:f82359c58eda 77 {
ll13j7b 2:01f697b856de 78 /// intitialise all tickers and button modes
ll13j7b 2:01f697b856de 79 /// including the K64F & N5110
ll13j7b 1:f82359c58eda 80 lcd.init(); // initialise the screen
ll13j7b 1:f82359c58eda 81 init_K64F(); // initialise the mbed
ll13j7b 1:f82359c58eda 82 init_serial(); // initialise the serial
ll13j7b 1:f82359c58eda 83 lcd.clear(); // clear the screen from all previous data
ll13j7b 1:f82359c58eda 84 ticker.attach(&timer_isr,1); // attach ticker 1 - ( 1 second fire rate )
ll13j7b 1:f82359c58eda 85 ticker2.attach(&timer_isr2,5); // attach ticker 2 - ( 3 second fire rate )
ll13j7b 1:f82359c58eda 86 settingsTicker.attach(&settings_timer_isr,0.5); // attach settings ticker - ( 1/2 second fire rate )
ll13j7b 2:01f697b856de 87 menuTicker.attach(&menu_isr,0.5); // attach settings ticker - ( 1/2 second fire rate )
ll13j7b 1:f82359c58eda 88 backButton.mode(PullDown);
ll13j7b 2:01f697b856de 89 backButton.fall(&backButton_isr);
ll13j7b 2:01f697b856de 90 interruptSelector.mode(PullDown);
ll13j7b 2:01f697b856de 91 interruptSelector.fall(&selector_isr);
ll13j7b 1:f82359c58eda 92 }
ll13j7b 1:f82359c58eda 93
ll13j7b 2:01f697b856de 94 //*****************************************************************************************************//
ll13j7b 2:01f697b856de 95
ll13j7b 2:01f697b856de 96 // END OF FUNCTION IMPLEMENTATIONS //
ll13j7b 2:01f697b856de 97
ll13j7b 2:01f697b856de 98 //*****************************************************************************************************//
ll13j7b 2:01f697b856de 99
ll13j7b 1:f82359c58eda 100
ll13j7b 1:f82359c58eda 101
ll13j7b 2:01f697b856de 102
ll13j7b 2:01f697b856de 103