Improvements and bug fixes

This commit is contained in:
anschrammh 2019-04-09 08:00:45 +02:00
parent aa95ce16ed
commit 26ddbedc55
9 changed files with 92 additions and 93 deletions

View File

@ -168,37 +168,3 @@ boolean CFGFileParser::save(void *data)
{ {
return true; return true;
} }
char *CFGFileParser::addChar(char *pointer, const char character)
{
char *tempAddr = NULL;
if(pointer == NULL)
{
tempAddr = (char *) realloc(pointer, 2*sizeof(char));
if(tempAddr == NULL)
return NULL;
else
{
pointer = tempAddr;
pointer[0] = character;
pointer[1] = '\0';
}
}
else
{
tempAddr = (char *) realloc(pointer, (strlen(pointer)+2)*sizeof(char));
if(tempAddr == NULL)
{
free(pointer);
return NULL;
}
else
{
pointer = tempAddr;
pointer[strlen(pointer)+1] = '\0';
pointer[strlen(pointer)] = character;
}
}
return pointer;
}

View File

@ -16,8 +16,6 @@ public:
protected: protected:
private: private:
//This part handles the _buff realloc //This part handles the _buff realloc
char *addChar(char *pointer, const char character);
enum State {INIT, COMMENT_SECTION, LINE_BREAK, PARAM_SECTION, ERROR, OPENING_QUOTE, SEPARATION}; enum State {INIT, COMMENT_SECTION, LINE_BREAK, PARAM_SECTION, ERROR, OPENING_QUOTE, SEPARATION};
enum Type {PARAMETER, VALUE}; enum Type {PARAMETER, VALUE};
State _state; State _state;

View File

