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
[solved]: freertos: c-example blink and cpp-example blink - invalid conversion error in cpp
[solved]: freertos: c-example blink and cpp-example blink - invalid conversion error in cpp
- Attachments
-
- blink.c.zip
- (959 Bytes) Downloaded 693 times
-
- cppblink.cpp.zip
- (975 Bytes) Downloaded 701 times
Last edited by DL88AI88 on Wed Apr 05, 2017 7:10 am, edited 2 times in total.
Re: freertos: c-example blink and cpp-example blink - invalid conversion error in cpp
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.
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
Re: freertos: c-example blink and cpp-example blink - invalid conversion error in cpp
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
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
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 889 times
-
- cppblink.cpp.zip
- (991 Bytes) Downloaded 1154 times
- 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
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.
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] and 60 guests