Dependencies:   mbed

Committer:
Wizo
Date:
Thu Nov 15 17:59:17 2018 +0000
Revision:
0:28f54d80907a
InterruptCounter

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Wizo 0:28f54d80907a 1 #include "mbed.h"
Wizo 0:28f54d80907a 2
Wizo 0:28f54d80907a 3 class Counter {
Wizo 0:28f54d80907a 4 public:
Wizo 0:28f54d80907a 5 Counter(PinName pin) : _interrupt(pin) { // create the InterruptIn on the pin specified to Counter
Wizo 0:28f54d80907a 6 _interrupt.rise(callback(this, &Counter::increment)); // attach increment function of this counter instance
Wizo 0:28f54d80907a 7 }
Wizo 0:28f54d80907a 8
Wizo 0:28f54d80907a 9 void increment() {
Wizo 0:28f54d80907a 10 _count++;
Wizo 0:28f54d80907a 11 }
Wizo 0:28f54d80907a 12
Wizo 0:28f54d80907a 13 int read() {
Wizo 0:28f54d80907a 14 return _count;
Wizo 0:28f54d80907a 15 }
Wizo 0:28f54d80907a 16
Wizo 0:28f54d80907a 17 private:
Wizo 0:28f54d80907a 18 InterruptIn _interrupt;
Wizo 0:28f54d80907a 19 volatile int _count;
Wizo 0:28f54d80907a 20 };
Wizo 0:28f54d80907a 21
Wizo 0:28f54d80907a 22
Wizo 0:28f54d80907a 23
Wizo 0:28f54d80907a 24 Counter counter(p14);
Wizo 0:28f54d80907a 25
Wizo 0:28f54d80907a 26 int main() {
Wizo 0:28f54d80907a 27 while(1) {
Wizo 0:28f54d80907a 28 printf("Count so far: %d\n", counter.read());
Wizo 0:28f54d80907a 29 wait(2);
Wizo 0:28f54d80907a 30 }
Wizo 0:28f54d80907a 31 }
Wizo 0:28f54d80907a 32
Wizo 0:28f54d80907a 33 // ---------------- Switch Event Class --------------------------
Wizo 0:28f54d80907a 34 class SwEvent {
Wizo 0:28f54d80907a 35 InterruptIn _isr;
Wizo 0:28f54d80907a 36 volatile int16_t _pressed;
Wizo 0:28f54d80907a 37 void _risingISR();
Wizo 0:28f54d80907a 38
Wizo 0:28f54d80907a 39 public:
Wizo 0:28f54d80907a 40 SwEvent(PinName pin) : _isr(pin) { // create the InterruptIn on the pin specified to SwEvent
Wizo 0:28f54d80907a 41 _isr.rise(callback(this, &SwEvent::_risingISR)); // attach ISR-function of this SwEvent instance
Wizo 0:28f54d80907a 42 _pressed=0;
Wizo 0:28f54d80907a 43 }
Wizo 0:28f54d80907a 44 int checkFlag(); // must in do-condition (while(true)-loop) continuously interrogated
Wizo 0:28f54d80907a 45 void init();
Wizo 0:28f54d80907a 46 };
Wizo 0:28f54d80907a 47 // ---------------- Switch Event Class Methodes --------------------------
Wizo 0:28f54d80907a 48 int SwEvent::checkFlag() {
Wizo 0:28f54d80907a 49 if( _pressed ) {
Wizo 0:28f54d80907a 50 _pressed = 0;
Wizo 0:28f54d80907a 51 return 1;
Wizo 0:28f54d80907a 52 }
Wizo 0:28f54d80907a 53 return 0;
Wizo 0:28f54d80907a 54 }
Wizo 0:28f54d80907a 55
Wizo 0:28f54d80907a 56 void SwEvent::_risingISR() {
Wizo 0:28f54d80907a 57 if( _isr.read() )
Wizo 0:28f54d80907a 58 _pressed = 1;
Wizo 0:28f54d80907a 59 }
Wizo 0:28f54d80907a 60
Wizo 0:28f54d80907a 61
Wizo 0:28f54d80907a 62 // ------------------------------------------------------------------------------
Wizo 0:28f54d80907a 63 /*
Wizo 0:28f54d80907a 64 class SwEvent {
Wizo 0:28f54d80907a 65
Wizo 0:28f54d80907a 66 private:
Wizo 0:28f54d80907a 67 InterruptIn _isr;
Wizo 0:28f54d80907a 68 volatile int16_t _pressed; //volatile benutzt den gleichen Cache bei einem MultiCore
Wizo 0:28f54d80907a 69 void _risingISR();
Wizo 0:28f54d80907a 70 char _str;
Wizo 0:28f54d80907a 71
Wizo 0:28f54d80907a 72 public:
Wizo 0:28f54d80907a 73 int checkFlag();
Wizo 0:28f54d80907a 74 void read(char& zeichen);
Wizo 0:28f54d80907a 75 SwEvent(PinName tx, PinName rx)
Wizo 0:28f54d80907a 76
Wizo 0:28f54d80907a 77 void SwEvent :: read(char& zeichen){
Wizo 0:28f54d80907a 78
Wizo 0:28f54d80907a 79 zeichen = _str;
Wizo 0:28f54d80907a 80
Wizo 0:28f54d80907a 81 }
Wizo 0:28f54d80907a 82
Wizo 0:28f54d80907a 83 Serial pc(USBTX, USBRX);
Wizo 0:28f54d80907a 84
Wizo 0:28f54d80907a 85
Wizo 0:28f54d80907a 86 void SwEvent :: _risingISR(){
Wizo 0:28f54d80907a 87
Wizo 0:28f54d80907a 88 _str = getc();
Wizo 0:28f54d80907a 89 _pressed = 1;
Wizo 0:28f54d80907a 90
Wizo 0:28f54d80907a 91 }
Wizo 0:28f54d80907a 92
Wizo 0:28f54d80907a 93 int SwEvent :: checkFlag(){
Wizo 0:28f54d80907a 94
Wizo 0:28f54d80907a 95 if(_pressed){
Wizo 0:28f54d80907a 96
Wizo 0:28f54d80907a 97 _pressed = 0;
Wizo 0:28f54d80907a 98 return true;
Wizo 0:28f54d80907a 99
Wizo 0:28f54d80907a 100 }
Wizo 0:28f54d80907a 101 else
Wizo 0:28f54d80907a 102 return false;
Wizo 0:28f54d80907a 103 }
Wizo 0:28f54d80907a 104
Wizo 0:28f54d80907a 105 main(){
Wizo 0:28f54d80907a 106
Wizo 0:28f54d80907a 107 char z;
Wizo 0:28f54d80907a 108 if(checkFlag())
Wizo 0:28f54d80907a 109 z = read();
Wizo 0:28f54d80907a 110
Wizo 0:28f54d80907a 111 }
Wizo 0:28f54d80907a 112 */