Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@4:d3a068487721, 2018-11-29 (annotated)
- Committer:
- mexx
- Date:
- Thu Nov 29 18:07:14 2018 +0000
- Revision:
- 4:d3a068487721
- Parent:
- 3:4bbef030b4b4
- Child:
- 5:2623865343d9
nicht gut
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| mexx | 0:ee837830d039 | 1 | #include "mbed.h" |
| mexx | 0:ee837830d039 | 2 | |
| mexx | 4:d3a068487721 | 3 | BusOut Leds(LED1, LED2, LED3, LED4); |
| mexx | 4:d3a068487721 | 4 | //BusOut zweiLeds(LED1, LED2); |
| mexx | 1:fb317288084d | 5 | |
| mexx | 0:ee837830d039 | 6 | //DigitalOut led1(LED1); |
| mexx | 0:ee837830d039 | 7 | //DigitalOut led2(LED2); |
| mexx | 0:ee837830d039 | 8 | //DigitalOut led3(LED3); |
| mexx | 0:ee837830d039 | 9 | //DigitalOut led4(LED4); |
| mexx | 4:d3a068487721 | 10 | //DigitalIn btn(P14); |
| mexx | 4:d3a068487721 | 11 | Ticker tickerflipper; |
| mexx | 4:d3a068487721 | 12 | |
| mexx | 4:d3a068487721 | 13 | void timeOut(void); |
| mexx | 0:ee837830d039 | 14 | |
| mexx | 2:73177929c8b4 | 15 | enum Status {ST_LEDOFF=0, ST_LEDEIN=1,ST_Error}; |
| mexx | 3:4bbef030b4b4 | 16 | Stm volatile stm; |
| mexx | 2:73177929c8b4 | 17 | |
| mexx | 2:73177929c8b4 | 18 | class IsA : BusOut{ |
| mexx | 2:73177929c8b4 | 19 | |
| mexx | 2:73177929c8b4 | 20 | IsA():BusOut(LED1, LED2, LED3, LED4){}; |
| mexx | 2:73177929c8b4 | 21 | |
| mexx | 2:73177929c8b4 | 22 | void LedOFF(){write(0x00);} |
| mexx | 2:73177929c8b4 | 23 | |
| mexx | 2:73177929c8b4 | 24 | void LedON(int lVal){write(lVal)}; |
| mexx | 2:73177929c8b4 | 25 | void LedON(){write(0x0f);} |
| mexx | 2:73177929c8b4 | 26 | |
| mexx | 2:73177929c8b4 | 27 | }; |
| mexx | 2:73177929c8b4 | 28 | |
| mexx | 2:73177929c8b4 | 29 | class IsAnEvent : public InterruptIn { |
| mexx | 2:73177929c8b4 | 30 | volatile int16_t _pressed; |
| mexx | 2:73177929c8b4 | 31 | void _RisingISR(); |
| mexx | 0:ee837830d039 | 32 | public: |
| mexx | 2:73177929c8b4 | 33 | IsAnEvent() : InterruptIn(p15) {}; |
| mexx | 2:73177929c8b4 | 34 | IsAnEvent(PinName pin) : InterruptIn(pin) { |
| mexx | 2:73177929c8b4 | 35 | rise(callback(this, &IsAnEvent::_RisingISR)); |
| mexx | 2:73177929c8b4 | 36 | _pressed=0; |
| mexx | 2:73177929c8b4 | 37 | }; |
| mexx | 2:73177929c8b4 | 38 | int CheckFlag(); |
| mexx | 0:ee837830d039 | 39 | void InitIsr(); |
| mexx | 0:ee837830d039 | 40 | }; |
| mexx | 2:73177929c8b4 | 41 | |
| mexx | 2:73177929c8b4 | 42 | void IsAnEvent::InitIsr() { |
| mexx | 2:73177929c8b4 | 43 | rise(callback(this, &IsAnEvent::_RisingISR)); |
| mexx | 2:73177929c8b4 | 44 | } |
| mexx | 2:73177929c8b4 | 45 | |
| mexx | 2:73177929c8b4 | 46 | void IsAnEvent::_RisingISR() { |
| mexx | 3:4bbef030b4b4 | 47 | wait_ms(100); |
| mexx | 2:73177929c8b4 | 48 | if( read() ) |
| mexx | 2:73177929c8b4 | 49 | _pressed = true; |
| mexx | 3:4bbef030b4b4 | 50 | |
| mexx | 2:73177929c8b4 | 51 | } |
| mexx | 3:4bbef030b4b4 | 52 | |
| mexx | 3:4bbef030b4b4 | 53 | |
| mexx | 3:4bbef030b4b4 | 54 | IsAnEvent sw1(p14); // Joy Stick Center |
| mexx | 3:4bbef030b4b4 | 55 | |
| mexx | 2:73177929c8b4 | 56 | int IsAnEvent::CheckFlag() { |
| mexx | 0:ee837830d039 | 57 | if( _pressed ) { |
| mexx | 0:ee837830d039 | 58 | _pressed = false; |
| mexx | 0:ee837830d039 | 59 | return 1; |
| mexx | 0:ee837830d039 | 60 | } |
| mexx | 0:ee837830d039 | 61 | return 0; |
| mexx | 0:ee837830d039 | 62 | } |
| mexx | 2:73177929c8b4 | 63 | |
| mexx | 0:ee837830d039 | 64 | |
| mexx | 0:ee837830d039 | 65 | void stateMachine() |
| mexx | 0:ee837830d039 | 66 | { |
| mexx | 0:ee837830d039 | 67 | printf("state: %d\n", stm.state); |
| mexx | 0:ee837830d039 | 68 | switch (stm.state) |
| mexx | 0:ee837830d039 | 69 | { |
| mexx | 4:d3a068487721 | 70 | case ST_LEDEIN: stm.LedEin(); |
| mexx | 0:ee837830d039 | 71 | break; |
| mexx | 4:d3a068487721 | 72 | case ST_LEDOFF: stm.LedAus(); |
| mexx | 0:ee837830d039 | 73 | break; |
| mexx | 0:ee837830d039 | 74 | default: stm.Error(); |
| mexx | 0:ee837830d039 | 75 | break; |
| mexx | 0:ee837830d039 | 76 | } |
| mexx | 0:ee837830d039 | 77 | } |
| mexx | 3:4bbef030b4b4 | 78 | IsAnEvent sw1(p14); // Joy Stick Center |
| mexx | 3:4bbef030b4b4 | 79 | |
| mexx | 4:d3a068487721 | 80 | class Stm { |
| mexx | 4:d3a068487721 | 81 | public: |
| mexx | 4:d3a068487721 | 82 | Stm() { state=ST_LEDAUS; } |
| mexx | 4:d3a068487721 | 83 | |
| mexx | 4:d3a068487721 | 84 | void Ein(); |
| mexx | 4:d3a068487721 | 85 | void Aus(); |
| mexx | 4:d3a068487721 | 86 | void Error(); |
| mexx | 4:d3a068487721 | 87 | |
| mexx | 4:d3a068487721 | 88 | uint8_t state; |
| mexx | 4:d3a068487721 | 89 | }; |
| mexx | 4:d3a068487721 | 90 | |
| mexx | 4:d3a068487721 | 91 | void Stm::Ein() |
| mexx | 4:d3a068487721 | 92 | { |
| mexx | 4:d3a068487721 | 93 | LedON(){write(0x0f)}; |
| mexx | 4:d3a068487721 | 94 | wait(0,1); |
| mexx | 4:d3a068487721 | 95 | LedON(){write(0x00)}; |
| mexx | 4:d3a068487721 | 96 | while(true) { |
| mexx | 4:d3a068487721 | 97 | LedON({write(0x0f)}; |
| mexx | 4:d3a068487721 | 98 | if(sw1.CheckFlag()) { |
| mexx | 4:d3a068487721 | 99 | state = ST_LEDAUS; |
| mexx | 4:d3a068487721 | 100 | return; |
| mexx | 4:d3a068487721 | 101 | } |
| mexx | 4:d3a068487721 | 102 | } |
| mexx | 4:d3a068487721 | 103 | } |
| mexx | 4:d3a068487721 | 104 | |
| mexx | 4:d3a068487721 | 105 | void Stm::LedAus(){ |
| mexx | 4:d3a068487721 | 106 | while(true) { |
| mexx | 4:d3a068487721 | 107 | led1 = 1; |
| mexx | 4:d3a068487721 | 108 | if(sw1.CheckFlag()) { |
| mexx | 4:d3a068487721 | 109 | state = ST_EIN; |
| mexx | 4:d3a068487721 | 110 | return; |
| mexx | 4:d3a068487721 | 111 | } |
| mexx | 4:d3a068487721 | 112 | } |
| mexx | 4:d3a068487721 | 113 | } |
| mexx | 4:d3a068487721 | 114 | |
| mexx | 4:d3a068487721 | 115 | void Stm::Error(){ |
| mexx | 4:d3a068487721 | 116 | while(1) { |
| mexx | 4:d3a068487721 | 117 | led3 = ~led3; |
| mexx | 4:d3a068487721 | 118 | wait_ms(200); |
| mexx | 4:d3a068487721 | 119 | } |
| mexx | 4:d3a068487721 | 120 | } |
| mexx | 4:d3a068487721 | 121 | |
| mexx | 4:d3a068487721 | 122 | |
| mexx | 4:d3a068487721 | 123 | |
| mexx | 0:ee837830d039 | 124 | |
| mexx | 0:ee837830d039 | 125 | int main() |
| mexx | 0:ee837830d039 | 126 | { |
| mexx | 0:ee837830d039 | 127 | |
| mexx | 0:ee837830d039 | 128 | while(1) { |
| mexx | 0:ee837830d039 | 129 | stateMachine(); |
| mexx | 0:ee837830d039 | 130 | } |
| mexx | 0:ee837830d039 | 131 | } |
| mexx | 2:73177929c8b4 | 132 | |
| mexx | 4:d3a068487721 | 133 | void timeOut(void) |
| mexx | 4:d3a068487721 | 134 | { |
| mexx | 4:d3a068487721 | 135 | |
| mexx | 4:d3a068487721 | 136 | |
| mexx | 4:d3a068487721 | 137 | if(counter >= 3) |
| mexx | 4:d3a068487721 | 138 | { |
| mexx | 4:d3a068487721 | 139 | tickerflipper.detach(); //ticker stop |
| mexx | 4:d3a068487721 | 140 | counter = 0; |
| mexx | 4:d3a068487721 | 141 | return; |
| mexx | 4:d3a068487721 | 142 | } |
| mexx | 4:d3a068487721 | 143 | else counter++; |
| mexx | 4:d3a068487721 | 144 | return; |
| mexx | 4:d3a068487721 | 145 | } |