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

Callback function data pointer is not a pointer to the ESP32Encoder instance #60

Open
matthewbeckler opened this issue Apr 27, 2022 · 2 comments

Comments

@matthewbeckler
Copy link

matthewbeckler commented Apr 27, 2022

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 to ESP32Encoder*, 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!

@guidoschreuder
Copy link
Contributor

maybe better to do it in the constructor?
like: if (enc_isr_cb_data == nullptr) { _enc_isr_cb_data = this; }

@ztjona
Copy link
Contributor

ztjona commented Nov 9, 2023

maybe better to do it in the constructor? like: if (enc_isr_cb_data == nullptr) { _enc_isr_cb_data = this; }

In the case somebody does not know where. It must be added between these lines:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants