From 362a561a35960ea8ae8a67f2ab2daa5af2f242df Mon Sep 17 00:00:00 2001 From: Chris Liebman Date: Sun, 4 Jun 2017 06:41:27 -0700 Subject: [PATCH] use malloc/free for WiFiManagerParameter* array instead of hard coding. add alternate constructor to specifiy max parameter count origional default constructor uses old hard codded value --- README.md | 5 +++++ WiFiManager.cpp | 27 +++++++++++++++++++++++++-- WiFiManager.h | 6 +++++- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a329f2b..3e83d4b 100644 --- a/README.md +++ b/README.md @@ -204,6 +204,11 @@ You should also take a look at adding custom HTML to your form. - Save and load custom parameters to file system in json form [AutoConnectWithFSParameters](https://github.com/tzapu/WiFiManager/tree/master/examples/AutoConnectWithFSParameters) - *Save and load custom parameters to EEPROM* (not done yet) +NOTE: if your need more than 10 (the default) custom parameters you will need to use the alternate constructor: +```cpp +WiFiManager wifiManager(15); // pass the number of params you will be adding +``` + #### Custom IP Configuration You can set a custom IP for both AP (access point, config mode) and STA (station mode, client mode, normal project state) diff --git a/WiFiManager.cpp b/WiFiManager.cpp index 4fba355..4be0105 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -60,14 +60,37 @@ const char* WiFiManagerParameter::getCustomHTML() { return _customHTML; } + WiFiManager::WiFiManager() { + init(WIFI_MANAGER_MAX_PARAMS); +} + +WiFiManager::WiFiManager(int max_params) { + init(max_params); +} + +void WiFiManager::init(int max_params) +{ + DEBUG_WM(F("setting max_params to:")); + DEBUG_WM(max_params); + _max_params = max_params; + _params = (WiFiManagerParameter**)malloc(_max_params * sizeof(WiFiManagerParameter*)); +} + +WiFiManager::~WiFiManager() +{ + if (_params != NULL) + { + DEBUG_WM(F("freeing allocated params!")); + free(_params); + } } void WiFiManager::addParameter(WiFiManagerParameter *p) { - if(_paramsCount + 1 > WIFI_MANAGER_MAX_PARAMS) + if(_paramsCount + 1 > _max_params) { //Max parameters exceeded! - DEBUG_WM("WIFI_MANAGER_MAX_PARAMS exceeded, increase number (in WiFiManager.h) before adding more parameters!"); + DEBUG_WM("max_params value exceeded, use WiFiManager(int max_params) constructor with a larger value!"); DEBUG_WM("Skipping parameter with ID:"); DEBUG_WM(p->getID()); return; diff --git a/WiFiManager.h b/WiFiManager.h index d86b904..2c8dece 100644 --- a/WiFiManager.h +++ b/WiFiManager.h @@ -65,6 +65,8 @@ class WiFiManager { public: WiFiManager(); + WiFiManager(int max_params); + ~WiFiManager(); boolean autoConnect(); boolean autoConnect(char const *apName, char const *apPassword = NULL); @@ -119,6 +121,7 @@ class WiFiManager //const String HTTP_HEAD = "{v}"; + void init(int max_params); void setupConfigPortal(); void startWPS(); @@ -176,7 +179,8 @@ class WiFiManager void (*_apcallback)(WiFiManager*) = NULL; void (*_savecallback)(void) = NULL; - WiFiManagerParameter* _params[WIFI_MANAGER_MAX_PARAMS]; + int _max_params; + WiFiManagerParameter** _params; template void DEBUG_WM(Generic text);