-
Notifications
You must be signed in to change notification settings - Fork 58
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
Callback function data pointer is not a pointer to the ESP32Encoder instance #60
Comments
maybe better to do it in the constructor? |
In the case somebody does not know where. It must be added between these lines: ESP32Encoder/src/ESP32Encoder.cpp Lines 45 to 46 in 9979722
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello, thanks for this great library. Hard to find an encoder library that uses interrupts or the PCNT hardware peripheral, and also supports registering a callback function.
However, there's a bug here (or at least a documentation bug) regarding the pointer passed to the callback function.
Per the docs:
https://github.com/madhephaestus/ESP32Encoder/blob/master/src/ESP32Encoder.h#L32
"@param enc_isr_cb callback executed on every encoder ISR, gets a pointer to the ESP32Encoder instance as an argument"
Per the example:
https://github.com/madhephaestus/ESP32Encoder/blob/master/examples/Encoder_interrupt_display/Encoder_interrupt_display.ino#L8
The ISR here casts the
void* v
toESP32Encoder*
, but this is always a null pointer.The instantiation of the ESP32Encoder class here:
https://github.com/madhephaestus/ESP32Encoder/blob/master/examples/Encoder_interrupt_display/Encoder_interrupt_display.ino#L18
ESP32Encoder encoder(true, enc_cb);
does not provide a third parameter here, hence the null pointer passed into the callback function.
We could fix this by changing this line:
https://github.com/madhephaestus/ESP32Encoder/blob/master/src/ESP32Encoder.cpp#L88
esp32enc->_enc_isr_cb(esp32enc->_enc_isr_cb_data);
to something like
esp32enc->_enc_isr_cb(esp32enc->_enc_isr_cb_data ? esp32enc->_enc_isr_cb_data : esp32enc);
where it could return the constructor-set
_enc_isr_cb_data
if one was provided at construction time, otherwise it would return a pointer to the ESP32Encoder instance.I can prepare a PR for this if you like. Thanks!
The text was updated successfully, but these errors were encountered: