C++ programmi ESP-IDF

rebel88
Posts: 8
Joined: Mon Mar 23, 2020 8:37 am

C++ programmi ESP-IDF

Postby rebel88 » Mon Mar 23, 2020 9:02 am

Hi All,
I'm new in the world of ESP32.
I'm starting studing esp32 with Visual Studio Code ESP-IDF extension (463a9d8b).I can create basic project like blink, gpio, interrupts using C programming.
Now i want to study C++ programming with ESP-IDF, I have created a basic class with no problems, i have used

Code: Select all

extern "C" void app_main(){
and defined my class in a .cpp and .h files.
Next step was create a more complex Class used to drive Shift Registers as an exercise.
I was following my previous steps used for the basic class as above :
create a ShiftR.h file :

Code: Select all

#ifndef SHIFTR_H
#define SHIFTR_H

#include <stdio.h>
#include <stdlib.h>
#include <cstddef>
#include "driver/gpio.h"

#define LSBFIRST 0
#define MSBFIRST 1
#define HIGH 1
#define LOW 0

using namespace std;

class ShiftR
{
    private:
        gpio_num_t latchPin;
        gpio_num_t dataPin;
        gpio_num_t clockPin;
        uint8_t nChip;

        unsigned long long bitMask;

    public:
        void begin(gpio_num_t latch, gpio_num_t clk, gpio_num_t data, uint8_t n);
        void ShiftOut(uint32_t value, uint8_t bitOrder);
};

#endif
create a ShiftR.cpp file

Code: Select all

#include "ShiftR.h"


void ShiftR::begin(gpio_num_t latch, gpio_num_t clk, gpio_num_t data, uint8_t n)
{
    clockPin = clk;
    latchPin = latch;
    dataPin  = data;
    nChip =n;       //Numbers of chip in series

    bitMask = ((1ULL<<clockPin) |(1ULL<<latchPin) | (1ULL<<dataPin));

    gpio_config_t io_conf;
    //disable interrupt
    io_conf.intr_type = GPIO_PIN_INTR_DISABLE;
    //set as output mode
    io_conf.mode = GPIO_MODE_OUTPUT;
    //bit mask of the pins that you want to set,e.g.GPIO18/19
    io_conf.pin_bit_mask = bitMask;
    //disable pull-down mode
    io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
    //disable pull-up mode
    io_conf.pull_up_en = GPIO_PULLUP_DISABLE;
    //configure GPIO with the given settings
    gpio_config(&io_conf);

}

void ShiftR::ShiftOut(uint32_t value, uint8_t bitOrder)
{
    uint8_t value_8;

    for(int j=0; j< nChip; j++ ) //For each Byte
    {
        value_8 = (byte)(value>>(8*j)); //calulate the BYTE to be written
        for(int i=0; i<8 ;i++)  //For each bit
        {
            if (bitOrder == LSBFIRST)
                 gpio_set_level(dataPin, !!(value_8 & (1 << i)));
           else      
                 gpio_set_level(dataPin, !!(value_8 & (1 << (7 - i))));
                 
           gpio_set_level(clockPin, HIGH);
           gpio_set_level(clockPin, LOW); 
        }
    }
}
and my main.cpp file :

Code: Select all

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_spi_flash.h"

#include "ShiftR.h"

extern "C" void app_main(void)
{
    printf("Start!\n");

    ShiftR shiftA;
    shiftA.begin(18,19,21,1);
    
}
When i try to compile I receive an error, VSCode tells me "Something went wrong while trying to build the project" and in the console is written :

Code: Select all

...
-- Build files have been written to: C:/ESP_IDF/06_Shift_Register/build
[1/55] Building C object esp-idf/driver/CMakeFiles/__idf_driver.dir/sdspi_transaction.c.obj
[2/55] Building C object esp-idf/driver/CMakeFiles/__idf_driver.dir/sigmadelta.c.obj
[3/55] Building C object esp-idf/driver/CMakeFiles/__idf_driver.dir/sdmmc_transaction.c.obj
[4/55] Building C object esp-idf/driver/CMakeFiles/__idf_driver.dir/sdmmc_host.c.obj
[5/55] Building C object esp-idf/driver/CMakeFiles/__idf_driver.dir/spi_common.c.obj
[6/55] Building C object esp-idf/driver/CMakeFiles/__idf_driver.dir/gpio.c.obj
[7/55] Building C object esp-idf/driver/CMakeFiles/__idf_driver.dir/can.c.obj
[8/55] Building C object esp-idf/driver/CMakeFiles/__idf_driver.dir/sdspi_host.c.obj
[9/55] Building C object esp-idf/driver/CMakeFiles/__idf_driver.dir/ledc.c.obj
[10/55] Building C object esp-idf/driver/CMakeFiles/__idf_driver.dir/rmt.c.obj
[11/55] Building C object esp-idf/driver/CMakeFiles/__idf_driver.dir/pcnt.c.obj
[12/55] Building C object esp-idf/driver/CMakeFiles/__idf_driver.dir/mcpwm.c.obj
[13/55] Building C object esp-idf/driver/CMakeFiles/__idf_driver.dir/spi_slave.c.obj
[14/55] Building C object esp-idf/espcoredump/CMakeFiles/__idf_espcoredump.dir/src/core_dump_uart.c.obj
[15/55] Building C object esp-idf/driver/CMakeFiles/__idf_driver.dir/i2s.c.obj
[16/55] Building C object esp-idf/driver/CMakeFiles/__idf_driver.dir/spi_master.c.obj
[17/55] Building C object esp-idf/driver/CMakeFiles/__idf_driver.dir/i2c.c.obj
[18/55] Building C object esp-idf/esp32/CMakeFiles/__idf_esp32.dir/pm_trace.c.obj
[19/55] Building C object esp-idf/driver/CMakeFiles/__idf_driver.dir/sdio_slave.c.obj
[20/55] Performing build step for 'bootloader'
ninja: no work to do.
[21/53] Building C object esp-idf/esp32/CMakeFiles/__idf_esp32.dir/spiram_psram.c.obj
[22/53] Building C object esp-idf/esp32/CMakeFiles/__idf_esp32.dir/cpu_start.c.obj
[23/53] Building C object esp-idf/driver/CMakeFiles/__idf_driver.dir/uart.c.obj
[24/53] Building C object esp-idf/driver/CMakeFiles/__idf_driver.dir/rtc_module.c.obj
[25/53] Building C object esp-idf/esp32/CMakeFiles/__idf_esp32.dir/pm_esp32.c.obj
[26/53] Building C object esp-idf/spi_flash/CMakeFiles/__idf_spi_flash.dir/esp_flash_spi_init.c.obj
[27/53] Building C object esp-idf/esp32/CMakeFiles/__idf_esp32.dir/sleep_modes.c.obj
[28/53] Building C object esp-idf/esp_eth/CMakeFiles/__idf_esp_eth.dir/src/esp_eth_phy_dp83848.c.obj
[29/53] Building C object esp-idf/esp_eth/CMakeFiles/__idf_esp_eth.dir/src/esp_eth_phy_dm9051.c.obj
[30/53] Building C object esp-idf/esp_eth/CMakeFiles/__idf_esp_eth.dir/src/esp_eth_phy_lan8720.c.obj
[31/53] Building C object esp-idf/esp_eth/CMakeFiles/__idf_esp_eth.dir/src/esp_eth_phy_ip101.c.obj
[32/53] Building C object esp-idf/esp_adc_cal/CMakeFiles/__idf_esp_adc_cal.dir/esp_adc_cal.c.obj
[33/53] Building C object esp-idf/esp_eth/CMakeFiles/__idf_esp_eth.dir/src/esp_eth_phy_rtl8201.c.obj
[34/53] Building C object esp-idf/esp_eth/CMakeFiles/__idf_esp_eth.dir/src/esp_eth_mac_dm9051.c.obj
[35/53] Linking CXX static library esp-idf\esp32\libesp32.a
[36/53] Building C object esp-idf/esp_eth/CMakeFiles/__idf_esp_eth.dir/src/esp_eth_mac_esp32.c.obj
[37/53] Building C object esp-idf/fatfs/CMakeFiles/__idf_fatfs.dir/vfs/vfs_fat_sdmmc.c.obj
[38/53] Linking CXX static library esp-idf\espcoredump\libespcoredump.a
[39/53] Building C object esp-idf/fatfs/CMakeFiles/__idf_fatfs.dir/vfs/vfs_fat_spiflash.c.obj
[40/53] Building CXX object esp-idf/main/CMakeFiles/__idf_main.dir/ShiftR.cpp.obj
FAILED: esp-idf/main/CMakeFiles/__idf_main.dir/ShiftR.cpp.obj 
C:\Users\Admin\.espressif\tools\xtensa-esp32-elf\esp32-2019r1-8.2.0\xtensa-esp32-elf\bin\xtensa-esp32-elf-g++.exe  -DHAVE_CONFIG_H -DMBEDTLS_CONFIG_FILE=\"mbedtls/esp_config.h\" -DUNITY_INCLUDE_CONFIG_H -DWITH_POSIX -Iconfig -IC:/Users/Admin/esp-idf/components/newlib/platform_include -IC:/Users/Admin/esp-idf/components/freertos/include -IC:/Users/Admin/esp-idf/components/heap/include -IC:/Users/Admin/esp-idf/components/log/include -IC:/Users/Admin/esp-idf/components/soc/esp32/include -IC:/Users/Admin/esp-idf/components/soc/include -IC:/Users/Admin/esp-idf/components/esp_rom/include -IC:/Users/Admin/esp-idf/components/esp_common/include -IC:/Users/Admin/esp-idf/components/xtensa/include -IC:/Users/Admin/esp-idf/components/xtensa/esp32/include -IC:/Users/Admin/esp-idf/components/esp32/include -IC:/Users/Admin/esp-idf/components/driver/include -IC:/Users/Admin/esp-idf/components/esp_ringbuf/include -IC:/Users/Admin/esp-idf/components/esp_event/include -IC:/Users/Admin/esp-idf/components/tcpip_adapter/include -IC:/Users/Admin/esp-idf/components/lwip/include/apps -IC:/Users/Admin/esp-idf/components/lwip/include/apps/sntp -IC:/Users/Admin/esp-idf/components/lwip/lwip/src/include -IC:/Users/Admin/esp-idf/components/lwip/port/esp32/include -IC:/Users/Admin/esp-idf/components/lwip/port/esp32/include/arch -IC:/Users/Admin/esp-idf/components/vfs/include -IC:/Users/Admin/esp-idf/components/esp_wifi/include -IC:/Users/Admin/esp-idf/components/esp_wifi/esp32/include -IC:/Users/Admin/esp-idf/components/esp_eth/include -IC:/Users/Admin/esp-idf/components/efuse/include -IC:/Users/Admin/esp-idf/components/efuse/esp32/include -IC:/Users/Admin/esp-idf/components/app_trace/include -IC:/Users/Admin/esp-idf/components/mbedtls/port/include -IC:/Users/Admin/esp-idf/components/mbedtls/mbedtls/include -IC:/Users/Admin/esp-idf/components/wpa_supplicant/include -IC:/Users/Admin/esp-idf/components/wpa_supplicant/port/include -IC:/Users/Admin/esp-idf/components/wpa_supplicant/include/esp_supplicant -IC:/Users/Admin/esp-idf/components/bootloader_support/include -IC:/Users/Admin/esp-idf/components/app_update/include -IC:/Users/Admin/esp-idf/components/spi_flash/include -IC:/Users/Admin/esp-idf/components/nvs_flash/include -IC:/Users/Admin/esp-idf/components/pthread/include -IC:/Users/Admin/esp-idf/components/espcoredump/include -IC:/Users/Admin/esp-idf/components/asio/asio/asio/include -IC:/Users/Admin/esp-idf/components/asio/port/include -IC:/Users/Admin/esp-idf/components/coap/port/include -IC:/Users/Admin/esp-idf/components/coap/port/include/coap -IC:/Users/Admin/esp-idf/components/coap/libcoap/include -IC:/Users/Admin/esp-idf/components/coap/libcoap/include/coap2 -IC:/Users/Admin/esp-idf/components/console -IC:/Users/Admin/esp-idf/components/nghttp/port/include -IC:/Users/Admin/esp-idf/components/nghttp/nghttp2/lib/includes -IC:/Users/Admin/esp-idf/components/esp-tls -IC:/Users/Admin/esp-idf/components/esp_adc_cal/include -IC:/Users/Admin/esp-idf/components/esp_gdbstub/include -IC:/Users/Admin/esp-idf/components/tcp_transport/include -IC:/Users/Admin/esp-idf/components/esp_http_client/include -IC:/Users/Admin/esp-idf/components/esp_http_server/include -IC:/Users/Admin/esp-idf/components/esp_https_ota/include -IC:/Users/Admin/esp-idf/components/protobuf-c/protobuf-c -IC:/Users/Admin/esp-idf/components/protocomm/include/common -IC:/Users/Admin/esp-idf/components/protocomm/include/security -IC:/Users/Admin/esp-idf/components/protocomm/include/transports -IC:/Users/Admin/esp-idf/components/mdns/include -IC:/Users/Admin/esp-idf/components/esp_local_ctrl/include -IC:/Users/Admin/esp-idf/components/esp_websocket_client/include -IC:/Users/Admin/esp-idf/components/expat/expat/expat/lib -IC:/Users/Admin/esp-idf/components/expat/port/include -IC:/Users/Admin/esp-idf/components/wear_levelling/include -IC:/Users/Admin/esp-idf/components/sdmmc/include -IC:/Users/Admin/esp-idf/components/fatfs/diskio -IC:/Users/Admin/esp-idf/components/fatfs/vfs -IC:/Users/Admin/esp-idf/components/fatfs/src -IC:/Users/Admin/esp-idf/components/freemodbus/common/include -IC:/Users/Admin/esp-idf/components/idf_test/include -IC:/Users/Admin/esp-idf/components/jsmn/include -IC:/Users/Admin/esp-idf/components/json/cJSON -IC:/Users/Admin/esp-idf/components/libsodium/libsodium/src/libsodium/include -IC:/Users/Admin/esp-idf/components/libsodium/port_include -IC:/Users/Admin/esp-idf/components/mqtt/esp-mqtt/include -IC:/Users/Admin/esp-idf/components/openssl/include -IC:/Users/Admin/esp-idf/components/spiffs/include -IC:/Users/Admin/esp-idf/components/ulp/include -IC:/Users/Admin/esp-idf/components/unity/include -IC:/Users/Admin/esp-idf/components/unity/unity/src -IC:/Users/Admin/esp-idf/components/wifi_provisioning/include -mlongcalls -Wno-frame-address   -ffunction-sections -fdata-sections -fstrict-volatile-bitfields -nostdlib -Wall -Werror=all -Wno-error=unused-function -Wno-error=unused-but-set-variable -Wno-error=unused-variable -Wno-error=deprecated-declarations -Wextra -Wno-unused-parameter -Wno-sign-compare -ggdb -Og -std=gnu++11 -fno-exceptions -fno-rtti -D_GNU_SOURCE -DIDF_VER=\"v4.0-dirty\" -DGCC_NOT_5_2_0 -DESP_PLATFORM -MD -MT esp-idf/main/CMakeFiles/__idf_main.dir/ShiftR.cpp.obj -MF esp-idf\main\CMakeFiles\__idf_main.dir\ShiftR.cpp.obj.d -o esp-idf/main/CMakeFiles/__idf_main.dir/ShiftR.cpp.obj -c ../main/ShiftR.cpp
../main/ShiftR.cpp: In member function 'void ShiftR::begin(gpio_num_t, gpio_num_t, gpio_num_t, uint8_t)':
../main/ShiftR.cpp:15:25: error: cannot convert 'GPIO_INT_TYPE' to 'gpio_int_type_t' in assignment
     io_conf.intr_type = GPIO_PIN_INTR_DISABLE;
                         ^~~~~~~~~~~~~~~~~~~~~
../main/ShiftR.cpp: In member function 'void ShiftR::ShiftOut(uint32_t, uint8_t)':
../main/ShiftR.cpp:35:20: error: 'byte' was not declared in this scope
         value_8 = (byte)(value>>(8*j)); //calulate the BYTE to be written
                    ^~~~
../main/ShiftR.cpp:35:20: note: suggested alternative: 'system'
         value_8 = (byte)(value>>(8*j)); //calulate the BYTE to be written
                    ^~~~
                    system
[41/53] Linking CXX static library esp-idf\esp_eth\libesp_eth.a
[42/53] Building C object esp-idf/freemodbus/CMakeFiles/__idf_freemodbus.dir/port/portserial.c.obj
[43/53] Building CXX object esp-idf/main/CMakeFiles/__idf_main.dir/main.cpp.obj
FAILED: esp-idf/main/CMakeFiles/__idf_main.dir/main.cpp.obj 
C:\Users\Admin\.espressif\tools\xtensa-esp32-elf\esp32-2019r1-8.2.0\xtensa-esp32-elf\bin\xtensa-esp32-elf-g++.exe  -DHAVE_CONFIG_H -DMBEDTLS_CONFIG_FILE=\"mbedtls/esp_config.h\" -DUNITY_INCLUDE_CONFIG_H -DWITH_POSIX -Iconfig -IC:/Users/Admin/esp-idf/components/newlib/platform_include -IC:/Users/Admin/esp-idf/components/freertos/include -IC:/Users/Admin/esp-idf/components/heap/include -IC:/Users/Admin/esp-idf/components/log/include -IC:/Users/Admin/esp-idf/components/soc/esp32/include -IC:/Users/Admin/esp-idf/components/soc/include -IC:/Users/Admin/esp-idf/components/esp_rom/include -IC:/Users/Admin/esp-idf/components/esp_common/include -IC:/Users/Admin/esp-idf/components/xtensa/include -IC:/Users/Admin/esp-idf/components/xtensa/esp32/include -IC:/Users/Admin/esp-idf/components/esp32/include -IC:/Users/Admin/esp-idf/components/driver/include -IC:/Users/Admin/esp-idf/components/esp_ringbuf/include -IC:/Users/Admin/esp-idf/components/esp_event/include -IC:/Users/Admin/esp-idf/components/tcpip_adapter/include -IC:/Users/Admin/esp-idf/components/lwip/include/apps -IC:/Users/Admin/esp-idf/components/lwip/include/apps/sntp -IC:/Users/Admin/esp-idf/components/lwip/lwip/src/include -IC:/Users/Admin/esp-idf/components/lwip/port/esp32/include -IC:/Users/Admin/esp-idf/components/lwip/port/esp32/include/arch -IC:/Users/Admin/esp-idf/components/vfs/include -IC:/Users/Admin/esp-idf/components/esp_wifi/include -IC:/Users/Admin/esp-idf/components/esp_wifi/esp32/include -IC:/Users/Admin/esp-idf/components/esp_eth/include -IC:/Users/Admin/esp-idf/components/efuse/include -IC:/Users/Admin/esp-idf/components/efuse/esp32/include -IC:/Users/Admin/esp-idf/components/app_trace/include -IC:/Users/Admin/esp-idf/components/mbedtls/port/include -IC:/Users/Admin/esp-idf/components/mbedtls/mbedtls/include -IC:/Users/Admin/esp-idf/components/wpa_supplicant/include -IC:/Users/Admin/esp-idf/components/wpa_supplicant/port/include -IC:/Users/Admin/esp-idf/components/wpa_supplicant/include/esp_supplicant -IC:/Users/Admin/esp-idf/components/bootloader_support/include -IC:/Users/Admin/esp-idf/components/app_update/include -IC:/Users/Admin/esp-idf/components/spi_flash/include -IC:/Users/Admin/esp-idf/components/nvs_flash/include -IC:/Users/Admin/esp-idf/components/pthread/include -IC:/Users/Admin/esp-idf/components/espcoredump/include -IC:/Users/Admin/esp-idf/components/asio/asio/asio/include -IC:/Users/Admin/esp-idf/components/asio/port/include -IC:/Users/Admin/esp-idf/components/coap/port/include -IC:/Users/Admin/esp-idf/components/coap/port/include/coap -IC:/Users/Admin/esp-idf/components/coap/libcoap/include -IC:/Users/Admin/esp-idf/components/coap/libcoap/include/coap2 -IC:/Users/Admin/esp-idf/components/console -IC:/Users/Admin/esp-idf/components/nghttp/port/include -IC:/Users/Admin/esp-idf/components/nghttp/nghttp2/lib/includes -IC:/Users/Admin/esp-idf/components/esp-tls -IC:/Users/Admin/esp-idf/components/esp_adc_cal/include -IC:/Users/Admin/esp-idf/components/esp_gdbstub/include -IC:/Users/Admin/esp-idf/components/tcp_transport/include -IC:/Users/Admin/esp-idf/components/esp_http_client/include -IC:/Users/Admin/esp-idf/components/esp_http_server/include -IC:/Users/Admin/esp-idf/components/esp_https_ota/include -IC:/Users/Admin/esp-idf/components/protobuf-c/protobuf-c -IC:/Users/Admin/esp-idf/components/protocomm/include/common -IC:/Users/Admin/esp-idf/components/protocomm/include/security -IC:/Users/Admin/esp-idf/components/protocomm/include/transports -IC:/Users/Admin/esp-idf/components/mdns/include -IC:/Users/Admin/esp-idf/components/esp_local_ctrl/include -IC:/Users/Admin/esp-idf/components/esp_websocket_client/include -IC:/Users/Admin/esp-idf/components/expat/expat/expat/lib -IC:/Users/Admin/esp-idf/components/expat/port/include -IC:/Users/Admin/esp-idf/components/wear_levelling/include -IC:/Users/Admin/esp-idf/components/sdmmc/include -IC:/Users/Admin/esp-idf/components/fatfs/diskio -IC:/Users/Admin/esp-idf/components/fatfs/vfs -IC:/Users/Admin/esp-idf/components/fatfs/src -IC:/Users/Admin/esp-idf/components/freemodbus/common/include -IC:/Users/Admin/esp-idf/components/idf_test/include -IC:/Users/Admin/esp-idf/components/jsmn/include -IC:/Users/Admin/esp-idf/components/json/cJSON -IC:/Users/Admin/esp-idf/components/libsodium/libsodium/src/libsodium/include -IC:/Users/Admin/esp-idf/components/libsodium/port_include -IC:/Users/Admin/esp-idf/components/mqtt/esp-mqtt/include -IC:/Users/Admin/esp-idf/components/openssl/include -IC:/Users/Admin/esp-idf/components/spiffs/include -IC:/Users/Admin/esp-idf/components/ulp/include -IC:/Users/Admin/esp-idf/components/unity/include -IC:/Users/Admin/esp-idf/components/unity/unity/src -IC:/Users/Admin/esp-idf/components/wifi_provisioning/include -mlongcalls -Wno-frame-address   -ffunction-sections -fdata-sections -fstrict-volatile-bitfields -nostdlib -Wall -Werror=all -Wno-error=unused-function -Wno-error=unused-but-set-variable -Wno-error=unused-variable -Wno-error=deprecated-declarations -Wextra -Wno-unused-parameter -Wno-sign-compare -ggdb -Og -std=gnu++11 -fno-exceptions -fno-rtti -D_GNU_SOURCE -DIDF_VER=\"v4.0-dirty\" -DGCC_NOT_5_2_0 -DESP_PLATFORM -MD -MT esp-idf/main/CMakeFiles/__idf_main.dir/main.cpp.obj -MF esp-idf\main\CMakeFiles\__idf_main.dir\main.cpp.obj.d -o esp-idf/main/CMakeFiles/__idf_main.dir/main.cpp.obj -c ../main/main.cpp
../main/main.cpp: In function 'void app_main()':
../main/main.cpp:25:18: error: invalid conversion from 'int' to 'gpio_num_t' [-fpermissive]
     shiftA.begin(18,19,21,1);
                  ^~
In file included from ../main/main.cpp:17:
../main/ShiftR.h:41:31: note:   initializing argument 1 of 'void ShiftR::begin(gpio_num_t, gpio_num_t, gpio_num_t, uint8_t)'
         void begin(gpio_num_t latch, gpio_num_t clk, gpio_num_t data, uint8_t n);
                    ~~~~~~~~~~~^~~~~
../main/main.cpp:25:21: error: invalid conversion from 'int' to 'gpio_num_t' [-fpermissive]
     shiftA.begin(18,19,21,1);
                     ^~
In file included from ../main/main.cpp:17:
../main/ShiftR.h:41:49: note:   initializing argument 2 of 'void ShiftR::begin(gpio_num_t, gpio_num_t, gpio_num_t, uint8_t)'
         void begin(gpio_num_t latch, gpio_num_t clk, gpio_num_t data, uint8_t n);
                                      ~~~~~~~~~~~^~~
../main/main.cpp:25:24: error: invalid conversion from 'int' to 'gpio_num_t' [-fpermissive]
     shiftA.begin(18,19,21,1);
                        ^~
In file included from ../main/main.cpp:17:
../main/ShiftR.h:41:65: note:   initializing argument 3 of 'void ShiftR::begin(gpio_num_t, gpio_num_t, gpio_num_t, uint8_t)'
         void begin(gpio_num_t latch, gpio_num_t clk, gpio_num_t data, uint8_t n);
                                                      ~~~~~~~~~~~^~~~
ninja: build stopped: subcommand failed.
Does anyone have any suggestions or can help me understand what is appening?

ESP_Sprite
Posts: 3461
Joined: Thu Nov 26, 2015 4:08 am

Re: C++ programmi ESP-IDF

Postby ESP_Sprite » Mon Mar 23, 2020 5:35 pm

Well, it literally tels you what it doesn't like. You probably want to do an explicit cast wherever a function expects a gpio_t but you want to give it a number, e.g.

Code: Select all

shiftA.begin((gpio_num_t)18,(gpio_num_t)19,(gpio_num_t)21,1);

PeterR
Posts: 369
Joined: Mon Jun 04, 2018 2:47 pm

Re: C++ programmi ESP-IDF

Postby PeterR » Tue Mar 24, 2020 2:59 am

I am sure that ESP is right.
Few other things to keep in mind. ESP driver are 'C' (rightly so). One practical upshot is that structure initilaisation is different in C++ and C. Drivers are heavy on structures so you will have to marshell that interface. You either explicitly initialise structure which is bad (ESP do not fully hold to PIMPL and arguably for good reasons) or you create a C++ class which wraps the ESP library of choice and so use C ESP's initialiser (my fav approach). With an abstact base/factory you are target agnostic++
So you are now abstracted and into pure C++ joy.
Well, yes C++ is a lot more fun than 'C 'IMHO. And portable without a doubt, well C++11 and above.
Just go easy on strings and streams etc and you will have fun. Remember that the ESP is a micro and somewhat resource limited. Awesume price point but not a RPI, was never intended to be an RPI. So we are embedded and RAM is short. Dont be lazy on string use.
& I also believe that IDF CAN should be fixed.

warren
Posts: 15
Joined: Thu May 02, 2019 3:16 pm

Re: C++ programmi ESP-IDF

Postby warren » Tue Mar 24, 2020 7:23 pm

In

Code: Select all

shiftA.begin((gpio_num_t)18,(gpio_num_t)19,(gpio_num_t)21,1);
is not wrong, but in C++ better cast as:

Code: Select all

shiftA.begin(gpio_num_t(18),gpio_num_t(19),gpio_num_t(21),1);
The reason for this is that in C++, casting can be overridden for the type. I don't believe the overriding code is executed if you use the C language syntax, which basically says "do what I say (I know what I'm doing)".

