ロータリーエンコーダー (機械接点式インクリメンタル型) の信号をデコードするクラスです。 Rotary Encoder (incremental, mechanical switch type) signal decoder class.
Diff: RotaryEncoder.cpp
- Revision:
- 0:fcf360069f17
diff -r 000000000000 -r fcf360069f17 RotaryEncoder.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RotaryEncoder.cpp Sun Sep 07 23:31:47 2014 +0000 @@ -0,0 +1,26 @@ +#include <RotaryEncoder.h> + +RotaryEncoder::RotaryEncoder(PinName A, PinName B) + : terminalA(A, PullUp), terminalB(B, PullUp) +{ +} + +void RotaryEncoder::attach(void (*callback)(int)) { + callbackFunc = callback; + tick.attach_us(this, &RotaryEncoder::smpling, RotaryEncoder_SamplingInterval); +} + +void RotaryEncoder::detach() { + tick.detach(); +} + +void RotaryEncoder::smpling() { + static uint8_t val = 0xFF; + uint8_t now = (terminalA.read() << 1) | (terminalB.read()); + if (now == (val & 0x03)) return; + val = (val << 2) | now; + switch (val) { + case 0x4B: (*callbackFunc)(1); break; // cw : 0b01001011 = 0x4B + case 0x87: (*callbackFunc)(0); break; // ccw : 0b10000111 = 0x87 + } +}