Server for window shades - using Soffy DCT-30 motors - more details here http://robdobson.com/2013/10/moving-my-window-shades-control-to-mbed/
Dependencies: EthernetInterface RdWebServer mbed-rtos mbed
Diff: WindowShade.h
- Revision:
- 2:24fd130c3600
- Child:
- 4:c593741df37e
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WindowShade.h Fri Oct 04 07:41:43 2013 +0000 @@ -0,0 +1,127 @@ +#ifndef RD_WIN_SHADE +#define RD_WIN_SHADE + +#include "mbed.h" + +class WindowShade +{ + public: + static const int MAX_SHADE_NAME_LEN = 20; + static const long PULSE_ON_MILLISECS = 500; + static const long MAX_SHADE_ON_MILLSECS = 60000; + + WindowShade(char* name, PinName up, PinName stop, PinName down) + { + strncpy(_shadeName, name, MAX_SHADE_NAME_LEN); + _shadeName[MAX_SHADE_NAME_LEN-1] = '\0'; + _pUpPin = new DigitalOut(up); + _pStopPin = new DigitalOut(stop); + _pDownPin = new DigitalOut(down); + _msTimeout = 0; + _tickCount = 0; + + } + + void ClearOutputs() + { + // Clear any existing command + _msTimeout = 0; + _tickCount = 0; + *_pUpPin = false; + *_pStopPin = false; + *_pDownPin = false; + } + + void SetTimedOutput(DigitalOut* pPin, bool state, long msDuration, bool bClearExisting) + { +// printf("Setting %s %d %ld %d\n\r", _shadeName, state, msDuration, bClearExisting); + + if (bClearExisting) + ClearOutputs(); + *pPin = state; + if (msDuration != 0) + { + _msTimeout = msDuration; + _tickCount = 0; + } + } + + void CheckTimeouts(int tickMs) + { + // Couldn't get Timer to work so doing with a tick-count + if (_msTimeout != 0) + { + _tickCount++; + if ((_tickCount-1)*tickMs > _msTimeout) + { + ClearOutputs(); + _msTimeout = 0; + } + } + } + + void DoCommand(char* pDirn, char* pDuration) + { +// printf("DoCommand %s %s %s\n\r", _shadeName, pDirn, pDuration); + + // Duration and state + int state = false; + int msDuration = 0; + if (strcmp(pDuration, "on") == 0) + { + state = true; + msDuration = MAX_SHADE_ON_MILLSECS; + } + else if (strcmp(pDuration, "off") == 0) + { + state = false; + msDuration = 0; + } + else if (strcmp(pDuration, "pulse") == 0) + { + state = true; + msDuration = PULSE_ON_MILLISECS; + } + + // Direction & config + if (strcmp(pDirn, "up") == 0) + { + SetTimedOutput(_pUpPin, state, msDuration, true); + } + else if (strcmp(pDirn, "stop") == 0) + { + SetTimedOutput(_pStopPin, state, msDuration, true); + } + else if (strcmp(pDirn, "down") == 0) + { + SetTimedOutput(_pDownPin, state, msDuration, true); + } + else if (strcmp(pDirn, "setuplimit") == 0) + { + SetTimedOutput(_pStopPin, state, 0, true); + SetTimedOutput(_pDownPin, state, msDuration, false); + } + else if (strcmp(pDirn, "setdownlimit") == 0) + { + SetTimedOutput(_pStopPin, state, 0, true); + SetTimedOutput(_pUpPin, state, msDuration, false); + } + else if (strcmp(pDirn, "resetmemory") == 0) + { + SetTimedOutput(_pStopPin, state, 0, true); + SetTimedOutput(_pDownPin, state, 0, false); + SetTimedOutput(_pUpPin, state, msDuration, false); + } + + } + + char _shadeName[MAX_SHADE_NAME_LEN]; + DigitalOut* _pUpPin; + DigitalOut* _pStopPin; + DigitalOut* _pDownPin; + long _msTimeout; + int _tickCount; + +}; + +#endif