Yoji KURODA / Mbed OS Mechatro_CounterClass
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers counter.h Source File

counter.h

00001 //
00002 //  Counter Class
00003 //
00004 //  2019.10.13 ... Originally written by Y.Kuroda
00005 //
00006 #ifndef _COUNTER_H
00007 #define _COUNTER_H
00008 
00009 class Counter {
00010   public:
00011     Counter(PinName pin) : _interrupt(pin), _count(0) {       // create the InterruptIn on the pin specified to Counter
00012         _interrupt.rise(callback(this, &Counter::increment)); // attach increment function of this counter instance
00013         _interrupt.fall(callback(this, &Counter::increment)); // attach increment function of this counter instance
00014     }
00015 
00016     void increment() { _count++; }
00017     int read() { return _count; }
00018     int set(int c) { return _count=c; }
00019     int reset(){ return set(0); }
00020 
00021     int operator=(int c) { return set(c); }
00022     operator int() { return read(); }
00023     Counter& operator=(Counter& c){ _count=c.read(); return *this; }
00024 
00025   protected:
00026     InterruptIn _interrupt;
00027     volatile int _count;
00028 };
00029 
00030 #endif
00031