Klassen

Dependencies:   mbed

Committer:
corsa1600
Date:
Mon Feb 04 17:01:00 2019 +0000
Revision:
0:66758402f28d
Klassen

Who changed what in which revision?

UserRevisionLine numberNew contents of line
corsa1600 0:66758402f28d 1 #include "mbed.h"
corsa1600 0:66758402f28d 2
corsa1600 0:66758402f28d 3 class SwEvent
corsa1600 0:66758402f28d 4 {
corsa1600 0:66758402f28d 5
corsa1600 0:66758402f28d 6 public:
corsa1600 0:66758402f28d 7 SwEvent (PinName pin) : _pressed(pin)
corsa1600 0:66758402f28d 8 {
corsa1600 0:66758402f28d 9 _pressed.rise(callback(this, &SwEvent::checkFlag)); // attach increment function of this counter instance
corsa1600 0:66758402f28d 10 }
corsa1600 0:66758402f28d 11 void checkFlag()
corsa1600 0:66758402f28d 12 {
corsa1600 0:66758402f28d 13 _checkFlag = !_checkFlag ;
corsa1600 0:66758402f28d 14 }
corsa1600 0:66758402f28d 15
corsa1600 0:66758402f28d 16
corsa1600 0:66758402f28d 17
corsa1600 0:66758402f28d 18 bool read()
corsa1600 0:66758402f28d 19 {
corsa1600 0:66758402f28d 20 return _checkFlag;
corsa1600 0:66758402f28d 21 }
corsa1600 0:66758402f28d 22
corsa1600 0:66758402f28d 23 private:
corsa1600 0:66758402f28d 24
corsa1600 0:66758402f28d 25 InterruptIn _pressed;
corsa1600 0:66758402f28d 26 volatile bool _checkFlag;
corsa1600 0:66758402f28d 27
corsa1600 0:66758402f28d 28 };
corsa1600 0:66758402f28d 29
corsa1600 0:66758402f28d 30
corsa1600 0:66758402f28d 31 class Counter
corsa1600 0:66758402f28d 32 {
corsa1600 0:66758402f28d 33 public:
corsa1600 0:66758402f28d 34 Counter(PinName pin) : _interrupt(pin) // create the InterruptIn on the pin specified to Counter
corsa1600 0:66758402f28d 35 {
corsa1600 0:66758402f28d 36 _interrupt.rise(callback(this, &Counter::increment)); // attach increment function of this counter instance
corsa1600 0:66758402f28d 37 }
corsa1600 0:66758402f28d 38
corsa1600 0:66758402f28d 39 void increment()
corsa1600 0:66758402f28d 40 {
corsa1600 0:66758402f28d 41 _count++;
corsa1600 0:66758402f28d 42 }
corsa1600 0:66758402f28d 43
corsa1600 0:66758402f28d 44 int read()
corsa1600 0:66758402f28d 45 {
corsa1600 0:66758402f28d 46 return _count;
corsa1600 0:66758402f28d 47 }
corsa1600 0:66758402f28d 48
corsa1600 0:66758402f28d 49 private:
corsa1600 0:66758402f28d 50 InterruptIn _interrupt;
corsa1600 0:66758402f28d 51 volatile int _count;
corsa1600 0:66758402f28d 52 };
corsa1600 0:66758402f28d 53
corsa1600 0:66758402f28d 54 Counter counter(p14);
corsa1600 0:66758402f28d 55 SwEvent swevent (p15);
corsa1600 0:66758402f28d 56
corsa1600 0:66758402f28d 57 int main() {
corsa1600 0:66758402f28d 58 while(1) {
corsa1600 0:66758402f28d 59 printf("Count so far: %d\n", counter.read());
corsa1600 0:66758402f28d 60 printf("true/false so far: %d\n", swevent.read());
corsa1600 0:66758402f28d 61 wait(2);
corsa1600 0:66758402f28d 62 }
corsa1600 0:66758402f28d 63 }