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