-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Make Serial.onReceive() accept std::function #6302
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
Comments
What about this C++ approach? class Receiver {
public:
static void gotUARTdata(void);
};
void Receiver::gotUARTdata(void) {
Serial.println("Got Data on UART...");
}
void setup() {
Serial.begin(115200);
Serial.onReceive(Receiver::gotUARTdata);
}
void loop() {
// Nothing here as this time.
} |
I also think that Arduino doesn't support Standard C++ Could you provide a simple example with |
Try this approach: class Receiver {
private:
int a;
public:
static void gotUARTdata(void);
void set(int i);
int get();
};
Receiver r;
void Receiver::gotUARTdata(void) {
Serial.println("Got Data on UART...");
Serial.print("Private Receiver::a = ");
Serial.println(r.get());
r.set(r.get() + 1);
}
void Receiver::set(int i) {
a = i;
}
int Receiver::get(void) {
return a;
}
void setup() {
r.set(10); // starting point
Serial.begin(115200);
Serial.onReceive(Receiver::gotUARTdata);
}
void loop() {
// Nothing here as this time.
} Output --> just hit Enter on Serial Monitor
|
@SuGlider Arduino-ESP32 does in fact support C++11 (or later) which supports std::function. I use it quite extensively in the OpenMRNLite library (latest published version only compiles on 1.0.6, 2.0.x support is complete but not published yet). ArduinoOTA library (in this repo) also uses std::function today. |
Nice @atanisoft ! |
Yes, I see the trick you did there. By declaring as static inside the class the function behaves as a global function for all the objects of that class. I see no real difference between using your method and a global function with global objects (with methods to access class variables). The advantage of functional is that you can bind a non-static class method and be able to access class variables directly without the need of having global objects or specific getters or setters. Functional works in Arduino and I use it in multiple libraries inside my project. Other examples of libraries already using functional: |
I agree that a static function in a class is just a global function for all objects. Using sdt::function has a big problem... make it work with esp32-hal.uart.c HardwareSerial.cpp is just a CPP adaptation layer to esp32-hal-uart.c As far as I know, it's not possible to convert std:function to a C function pointer. |
I think that the way to make onReceive(std::function) work will be by moving all the processing of events to the HardwareSerial.cpp part of code... But it will take some time to be done. I think it is a nice improvenment, but it's low priority at this time. |
Thanks @SuGlider If I have some time I will try to propose a solution to this. Thanks |
@SuGlider : A not complex solution is (and largely used) is to add a parameter to the onReceive function. The application looks like this :
I can make a PR for you if like the idea. |
Related area
UART
Hardware specification
ESP32 DevKit
Is your feature request related to a problem?
Hello. It would be useful if you could make onReceive() on HardwareSerial accept std:function so I can point it to a class method with std:bind. You would need to include .
This request could be extended to other callbacks in the framework (like Wifi.onEvent() ).
If you don't want to include by default, could you at least provide us with a MACRO that enables or disables this?
Thanks
Describe the solution you'd like
Make callbacks use functional
Describe alternatives you've considered
Global function. But if I'm programming in pure C++ and I need to access privates variables of a class from the callback, it gets weird. I would prefer to be able to call a class method directly to avoid exposing data outside the class.
Additional context
No response
I have checked existing list of Feature requests and the Contribution Guide
The text was updated successfully, but these errors were encountered: