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 2:01f697b856de 1 /**
ll13j7b 2:01f697b856de 2 @file UserSettings.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 USERSETTINGS_H
ll13j7b 1:f82359c58eda 8 #define USERSETTINGS_H
ll13j7b 1:f82359c58eda 9
ll13j7b 1:f82359c58eda 10 #include "mbed.h" // mbed library
ll13j7b 1:f82359c58eda 11 #include "DataController.h" // handling the common data
ll13j7b 1:f82359c58eda 12 #include <string> // string library
ll13j7b 1:f82359c58eda 13
ll13j7b 1:f82359c58eda 14 // creating the class used in plotting the graph
ll13j7b 1:f82359c58eda 15 class UserSettings
ll13j7b 1:f82359c58eda 16 {
ll13j7b 1:f82359c58eda 17 // defining the public functions
ll13j7b 1:f82359c58eda 18 public:
ll13j7b 1:f82359c58eda 19
ll13j7b 1:f82359c58eda 20 /**
ll13j7b 2:01f697b856de 21 function is used to print the settings on the screen in order for the user to interact with and chose the option
ll13j7b 2:01f697b856de 22 within the function, it controls the scrolling for the screen
ll13j7b 1:f82359c58eda 23 */
ll13j7b 1:f82359c58eda 24 void printScreen();
ll13j7b 1:f82359c58eda 25 /**
ll13j7b 2:01f697b856de 26 function allows the user to scroll up and down on the screen to view all the options
ll13j7b 2:01f697b856de 27 @param bank
ll13j7b 1:f82359c58eda 28 */
ll13j7b 1:f82359c58eda 29 void scrollUp_Down(int bank);
ll13j7b 1:f82359c58eda 30 /**
ll13j7b 2:01f697b856de 31 function to return the value of the bank to invert
ll13j7b 2:01f697b856de 32 @returns bank - to invert colour
ll13j7b 1:f82359c58eda 33 */
ll13j7b 2:01f697b856de 34 int setBank();
ll13j7b 1:f82359c58eda 35
ll13j7b 1:f82359c58eda 36
ll13j7b 1:f82359c58eda 37 // defining the private functions
ll13j7b 1:f82359c58eda 38 private:
ll13j7b 2:01f697b856de 39
ll13j7b 1:f82359c58eda 40 };
ll13j7b 2:01f697b856de 41
ll13j7b 2:01f697b856de 42 //*****************************************************************************************************//
ll13j7b 2:01f697b856de 43
ll13j7b 2:01f697b856de 44 // END OF CLASS //
ll13j7b 2:01f697b856de 45
ll13j7b 2:01f697b856de 46 //*****************************************************************************************************//
ll13j7b 2:01f697b856de 47
ll13j7b 1:f82359c58eda 48 #endif
ll13j7b 1:f82359c58eda 49
ll13j7b 2:01f697b856de 50 /**
ll13j7b 2:01f697b856de 51 @struct settingsStrings
ll13j7b 2:01f697b856de 52 struct that holds the array of chars to state whether the LED & Buzzer are ON or OFF
ll13j7b 2:01f697b856de 53 */
ll13j7b 1:f82359c58eda 54 struct settingsStrings {
ll13j7b 2:01f697b856de 55 char LED_State[5]; /*!< 3 chars to hold either "on" or "off", 5 used to give space for printing text */
ll13j7b 2:01f697b856de 56 char buzzer_State[5]; /*!< 3 chars to hold either "on" or "off" 5 used to give space for printing text */
ll13j7b 1:f82359c58eda 57 };
ll13j7b 1:f82359c58eda 58
ll13j7b 1:f82359c58eda 59 settingsStrings setAndSaveSettings();
ll13j7b 1:f82359c58eda 60
ll13j7b 2:01f697b856de 61 char buffer2[14]; /*!<character length #2 as two strings are printed at once*/
ll13j7b 2:01f697b856de 62
ll13j7b 2:01f697b856de 63 char OFF[] = "OFF"; /*!< copy this string when off... */
ll13j7b 2:01f697b856de 64 char ON[] = "ON"; /*!< copy this string when on... */
ll13j7b 2:01f697b856de 65
ll13j7b 2:01f697b856de 66 volatile bool LED_Toggle = 0; /*!< determines whether LED is logic high(1) or logic low (0)*/
ll13j7b 2:01f697b856de 67 volatile bool buzzer_Toggle = 0; /*!< determines whether buzzer is logic high (1) or logic low (0)*/
ll13j7b 2:01f697b856de 68 volatile bool brightness_Toggle = 0; /*!< determines whether the brightness is being changed */
ll13j7b 1:f82359c58eda 69
ll13j7b 2:01f697b856de 70 /**
ll13j7b 2:01f697b856de 71 function to allow the text that is off the screen to be printed when the user scrolls down the screen
ll13j7b 2:01f697b856de 72 */
ll13j7b 2:01f697b856de 73 void printText(settingsStrings state);
ll13j7b 2:01f697b856de 74
ll13j7b 2:01f697b856de 75 /**
ll13j7b 2:01f697b856de 76 function that allows the main menu to be returned if the interrupt button has been pressed
ll13j7b 2:01f697b856de 77 */
ll13j7b 2:01f697b856de 78 void goBackToMenu();
ll13j7b 1:f82359c58eda 79
ll13j7b 2:01f697b856de 80 /**
ll13j7b 2:01f697b856de 81 function to allow the brightness of the LCD screen to be changed using PWM
ll13j7b 2:01f697b856de 82 @param br
ll13j7b 2:01f697b856de 83 */
ll13j7b 2:01f697b856de 84 void changeBrightness(float br);
ll13j7b 2:01f697b856de 85 /**
ll13j7b 2:01f697b856de 86 @namespace settingsStrings
ll13j7b 2:01f697b856de 87 @brief allows passage of the struct to be used in other functions
ll13j7b 2:01f697b856de 88 */
ll13j7b 2:01f697b856de 89 settingsStrings setString;
ll13j7b 1:f82359c58eda 90
ll13j7b 2:01f697b856de 91 int brightness = 50; /*!< intialises the brightness at start up */
ll13j7b 2:01f697b856de 92
ll13j7b 2:01f697b856de 93 //*****************************************************************************************************//
ll13j7b 1:f82359c58eda 94
ll13j7b 2:01f697b856de 95 // END OF GLOBAL VARIABLES //
ll13j7b 1:f82359c58eda 96
ll13j7b 2:01f697b856de 97 //*****************************************************************************************************//
ll13j7b 1:f82359c58eda 98
ll13j7b 1:f82359c58eda 99 // SAVE THE STRINGS TO THE SETTINGS SCREEN AND REWRITE THE CHANGED DATA TO THE SYSTEM...
ll13j7b 1:f82359c58eda 100 settingsStrings setAndSaveSettings()
ll13j7b 1:f82359c58eda 101 {
ll13j7b 2:01f697b856de 102 /// checks position of the potentiometer
ll13j7b 2:01f697b856de 103 /// if the settings flag has been activated
ll13j7b 2:01f697b856de 104 /// flip the toggle of the
ll13j7b 1:f82359c58eda 105 if (selectorPot < 0.15f) { // for the LED toggle states
ll13j7b 2:01f697b856de 106 if (selector_flag) {
ll13j7b 1:f82359c58eda 107 LED_Toggle = !LED_Toggle;
ll13j7b 1:f82359c58eda 108 if (LED_Toggle) {
ll13j7b 1:f82359c58eda 109 strcpy(setString.LED_State,ON);
ll13j7b 1:f82359c58eda 110 LED = 1;
ll13j7b 1:f82359c58eda 111 } else {
ll13j7b 1:f82359c58eda 112 strcpy(setString.LED_State,OFF);
ll13j7b 1:f82359c58eda 113 LED = 0;
ll13j7b 1:f82359c58eda 114 }
ll13j7b 2:01f697b856de 115 selector_flag = 0; // put the flag back to zero ready for another use
ll13j7b 1:f82359c58eda 116 }
ll13j7b 1:f82359c58eda 117 }
ll13j7b 1:f82359c58eda 118 if (selectorPot >= 0.3f && selectorPot < 0.45f) { // for the Buzzer toggle states
ll13j7b 2:01f697b856de 119 if (selector_flag) {
ll13j7b 1:f82359c58eda 120 buzzer_Toggle = !buzzer_Toggle;
ll13j7b 1:f82359c58eda 121 if (buzzer_Toggle) {
ll13j7b 1:f82359c58eda 122 strcpy(setString.buzzer_State,ON);
ll13j7b 1:f82359c58eda 123 } else {
ll13j7b 1:f82359c58eda 124 strcpy(setString.buzzer_State,OFF);
ll13j7b 1:f82359c58eda 125 }
ll13j7b 2:01f697b856de 126 selector_flag = 0; // put the flag back to zero ready for another use
ll13j7b 1:f82359c58eda 127 }
ll13j7b 1:f82359c58eda 128 }
ll13j7b 1:f82359c58eda 129 if (selectorPot >= 0.7f ) { // hold while brightness is changed
ll13j7b 2:01f697b856de 130 if (selector_flag) {
ll13j7b 1:f82359c58eda 131 do {
ll13j7b 2:01f697b856de 132 brightness = selectorPot*100; // setting brightness as a percentage
ll13j7b 2:01f697b856de 133 changeBrightness(selectorPot*2);
ll13j7b 1:f82359c58eda 134 int length = sprintf(buffer,"%d %% ",brightness); // defining max length of chars 84 ÷ 6 = 14
ll13j7b 1:f82359c58eda 135 if ( length <= 14 ) { // if string will fit on display
ll13j7b 1:f82359c58eda 136 lcd.printString(buffer, 35, 5);
ll13j7b 1:f82359c58eda 137 }
ll13j7b 2:01f697b856de 138 if (back_flag) {
ll13j7b 2:01f697b856de 139 selector_flag = 0;
ll13j7b 2:01f697b856de 140 isInMenu = 0;
ll13j7b 2:01f697b856de 141 isInSettings = 1;
ll13j7b 2:01f697b856de 142 } // if back, return to settings not the main menu!!
ll13j7b 1:f82359c58eda 143 lcd.printString("Adjust Your",9,2);
ll13j7b 1:f82359c58eda 144 lcd.printString("Brightness",13,3);
ll13j7b 2:01f697b856de 145 } while (selector_flag);
ll13j7b 1:f82359c58eda 146 }
ll13j7b 2:01f697b856de 147 sprintf(buffer,"%d %% ",brightness); //ensure brightness is still printed after loop
ll13j7b 2:01f697b856de 148 lcd.printString(buffer, 35, 5);
ll13j7b 1:f82359c58eda 149 }
ll13j7b 1:f82359c58eda 150 return setString;
ll13j7b 1:f82359c58eda 151 }
ll13j7b 1:f82359c58eda 152
ll13j7b 2:01f697b856de 153 // FUNCTION TO PRINT THE SETTINGS SCREEN ACCORDING TO THE CHANGED PARAMETERS
ll13j7b 1:f82359c58eda 154 void UserSettings::printScreen()
ll13j7b 1:f82359c58eda 155 {
ll13j7b 2:01f697b856de 156 /// initially set the buzzer & LED to off
ll13j7b 2:01f697b856de 157 isInSettings = 1;
ll13j7b 1:f82359c58eda 158 unsigned int y_scroller = 0;
ll13j7b 1:f82359c58eda 159
ll13j7b 1:f82359c58eda 160 settingsStrings newStrings; // call the struct
ll13j7b 2:01f697b856de 161 selector_flag = 0;
ll13j7b 2:01f697b856de 162 // COPY THE CHARS OFF TO THE EMPTY STATES
ll13j7b 1:f82359c58eda 163 strcpy(setString.LED_State,OFF); // set initial state as off
ll13j7b 1:f82359c58eda 164 strcpy(setString.buzzer_State,OFF); // set initial state as off
ll13j7b 2:01f697b856de 165
ll13j7b 2:01f697b856de 166 while (isInSettings) {
ll13j7b 2:01f697b856de 167
ll13j7b 2:01f697b856de 168 goBackToMenu();
ll13j7b 2:01f697b856de 169
ll13j7b 1:f82359c58eda 170 if (settings_timer_flag) {
ll13j7b 1:f82359c58eda 171 settings_timer_flag = 0;
ll13j7b 1:f82359c58eda 172
ll13j7b 1:f82359c58eda 173 lcd.clear();
ll13j7b 1:f82359c58eda 174 unsigned int bank = setBank();
ll13j7b 1:f82359c58eda 175 // normalising the scroller icons pixel values
ll13j7b 2:01f697b856de 176 /// set the scrolling ican and move down when the screen scrolls
ll13j7b 1:f82359c58eda 177 if ( selectorPot > 0.45f ) {
ll13j7b 1:f82359c58eda 178 y_scroller = ((selectorPot*25)*2) - 10; // 40 is the amount of free pixels the scrolling icon can move
ll13j7b 1:f82359c58eda 179 }
ll13j7b 1:f82359c58eda 180 // doubled the normalised value to compensate for the loss of pot range due to brightness..
ll13j7b 1:f82359c58eda 181 // ..taking up most of the value
ll13j7b 1:f82359c58eda 182 if (y_scroller > 48) {// ensuring the scroller does not exceed out of bounds
ll13j7b 1:f82359c58eda 183 y_scroller = 48;
ll13j7b 1:f82359c58eda 184 }
ll13j7b 2:01f697b856de 185 // print new strings obtained
ll13j7b 1:f82359c58eda 186 printText(newStrings);
ll13j7b 1:f82359c58eda 187 lcd.drawLine(82,y_scroller,82,y_scroller+8,1);// scrolling icon
ll13j7b 1:f82359c58eda 188 lcd.drawLine(83,y_scroller,83,y_scroller+8,1);// scrolling icon
ll13j7b 1:f82359c58eda 189
ll13j7b 1:f82359c58eda 190 lcd.refresh();
ll13j7b 1:f82359c58eda 191 }
ll13j7b 2:01f697b856de 192 sleep(); // sleep after ticker has run
ll13j7b 1:f82359c58eda 193 }
ll13j7b 1:f82359c58eda 194 }
ll13j7b 1:f82359c58eda 195
ll13j7b 2:01f697b856de 196 // FUNCTION TO RETURN THE BANK VALUE OF THE CURSOR
ll13j7b 1:f82359c58eda 197 int UserSettings::setBank()
ll13j7b 1:f82359c58eda 198 {
ll13j7b 1:f82359c58eda 199 int currentBank = 3;
ll13j7b 1:f82359c58eda 200
ll13j7b 1:f82359c58eda 201 if ( selectorPot < 0.15f ) {
ll13j7b 1:f82359c58eda 202 currentBank = 3;
ll13j7b 1:f82359c58eda 203 }
ll13j7b 1:f82359c58eda 204 if ( selectorPot >= 0.15f && selectorPot < 0.3f ) {
ll13j7b 1:f82359c58eda 205 currentBank = 4;
ll13j7b 1:f82359c58eda 206 }
ll13j7b 1:f82359c58eda 207 if ( selectorPot >= 0.3f && selectorPot < 0.45f ) {
ll13j7b 1:f82359c58eda 208 currentBank = 5;
ll13j7b 1:f82359c58eda 209 }
ll13j7b 1:f82359c58eda 210 if ( selectorPot >= 0.45f && selectorPot < 0.7f ) {
ll13j7b 1:f82359c58eda 211 currentBank = 6; // exceeds screens length, scrolling algorithm must be implemented
ll13j7b 1:f82359c58eda 212 }
ll13j7b 1:f82359c58eda 213 if ( selectorPot >= 0.7f ) {
ll13j7b 1:f82359c58eda 214 currentBank = 7; // exceeds screens length, scrolling algorithm must be implemented
ll13j7b 1:f82359c58eda 215 }
ll13j7b 1:f82359c58eda 216
ll13j7b 1:f82359c58eda 217 int cursorBank = currentBank; // creating a stopping point for the cursor
ll13j7b 1:f82359c58eda 218 if ( currentBank > 5 ) {
ll13j7b 1:f82359c58eda 219 cursorBank = 5;
ll13j7b 1:f82359c58eda 220 }
ll13j7b 1:f82359c58eda 221 lcd.printChar('<',70, cursorBank);
ll13j7b 1:f82359c58eda 222 return currentBank;
ll13j7b 1:f82359c58eda 223 }
ll13j7b 1:f82359c58eda 224
ll13j7b 1:f82359c58eda 225 // FUNCTION TO OVERRIDE, WHEN THE BANK IS > 5 PUSH TEXT BACK TO A BANK ON SCREEN
ll13j7b 1:f82359c58eda 226 void printText(settingsStrings state)
ll13j7b 1:f82359c58eda 227 {
ll13j7b 2:01f697b856de 228 /// pass in the new values of the buzzer and LED state from the struct
ll13j7b 1:f82359c58eda 229 state = setAndSaveSettings(); // assign new values
ll13j7b 1:f82359c58eda 230
ll13j7b 1:f82359c58eda 231 // Print text as an array so that it can be manipulated to act as if the
ll13j7b 1:f82359c58eda 232 // text is scrolliing bank by bank
ll13j7b 2:01f697b856de 233 /// print the screen as an array that only displays parts of that array
ll13j7b 1:f82359c58eda 234 const char screenText[8][14] = {
ll13j7b 1:f82359c58eda 235 {" SETTINGS"},
ll13j7b 1:f82359c58eda 236 { }, // empty slots
ll13j7b 1:f82359c58eda 237 {"LED:" },
ll13j7b 1:f82359c58eda 238 { }, // empty slots
ll13j7b 1:f82359c58eda 239 {"BUZZER:" },
ll13j7b 1:f82359c58eda 240 { }, // empty slots
ll13j7b 1:f82359c58eda 241 {"BRIGHTNESS:"},
ll13j7b 1:f82359c58eda 242 { }, // empty slots
ll13j7b 1:f82359c58eda 243 };
ll13j7b 1:f82359c58eda 244 int length, length2;
ll13j7b 1:f82359c58eda 245 unsigned int textRow = 0;
ll13j7b 2:01f697b856de 246 /// print the states as a string even when scrolling is implemented
ll13j7b 1:f82359c58eda 247 if ( selectorPot < 0.45f ) {
ll13j7b 1:f82359c58eda 248 textRow = 5;
ll13j7b 1:f82359c58eda 249 length = sprintf(buffer,"%s",state.LED_State); // defining max length of chars 84 ÷ 6 = 14
ll13j7b 1:f82359c58eda 250 if ( length <= 14 ) { // if string will fit on display
ll13j7b 1:f82359c58eda 251 lcd.printString(buffer, 20, 3);
ll13j7b 1:f82359c58eda 252 }
ll13j7b 1:f82359c58eda 253 length2 = sprintf(buffer2,"%s",state.buzzer_State); // defining max length of chars 84 ÷ 6 = 14
ll13j7b 1:f82359c58eda 254 if ( length2 <= 14 ) { // if string will fit on display
ll13j7b 1:f82359c58eda 255 lcd.printString(buffer2, 20, 5);
ll13j7b 1:f82359c58eda 256 }
ll13j7b 1:f82359c58eda 257
ll13j7b 1:f82359c58eda 258 } else if ( selectorPot >= 0.45f && selectorPot < 0.7f ) {
ll13j7b 1:f82359c58eda 259 textRow = 6;
ll13j7b 1:f82359c58eda 260 length = sprintf(buffer,"%s",state.LED_State); // defining max length of chars 84 ÷ 6 = 14
ll13j7b 1:f82359c58eda 261 if ( length <= 14 ) { // if string will fit on display
ll13j7b 1:f82359c58eda 262 lcd.printString(buffer, 20, 2);
ll13j7b 1:f82359c58eda 263 }
ll13j7b 1:f82359c58eda 264 length2 = sprintf(buffer2,"%s",state.buzzer_State); // defining max length of chars 84 ÷ 6 = 14
ll13j7b 1:f82359c58eda 265 if ( length2 <= 14 ) { // if string will fit on display
ll13j7b 1:f82359c58eda 266 lcd.printString(buffer2, 20, 4);
ll13j7b 1:f82359c58eda 267 }
ll13j7b 1:f82359c58eda 268
ll13j7b 1:f82359c58eda 269 } else if ( selectorPot >= 0.7f) {
ll13j7b 1:f82359c58eda 270 textRow = 7;
ll13j7b 1:f82359c58eda 271 length = sprintf(buffer,"%s",state.LED_State); // defining max length of chars 84 ÷ 6 = 14
ll13j7b 1:f82359c58eda 272 if ( length <= 14 ) { // if string will fit on display
ll13j7b 1:f82359c58eda 273 lcd.printString(buffer, 20, 1);
ll13j7b 1:f82359c58eda 274 }
ll13j7b 1:f82359c58eda 275 length2 = sprintf(buffer2,"%s",state.buzzer_State); // defining max length of chars 84 ÷ 6 = 14
ll13j7b 1:f82359c58eda 276 if ( length2 <= 14 ) { // if string will fit on display
ll13j7b 1:f82359c58eda 277 lcd.printString(buffer2, 20, 3);
ll13j7b 1:f82359c58eda 278 }
ll13j7b 1:f82359c58eda 279 }
ll13j7b 1:f82359c58eda 280 lcd.printString(screenText[textRow - 5],2,0);
ll13j7b 1:f82359c58eda 281 lcd.printString(screenText[textRow - 4],2,1);
ll13j7b 1:f82359c58eda 282 lcd.printString(screenText[textRow - 3],2,2);
ll13j7b 1:f82359c58eda 283 lcd.printString(screenText[textRow - 2],2,3);
ll13j7b 1:f82359c58eda 284 lcd.printString(screenText[textRow - 1],2,4);
ll13j7b 1:f82359c58eda 285 lcd.printString(screenText[textRow],2,5);
ll13j7b 1:f82359c58eda 286 }
ll13j7b 1:f82359c58eda 287
ll13j7b 2:01f697b856de 288 // FUNCTION TO CHANGE THE BRIGHTNESS OF THE LCD BACKLIGHT
ll13j7b 1:f82359c58eda 289 void changeBrightness(float br)
ll13j7b 1:f82359c58eda 290 {
ll13j7b 2:01f697b856de 291 /// ensure the brightness value never goes out of range 0-1
ll13j7b 1:f82359c58eda 292 if(br>=1) {
ll13j7b 1:f82359c58eda 293 br = 1;
ll13j7b 1:f82359c58eda 294 } else if(br <= 0) {
ll13j7b 1:f82359c58eda 295 br = 0;
ll13j7b 1:f82359c58eda 296 }
ll13j7b 2:01f697b856de 297 /// write the value to the PWM pin defined as 'backLight'
ll13j7b 2:01f697b856de 298 backLight.write(br);
ll13j7b 2:01f697b856de 299 }
ll13j7b 1:f82359c58eda 300
ll13j7b 1:f82359c58eda 301
ll13j7b 2:01f697b856de 302 //*****************************************************************************************************//
ll13j7b 2:01f697b856de 303
ll13j7b 2:01f697b856de 304 // END OF FUNCTION IMPLEMENTATIONS //
ll13j7b 2:01f697b856de 305
ll13j7b 2:01f697b856de 306 //*****************************************************************************************************//
ll13j7b 2:01f697b856de 307
ll13j7b 2:01f697b856de 308
ll13j7b 2:01f697b856de 309
ll13j7b 2:01f697b856de 310
ll13j7b 2:01f697b856de 311