![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Serial
main.cpp@0:645979190d77, 2018-11-15 (annotated)
- Committer:
- mexx
- Date:
- Thu Nov 15 17:24:05 2018 +0000
- Revision:
- 0:645979190d77
Serial
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mexx | 0:645979190d77 | 1 | #include "mbed.h" |
mexx | 0:645979190d77 | 2 | |
mexx | 0:645979190d77 | 3 | |
mexx | 0:645979190d77 | 4 | class SerialEvent |
mexx | 0:645979190d77 | 5 | { |
mexx | 0:645979190d77 | 6 | private: |
mexx | 0:645979190d77 | 7 | Serial _isr; // für serielles einlesen |
mexx | 0:645979190d77 | 8 | volatile int16_t _pressed; // wegen multicore unterstützung |
mexx | 0:645979190d77 | 9 | void _risingISR(); // prototyping |
mexx | 0:645979190d77 | 10 | char _str; // str ist nur eine Variable, könnte auch zeichen heißen |
mexx | 0:645979190d77 | 11 | |
mexx | 0:645979190d77 | 12 | public: |
mexx | 0:645979190d77 | 13 | int checkFlag(); |
mexx | 0:645979190d77 | 14 | void read(char& zeichen); |
mexx | 0:645979190d77 | 15 | SerialEvent(PinName tx, PinName rx): _isr(tx,rx) //Konstruktor |
mexx | 0:645979190d77 | 16 | { |
mexx | 0:645979190d77 | 17 | _isr.attach(callback(this, &SerialEvent::_risingISR)); |
mexx | 0:645979190d77 | 18 | } |
mexx | 0:645979190d77 | 19 | }; |
mexx | 0:645979190d77 | 20 | |
mexx | 0:645979190d77 | 21 | void SerialEvent :: read(char& zeichen) |
mexx | 0:645979190d77 | 22 | { |
mexx | 0:645979190d77 | 23 | zeichen= _str; |
mexx | 0:645979190d77 | 24 | } |
mexx | 0:645979190d77 | 25 | |
mexx | 0:645979190d77 | 26 | |
mexx | 0:645979190d77 | 27 | void SerialEvent :: _risingISR() |
mexx | 0:645979190d77 | 28 | { |
mexx | 0:645979190d77 | 29 | _str = _isr.getc(); |
mexx | 0:645979190d77 | 30 | _pressed=1; |
mexx | 0:645979190d77 | 31 | } |
mexx | 0:645979190d77 | 32 | |
mexx | 0:645979190d77 | 33 | int SerialEvent :: checkFlag () |
mexx | 0:645979190d77 | 34 | { |
mexx | 0:645979190d77 | 35 | if(_pressed) |
mexx | 0:645979190d77 | 36 | { |
mexx | 0:645979190d77 | 37 | _pressed=0; |
mexx | 0:645979190d77 | 38 | return true; |
mexx | 0:645979190d77 | 39 | } |
mexx | 0:645979190d77 | 40 | else |
mexx | 0:645979190d77 | 41 | return false; |
mexx | 0:645979190d77 | 42 | } |
mexx | 0:645979190d77 | 43 | |
mexx | 0:645979190d77 | 44 | SerialEvent pc(USBTX, USBRX); //pc ist wieder nur irgendeine Variable |
mexx | 0:645979190d77 | 45 | |
mexx | 0:645979190d77 | 46 | |
mexx | 0:645979190d77 | 47 | |
mexx | 0:645979190d77 | 48 | |
mexx | 0:645979190d77 | 49 | main() |
mexx | 0:645979190d77 | 50 | { |
mexx | 0:645979190d77 | 51 | |
mexx | 0:645979190d77 | 52 | char z; |
mexx | 0:645979190d77 | 53 | if( pc.checkFlag()) |
mexx | 0:645979190d77 | 54 | pc.read(z); |
mexx | 0:645979190d77 | 55 | printf("Zeichen: %c",z); |
mexx | 0:645979190d77 | 56 | |
mexx | 0:645979190d77 | 57 | } |