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

Use without global ACAN2517 objects #4

Open
driftregion opened this issue May 25, 2019 · 6 comments
Open

Use without global ACAN2517 objects #4

driftregion opened this issue May 25, 2019 · 6 comments

Comments

@driftregion
Copy link

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.

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

@pierremolinaro
Copy link
Owner

pierremolinaro commented May 25, 2019 via email

@driftregion
Copy link
Author

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

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).

@pierremolinaro
Copy link
Owner

pierremolinaro commented May 27, 2019 via email

@driftregion
Copy link
Author

After reading StackOverflow: Passing capturing lambda as a 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)

@pierremolinaro
Copy link
Owner

pierremolinaro commented May 29, 2019 via email

@driftregion
Copy link
Author

It looks like the necessary function exists inside arduino-esp32 FunctionalInterrupt.cpp, but it's not a public function.
I've posted a question on StackOverflow hoping to draw on the expertise there.

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

2 participants