インクリメント方式エンコーダ用ライブラリ

Dependents:   RobotControl_Sample2022 sotsuken_mecha

初期化 IncEncoder name(pinA,pinB,分解能); 分解能 : x2_resolution x4_resolution

リセット name.reset();

Revision:
0:c40c29a063c0
diff -r 000000000000 -r c40c29a063c0 IncEncoder.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/IncEncoder.cpp	Fri Jul 01 06:56:51 2022 +0000
@@ -0,0 +1,42 @@
+#include "IncEncoder.h"
+#include "mbed.h"
+
+IncEncoder::IncEncoder(PinName ENC_A,PinName ENC_B,Encoding encoding) : ENC_A_(ENC_A),ENC_B_(ENC_B){
+    pulses = 0;
+    
+    ENC_A_.rise(this, &IncEncoder::A_RISE);
+    ENC_A_.fall(this, &IncEncoder::A_FALL);
+    
+    if(encoding == x4_Resolution){
+        ENC_B_.rise(this, &IncEncoder::B_RISE);
+        ENC_B_.fall(this, &IncEncoder::B_FALL);
+    }
+}
+
+int IncEncoder::GetIncPulses(){
+    return pulses;
+}
+
+void IncEncoder::reset(){
+    pulses = 0;
+}
+
+void IncEncoder::A_RISE(){
+    if(ENC_B_) pulses++;
+    else       pulses--;
+}
+
+void IncEncoder::A_FALL(){
+    if(ENC_B_) pulses--;
+    else       pulses++;
+}
+
+void IncEncoder::B_RISE(){
+    if(ENC_A_) pulses--;
+    else       pulses++;
+}
+
+void IncEncoder::B_FALL(){
+    if(ENC_A_) pulses++;
+    else       pulses--;
+}
\ No newline at end of file