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.
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) { } }
DigitalOutEx.cpp
- Committer:
- Byrn
- Date:
- 2013-04-26
- Revision:
- 0:1798e1bf583a
- Child:
- 1:6f8ca7b70778
File content as of revision 0:1798e1bf583a:
#include "DigitalOutEx.h" DigitalOutEx::DigitalOutEx(PinName pin) : DigitalOut(pin), _flashThread(NULL), _flashing(0), _flashInterval(200) { } void DigitalOutEx::write(int value) { DigitalOut::write(value); if (_flashing) { stop_flashing(); } } void DigitalOutEx::flash(int intervalMs) { _flashInterval = intervalMs; if (_flashing) return; _flashThread = new Thread(DigitalOutEx::_flash_thread, this); } void DigitalOutEx::stop_flashing() { if (!_flashing) return; _flashThread->terminate(); _flashThread = NULL; _flashing = 0; } DigitalOutEx &DigitalOutEx::operator=(int value) { write(value); return *this; } void DigitalOutEx::_flash_thread(const void *argument) { DigitalOutEx *out = (DigitalOutEx *)argument; out->_flashing = 1; while (1) { // use the base class, as DigitalOutEx's write() stops the flashing for us ((DigitalOut *)out)->write(!out->read()); Thread::wait(((DigitalOutEx *)argument)->_flashInterval); } }