Distance Sensor Embedded Systems Project SID: 200864479 James Erringham-Bruce
Dependencies: N5110 SRF02-JEB mbed
Diff: UserSettings/UserSettings.h
- Revision:
- 1:f82359c58eda
- Child:
- 2:01f697b856de
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/UserSettings/UserSettings.h Wed May 04 21:20:21 2016 +0000 @@ -0,0 +1,276 @@ +/* +@file Settings.h +@brief Header file containing member functions and variables +@author James Erringham-Bruce +*/ + +#ifndef USERSETTINGS_H +#define USERSETTINGS_H + +#include "mbed.h" // mbed library +#include "DataController.h" // handling the common data +#include <string> // string library + +// creating the class used in plotting the graph +class UserSettings +{ + // defining the public functions +public: + + /** + @fn printScreen + @brief prints the settings screen + @brief controls the scroller icon at the far right of the screen + */ + void printScreen(); + /** + @fn scrollUp_down + @brief control the scrolling of the screen + @brief both directions ( up, down ) + */ + void scrollUp_Down(int bank); + /** + @fn printText + @brief controls the printing of the text + */ + + /** + @fn cursorControl + @brief controlls the cursors movement and its selection + */ + int setBank(); + + void SendBufferToLCD(); + + // defining the private functions +private: + /** + @var bankValue + @brief the bank that the cursor currently lies + */ + int bankValue; +}; +#endif + +struct settingsStrings { + char LED_State[5]; // 3 chars to hold either "on" or "off" + char buzzer_State[5]; // 3 chars to hold either "on" or "off" +}; + +settingsStrings setAndSaveSettings(); + +char buffer2[14]; + +char OFF[] = "OFF"; // copy this string when off... +char ON[] = "ON"; // copy this string when on... + +volatile bool LED_Toggle = 0; +volatile bool buzzer_Toggle = 0; +volatile bool brightness_Toggle = 0; + +void printText(settingsStrings state); +void goBackToMenu(); +void changeBrightness(float br); + +volatile int savedBrightness = 10; // intially set to 50% + +settingsStrings setString; + +// SAVE THE STRINGS TO THE SETTINGS SCREEN AND REWRITE THE CHANGED DATA TO THE SYSTEM... +settingsStrings setAndSaveSettings() +{ + if (selectorPot < 0.15f) { // for the LED toggle states + if (settings_flag) { + LED_Toggle = !LED_Toggle; + if (LED_Toggle) { + strcpy(setString.LED_State,ON); + LED = 1; + } else { + strcpy(setString.LED_State,OFF); + LED = 0; + } + settings_flag = 0; // put the flag back to zero ready for another use + } + } + if (selectorPot >= 0.3f && selectorPot < 0.45f) { // for the Buzzer toggle states + if (settings_flag) { + buzzer_Toggle = !buzzer_Toggle; + if (buzzer_Toggle) { + strcpy(setString.buzzer_State,ON); + } else { + strcpy(setString.buzzer_State,OFF); + } + settings_flag = 0; // put the flag back to zero ready for another use + } + } + if (selectorPot >= 0.7f ) { // hold while brightness is changed + if (settings_flag) { + do { + int brightness = selectorPot*100; + changeBrightness(selectorPot); + int length = sprintf(buffer,"%d %% ",brightness); // defining max length of chars 84 ÷ 6 = 14 + if ( length <= 14 ) { // if string will fit on display + lcd.printString(buffer, 35, 5); + } + if (backButton) { + settings_flag = 0; + } + lcd.printString("Adjust Your",9,2); + lcd.printString("Brightness",13,3); + } while (settings_flag); + } + } + return setString; +} + + +void UserSettings::printScreen() +{ + unsigned int y_scroller = 0; + interruptSelector.fall(&settings_isr); + + settingsStrings newStrings; // call the struct + settings_flag = 0; + strcpy(setString.LED_State,OFF); // set initial state as off + strcpy(setString.buzzer_State,OFF); // set initial state as off + while (1) { + //settings_flag = 0; // put the flag back to zero ready for another use + if (settings_timer_flag) { + settings_timer_flag = 0; + + lcd.clear(); + unsigned int bank = setBank(); + // normalising the scroller icons pixel values + if ( selectorPot > 0.45f ) { + y_scroller = ((selectorPot*25)*2) - 10; // 40 is the amount of free pixels the scrolling icon can move + } + // doubled the normalised value to compensate for the loss of pot range due to brightness.. + // ..taking up most of the value + if (y_scroller > 48) {// ensuring the scroller does not exceed out of bounds + y_scroller = 48; + } + printText(newStrings); + lcd.drawLine(82,y_scroller,82,y_scroller+8,1);// scrolling icon + lcd.drawLine(83,y_scroller,83,y_scroller+8,1);// scrolling icon + + lcd.refresh(); + } + sleep(); + } +} + +int UserSettings::setBank() +{ + int currentBank = 3; + + if ( selectorPot < 0.15f ) { + currentBank = 3; + } + if ( selectorPot >= 0.15f && selectorPot < 0.3f ) { + currentBank = 4; + } + if ( selectorPot >= 0.3f && selectorPot < 0.45f ) { + currentBank = 5; + } + if ( selectorPot >= 0.45f && selectorPot < 0.7f ) { + currentBank = 6; // exceeds screens length, scrolling algorithm must be implemented + } + if ( selectorPot >= 0.7f ) { + currentBank = 7; // exceeds screens length, scrolling algorithm must be implemented + } + + int cursorBank = currentBank; // creating a stopping point for the cursor + if ( currentBank > 5 ) { + cursorBank = 5; + } + lcd.printChar('<',70, cursorBank); + return currentBank; +} + +// FUNCTION TO OVERRIDE, WHEN THE BANK IS > 5 PUSH TEXT BACK TO A BANK ON SCREEN +void printText(settingsStrings state) +{ + + state = setAndSaveSettings(); // assign new values + + // Print text as an array so that it can be manipulated to act as if the + // text is scrolliing bank by bank + const char screenText[8][14] = { + {" SETTINGS"}, + { }, // empty slots + {"LED:" }, + { }, // empty slots + {"BUZZER:" }, + { }, // empty slots + {"BRIGHTNESS:"}, + { }, // empty slots + }; + int length, length2; + unsigned int textRow = 0; + + if ( selectorPot < 0.45f ) { + textRow = 5; + length = sprintf(buffer,"%s",state.LED_State); // defining max length of chars 84 ÷ 6 = 14 + if ( length <= 14 ) { // if string will fit on display + lcd.printString(buffer, 20, 3); + } + length2 = sprintf(buffer2,"%s",state.buzzer_State); // defining max length of chars 84 ÷ 6 = 14 + if ( length2 <= 14 ) { // if string will fit on display + lcd.printString(buffer2, 20, 5); + } + + } else if ( selectorPot >= 0.45f && selectorPot < 0.7f ) { + textRow = 6; + length = sprintf(buffer,"%s",state.LED_State); // defining max length of chars 84 ÷ 6 = 14 + if ( length <= 14 ) { // if string will fit on display + lcd.printString(buffer, 20, 2); + } + length2 = sprintf(buffer2,"%s",state.buzzer_State); // defining max length of chars 84 ÷ 6 = 14 + if ( length2 <= 14 ) { // if string will fit on display + lcd.printString(buffer2, 20, 4); + } + + } else if ( selectorPot >= 0.7f) { + textRow = 7; + length = sprintf(buffer,"%s",state.LED_State); // defining max length of chars 84 ÷ 6 = 14 + if ( length <= 14 ) { // if string will fit on display + lcd.printString(buffer, 20, 1); + } + length2 = sprintf(buffer2,"%s",state.buzzer_State); // defining max length of chars 84 ÷ 6 = 14 + if ( length2 <= 14 ) { // if string will fit on display + lcd.printString(buffer2, 20, 3); + } + } + lcd.printString(screenText[textRow - 5],2,0); + lcd.printString(screenText[textRow - 4],2,1); + lcd.printString(screenText[textRow - 3],2,2); + lcd.printString(screenText[textRow - 2],2,3); + lcd.printString(screenText[textRow - 1],2,4); + lcd.printString(screenText[textRow],2,5); +} + +void settings_isr() +{ + settings_flag = 1; +} + +void goBackToMenu() +{ + if (backButton) { + + } +} + +void changeBrightness(float br) +{ + //br *= 0.12f; + + if(br>=1) { + br = 1; + } else if(br <= 0) { + br = 0; + } + + + backLight.write(br); +} \ No newline at end of file