Provides an extended version of DigitalOut that adds a "toggle()" function, and uses the RTOS Thread API to provide a "flash()" function that toggles the pin in the background at a given interval.

Dependents:   Car_Simulator

Example of use:

#include "mbed.h"
#include "rtos.h"    /* NOTE: Requires the RTOS library or a matching API */
#include "DigitalOutEx.h"

DigitalOutEx led1(LED1);
DigitalOutEx led2(LED2);

int main() {
    
    led1.flash(200);
    led2.flash(500);
    
    Thread::wait(5000);
    
    led1.flash(50);
    
    Thread::wait(5000);
    
    led1 = 0;
    
    Thread::wait(5000);
    
    led1.flash(1000);
    
    while (1) { }    
}
Committer:
Byrn
Date:
Fri Apr 26 10:31:31 2013 +0000
Revision:
0:1798e1bf583a
Initial commit.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Byrn 0:1798e1bf583a 1
Byrn 0:1798e1bf583a 2 #ifndef __DIGITALOUTEX_H__
Byrn 0:1798e1bf583a 3 #define __DIGITALOUTEX_H__
Byrn 0:1798e1bf583a 4
Byrn 0:1798e1bf583a 5 #include "mbed.h"
Byrn 0:1798e1bf583a 6 #include "rtos.h"
Byrn 0:1798e1bf583a 7
Byrn 0:1798e1bf583a 8 /** DigitalOutEx
Byrn 0:1798e1bf583a 9 *
Byrn 0:1798e1bf583a 10 * This class provides an extended version of DigitalOut that
Byrn 0:1798e1bf583a 11 * adds a toggle() function, and uses the RTOS Thread API to
Byrn 0:1798e1bf583a 12 * provide flashing services.
Byrn 0:1798e1bf583a 13 */
Byrn 0:1798e1bf583a 14 class DigitalOutEx : public DigitalOut {
Byrn 0:1798e1bf583a 15 public:
Byrn 0:1798e1bf583a 16 /** Construct a new DigitalOutEx object.
Byrn 0:1798e1bf583a 17 * @param pin The pin to control, e.g. LED1.
Byrn 0:1798e1bf583a 18 */
Byrn 0:1798e1bf583a 19 DigitalOutEx(PinName pin);
Byrn 0:1798e1bf583a 20
Byrn 0:1798e1bf583a 21 /** Set the state of the pin.
Byrn 0:1798e1bf583a 22 * @param value The state to write; 0 or 1.
Byrn 0:1798e1bf583a 23 */
Byrn 0:1798e1bf583a 24 void write(int value);
Byrn 0:1798e1bf583a 25
Byrn 0:1798e1bf583a 26 /** Toggle the state of the pin. */
Byrn 0:1798e1bf583a 27 void toggle() { write(!read()); }
Byrn 0:1798e1bf583a 28
Byrn 0:1798e1bf583a 29 /** Start 'flashing' the pin.
Byrn 0:1798e1bf583a 30 * This will toggle the state of the pin at the given
Byrn 0:1798e1bf583a 31 * interval. To stop flashing, simply set the state
Byrn 0:1798e1bf583a 32 * of the pin. Flashing is performed in an RTOS Thread.
Byrn 0:1798e1bf583a 33 * @param intervalMs The interval to flash in milliseconds.
Byrn 0:1798e1bf583a 34 */
Byrn 0:1798e1bf583a 35 void flash(int intervalMs = 200);
Byrn 0:1798e1bf583a 36
Byrn 0:1798e1bf583a 37 /** Return 1 if the pin is currently being 'flashed'
Byrn 0:1798e1bf583a 38 * (toggled).
Byrn 0:1798e1bf583a 39 */
Byrn 0:1798e1bf583a 40 int is_flashing() { return _flashing; }
Byrn 0:1798e1bf583a 41
Byrn 0:1798e1bf583a 42 /** Stop the pin from 'flashing' if it is currently doing so.
Byrn 0:1798e1bf583a 43 * The pin will be left in its current state (can be
Byrn 0:1798e1bf583a 44 * determined with read()).
Byrn 0:1798e1bf583a 45 */
Byrn 0:1798e1bf583a 46 void stop_flashing();
Byrn 0:1798e1bf583a 47
Byrn 0:1798e1bf583a 48 /** Set the state of the pin. Shorthand for write().
Byrn 0:1798e1bf583a 49 * @param value The state to write; 0 or 1.
Byrn 0:1798e1bf583a 50 */
Byrn 0:1798e1bf583a 51 DigitalOutEx &operator=(int value);
Byrn 0:1798e1bf583a 52
Byrn 0:1798e1bf583a 53 protected:
Byrn 0:1798e1bf583a 54 Thread *_flashThread;
Byrn 0:1798e1bf583a 55 int _flashing;
Byrn 0:1798e1bf583a 56 int _flashInterval;
Byrn 0:1798e1bf583a 57
Byrn 0:1798e1bf583a 58 static void _flash_thread(const void *argument);
Byrn 0:1798e1bf583a 59 };
Byrn 0:1798e1bf583a 60
Byrn 0:1798e1bf583a 61 #endif