Additional InterruptIn example

Committer:
sarahmarshy
Date:
Fri Jun 23 16:15:38 2017 -0500
Revision:
1:49002ccc54b5
Parent:
0:8c7b073576c5
"Update mbed-os"

Who changed what in which revision?

UserRevisionLine numberNew 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 }