Encoder (single state) class library
encoder.h@0:b89384fab7bf, 2019-10-24 (annotated)
- Committer:
- ykuroda
- Date:
- Thu Oct 24 02:56:58 2019 +0000
- Revision:
- 0:b89384fab7bf
Encoder (single state) class library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ykuroda | 0:b89384fab7bf | 1 | // |
ykuroda | 0:b89384fab7bf | 2 | // Encoder Class |
ykuroda | 0:b89384fab7bf | 3 | // |
ykuroda | 0:b89384fab7bf | 4 | // 2019.10.13 ... Originally written by Y.Kuroda |
ykuroda | 0:b89384fab7bf | 5 | // |
ykuroda | 0:b89384fab7bf | 6 | #ifndef _COUNTER_H |
ykuroda | 0:b89384fab7bf | 7 | #define _COUNTER_H |
ykuroda | 0:b89384fab7bf | 8 | |
ykuroda | 0:b89384fab7bf | 9 | class Encoder { |
ykuroda | 0:b89384fab7bf | 10 | public: |
ykuroda | 0:b89384fab7bf | 11 | Encoder(PinName pin) : _interrupt(pin), _count(0) { // create the InterruptIn on the pin specified to Counter |
ykuroda | 0:b89384fab7bf | 12 | _interrupt.rise(callback(this, &Encoder::increment)); // attach increment function of this counter instance |
ykuroda | 0:b89384fab7bf | 13 | _interrupt.fall(callback(this, &Encoder::increment)); // attach increment function of this counter instance |
ykuroda | 0:b89384fab7bf | 14 | } |
ykuroda | 0:b89384fab7bf | 15 | |
ykuroda | 0:b89384fab7bf | 16 | void increment() { _count++; } |
ykuroda | 0:b89384fab7bf | 17 | int read() const { return _count; } |
ykuroda | 0:b89384fab7bf | 18 | int set(int c) { return _count=c; } |
ykuroda | 0:b89384fab7bf | 19 | int reset(){ return set(0); } |
ykuroda | 0:b89384fab7bf | 20 | |
ykuroda | 0:b89384fab7bf | 21 | int operator=(int c) { return set(c); } |
ykuroda | 0:b89384fab7bf | 22 | // Encoder& operator=(Encoder& c){ return *this; } |
ykuroda | 0:b89384fab7bf | 23 | // int operator=(Encoder& c){ return read(); } |
ykuroda | 0:b89384fab7bf | 24 | |
ykuroda | 0:b89384fab7bf | 25 | operator int() const { return read(); } |
ykuroda | 0:b89384fab7bf | 26 | |
ykuroda | 0:b89384fab7bf | 27 | protected: |
ykuroda | 0:b89384fab7bf | 28 | InterruptIn _interrupt; |
ykuroda | 0:b89384fab7bf | 29 | volatile int _count; |
ykuroda | 0:b89384fab7bf | 30 | }; |
ykuroda | 0:b89384fab7bf | 31 | |
ykuroda | 0:b89384fab7bf | 32 | #endif |
ykuroda | 0:b89384fab7bf | 33 |