@ -6,6 +6,9 @@
#include <stdio.h> #include <stdio.h>
#include <Arduino.h> #include <Arduino.h>
template <typename T>
class Dictionary;
namespace DictionaryHelper namespace DictionaryHelper
{ {
//String helper class with c style char array //String helper class with c style char array
@ -32,6 +35,7 @@ namespace DictionaryHelper
} }
~StringEntity(){free(_string);} ~StringEntity(){free(_string);}
char *getString(){return _string;} char *getString(){return _string;}
Dictionary<StringEntity> *split(char character);
private: private:
char *_string; char *_string;
}; };
@ -78,12 +82,28 @@ public:
return addNewNodeAtTheEnd(dictionaryNode); return addNewNodeAtTheEnd(dictionaryNode);
} }
boolean add(int indice, T *value)
{
char indiceToStr[10];
sprintf(indiceToStr,"%d", indice);
Dictionary *dictionaryNode = new Dictionary(indiceToStr, value);
return addNewNodeAtTheEnd(dictionaryNode);
}
boolean add(const char *parameter, T value) boolean add(const char *parameter, T value)
{ {
Dictionary *dictionaryNode = new Dictionary(parameter, new T(value)); Dictionary *dictionaryNode = new Dictionary(parameter, new T(value));
return addNewNodeAtTheEnd(dictionaryNode); return addNewNodeAtTheEnd(dictionaryNode);
} }
boolean add(int indice, T value)
{
char indiceToStr[10];
sprintf(indiceToStr,"%d", indice);
Dictionary *dictionaryNode = new Dictionary(indiceToStr, new T(value));
return addNewNodeAtTheEnd(dictionaryNode);
}
boolean remove(const char *parameter) boolean remove(const char *parameter)
{ {
if(_head->_next == NULL) return false; if(_head->_next == NULL) return false;

View File

@ -1,6 +1,6 @@
#include "ScreenManager.h" #include "ScreenManager.h"
ScreenManager::ScreenManager(Adafruit_SSD1306 &display) : _displayRef(display), _displayColorInverted(false), _displayDimmed(false), _currentView(NO_CURRENT_VIEW), _error(0) ScreenManager::ScreenManager(Adafruit_SSD1306 &display) : _displayRef(display), _displayColorInverted(false), _displayDimmed(false), _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}
@ -27,7 +27,7 @@ boolean ScreenManager::addNewLinkAtTheEnd(ViewLinkedList *viewLinkedList, ViewLi
ViewLink *newViewLink = (ViewLink*) malloc(sizeof(ViewLink)); ViewLink *newViewLink = (ViewLink*) malloc(sizeof(ViewLink));
if(newViewLink == NULL) if(newViewLink == NULL)
{ {
_error |= MALLOC_ERR; _error = MALLOC_FAILED;
return false; return false;
} }
@ -103,7 +103,7 @@ boolean ScreenManager::isListEmpty(ViewLinkedList viewLinkedList)
return viewLinkedList == NULL; return viewLinkedList == NULL;
} }
unsigned char ScreenManager::getError() const ScreenManager::Error ScreenManager::getError() const
{ {
return _error; return _error;
} }
@ -148,23 +148,37 @@ boolean ScreenManager::displayView(const int UID)
((ErrorInfo *)_currentViewUndefined.pData)->viewUID = UID; ((ErrorInfo *)_currentViewUndefined.pData)->viewUID = UID;
(*_currentViewUndefined.viewLogicFunction)(_displayRef, _currentViewUndefined.pData); (*_currentViewUndefined.viewLogicFunction)(_displayRef, _currentViewUndefined.pData);
_displayRef.display(); _displayRef.display();
_error = CURRENT_VIEW_UNDEFINED;
return false; return false;
} }
else if(UID == -1) else if(UID == -1)
{ {
if(!(*_currentView->viewLogicFunction)(_displayRef, _currentView->pData)) if(_currentView->viewLogicFunction == NULL)
{
//We display an error message on the screen
((ErrorInfo *)_viewFuncUndefined.pData)->errorMessage = F("View function is NULL");
((ErrorInfo *)_viewFuncUndefined.pData)->viewUID = _currentView->UID;
(*_viewFuncUndefined.viewLogicFunction)(_displayRef, _viewFuncUndefined.pData);
_displayRef.display();
_error = VIEW_FUNC_UNDEFINED;
return false;
}
else if(!(*_currentView->viewLogicFunction)(_displayRef, _currentView->pData))
{ {
//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 = F("View function failed\nto execute");
((ErrorInfo *)_viewFunctionFailedToExecute.pData)->viewUID = UID; ((ErrorInfo *)_viewFunctionFailedToExecute.pData)->viewUID = _currentView->UID;
(*_viewFunctionFailedToExecute.viewLogicFunction)(_displayRef, _viewFunctionFailedToExecute.pData); (*_viewFunctionFailedToExecute.viewLogicFunction)(_displayRef, _viewFunctionFailedToExecute.pData);
_displayRef.display(); _displayRef.display();
_currentView = &_viewFunctionFailedToExecute; _error = VIEW_FAILED_TO_EXECUTE;
return false; return false;
} }
_error = OK;
_displayRef.display(); _displayRef.display();
return true; return true;
} }
@ -178,6 +192,7 @@ boolean ScreenManager::displayView(const int UID)
(*_viewNotFound.viewLogicFunction)(_displayRef, _viewNotFound.pData); (*_viewNotFound.viewLogicFunction)(_displayRef, _viewNotFound.pData);
_displayRef.display(); _displayRef.display();
_error = VIEW_NOT_FOUND;
_currentView = &_viewNotFound; _currentView = &_viewNotFound;
return false; return false;
}else if(viewLink->viewLogicFunction == NULL) }else if(viewLink->viewLogicFunction == NULL)
@ -187,8 +202,9 @@ boolean ScreenManager::displayView(const int UID)
((ErrorInfo *)_viewFuncUndefined.pData)->viewUID = UID; ((ErrorInfo *)_viewFuncUndefined.pData)->viewUID = UID;
(*_viewFuncUndefined.viewLogicFunction)(_displayRef, _viewFuncUndefined.pData); (*_viewFuncUndefined.viewLogicFunction)(_displayRef, _viewFuncUndefined.pData);
_displayRef.display(); _displayRef.display();
_currentView = &_viewFuncUndefined; _error = VIEW_FUNC_UNDEFINED;
_currentView = viewLink;
return false; return false;
} }
@ -201,20 +217,20 @@ boolean ScreenManager::displayView(const int UID)
((ErrorInfo *)_viewFunctionFailedToExecute.pData)->viewUID = UID; ((ErrorInfo *)_viewFunctionFailedToExecute.pData)->viewUID = UID;
(*_viewFunctionFailedToExecute.viewLogicFunction)(_displayRef, _viewFunctionFailedToExecute.pData); (*_viewFunctionFailedToExecute.viewLogicFunction)(_displayRef, _viewFunctionFailedToExecute.pData);
_displayRef.display(); _displayRef.display();
_currentView = &_viewFunctionFailedToExecute; _error = VIEW_FAILED_TO_EXECUTE;
_currentView = viewLink;
return false; return false;
} }
_displayRef.display(); _displayRef.display();
_currentView = viewLink; _currentView = viewLink;
_error = OK;
return true; return true;
} }
boolean ScreenManager::displayNextView() boolean ScreenManager::displayNextView()
{ {
if(_currentView->UID < 0) return false;
if(_currentView == NO_CURRENT_VIEW && !isListEmpty(_viewLinkedList)) if(_currentView == NO_CURRENT_VIEW && !isListEmpty(_viewLinkedList))
{ {
_currentView = _viewLinkedList; _currentView = _viewLinkedList;
@ -317,3 +333,25 @@ boolean ScreenManager::displayError(Adafruit_SSD1306 &display, void *pData)
return true; return true;
} }
const char* ScreenManager::getErrorMessage()const
{
//NO_VIEW_ERROR, MALLOC_FAILED, VIEW_NOT_FOUND, VIEW_FUNC_UNDEFINED, VIEW_FAILED_TO_EXECUTE, CURRENT_VIEW_UNDEFINED
switch(_error)
{
case OK:
return "NO ERROR";
case MALLOC_FAILED:
return "MALLOC FAILED";
case VIEW_NOT_FOUND:
return "VIEW NOT FOUND";
case VIEW_FUNC_UNDEFINED:
return "VIEW FUNCTION UNDEFINED (NULL)";
case VIEW_FAILED_TO_EXECUTE:
return "VIEW FAILED TO EXECUTE (RETURNED FALSE)";
case CURRENT_VIEW_UNDEFINED:
return "CURRENT VIEW UNDEFINED";
default :
return "UNKNOWN ERROR";
}
}

View File

@ -7,6 +7,8 @@ class ScreenManager
{ {
friend class SAB; friend class SAB;
public: public:
enum Error {OK, MALLOC_FAILED, VIEW_NOT_FOUND, VIEW_FUNC_UNDEFINED, VIEW_FAILED_TO_EXECUTE, CURRENT_VIEW_UNDEFINED};
boolean addView(boolean (*viewLogicFunction)(Adafruit_SSD1306&, void*), void *pData, const unsigned char UID); boolean addView(boolean (*viewLogicFunction)(Adafruit_SSD1306&, void*), void *pData, const unsigned char UID);
boolean removeView(const unsigned char UID); boolean removeView(const unsigned char UID);
boolean displayView(const int UID = -1); boolean displayView(const int UID = -1);
@ -16,7 +18,8 @@ class ScreenManager
void dimDisplay(const boolean dimmed); void dimDisplay(const boolean dimmed);
void clearDisplay(const boolean bufferOnly = false); void clearDisplay(const boolean bufferOnly = false);
unsigned char getError() const; Error getError() const;
const char* getErrorMessage() const;
boolean isDisplayColorInverted() const; boolean isDisplayColorInverted() const;
Orientation getDisplayOrientation() const; Orientation getDisplayOrientation() const;
boolean isDisplayDimmed() const; boolean isDisplayDimmed() const;
@ -38,7 +41,7 @@ class ScreenManager
Adafruit_SSD1306 &_displayRef; Adafruit_SSD1306 &_displayRef;
ViewLinkedList _viewLinkedList; ViewLinkedList _viewLinkedList;
unsigned char _error; Error _error;
boolean _displayColorInverted; boolean _displayColorInverted;
boolean _displayDimmed; boolean _displayDimmed;
ViewLink* _currentView; ViewLink* _currentView;

View File

@ -252,40 +252,6 @@ unsigned int WEBServerManager::getPort() const
return _port; return _port;
} }
char *WEBServerManager::addChar(char *pointer, const char character)
{
char *tempAddr = NULL;
if(pointer == NULL)
{
tempAddr = (char *) realloc(pointer, 2*sizeof(char));
if(tempAddr == NULL)
return NULL;
else
{
pointer = tempAddr;
pointer[0] = character;
pointer[1] = '\0';
}
}
else
{
tempAddr = (char *) realloc(pointer, (strlen(pointer)+2)*sizeof(char));
if(tempAddr == NULL)
{
free(pointer);
return NULL;
}
else
{
pointer = tempAddr;
pointer[strlen(pointer)+1] = '\0';
pointer[strlen(pointer)] = character;
}
}
return pointer;
}
boolean WEBServerManager::sendPageToClientFromSdCard(WiFiClient *wifiClient) boolean WEBServerManager::sendPageToClientFromSdCard(WiFiClient *wifiClient)
{ {
if(_sdCardManager != NULL) if(_sdCardManager != NULL)

View File

@ -46,7 +46,6 @@ class WEBServerManager
boolean parseQuery(WiFiClient *wifiClient); boolean parseQuery(WiFiClient *wifiClient);
boolean sendPageToClientFromSdCard(WiFiClient *wifiClient); boolean sendPageToClientFromSdCard(WiFiClient *wifiClient);
boolean sendPageToClientFromApiDictio(WiFiClient *wifiClient); boolean sendPageToClientFromApiDictio(WiFiClient *wifiClient);
char *addChar(char *pointer, const char character);
HttpRequestMethod getHttpVerbEnumValue(const char *parseBuffer); HttpRequestMethod getHttpVerbEnumValue(const char *parseBuffer);
HttpVersion getHttpVersionEnumValue(const char *parseBuffer); HttpVersion getHttpVersionEnumValue(const char *parseBuffer);
char *getFilePathByHttpResource(const char *res); char *getFilePathByHttpResource(const char *res);

View File

@ -31,6 +31,10 @@ void setup()
sab.getScreenManager().addView(&(view_1), &v1p, 0); sab.getScreenManager().addView(&(view_1), &v1p, 0);
sab.getScreenManager().addView(&(view_2), &vap, 1); sab.getScreenManager().addView(&(view_2), &vap, 1);
sab.getScreenManager().addView(&(view_3), &vstap, 2); sab.getScreenManager().addView(&(view_3), &vstap, 2);
sab.getScreenManager().addView(&(memInfo), NULL, 3);
sab.getScreenManager().addView(NULL, NULL, 4); //for testin purposes
sab.getScreenManager().addView(&(dummy), NULL, 5); //for testin purposes
sab.getScreenManager().displayView(0); sab.getScreenManager().displayView(0);
if(sab.getRtcManager().hasLostPower()) if(sab.getRtcManager().hasLostPower())
{ {
@ -38,11 +42,15 @@ void setup()
sab.getRtcManager().setDateTime(DateTime(F(__DATE__), F(__TIME__))); sab.getRtcManager().setDateTime(DateTime(F(__DATE__), F(__TIME__)));
} }
sab.getWebServerManager().addApiRoutine("/helloServer", &(helloServerApi), NULL); sab.getWebServerManager().addApiRoutine("/sab/web/apitester", &(apiTesterApi), NULL);
sab.getWebServerManager().addApiRoutine("/view/next", &(nextViewApi), &sab, WEBServerManager::GET); sab.getWebServerManager().addApiRoutine("/sab/view/next", &(nextViewApi), &sab, WEBServerManager::GET);
sab.getWebServerManager().addApiRoutine("/rtc/get/time", &(rtcTimeApi), &sab, WEBServerManager::GET); sab.getWebServerManager().addApiRoutine("/sab/view", &(ViewByUIDApi), &sab, WEBServerManager::GET);
sab.getWebServerManager().addApiRoutine("/sdcard/eject", &(sdCardEjectApi), &sab, WEBServerManager::GET); sab.getWebServerManager().addApiRoutine("/sab/rtc/get/datetime", &(rtcGetTimeApi), &sab, WEBServerManager::GET);
sab.getWebServerManager().addApiRoutine("/sdcard/insert", &(sdCardInsertApi), &sab, WEBServerManager::GET); sab.getWebServerManager().addApiRoutine("/sab/rtc/set/datetime", &(rtcSetTimeApi), &sab, WEBServerManager::GET);
sab.getWebServerManager().addApiRoutine("/sab/sdcard/unmount", &(sdCardUnmountApi), &sab, WEBServerManager::GET);
sab.getWebServerManager().addApiRoutine("/sab/sdcard/mount", &(sdCardMountApi), &sab, WEBServerManager::GET);
sab.getWebServerManager().addApiRoutine("/esp/restart", &(espRestartApi), NULL, WEBServerManager::GET);
sab.getWebServerManager().addApiRoutine("/esp/reset", &(espResetApi), NULL, WEBServerManager::GET);
Serial.println("End setup"); Serial.println("End setup");
} }

View File

@ -67,6 +67,7 @@ typedef struct powerInfo
PowerType powerType; PowerType powerType;
unsigned char level; unsigned char level;
}PowerInfo; }PowerInfo;
char *addChar(char *pointer, const char character);
#endif //DEFINITION_H #endif //DEFINITION_H