-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Conversation
… 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
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 ? |
bump ? |
Sorry. I'll get an example added to the PR. I just haven't had time yet. |
You can just paste something if you want, I just do not have time to write one, or test this |
This is working for me. |
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:
} |
I will look at this tomorrow and possibly merge. |
Oh and I meant a simple example like a lambda etc to stick in a callback example. |
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:
} |
Awesome thanks |
I apologize got not getting to this. I’ve gotten dragged off onto other things over the past few months. But thanks to liebman for the good examples. I hope this will allow the PR to get integrated!
… On Jun 17, 2018, at 12:54 PM, Shawn A ***@***.***> wrote:
Awesome thanks
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub <#584 (comment)>, or mute the thread <https://github.com/notifications/unsubscribe-auth/AAkNOU5XcCjvpogQkmH8CXTBYGt9YbEZks5t9rP2gaJpZM4TLJQ9>.
-----
~Chris
[email protected]
|
Do we need to modify or add WiFiManager arg to all callbacks ? |
This should be fully backwards compatible. |
I mean 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? |
I've never used the WiFiManager* passed in to the AP callback so I'd say no. |
Sorry I was referring to this |
Ahh - this fixes that issue - can be closed. |
… 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