Yoji KURODA
/
Mechatro_CounterClass
Example code for 'Mechatronics Class'
main.cpp@0:8c7b073576c5, 2017-01-19 (annotated)
- Committer:
- mab5449
- Date:
- Thu Jan 19 14:21:47 2017 -0600
- Revision:
- 0:8c7b073576c5
- Child:
- 2:83a817de162e
Initial commit. Ported code snippets to mbed OS 5 projects
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mab5449 | 0:8c7b073576c5 | 1 | #include "mbed.h" |
mab5449 | 0:8c7b073576c5 | 2 | |
mab5449 | 0:8c7b073576c5 | 3 | class Counter { |
mab5449 | 0:8c7b073576c5 | 4 | public: |
mab5449 | 0:8c7b073576c5 | 5 | Counter(PinName pin) : _interrupt(pin) { // create the InterruptIn on the pin specified to Counter |
mab5449 | 0:8c7b073576c5 | 6 | _interrupt.rise(callback(this, &Counter::increment)); // attach increment function of this counter instance |
mab5449 | 0:8c7b073576c5 | 7 | } |
mab5449 | 0:8c7b073576c5 | 8 | |
mab5449 | 0:8c7b073576c5 | 9 | void increment() { |
mab5449 | 0:8c7b073576c5 | 10 | _count++; |
mab5449 | 0:8c7b073576c5 | 11 | } |
mab5449 | 0:8c7b073576c5 | 12 | |
mab5449 | 0:8c7b073576c5 | 13 | int read() { |
mab5449 | 0:8c7b073576c5 | 14 | return _count; |
mab5449 | 0:8c7b073576c5 | 15 | } |
mab5449 | 0:8c7b073576c5 | 16 | |
mab5449 | 0:8c7b073576c5 | 17 | private: |
mab5449 | 0:8c7b073576c5 | 18 | InterruptIn _interrupt; |
mab5449 | 0:8c7b073576c5 | 19 | volatile int _count; |
mab5449 | 0:8c7b073576c5 | 20 | }; |
mab5449 | 0:8c7b073576c5 | 21 | |
mab5449 | 0:8c7b073576c5 | 22 | Counter counter(SW2); |
mab5449 | 0:8c7b073576c5 | 23 | |
mab5449 | 0:8c7b073576c5 | 24 | int main() { |
mab5449 | 0:8c7b073576c5 | 25 | while(1) { |
mab5449 | 0:8c7b073576c5 | 26 | printf("Count so far: %d\n", counter.read()); |
mab5449 | 0:8c7b073576c5 | 27 | wait(2); |
mab5449 | 0:8c7b073576c5 | 28 | } |
mab5449 | 0:8c7b073576c5 | 29 | } |