Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed setAPCallback and setSaveConfigCallback to take std::function… #584

Merged
merged 1 commit into from
Jun 19, 2018

Conversation

cmarrin
Copy link
Contributor

@cmarrin cmarrin commented Apr 7, 2018

… params

This allows Predicates and Lambdas to be passed. Still allows bare function pointers
so this patch is fully backward compatible. Tested on WiFiManager examples for
backward compatibility and on my own project (OfficeClock) for handling lambdas with
captured variables

… params

This allows Predicates and Lambdas to be passed. Still allows bare function pointers
so this patch is fully backward compatible. Tested on WiFiManager examples for
backward compatibility and on my own project (OfficeClock) for handling lambdas with
captured variables
@tablatronix
Copy link
Collaborator

can you post an example so I can show this in docs, or maybe a callback example for travis in the future, or unit testing ?

@tablatronix
Copy link
Collaborator

bump ?

@tablatronix tablatronix added the enhancement Feature Request label Apr 11, 2018
@tablatronix tablatronix added this to the dev milestone Apr 11, 2018
@cmarrin
Copy link
Contributor Author

cmarrin commented Apr 11, 2018

Sorry. I'll get an example added to the PR. I just haven't had time yet.

@tablatronix
Copy link
Collaborator

You can just paste something if you want, I just do not have time to write one, or test this

@tablatronix tablatronix added In Progress QA Quality Assurance testing needed labels May 20, 2018
@liebman
Copy link
Contributor

liebman commented May 28, 2018

This is working for me.

@liebman
Copy link
Contributor

liebman commented Jun 17, 2018

Here is a contrived example using std::bind for both APCallback and SaveConfigCallbacks - can we get this merged soon? :-)

#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager

class WM_Handlers {
public:
    WM_Handlers(WiFiManager& wm) {
        //need placeholders for binding with args
        using namespace std::placeholders;
        //set callback that gets called when connecting to previous WiFi fails, and enters Access Point mode
        wm.setAPCallback(std::bind(&WM_Handlers::startingPortal, this, _1));
        //set a call back when config portal save is acalled
        wm.setSaveConfigCallback(std::bind(&WM_Handlers::saveConfig, this));
    }
private:
    void startingPortal(WiFiManager* myWiFiManager)
    {
        Serial.println("Entered config mode");
        Serial.println(WiFi.softAPIP());
        //if you used auto generated SSID, print it
        Serial.println(myWiFiManager->getConfigPortalSSID());
    }

    void saveConfig()
    {
        Serial.println("Save Called");
    }

};

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println("Creating WiFiManager");
  //WiFiManager
  //Local intialization. Once its business is done, there is no need to keep it around
  WiFiManager wifiManager;
  //reset settings - for testing
  //wifiManager.resetSettings();

  // create portal and save callbacks using std::function
  WM_Handlers wmHandlers(wifiManager);

  //fetches ssid and pass and tries to connect
  //if it does not connect it starts an access point with the specified name
  //here  "AutoConnectAP"
  //and goes into a blocking loop awaiting configuration
  if(!wifiManager.autoConnect()) {
    Serial.println("failed to connect and hit timeout");
    //reset and try again, or maybe put it to deep sleep
    ESP.restart();
    delay(1000);
  }

  //if you get here you have connected to the WiFi
  Serial.println("connected...yeey :)");
 
}

void loop() {
  // put your main code here, to run repeatedly:

}

@tablatronix
Copy link
Collaborator

I will look at this tomorrow and possibly merge.

@tablatronix
Copy link
Collaborator

tablatronix commented Jun 17, 2018

Oh and I meant a simple example like a lambda etc to stick in a callback example.
But this is good for docs

@liebman
Copy link
Contributor

liebman commented Jun 17, 2018

Here is a more useful lambda expression example:

#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);

  bool config_saved  = false;

  Serial.println("Creating WiFiManager");
  //WiFiManager
  //Local intialization. Once its business is done, there is no need to keep it around
  WiFiManager wifiManager;
  //reset settings - for testing
  //wifiManager.resetSettings();

  wifiManager.setAPCallback([](WiFiManager* myWiFiManager){
      Serial.println("Entered config mode");
      Serial.println(WiFi.softAPIP());
      //if you used auto generated SSID, print it
      Serial.println(myWiFiManager->getConfigPortalSSID());
  });

  wifiManager.setSaveConfigCallback([&config_saved](){
      config_saved = true;
  });

  //fetches ssid and pass and tries to connect
  //if it does not connect it starts an access point with the specified name
  //here  "AutoConnectAP"
  //and goes into a blocking loop awaiting configuration
  if(!wifiManager.autoConnect()) {
    Serial.println("failed to connect and hit timeout");
    //reset and try again, or maybe put it to deep sleep
    ESP.restart();
    delay(1000);
  }

  if (config_saved)
  {
      Serial.println("config WAS saved!");
  }

  //if you get here you have connected to the WiFi
  Serial.println("connected...yeey :)");
 
}

void loop() {
  // put your main code here, to run repeatedly:

}

@tablatronix
Copy link
Collaborator

Awesome thanks

@cmarrin
Copy link
Contributor Author

cmarrin commented Jun 17, 2018 via email

@tablatronix tablatronix merged commit bff3d75 into tzapu:development Jun 19, 2018
@tablatronix
Copy link
Collaborator

Do we need to modify or add WiFiManager arg to all callbacks ?

@liebman
Copy link
Contributor

liebman commented Jun 19, 2018

This should be fully backwards compatible.

@tablatronix
Copy link
Collaborator

tablatronix commented Jun 19, 2018

I mean
saveconfigcallback
There was some discussion about it elsewhere

    void          setAPCallback( std::function<void(WiFiManager*)> func );
    //called when settings have been changed and connection was successful
    void          setSaveConfigCallback( std::function<void()> func );

I would add it, but I am not so sure how to do multiples, templates?

@liebman
Copy link
Contributor

liebman commented Jun 19, 2018

I've never used the WiFiManager* passed in to the AP callback so I'd say no.
And unless you specify two callback signatures you would loose backwards compatibility, which may be ok.

@tablatronix
Copy link
Collaborator

Sorry I was referring to this
#466

@liebman
Copy link
Contributor

liebman commented Jun 19, 2018

Ahh - this fixes that issue - can be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Feature Request In Progress QA Quality Assurance testing needed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants