ELEC2645 (2015/16) / Mbed 2 deprecated SensorMatic_Project

Dependencies:   N5110-JDB SRF02 SoftPWM-JDB TMP102 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.h Source File

main.h

Go to the documentation of this file.
00001 /**
00002 @file main.h
00003 @brief Header File containing Defines, Function Prototypes and Global Variables.
00004 @brief Revision 1.0.
00005 @author Jack David Berriman - 200836574
00006 @date Summer 2016
00007 */
00008 
00009 #ifndef MAIN_H // Define Guard - Prevents Multiple Definitions of Same File.
00010 #define MAIN_H
00011 
00012 
00013 // ------------------------- INCLUDES ------------------------- //
00014 
00015 #include "mbed.h"
00016 #include "N5110.h" // Include Library for Nokia N5110 LCD Screen.
00017 #include "SRF02.h" // Include Library for SRF02 Distance Sensor.
00018 #include "TMP102.h" // Include Library for TMP102 Temperature Sensor.
00019 #include "SoftPWM.h" // Include Library for Software PWM.
00020 
00021 
00022 // ------------------------- DEFINES ------------------------- //
00023 
00024 /** Pixel Per Degree = 46/35... 46 Pixels in 'Thermometer', Max 35º = 1.314285714.
00025 */
00026 #define PIXEL_PER_DEGREE 1.314285714
00027 
00028 /** Pixels Per CM = 72/250... 72 Pixels in Box, Max 250cm = 0.288.
00029 */
00030 #define PIXEL_PER_CM 0.288
00031 
00032 
00033 // ------------------------- CLASS REFERENCES ------------------------- //
00034 
00035 /**
00036 @namespace screen
00037 @brief Object to Class for Nokia N5110 Library.
00038 */
00039 N5110 screen(PTE26 , PTA0 , PTC4 , PTD0 , PTD2 , PTD1 , PTC3);
00040 //           VCC,    SCE,   RST,   D/C,   MOSI,  SCLK,   LED
00041 
00042 /**
00043 @namespace srf02
00044 @brief Object to Class for SRF02 Distance Sensor.
00045 */
00046 SRF02 srf02(I2C_SDA,I2C_SCL);
00047 
00048 /**
00049 @namespace tmp102
00050 @brief Object to Class for TMP102 Temperature Sensor.
00051 */
00052 TMP102 tmp102(I2C_SDA,I2C_SCL);
00053 
00054 /**
00055 @namespace pc
00056 @brief Object to Class for Serial Library
00057 */
00058 Serial pc(USBTX,USBRX); // UART Connection for PC - Used for Debugging (Displays Output on 'CoolTerm.exe'.
00059 
00060 
00061 // ------------------------- TICKERS ------------------------- //
00062 
00063 /**
00064 @namespace systemTicker
00065 @brief Object for a Ticker which keeps the whole system in time.
00066 */
00067 Ticker systemTicker;
00068 
00069 
00070 // ------------------------- SYSTEM INPUTS ------------------------- //
00071 
00072 /**
00073 @namespace controller
00074 @brief Potentiometer connected to AnalogIn Pin PTB10 (Used to Select Menu Items).
00075 */
00076 AnalogIn controller(PTB10); // Potentiometer
00077 
00078 /**
00079 @namespace backward
00080 @brief Push Button connected to InterruptIn Pin PTB19 (Used to Return to Main Menu).
00081 */
00082 InterruptIn backward(PTB19); // Backward Button
00083 
00084 /**
00085 @namespace forward
00086 @brief Push Button connected to InterruptIn Pin PTB18 (Various Uses - Menu Selection & Toggling Options...).
00087 */
00088 InterruptIn forward(PTB18); // Forward Button
00089 
00090 
00091 // ------------------------- SYSTEM OUTPUTS ------------------------- //
00092 
00093 /**
00094 @namespace piezo
00095 @brief Piezo Buzzer connected to PwmOut Pin PTA2 (Used w/ Distance Sensor for Audible Warning).
00096 */
00097 PwmOut piezo(PTA2); // Piezo (PWM)
00098 
00099 /**
00100 @namespace red_led
00101 @brief Red LED connected to DigitalOut Pin PTC9 (Used w/ Distance and Temperature Sensor for Visual Warning).
00102 */
00103 DigitalOut red_led(PTC9); // Red LED (Digital)
00104 
00105 /**
00106 @namespace yellow_led
00107 @brief Yellow LED connected to DigitalOut Pin PTC0 (Used w/ Distance and Temperature Sensor for Visual Warning).
00108 */
00109 DigitalOut yellow_led(PTC0); // Yellow LED (Digital)
00110 
00111 /**
00112 @namespace green_led
00113 @brief Green LED connected to DigitalOut Pin PTC7 (Used w/ Distance and Temperature Sensor for Visual Warning).
00114 */
00115 DigitalOut green_led(PTC7); // Green LED (Digital)
00116 
00117 
00118 // ------------------------- SOFT PWM ------------------------- //
00119 
00120 // Third Party Library for Generating Software PWM - https://developer.mbed.org/users/komaida424/code/SoftPWM/
00121 
00122 /**
00123 @namespace redPWM
00124 @brief Red LED connected to DigitalOut Pin PTC9 (BUT used with 'SoftPWM' so it can flash periodically).
00125 */
00126 SoftPWM redPWM(PTC9);
00127 
00128 /**
00129 @namespace yellowPWM
00130 @brief Yelow LED connected to DigitalOut Pin PTC0 (BUT used with 'SoftPWM' so it can flash periodically).
00131 */
00132 SoftPWM yellowPWM(PTC0);
00133 
00134 /**
00135 @namespace greenPWM
00136 @brief Green LED connected to DigitalOut Pin PTC7 (BUT used with 'SoftPWM' so it can flash periodically).
00137 */
00138 SoftPWM greenPWM(PTC7);
00139 
00140 
00141 // ------------------------- K64F ON-BOARD PERIPHERALS ------------------------- //
00142 
00143 /**
00144 @namespace r_led
00145 @brief Object for the K64F On-Board Red LED - Never used but necessary to turn it off.
00146 */
00147 DigitalOut r_led(LED_RED);
00148 
00149 /**
00150 @namespace g_led
00151 @brief Object for the K64F On-Board Green LED - Never used but necessary to turn it off.
00152 */
00153 DigitalOut g_led(LED_GREEN);
00154 
00155 /**
00156 @namespace b_led
00157 @brief Object for the K64F On-Board Blue LED - Never used but necessary to turn it off.
00158 */
00159 DigitalOut b_led(LED_BLUE);
00160 
00161 
00162 // ------------------------- ISR FUNCTIONS (INTERRUPT SERVICE ROUTINE) ------------------------- //
00163 
00164 /**
00165 @brief ISR Function for the 'systemTicker' Flag (ISR = Interrupt Service Routine).
00166 */
00167 void system_isr();
00168 
00169 /**
00170 @brief ISR Function for the 'forward' Button InterruptIn (ISR = Interrupt Service Routine).
00171 */
00172 void forward_isr();
00173 
00174 /**
00175 @brief ISR Function for the 'backward' Button InterruptIn (ISR = Interrupt Service Routine).
00176 */
00177 void backward_isr();
00178 
00179 
00180 // ------------------------- VOID/BOOL/INT FUNCTIONS ------------------------- //
00181 
00182 /**
00183 @brief Function clears the buffer - Any text or set pixels on the screen.
00184 */
00185 void clearAll(); // Clears Buffer
00186 
00187 /**
00188 @brief Initialises the Nokia Screen by setting various parameters (i.e Button Pull-Ups/Downs and RGB LEDs).
00189 */
00190 void init_K64F(); // Initialise K64F
00191 
00192 /**
00193 @brief Initialises the Piezo Buzzer by setting various parameters (i.e Initial Period and Duty Cycle).
00194 */
00195 void init_buzzer(); // Initialise Buzzer
00196 
00197 /**
00198 @brief Initialises all other aspects surrounding the programme.
00199 */
00200 void init_program(); // Initialise Program
00201 
00202 /**
00203 @brief Contains strings which display an introductory message on the screen before the programme begins.
00204 */
00205 void welcomeScreen(); // Displays Start-Up Splash Screen
00206 
00207 /**
00208 @brief Function for displaying text and navigating around the Main Menu (Sensors/Options).
00209 */
00210 void mainMenu(); // Function for Main Menu (Sensors/Options)
00211 
00212 /**
00213 @brief Function for displaying text and navigating around the Sensor Menu (Distance/Temperature).
00214 */
00215 void sensorMenu(); // Function for Sensors Menu (Distance/Temperature)
00216 
00217 /**
00218 @brief Function for displaying text and navigating around the Options Menu (LEDs/Sound/Colour).
00219 */
00220 void optionsMenu(); // Function for Options Menu (LEDs/Sound/Colour)
00221 
00222 /**
00223 @brief Function for displaying the output from the distance sensor numerically and graphically.
00224 */
00225 void distanceDisplay(); // Function for Distance Display
00226 
00227 /**
00228 @brief Function for displaying the output from the temperature sensor numerically and graphically.
00229 */
00230 void temperatureDisplay(); // Function for Temperature Screen
00231 
00232 /**
00233 @brief Returns the programme back to the Main Menu from any screen when the 'backward' Button is pressed.
00234 */
00235 void returnToMenu(); // Returns To Main Menu (When Pressed)
00236 
00237 /**
00238 @brief Prints the Position of the Cursor - Used in the Main and Sensor Menus Only.
00239 @brief The function must be used in conjuction with 'getCursorPosition'.
00240 @param cursor - The 'Bank' Number (0-5) is written to it
00241 */
00242 void printCursorPositionMain(int cursor); // Print the Position of Cursor in Main Menu & Sensor Menu
00243 
00244 /**
00245 @brief Prints the Position of the Cursor - Used in the OptionsOnly.
00246 @brief The function must be used in conjuction with 'getCursorPosition'.
00247 @param cursor - The 'Bank' Number (0-5) is written to it
00248 @param stateOfLEDTrigger - Check LED Trigger for 1 or 0
00249 @param stateOfSoundTrigger - Check Sound Trigger for 1 or 0
00250 */
00251 void printCursorPositionOptions(int cursor, int stateOfLEDTrigger, int stateOfSoundTrigger); // Print the Position of Cursor in Options Menu
00252 
00253 /**
00254 @brief Reads value of Potentiometer ('controller') and assigns it to a Bank (0-5) through Multiplication.
00255 @brief Ensures that cursor is always set to a Bank - Anything greater than 5 is set to Bank 5.
00256 */
00257 int getCursorPosition(); // Gets Position of Cursor from Potentiometer
00258 
00259 /**
00260 @brief Uses Software PWM to cause the Red/Yellow/Green LEDs to flash at a specified period.
00261 @brief Used with the Distance Sensor when the distance value is below 15 cm.
00262 @returns Bank - Value of Bank (0-5) 
00263 */
00264 void LEDs_Flashing(); // Flashes LEDs Using SoftPWM
00265 
00266 /**
00267 @brief Uses Software PWM to cause the Red/Yellow/Green LEDs to stop flashing.
00268 @brief Called in order to explicitly tell the K64F to stop using SoftPWM.
00269 */
00270 void cancel_LEDs_Flashing(); // Stops LEDs Flashing Using SoftPWM
00271 
00272 /**
00273 @brief Function for turning ON/OFF LEDs depending on the distance from the SRF02 Sensor.
00274 */
00275 void LEDs_Distance(int d); // Function for Turning LEDs ON/OFF Depending on Distance
00276 
00277 /**
00278 @brief Function for turning ON/OFF LEDs depending on the temperature from the TMP102 Sensor.
00279 */
00280 void LEDs_Temperature(int t); // Function for Turning LEDs ON/OFF Depending on Temperature
00281 
00282 /**
00283 @brief Turns all LEDs ON.
00284 */
00285 void allLEDOn(); // Turns ON All LEDs
00286 
00287 /**
00288 @brief Turns all LEDs OFF.
00289 */
00290 void allLEDOff(); // Turns OFF All LEDs
00291 
00292 /**
00293 @brief Turns ON the Piezo and its outputs a constant noise.
00294 */
00295 void buzzerOn(); // Turns Buzzer ON
00296 
00297 /**
00298 @brief Turns ON the Piezo and uses a period that simulates being 'far away' - Less frequent bleeps.
00299 */
00300 void buzzerFar(); // 'Beeps' Buzzer Periodically ('Far' Distance)
00301 
00302 /**
00303 @brief Turns ON the Piezo and uses a period that simulates being a 'safe, normal' distance - Moderately frequent bleeps.
00304 */
00305 void buzzerNormal(); // 'Beeps' Buzzer Periodically ('Normal' Distance)
00306 
00307 /**
00308 @brief Turns ON the Piezo and uses a period that simulates being 'close' to an object - Highly freqeuent bleeps.
00309 */
00310 void buzzerClose(); // 'Beeps' Buzzer Periodically ('Close' Distance)
00311 
00312 /**
00313 @brief After the 'forward' Button is pressed - Checks the status of a 'Trigger' (triggerLEDs).
00314 @brief Depending on Trigger status, it prevents or enables the LEDs from illuminating in the programme.
00315 @returns 'stateOfLED' - A 1 or 0 which is used to either fill or unfill the Cursor.
00316 */
00317 bool toggleLEDs(); // Toggles LEDs (Options Menu)
00318 
00319 /**
00320 @brief After the 'forward' Button is pressed - Checks the status of a 'Trigger' (triggerSound).
00321 @brief Depending on Trigger status, it prevents or enables the Piezo from sounding in the programme.
00322 @returns 'stateOfSound' - A 1 or 0 which is used to either fill or unfill the Cursor.
00323 */
00324 bool toggleSound(); // Toggles Sound (Options Menu)
00325 
00326 /**
00327 @brief After the 'forward' Button is pressed - Checks the status of a 'Trigger' (triggerColour).
00328 @brief Depending on Trigger status, it changes the colour of the screen: Black-on-White or White-on-Black
00329 */
00330 void toggleColour(); // Toggles Colour - Black-on-White or White-on-Black (Options Menu)
00331 
00332 
00333 // ------------------------- STRUCTS ------------------------- //
00334 
00335 /**
00336 @struct AverageValues
00337 @brief Takes the average over 10 reading from each sensor
00338 @brief Precautionary measure to reduce the effect of random errors and improves accuracy.
00339 */
00340 struct AverageValues { // Struct For Calculating Average Values (Temp/Dist Sensor)
00341     int averageTemperature;
00342     int averageDistance;
00343 };
00344 
00345 
00346 // ------------------------- GLOBAL VARIABLES ------------------------- // 
00347 
00348 /**
00349 @brief Each Screen Number denotes a different navigation menu.
00350 @brief Initially set to zero because 'MainMenu' = Screen Number = 0.
00351 */
00352 int screenNumber = 0; // Sets Initial Screen Number to 0 (Main Menu)
00353 
00354 /**
00355 @brief Converts value of cursor into a pixel number (0-48) - Main Menu or Sensor Menu Only.
00356 @brief Conversion is passed into a function for drawing the option 'selector'.
00357 */
00358 int y_axis_rect_main; // Y-Position of Screen Cursor (Main Menu & Sensor Menu)
00359 
00360 /**
00361 @brief Converts value of cursor into a pixel number (0-48) - Options Menu Only.
00362 @brief Conversion is passed into a function for drawing the option 'selector'.
00363 */
00364 int y_axis_rect_options; // // Y-Position of Screen Cursor (Options Menu)
00365 
00366 
00367 // ------------------------- GLOBAL FLAGS ------------------------- //
00368 
00369 /**
00370 @brief Sets the Flag for 'systemTicker' initially to zero.
00371 */
00372 volatile int g_system_flag = 0; // Flag for 'systemTicker'
00373 
00374 /**
00375 @brief Sets the Flag for 'forward' Button Interrupt initially to zero.
00376 */
00377 volatile int g_forward_flag = 0; // Flag for Forward Button Interrupt
00378 
00379 /**
00380 @brief Sets the Flag for 'backward' Button Interrupt initially to zero.
00381 */
00382 volatile int g_backward_flag = 0; // Flag for Backward Button Interrupt
00383 
00384 
00385 // ------------------------- VOLATILE & NON-VOLATILE BOOLS ------------------------- //
00386 
00387 /**
00388 @brief Sets the status of the variable from the 'ToggleLED()' function.
00389 @brief Used for filling or unfilling the option 'selector'.
00390 */
00391 bool stateOfLED = 0; // Initial State of LED = 0
00392 
00393 /**
00394 @brief Sets the status of the variable from the 'ToggleSound()' function.
00395 @brief Used for filling or unfilling the option 'selector'.
00396 */
00397 bool stateOfSound = 0; // Initial State of Sound = 0
00398 
00399 /**
00400 @brief Trigger is used for enable/disable LED option.
00401 @brief If trigger equals 0 = disable LEDs.
00402 @brief If trigger equals 1 = enable LEDs.
00403 */
00404 volatile bool triggerLEDs = 0; // Trigger Set to 0 for LED
00405 
00406 /**
00407 @brief Trigger is used for enable/disable Sound option.
00408 @brief If trigger equals 0 = disable Piezo.
00409 @brief If trigger equals 1 = enable Piezo.
00410 */
00411 volatile bool triggerSound = 0; // Trigger Set to 0 for Sound
00412 
00413 /**
00414 @brief Trigger is used for enable/disable Colour option.
00415 @brief If trigger equals 0 = Black Text/Pixels on White Background.
00416 @brief If trigger equals 1 = White Text/Pixels on Black Background.
00417 */
00418 volatile bool triggerColour = 0; // Trigger Set to 0 for Colour
00419 
00420 #endif