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

DL88AI88
Posts: 29
Joined: Mon Aug 29, 2016 8:26 am

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

Postby DL88AI88 » Tue Apr 04, 2017 10:48 am

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
Attachments
blink.c.zip
(959 Bytes) Downloaded 694 times
cppblink.cpp.zip
(975 Bytes) Downloaded 703 times
Last edited by DL88AI88 on Wed Apr 05, 2017 7:10 am, edited 2 times in total.

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

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

Postby kolban » Tue Apr 04, 2017 7:54 pm

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.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

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

Postby ESP_Angus » Tue Apr 04, 2017 10:53 pm

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.

DL88AI88
Posts: 29
Joined: Mon Aug 29, 2016 8:26 am

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

Postby DL88AI88 » Wed Apr 05, 2017 7:08 am

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
Attachments
blink.c.zip
(959 Bytes) Downloaded 890 times
cppblink.cpp.zip
(991 Bytes) Downloaded 1155 times

User avatar
ESP_krzychb
Posts: 400
Joined: Sat Oct 01, 2016 9:05 am
Contact:

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

Postby ESP_krzychb » Wed Apr 05, 2017 8:53 am

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.

Who is online

Users browsing this forum: Bing [Bot], MicroController and 170 guests