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