Distance Sensor Embedded Systems Project SID: 200864479 James Erringham-Bruce
Dependencies: N5110 SRF02-JEB mbed
Main/DataController.h@3:ab75e6a12701, 2016-05-05 (annotated)
- 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?
User | Revision | Line number | New contents of line |
---|---|---|---|
ll13j7b | 1:f82359c58eda | 1 | /** |
ll13j7b | 1:f82359c58eda | 2 | @file DataController.h |
ll13j7b | 1:f82359c58eda | 3 | @brief Header file containing common data. |
ll13j7b | 1:f82359c58eda | 4 | @author James Erringham-Bruce |
ll13j7b | 1:f82359c58eda | 5 | @date March 2015 |
ll13j7b | 1:f82359c58eda | 6 | */ |
ll13j7b | 1:f82359c58eda | 7 | |
ll13j7b | 1:f82359c58eda | 8 | #ifndef DATACONTROLLER_H |
ll13j7b | 1:f82359c58eda | 9 | #define DATACONTROLLER_H |
ll13j7b | 1:f82359c58eda | 10 | |
ll13j7b | 1:f82359c58eda | 11 | #include "mbed.h" |
ll13j7b | 1:f82359c58eda | 12 | #include "N5110.h" |
ll13j7b | 1:f82359c58eda | 13 | #include "SRF02.h" |
ll13j7b | 1:f82359c58eda | 14 | |
ll13j7b | 1:f82359c58eda | 15 | #define nx 84 |
ll13j7b | 1:f82359c58eda | 16 | #define ny 48 |
ll13j7b | 1:f82359c58eda | 17 | |
ll13j7b | 1:f82359c58eda | 18 | |
ll13j7b | 1:f82359c58eda | 19 | /** |
ll13j7b | 2:01f697b856de | 20 | @namespace LCD 5110 |
ll13j7b | 2:01f697b856de | 21 | @brief outputs displaying the pin connections for the screen to mbed |
ll13j7b | 1:f82359c58eda | 22 | */ |
ll13j7b | 1:f82359c58eda | 23 | N5110 lcd (PTE26 , PTA0 , PTC4 , PTD0 , PTD2 , PTD1 , PTC5); |
ll13j7b | 1:f82359c58eda | 24 | /** |
ll13j7b | 2:01f697b856de | 25 | @namespace PC |
ll13j7b | 2:01f697b856de | 26 | @brief serial input and output lines for printing data |
ll13j7b | 1:f82359c58eda | 27 | */ |
ll13j7b | 1:f82359c58eda | 28 | Serial pc(USBTX,USBRX); |
ll13j7b | 1:f82359c58eda | 29 | /** |
ll13j7b | 2:01f697b856de | 30 | @namespace DistanceSensor |
ll13j7b | 2:01f697b856de | 31 | @brief I2CC pins to SRF02 Sensor |
ll13j7b | 1:f82359c58eda | 32 | */ |
ll13j7b | 1:f82359c58eda | 33 | SRF02 ds(PTE25, PTE24); |
ll13j7b | 1:f82359c58eda | 34 | /** |
ll13j7b | 2:01f697b856de | 35 | @namespace r_led |
ll13j7b | 2:01f697b856de | 36 | @brief digital output for the on board red LED |
ll13j7b | 1:f82359c58eda | 37 | */ |
ll13j7b | 1:f82359c58eda | 38 | DigitalOut r_led(LED_RED); |
ll13j7b | 2:01f697b856de | 39 | |
ll13j7b | 2:01f697b856de | 40 | /** |
ll13j7b | 2:01f697b856de | 41 | @namespace g_led |
ll13j7b | 2:01f697b856de | 42 | @brief digital output for the on board green LED |
ll13j7b | 2:01f697b856de | 43 | */ |
ll13j7b | 1:f82359c58eda | 44 | DigitalOut g_led(LED_GREEN); |
ll13j7b | 2:01f697b856de | 45 | |
ll13j7b | 2:01f697b856de | 46 | /** |
ll13j7b | 2:01f697b856de | 47 | @namespace b_led |
ll13j7b | 2:01f697b856de | 48 | @brief digital output for the on board blue LED |
ll13j7b | 2:01f697b856de | 49 | */ |
ll13j7b | 1:f82359c58eda | 50 | DigitalOut b_led(LED_BLUE); |
ll13j7b | 2:01f697b856de | 51 | |
ll13j7b | 2:01f697b856de | 52 | /** |
ll13j7b | 2:01f697b856de | 53 | @namespace LED |
ll13j7b | 2:01f697b856de | 54 | @brief digital output for warning LED |
ll13j7b | 2:01f697b856de | 55 | */ |
ll13j7b | 1:f82359c58eda | 56 | DigitalOut LED(PTC2); |
ll13j7b | 1:f82359c58eda | 57 | |
ll13j7b | 2:01f697b856de | 58 | /** |
ll13j7b | 2:01f697b856de | 59 | @namespace backlight |
ll13j7b | 2:01f697b856de | 60 | @brief defining the LED for 5110 LCD screen |
ll13j7b | 2:01f697b856de | 61 | */ |
ll13j7b | 1:f82359c58eda | 62 | PwmOut backLight(PTC3); |
ll13j7b | 1:f82359c58eda | 63 | /** |
ll13j7b | 2:01f697b856de | 64 | @namespace Buzzer |
ll13j7b | 2:01f697b856de | 65 | @brief definig the pin for the buzzer |
ll13j7b | 1:f82359c58eda | 66 | */ |
ll13j7b | 2:01f697b856de | 67 | PwmOut buzzerAlert(PTA2); |
ll13j7b | 2:01f697b856de | 68 | /** |
ll13j7b | 2:01f697b856de | 69 | @namespace backButton |
ll13j7b | 2:01f697b856de | 70 | @brief defining the button to go back as an action |
ll13j7b | 2:01f697b856de | 71 | */ |
ll13j7b | 2:01f697b856de | 72 | InterruptIn backButton(PTB19); |
ll13j7b | 2:01f697b856de | 73 | /** |
ll13j7b | 2:01f697b856de | 74 | @namespace interruptSelector |
ll13j7b | 2:01f697b856de | 75 | @brief defining the pin for buttons |
ll13j7b | 2:01f697b856de | 76 | */ |
ll13j7b | 1:f82359c58eda | 77 | InterruptIn interruptSelector(PTB18); |
ll13j7b | 1:f82359c58eda | 78 | /** |
ll13j7b | 2:01f697b856de | 79 | @namespace Potentiometer |
ll13j7b | 2:01f697b856de | 80 | @brief defining the pins for the potentiometer |
ll13j7b | 1:f82359c58eda | 81 | */ |
ll13j7b | 1:f82359c58eda | 82 | AnalogIn selectorPot(PTB2); |
ll13j7b | 1:f82359c58eda | 83 | /** |
ll13j7b | 2:01f697b856de | 84 | @namespace sw2 |
ll13j7b | 2:01f697b856de | 85 | @brief defining the on board switch 2 |
ll13j7b | 1:f82359c58eda | 86 | */ |
ll13j7b | 1:f82359c58eda | 87 | InterruptIn sw2(SW2); |
ll13j7b | 2:01f697b856de | 88 | /** |
ll13j7b | 2:01f697b856de | 89 | @namespace sw3 |
ll13j7b | 2:01f697b856de | 90 | @brief defining the on board sw3 |
ll13j7b | 2:01f697b856de | 91 | */ |
ll13j7b | 1:f82359c58eda | 92 | InterruptIn sw3(SW3); |
ll13j7b | 1:f82359c58eda | 93 | /** |
ll13j7b | 1:f82359c58eda | 94 | @namespace Ticker |
ll13j7b | 1:f82359c58eda | 95 | @brief defining the tickers used |
ll13j7b | 1:f82359c58eda | 96 | */ |
ll13j7b | 1:f82359c58eda | 97 | Ticker ticker; |
ll13j7b | 2:01f697b856de | 98 | |
ll13j7b | 2:01f697b856de | 99 | /** |
ll13j7b | 2:01f697b856de | 100 | @namespace Menu |
ll13j7b | 2:01f697b856de | 101 | @brief defining the tickers used |
ll13j7b | 2:01f697b856de | 102 | */ |
ll13j7b | 2:01f697b856de | 103 | Ticker menuTicker; |
ll13j7b | 2:01f697b856de | 104 | |
ll13j7b | 1:f82359c58eda | 105 | /** |
ll13j7b | 1:f82359c58eda | 106 | @namespace averageTicker |
ll13j7b | 1:f82359c58eda | 107 | @brief defining the tickers used |
ll13j7b | 1:f82359c58eda | 108 | */ |
ll13j7b | 1:f82359c58eda | 109 | Ticker ticker2; |
ll13j7b | 2:01f697b856de | 110 | |
ll13j7b | 1:f82359c58eda | 111 | /** |
ll13j7b | 1:f82359c58eda | 112 | @namespace settingsTicker |
ll13j7b | 1:f82359c58eda | 113 | @brief defining the tickers used |
ll13j7b | 1:f82359c58eda | 114 | */ |
ll13j7b | 1:f82359c58eda | 115 | Ticker settingsTicker; |
ll13j7b | 2:01f697b856de | 116 | |
ll13j7b | 1:f82359c58eda | 117 | /** |
ll13j7b | 2:01f697b856de | 118 | function to display an error warning message on screen to the user as a visual display when they are out of bounds |
ll13j7b | 1:f82359c58eda | 119 | */ |
ll13j7b | 1:f82359c58eda | 120 | void readingErrors(); |
ll13j7b | 1:f82359c58eda | 121 | /** |
ll13j7b | 2:01f697b856de | 122 | function to initialise the mbed with the on board LED's and switches |
ll13j7b | 1:f82359c58eda | 123 | */ |
ll13j7b | 1:f82359c58eda | 124 | void init_K64F(); |
ll13j7b | 1:f82359c58eda | 125 | /** |
ll13j7b | 2:01f697b856de | 126 | function that displays the menu to the user for the user to interact with and select the various options displayed on screen |
ll13j7b | 1:f82359c58eda | 127 | @param int average - average reading of distance |
ll13j7b | 1:f82359c58eda | 128 | */ |
ll13j7b | 1:f82359c58eda | 129 | void mainMenu(); |
ll13j7b | 1:f82359c58eda | 130 | /** |
ll13j7b | 2:01f697b856de | 131 | function to return back to the main menu at the trigger of an interrupt |
ll13j7b | 2:01f697b856de | 132 | */ |
ll13j7b | 2:01f697b856de | 133 | void goBackToMenu(); |
ll13j7b | 2:01f697b856de | 134 | /** |
ll13j7b | 2:01f697b856de | 135 | function to manage the flag of the ticker |
ll13j7b | 1:f82359c58eda | 136 | */ |
ll13j7b | 1:f82359c58eda | 137 | void timer_isr(); |
ll13j7b | 1:f82359c58eda | 138 | /** |
ll13j7b | 2:01f697b856de | 139 | function to manage the flag of the ticker |
ll13j7b | 1:f82359c58eda | 140 | */ |
ll13j7b | 1:f82359c58eda | 141 | void timer_isr2(); |
ll13j7b | 2:01f697b856de | 142 | /** |
ll13j7b | 2:01f697b856de | 143 | function to enable the user to go back to previous screens once they are within different settings screens |
ll13j7b | 2:01f697b856de | 144 | */ |
ll13j7b | 2:01f697b856de | 145 | void backButton_isr(); |
ll13j7b | 1:f82359c58eda | 146 | /** |
ll13j7b | 2:01f697b856de | 147 | function to enable the user to go back to previous screens once they are within different settings screens |
ll13j7b | 2:01f697b856de | 148 | */ |
ll13j7b | 2:01f697b856de | 149 | void forwardButton_isr(); |
ll13j7b | 2:01f697b856de | 150 | /** |
ll13j7b | 2:01f697b856de | 151 | function to manage flag of the ticker |
ll13j7b | 2:01f697b856de | 152 | */ |
ll13j7b | 2:01f697b856de | 153 | void selector_isr(); |
ll13j7b | 2:01f697b856de | 154 | /** |
ll13j7b | 2:01f697b856de | 155 | function to manage the flag of the ticker |
ll13j7b | 1:f82359c58eda | 156 | */ |
ll13j7b | 1:f82359c58eda | 157 | void settings_timer_isr(); |
ll13j7b | 1:f82359c58eda | 158 | /** |
ll13j7b | 2:01f697b856de | 159 | function to manage the flag of the ticker |
ll13j7b | 1:f82359c58eda | 160 | */ |
ll13j7b | 2:01f697b856de | 161 | void menu_isr(); |
ll13j7b | 1:f82359c58eda | 162 | /** |
ll13j7b | 2:01f697b856de | 163 | function for initialising the serial port for the pc |
ll13j7b | 1:f82359c58eda | 164 | */ |
ll13j7b | 2:01f697b856de | 165 | void init_serial(); |
ll13j7b | 1:f82359c58eda | 166 | /** |
ll13j7b | 2:01f697b856de | 167 | function to initialise the serial connection, LCD screen, tickers and the mbed with the on board switches & LEDs |
ll13j7b | 1:f82359c58eda | 168 | */ |
ll13j7b | 2:01f697b856de | 169 | void init_all(); |
ll13j7b | 2:01f697b856de | 170 | |
ll13j7b | 2:01f697b856de | 171 | |
ll13j7b | 2:01f697b856de | 172 | |
ll13j7b | 2:01f697b856de | 173 | int getAverageReadingInch(); /*!< function that returns int 'getAverageReadingInch' */ |
ll13j7b | 2:01f697b856de | 174 | |
ll13j7b | 2:01f697b856de | 175 | char buffer[14]; /*!< buffer length for character length to print text on screen*/ |
ll13j7b | 2:01f697b856de | 176 | |
ll13j7b | 2:01f697b856de | 177 | volatile int g_timer_flag = 0; /*!< to ensure the compiler does not stick an object with permanent logic 1 so we can change the logic |
ll13j7b | 2:01f697b856de | 178 | of this object to high or low when needed by the user */ |
ll13j7b | 2:01f697b856de | 179 | |
ll13j7b | 2:01f697b856de | 180 | volatile int g_timer_flag2 = 0;/*!< to ensure the compiler does not stick an object with permanent logic 1 so we can change the logic |
ll13j7b | 2:01f697b856de | 181 | of this object to high or low when needed by the user */ |
ll13j7b | 2:01f697b856de | 182 | |
ll13j7b | 2:01f697b856de | 183 | volatile int settings_timer_flag = 0; /*!< to ensure the compiler does not stick an object with permanent logic 1 so we can change the logic |
ll13j7b | 2:01f697b856de | 184 | of this object to high or low when needed by the user */ |
ll13j7b | 2:01f697b856de | 185 | |
ll13j7b | 2:01f697b856de | 186 | volatile int menu_flag = 0; /*!< to ensure the compiler does not stick an object with permanent logic 1 so we can change the logic |
ll13j7b | 2:01f697b856de | 187 | of this object to high or low when needed by the user */ |
ll13j7b | 2:01f697b856de | 188 | |
ll13j7b | 2:01f697b856de | 189 | volatile int selector_flag = 0; /*!< to ensure the compiler does not stick an object with permanent logic 1 so we can change the logic |
ll13j7b | 2:01f697b856de | 190 | of this object to high or low when needed by the user */ |
ll13j7b | 1:f82359c58eda | 191 | |
ll13j7b | 2:01f697b856de | 192 | volatile int back_flag = 0; /*!< to ensure the compiler does not stick an object with permanent logic 1 so we can change the logic |
ll13j7b | 2:01f697b856de | 193 | of this object to high or low when needed by the user */ |
ll13j7b | 2:01f697b856de | 194 | |
ll13j7b | 2:01f697b856de | 195 | volatile bool isInMenu = 0; /*!< to ensure the compiler does not stick an object with permanent logic 1 so we can change the logic |
ll13j7b | 2:01f697b856de | 196 | of this object to high or low when needed by the user */ |
ll13j7b | 2:01f697b856de | 197 | |
ll13j7b | 2:01f697b856de | 198 | volatile bool isInNumerical = 0; /*!< to ensure the compiler does not stick an object with permanent logic 1 so we can change the logic |
ll13j7b | 2:01f697b856de | 199 | of this object to high or low when needed by the user */ |
ll13j7b | 2:01f697b856de | 200 | |
ll13j7b | 2:01f697b856de | 201 | volatile bool isInGraph = 0; /*!< to ensure the compiler does not stick an object with permanent logic 1 so we can change the logic |
ll13j7b | 2:01f697b856de | 202 | of this object to high or low when needed by the user */ |
ll13j7b | 1:f82359c58eda | 203 | |
ll13j7b | 2:01f697b856de | 204 | volatile bool isInRadar = 0; /*!< to ensure the compiler does not stick an object with permanent logic 1 so we can change the logic |
ll13j7b | 2:01f697b856de | 205 | of this object to high or low when needed by the user */ |
ll13j7b | 2:01f697b856de | 206 | |
ll13j7b | 2:01f697b856de | 207 | volatile bool isInSettings = 0; /*!< to ensure the compiler does not stick an object with permanent logic 1 so we can change the logic |
ll13j7b | 2:01f697b856de | 208 | of this object to high or low when needed by the user */ |
ll13j7b | 2:01f697b856de | 209 | |
ll13j7b | 2:01f697b856de | 210 | //*****************************************************************************************************// |
ll13j7b | 2:01f697b856de | 211 | |
ll13j7b | 2:01f697b856de | 212 | // END OF GLOBAL VARIABLES & FUNCTIONS // |
ll13j7b | 2:01f697b856de | 213 | |
ll13j7b | 2:01f697b856de | 214 | //*****************************************************************************************************// |
ll13j7b | 2:01f697b856de | 215 | |
ll13j7b | 2:01f697b856de | 216 | |
ll13j7b | 2:01f697b856de | 217 | // FUNCTION TO RETURN AVERAGE VALUE IN CM |
ll13j7b | 1:f82359c58eda | 218 | int getAverageReadingCm() |
ll13j7b | 1:f82359c58eda | 219 | { |
ll13j7b | 1:f82359c58eda | 220 | int sum = 0; |
ll13j7b | 1:f82359c58eda | 221 | /// algorithm to implement ouput of average reading after 10 readings |
ll13j7b | 1:f82359c58eda | 222 | for( int j = 0; j < 10; j++) { |
ll13j7b | 1:f82359c58eda | 223 | int i = ds.getDistanceCm(); /// function that keeps saving sensor values as int i |
ll13j7b | 1:f82359c58eda | 224 | sum += i; // increase the sum 10 times |
ll13j7b | 1:f82359c58eda | 225 | } |
ll13j7b | 1:f82359c58eda | 226 | float avg = sum / 10; // sum ÷ number of samples = average formula |
ll13j7b | 1:f82359c58eda | 227 | return avg; // return the average value |
ll13j7b | 1:f82359c58eda | 228 | } |
ll13j7b | 1:f82359c58eda | 229 | |
ll13j7b | 1:f82359c58eda | 230 | // FUNCTION TO RETURN AVERAGE VALUE IN INCH |
ll13j7b | 1:f82359c58eda | 231 | int getAverageReadingInch() |
ll13j7b | 1:f82359c58eda | 232 | { |
ll13j7b | 1:f82359c58eda | 233 | int sum = 0; |
ll13j7b | 1:f82359c58eda | 234 | /// algorithm to implement ouput of average reading after 10 readings |
ll13j7b | 1:f82359c58eda | 235 | for( int j = 0; j < 10; j++) { |
ll13j7b | 1:f82359c58eda | 236 | int i = ds.getDistanceInch(); /// function that keeps saving sensor values as int i |
ll13j7b | 1:f82359c58eda | 237 | sum += i; // increase the sum 10 times |
ll13j7b | 1:f82359c58eda | 238 | } |
ll13j7b | 1:f82359c58eda | 239 | float avg = sum / 10; // sum ÷ number of samples = average formula |
ll13j7b | 1:f82359c58eda | 240 | return avg; // return the average value |
ll13j7b | 1:f82359c58eda | 241 | } |
ll13j7b | 1:f82359c58eda | 242 | |
ll13j7b | 2:01f697b856de | 243 | // FUNCTION TO RETURN BACK TO THE MAIN MENU ON PRESS |
ll13j7b | 2:01f697b856de | 244 | void backButton_isr() |
ll13j7b | 2:01f697b856de | 245 | { |
ll13j7b | 2:01f697b856de | 246 | back_flag = 1; /// flip the flag to go back |
ll13j7b | 2:01f697b856de | 247 | /// then reset all screen flags that control the while loops |
ll13j7b | 2:01f697b856de | 248 | isInSettings = 0; |
ll13j7b | 2:01f697b856de | 249 | isInRadar = 0; |
ll13j7b | 2:01f697b856de | 250 | isInGraph = 0; |
ll13j7b | 2:01f697b856de | 251 | isInNumerical = 0; |
ll13j7b | 2:01f697b856de | 252 | isInMenu = 1; |
ll13j7b | 2:01f697b856de | 253 | } |
ll13j7b | 2:01f697b856de | 254 | |
ll13j7b | 2:01f697b856de | 255 | // FUNCTION TO FLIP THE FLAG OF THE SETTINGS TICKER |
ll13j7b | 2:01f697b856de | 256 | void selector_isr() |
ll13j7b | 2:01f697b856de | 257 | { |
ll13j7b | 2:01f697b856de | 258 | selector_flag = 1; /// flip the flag of the settings ticker |
ll13j7b | 2:01f697b856de | 259 | } |
ll13j7b | 2:01f697b856de | 260 | |
ll13j7b | 2:01f697b856de | 261 | // FUNCTION TO GO BACK TO MAIN MENU |
ll13j7b | 2:01f697b856de | 262 | void goBackToMenu() |
ll13j7b | 2:01f697b856de | 263 | { |
ll13j7b | 2:01f697b856de | 264 | /// if the back flag is activated then reset the flag and go back to menu |
ll13j7b | 2:01f697b856de | 265 | if (back_flag) { |
ll13j7b | 2:01f697b856de | 266 | back_flag = 0; |
ll13j7b | 2:01f697b856de | 267 | |
ll13j7b | 2:01f697b856de | 268 | } |
ll13j7b | 2:01f697b856de | 269 | } |
ll13j7b | 2:01f697b856de | 270 | |
ll13j7b | 2:01f697b856de | 271 | //*****************************************************************************************************// |
ll13j7b | 2:01f697b856de | 272 | |
ll13j7b | 2:01f697b856de | 273 | // END OF FUNCTION IMPLEMENTATIONS // |
ll13j7b | 2:01f697b856de | 274 | |
ll13j7b | 2:01f697b856de | 275 | //*****************************************************************************************************// |
ll13j7b | 2:01f697b856de | 276 | |
ll13j7b | 2:01f697b856de | 277 | |
ll13j7b | 2:01f697b856de | 278 | |
ll13j7b | 2:01f697b856de | 279 | |
ll13j7b | 2:01f697b856de | 280 | |
ll13j7b | 2:01f697b856de | 281 | |
ll13j7b | 2:01f697b856de | 282 | |
ll13j7b | 1:f82359c58eda | 283 | #endif |