在VisualStudio下开发第一个PlatformIO项目并集成nanopb

在VisualStudio下开发第一个PlatformIO项目并集成nanopb

命令行创建PlatformIO项目

创建新的目录,firstpio,进入这个目录,执行下面的命令创建一个新的platformio项目,同时创建visualstudio的工程文件,这里我用到了两块板。

platformio init --ide visualstudio --board uno --board d1_mini

创建成功,命令行可以看到下面的输出:

在VisualStudio下开发第一个PlatformIO项目并集成nanopb_第1张图片

项目的目录结构如下:

在VisualStudio下开发第一个PlatformIO项目并集成nanopb_第2张图片

include目录,放项目用到的头文件

lib目录,放项目的私有库

src目录,放项目的源代码文件

platformio.ini,项目配置文件,编译,链接,运行相关的参数都在这个文件中

项目配置文件

在VisualStudio下开发第一个PlatformIO项目并集成nanopb_第3张图片

默认生成的配置文件,包含两个board,就是上面命令行传入的uno和d1mini,在配置文件中分别对应env:d1mini和env:uno两个节点。可以针对不同的board来设置不同的编译配置,依赖的库,运行的命令等。

我们为两个board设置不同的参数:

[common_env_data]
build_flags = 
    -D VERSION=1.2.3
    -D DEBUG=1

lib_deps_builtin = 
    SPI
    Wire

lib_deps_external = 
    ArduinoJson

上面是公共的编译选项,内置库依赖,外部库依赖的定义。修改d1_mini的配置:

[env:d1_mini]
platform = espressif8266
board = d1_mini
framework = arduino
build_flags = 
    ${common_env_data.build_flags}
    -DSSID_NAME=Hello
    -SSID_PASSWORD=World
    
lib_deps =
    ${common_env_data.lib_deps_builtin}
    ${common_env_data.lib_deps_external}
    PubSubClient
 
 monitor_speed = 115200
 upload_port = COM4

libdeps配置项目依赖的库,上面的${commonenvdata.libdepsbuiltin},就是用来引用上面定义的变量。monitorspeed,设置串口的波特率。upload_port设置上传的串口号。这里我设的是COM4,因为在Windows平台上,Linux上应该是/dev/ttyUSB*等等。

board uno的配置跟上面类似,不再赘述。

在Visual Studio中编译

在src目录下添加一个名为main.cpp的文件,打开platformio.vcproj项目文件,将main.cpp添加到项目中。写入测试代码:

#include 

void setup() {
    Serial.begin(115200);
    Serial.println("Hello PlatformIO");
}

void loop() {

}

在VS中选择编译,可以看到,它同时编译了d1_mini和uno两个平台。很方便。

命令行编译和上传

pio run -e d1_mini -t upload

上面的命令,编译d1_mini环境,编译完毕后直接上传。-e 指定编译环境。-t 指定编译后的操作,还可以是monitor,即打开串口监视,如下:

pio run -e d1_mini -t monitor

uno环境的编译同理,不再赘述。

测试集成nanopb

添加nanopb库

在项目的lib目录下新建一个目录nanopb,将下面的源文件拷到这里:

在VisualStudio下开发第一个PlatformIO项目并集成nanopb_第4张图片

lib下面的库,platformio编译的时候会自动添加头文件和源文件进行编译,不用手动操作,很方便。在项目根目录下面新建目录nanopb,将里面放入protobuf的proto文件,我写了个批处理,自动编译proto文件:

@echo off

protoc --nanopb_out=..\src simple.proto

生成的文件自动放到src目录,同样的不用手动操作,platformio会自动将这个目录下的源代码添加进编译过程。

调用nanopb

修改main.cpp:

#include 
#include 
#include 
#include "simple.pb.h"

void setup() {
    Serial.begin(115200);
    Serial.println("Hello PlatformIO");

    /* This is the buffer where we will store our message. */
    uint8_t buffer[128];
    size_t message_length;
    bool status;

    /* Encode our message */
    {
        /* Allocate space on the stack to store the message data.
         *
         * Nanopb generates simple struct definitions for all the messages.
         * - check out the contents of simple.pb.h!
         * It is a good idea to always initialize your structures
         * so that you do not have garbage data from RAM in there.
         */
        SimpleMessage message = SimpleMessage_init_zero;

        /* Create a stream that will write to our buffer. */
        pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));

        /* Fill in the lucky number */
        message.lucky_number = 13;

        /* Now we are ready to encode the message! */
        status = pb_encode(&stream, SimpleMessage_fields, &message);
        message_length = stream.bytes_written;

        /* Then just check for any errors.. */
        if (!status)
        {
#ifdef D1_MINI
            printf("Encoding failed: %s\n", PB_GET_ERROR(&stream));
#endif
        }
    }

    /* Now we could transmit the message over network, store it in a file or
     * wrap it to a pigeon's leg.
     */

     /* But because we are lazy, we will just decode it immediately. */

    {
        /* Allocate space for the decoded message. */
        SimpleMessage message = SimpleMessage_init_zero;

        /* Create a stream that reads from the buffer. */
        pb_istream_t stream = pb_istream_from_buffer(buffer, message_length);

        /* Now we are ready to decode the message. */
        status = pb_decode(&stream, SimpleMessage_fields, &message);

        /* Check for errors... */
        if (!status)
        {
#ifdef D1_MINI
            printf("Decoding failed: %s\n", PB_GET_ERROR(&stream));
#endif
        }

        /* Print the data contained in the message. */
#ifdef D1_MINI
        printf("Your lucky number was %d!\n", (int)message.lucky_number);
#endif
    }
}

void loop() {

}

编译上传

pio run -e d1_mini -t upload

在VisualStudio下开发第一个PlatformIO项目并集成nanopb_第5张图片

打工告成!

本文由博客一文多发平台 OpenWrite 发布!

你可能感兴趣的:(在VisualStudio下开发第一个PlatformIO项目并集成nanopb)