Distance Sensor Embedded Systems Project SID: 200864479 James Erringham-Bruce
Dependencies: N5110 SRF02-JEB mbed
Main/MainMenu.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 MainMenu.h |
ll13j7b | 1:f82359c58eda | 3 | @brief Header file containing member functions and variables |
ll13j7b | 1:f82359c58eda | 4 | @author James Erringham-Bruce |
ll13j7b | 1:f82359c58eda | 5 | */ |
ll13j7b | 1:f82359c58eda | 6 | |
ll13j7b | 1:f82359c58eda | 7 | #ifndef MAINMENU_H |
ll13j7b | 1:f82359c58eda | 8 | #define MAINMENU_H |
ll13j7b | 1:f82359c58eda | 9 | |
ll13j7b | 1:f82359c58eda | 10 | #include "mbed.h" // mbed library |
ll13j7b | 1:f82359c58eda | 11 | #include "Radar.h" |
ll13j7b | 1:f82359c58eda | 12 | #include "Graph.h" |
ll13j7b | 1:f82359c58eda | 13 | #include "UserSettings.h" |
ll13j7b | 2:01f697b856de | 14 | #include "DataController.h" |
ll13j7b | 1:f82359c58eda | 15 | |
ll13j7b | 1:f82359c58eda | 16 | // creating the class used in plotting the graph |
ll13j7b | 1:f82359c58eda | 17 | class Menu |
ll13j7b | 1:f82359c58eda | 18 | { |
ll13j7b | 1:f82359c58eda | 19 | // defining the public functions and variables |
ll13j7b | 1:f82359c58eda | 20 | public: |
ll13j7b | 2:01f697b856de | 21 | /** |
ll13j7b | 2:01f697b856de | 22 | function to intilaise the banks where each bank is declared in order to display the different settings on the screen |
ll13j7b | 2:01f697b856de | 23 | to the user to interact with |
ll13j7b | 2:01f697b856de | 24 | */ |
ll13j7b | 1:f82359c58eda | 25 | void mainMenu(); |
ll13j7b | 2:01f697b856de | 26 | /** |
ll13j7b | 2:01f697b856de | 27 | function to state which option on the menu the user has selected in order to print the next screen |
ll13j7b | 2:01f697b856de | 28 | */ |
ll13j7b | 1:f82359c58eda | 29 | void runSelection(int bank); |
ll13j7b | 2:01f697b856de | 30 | /** |
ll13j7b | 2:01f697b856de | 31 | function to state which labels will be displayed on the screen to the user |
ll13j7b | 2:01f697b856de | 32 | */ |
ll13j7b | 1:f82359c58eda | 33 | void printText(); |
ll13j7b | 2:01f697b856de | 34 | /** |
ll13j7b | 2:01f697b856de | 35 | function that uses the potentiometer value in order to achieve the bank value |
ll13j7b | 2:01f697b856de | 36 | */ |
ll13j7b | 1:f82359c58eda | 37 | int getBank(); |
ll13j7b | 1:f82359c58eda | 38 | /** |
ll13j7b | 2:01f697b856de | 39 | function which takes measurements from the sensor and calculates an average measurement |
ll13j7b | 2:01f697b856de | 40 | then prints each of the values in cm and inches on the screen for the user to see as a visual display |
ll13j7b | 1:f82359c58eda | 41 | */ |
ll13j7b | 1:f82359c58eda | 42 | void numerical(); |
ll13j7b | 1:f82359c58eda | 43 | |
ll13j7b | 1:f82359c58eda | 44 | // defining the private functions and variables |
ll13j7b | 1:f82359c58eda | 45 | private: |
ll13j7b | 1:f82359c58eda | 46 | |
ll13j7b | 1:f82359c58eda | 47 | }; |
ll13j7b | 2:01f697b856de | 48 | //*****************************************************************************************************// |
ll13j7b | 1:f82359c58eda | 49 | |
ll13j7b | 2:01f697b856de | 50 | // END OF CLASS // |
ll13j7b | 2:01f697b856de | 51 | |
ll13j7b | 2:01f697b856de | 52 | //*****************************************************************************************************// |
ll13j7b | 1:f82359c58eda | 53 | #endif |
ll13j7b | 1:f82359c58eda | 54 | |
ll13j7b | 2:01f697b856de | 55 | // pass in the data from classes elsewhere |
ll13j7b | 1:f82359c58eda | 56 | Graph graph; |
ll13j7b | 1:f82359c58eda | 57 | Radar radar; |
ll13j7b | 1:f82359c58eda | 58 | UserSettings setting; |
ll13j7b | 1:f82359c58eda | 59 | |
ll13j7b | 2:01f697b856de | 60 | // FUNCTION TO GET THE BANK THAT THE HIGHLIGHT SHOULD BE PRINTED AT |
ll13j7b | 1:f82359c58eda | 61 | int Menu::getBank() |
ll13j7b | 1:f82359c58eda | 62 | { |
ll13j7b | 2:01f697b856de | 63 | /// take the value of the potentiometer and return the corresponding bank |
ll13j7b | 1:f82359c58eda | 64 | int bankReturn; |
ll13j7b | 1:f82359c58eda | 65 | if (selectorPot < 0.25f) { |
ll13j7b | 1:f82359c58eda | 66 | bankReturn = 2; |
ll13j7b | 1:f82359c58eda | 67 | } |
ll13j7b | 1:f82359c58eda | 68 | if (selectorPot >= 0.25f && selectorPot < 0.5f) { |
ll13j7b | 1:f82359c58eda | 69 | bankReturn = 3; |
ll13j7b | 1:f82359c58eda | 70 | } |
ll13j7b | 1:f82359c58eda | 71 | if (selectorPot >= 0.5f && selectorPot < 0.75f) { |
ll13j7b | 1:f82359c58eda | 72 | bankReturn = 4; |
ll13j7b | 1:f82359c58eda | 73 | } |
ll13j7b | 1:f82359c58eda | 74 | if (selectorPot >= 0.75f) { |
ll13j7b | 1:f82359c58eda | 75 | bankReturn = 5; |
ll13j7b | 1:f82359c58eda | 76 | } |
ll13j7b | 2:01f697b856de | 77 | return bankReturn; // return the value obtained |
ll13j7b | 1:f82359c58eda | 78 | } |
ll13j7b | 1:f82359c58eda | 79 | |
ll13j7b | 1:f82359c58eda | 80 | // MAIN MENU FUNCTION WHERE THE USER CAN INTERFACE |
ll13j7b | 1:f82359c58eda | 81 | void Menu::mainMenu() |
ll13j7b | 1:f82359c58eda | 82 | { |
ll13j7b | 1:f82359c58eda | 83 | int bank = 0; // initialise the bank |
ll13j7b | 2:01f697b856de | 84 | isInMenu = 1; // declaring the menu screen is active |
ll13j7b | 1:f82359c58eda | 85 | |
ll13j7b | 2:01f697b856de | 86 | while (isInMenu) { |
ll13j7b | 2:01f697b856de | 87 | if (menu_flag) { // ticker |
ll13j7b | 2:01f697b856de | 88 | menu_flag = 0; |
ll13j7b | 2:01f697b856de | 89 | lcd.clear(); |
ll13j7b | 2:01f697b856de | 90 | /// get the bank value from the 'getBank' function |
ll13j7b | 2:01f697b856de | 91 | bank = getBank(); // get the bank value |
ll13j7b | 2:01f697b856de | 92 | printText(); // print the users options and title |
ll13j7b | 2:01f697b856de | 93 | // looping through one bank of the screen ( bank 0 in this case) |
ll13j7b | 2:01f697b856de | 94 | // similar algorithm to my scrolling method |
ll13j7b | 2:01f697b856de | 95 | /// convert to pixel form by multiplying by 8 |
ll13j7b | 2:01f697b856de | 96 | int tempBank = bank*8; |
ll13j7b | 2:01f697b856de | 97 | /// loop through columns and rows and invert the bank returned |
ll13j7b | 2:01f697b856de | 98 | for ( int columns = 0; columns < 84; columns++ ) { |
ll13j7b | 2:01f697b856de | 99 | // multiply by eight to convert bank value to the rows in that bank |
ll13j7b | 2:01f697b856de | 100 | for ( int currentBank = (bank*8) - 1; currentBank < tempBank+8; currentBank++) { |
ll13j7b | 2:01f697b856de | 101 | if (lcd.getPixel(columns, currentBank)) { // if the pixel is set |
ll13j7b | 2:01f697b856de | 102 | lcd.clearPixel(columns, currentBank); // clear it |
ll13j7b | 2:01f697b856de | 103 | } else { // set the other pixels |
ll13j7b | 2:01f697b856de | 104 | lcd.setPixel(columns, currentBank); // basically inverting the screen |
ll13j7b | 2:01f697b856de | 105 | } |
ll13j7b | 1:f82359c58eda | 106 | } |
ll13j7b | 1:f82359c58eda | 107 | } |
ll13j7b | 2:01f697b856de | 108 | runSelection(bank); // check if a selection has been made |
ll13j7b | 2:01f697b856de | 109 | lcd.refresh(); |
ll13j7b | 1:f82359c58eda | 110 | } |
ll13j7b | 1:f82359c58eda | 111 | } |
ll13j7b | 1:f82359c58eda | 112 | } |
ll13j7b | 1:f82359c58eda | 113 | |
ll13j7b | 2:01f697b856de | 114 | // FUNCTION TO RUN THE SELECTED OPTION IF BUTTON IS PRESSED |
ll13j7b | 1:f82359c58eda | 115 | void Menu::runSelection(int bank) |
ll13j7b | 1:f82359c58eda | 116 | { |
ll13j7b | 2:01f697b856de | 117 | /// if the buttton is pressed while at a certain bank, then run that option |
ll13j7b | 2:01f697b856de | 118 | |
ll13j7b | 2:01f697b856de | 119 | if (selector_flag ) { |
ll13j7b | 2:01f697b856de | 120 | selector_flag = 0; |
ll13j7b | 2:01f697b856de | 121 | |
ll13j7b | 2:01f697b856de | 122 | if (bank == 2) { |
ll13j7b | 2:01f697b856de | 123 | isInMenu = 0; |
ll13j7b | 2:01f697b856de | 124 | setting.printScreen(); // settings screen |
ll13j7b | 2:01f697b856de | 125 | } |
ll13j7b | 2:01f697b856de | 126 | if (bank == 3) { |
ll13j7b | 2:01f697b856de | 127 | isInMenu = 0; |
ll13j7b | 2:01f697b856de | 128 | graph.plotGraph(); // graph screen |
ll13j7b | 2:01f697b856de | 129 | } |
ll13j7b | 2:01f697b856de | 130 | if (bank == 4) { |
ll13j7b | 2:01f697b856de | 131 | isInMenu = 0; |
ll13j7b | 2:01f697b856de | 132 | numerical(); // numerical screen |
ll13j7b | 2:01f697b856de | 133 | } |
ll13j7b | 2:01f697b856de | 134 | if (bank == 5) { |
ll13j7b | 2:01f697b856de | 135 | isInMenu = 0; |
ll13j7b | 2:01f697b856de | 136 | radar.radarMode(); // radar view (basic Y axis, no X axis movement) |
ll13j7b | 2:01f697b856de | 137 | } |
ll13j7b | 2:01f697b856de | 138 | |
ll13j7b | 1:f82359c58eda | 139 | } |
ll13j7b | 1:f82359c58eda | 140 | } |
ll13j7b | 1:f82359c58eda | 141 | |
ll13j7b | 1:f82359c58eda | 142 | // FUNCTION TO PRINT THE DISTANCE ON SCREEN IN NUMERICAL FORM |
ll13j7b | 1:f82359c58eda | 143 | void Menu::numerical() |
ll13j7b | 1:f82359c58eda | 144 | { |
ll13j7b | 2:01f697b856de | 145 | isInNumerical = 1;// sets flag to 1 so it can keep running in a cycle |
ll13j7b | 2:01f697b856de | 146 | |
ll13j7b | 2:01f697b856de | 147 | while (isInNumerical) { |
ll13j7b | 2:01f697b856de | 148 | /// obtain the average value in both cm & inches |
ll13j7b | 2:01f697b856de | 149 | int avgCm = getAverageReadingCm(); //average reading in cm |
ll13j7b | 2:01f697b856de | 150 | int avgInch = getAverageReadingInch(); //average reading in inches |
ll13j7b | 2:01f697b856de | 151 | LED = 0; //LED set to logic 0 |
ll13j7b | 2:01f697b856de | 152 | goBackToMenu();//if interrupt is pressed go back to main menu |
ll13j7b | 2:01f697b856de | 153 | |
ll13j7b | 1:f82359c58eda | 154 | if (g_timer_flag) { // firing the first timer flag |
ll13j7b | 2:01f697b856de | 155 | g_timer_flag = 0;//resets the flag and runs again |
ll13j7b | 1:f82359c58eda | 156 | lcd.clear(); // clear the screen ( preparing for the reading to be dispalyed |
ll13j7b | 2:01f697b856de | 157 | /// print both of the values on the screen as timer fires |
ll13j7b | 2:01f697b856de | 158 | if (avgCm <=250 && avgCm >=15) { // if in bounds.. |
ll13j7b | 2:01f697b856de | 159 | lcd.printString("DISTANCE", 18, 0); //display distance |
ll13j7b | 2:01f697b856de | 160 | // 6 pixels across per char, 84 pixels wide along the screen |
ll13j7b | 2:01f697b856de | 161 | int length = sprintf(buffer,"d = %d cm",avgCm); // defining max length of chars 84 ÷ 6 = 14 |
ll13j7b | 2:01f697b856de | 162 | if (length <= 14) { /// if string will fit on display |
ll13j7b | 2:01f697b856de | 163 | lcd.printString(buffer, 5, 3);//print string to buffer |
ll13j7b | 2:01f697b856de | 164 | } |
ll13j7b | 2:01f697b856de | 165 | // 6 pixels across per char, 84 pixels wide along the screen |
ll13j7b | 2:01f697b856de | 166 | length = sprintf(buffer,"d = %d Inch",avgInch); // defining max length of chars 84 ÷ 6 = 14 |
ll13j7b | 2:01f697b856de | 167 | if (length <= 14) { /// if string will fit on display |
ll13j7b | 2:01f697b856de | 168 | lcd.printString(buffer, 5, 4); //print string to buffer |
ll13j7b | 2:01f697b856de | 169 | } |
ll13j7b | 2:01f697b856de | 170 | } else { |
ll13j7b | 2:01f697b856de | 171 | /// check for reading errors to determine if LED or Buzzer turns on |
ll13j7b | 2:01f697b856de | 172 | readingErrors(); //check for reading error |
ll13j7b | 2:01f697b856de | 173 | } |
ll13j7b | 1:f82359c58eda | 174 | lcd.refresh(); // refresh the screen displaying the average reading |
ll13j7b | 1:f82359c58eda | 175 | } |
ll13j7b | 1:f82359c58eda | 176 | sleep(); // sleep before next interrupt |
ll13j7b | 1:f82359c58eda | 177 | } |
ll13j7b | 1:f82359c58eda | 178 | } |
ll13j7b | 1:f82359c58eda | 179 | |
ll13j7b | 2:01f697b856de | 180 | // FUNCTION TO PRINT TEXT TO THE MAIN MENU |
ll13j7b | 1:f82359c58eda | 181 | void Menu::printText() |
ll13j7b | 1:f82359c58eda | 182 | { |
ll13j7b | 2:01f697b856de | 183 | /// print the text of the menu |
ll13j7b | 1:f82359c58eda | 184 | lcd.printString("MENU",30,0); // print the title |
ll13j7b | 1:f82359c58eda | 185 | lcd.drawLine(28,8,54,8,1); // underlining the title |
ll13j7b | 1:f82359c58eda | 186 | lcd.printString("SETTINGS",17,2); // options |
ll13j7b | 2:01f697b856de | 187 | lcd.printString("GRAPH PLOT",12,3); //... |
ll13j7b | 2:01f697b856de | 188 | lcd.printString("NUMERICAL",16,4); //... |
ll13j7b | 2:01f697b856de | 189 | lcd.printString("RADAR MODE",12,5); //... |
ll13j7b | 2:01f697b856de | 190 | } |
ll13j7b | 2:01f697b856de | 191 | |
ll13j7b | 2:01f697b856de | 192 | // |
ll13j7b | 2:01f697b856de | 193 | void menu_isr() |
ll13j7b | 2:01f697b856de | 194 | { |
ll13j7b | 2:01f697b856de | 195 | menu_flag = 1; |
ll13j7b | 2:01f697b856de | 196 | |
ll13j7b | 2:01f697b856de | 197 | } |
ll13j7b | 2:01f697b856de | 198 | |
ll13j7b | 2:01f697b856de | 199 | //*****************************************************************************************************// |
ll13j7b | 2:01f697b856de | 200 | |
ll13j7b | 2:01f697b856de | 201 | // END OF FUNCTION IMPLEMENTATIONS // |
ll13j7b | 2:01f697b856de | 202 | |
ll13j7b | 2:01f697b856de | 203 | //*****************************************************************************************************// |
ll13j7b | 2:01f697b856de | 204 | |
ll13j7b | 2:01f697b856de | 205 | |
ll13j7b | 2:01f697b856de | 206 | |
ll13j7b | 2:01f697b856de | 207 | |
ll13j7b | 2:01f697b856de | 208 | |
ll13j7b | 2:01f697b856de | 209 |