Library to show error code with MBED leds. Decimal error code 1-15 can be set

Dependencies:   mbed

This library can show Your error code (integer 1-15) on MBED Leds.

Committer:
eqon
Date:
Wed Sep 05 07:27:16 2012 +0000
Revision:
12:8f420e390452
Parent:
11:e23a7ac024c6
Publish v1

Who changed what in which revision?

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