ロータリーエンコーダー (機械接点式インクリメンタル型) の信号をデコードするクラスです。 Rotary Encoder (incremental, mechanical switch type) signal decoder class.
RotaryEncoder.cpp@0:fcf360069f17, 2014-09-07 (annotated)
- Committer:
- tetsuya256
- Date:
- Sun Sep 07 23:31:47 2014 +0000
- Revision:
- 0:fcf360069f17
First commit.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tetsuya256 | 0:fcf360069f17 | 1 | #include <RotaryEncoder.h> |
tetsuya256 | 0:fcf360069f17 | 2 | |
tetsuya256 | 0:fcf360069f17 | 3 | RotaryEncoder::RotaryEncoder(PinName A, PinName B) |
tetsuya256 | 0:fcf360069f17 | 4 | : terminalA(A, PullUp), terminalB(B, PullUp) |
tetsuya256 | 0:fcf360069f17 | 5 | { |
tetsuya256 | 0:fcf360069f17 | 6 | } |
tetsuya256 | 0:fcf360069f17 | 7 | |
tetsuya256 | 0:fcf360069f17 | 8 | void RotaryEncoder::attach(void (*callback)(int)) { |
tetsuya256 | 0:fcf360069f17 | 9 | callbackFunc = callback; |
tetsuya256 | 0:fcf360069f17 | 10 | tick.attach_us(this, &RotaryEncoder::smpling, RotaryEncoder_SamplingInterval); |
tetsuya256 | 0:fcf360069f17 | 11 | } |
tetsuya256 | 0:fcf360069f17 | 12 | |
tetsuya256 | 0:fcf360069f17 | 13 | void RotaryEncoder::detach() { |
tetsuya256 | 0:fcf360069f17 | 14 | tick.detach(); |
tetsuya256 | 0:fcf360069f17 | 15 | } |
tetsuya256 | 0:fcf360069f17 | 16 | |
tetsuya256 | 0:fcf360069f17 | 17 | void RotaryEncoder::smpling() { |
tetsuya256 | 0:fcf360069f17 | 18 | static uint8_t val = 0xFF; |
tetsuya256 | 0:fcf360069f17 | 19 | uint8_t now = (terminalA.read() << 1) | (terminalB.read()); |
tetsuya256 | 0:fcf360069f17 | 20 | if (now == (val & 0x03)) return; |
tetsuya256 | 0:fcf360069f17 | 21 | val = (val << 2) | now; |
tetsuya256 | 0:fcf360069f17 | 22 | switch (val) { |
tetsuya256 | 0:fcf360069f17 | 23 | case 0x4B: (*callbackFunc)(1); break; // cw : 0b01001011 = 0x4B |
tetsuya256 | 0:fcf360069f17 | 24 | case 0x87: (*callbackFunc)(0); break; // ccw : 0b10000111 = 0x87 |
tetsuya256 | 0:fcf360069f17 | 25 | } |
tetsuya256 | 0:fcf360069f17 | 26 | } |