Using Arduino C++ libraries on PC platform

2019-03-03 17:53发布

问题:

Arduino has some good C++ libraries which I would like to use on a PC platform. Another advantage is that one can test and debug Arduino code on a PC. It is much easier to do debugging on a PC.

One library I am particularly keen on is the String library. https://www.arduino.cc/en/Reference/String

Is it possible to somehow import the Arduino String library into a C++ IDE like Visual Studio given that the Arduino library is open-source? How can this be done?

回答1:

The String library is mostly free of platform-specific dependencies, so you could simply add WString.h and WString.cpp to your source files. You will likely have to do a little bit of porting (cleaning up some macros, I suspect), but there's no reason it shouldn't eventually build.

Many other libraries are going to be considerably more difficult to port over to your PC; anything that touches peripherals of the MCU are not going to port well.



回答2:

I have been struggling with this issue on and off for the last three months, and I have spent a few hundred hours staring at my computer screen, trying stuff, documenting the results, trying other stuff (repeat). I have done a fair amount of digging on the internet and have found very little that constitutes a definitive resource on this topic. That being said, here is what I have found that WORKS. One caveat is that these instructions are based on ESP32 hardware. Note that this is still a work-in-process, as there is some clean-up left to do. Also note that this avoids having to do the manual/command line tasks such as "makefile". If you are coming from using the Arduino IDE environment you will probably appreciate this.

First of all, some details on the development environment:

  • Install MS Visual Studio Community Edition (free). I installed "Desktop development with C++ option" (Visual Studio link)
  • Note that I am developing code for an ESP32 module. There are lots of different modules to select from, they are low cost (~$7 on a breakout board), are very powerful (compared to Arduino hardware) and have Bluetooth and WiFi embedded. If you are using some other hardware then some of the remaining steps may not apply/will need to change.
  • Install drivers (these are used so the ESP32 gets assigned a COM port automatically) (Silabs link)
  • Install VisualGDB (download link) for MSVS as described in this link: (VisualGDB tutorial) Note that downloading the ESP32 toolchain as described in the link takes a long time (an hour?), be patient.
  • Note where the folder \SysGCC\esp32 is located on your computer (most likely in the root of your C drive). This is the ESP32 toolchain that you installed on the previous step.
  • As part of the VisualGDB install step that is described in the VisualGDB tutorial, you have to change the "Path Mapping". I was prompted by the install to download CMake.exe from VisualGDB at this point. In the field "Absolute Paths (starting with /)" you will need to enter the path to SysGCC that I described above (mine was C:\SysGCC\esp32)
  • At this point you should be able to connect an ESP32 module to your computer and use the "New Embedded Project" wizard in VisualGDB to create a simple project. I generally choose "LEDBlink". Note that most of the ESP32 boards I have used have the built-in LED on GPIO 2, not GPIO 5 (which is the default in the wizard).

So far, so good. Confirm that you can blink the LED. Clap your hands, shout with glee, grab a beer, or do whatever else you want to celebrate. At this point we have not really done much to help the importing of Arduino code, so here is where the fun REALLY begins.

  • Note the location of your LEDBlink project folder on your hard drive.
  • Download the Arduino core for ESP32 from Espressif on Github.
  • Copy all the *.h, *.c, and *.cpp files from your downloaded Arduino Core into your Blink project folder, EXCEPT the files that are in the "variants" folder. I ended up copying 77 files. In fact, what I did was first create a new folder called "bulk libraries" to copy all the source and header files into, in order to make it easier to perform this step for each project.
  • The "variants" folder contains a load of subfolders, each containing one file named "pins_arduino.h". You have to find the folder for your particular ESP32 module, and put that particular "pins_arduino.h" file in your project folder.
  • In your project source code, add the following line:

    #include "Arduino.h"
    

Build the project and you should get no errors. Note that in the MSVS Solution Explorer window for your project, under External Dependencies, you will see a list of all the *.h files that you copied into the project folder. This won't happen right away, but in the lower left corner of the MSVS app you will see a bunch of things going on in the background, parsing files etc., THEN the External Dependencies will appear.

Now in app_main() add the following lines:

    initArduino();
    Serial.begin(115200);

If you rebuild the project you will get a few errors during linking, because all of the file references to the *.cpp files are not (yet) included in your project.

  • In the Solution Explorer, right click "Source Files" and "Add Existing Item" and add all the *.c and *.cpp files to your project (not the *.h files)
  • Rebuild your project. You will most likely get some errors of "(something).h: no such file or directory". That is because at the time of this writing, the Arduino core provided by Espressif is not complete. In the Error list, note the file location of the error, and REMOVE this file from the project in the Solution Explorer. For example, I had the following error: "vfs_api.h: no such file or directory", and the error was located in the file SD.cpp. So I removed SD.cpp from the project. Keep iterating through this step one file at a time until you get a build with no errors. Take notes as you remove files (don't delete them) so you can add them back if you make a mistake. You may need to do some additional debugging here depending on what source files you added to your project. Be patient and expect a little trial-and-error as you review the Error List that is generated during the Rebuild. When you have a project that compiles without errors, celebrate with another beer (or two).

  • In your project source code, add the line:

    Serial.println("Hello World!");
    

I put this in the while(1) loop of the blink-task function. This line of code will write to the serial port once every time the LED blinks. Since Serial.println is an Arduino function, you can be assured that at least this Arduino library is up and running. I believe you should be able to add more #includes (such as Wire.h) to your project and proceed in the same way (but wait on that for now).

  • Rebuild and upload to the ESP32. Open the serial port monitor (button near the top of the MSVS window, to the immediate right of the COM port pull-down) and verify that you are getting the "Hello World!" messages as the LED blinks.

Now, before you give me any grief about "dumping" all the library, header, and source files into my project folder, I realize this is not best practice. If you created a "bulk libraries" folder as I suggested, you should be able to organize your project better. This is left a simple step for the reader.

Since this solution relies on ESP32 hardware and VisualGDB, it won't work for everyone. However it allows you to migrate away from the "mysteries" that take place behind-the-scenes in the Arduino IDE, and allows you to create a foundation for better source control and project development. For a related discussion, see this link on sysprogs.com.