简体   繁体   English

在PC平台上使用Arduino C ++库

[英]Using Arduino C++ libraries on PC platform

Arduino has some good C++ libraries which I would like to use on a PC platform. Arduino有一些很好的C ++库,我想在PC平台上使用。 Another advantage is that one can test and debug Arduino code on a PC. 另一个优点是可以在PC上测试和调试Arduino代码。 It is much easier to do debugging on a PC. 在PC上进行调试要容易得多。

One library I am particularly keen on is the String library. 我特别热衷的一个库是String库。 https://www.arduino.cc/en/Reference/String 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? 假设Arduino库是开源的,是否可以以某种方式将Arduino字符串库导入到Visual Studio这样的C ++ IDE中? How can this be done? 如何才能做到这一点?

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. 请注意,这些指令基于ESP32硬件。 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". 另请注意,这避免了执行手动/命令行任务,例如“ makefile”。 If you are coming from using the Arduino IDE environment you will probably appreciate this. 如果您来自使用Arduino IDE环境,则可能会感激不尽。

First of all, some details on the development environment: 首先,关于开发环境的一些细节:

  • Install MS Visual Studio Community Edition (free). 安装MS Visual Studio社区版(免费)。 I installed "Desktop development with C++ option" (Visual Studio link) 我安装了“带C ++选项的桌面开发” (Visual Studio链接)
  • Note that I am developing code for an ESP32 module. 请注意,我正在为ESP32模块开发代码。 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. 有很多不同的模块可供选择,它们的成本很低(在分线板上约为7美元),功能非常强大(与Arduino硬件相比),并且内置了蓝牙和WiFi。 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) 安装驱动程序(使用这些驱动程序会为ESP32自动分配一个COM端口) (Silabs链接)
  • 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. 按照此链接中的说明安装用于MSVS的VisualGDB (下载链接) :( VisualGDB教程)请注意,按照链接中的描述下载ESP32工具链需要很长时间(一个小时?),请耐心等待。
  • Note where the folder \\SysGCC\\esp32 is located on your computer (most likely in the root of your C drive). 注意文件夹\\ SysGCC \\ esp32在计算机上的位置(很可能在C驱动器的根目录中)。 This is the ESP32 toolchain that you installed on the previous step. 这是您在上一步中安装的ESP32工具链。
  • As part of the VisualGDB install step that is described in the VisualGDB tutorial, you have to change the "Path Mapping". 作为VisualGDB教程中描述的VisualGDB安装步骤的一部分,您必须更改“路径映射”。 I was prompted by the install to download CMake.exe from VisualGDB at this point. 此时,安装提示我从VisualGDB下载CMake.exe。 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) 在“绝对路径(以/开头)”字段中,您需要输入我上面描述的SysGCC的路径(我的路径是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. 此时,您应该能够将ESP32模块连接到计算机,并使用VisualGDB中的“新建嵌入式项目”向导来创建一个简单项目。 I generally choose "LEDBlink". 我通常选择“ 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). 请注意,我使用的大多数ESP32板在GPIO 2而非GPIO 5(向导中的默认值)上都具有内置LED。

So far, so good. 到现在为止还挺好。 Confirm that you can blink the LED. 确认您可以使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. 在这一点上,我们还没有做很多事情来帮助导入Arduino代码,所以这才是真正有趣的地方。

  • Note the location of your LEDBlink project folder on your hard drive. 请注意您的LEDBlink项目文件夹在硬盘驱动器上的位置。
  • Download the Arduino core for ESP32 from Espressif on Github . Github上的Espressif下载ESP32的Arduino核心。
  • 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. 将下载的Arduino Core中的所有* .h,*。c和* .cpp文件复制到Blink项目文件夹中,但不包括“ variants”文件夹中的文件。 I ended up copying 77 files. 我最终复制了77个文件。 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". “ variants”文件夹包含许多子文件夹,每个子文件夹包含一个名为“ 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. 您必须找到特定ESP32模块的文件夹,并将该特定“ pins_arduino.h”文件放入项目文件夹。
  • 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. 请注意,在项目的“ MSVS解决方案资源管理器”窗口中的“外部依赖关系”下,您将看到复制到项目文件夹中的所有* .h文件的列表。 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. 这不会立即发生,但是在MSVS应用程序的左下角,您会看到许多事情在后台进行,解析文件等,然后将显示“外部依赖关系”。

Now in app_main() add the following lines: 现在在app_main()中添加以下行:

    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. 如果您重建项目,则在链接过程中会遇到一些错误,因为对* .cpp文件的所有文件引用都尚未包含在项目中。

  • 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) 在解决方案资源管理器中,右键单击“源文件”和“添加现有项”,然后将所有* .c和* .cpp文件添加到您的项目中(而不是* .h文件)
  • Rebuild your project. 重建您的项目。 You will most likely get some errors of "(something).h: no such file or directory". 您很可能会收到“(something).h:无此类文件或目录”的错误。 That is because at the time of this writing, the Arduino core provided by Espressif is not complete. 这是因为在撰写本文时,Espressif提供的Arduino核心尚未完成。 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 . 例如,我遇到以下错误:“ vfs_api.h:没有这样的文件或目录”,并且错误位于文件SD.cpp中 So I removed SD.cpp from the project. 所以我从项目中删除了SD.cpp。 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. 我把它放在了眨眼任务功能的while(1)循环中。 This line of code will write to the serial port once every time the LED blinks. 每次LED闪烁时,此行代码将写入串行端口。 Since Serial.println is an Arduino function, you can be assured that at least this Arduino library is up and running. 由于Serial.println是Arduino函数,因此可以确保至少 Arduino库已启动并正在运行。 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). 我相信您应该能够在项目中添加更多的#include(例如Wire.h),并以相同的方式进行操作(但请稍候)。

  • Rebuild and upload to the ESP32. 重建并上传到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!" 打开串行端口监视器(靠近MSVS窗口顶部的按钮,在COM端口下拉菜单的最右边),并验证您是否在获取“ Hello World!”。 messages as the LED blinks. LED闪烁时显示消息。

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. 由于此解决方案依赖于ESP32硬件和VisualGDB,因此它不适用于所有人。 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. 但是,它使您能够摆脱Arduino IDE幕后发生的“谜”,并为更好的源代码控制和项目开发奠定基础。 For a related discussion, see this link on sysprogs.com . 有关相关讨论,请参见sysprogs.com上的此链接

The String library is mostly free of platform-specific dependencies, so you could simply add WString.h and WString.cpp to your source files. String库大多数没有特定于平台的依赖项,因此您只需将WString.h和WString.cpp添加到源文件中即可。 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; 许多其他库将很难移植到您的PC上。 anything that touches peripherals of the MCU are not going to port well. 碰到MCU外围设备的任何东西都不能很好地移植。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM