Year Two Project ELEC 2645: Embedded Systems Project Portable Weather Station

Dependencies:   BMP180 ConfigFile N5110 PowerControl beep mbed

Committer:
OHstin
Date:
Mon May 11 15:25:52 2015 +0000
Revision:
0:da2b8c7a1ec1
Completed Weather Station

Who changed what in which revision?

UserRevisionLine numberNew contents of line
OHstin 0:da2b8c7a1ec1 1 /**
OHstin 0:da2b8c7a1ec1 2 @file BrightnessScreen.h
OHstin 0:da2b8c7a1ec1 3
OHstin 0:da2b8c7a1ec1 4
OHstin 0:da2b8c7a1ec1 5 */
OHstin 0:da2b8c7a1ec1 6 #ifndef BRIGHTNESSSCREEN_H
OHstin 0:da2b8c7a1ec1 7 #define BRIGHTNESSSCREEN_H
OHstin 0:da2b8c7a1ec1 8
OHstin 0:da2b8c7a1ec1 9 #include "SettingController.h"
OHstin 0:da2b8c7a1ec1 10 #include "Inputs.h"
OHstin 0:da2b8c7a1ec1 11
OHstin 0:da2b8c7a1ec1 12
OHstin 0:da2b8c7a1ec1 13 /**
OHstin 0:da2b8c7a1ec1 14
OHstin 0:da2b8c7a1ec1 15 @brief Shows the current brightness setting of the LCD backlight brightness\n
OHstin 0:da2b8c7a1ec1 16 @brief Consists of a Setting Controller\n
OHstin 0:da2b8c7a1ec1 17 @brief brightness can be set to 100%, 50%, 25%, or Off\n
OHstin 0:da2b8c7a1ec1 18 @brief the setting is persistent i.e saved onto onboard flash memory\n
OHstin 0:da2b8c7a1ec1 19 @author Augustine K Kizito\n
OHstin 0:da2b8c7a1ec1 20 @date April 2015
OHstin 0:da2b8c7a1ec1 21
OHstin 0:da2b8c7a1ec1 22 */
OHstin 0:da2b8c7a1ec1 23
OHstin 0:da2b8c7a1ec1 24
OHstin 0:da2b8c7a1ec1 25
OHstin 0:da2b8c7a1ec1 26 class BrightnessScreen: public SettingController // inherit SettingController class
OHstin 0:da2b8c7a1ec1 27 {
OHstin 0:da2b8c7a1ec1 28 private:
OHstin 0:da2b8c7a1ec1 29 bool changeScreen; // tracks if the user pressed a button that changes the screen
OHstin 0:da2b8c7a1ec1 30 char *brightnessKey; // key to be used for the cfg file
OHstin 0:da2b8c7a1ec1 31 char brightnessValue[BUFSIZ]; // buffer to store the brightness value retrieved from cfg file
OHstin 0:da2b8c7a1ec1 32 int brightness; // the brightness stored as an int
OHstin 0:da2b8c7a1ec1 33
OHstin 0:da2b8c7a1ec1 34 void onScrollUp(); // user pressed the scroll up button
OHstin 0:da2b8c7a1ec1 35 void onScrollDown(); // user pressed the scroll down button
OHstin 0:da2b8c7a1ec1 36 void onEnter(); // user pressed the enter button
OHstin 0:da2b8c7a1ec1 37 void onBack(); // user pressed the back button
OHstin 0:da2b8c7a1ec1 38 void saveSetting(); // save a selected option as a setting
OHstin 0:da2b8c7a1ec1 39 void adjustBrightness(); //adjust brightness according to the stored setting
OHstin 0:da2b8c7a1ec1 40
OHstin 0:da2b8c7a1ec1 41 public:
OHstin 0:da2b8c7a1ec1 42 /**
OHstin 0:da2b8c7a1ec1 43 This function creates an instance of the brightness screen
OHstin 0:da2b8c7a1ec1 44
OHstin 0:da2b8c7a1ec1 45 */
OHstin 0:da2b8c7a1ec1 46 BrightnessScreen()
OHstin 0:da2b8c7a1ec1 47 :SettingController( "Brightness", // Title
OHstin 0:da2b8c7a1ec1 48 "100%", // Option 1
OHstin 0:da2b8c7a1ec1 49 "50%", // Option 2
OHstin 0:da2b8c7a1ec1 50 "25%", // Option 3
OHstin 0:da2b8c7a1ec1 51 "Off", // Option 4
OHstin 0:da2b8c7a1ec1 52 4) // Number of Options
OHstin 0:da2b8c7a1ec1 53 {};
OHstin 0:da2b8c7a1ec1 54 /**
OHstin 0:da2b8c7a1ec1 55 This function manages the exectution of the brightness screen
OHstin 0:da2b8c7a1ec1 56
OHstin 0:da2b8c7a1ec1 57 @returns
OHstin 0:da2b8c7a1ec1 58 -1 - navigate to previous screen
OHstin 0:da2b8c7a1ec1 59 */
OHstin 0:da2b8c7a1ec1 60 int start();
OHstin 0:da2b8c7a1ec1 61
OHstin 0:da2b8c7a1ec1 62 };
OHstin 0:da2b8c7a1ec1 63
OHstin 0:da2b8c7a1ec1 64
OHstin 0:da2b8c7a1ec1 65
OHstin 0:da2b8c7a1ec1 66 int BrightnessScreen::start()
OHstin 0:da2b8c7a1ec1 67 {
OHstin 0:da2b8c7a1ec1 68
OHstin 0:da2b8c7a1ec1 69 enterButton.mode(PullDown); // activate pull down resistor
OHstin 0:da2b8c7a1ec1 70 upButton.mode(PullDown); // activate pull down resistor
OHstin 0:da2b8c7a1ec1 71 downButton.mode(PullDown); // acivate pull down resistor
OHstin 0:da2b8c7a1ec1 72 backButton.mode(PullDown); // activate pull down resistor
OHstin 0:da2b8c7a1ec1 73
OHstin 0:da2b8c7a1ec1 74 upButton.rise(this,&BrightnessScreen::onScrollUp); // call onScrollUp when the up button is pressed
OHstin 0:da2b8c7a1ec1 75 downButton.rise(this,&BrightnessScreen::onScrollDown); // call onScrollDown when the down button is pressed
OHstin 0:da2b8c7a1ec1 76 enterButton.rise(this,&BrightnessScreen::onEnter); // call onEnter when the enter button is pressed
OHstin 0:da2b8c7a1ec1 77 backButton.rise(this, &BrightnessScreen::onBack);
OHstin 0:da2b8c7a1ec1 78
OHstin 0:da2b8c7a1ec1 79 brightnessKey = "brightness ";
OHstin 0:da2b8c7a1ec1 80
OHstin 0:da2b8c7a1ec1 81 // prepare the cfg file to be read
OHstin 0:da2b8c7a1ec1 82 cfg.read("/local/bright.cfg");
OHstin 0:da2b8c7a1ec1 83
OHstin 0:da2b8c7a1ec1 84 // if key aand value exist, retrieve and store
OHstin 0:da2b8c7a1ec1 85 if (cfg.getValue(brightnessKey, &brightnessValue[0], sizeof(brightnessValue))) { // if key aand value exist, retrieve and store
OHstin 0:da2b8c7a1ec1 86
OHstin 0:da2b8c7a1ec1 87 brightness = atoi(brightnessValue); // convert string to integer
OHstin 0:da2b8c7a1ec1 88 currentBrightness = brightness;
OHstin 0:da2b8c7a1ec1 89 // set the ListController setting appropriateley
OHstin 0:da2b8c7a1ec1 90 if (brightness == 100) {
OHstin 0:da2b8c7a1ec1 91 SettingController::setCurrentSetting(0);
OHstin 0:da2b8c7a1ec1 92 } else if (brightness == 50) {
OHstin 0:da2b8c7a1ec1 93 SettingController::setCurrentSetting(1);
OHstin 0:da2b8c7a1ec1 94 } else if (brightness == 25) {
OHstin 0:da2b8c7a1ec1 95 SettingController::setCurrentSetting(2);
OHstin 0:da2b8c7a1ec1 96 } else {
OHstin 0:da2b8c7a1ec1 97 SettingController::setCurrentSetting(3);
OHstin 0:da2b8c7a1ec1 98 }
OHstin 0:da2b8c7a1ec1 99 }
OHstin 0:da2b8c7a1ec1 100
OHstin 0:da2b8c7a1ec1 101 adjustBrightness(); // adjust brightness apprpriately
OHstin 0:da2b8c7a1ec1 102 SettingController::showInfo(); //populate the settingController
OHstin 0:da2b8c7a1ec1 103 changeScreen = false; // stay on this screen until further notice
OHstin 0:da2b8c7a1ec1 104
OHstin 0:da2b8c7a1ec1 105 while(!changeScreen) {
OHstin 0:da2b8c7a1ec1 106 // mbed goes to sleep to save power
OHstin 0:da2b8c7a1ec1 107 Sleep();
OHstin 0:da2b8c7a1ec1 108 }
OHstin 0:da2b8c7a1ec1 109
OHstin 0:da2b8c7a1ec1 110 // detach all interrupts
OHstin 0:da2b8c7a1ec1 111 enterButton.rise(NULL);
OHstin 0:da2b8c7a1ec1 112 backButton.rise(NULL);
OHstin 0:da2b8c7a1ec1 113
OHstin 0:da2b8c7a1ec1 114 return -1; // go to the previous screen
OHstin 0:da2b8c7a1ec1 115
OHstin 0:da2b8c7a1ec1 116 }
OHstin 0:da2b8c7a1ec1 117
OHstin 0:da2b8c7a1ec1 118 void BrightnessScreen::onEnter()
OHstin 0:da2b8c7a1ec1 119 {
OHstin 0:da2b8c7a1ec1 120 if (debounceSuccess()) { // debouncing successful
OHstin 0:da2b8c7a1ec1 121 //sound the buzzer
OHstin 0:da2b8c7a1ec1 122 playSound();
OHstin 0:da2b8c7a1ec1 123 // save the selected option as the new setting
OHstin 0:da2b8c7a1ec1 124 saveSetting();
OHstin 0:da2b8c7a1ec1 125
OHstin 0:da2b8c7a1ec1 126 //change the setting marker on the screen accordingly
OHstin 0:da2b8c7a1ec1 127 SettingController::changeSetting();
OHstin 0:da2b8c7a1ec1 128
OHstin 0:da2b8c7a1ec1 129 //adjust the brigtness accordingly
OHstin 0:da2b8c7a1ec1 130 adjustBrightness();
OHstin 0:da2b8c7a1ec1 131
OHstin 0:da2b8c7a1ec1 132
OHstin 0:da2b8c7a1ec1 133 // reset the debounce timer
OHstin 0:da2b8c7a1ec1 134 debounce.reset();
OHstin 0:da2b8c7a1ec1 135 }
OHstin 0:da2b8c7a1ec1 136
OHstin 0:da2b8c7a1ec1 137 }
OHstin 0:da2b8c7a1ec1 138
OHstin 0:da2b8c7a1ec1 139 void BrightnessScreen::onBack()
OHstin 0:da2b8c7a1ec1 140 {
OHstin 0:da2b8c7a1ec1 141
OHstin 0:da2b8c7a1ec1 142 if (debounceSuccess()) { // debouncing successful
OHstin 0:da2b8c7a1ec1 143 // sound the buzzer
OHstin 0:da2b8c7a1ec1 144 playSound();
OHstin 0:da2b8c7a1ec1 145 changeScreen = true; // user wants to change screen
OHstin 0:da2b8c7a1ec1 146
OHstin 0:da2b8c7a1ec1 147 // reset the debounce timer
OHstin 0:da2b8c7a1ec1 148 debounce.reset();
OHstin 0:da2b8c7a1ec1 149 }
OHstin 0:da2b8c7a1ec1 150
OHstin 0:da2b8c7a1ec1 151
OHstin 0:da2b8c7a1ec1 152 }
OHstin 0:da2b8c7a1ec1 153
OHstin 0:da2b8c7a1ec1 154 void BrightnessScreen::onScrollDown()
OHstin 0:da2b8c7a1ec1 155 {
OHstin 0:da2b8c7a1ec1 156 if (debounceSuccess()) { // debouncing successful
OHstin 0:da2b8c7a1ec1 157
OHstin 0:da2b8c7a1ec1 158 SettingController::scrollDown(); //scroll Up
OHstin 0:da2b8c7a1ec1 159 // reset the timer
OHstin 0:da2b8c7a1ec1 160 debounce.reset();
OHstin 0:da2b8c7a1ec1 161 }
OHstin 0:da2b8c7a1ec1 162
OHstin 0:da2b8c7a1ec1 163 }
OHstin 0:da2b8c7a1ec1 164
OHstin 0:da2b8c7a1ec1 165 void BrightnessScreen::onScrollUp()
OHstin 0:da2b8c7a1ec1 166 {
OHstin 0:da2b8c7a1ec1 167 if (debounceSuccess()) { // debouncing successful
OHstin 0:da2b8c7a1ec1 168
OHstin 0:da2b8c7a1ec1 169 SettingController::scrollUp(); //scroll Down
OHstin 0:da2b8c7a1ec1 170 // reset the debounce timer
OHstin 0:da2b8c7a1ec1 171 debounce.reset();
OHstin 0:da2b8c7a1ec1 172 }
OHstin 0:da2b8c7a1ec1 173
OHstin 0:da2b8c7a1ec1 174 }
OHstin 0:da2b8c7a1ec1 175
OHstin 0:da2b8c7a1ec1 176 void BrightnessScreen::saveSetting()
OHstin 0:da2b8c7a1ec1 177 {
OHstin 0:da2b8c7a1ec1 178 // retrieve the current option
OHstin 0:da2b8c7a1ec1 179 int currentOption = SettingController::getCurrentOption();
OHstin 0:da2b8c7a1ec1 180
OHstin 0:da2b8c7a1ec1 181
OHstin 0:da2b8c7a1ec1 182
OHstin 0:da2b8c7a1ec1 183 if ( currentOption == 0) {
OHstin 0:da2b8c7a1ec1 184 cfg.setValue(brightnessKey,"100"); //100%
OHstin 0:da2b8c7a1ec1 185 } else if ( currentOption == 1) {
OHstin 0:da2b8c7a1ec1 186 cfg.setValue(brightnessKey,"50"); //50%
OHstin 0:da2b8c7a1ec1 187 } else if ( currentOption == 2) {
OHstin 0:da2b8c7a1ec1 188 cfg.setValue(brightnessKey,"25"); //25%
OHstin 0:da2b8c7a1ec1 189 } else if ( currentOption == 3) {
OHstin 0:da2b8c7a1ec1 190 cfg.setValue(brightnessKey,"0"); //Off
OHstin 0:da2b8c7a1ec1 191 } else {
OHstin 0:da2b8c7a1ec1 192 // do nothing
OHstin 0:da2b8c7a1ec1 193 }
OHstin 0:da2b8c7a1ec1 194
OHstin 0:da2b8c7a1ec1 195 // write data to the file
OHstin 0:da2b8c7a1ec1 196 cfg.write("/local/bright.cfg");
OHstin 0:da2b8c7a1ec1 197
OHstin 0:da2b8c7a1ec1 198 }
OHstin 0:da2b8c7a1ec1 199
OHstin 0:da2b8c7a1ec1 200 void BrightnessScreen::adjustBrightness()
OHstin 0:da2b8c7a1ec1 201 {
OHstin 0:da2b8c7a1ec1 202
OHstin 0:da2b8c7a1ec1 203 // retireve currentSetting
OHstin 0:da2b8c7a1ec1 204 int currentSetting = SettingController::getCurrentSetting();
OHstin 0:da2b8c7a1ec1 205
OHstin 0:da2b8c7a1ec1 206 if ( currentSetting == 0) { // 100%
OHstin 0:da2b8c7a1ec1 207 currentBrightness = 100;
OHstin 0:da2b8c7a1ec1 208 lcd.setBrightness(1.0);
OHstin 0:da2b8c7a1ec1 209 } else if ( currentSetting == 1) { // 50%
OHstin 0:da2b8c7a1ec1 210 currentBrightness = 50;
OHstin 0:da2b8c7a1ec1 211 lcd.setBrightness(0.5);
OHstin 0:da2b8c7a1ec1 212 } else if ( currentSetting == 2) { // 25%
OHstin 0:da2b8c7a1ec1 213 currentBrightness = 25;
OHstin 0:da2b8c7a1ec1 214 lcd.setBrightness(0.25);
OHstin 0:da2b8c7a1ec1 215 } else if ( currentSetting == 3) { // Off
OHstin 0:da2b8c7a1ec1 216 currentBrightness = 0;
OHstin 0:da2b8c7a1ec1 217 lcd.setBrightness(0);
OHstin 0:da2b8c7a1ec1 218 } else {
OHstin 0:da2b8c7a1ec1 219 // do nothing
OHstin 0:da2b8c7a1ec1 220 }
OHstin 0:da2b8c7a1ec1 221
OHstin 0:da2b8c7a1ec1 222 }
OHstin 0:da2b8c7a1ec1 223
OHstin 0:da2b8c7a1ec1 224 #endif