Page 1 of 1

[solved]: freertos: c-example blink and cpp-example blink - invalid conversion error in cpp

Posted: Tue Apr 04, 2017 10:48 am
by DL88AI88
Hi there,

out of curiosity I just wanted to compare the usage of freertos within a
blink example between 'c' and 'cpp'.
So here are the examples.
The c example works fine.
The cpp example gives me some errors like this
at
gpio_set_direction(BLINK_GPIO_1, GPIO_MODE_OUTPUT);
error: invalid conversion from 'int' to 'gpio_num_t' [-fpermissive]
...
include/driver/gpio.h:302:11: note: initializing argument 1 of 'esp_err_t gpio_set_direction(gpio_num_t, gpio_mode_t)'

also at
gpio_set_level(BLINK_GPIO_1, 0);
..
gpio_set_level(BLINK_GPIO_1, 1);
..
and so on.

Any tips on solving this are very welcome.

best regards

DL88AI88

Re: freertos: c-example blink and cpp-example blink - invalid conversion error in cpp

Posted: Tue Apr 04, 2017 7:54 pm
by kolban
Thankfully the solution is quite simple.

In the code, BLINK_GPIO_1 is defined as a macro for the integer constant 16. So when the code is compiled we have:

gpio_set_direction(BLINK_GPIO_1, GPIO_MODE_OUTPUT);

which translates to

gpio_set_direction(16, GPIO_MODE_OUTPUT);

however, the signature of gpio_set_direction takes two parameters:

gpio_set_direction(gpio_num_t, gpio_mode_t)

and the int value of 16 is not a gpio_num_t .... so the solution would be:

gpio_set_direction((gpio_num_t)BLINK_GPIO_1, GPIO_MODE_OUTPUT);

However the CORRECT solution is not to define:

#define BLINK_GPIO_1 16

but instead to define:

#define BLINK_GPIO_1 GPIO_NUM_16

The definitions of the "GPIO_NUM_xx" are already defined and of the correct type.

Re: freertos: c-example blink and cpp-example blink - invalid conversion error in cpp

Posted: Tue Apr 04, 2017 10:53 pm
by ESP_Angus
To add a minor note to kolban's 100% correct answer, this is one of the subtle ways in which C++ is not "C plus classes" - the C++ type system enforces things like automatic conversion between 'int' and 'enum'. Which, overall, makes it easier for the compiler to check that you're always getting the types that you think you're getting. But it does make some code like this a little more fiddly.

[solved]: c-example blink and cpp-example blink - invalid conversion error in cpp

Posted: Wed Apr 05, 2017 7:08 am
by DL88AI88
Hi guys,

thanks for clearing this cpp thing up and showing the right solution.

Please, find attached two working freertos blink examples
written in cpp and c.

If you find them useful, feel free to use them.


Best regards

DL88AI88

Re: [solved]: freertos: c-example blink and cpp-example blink - invalid conversion error in cpp

Posted: Wed Apr 05, 2017 8:53 am
by ESP_krzychb
Hi @DL88AI88,

I think this is a nice example to help people get started with ESP32 and C++.

Please consider creating a pull request in esp-idf to add cppblink.cpp to examples/get-started.