diff --git a/src/app/ScreenManager.cpp b/src/app/ScreenManager.cpp index 733683e..03e581b 100644 --- a/src/app/ScreenManager.cpp +++ b/src/app/ScreenManager.cpp @@ -1,12 +1,65 @@ #include "ScreenManager.h" -ScreenManager::ScreenManager(Adafruit_SSD1306 &display) : _displayRef(display), _displayColorInverted(false), _displayDimmed(false), _currentView(NO_CURRENT_VIEW), _error(OK) +ScreenManager::ScreenManager(Adafruit_SSD1306 &display, SDCardManager *sdCardManager) : _displayRef(display), _displayColorInverted(false), _displayDimmed(false), _enabled(true), _currentView(NO_CURRENT_VIEW), _error(OK) , _viewNotFound{&(displayError), (ErrorInfo *)malloc(sizeof(ErrorInfo)), RESERVED_VIEW_UID, NULL} , _viewFuncUndefined{&(displayError), (ErrorInfo *)malloc(sizeof(ErrorInfo)), RESERVED_VIEW_UID, NULL} , _currentViewUndefined{&(displayError), (ErrorInfo *)malloc(sizeof(ErrorInfo)), RESERVED_VIEW_UID, NULL} , _viewFunctionFailedToExecute{&(displayError), (ErrorInfo *)malloc(sizeof(ErrorInfo)), RESERVED_VIEW_UID, NULL} +,_sdCardManager(sdCardManager) { _viewLinkedList = (ViewLinkedList) createEmptyList(); +} + +boolean ScreenManager::init() +{ + applyCfgFromSD(); +} + +boolean ScreenManager::applyCfgFromSD() +{ + if(_sdCardManager != NULL) + { + CFGFileParser cfgFileParser(*_sdCardManager, SCREEN_CFG_FILE); + CFGDictionary *cfgDictionary = (CFGDictionary *) cfgFileParser.parseFile(); + + if(cfgDictionary == NULL) + { + setDefault(); + return false; + } + + + if( cfgDictionary->get("ENABLED") != NULL && + cfgDictionary->get("DIMMED") != NULL && + cfgDictionary->get("INVERTED") != NULL && + cfgDictionary->get("ORIENTATION") != NULL && + cfgDictionary->get("AUTO_OFF") != NULL) + { + setEnabled(cfgDictionary->get("ENABLED")->booleanValue()); + _displayRef.setRotation(orientationTranslator(cfgDictionary->get("ORIENTATION")->intValue())); + _displayRef.invertDisplay(cfgDictionary->get("INVERTED")->booleanValue()); + _displayRef.dim(cfgDictionary->get("DIMMED")->booleanValue()); + _displayRef.setTextColor(WHITE); + + delete cfgDictionary; + return true; + } + else //Default value applied + { + setDefault(); + delete cfgDictionary; + return false; + } + } + else //Default value applied + { + setDefault(); + return false; + } +} + +void ScreenManager::setDefault() +{ _displayRef.setRotation(OR_0); _displayRef.setTextColor(WHITE); } @@ -135,6 +188,7 @@ ViewLink* ScreenManager::getLinkByUID(ViewLinkedList viewLinkedList, const unsig boolean ScreenManager::displayView(const int UID) { + if(!_enabled) return true; //Reset draw settings: _displayRef.clearDisplay(); _displayRef.setCursor(0,0); @@ -144,7 +198,7 @@ boolean ScreenManager::displayView(const int UID) if(UID == -1 && _currentView == NO_CURRENT_VIEW) { //We display an error message on the screen - ((ErrorInfo *)_currentViewUndefined.pData)->errorMessage = F("Could not display current view"); + ((ErrorInfo *)_currentViewUndefined.pData)->errorMessage = FPSTR("Could not display current view"); ((ErrorInfo *)_currentViewUndefined.pData)->viewUID = UID; (*_currentViewUndefined.viewLogicFunction)(_displayRef, _currentViewUndefined.pData); _displayRef.display(); @@ -157,7 +211,7 @@ boolean ScreenManager::displayView(const int UID) if(_currentView->viewLogicFunction == NULL) { //We display an error message on the screen - ((ErrorInfo *)_viewFuncUndefined.pData)->errorMessage = F("View function is NULL"); + ((ErrorInfo *)_viewFuncUndefined.pData)->errorMessage = FPSTR("View function is NULL"); ((ErrorInfo *)_viewFuncUndefined.pData)->viewUID = _currentView->UID; (*_viewFuncUndefined.viewLogicFunction)(_displayRef, _viewFuncUndefined.pData); _displayRef.display(); @@ -169,7 +223,7 @@ boolean ScreenManager::displayView(const int UID) { //We display an error message on the screen _displayRef.clearDisplay(); - ((ErrorInfo *)_viewFunctionFailedToExecute.pData)->errorMessage = F("View function failed\nto execute"); + ((ErrorInfo *)_viewFunctionFailedToExecute.pData)->errorMessage = FPSTR("View function failed\nto execute"); ((ErrorInfo *)_viewFunctionFailedToExecute.pData)->viewUID = _currentView->UID; (*_viewFunctionFailedToExecute.viewLogicFunction)(_displayRef, _viewFunctionFailedToExecute.pData); _displayRef.display(); @@ -187,7 +241,7 @@ boolean ScreenManager::displayView(const int UID) if(viewLink == NULL) { //We display an error message on the screen - ((ErrorInfo *)_viewNotFound.pData)->errorMessage = F("View does not exist"); + ((ErrorInfo *)_viewNotFound.pData)->errorMessage = FPSTR("View does not exist"); ((ErrorInfo *)_viewNotFound.pData)->viewUID = UID; (*_viewNotFound.viewLogicFunction)(_displayRef, _viewNotFound.pData); _displayRef.display(); @@ -198,7 +252,7 @@ boolean ScreenManager::displayView(const int UID) }else if(viewLink->viewLogicFunction == NULL) { //We display an error message on the screen - ((ErrorInfo *)_viewFuncUndefined.pData)->errorMessage = F("View function is NULL"); + ((ErrorInfo *)_viewFuncUndefined.pData)->errorMessage = FPSTR("View function is NULL"); ((ErrorInfo *)_viewFuncUndefined.pData)->viewUID = UID; (*_viewFuncUndefined.viewLogicFunction)(_displayRef, _viewFuncUndefined.pData); _displayRef.display(); @@ -213,7 +267,7 @@ boolean ScreenManager::displayView(const int UID) { //We display an error message on the screen _displayRef.clearDisplay(); - ((ErrorInfo *)_viewFunctionFailedToExecute.pData)->errorMessage = F("View function failed\nto execute"); + ((ErrorInfo *)_viewFunctionFailedToExecute.pData)->errorMessage = FPSTR("View function failed\nto execute"); ((ErrorInfo *)_viewFunctionFailedToExecute.pData)->viewUID = UID; (*_viewFunctionFailedToExecute.viewLogicFunction)(_displayRef, _viewFunctionFailedToExecute.pData); _displayRef.display(); @@ -300,6 +354,20 @@ void ScreenManager::clearDisplay(const boolean bufferOnly) } } +void ScreenManager::setEnabled(boolean value) +{ + _enabled = value; + if(value) + wakeUp(); + else + sleep(); +} + +boolean ScreenManager::getEnabled() +{ + return _enabled; +} + unsigned char ScreenManager::getViewCount() { unsigned char counter = 0; @@ -364,3 +432,20 @@ const char* ScreenManager::getErrorMessage()const return "UNKNOWN ERROR"; } } + +uint8_t ScreenManager::orientationTranslator(uint16_t degres) +{ + switch(degres) + { + case 0: + return 2; + case 90: + return 3; + case 180: + return 0; + case 270: + return 1; + default : + return 2; + } +} diff --git a/src/app/ScreenManager.h b/src/app/ScreenManager.h index 5e73f96..cf815d1 100644 --- a/src/app/ScreenManager.h +++ b/src/app/ScreenManager.h @@ -1,7 +1,12 @@ #ifndef SCREENMANAGER_H #define SCREENMANAGER_H -#include "definition.h" + #include +#include "definition.h" +#include "SDCardManager.h" +#include "CFGFileParser.h" +#include "CFGDictionary.h" + class ScreenManager { @@ -13,12 +18,16 @@ class ScreenManager boolean removeView(const unsigned char UID); boolean displayView(const int UID = -1); boolean displayNextView(); + boolean applyCfgFromSD(); void invertDisplayColor(const boolean inverted); void orientDisplay(const Orientation orientation); void dimDisplay(const boolean dimmed); void clearDisplay(const boolean bufferOnly = false); void sleep(); void wakeUp(); + void setEnabled(boolean value); + boolean getEnabled(); + boolean init(); Error getError() const; const char* getErrorMessage() const; @@ -32,7 +41,7 @@ class ScreenManager void iterateThroughList(); protected: - ScreenManager(Adafruit_SSD1306 &display); + ScreenManager(Adafruit_SSD1306 &display, SDCardManager *sdCardManager = NULL); private: void *createEmptyList(); boolean addNewLinkAtTheEnd(ViewLinkedList *viewLinkedList, ViewLink viewLink); @@ -40,15 +49,19 @@ class ScreenManager ViewLink* getLinkByUID(ViewLinkedList viewLinkedList, const unsigned char UID); boolean isListEmpty(ViewLinkedList viewLinkedList); static boolean displayError(Adafruit_SSD1306 &display, void *pData); + void setDefault(); + uint8_t orientationTranslator(uint16_t degres); Adafruit_SSD1306 &_displayRef; ViewLinkedList _viewLinkedList; Error _error; boolean _displayColorInverted; boolean _displayDimmed; + boolean _enabled; ViewLink* _currentView; ViewLink _viewNotFound, _viewFuncUndefined, _currentViewUndefined, _viewFunctionFailedToExecute; - + SDCardManager *_sdCardManager; + //This structure contains the error message as well as the UID from the view that caused the error if available. typedef struct {const __FlashStringHelper* errorMessage; int viewUID;} ErrorInfo; };