Anywhere the Espressif examples initialize a structure (in C language), you'll have to initialize differently in C++. You'll want to use memset() to set the whole struct to zeros and then initialize the various .members of the struct as required. If you don't memset() to zero, you will see trouble. IIUC, C++20 will bring in compatible array initialization like C, making that less painful. Of course, you could always break out to straight C in places where it matters for now.

rebel88
Posts: 8
Joined: Mon Mar 23, 2020 8:37 am

Re: C++ programmi ESP-IDF

Postby rebel88 » Thu Mar 26, 2020 4:44 pm

Thank you all for your answers.
The main reason because my code didn't work is the wrong idea that I had: it is not possible to have implicit cast.
My question is : Why when I'm using main.c and C programming the automatic casting was working but when i tried to use C++ it stopped working? Is it only my wrong idea and it was not working either with C programming?

ESP_Sprite
Posts: 3461
Joined: Thu Nov 26, 2015 4:08 am

Re: C++ programmi ESP-IDF

Postby ESP_Sprite » Thu Mar 26, 2020 9:05 pm

Because C and C++ are not the same language, even if they are pretty similar. Things can be valid in C but not in C++. In this case, that line happened to be valid C but not valid C++.

PeterR
Posts: 369
Joined: Mon Jun 04, 2018 2:47 pm

Re: C++ programmi ESP-IDF

Postby PeterR » Fri Mar 27, 2020 1:59 am

ESP is right.
The main problems are probably structure intialisation.
Throw an

Code: Select all

#ifdef __cplusplus
extern "C" {
#endif
(Google it) around the sensitive bits.
StackOverflow is probably a better place to get coding support.
& I also believe that IDF CAN should be fixed.

Who is online

Users browsing this forum: Agree007, Google [Bot] and 67 guests