Added three new methods, one to enable or disable the screen, one to reload the config on the fly, and one to init the object

This commit is contained in:
anschrammh 2019-10-20 18:16:27 +02:00
parent 35596c1296
commit 9660d38dac
2 changed files with 108 additions and 10 deletions

View File

@ -1,12 +1,65 @@
#include "ScreenManager.h" #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} , _viewNotFound{&(displayError), (ErrorInfo *)malloc(sizeof(ErrorInfo)), RESERVED_VIEW_UID, NULL}
, _viewFuncUndefined{&(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} , _currentViewUndefined{&(displayError), (ErrorInfo *)malloc(sizeof(ErrorInfo)), RESERVED_VIEW_UID, NULL}
, _viewFunctionFailedToExecute{&(displayError), (ErrorInfo *)malloc(sizeof(ErrorInfo)), RESERVED_VIEW_UID, NULL} , _viewFunctionFailedToExecute{&(displayError), (ErrorInfo *)malloc(sizeof(ErrorInfo)), RESERVED_VIEW_UID, NULL}
,_sdCardManager(sdCardManager)
{ {
_viewLinkedList = (ViewLinkedList) createEmptyList(); _viewLinkedList = (ViewLinkedList) createEmptyList();
}
boolean ScreenManager::init()
{
applyCfgFromSD();
}
boolean ScreenManager::applyCfgFromSD()
{
if(_sdCardManager != NULL)
{
CFGFileParser cfgFileParser(*_sdCardManager, SCREEN_CFG_FILE);
CFGDictionary<CFGParameterValue> *cfgDictionary = (CFGDictionary<CFGParameterValue> *) 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.setRotation(OR_0);
_displayRef.setTextColor(WHITE); _displayRef.setTextColor(WHITE);
} }
@ -135,6 +188,7 @@ ViewLink* ScreenManager::getLinkByUID(ViewLinkedList viewLinkedList, const unsig
boolean ScreenManager::displayView(const int UID) boolean ScreenManager::displayView(const int UID)
{ {
if(!_enabled) return true;
//Reset draw settings: //Reset draw settings:
_displayRef.clearDisplay(); _displayRef.clearDisplay();
_displayRef.setCursor(0,0); _displayRef.setCursor(0,0);
@ -144,7 +198,7 @@ boolean ScreenManager::displayView(const int UID)
if(UID == -1 && _currentView == NO_CURRENT_VIEW) if(UID == -1 && _currentView == NO_CURRENT_VIEW)
{ {
//We display an error message on the screen //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; ((ErrorInfo *)_currentViewUndefined.pData)->viewUID = UID;
(*_currentViewUndefined.viewLogicFunction)(_displayRef, _currentViewUndefined.pData); (*_currentViewUndefined.viewLogicFunction)(_displayRef, _currentViewUndefined.pData);
_displayRef.display(); _displayRef.display();
@ -157,7 +211,7 @@ boolean ScreenManager::displayView(const int UID)
if(_currentView->viewLogicFunction == NULL) if(_currentView->viewLogicFunction == NULL)
{ {
//We display an error message on the screen //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; ((ErrorInfo *)_viewFuncUndefined.pData)->viewUID = _currentView->UID;
(*_viewFuncUndefined.viewLogicFunction)(_displayRef, _viewFuncUndefined.pData); (*_viewFuncUndefined.viewLogicFunction)(_displayRef, _viewFuncUndefined.pData);
_displayRef.display(); _displayRef.display();
@ -169,7 +223,7 @@ boolean ScreenManager::displayView(const int UID)
{ {
//We display an error message on the screen //We display an error message on the screen
_displayRef.clearDisplay(); _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; ((ErrorInfo *)_viewFunctionFailedToExecute.pData)->viewUID = _currentView->UID;
(*_viewFunctionFailedToExecute.viewLogicFunction)(_displayRef, _viewFunctionFailedToExecute.pData); (*_viewFunctionFailedToExecute.viewLogicFunction)(_displayRef, _viewFunctionFailedToExecute.pData);
_displayRef.display(); _displayRef.display();
@ -187,7 +241,7 @@ boolean ScreenManager::displayView(const int UID)
if(viewLink == NULL) if(viewLink == NULL)
{ {
//We display an error message on the screen //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; ((ErrorInfo *)_viewNotFound.pData)->viewUID = UID;
(*_viewNotFound.viewLogicFunction)(_displayRef, _viewNotFound.pData); (*_viewNotFound.viewLogicFunction)(_displayRef, _viewNotFound.pData);
_displayRef.display(); _displayRef.display();
@ -198,7 +252,7 @@ boolean ScreenManager::displayView(const int UID)
}else if(viewLink->viewLogicFunction == NULL) }else if(viewLink->viewLogicFunction == NULL)
{ {
//We display an error message on the screen //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; ((ErrorInfo *)_viewFuncUndefined.pData)->viewUID = UID;
(*_viewFuncUndefined.viewLogicFunction)(_displayRef, _viewFuncUndefined.pData); (*_viewFuncUndefined.viewLogicFunction)(_displayRef, _viewFuncUndefined.pData);
_displayRef.display(); _displayRef.display();
@ -213,7 +267,7 @@ boolean ScreenManager::displayView(const int UID)
{ {
//We display an error message on the screen //We display an error message on the screen
_displayRef.clearDisplay(); _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; ((ErrorInfo *)_viewFunctionFailedToExecute.pData)->viewUID = UID;
(*_viewFunctionFailedToExecute.viewLogicFunction)(_displayRef, _viewFunctionFailedToExecute.pData); (*_viewFunctionFailedToExecute.viewLogicFunction)(_displayRef, _viewFunctionFailedToExecute.pData);
_displayRef.display(); _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 ScreenManager::getViewCount()
{ {
unsigned char counter = 0; unsigned char counter = 0;
@ -364,3 +432,20 @@ const char* ScreenManager::getErrorMessage()const
return "UNKNOWN ERROR"; 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;
}
}

View File

@ -1,7 +1,12 @@
#ifndef SCREENMANAGER_H #ifndef SCREENMANAGER_H
#define SCREENMANAGER_H #define SCREENMANAGER_H
#include "definition.h"
#include <Adafruit_SSD1306.h> #include <Adafruit_SSD1306.h>
#include "definition.h"
#include "SDCardManager.h"
#include "CFGFileParser.h"
#include "CFGDictionary.h"
class ScreenManager class ScreenManager
{ {
@ -13,12 +18,16 @@ class ScreenManager
boolean removeView(const unsigned char UID); boolean removeView(const unsigned char UID);
boolean displayView(const int UID = -1); boolean displayView(const int UID = -1);
boolean displayNextView(); boolean displayNextView();
boolean applyCfgFromSD();
void invertDisplayColor(const boolean inverted); void invertDisplayColor(const boolean inverted);
void orientDisplay(const Orientation orientation); void orientDisplay(const Orientation orientation);
void dimDisplay(const boolean dimmed); void dimDisplay(const boolean dimmed);
void clearDisplay(const boolean bufferOnly = false); void clearDisplay(const boolean bufferOnly = false);
void sleep(); void sleep();
void wakeUp(); void wakeUp();
void setEnabled(boolean value);
boolean getEnabled();
boolean init();
Error getError() const; Error getError() const;
const char* getErrorMessage() const; const char* getErrorMessage() const;
@ -32,7 +41,7 @@ class ScreenManager
void iterateThroughList(); void iterateThroughList();
protected: protected:
ScreenManager(Adafruit_SSD1306 &display); ScreenManager(Adafruit_SSD1306 &display, SDCardManager *sdCardManager = NULL);
private: private:
void *createEmptyList(); void *createEmptyList();
boolean addNewLinkAtTheEnd(ViewLinkedList *viewLinkedList, ViewLink viewLink); boolean addNewLinkAtTheEnd(ViewLinkedList *viewLinkedList, ViewLink viewLink);
@ -40,14 +49,18 @@ class ScreenManager
ViewLink* getLinkByUID(ViewLinkedList viewLinkedList, const unsigned char UID); ViewLink* getLinkByUID(ViewLinkedList viewLinkedList, const unsigned char UID);
boolean isListEmpty(ViewLinkedList viewLinkedList); boolean isListEmpty(ViewLinkedList viewLinkedList);
static boolean displayError(Adafruit_SSD1306 &display, void *pData); static boolean displayError(Adafruit_SSD1306 &display, void *pData);
void setDefault();
uint8_t orientationTranslator(uint16_t degres);
Adafruit_SSD1306 &_displayRef; Adafruit_SSD1306 &_displayRef;
ViewLinkedList _viewLinkedList; ViewLinkedList _viewLinkedList;
Error _error; Error _error;
boolean _displayColorInverted; boolean _displayColorInverted;
boolean _displayDimmed; boolean _displayDimmed;
boolean _enabled;
ViewLink* _currentView; ViewLink* _currentView;
ViewLink _viewNotFound, _viewFuncUndefined, _currentViewUndefined, _viewFunctionFailedToExecute; 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. //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; typedef struct {const __FlashStringHelper* errorMessage; int viewUID;} ErrorInfo;