Man spricht bei Vererbung von einer Ist ein-Beziehung
Übung zu IsA
https://os.mbed.com/users/fpucher/code/HIM0Board/wiki/IsA
ISA
#include "mbed.h" class SwEventInh: public InterruptIn { // InterruptIn _isr; volatile bool _pressed; void _risingISR(); public: SwEventInh(PinName pin) : InterruptIn (pin) { // create the InterruptIn on the pin specified to SwEvent rise(callback(this, &SwEventInh::_risingISR)); // attach ISR-function of this SwEvent instance _pressed=0; } int checkFlag(); // must in do-condition (while(true)-loop) continuously interrogated }; // ---------------- Switch Event Class Methodes -------------------------- int SwEventInh::checkFlag() { if( _pressed ) { _pressed = 0; return 1; } return 0; } void SwEventInh::_risingISR() { if( read() ) _pressed = 1; _pressed = !_pressed; } SwEventInh sw1(p14); DigitalOut myled(LED1); int main() { myled = 1; wait(1); myled = 0; printf("Hello SwEvent v0.1\n"); while(1) { sw1.read(); if(sw1.checkFlag()) myled = ! myled; } }
main.cpp@0:35c32e6d03ab, 2019-02-04 (annotated)
- Committer:
- corsa1600
- Date:
- Mon Feb 04 16:38:41 2019 +0000
- Revision:
- 0:35c32e6d03ab
Man spricht bei Vererbung von einer Ist ein-Beziehung
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
corsa1600 | 0:35c32e6d03ab | 1 | #include "mbed.h" |
corsa1600 | 0:35c32e6d03ab | 2 | |
corsa1600 | 0:35c32e6d03ab | 3 | class SwEventInh: public InterruptIn { |
corsa1600 | 0:35c32e6d03ab | 4 | // InterruptIn _isr; |
corsa1600 | 0:35c32e6d03ab | 5 | volatile bool _pressed; |
corsa1600 | 0:35c32e6d03ab | 6 | void _risingISR(); |
corsa1600 | 0:35c32e6d03ab | 7 | |
corsa1600 | 0:35c32e6d03ab | 8 | public: |
corsa1600 | 0:35c32e6d03ab | 9 | SwEventInh(PinName pin) : InterruptIn (pin) { // create the InterruptIn on the pin specified to SwEvent |
corsa1600 | 0:35c32e6d03ab | 10 | rise(callback(this, &SwEventInh::_risingISR)); // attach ISR-function of this SwEvent instance |
corsa1600 | 0:35c32e6d03ab | 11 | _pressed=0; |
corsa1600 | 0:35c32e6d03ab | 12 | } |
corsa1600 | 0:35c32e6d03ab | 13 | int checkFlag(); // must in do-condition (while(true)-loop) continuously interrogated |
corsa1600 | 0:35c32e6d03ab | 14 | }; |
corsa1600 | 0:35c32e6d03ab | 15 | // ---------------- Switch Event Class Methodes -------------------------- |
corsa1600 | 0:35c32e6d03ab | 16 | int SwEventInh::checkFlag() { |
corsa1600 | 0:35c32e6d03ab | 17 | if( _pressed ) { |
corsa1600 | 0:35c32e6d03ab | 18 | _pressed = 0; |
corsa1600 | 0:35c32e6d03ab | 19 | return 1; |
corsa1600 | 0:35c32e6d03ab | 20 | } |
corsa1600 | 0:35c32e6d03ab | 21 | return 0; |
corsa1600 | 0:35c32e6d03ab | 22 | } |
corsa1600 | 0:35c32e6d03ab | 23 | |
corsa1600 | 0:35c32e6d03ab | 24 | void SwEventInh::_risingISR() { |
corsa1600 | 0:35c32e6d03ab | 25 | if( read() ) |
corsa1600 | 0:35c32e6d03ab | 26 | _pressed = 1; |
corsa1600 | 0:35c32e6d03ab | 27 | _pressed = !_pressed; |
corsa1600 | 0:35c32e6d03ab | 28 | } |
corsa1600 | 0:35c32e6d03ab | 29 | |
corsa1600 | 0:35c32e6d03ab | 30 | SwEventInh sw1(p14); |
corsa1600 | 0:35c32e6d03ab | 31 | DigitalOut myled(LED1); |
corsa1600 | 0:35c32e6d03ab | 32 | |
corsa1600 | 0:35c32e6d03ab | 33 | int main() { |
corsa1600 | 0:35c32e6d03ab | 34 | myled = 1; |
corsa1600 | 0:35c32e6d03ab | 35 | wait(1); |
corsa1600 | 0:35c32e6d03ab | 36 | myled = 0; |
corsa1600 | 0:35c32e6d03ab | 37 | printf("Hello SwEvent v0.1\n"); |
corsa1600 | 0:35c32e6d03ab | 38 | while(1) { |
corsa1600 | 0:35c32e6d03ab | 39 | sw1.read(); |
corsa1600 | 0:35c32e6d03ab | 40 | if(sw1.checkFlag()) |
corsa1600 | 0:35c32e6d03ab | 41 | myled = ! myled; |
corsa1600 | 0:35c32e6d03ab | 42 | } |
corsa1600 | 0:35c32e6d03ab | 43 | } |
corsa1600 | 0:35c32e6d03ab | 44 | |
corsa1600 | 0:35c32e6d03ab | 45 |