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 06:52:30 2012 +0000
Revision:
4:74c0b4be19a9
Parent:
3:cbca11570652
Child:
5:1e36a6806e12
docu

Who changed what in which revision?

UserRevisionLine numberNew contents of line
eqon 0:3e7de538b366 1 #include "mbed.h"
eqon 1:bef84bec3049 2
eqon 4:74c0b4be19a9 3 /**Library to show error code with MBED leds.
eqon 1:bef84bec3049 4 *@file LEDout.h
eqon 4:74c0b4be19a9 5 @code
eqon 4:74c0b4be19a9 6 #include "mbed.h"
eqon 4:74c0b4be19a9 7 #include "LEDout.h"
eqon 4:74c0b4be19a9 8 int main()
eqon 4:74c0b4be19a9 9 {
eqon 4:74c0b4be19a9 10 LEDout led;
eqon 4:74c0b4be19a9 11 led.blink(15,20);//Blink code 15, 20times, //noblocking, so code can continue
eqon 4:74c0b4be19a9 12 for(int k=0; k<10; k++){
eqon 4:74c0b4be19a9 13 printf("printing while blinking ;) \n");
eqon 4:74c0b4be19a9 14 wait(1);
eqon 4:74c0b4be19a9 15 }
eqon 4:74c0b4be19a9 16 wait(2);
eqon 4:74c0b4be19a9 17 led.blinks(1,3);//blocking, code 1, 3 blinks, code will not continue until blinks done
eqon 4:74c0b4be19a9 18 led.blinkSet(2,3);//blocking, code stops here, blinks will continue blinking
eqon 4:74c0b4be19a9 19 return 0;
eqon 4:74c0b4be19a9 20 }
eqon 4:74c0b4be19a9 21 @endcode
eqon 1:bef84bec3049 22 *@author Egon
eqon 1:bef84bec3049 23 *@date 9/5/2012
eqon 1:bef84bec3049 24 */
eqon 4:74c0b4be19a9 25
eqon 4:74c0b4be19a9 26
eqon 4:74c0b4be19a9 27
eqon 0:3e7de538b366 28 class LEDout : public BusOut
eqon 0:3e7de538b366 29 {
eqon 1:bef84bec3049 30 public:
eqon 1:bef84bec3049 31 /**
eqon 1:bef84bec3049 32 *Create a LEDout object
eqon 1:bef84bec3049 33 */
eqon 0:3e7de538b366 34
eqon 0:3e7de538b366 35 LEDout():BusOut(LED1,LED2,LED3,LED4) {
eqon 0:3e7de538b366 36 _cmd=0;
eqon 0:3e7de538b366 37 _dur=0;
eqon 0:3e7de538b366 38 _blankwait=2; //sec
eqon 0:3e7de538b366 39 // printf("Ledout init\n");
eqon 0:3e7de538b366 40 }
eqon 4:74c0b4be19a9 41 /**
eqon 2:947917aff1fa 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 0:3e7de538b366 47 void blink(int cmd,int cnt, float dur=0.2) {// parallel process
eqon 0:3e7de538b366 48 _cmd=0;
eqon 0:3e7de538b366 49 _dur=0;
eqon 0:3e7de538b366 50 _cmd=cmd;
eqon 0:3e7de538b366 51 _dur=dur*1000*1000;
eqon 0:3e7de538b366 52 _blinkcnt=0;//reset
eqon 0:3e7de538b366 53 _blinkamount=cnt*2;//on/off
eqon 0:3e7de538b366 54 // printf("Blink start cmd %d dur %f blinkcnt %d amount %d cnt %d\n",_cmd,_dur,_blinkcnt,_blinkamount, cnt);
eqon 0:3e7de538b366 55 _ticker2.attach_us(this,&LEDout::blinktick,_dur);
eqon 0:3e7de538b366 56 // printf("Blink start cmd %d dur %f blinkcnt %d amount %d cnt %d\n",_cmd,_dur,_blinkcnt,_blinkamount, cnt);
eqon 0:3e7de538b366 57
eqon 0:3e7de538b366 58
eqon 0:3e7de538b366 59 }
eqon 2:947917aff1fa 60 /*
eqon 2:947917aff1fa 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 };