Library to show error code with MBED leds. Decimal error code 1-15 can be set
This library can show Your error code (integer 1-15) on MBED Leds.
LEDout.h@7:5f7e329a4650, 2012-09-05 (annotated)
- Committer:
- eqon
- Date:
- Wed Sep 05 07:06:20 2012 +0000
- Revision:
- 7:5f7e329a4650
- Parent:
- 6:b7826fcb369b
- Child:
- 10:79c498f26fbd
docs
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
eqon | 7:5f7e329a4650 | 1 | #ifndef LEDOUT_H |
eqon | 7:5f7e329a4650 | 2 | #define LEDOUT_H |
eqon | 7:5f7e329a4650 | 3 | #include "mbed.h" |
eqon | 7:5f7e329a4650 | 4 | |
eqon | 7:5f7e329a4650 | 5 | /**Library to show error code with MBED leds. |
eqon | 7:5f7e329a4650 | 6 | @file LEDout.h |
eqon | 7:5f7e329a4650 | 7 | @code |
eqon | 7:5f7e329a4650 | 8 | #include "mbed.h" |
eqon | 7:5f7e329a4650 | 9 | #include "LEDout.h" |
eqon | 7:5f7e329a4650 | 10 | int main() |
eqon | 7:5f7e329a4650 | 11 | { |
eqon | 7:5f7e329a4650 | 12 | LEDout led; |
eqon | 7:5f7e329a4650 | 13 | led.blink(15,20);//Blink code 15, 20times, //noblocking, so code can continue |
eqon | 7:5f7e329a4650 | 14 | for(int k=0; k<10; k++){ |
eqon | 7:5f7e329a4650 | 15 | printf("printing while blinking ;) \n"); |
eqon | 7:5f7e329a4650 | 16 | wait(1); |
eqon | 7:5f7e329a4650 | 17 | } |
eqon | 7:5f7e329a4650 | 18 | wait(2); |
eqon | 7:5f7e329a4650 | 19 | led.blinks(1,3);//blocking, code 1, 3 blinks, code will not continue until blinks done |
eqon | 7:5f7e329a4650 | 20 | led.blinkSet(2,3);//blocking, code stops here, blinks will continue blinking |
eqon | 7:5f7e329a4650 | 21 | return 0; |
eqon | 7:5f7e329a4650 | 22 | } |
eqon | 7:5f7e329a4650 | 23 | @endcode |
eqon | 7:5f7e329a4650 | 24 | */ |
eqon | 7:5f7e329a4650 | 25 | |
eqon | 7:5f7e329a4650 | 26 | |
eqon | 7:5f7e329a4650 | 27 | |
eqon | 7:5f7e329a4650 | 28 | class LEDout : public BusOut |
eqon | 7:5f7e329a4650 | 29 | { |
eqon | 7:5f7e329a4650 | 30 | public: |
eqon | 7:5f7e329a4650 | 31 | /** Create a LEDout object |
eqon | 7:5f7e329a4650 | 32 | */ |
eqon | 7:5f7e329a4650 | 33 | |
eqon | 7:5f7e329a4650 | 34 | LEDout():BusOut(LED1,LED2,LED3,LED4) { |
eqon | 7:5f7e329a4650 | 35 | _cmd=0; |
eqon | 7:5f7e329a4650 | 36 | _dur=0; |
eqon | 7:5f7e329a4650 | 37 | _blankwait=2; //sec |
eqon | 7:5f7e329a4650 | 38 | // printf("Ledout init\n"); |
eqon | 7:5f7e329a4650 | 39 | } |
eqon | 7:5f7e329a4650 | 40 | /** Non-blocking process blink |
eqon | 7:5f7e329a4650 | 41 | * @param cmd 1-15 |
eqon | 7:5f7e329a4650 | 42 | * @param cnt how many times to blink |
eqon | 7:5f7e329a4650 | 43 | * @param dur blink interval default 0.2sec |
eqon | 7:5f7e329a4650 | 44 | */ |
eqon | 7:5f7e329a4650 | 45 | |
eqon | 7:5f7e329a4650 | 46 | void blink(int cmd,int cnt, float dur=0.2) {// parallel process |
eqon | 7:5f7e329a4650 | 47 | _cmd=0; |
eqon | 7:5f7e329a4650 | 48 | _dur=0; |
eqon | 7:5f7e329a4650 | 49 | _cmd=cmd; |
eqon | 7:5f7e329a4650 | 50 | _dur=dur*1000*1000; |
eqon | 7:5f7e329a4650 | 51 | _blinkcnt=0;//reset |
eqon | 7:5f7e329a4650 | 52 | _blinkamount=cnt*2;//on/off |
eqon | 7:5f7e329a4650 | 53 | // printf("Blink start cmd %d dur %f blinkcnt %d amount %d cnt %d\n",_cmd,_dur,_blinkcnt,_blinkamount, cnt); |
eqon | 7:5f7e329a4650 | 54 | _ticker2.attach_us(this,&LEDout::blinktick,_dur); |
eqon | 7:5f7e329a4650 | 55 | // printf("Blink start cmd %d dur %f blinkcnt %d amount %d cnt %d\n",_cmd,_dur,_blinkcnt,_blinkamount, cnt); |
eqon | 7:5f7e329a4650 | 56 | |
eqon | 7:5f7e329a4650 | 57 | |
eqon | 7:5f7e329a4650 | 58 | } |
eqon | 7:5f7e329a4650 | 59 | /** Blocking process blink |
eqon | 7:5f7e329a4650 | 60 | * @param cmd 1-15 |
eqon | 7:5f7e329a4650 | 61 | * @param cnt how many times to blink |
eqon | 7:5f7e329a4650 | 62 | * @param dur blink interval default 0.2sec |
eqon | 7:5f7e329a4650 | 63 | */ |
eqon | 7:5f7e329a4650 | 64 | void blinks(int cmd,int cnt, float dur =0.2) {// blocking |
eqon | 7:5f7e329a4650 | 65 | for (int k=1; k<=cnt; k++) { |
eqon | 7:5f7e329a4650 | 66 | LEDout::write(cmd);//ON |
eqon | 7:5f7e329a4650 | 67 | wait(dur); |
eqon | 7:5f7e329a4650 | 68 | LEDout::write(0);//OFF |
eqon | 7:5f7e329a4650 | 69 | wait(dur); |
eqon | 7:5f7e329a4650 | 70 | } |
eqon | 7:5f7e329a4650 | 71 | } |
eqon | 7:5f7e329a4650 | 72 | /** Blocking process and end with loop |
eqon | 7:5f7e329a4650 | 73 | * @param cmd 1-15 |
eqon | 7:5f7e329a4650 | 74 | * @param cnt how many times to blink |
eqon | 7:5f7e329a4650 | 75 | * @param dur blink interval default 0.2sec |
eqon | 7:5f7e329a4650 | 76 | */ |
eqon | 7:5f7e329a4650 | 77 | void blinkSet(int cmd,int cnt, float dur =0.2) {// blocking and stopping |
eqon | 7:5f7e329a4650 | 78 | |
eqon | 7:5f7e329a4650 | 79 | while (1) { |
eqon | 7:5f7e329a4650 | 80 | blinks( cmd, cnt, dur); |
eqon | 7:5f7e329a4650 | 81 | wait(_blankwait); // blank space |
eqon | 7:5f7e329a4650 | 82 | } |
eqon | 7:5f7e329a4650 | 83 | } |
eqon | 7:5f7e329a4650 | 84 | protected: |
eqon | 7:5f7e329a4650 | 85 | Timeout _timeout; |
eqon | 7:5f7e329a4650 | 86 | Ticker _ticker2; |
eqon | 7:5f7e329a4650 | 87 | int _blinkcnt; |
eqon | 7:5f7e329a4650 | 88 | int _blinkamount; |
eqon | 7:5f7e329a4650 | 89 | int _cmd; // bit command |
eqon | 7:5f7e329a4650 | 90 | float _blankwait; |
eqon | 7:5f7e329a4650 | 91 | float _dur; |
eqon | 7:5f7e329a4650 | 92 | |
eqon | 7:5f7e329a4650 | 93 | |
eqon | 7:5f7e329a4650 | 94 | void blinktick(void) {// ticker function |
eqon | 7:5f7e329a4650 | 95 | |
eqon | 7:5f7e329a4650 | 96 | // printf("blink\n"); |
eqon | 7:5f7e329a4650 | 97 | if (LEDout::read()==_cmd)//command |
eqon | 7:5f7e329a4650 | 98 | LEDout::write(0); |
eqon | 7:5f7e329a4650 | 99 | else |
eqon | 7:5f7e329a4650 | 100 | LEDout::write(_cmd); |
eqon | 7:5f7e329a4650 | 101 | |
eqon | 7:5f7e329a4650 | 102 | _blinkcnt++; |
eqon | 7:5f7e329a4650 | 103 | |
eqon | 7:5f7e329a4650 | 104 | if (_blinkamount==NULL or !_blinkamount)return; |
eqon | 7:5f7e329a4650 | 105 | |
eqon | 7:5f7e329a4650 | 106 | if (_blinkcnt==_blinkamount) { |
eqon | 7:5f7e329a4650 | 107 | _ticker2.detach(); |
eqon | 7:5f7e329a4650 | 108 | return; |
eqon | 7:5f7e329a4650 | 109 | } |
eqon | 7:5f7e329a4650 | 110 | } |
eqon | 7:5f7e329a4650 | 111 | }; |
eqon | 7:5f7e329a4650 | 112 | #endif |