押しボタンスイッチのチャタリングを抑制するためのクラス.登録した際のプログラム名:Demo_PushButton. This class suppresses chattering of push-button switch.
Dependents: Demo_PushuButton Demo_SwiManager
Revision 0:514eecdab861, committed 2020-04-21
- Comitter:
- MikamiUitOpen
- Date:
- Tue Apr 21 01:48:38 2020 +0000
- Commit message:
- 1
Changed in this revision
PushButton.cpp | Show annotated file Show diff for this revision Revisions of this file |
PushButton.hpp | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PushButton.cpp Tue Apr 21 01:48:38 2020 +0000 @@ -0,0 +1,35 @@ +//---------------------------------------------------------------- +// InterruptIn, Timeout を利用してチャタリングを防止するクラス +// 割込みサービス・ルーチンは非 static 関数にしている +// +// 2019/03/22, Copyright (c) 2019 MIKAMI, Naoki +//---------------------------------------------------------------- + +#include "PushButton.hpp" + +using namespace Mikami; + +// コンストラクタ +PushButton::PushButton(PinName pin, PinMode mode, RiseFall rf, + void (*Func)(), float time) + : pbSw_(InterruptIn(pin, mode)), fp_(Func), time_(time) +{ + if (rf == RISE) + pbSw_.rise(callback(this, &PushButton::IsrIntrIn)); + else + pbSw_.fall(callback(this, &PushButton::IsrIntrIn)); +} + +// InterruptIn の割込みサービス・ルーチン +void PushButton::IsrIntrIn() +{ + pbSw_.disable_irq(); + enabler_.attach(callback(this, &PushButton::IsrTimeout), time_); + fp_(); // コンストラクタの引数で与えられた関数が実行される +} + +// Timeout の割込みサービス・ルーチン +void PushButton::IsrTimeout() +{ + pbSw_.enable_irq(); +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PushButton.hpp Tue Apr 21 01:48:38 2020 +0000 @@ -0,0 +1,44 @@ +//------------------------------------------------------------------------ +// InterruptIn, Timeout を利用してチャタリングを防止するクラス,ヘッダ +// 割込みサービス・ルーチンは非 static 関数にしている +// +// 2020/04/21, Copyright (c) 2020 MIKAMI, Naoki +//------------------------------------------------------------------------ + +#include "mbed.h" + +#ifndef PUSHBUTTON_CLASS_HPP +#define PUSHBUTTON_CLASS_HPP + +namespace Mikami +{ + class PushButton + { + public: + enum RiseFall { RISE, FALL }; + + // コンストラクタ + PushButton(PinName pin, // 入力のピン + PinMode mode, // "PullUp", "PullDown" などを指定 + RiseFall rf, // 割込み発生が立ち上りか立ち下りかを指定 + void (*Func)(), // 割込みサービス・ルーチン + float time = 0.1); // 入力を禁止する時間 + + private: + InterruptIn pbSw_; // Mbed オフィシャル・ライブラリのクラスのオブジェクト + Timeout enabler_; // Mbed オフィシャル・ライブラリのクラスのオブジェクト + void (*fp_)(); // コンストラクタの引数で与えられた関数に対応するポインタ + float time_; // InterruptIn 割込みを無効にしている時間 + + // InterruptIn の割込みサービス・ルーチン + void IsrIntrIn(); + + // Timeout の割込みサービス・ルーチン + void IsrTimeout(); + + // コピー・コンストラクタおよび代入演算子の禁止のため + PushButton(const PushButton& ); + PushButton& operator=(const PushButton& ); + }; +} +#endif // PUSHBUTTON_CLASS_HPP \ No newline at end of file