Arduino自作ライブラリの作り方
Arduino用のライブラリを自作するのに手こずったので覚書を
ライブラリには3つのファイルがある。
- ヘッダーファイル(.h)
- ソースファイル(.cpp)
- キーワードファイル(keyword.txt)
ヘッダーファイル
- ライブラリ名をきめる
- クラス名をきめる
- 関数名をきめる
- 変数が必要ならグローバル変数を
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef ライブラリ名_h | |
#define ライブラリ名_h | |
#include "Arduino.h" | |
class クラス名 | |
{ | |
public: | |
クラス名(int 変数1); | |
void 関数名1(); | |
void 関数名2(); | |
private: | |
int グローバル変数1; | |
}; | |
#endif |
注意:#endifの前に;があること。クラス名と同じpublicがいる。
ソースファイル
ライブラリ名、クラス名、関数名、変数名は統一されていなければならない。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Arduino.h" | |
#include "ライブラリ名.h" | |
クラス名::クラス名(int 変数1) | |
{ | |
グローバル変数1 = 変数1; | |
} | |
void クラス名::関数名1() | |
{ | |
digitalWrite(グローバル変数1,HIGH); | |
} | |
void クラス名::関数名2() | |
{ | |
//コードを入れる | |
} |
実際のプログラムの例
ArduinoIDEでは次のように呼び出す。
ライブラリ名、クラス名は統一。
クラス名に対して、任意の名前をあてて使用する
キーワードファイルは言葉に色をつけるためのもの。特になくてもよい。うまく使えなかったのでまた今度。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "ライブラリ名.h" | |
クラス名 任意の名(変数); | |
void setup() { | |
} | |
void loop() { | |
任意の名.関数1(); | |
任意の名.関数2(); | |
} |
Post a Comment