PlatformIO開發筆記2:點亮LED(面向對象封裝delay函數)

本文介紹在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後,根據提示進行操作即可。

 

 

 

 

 


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章