[GH-ISSUE #1113] Suggestion: Pass Pointers to Parameters #952

Open
opened 2026-02-28 01:27:50 +03:00 by kerem · 0 comments
Owner

Originally created by @Lithimlin on GitHub (Aug 20, 2020).
Original GitHub issue: https://github.com/tzapu/WiFiManager/issues/1113

I imagine that in many cases, it would be easiest to pass a pointer to a WiFiManagerParameter that points to the value that should be linked with the parameter. This could then automatically be updated whenever the parameters are saved.

I have coded something like this for personal use and would like to share it here.
I had to modify the WifiManager.h slightly by adding a virtual bool updateValue() to the Parameter class.

class IntParameter : public WiFiManagerParameter {
public:
  IntParameter(const char *id, const char *label, long value, const uint8_t length = 10, int *valueptr = nullptr)
    : WiFiManagerParameter("") {
    init(id, label, String(value).c_str(), length, "", WFM_LABEL_BEFORE);
    _vp = valueptr;
  }

  long getValue() {
    return String(WiFiManagerParameter::getValue()).toInt();
  }

  virtual bool updateValue() {
    if(_vp) {
      *_vp = getValue();
      return true;
    }
    return false;
  }

private:
  int *_vp;
};

class FloatParameter : public WiFiManagerParameter {
public:
  FloatParameter(const char *id, const char *label, float value, const uint8_t length = 10, float *valueptr = nullptr)
    : WiFiManagerParameter("") {
    init(id, label, String(value).c_str(), length, "", WFM_LABEL_BEFORE);
    _vp = valueptr;
  }

  float getValue() {
    return String(WiFiManagerParameter::getValue()).toFloat();
  }

  virtual bool updateValue() {
    if(_vp) {
      *_vp = getValue();
      return true;
    }
    return false;
  }

private:
  float *_vp;
};

Later, in my save callback, I call updateValue() on all parameters:

void saveParamCallback() {
  WiFiManagerParameter **params = wm.getParameters();
  for(size_t i = 0; i < wm.getParametersCount(); i++) {
    params[i]->updateValue();
  }
}

For the default WiFiManagerParameter, a char **valueptr would have to be used instead and the length would still have to be adjusted somehow, possibly through a second pointer to a size_t.

Originally created by @Lithimlin on GitHub (Aug 20, 2020). Original GitHub issue: https://github.com/tzapu/WiFiManager/issues/1113 I imagine that in many cases, it would be easiest to pass a pointer to a `WiFiManagerParameter` that points to the value that should be linked with the parameter. This could then automatically be updated whenever the parameters are saved. I have coded something like this for personal use and would like to share it here. I had to modify the `WifiManager.h` slightly by adding a `virtual bool updateValue()` to the Parameter class. ```cpp class IntParameter : public WiFiManagerParameter { public: IntParameter(const char *id, const char *label, long value, const uint8_t length = 10, int *valueptr = nullptr) : WiFiManagerParameter("") { init(id, label, String(value).c_str(), length, "", WFM_LABEL_BEFORE); _vp = valueptr; } long getValue() { return String(WiFiManagerParameter::getValue()).toInt(); } virtual bool updateValue() { if(_vp) { *_vp = getValue(); return true; } return false; } private: int *_vp; }; class FloatParameter : public WiFiManagerParameter { public: FloatParameter(const char *id, const char *label, float value, const uint8_t length = 10, float *valueptr = nullptr) : WiFiManagerParameter("") { init(id, label, String(value).c_str(), length, "", WFM_LABEL_BEFORE); _vp = valueptr; } float getValue() { return String(WiFiManagerParameter::getValue()).toFloat(); } virtual bool updateValue() { if(_vp) { *_vp = getValue(); return true; } return false; } private: float *_vp; }; ``` Later, in my save callback, I call `updateValue()` on all parameters: ```cpp void saveParamCallback() { WiFiManagerParameter **params = wm.getParameters(); for(size_t i = 0; i < wm.getParametersCount(); i++) { params[i]->updateValue(); } } ``` For the default `WiFiManagerParameter`, a `char **valueptr` would have to be used instead and the length would still have to be adjusted somehow, possibly through a second pointer to a `size_t`.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/WiFiManager#952
No description provided.