Year Two Project ELEC 2645: Embedded Systems Project Portable Weather Station
Dependencies: BMP180 ConfigFile N5110 PowerControl beep mbed
SoundScreen.h@0:da2b8c7a1ec1, 2015-05-11 (annotated)
- Committer:
- OHstin
- Date:
- Mon May 11 15:25:52 2015 +0000
- Revision:
- 0:da2b8c7a1ec1
Completed Weather Station
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
OHstin | 0:da2b8c7a1ec1 | 1 | /** |
OHstin | 0:da2b8c7a1ec1 | 2 | @file SoundScreen.h |
OHstin | 0:da2b8c7a1ec1 | 3 | |
OHstin | 0:da2b8c7a1ec1 | 4 | */ |
OHstin | 0:da2b8c7a1ec1 | 5 | |
OHstin | 0:da2b8c7a1ec1 | 6 | #ifndef SOUNDSCREEN_H |
OHstin | 0:da2b8c7a1ec1 | 7 | #define SOUNDSCREEN_H |
OHstin | 0:da2b8c7a1ec1 | 8 | |
OHstin | 0:da2b8c7a1ec1 | 9 | #include "SettingController.h" |
OHstin | 0:da2b8c7a1ec1 | 10 | #include "Inputs.h" |
OHstin | 0:da2b8c7a1ec1 | 11 | #include "Outputs.h" |
OHstin | 0:da2b8c7a1ec1 | 12 | #include "ConfigFile.h" |
OHstin | 0:da2b8c7a1ec1 | 13 | |
OHstin | 0:da2b8c7a1ec1 | 14 | /** |
OHstin | 0:da2b8c7a1ec1 | 15 | @brief Shows the current sound setting of the buzzer\n |
OHstin | 0:da2b8c7a1ec1 | 16 | @brief Consists of a Setting Controller\n |
OHstin | 0:da2b8c7a1ec1 | 17 | @brief Sound can be set On 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 |
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 | class SoundScreen: public SettingController // inherit SettingController class |
OHstin | 0:da2b8c7a1ec1 | 26 | { |
OHstin | 0:da2b8c7a1ec1 | 27 | private: |
OHstin | 0:da2b8c7a1ec1 | 28 | bool changeScreen; // tracks if the user pressed a button that changes the screen |
OHstin | 0:da2b8c7a1ec1 | 29 | char *soundKey; // key to be used for the cfg file |
OHstin | 0:da2b8c7a1ec1 | 30 | char soundValue[BUFSIZ]; // buffer to store the brightness value retrieved from cfg file |
OHstin | 0:da2b8c7a1ec1 | 31 | int sound; // the brightness stored as an int |
OHstin | 0:da2b8c7a1ec1 | 32 | |
OHstin | 0:da2b8c7a1ec1 | 33 | void onScrollUp(); // user pressed the scroll up button |
OHstin | 0:da2b8c7a1ec1 | 34 | void onScrollDown(); // user pressed the scroll down button |
OHstin | 0:da2b8c7a1ec1 | 35 | void onEnter(); // user pressed the enter button |
OHstin | 0:da2b8c7a1ec1 | 36 | void onBack(); // user pressed the back button |
OHstin | 0:da2b8c7a1ec1 | 37 | void saveSetting(); // save a selected option as a setting |
OHstin | 0:da2b8c7a1ec1 | 38 | void adjustSound(); //adjust brightness according to the stored setting |
OHstin | 0:da2b8c7a1ec1 | 39 | |
OHstin | 0:da2b8c7a1ec1 | 40 | public: |
OHstin | 0:da2b8c7a1ec1 | 41 | /** |
OHstin | 0:da2b8c7a1ec1 | 42 | Creates an instance of the Sound Screen |
OHstin | 0:da2b8c7a1ec1 | 43 | |
OHstin | 0:da2b8c7a1ec1 | 44 | */ |
OHstin | 0:da2b8c7a1ec1 | 45 | SoundScreen() |
OHstin | 0:da2b8c7a1ec1 | 46 | :SettingController( "Sound Fx", // Title |
OHstin | 0:da2b8c7a1ec1 | 47 | "On", // Option 1 |
OHstin | 0:da2b8c7a1ec1 | 48 | "Off", // Option 2 |
OHstin | 0:da2b8c7a1ec1 | 49 | "", |
OHstin | 0:da2b8c7a1ec1 | 50 | "", |
OHstin | 0:da2b8c7a1ec1 | 51 | 2) // Number of Options |
OHstin | 0:da2b8c7a1ec1 | 52 | {}; |
OHstin | 0:da2b8c7a1ec1 | 53 | /** |
OHstin | 0:da2b8c7a1ec1 | 54 | Manages the execution of the Sound Screen |
OHstin | 0:da2b8c7a1ec1 | 55 | |
OHstin | 0:da2b8c7a1ec1 | 56 | @returns |
OHstin | 0:da2b8c7a1ec1 | 57 | |
OHstin | 0:da2b8c7a1ec1 | 58 | -1 - Navigate to previous screen |
OHstin | 0:da2b8c7a1ec1 | 59 | |
OHstin | 0:da2b8c7a1ec1 | 60 | */ |
OHstin | 0:da2b8c7a1ec1 | 61 | int start(); |
OHstin | 0:da2b8c7a1ec1 | 62 | |
OHstin | 0:da2b8c7a1ec1 | 63 | }; |
OHstin | 0:da2b8c7a1ec1 | 64 | |
OHstin | 0:da2b8c7a1ec1 | 65 | |
OHstin | 0:da2b8c7a1ec1 | 66 | |
OHstin | 0:da2b8c7a1ec1 | 67 | |
OHstin | 0:da2b8c7a1ec1 | 68 | |
OHstin | 0:da2b8c7a1ec1 | 69 | |
OHstin | 0:da2b8c7a1ec1 | 70 | |
OHstin | 0:da2b8c7a1ec1 | 71 | |
OHstin | 0:da2b8c7a1ec1 | 72 | |
OHstin | 0:da2b8c7a1ec1 | 73 | |
OHstin | 0:da2b8c7a1ec1 | 74 | |
OHstin | 0:da2b8c7a1ec1 | 75 | |
OHstin | 0:da2b8c7a1ec1 | 76 | |
OHstin | 0:da2b8c7a1ec1 | 77 | |
OHstin | 0:da2b8c7a1ec1 | 78 | |
OHstin | 0:da2b8c7a1ec1 | 79 | |
OHstin | 0:da2b8c7a1ec1 | 80 | |
OHstin | 0:da2b8c7a1ec1 | 81 | |
OHstin | 0:da2b8c7a1ec1 | 82 | |
OHstin | 0:da2b8c7a1ec1 | 83 | |
OHstin | 0:da2b8c7a1ec1 | 84 | int SoundScreen::start() |
OHstin | 0:da2b8c7a1ec1 | 85 | { |
OHstin | 0:da2b8c7a1ec1 | 86 | |
OHstin | 0:da2b8c7a1ec1 | 87 | enterButton.mode(PullDown); // activate pull down resistor |
OHstin | 0:da2b8c7a1ec1 | 88 | upButton.mode(PullDown); // activate pull down resistor |
OHstin | 0:da2b8c7a1ec1 | 89 | downButton.mode(PullDown); // acivate pull down resistor |
OHstin | 0:da2b8c7a1ec1 | 90 | backButton.mode(PullDown); // activate pull down resistor |
OHstin | 0:da2b8c7a1ec1 | 91 | |
OHstin | 0:da2b8c7a1ec1 | 92 | upButton.rise(this,&SoundScreen::onScrollUp); // call onScrollUp when the up button is pressed |
OHstin | 0:da2b8c7a1ec1 | 93 | downButton.rise(this,&SoundScreen::onScrollDown); // call onScrollDown when the down button is pressed |
OHstin | 0:da2b8c7a1ec1 | 94 | enterButton.rise(this,&SoundScreen::onEnter); // call onEnter when the enter button is pressed |
OHstin | 0:da2b8c7a1ec1 | 95 | backButton.rise(this, &SoundScreen::onBack); |
OHstin | 0:da2b8c7a1ec1 | 96 | |
OHstin | 0:da2b8c7a1ec1 | 97 | debounce.start(); // start the debounce timer |
OHstin | 0:da2b8c7a1ec1 | 98 | |
OHstin | 0:da2b8c7a1ec1 | 99 | soundKey = "sound "; |
OHstin | 0:da2b8c7a1ec1 | 100 | cfg.read("/local/sound.cfg"); // prepare the cfg file to be read |
OHstin | 0:da2b8c7a1ec1 | 101 | |
OHstin | 0:da2b8c7a1ec1 | 102 | // if key aand value exist, retrieve and store |
OHstin | 0:da2b8c7a1ec1 | 103 | if (cfg.getValue(soundKey, &soundValue[0], sizeof(soundValue))) { // if key aand value exist, retrieve and store |
OHstin | 0:da2b8c7a1ec1 | 104 | |
OHstin | 0:da2b8c7a1ec1 | 105 | sound = atoi(soundValue); // convert string to integer |
OHstin | 0:da2b8c7a1ec1 | 106 | |
OHstin | 0:da2b8c7a1ec1 | 107 | // set the ListController setting appropriateley |
OHstin | 0:da2b8c7a1ec1 | 108 | if (sound == 1) { |
OHstin | 0:da2b8c7a1ec1 | 109 | SettingController::setCurrentSetting(0); |
OHstin | 0:da2b8c7a1ec1 | 110 | } else { |
OHstin | 0:da2b8c7a1ec1 | 111 | SettingController::setCurrentSetting(1); |
OHstin | 0:da2b8c7a1ec1 | 112 | } |
OHstin | 0:da2b8c7a1ec1 | 113 | } |
OHstin | 0:da2b8c7a1ec1 | 114 | |
OHstin | 0:da2b8c7a1ec1 | 115 | adjustSound(); // adjust brightness apprpriately |
OHstin | 0:da2b8c7a1ec1 | 116 | SettingController::showInfo(); //populate the settingController |
OHstin | 0:da2b8c7a1ec1 | 117 | changeScreen = false; // stay on this screen until further notice |
OHstin | 0:da2b8c7a1ec1 | 118 | |
OHstin | 0:da2b8c7a1ec1 | 119 | while(!changeScreen) { |
OHstin | 0:da2b8c7a1ec1 | 120 | // mbed goes to sleep to save power |
OHstin | 0:da2b8c7a1ec1 | 121 | Sleep(); |
OHstin | 0:da2b8c7a1ec1 | 122 | } |
OHstin | 0:da2b8c7a1ec1 | 123 | |
OHstin | 0:da2b8c7a1ec1 | 124 | // detach all interrupts |
OHstin | 0:da2b8c7a1ec1 | 125 | enterButton.rise(NULL); |
OHstin | 0:da2b8c7a1ec1 | 126 | backButton.rise(NULL); |
OHstin | 0:da2b8c7a1ec1 | 127 | |
OHstin | 0:da2b8c7a1ec1 | 128 | return -1; // go to the previous screen |
OHstin | 0:da2b8c7a1ec1 | 129 | |
OHstin | 0:da2b8c7a1ec1 | 130 | } |
OHstin | 0:da2b8c7a1ec1 | 131 | |
OHstin | 0:da2b8c7a1ec1 | 132 | void SoundScreen::onEnter() |
OHstin | 0:da2b8c7a1ec1 | 133 | { |
OHstin | 0:da2b8c7a1ec1 | 134 | if (debounceSuccess()) { // debouncing successful |
OHstin | 0:da2b8c7a1ec1 | 135 | |
OHstin | 0:da2b8c7a1ec1 | 136 | // save the setting |
OHstin | 0:da2b8c7a1ec1 | 137 | saveSetting(); |
OHstin | 0:da2b8c7a1ec1 | 138 | |
OHstin | 0:da2b8c7a1ec1 | 139 | //change the setting marker on the screen accordingly |
OHstin | 0:da2b8c7a1ec1 | 140 | SettingController::changeSetting(); |
OHstin | 0:da2b8c7a1ec1 | 141 | |
OHstin | 0:da2b8c7a1ec1 | 142 | //adjust the sound accordingly |
OHstin | 0:da2b8c7a1ec1 | 143 | adjustSound(); |
OHstin | 0:da2b8c7a1ec1 | 144 | |
OHstin | 0:da2b8c7a1ec1 | 145 | playSound(); |
OHstin | 0:da2b8c7a1ec1 | 146 | // reset the debounce timer |
OHstin | 0:da2b8c7a1ec1 | 147 | debounce.reset(); |
OHstin | 0:da2b8c7a1ec1 | 148 | } |
OHstin | 0:da2b8c7a1ec1 | 149 | |
OHstin | 0:da2b8c7a1ec1 | 150 | } |
OHstin | 0:da2b8c7a1ec1 | 151 | |
OHstin | 0:da2b8c7a1ec1 | 152 | void SoundScreen::onBack() |
OHstin | 0:da2b8c7a1ec1 | 153 | { |
OHstin | 0:da2b8c7a1ec1 | 154 | if(debounceSuccess()) { // debounce successful |
OHstin | 0:da2b8c7a1ec1 | 155 | // play the sound |
OHstin | 0:da2b8c7a1ec1 | 156 | playSound(); |
OHstin | 0:da2b8c7a1ec1 | 157 | changeScreen = true; // user wants to change screen |
OHstin | 0:da2b8c7a1ec1 | 158 | // reset the debounce timer |
OHstin | 0:da2b8c7a1ec1 | 159 | debounce.reset(); |
OHstin | 0:da2b8c7a1ec1 | 160 | |
OHstin | 0:da2b8c7a1ec1 | 161 | } |
OHstin | 0:da2b8c7a1ec1 | 162 | |
OHstin | 0:da2b8c7a1ec1 | 163 | |
OHstin | 0:da2b8c7a1ec1 | 164 | } |
OHstin | 0:da2b8c7a1ec1 | 165 | |
OHstin | 0:da2b8c7a1ec1 | 166 | void SoundScreen::onScrollDown() |
OHstin | 0:da2b8c7a1ec1 | 167 | { |
OHstin | 0:da2b8c7a1ec1 | 168 | if (debounceSuccess()) { // debouncing successful |
OHstin | 0:da2b8c7a1ec1 | 169 | SettingController::scrollDown(); //scroll Up |
OHstin | 0:da2b8c7a1ec1 | 170 | debounce.reset(); // reset the debounce timer |
OHstin | 0:da2b8c7a1ec1 | 171 | } |
OHstin | 0:da2b8c7a1ec1 | 172 | |
OHstin | 0:da2b8c7a1ec1 | 173 | } |
OHstin | 0:da2b8c7a1ec1 | 174 | |
OHstin | 0:da2b8c7a1ec1 | 175 | void SoundScreen::onScrollUp() |
OHstin | 0:da2b8c7a1ec1 | 176 | { |
OHstin | 0:da2b8c7a1ec1 | 177 | if (debounceSuccess()) { // debouncing was successful |
OHstin | 0:da2b8c7a1ec1 | 178 | SettingController::scrollUp(); //scroll Down |
OHstin | 0:da2b8c7a1ec1 | 179 | debounce.reset(); // reset the debounce timer |
OHstin | 0:da2b8c7a1ec1 | 180 | } |
OHstin | 0:da2b8c7a1ec1 | 181 | |
OHstin | 0:da2b8c7a1ec1 | 182 | } |
OHstin | 0:da2b8c7a1ec1 | 183 | |
OHstin | 0:da2b8c7a1ec1 | 184 | void SoundScreen::saveSetting() |
OHstin | 0:da2b8c7a1ec1 | 185 | { |
OHstin | 0:da2b8c7a1ec1 | 186 | // retrieve the current option |
OHstin | 0:da2b8c7a1ec1 | 187 | int currentOption = SettingController::getCurrentOption(); |
OHstin | 0:da2b8c7a1ec1 | 188 | |
OHstin | 0:da2b8c7a1ec1 | 189 | if ( currentOption == 0) { |
OHstin | 0:da2b8c7a1ec1 | 190 | cfg.setValue(soundKey,"1"); //On |
OHstin | 0:da2b8c7a1ec1 | 191 | } else if ( currentOption == 1) { |
OHstin | 0:da2b8c7a1ec1 | 192 | cfg.setValue(soundKey,"0"); //Off |
OHstin | 0:da2b8c7a1ec1 | 193 | } else { |
OHstin | 0:da2b8c7a1ec1 | 194 | // do nothing |
OHstin | 0:da2b8c7a1ec1 | 195 | } |
OHstin | 0:da2b8c7a1ec1 | 196 | |
OHstin | 0:da2b8c7a1ec1 | 197 | // write to the cfg file |
OHstin | 0:da2b8c7a1ec1 | 198 | cfg.write("/local/sound.cfg"); |
OHstin | 0:da2b8c7a1ec1 | 199 | } |
OHstin | 0:da2b8c7a1ec1 | 200 | |
OHstin | 0:da2b8c7a1ec1 | 201 | void SoundScreen::adjustSound() |
OHstin | 0:da2b8c7a1ec1 | 202 | { |
OHstin | 0:da2b8c7a1ec1 | 203 | |
OHstin | 0:da2b8c7a1ec1 | 204 | // retireve currentSetting |
OHstin | 0:da2b8c7a1ec1 | 205 | int currentSetting = SettingController::getCurrentSetting(); |
OHstin | 0:da2b8c7a1ec1 | 206 | |
OHstin | 0:da2b8c7a1ec1 | 207 | if ( currentSetting == 0) { // On |
OHstin | 0:da2b8c7a1ec1 | 208 | soundFx = true; |
OHstin | 0:da2b8c7a1ec1 | 209 | } else if ( currentSetting == 1) { // Off |
OHstin | 0:da2b8c7a1ec1 | 210 | soundFx = false; |
OHstin | 0:da2b8c7a1ec1 | 211 | } else { |
OHstin | 0:da2b8c7a1ec1 | 212 | // do nothing |
OHstin | 0:da2b8c7a1ec1 | 213 | } |
OHstin | 0:da2b8c7a1ec1 | 214 | |
OHstin | 0:da2b8c7a1ec1 | 215 | } |
OHstin | 0:da2b8c7a1ec1 | 216 | |
OHstin | 0:da2b8c7a1ec1 | 217 | #endif |