Distance Sensor Embedded Systems Project SID: 200864479 James Erringham-Bruce
Dependencies: N5110 SRF02-JEB mbed
UserSettings/UserSettings.h@1:f82359c58eda, 2016-05-04 (annotated)
- Committer:
- ll13j7b
- Date:
- Wed May 04 21:20:21 2016 +0000
- Revision:
- 1:f82359c58eda
- Child:
- 2:01f697b856de
working version before adding classes
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ll13j7b | 1:f82359c58eda | 1 | /* |
ll13j7b | 1:f82359c58eda | 2 | @file Settings.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 | 1:f82359c58eda | 21 | @fn printScreen |
ll13j7b | 1:f82359c58eda | 22 | @brief prints the settings screen |
ll13j7b | 1:f82359c58eda | 23 | @brief controls the scroller icon at the far right of the screen |
ll13j7b | 1:f82359c58eda | 24 | */ |
ll13j7b | 1:f82359c58eda | 25 | void printScreen(); |
ll13j7b | 1:f82359c58eda | 26 | /** |
ll13j7b | 1:f82359c58eda | 27 | @fn scrollUp_down |
ll13j7b | 1:f82359c58eda | 28 | @brief control the scrolling of the screen |
ll13j7b | 1:f82359c58eda | 29 | @brief both directions ( up, down ) |
ll13j7b | 1:f82359c58eda | 30 | */ |
ll13j7b | 1:f82359c58eda | 31 | void scrollUp_Down(int bank); |
ll13j7b | 1:f82359c58eda | 32 | /** |
ll13j7b | 1:f82359c58eda | 33 | @fn printText |
ll13j7b | 1:f82359c58eda | 34 | @brief controls the printing of the text |
ll13j7b | 1:f82359c58eda | 35 | */ |
ll13j7b | 1:f82359c58eda | 36 | |
ll13j7b | 1:f82359c58eda | 37 | /** |
ll13j7b | 1:f82359c58eda | 38 | @fn cursorControl |
ll13j7b | 1:f82359c58eda | 39 | @brief controlls the cursors movement and its selection |
ll13j7b | 1:f82359c58eda | 40 | */ |
ll13j7b | 1:f82359c58eda | 41 | int setBank(); |
ll13j7b | 1:f82359c58eda | 42 | |
ll13j7b | 1:f82359c58eda | 43 | void SendBufferToLCD(); |
ll13j7b | 1:f82359c58eda | 44 | |
ll13j7b | 1:f82359c58eda | 45 | // defining the private functions |
ll13j7b | 1:f82359c58eda | 46 | private: |
ll13j7b | 1:f82359c58eda | 47 | /** |
ll13j7b | 1:f82359c58eda | 48 | @var bankValue |
ll13j7b | 1:f82359c58eda | 49 | @brief the bank that the cursor currently lies |
ll13j7b | 1:f82359c58eda | 50 | */ |
ll13j7b | 1:f82359c58eda | 51 | int bankValue; |
ll13j7b | 1:f82359c58eda | 52 | }; |
ll13j7b | 1:f82359c58eda | 53 | #endif |
ll13j7b | 1:f82359c58eda | 54 | |
ll13j7b | 1:f82359c58eda | 55 | struct settingsStrings { |
ll13j7b | 1:f82359c58eda | 56 | char LED_State[5]; // 3 chars to hold either "on" or "off" |
ll13j7b | 1:f82359c58eda | 57 | char buzzer_State[5]; // 3 chars to hold either "on" or "off" |
ll13j7b | 1:f82359c58eda | 58 | }; |
ll13j7b | 1:f82359c58eda | 59 | |
ll13j7b | 1:f82359c58eda | 60 | settingsStrings setAndSaveSettings(); |
ll13j7b | 1:f82359c58eda | 61 | |
ll13j7b | 1:f82359c58eda | 62 | char buffer2[14]; |
ll13j7b | 1:f82359c58eda | 63 | |
ll13j7b | 1:f82359c58eda | 64 | char OFF[] = "OFF"; // copy this string when off... |
ll13j7b | 1:f82359c58eda | 65 | char ON[] = "ON"; // copy this string when on... |
ll13j7b | 1:f82359c58eda | 66 | |
ll13j7b | 1:f82359c58eda | 67 | volatile bool LED_Toggle = 0; |
ll13j7b | 1:f82359c58eda | 68 | volatile bool buzzer_Toggle = 0; |
ll13j7b | 1:f82359c58eda | 69 | volatile bool brightness_Toggle = 0; |
ll13j7b | 1:f82359c58eda | 70 | |
ll13j7b | 1:f82359c58eda | 71 | void printText(settingsStrings state); |
ll13j7b | 1:f82359c58eda | 72 | void goBackToMenu(); |
ll13j7b | 1:f82359c58eda | 73 | void changeBrightness(float br); |
ll13j7b | 1:f82359c58eda | 74 | |
ll13j7b | 1:f82359c58eda | 75 | volatile int savedBrightness = 10; // intially set to 50% |
ll13j7b | 1:f82359c58eda | 76 | |
ll13j7b | 1:f82359c58eda | 77 | settingsStrings setString; |
ll13j7b | 1:f82359c58eda | 78 | |
ll13j7b | 1:f82359c58eda | 79 | // SAVE THE STRINGS TO THE SETTINGS SCREEN AND REWRITE THE CHANGED DATA TO THE SYSTEM... |
ll13j7b | 1:f82359c58eda | 80 | settingsStrings setAndSaveSettings() |
ll13j7b | 1:f82359c58eda | 81 | { |
ll13j7b | 1:f82359c58eda | 82 | if (selectorPot < 0.15f) { // for the LED toggle states |
ll13j7b | 1:f82359c58eda | 83 | if (settings_flag) { |
ll13j7b | 1:f82359c58eda | 84 | LED_Toggle = !LED_Toggle; |
ll13j7b | 1:f82359c58eda | 85 | if (LED_Toggle) { |
ll13j7b | 1:f82359c58eda | 86 | strcpy(setString.LED_State,ON); |
ll13j7b | 1:f82359c58eda | 87 | LED = 1; |
ll13j7b | 1:f82359c58eda | 88 | } else { |
ll13j7b | 1:f82359c58eda | 89 | strcpy(setString.LED_State,OFF); |
ll13j7b | 1:f82359c58eda | 90 | LED = 0; |
ll13j7b | 1:f82359c58eda | 91 | } |
ll13j7b | 1:f82359c58eda | 92 | settings_flag = 0; // put the flag back to zero ready for another use |
ll13j7b | 1:f82359c58eda | 93 | } |
ll13j7b | 1:f82359c58eda | 94 | } |
ll13j7b | 1:f82359c58eda | 95 | if (selectorPot >= 0.3f && selectorPot < 0.45f) { // for the Buzzer toggle states |
ll13j7b | 1:f82359c58eda | 96 | if (settings_flag) { |
ll13j7b | 1:f82359c58eda | 97 | buzzer_Toggle = !buzzer_Toggle; |
ll13j7b | 1:f82359c58eda | 98 | if (buzzer_Toggle) { |
ll13j7b | 1:f82359c58eda | 99 | strcpy(setString.buzzer_State,ON); |
ll13j7b | 1:f82359c58eda | 100 | } else { |
ll13j7b | 1:f82359c58eda | 101 | strcpy(setString.buzzer_State,OFF); |
ll13j7b | 1:f82359c58eda | 102 | } |
ll13j7b | 1:f82359c58eda | 103 | settings_flag = 0; // put the flag back to zero ready for another use |
ll13j7b | 1:f82359c58eda | 104 | } |
ll13j7b | 1:f82359c58eda | 105 | } |
ll13j7b | 1:f82359c58eda | 106 | if (selectorPot >= 0.7f ) { // hold while brightness is changed |
ll13j7b | 1:f82359c58eda | 107 | if (settings_flag) { |
ll13j7b | 1:f82359c58eda | 108 | do { |
ll13j7b | 1:f82359c58eda | 109 | int brightness = selectorPot*100; |
ll13j7b | 1:f82359c58eda | 110 | changeBrightness(selectorPot); |
ll13j7b | 1:f82359c58eda | 111 | int length = sprintf(buffer,"%d %% ",brightness); // defining max length of chars 84 ÷ 6 = 14 |
ll13j7b | 1:f82359c58eda | 112 | if ( length <= 14 ) { // if string will fit on display |
ll13j7b | 1:f82359c58eda | 113 | lcd.printString(buffer, 35, 5); |
ll13j7b | 1:f82359c58eda | 114 | } |
ll13j7b | 1:f82359c58eda | 115 | if (backButton) { |
ll13j7b | 1:f82359c58eda | 116 | settings_flag = 0; |
ll13j7b | 1:f82359c58eda | 117 | } |
ll13j7b | 1:f82359c58eda | 118 | lcd.printString("Adjust Your",9,2); |
ll13j7b | 1:f82359c58eda | 119 | lcd.printString("Brightness",13,3); |
ll13j7b | 1:f82359c58eda | 120 | } while (settings_flag); |
ll13j7b | 1:f82359c58eda | 121 | } |
ll13j7b | 1:f82359c58eda | 122 | } |
ll13j7b | 1:f82359c58eda | 123 | return setString; |
ll13j7b | 1:f82359c58eda | 124 | } |
ll13j7b | 1:f82359c58eda | 125 | |
ll13j7b | 1:f82359c58eda | 126 | |
ll13j7b | 1:f82359c58eda | 127 | void UserSettings::printScreen() |
ll13j7b | 1:f82359c58eda | 128 | { |
ll13j7b | 1:f82359c58eda | 129 | unsigned int y_scroller = 0; |
ll13j7b | 1:f82359c58eda | 130 | interruptSelector.fall(&settings_isr); |
ll13j7b | 1:f82359c58eda | 131 | |
ll13j7b | 1:f82359c58eda | 132 | settingsStrings newStrings; // call the struct |
ll13j7b | 1:f82359c58eda | 133 | settings_flag = 0; |
ll13j7b | 1:f82359c58eda | 134 | strcpy(setString.LED_State,OFF); // set initial state as off |
ll13j7b | 1:f82359c58eda | 135 | strcpy(setString.buzzer_State,OFF); // set initial state as off |
ll13j7b | 1:f82359c58eda | 136 | while (1) { |
ll13j7b | 1:f82359c58eda | 137 | //settings_flag = 0; // put the flag back to zero ready for another use |
ll13j7b | 1:f82359c58eda | 138 | if (settings_timer_flag) { |
ll13j7b | 1:f82359c58eda | 139 | settings_timer_flag = 0; |
ll13j7b | 1:f82359c58eda | 140 | |
ll13j7b | 1:f82359c58eda | 141 | lcd.clear(); |
ll13j7b | 1:f82359c58eda | 142 | unsigned int bank = setBank(); |
ll13j7b | 1:f82359c58eda | 143 | // normalising the scroller icons pixel values |
ll13j7b | 1:f82359c58eda | 144 | if ( selectorPot > 0.45f ) { |
ll13j7b | 1:f82359c58eda | 145 | y_scroller = ((selectorPot*25)*2) - 10; // 40 is the amount of free pixels the scrolling icon can move |
ll13j7b | 1:f82359c58eda | 146 | } |
ll13j7b | 1:f82359c58eda | 147 | // doubled the normalised value to compensate for the loss of pot range due to brightness.. |
ll13j7b | 1:f82359c58eda | 148 | // ..taking up most of the value |
ll13j7b | 1:f82359c58eda | 149 | if (y_scroller > 48) {// ensuring the scroller does not exceed out of bounds |
ll13j7b | 1:f82359c58eda | 150 | y_scroller = 48; |
ll13j7b | 1:f82359c58eda | 151 | } |
ll13j7b | 1:f82359c58eda | 152 | printText(newStrings); |
ll13j7b | 1:f82359c58eda | 153 | lcd.drawLine(82,y_scroller,82,y_scroller+8,1);// scrolling icon |
ll13j7b | 1:f82359c58eda | 154 | lcd.drawLine(83,y_scroller,83,y_scroller+8,1);// scrolling icon |
ll13j7b | 1:f82359c58eda | 155 | |
ll13j7b | 1:f82359c58eda | 156 | lcd.refresh(); |
ll13j7b | 1:f82359c58eda | 157 | } |
ll13j7b | 1:f82359c58eda | 158 | sleep(); |
ll13j7b | 1:f82359c58eda | 159 | } |
ll13j7b | 1:f82359c58eda | 160 | } |
ll13j7b | 1:f82359c58eda | 161 | |
ll13j7b | 1:f82359c58eda | 162 | int UserSettings::setBank() |
ll13j7b | 1:f82359c58eda | 163 | { |
ll13j7b | 1:f82359c58eda | 164 | int currentBank = 3; |
ll13j7b | 1:f82359c58eda | 165 | |
ll13j7b | 1:f82359c58eda | 166 | if ( selectorPot < 0.15f ) { |
ll13j7b | 1:f82359c58eda | 167 | currentBank = 3; |
ll13j7b | 1:f82359c58eda | 168 | } |
ll13j7b | 1:f82359c58eda | 169 | if ( selectorPot >= 0.15f && selectorPot < 0.3f ) { |
ll13j7b | 1:f82359c58eda | 170 | currentBank = 4; |
ll13j7b | 1:f82359c58eda | 171 | } |
ll13j7b | 1:f82359c58eda | 172 | if ( selectorPot >= 0.3f && selectorPot < 0.45f ) { |
ll13j7b | 1:f82359c58eda | 173 | currentBank = 5; |
ll13j7b | 1:f82359c58eda | 174 | } |
ll13j7b | 1:f82359c58eda | 175 | if ( selectorPot >= 0.45f && selectorPot < 0.7f ) { |
ll13j7b | 1:f82359c58eda | 176 | currentBank = 6; // exceeds screens length, scrolling algorithm must be implemented |
ll13j7b | 1:f82359c58eda | 177 | } |
ll13j7b | 1:f82359c58eda | 178 | if ( selectorPot >= 0.7f ) { |
ll13j7b | 1:f82359c58eda | 179 | currentBank = 7; // exceeds screens length, scrolling algorithm must be implemented |
ll13j7b | 1:f82359c58eda | 180 | } |
ll13j7b | 1:f82359c58eda | 181 | |
ll13j7b | 1:f82359c58eda | 182 | int cursorBank = currentBank; // creating a stopping point for the cursor |
ll13j7b | 1:f82359c58eda | 183 | if ( currentBank > 5 ) { |
ll13j7b | 1:f82359c58eda | 184 | cursorBank = 5; |
ll13j7b | 1:f82359c58eda | 185 | } |
ll13j7b | 1:f82359c58eda | 186 | lcd.printChar('<',70, cursorBank); |
ll13j7b | 1:f82359c58eda | 187 | return currentBank; |
ll13j7b | 1:f82359c58eda | 188 | } |
ll13j7b | 1:f82359c58eda | 189 | |
ll13j7b | 1:f82359c58eda | 190 | // FUNCTION TO OVERRIDE, WHEN THE BANK IS > 5 PUSH TEXT BACK TO A BANK ON SCREEN |
ll13j7b | 1:f82359c58eda | 191 | void printText(settingsStrings state) |
ll13j7b | 1:f82359c58eda | 192 | { |
ll13j7b | 1:f82359c58eda | 193 | |
ll13j7b | 1:f82359c58eda | 194 | state = setAndSaveSettings(); // assign new values |
ll13j7b | 1:f82359c58eda | 195 | |
ll13j7b | 1:f82359c58eda | 196 | // Print text as an array so that it can be manipulated to act as if the |
ll13j7b | 1:f82359c58eda | 197 | // text is scrolliing bank by bank |
ll13j7b | 1:f82359c58eda | 198 | const char screenText[8][14] = { |
ll13j7b | 1:f82359c58eda | 199 | {" SETTINGS"}, |
ll13j7b | 1:f82359c58eda | 200 | { }, // empty slots |
ll13j7b | 1:f82359c58eda | 201 | {"LED:" }, |
ll13j7b | 1:f82359c58eda | 202 | { }, // empty slots |
ll13j7b | 1:f82359c58eda | 203 | {"BUZZER:" }, |
ll13j7b | 1:f82359c58eda | 204 | { }, // empty slots |
ll13j7b | 1:f82359c58eda | 205 | {"BRIGHTNESS:"}, |
ll13j7b | 1:f82359c58eda | 206 | { }, // empty slots |
ll13j7b | 1:f82359c58eda | 207 | }; |
ll13j7b | 1:f82359c58eda | 208 | int length, length2; |
ll13j7b | 1:f82359c58eda | 209 | unsigned int textRow = 0; |
ll13j7b | 1:f82359c58eda | 210 | |
ll13j7b | 1:f82359c58eda | 211 | if ( selectorPot < 0.45f ) { |
ll13j7b | 1:f82359c58eda | 212 | textRow = 5; |
ll13j7b | 1:f82359c58eda | 213 | length = sprintf(buffer,"%s",state.LED_State); // defining max length of chars 84 ÷ 6 = 14 |
ll13j7b | 1:f82359c58eda | 214 | if ( length <= 14 ) { // if string will fit on display |
ll13j7b | 1:f82359c58eda | 215 | lcd.printString(buffer, 20, 3); |
ll13j7b | 1:f82359c58eda | 216 | } |
ll13j7b | 1:f82359c58eda | 217 | length2 = sprintf(buffer2,"%s",state.buzzer_State); // defining max length of chars 84 ÷ 6 = 14 |
ll13j7b | 1:f82359c58eda | 218 | if ( length2 <= 14 ) { // if string will fit on display |
ll13j7b | 1:f82359c58eda | 219 | lcd.printString(buffer2, 20, 5); |
ll13j7b | 1:f82359c58eda | 220 | } |
ll13j7b | 1:f82359c58eda | 221 | |
ll13j7b | 1:f82359c58eda | 222 | } else if ( selectorPot >= 0.45f && selectorPot < 0.7f ) { |
ll13j7b | 1:f82359c58eda | 223 | textRow = 6; |
ll13j7b | 1:f82359c58eda | 224 | length = sprintf(buffer,"%s",state.LED_State); // defining max length of chars 84 ÷ 6 = 14 |
ll13j7b | 1:f82359c58eda | 225 | if ( length <= 14 ) { // if string will fit on display |
ll13j7b | 1:f82359c58eda | 226 | lcd.printString(buffer, 20, 2); |
ll13j7b | 1:f82359c58eda | 227 | } |
ll13j7b | 1:f82359c58eda | 228 | length2 = sprintf(buffer2,"%s",state.buzzer_State); // defining max length of chars 84 ÷ 6 = 14 |
ll13j7b | 1:f82359c58eda | 229 | if ( length2 <= 14 ) { // if string will fit on display |
ll13j7b | 1:f82359c58eda | 230 | lcd.printString(buffer2, 20, 4); |
ll13j7b | 1:f82359c58eda | 231 | } |
ll13j7b | 1:f82359c58eda | 232 | |
ll13j7b | 1:f82359c58eda | 233 | } else if ( selectorPot >= 0.7f) { |
ll13j7b | 1:f82359c58eda | 234 | textRow = 7; |
ll13j7b | 1:f82359c58eda | 235 | length = sprintf(buffer,"%s",state.LED_State); // defining max length of chars 84 ÷ 6 = 14 |
ll13j7b | 1:f82359c58eda | 236 | if ( length <= 14 ) { // if string will fit on display |
ll13j7b | 1:f82359c58eda | 237 | lcd.printString(buffer, 20, 1); |
ll13j7b | 1:f82359c58eda | 238 | } |
ll13j7b | 1:f82359c58eda | 239 | length2 = sprintf(buffer2,"%s",state.buzzer_State); // defining max length of chars 84 ÷ 6 = 14 |
ll13j7b | 1:f82359c58eda | 240 | if ( length2 <= 14 ) { // if string will fit on display |
ll13j7b | 1:f82359c58eda | 241 | lcd.printString(buffer2, 20, 3); |
ll13j7b | 1:f82359c58eda | 242 | } |
ll13j7b | 1:f82359c58eda | 243 | } |
ll13j7b | 1:f82359c58eda | 244 | lcd.printString(screenText[textRow - 5],2,0); |
ll13j7b | 1:f82359c58eda | 245 | lcd.printString(screenText[textRow - 4],2,1); |
ll13j7b | 1:f82359c58eda | 246 | lcd.printString(screenText[textRow - 3],2,2); |
ll13j7b | 1:f82359c58eda | 247 | lcd.printString(screenText[textRow - 2],2,3); |
ll13j7b | 1:f82359c58eda | 248 | lcd.printString(screenText[textRow - 1],2,4); |
ll13j7b | 1:f82359c58eda | 249 | lcd.printString(screenText[textRow],2,5); |
ll13j7b | 1:f82359c58eda | 250 | } |
ll13j7b | 1:f82359c58eda | 251 | |
ll13j7b | 1:f82359c58eda | 252 | void settings_isr() |
ll13j7b | 1:f82359c58eda | 253 | { |
ll13j7b | 1:f82359c58eda | 254 | settings_flag = 1; |
ll13j7b | 1:f82359c58eda | 255 | } |
ll13j7b | 1:f82359c58eda | 256 | |
ll13j7b | 1:f82359c58eda | 257 | void goBackToMenu() |
ll13j7b | 1:f82359c58eda | 258 | { |
ll13j7b | 1:f82359c58eda | 259 | if (backButton) { |
ll13j7b | 1:f82359c58eda | 260 | |
ll13j7b | 1:f82359c58eda | 261 | } |
ll13j7b | 1:f82359c58eda | 262 | } |
ll13j7b | 1:f82359c58eda | 263 | |
ll13j7b | 1:f82359c58eda | 264 | void changeBrightness(float br) |
ll13j7b | 1:f82359c58eda | 265 | { |
ll13j7b | 1:f82359c58eda | 266 | //br *= 0.12f; |
ll13j7b | 1:f82359c58eda | 267 | |
ll13j7b | 1:f82359c58eda | 268 | if(br>=1) { |
ll13j7b | 1:f82359c58eda | 269 | br = 1; |
ll13j7b | 1:f82359c58eda | 270 | } else if(br <= 0) { |
ll13j7b | 1:f82359c58eda | 271 | br = 0; |
ll13j7b | 1:f82359c58eda | 272 | } |
ll13j7b | 1:f82359c58eda | 273 | |
ll13j7b | 1:f82359c58eda | 274 | |
ll13j7b | 1:f82359c58eda | 275 | backLight.write(br); |
ll13j7b | 1:f82359c58eda | 276 | } |