本文介绍在PlatformIO中,使用面向对象程序设计思想进行程序构建,并以封装delay函数进行阐述。本系列文章将系统阐述其开发环境的使用方法,并期待构建基于Arduino的C++嵌入式开发平台。
MCU:ATmega168PA
系统平台:Arduino
github:https://github.com/snmplink/StarrySky
一、开发步骤
1、在include目录下,建立target.h文件,源代码如下:
#ifndef TARGET_H_
#define TARGET_H_
#include <Arduino.h>
#ifdef __cplusplus
extern "C"{
class CTarget
{
public:
void Delayms(uint16_t u16_ms);
};
extern CTarget Target;
}
#endif
#endif
2、在src目录下,建立InternalPeripheralLayer(内部外设层),并建立target.cpp文件,源代码如下:
#include "target.h"
void CTarget::Delayms(uint16_t u16_ms)
{
delay(u16_ms);
}
3、上述步骤完成后,就可以在main.cpp中使用Target类了,源代码如下:
#include "target.h"
CTarget Target;
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
}
void loop()
{
digitalWrite(LED_BUILTIN, HIGH);
Target.Delayms(1000);
digitalWrite(LED_BUILTIN, LOW);
Target.Delayms(1000);
}
4、后续步骤可参看开发笔记1,程序下载到目标板后,可观察到LED以1秒为间隔闪烁。
二、注释
1、本文看似简单,但已使用面向对象程序设计思想,进行项目设计。
2、采用面向对象程序设计后,不需在强制记忆函数名,只需键入target后,根据提示进行操作即可。
扫描二维码关注公众号,回复:
8680692 查看本文章