8ABELI Event Klasse

Dependencies:   mbed

8ABELI

Das ist der aktuelle Code für IsA versus HasA.

C++ Seite

Committer:
bulmecisco
Date:
Thu Nov 08 16:42:33 2018 +0000
Revision:
2:24891d3b0e0c
Parent:
1:69b984cadc60
add Led

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bulmecisco 0:63aeb8ecad77 1 #include "mbed.h"
bulmecisco 0:63aeb8ecad77 2
bulmecisco 0:63aeb8ecad77 3 // ---------------- Inherited Switch Event Class from InterruptIn --------------------------
bulmecisco 0:63aeb8ecad77 4 class SwEventInh : public InterruptIn {
bulmecisco 0:63aeb8ecad77 5 //InterruptIn _isr;
bulmecisco 0:63aeb8ecad77 6 volatile int16_t _pressed;
bulmecisco 0:63aeb8ecad77 7 void _risingISR();
bulmecisco 0:63aeb8ecad77 8
bulmecisco 0:63aeb8ecad77 9 public:
bulmecisco 0:63aeb8ecad77 10 SwEventInh(PinName pin) : InterruptIn(pin) { // create the InterruptIn on the pin specified to SwEvent
bulmecisco 0:63aeb8ecad77 11 rise(callback(this, &SwEventInh::_risingISR)); // attach ISR-function of this SwEvent instance
bulmecisco 0:63aeb8ecad77 12 _pressed=0;
bulmecisco 0:63aeb8ecad77 13 }
bulmecisco 0:63aeb8ecad77 14 int checkFlag(); // must in do-condition (while(true)-loop) continuously interrogated
bulmecisco 0:63aeb8ecad77 15 };
bulmecisco 0:63aeb8ecad77 16 // ---------------- Switch Event Class Methodes --------------------------
bulmecisco 0:63aeb8ecad77 17 int SwEventInh::checkFlag() {
bulmecisco 0:63aeb8ecad77 18 if( _pressed ) {
bulmecisco 0:63aeb8ecad77 19 _pressed = 0;
bulmecisco 0:63aeb8ecad77 20 return 1;
bulmecisco 0:63aeb8ecad77 21 }
bulmecisco 0:63aeb8ecad77 22 return 0;
bulmecisco 0:63aeb8ecad77 23 }
bulmecisco 0:63aeb8ecad77 24
bulmecisco 0:63aeb8ecad77 25 void SwEventInh::_risingISR() {
bulmecisco 0:63aeb8ecad77 26 if( read() )
bulmecisco 0:63aeb8ecad77 27 _pressed = 1;
bulmecisco 0:63aeb8ecad77 28 }
bulmecisco 0:63aeb8ecad77 29
bulmecisco 0:63aeb8ecad77 30
bulmecisco 0:63aeb8ecad77 31 // ---------------- Switch Event Class --------------------------
bulmecisco 0:63aeb8ecad77 32 class SwEvent {
bulmecisco 0:63aeb8ecad77 33 InterruptIn _isr;
bulmecisco 0:63aeb8ecad77 34 volatile int16_t _pressed;
bulmecisco 0:63aeb8ecad77 35 void _risingISR();
bulmecisco 0:63aeb8ecad77 36
bulmecisco 0:63aeb8ecad77 37 public:
bulmecisco 0:63aeb8ecad77 38 SwEvent(PinName pin) : _isr(pin) { // create the InterruptIn on the pin specified to SwEvent
bulmecisco 0:63aeb8ecad77 39 _isr.rise(callback(this, &SwEvent::_risingISR)); // attach ISR-function of this SwEvent instance
bulmecisco 0:63aeb8ecad77 40 _pressed=0;
bulmecisco 0:63aeb8ecad77 41 }
bulmecisco 0:63aeb8ecad77 42 int checkFlag(); // must in do-condition (while(true)-loop) continuously interrogated
bulmecisco 0:63aeb8ecad77 43 };
bulmecisco 0:63aeb8ecad77 44 // ---------------- Switch Event Class Methodes --------------------------
bulmecisco 0:63aeb8ecad77 45 int SwEvent::checkFlag() {
bulmecisco 0:63aeb8ecad77 46 if( _pressed ) {
bulmecisco 0:63aeb8ecad77 47 _pressed = 0;
bulmecisco 0:63aeb8ecad77 48 return 1;
bulmecisco 0:63aeb8ecad77 49 }
bulmecisco 0:63aeb8ecad77 50 return 0;
bulmecisco 0:63aeb8ecad77 51 }
bulmecisco 0:63aeb8ecad77 52
bulmecisco 0:63aeb8ecad77 53 void SwEvent::_risingISR() {
bulmecisco 0:63aeb8ecad77 54 if( _isr.read() )
bulmecisco 0:63aeb8ecad77 55 _pressed = 1;
bulmecisco 0:63aeb8ecad77 56 }
bulmecisco 0:63aeb8ecad77 57
bulmecisco 0:63aeb8ecad77 58 SwEventInh sw1(p14);
bulmecisco 0:63aeb8ecad77 59
bulmecisco 0:63aeb8ecad77 60 DigitalOut myled(LED1);
bulmecisco 2:24891d3b0e0c 61 DigitalOut myled2(LED2);
bulmecisco 0:63aeb8ecad77 62
bulmecisco 0:63aeb8ecad77 63 int main() {
bulmecisco 0:63aeb8ecad77 64 myled = 1;
bulmecisco 0:63aeb8ecad77 65 wait(1);
bulmecisco 0:63aeb8ecad77 66 myled = 0;
bulmecisco 0:63aeb8ecad77 67 printf("Hello SwEvent v0.1\n");
bulmecisco 0:63aeb8ecad77 68 while(1) {
bulmecisco 0:63aeb8ecad77 69 sw1.read();
bulmecisco 0:63aeb8ecad77 70 if(sw1.checkFlag())
bulmecisco 0:63aeb8ecad77 71 myled = ! myled;
bulmecisco 0:63aeb8ecad77 72 }
bulmecisco 0:63aeb8ecad77 73 }