ロータリーエンコーダー (機械接点式インクリメンタル型) の信号をデコードするクラスです。 Rotary Encoder (incremental, mechanical switch type) signal decoder class.

Committer:
tetsuya256
Date:
Sun Sep 07 23:31:47 2014 +0000
Revision:
0:fcf360069f17
First commit.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tetsuya256 0:fcf360069f17 1 #ifndef RotaryEncoder_H
tetsuya256 0:fcf360069f17 2 #define RotaryEncoder_H
tetsuya256 0:fcf360069f17 3
tetsuya256 0:fcf360069f17 4 #include <mbed.h>
tetsuya256 0:fcf360069f17 5
tetsuya256 0:fcf360069f17 6 #define RotaryEncoder_SamplingInterval 2000 // Sampling interval (us)
tetsuya256 0:fcf360069f17 7
tetsuya256 0:fcf360069f17 8 /**
tetsuya256 0:fcf360069f17 9 * ロータリーエンコーダー (機械接点式インクリメンタル型) の信号をデコードするクラスです。<br />
tetsuya256 0:fcf360069f17 10 * Rotary Encoder (incremental, mechanical switch type) signal decoder class.<br />
tetsuya256 0:fcf360069f17 11 * Copyright(C)2014 T.Hoshino<br />
tetsuya256 0:fcf360069f17 12 * <br />
tetsuya256 0:fcf360069f17 13 * CAUTION: My English is very poor, it might be wrong. (I rely on Google Translate almost.)
tetsuya256 0:fcf360069f17 14 * <ul>
tetsuya256 0:fcf360069f17 15 * <li>コンストラクタに指定した2つのピンは「入力・内蔵プルアップ有効」に設定されます。外部プルアップは必要ありません。<br />
tetsuya256 0:fcf360069f17 16 * Two pins that you specified in constructor parameter is set to "input, built-in pull-up enabled". There is no need for external pull-up.
tetsuya256 0:fcf360069f17 17 * </li>
tetsuya256 0:fcf360069f17 18 * <li>機械接点のチャタリングは、Ticker による2ミリ秒ごとのサンプリングで回避します。RCローパスフィルタは必要ありません。<br />
tetsuya256 0:fcf360069f17 19 * This will debounced by software signal sampling. RC lowpass filter is not required.
tetsuya256 0:fcf360069f17 20 * </li>
tetsuya256 0:fcf360069f17 21 * <li>回転検出時は #attach で指定したコールバック関数を呼び出します。<br />
tetsuya256 0:fcf360069f17 22 * Will invoke the callback function you specified in #attach method when detected rotation.
tetsuya256 0:fcf360069f17 23 * </li>
tetsuya256 0:fcf360069f17 24 * <li>回転方向はコールバック関数のパラメータで判別できます。右回りなら 1、左回りなら 0 です。<br />
tetsuya256 0:fcf360069f17 25 * Can detect rotate direction in the parameter of callback function. If clockwise, It's 1. If counterclockwise, It's 0.
tetsuya256 0:fcf360069f17 26 * </li>
tetsuya256 0:fcf360069f17 27 * <li>以下のようなデバイスでの正常動作を確認しています。<br />
tetsuya256 0:fcf360069f17 28 * It has been confirmed the correct operation of following devices.<br />
tetsuya256 0:fcf360069f17 29 * http://akizukidenshi.com/catalog/g/gP-05651/
tetsuya256 0:fcf360069f17 30 * </ul>
tetsuya256 0:fcf360069f17 31 * @code
tetsuya256 0:fcf360069f17 32 //----------------------------------------
tetsuya256 0:fcf360069f17 33 // Sample code for ST Nucleo F401RE
tetsuya256 0:fcf360069f17 34 //----------------------------------------
tetsuya256 0:fcf360069f17 35 #include <mbed.h>
tetsuya256 0:fcf360069f17 36 #include <AQM0802A.h>
tetsuya256 0:fcf360069f17 37 #include <RotaryEncoder.h>
tetsuya256 0:fcf360069f17 38
tetsuya256 0:fcf360069f17 39 AQM0802A lcd(D14, D15); // I2C Character LCD
tetsuya256 0:fcf360069f17 40 RotaryEncoder rotary(D8, D9); // <-- this!
tetsuya256 0:fcf360069f17 41
tetsuya256 0:fcf360069f17 42 // RotaryEncoder callback function
tetsuya256 0:fcf360069f17 43 // cw = 1:clockwise, 0:counterclockwise
tetsuya256 0:fcf360069f17 44 void rotate(int cw) {
tetsuya256 0:fcf360069f17 45 static int value = 0;
tetsuya256 0:fcf360069f17 46 value += cw ? 1 : -1;
tetsuya256 0:fcf360069f17 47 lcd.cls();
tetsuya256 0:fcf360069f17 48 lcd.printf("%d", value);
tetsuya256 0:fcf360069f17 49 }
tetsuya256 0:fcf360069f17 50
tetsuya256 0:fcf360069f17 51 int main() {
tetsuya256 0:fcf360069f17 52 lcd.printf("Rotary\n Encoder");
tetsuya256 0:fcf360069f17 53 rotary.attach(&rotate);
tetsuya256 0:fcf360069f17 54 while(1) {
tetsuya256 0:fcf360069f17 55 sleep(); // sleep() is optional
tetsuya256 0:fcf360069f17 56 }
tetsuya256 0:fcf360069f17 57 }
tetsuya256 0:fcf360069f17 58 * @endcode
tetsuya256 0:fcf360069f17 59 * ご自由にお使いいただけますが、無保証です。使用は自己責任でどうぞ。<br />
tetsuya256 0:fcf360069f17 60 * The use of this is free, but no warranty. please use at your own risk.<br />
tetsuya256 0:fcf360069f17 61 */
tetsuya256 0:fcf360069f17 62 class RotaryEncoder {
tetsuya256 0:fcf360069f17 63 public:
tetsuya256 0:fcf360069f17 64
tetsuya256 0:fcf360069f17 65 /**
tetsuya256 0:fcf360069f17 66 * コンストラクタです。<br />
tetsuya256 0:fcf360069f17 67 * Constructor.<br />
tetsuya256 0:fcf360069f17 68 * ロータリーエンコーダーの信号線に接続されたピンを指定してください。<br />
tetsuya256 0:fcf360069f17 69 * Specify pins connected to the signal line of rotary encoder.<br />
tetsuya256 0:fcf360069f17 70 * どちらのピンも「入力、内蔵プルアップ有効」に設定されます。<br />
tetsuya256 0:fcf360069f17 71 * Both pins are set to "input, built-in pull-up enabled".
tetsuya256 0:fcf360069f17 72 * @param A Terminal A signal input pin.
tetsuya256 0:fcf360069f17 73 * @param B Terminal B signal input pin.
tetsuya256 0:fcf360069f17 74 */
tetsuya256 0:fcf360069f17 75 RotaryEncoder(PinName A, PinName B);
tetsuya256 0:fcf360069f17 76
tetsuya256 0:fcf360069f17 77 /**
tetsuya256 0:fcf360069f17 78 * ロータリーエンコーダーの回転検出時に呼び出すコールバック関数を登録します。<br />
tetsuya256 0:fcf360069f17 79 * Register callback function to be called, when detect rotary encoder rotate.<br />
tetsuya256 0:fcf360069f17 80 * <ul>
tetsuya256 0:fcf360069f17 81 * <li>ロータリーエンコーダーの信号サンプリングもここから開始されます。<br />
tetsuya256 0:fcf360069f17 82 * Signal sampling of rotary encoder also starts from here.
tetsuya256 0:fcf360069f17 83 * </li>
tetsuya256 0:fcf360069f17 84 * <li>回転検出時は、回転方向がt時計回りならパラメータに 1 を、反時計回りなら 0 を渡してコールバック関数を呼び出します。<br />
tetsuya256 0:fcf360069f17 85 * When detect rotation, Call the callback function with int parameter. If clockwise, parameter is 1. Otherwise(counterclockwise) 0.<br />
tetsuya256 0:fcf360069f17 86 * コールバック関数の定義方法は、クラス概要のサンプルコードを参照してください。<br />
tetsuya256 0:fcf360069f17 87 * How to define callback function, please refer to the sample code for class description.
tetsuya256 0:fcf360069f17 88 * </li>
tetsuya256 0:fcf360069f17 89 * </ul>
tetsuya256 0:fcf360069f17 90 * @param callback 登録するコールバック関数。 Callback function to be registered.
tetsuya256 0:fcf360069f17 91 * @see #detach()
tetsuya256 0:fcf360069f17 92 */
tetsuya256 0:fcf360069f17 93 void attach(void (*callback)(int));
tetsuya256 0:fcf360069f17 94
tetsuya256 0:fcf360069f17 95 /**
tetsuya256 0:fcf360069f17 96 * #attach で登録したコールバック関数を切り離します。<br />
tetsuya256 0:fcf360069f17 97 * Detach the callback function that was registered in #attach method.<br />
tetsuya256 0:fcf360069f17 98 * ロータリーエンコーダーの信号サンプリングも停止します。<br />
tetsuya256 0:fcf360069f17 99 * Also stop signal sampling of rotary encoder.
tetsuya256 0:fcf360069f17 100 * @see #attach
tetsuya256 0:fcf360069f17 101 */
tetsuya256 0:fcf360069f17 102 void detach();
tetsuya256 0:fcf360069f17 103
tetsuya256 0:fcf360069f17 104 private:
tetsuya256 0:fcf360069f17 105 DigitalIn terminalA;
tetsuya256 0:fcf360069f17 106 DigitalIn terminalB;
tetsuya256 0:fcf360069f17 107 Ticker tick;
tetsuya256 0:fcf360069f17 108 void smpling();
tetsuya256 0:fcf360069f17 109 void (*callbackFunc)(int);
tetsuya256 0:fcf360069f17 110 };
tetsuya256 0:fcf360069f17 111
tetsuya256 0:fcf360069f17 112 #endif