# 4.PIO工程结构&构建方式 你好,我是爱吃鱼香ROS的小鱼。上一节我们搭建好了PIO的开发环境,并新建了第一个工程。本节我们详细了解下该工程,了解我们需要在哪里写代码,在哪里改配置。 ## 一、工程目录概述 ![image-20221218001241164](4.PIO%E5%B7%A5%E7%A8%8B%E7%BB%93%E6%9E%84&%E6%9E%84%E5%BB%BA%E6%96%B9%E5%BC%8F/imgs/image-20221218001241164.png) 首先展开工程,可以看到工程一共有8个部分如上图所示。 1. PIO配置文件 2. VsCode配置文件 3. **头文件放置目录** 4. **库文件放置目录** 5. **代码资源放置目录,主函数就在这里** 6. 测试文件放置目录 7. git忽略文件 8. **platformio配置文件** ## 二、在哪里写代码? 打开`src/main.cpp`就是我们工程的程序入口文件,打开该文件,已经默认给我们生成了9行代码,后续的主要开发就在这里进行。 ```c++ #include void setup() { // put your setup code here, to run once: } void loop() { // put your main code here, to run repeatedly: } ``` ## 三、工程配置文件-platformio.ini 打开工程主目录下的`platformio.ini`文件,预生成的配置文件如下 ```ini ; PlatformIO Project Configuration File ; ; Build options: build flags, source filter ; Upload options: custom upload port, speed and extra flags ; Library options: dependencies, extra library storages ; Advanced options: extra scripting ; ; Please visit documentation for the other options and examples ; https://docs.platformio.org/page/projectconf.html [env:featheresp32] platform = espressif32 board = featheresp32 framework = arduino ``` 这里用到的主要配置有四个 1. `[env:featheresp32]`编译环境 2. `platform = espressif32`,单片机平台 3. `board = featheresp32`,开发板 4. `framework = arduino`,开发框架-arduino 后续还有很多关于工程的配置都放在这里,同时我们可以添加一条配置`board_build.f_cpu = 240000000L`,将单片机的主频提高到`240MHZ`的主频。 ```ini [env:featheresp32] platform = espressif32 board = featheresp32 framework = arduino board_build.f_cpu = 240000000L ``` ## 四、编译工程 在VsCode中编译PIO,编译工程和将编译结果下载到开发板上都非常的方便。 编译工程可以手动点击左下角的对号进行,其他操作也可以通过按钮进行。 ![image-20221218013002983](4.PIO%E5%B7%A5%E7%A8%8B%E7%BB%93%E6%9E%84&%E6%9E%84%E5%BB%BA%E6%96%B9%E5%BC%8F/imgs/image-20221218013002983.png) 点击编译按钮,看到如下界面则代表编译成功 ![image-20221218013137654](4.PIO%E5%B7%A5%E7%A8%8B%E7%BB%93%E6%9E%84&%E6%9E%84%E5%BB%BA%E6%96%B9%E5%BC%8F/imgs/image-20221218013137654.png) 其中打印信息有很多有用的提示,比如工程占用的RAM和Flash大小(可以理解为系统程序大小) ``` RAM: [ ] 4.9% (used 16144 bytes from 327680 bytes) Flash: [== ] 16.2% (used 212961 bytes from 1310720 bytes) ``` 编译完成工程,在`.pio/build/featheresp32`目录下可以看到`firmware.bin`,这个就是我们工程编译之后生成的二进制文件,将该文件下载到开发板上就可以运行了。 ## 五、PIO快捷键 这里再介绍几个PIO的快捷键,在接下来的学习中你肯定能用到 | 快捷键 | 内容 | | ---------- | ---------------------- | | Ctrl+Alt+B | 编译工程 | | Ctrl+Alt+U | 将程序上传烧录到开发板 | | Ctrl+Alt+S | 打开串口Monitor | ## 六、总结 本节我们简单的了解下PIO工程的结构以及配置文件,下一节我们开始学习如何输出`Hello World!`