-
Notifications
You must be signed in to change notification settings - Fork 12
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
Use without global ACAN2517 objects #4
Comments
Hi Nick,
Thank you for your message!
I am not a C++ lambda specialist. ESP32 current build system uses gcc with -std=gnu++11 option, and it seems C++11 with the lambda expression partially supports closure.
So I did find a good solution for your problem.
I only find a workaround: using the global Parent instance. If you have declared :
Parent parent ;
You can write:
const uint32_t errorCode = driver.begin (settings, [] { parent.driver.isr () ; }) ;
I expect this can help.
Best regards,
Pierre
… Le 25 mai 2019 à 10:19, Nick Kirkby ***@***.***> a écrit :
Hi Pierre,
thanks for this great library! It is working out of the box on my ESP32 dev board with two MCP2517FD devices on a single SPI bus <https://github.com/nkirkby/acan2517/blob/two_devices/examples/LoopBackDemoESP32/LoopBackDemoESP32.ino#L46>.
I'm trying to organize ACAN2517 objects as struct members of a parent object instead of as globally scoped instances.
I'm failing to understand how to properly call ACAN2517.begin(...) in this scenario.
void Parent::init(uint32_t baud_rate)
{
ACAN2517Settings settings(ACAN2517Settings::OSC_40MHz, baudrate);
settings.mRequestedMode = ACAN2517Settings::Normal20B;
const uint32_t errorCode = driver->begin(settings, [] {driver->isr();});
if (errorCode)
Serial.printf("Configuration error 0x%x\n", errorCode);
else
Serial.println("ACAN2517 Configuration success");
}
This fails to compile with the error message:
the enclosing-function 'this' cannot be referenced in a lambda body unless it is in the capture list
As best I understand:
driver->begin() requires a callback function of type void (*)(void).
If I change the lambda function definition to include this:
const uint32_t errorCode = driver->begin(settings, [this] {driver->isr();});
Then the lambda function signature becomes void(*)(Parent*) which is incompatible with driver.begin().
How might one call begin() on ACAN2517 objects that are private to a class?
Thank you,
Nick
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub <#4?email_source=notifications&email_token=AEWKZVDIQTK54MBA22U5ICTPXDY7PA5CNFSM4HPUEH7KYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4GV2TXFA>, or mute the thread <https://github.com/notifications/unsubscribe-auth/AEWKZVERVKQ6EENQ4GDWYFTPXDY7PANCNFSM4HPUEH7A>.
|
Pierre, I think I've found a relevant Q&A on StackOverflow: FreeRTOS's static void led_callback(void *pvParameter)
{
StatusLED *led = (StatusLED*)pvParameter;
while (1)
{
switch (led->state)
{
case FAST_BLINK:
vTaskDelay(50 / portTICK_PERIOD_MS);
led->set_brightness(0);
vTaskDelay(50 / portTICK_PERIOD_MS);
led->set_brightness(1);
break;
case OFF:
default:
led->set_brightness(0);
vTaskDelay(500 / portTICK_PERIOD_MS);
break;
}
}
}
void StatusLED::setup()
{
xTaskCreate(led_callback, "LED blinker", 2048, this, 1, NULL);
} In the event that no such object is needed, a I looked at adding this to |
Hello,
You are right, ESP32 task creation has a parameter, I use this feature for the myESP32Task task. But the problem is on the interrupt service routine, that does not accept any argument.
Pierre
… Le 26 mai 2019 à 02:51, Nick Kirkby ***@***.***> a écrit :
Pierre,
Thank you for your response! This is a good solution for the time being.
I think I've found a relevant Q&A on StackOverflow:
https://stackoverflow.com/questions/27045304/callback-to-a-member-function <https://stackoverflow.com/questions/27045304/callback-to-a-member-function>
FreeRTOS's xTaskCreate handles this scenario by accepting a callback function of signature void (*)(void *).
This allows the optional use of an object within a static callback function. For example:
static void led_callback(void *pvParameter)
{
StatusLED *led = (StatusLED*)pvParameter;
while (1)
{
switch (led->state)
{
case FAST_BLINK:
vTaskDelay(50 / portTICK_PERIOD_MS);
led->set_brightness(0);
vTaskDelay(50 / portTICK_PERIOD_MS);
led->set_brightness(1);
break;
case OFF:
default:
led->set_brightness(0);
vTaskDelay(500 / portTICK_PERIOD_MS);
break;
}
}
}
void StatusLED::setup()
{
xTaskCreate(led_callback, "LED blinker", 2048, this, 1, NULL);
}
In the event that no such object is needed, a NULL pointer may be passed instead of this.
I looked at adding this to ACAN2517, but found that the Arduino attachInterrupt function only accepts callbacks of type void (*)(void).
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub <#4?email_source=notifications&email_token=AEWKZVCRBWI32V5PKRZIOHLPXHNJ5A5CNFSM4HPUEH7KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODWH3VPA#issuecomment-495958716>, or mute the thread <https://github.com/notifications/unsubscribe-auth/AEWKZVBVT6OTW6WMHWNJWVLPXHNJ5ANCNFSM4HPUEH7A>.
|
After reading StackOverflow: Passing capturing lambda as a function pointer, I made another attempt at this and failed.
|
Thank you for theses points.
Best regards,
Pierre
… Le 27 mai 2019 à 22:32, Nick Kirkby ***@***.***> a écrit :
After reading StackOverflow: Passing capturing lambda as a function pointer <https://stackoverflow.com/questions/28746744/passing-capturing-lambda-as-function-pointer>, I made another attempt at this and failed.
I learned a couple things along the way:
it's possible to change the compiler flags used by Arduino to compile with xtensa-esp32-elf-gcc in ~/ Arduino/hardware/espressif/esp32/platform.txt
ESP32 for Arduino currently uses std=gnu++11 (source <espressif/esp-idf#2449>)
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub <#4?email_source=notifications&email_token=AEWKZVFFK7IOX4QZ2XGQEMTPXRANDA5CNFSM4HPUEH7KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODWKPEBY#issuecomment-496300551>, or mute the thread <https://github.com/notifications/unsubscribe-auth/AEWKZVEDWZ37EBTLFOXOT4TPXRANDANCNFSM4HPUEH7A>.
|
It looks like the necessary function exists inside |
Hi Pierre,
thanks for this great library! It is working out of the box on my ESP32 dev board with two MCP2517FD devices on a single SPI bus.
I'm trying to organize ACAN2517 objects as struct members of a parent object instead of as globally scoped instances.
I'm failing to understand how to properly call
ACAN2517.begin(...)
in this scenario.This fails to compile with the error message:
the enclosing-function 'this' cannot be referenced in a lambda body unless it is in the capture list
As best I understand:
driver->begin()
requires a callback function of typevoid (*)(void)
.If I change the lambda function definition to include
this
:Then the lambda function signature becomes
void(*)(Parent*)
which is incompatible withdriver.begin()
.How might one call
begin()
on ACAN2517 objects that are private to a class?Thank you,
Nick
The text was updated successfully, but these errors were encountered: