microsoft / vscode-arduino

Visual Studio Code extension for Arduino
Other
1.15k stars 200 forks source link

Include custom libraries in the VSCode project #1725

Open wbadry opened 5 months ago

wbadry commented 5 months ago

Hello, I am using the Arduino extension for months now with no issues.

The new scenario I have is I need to include custom libraries (not from the Arduino library manager). My project structure, which usually works if I rely on the library manager,

C:.
│   esp32s3_usb_msc.ino
│
├───.vscode
│       arduino.json
│       c_cpp_properties.json
│
├───build
│   │   .last-used
│   │   build.options.json
│   │   build_opt.h
│   │   esp32s3_usb_msc.ino.bootloader.bin
│   │   esp32s3_usb_msc.ino.map
│   │   file_opts
│   │   includes.cache
│   │   libraries.cache
│   │   partitions.csv
│   │
│   ├───core
│   ├───libraries
│   └───sketch
│       │   esp32s3_usb_msc.ino.cpp
│       │   esp32s3_usb_msc.ino.cpp.d
│       │   esp32s3_usb_msc.ino.cpp.o
│       │
│       ├───.vscode
│       │       arduino.json
│       │       c_cpp_properties.json
│       │
│       └───include
│               SDCard.cpp
│               SDCard.h
│               SDCardArduino.cpp
│               SDCardArduino.h
│               SDCardIdf.cpp
│               SDCardIdf.h
│               SDCardLazyWrite.cpp
│               SDCardLazyWrite.h
│               SDCardMultiSector.cpp
│               SDCardMultiSector.h
│
└───include
        SDCard.cpp
        SDCard.h
        SDCardArduino.cpp
        SDCardArduino.h
        SDCardIdf.cpp
        SDCardIdf.h
        SDCardLazyWrite.cpp
        SDCardLazyWrite.h
        SDCardMultiSector.cpp
        SDCardMultiSector.h

The problem, as you may notice, is that I need to include custom libraries found in my include folder. The intellisense works and detects the members and methods. However, when compiling , I get the error

undefined reference to `SDCardMultiSector::SDCardMultiSector(Stream&, char const*, gpio_num_t, gpio_num_t, gpio_num_t, gpio_num_t, gpio_num_t, gpio_num_t)

The function is detected by the Intellisense and included the libraries as it I guess in a correct way.

#include "USB.h"
#include "USBMSC.h"

#include "include/SDCardArduino.h"
#include "include/SDCardMultiSector.h"
#include "include/SDCardLazyWrite.h"

#define BOOT_BUTTON 0

// #define SD_CARD_SPEED_TEST
#define SPEED_TEST_BUFFER_SIZE 4096
#define SPEED_TEST_NUMBER_SECTORS (SPEED_TEST_BUFFER_SIZE / 512)

USBMSC msc;
SDCard *card;

// Pin mapping using SDIO (ESP32-S3 only)
// Link :
// https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/sdmmc_host.html
int SD_CARD_CLK = 14;
int SD_CARD_CMD = 15;
int SD_CARD_DAT0 = 2;
int SD_CARD_DAT1 = 4;
int SD_CARD_DAT2 = 12;
int SD_CARD_DAT3 = 13;

// void log(const char *str)
// {
//   Serial.println(str);
// }

static int32_t onWrite(uint32_t lba, uint32_t offset, uint8_t *buffer, uint32_t bufsize)
{
  // Serial.printf("Writing %d bytes to %d at offset\n", bufsize, lba, offset);
  // this writes a complete sector so we should return sector size on success
  if (card->writeSectors(buffer, lba, bufsize / card->getSectorSize()))
  {
    return bufsize;
  }
  return bufsize;
  // return -1;
}

static int32_t onRead(uint32_t lba, uint32_t offset, void *buffer, uint32_t bufsize)
{
  // Serial.printf("Reading %d bytes from %d at offset %d\n", bufsize, lba, offset);
  // this reads a complete sector so we should return sector size on success
  if (card->readSectors((uint8_t *)buffer, lba, bufsize / card->getSectorSize()))
  {
    return bufsize;
  }
  return -1;
}

static bool onStartStop(uint8_t power_condition, bool start, bool load_eject)
{
  //Serial.printf("StartStop: %d %d %d\n", power_condition, start, load_eject);
  if (load_eject)
  {
#ifndef SD_CARD_SPEED_TEST
    msc.end();
#endif
  }
  return true;
}

bool isBootButtonClicked()
{
  return digitalRead(BOOT_BUTTON) == LOW;
}

void setup()
{
  // pinMode(GPIO_NUM_2, OUTPUT);
  card = new SDCardMultiSector(Serial, "/sd", GPIO_NUM_14, GPIO_NUM_15, GPIO_NUM_2, GPIO_NUM_4, GPIO_NUM_12, GPIO_NUM_13);
  msc.vendorID("ESP32");
  msc.productID("USB_MSC");
  msc.productRevision("1.0");
  msc.onRead(onRead);
  msc.onWrite(onWrite);
  msc.onStartStop(onStartStop);
  msc.mediaPresent(true);

  msc.begin(card->getSectorCount(), card->getSectorSize());
  Serial.begin(115200);
  USB.begin();
}

void loop()
{
  // put your main code here, to run repeatedly:
  delay(200);
  // if (isBootButtonClicked())
  // {
  //     if (MySD.cardType() == CARD_NONE)
  //     {
  //         log("No SD card");
  //     }
  // }
}

I tried to include the folder in c_cpp_properties.json but the file is generated, and therefore it is automatically deleted.

I believe this is a common issue and there is a standard solution for it.

merlinz01 commented 1 month ago

Did you try configuring the buildPreferences option in arduino.json? I can't say for sure, but you might be able to just add the filenames to one of the below.

    "buildPreferences": [
        ["build.extra_flags", "-DMY_DEFINE=666 -DANOTHER_DEFINE=3.14 -Wall"],
        ["compiler.cpp.extra_flags", "-DYET_ANOTHER=\"hello\""]
    ]