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
WindowShade.h@2:24fd130c3600, 2013-10-04 (annotated)
- Committer:
- Bobty
- Date:
- Fri Oct 04 07:41:43 2013 +0000
- Revision:
- 2:24fd130c3600
- Child:
- 4:c593741df37e
Initial Revision Working
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Bobty | 2:24fd130c3600 | 1 | #ifndef RD_WIN_SHADE |
Bobty | 2:24fd130c3600 | 2 | #define RD_WIN_SHADE |
Bobty | 2:24fd130c3600 | 3 | |
Bobty | 2:24fd130c3600 | 4 | #include "mbed.h" |
Bobty | 2:24fd130c3600 | 5 | |
Bobty | 2:24fd130c3600 | 6 | class WindowShade |
Bobty | 2:24fd130c3600 | 7 | { |
Bobty | 2:24fd130c3600 | 8 | public: |
Bobty | 2:24fd130c3600 | 9 | static const int MAX_SHADE_NAME_LEN = 20; |
Bobty | 2:24fd130c3600 | 10 | static const long PULSE_ON_MILLISECS = 500; |
Bobty | 2:24fd130c3600 | 11 | static const long MAX_SHADE_ON_MILLSECS = 60000; |
Bobty | 2:24fd130c3600 | 12 | |
Bobty | 2:24fd130c3600 | 13 | WindowShade(char* name, PinName up, PinName stop, PinName down) |
Bobty | 2:24fd130c3600 | 14 | { |
Bobty | 2:24fd130c3600 | 15 | strncpy(_shadeName, name, MAX_SHADE_NAME_LEN); |
Bobty | 2:24fd130c3600 | 16 | _shadeName[MAX_SHADE_NAME_LEN-1] = '\0'; |
Bobty | 2:24fd130c3600 | 17 | _pUpPin = new DigitalOut(up); |
Bobty | 2:24fd130c3600 | 18 | _pStopPin = new DigitalOut(stop); |
Bobty | 2:24fd130c3600 | 19 | _pDownPin = new DigitalOut(down); |
Bobty | 2:24fd130c3600 | 20 | _msTimeout = 0; |
Bobty | 2:24fd130c3600 | 21 | _tickCount = 0; |
Bobty | 2:24fd130c3600 | 22 | |
Bobty | 2:24fd130c3600 | 23 | } |
Bobty | 2:24fd130c3600 | 24 | |
Bobty | 2:24fd130c3600 | 25 | void ClearOutputs() |
Bobty | 2:24fd130c3600 | 26 | { |
Bobty | 2:24fd130c3600 | 27 | // Clear any existing command |
Bobty | 2:24fd130c3600 | 28 | _msTimeout = 0; |
Bobty | 2:24fd130c3600 | 29 | _tickCount = 0; |
Bobty | 2:24fd130c3600 | 30 | *_pUpPin = false; |
Bobty | 2:24fd130c3600 | 31 | *_pStopPin = false; |
Bobty | 2:24fd130c3600 | 32 | *_pDownPin = false; |
Bobty | 2:24fd130c3600 | 33 | } |
Bobty | 2:24fd130c3600 | 34 | |
Bobty | 2:24fd130c3600 | 35 | void SetTimedOutput(DigitalOut* pPin, bool state, long msDuration, bool bClearExisting) |
Bobty | 2:24fd130c3600 | 36 | { |
Bobty | 2:24fd130c3600 | 37 | // printf("Setting %s %d %ld %d\n\r", _shadeName, state, msDuration, bClearExisting); |
Bobty | 2:24fd130c3600 | 38 | |
Bobty | 2:24fd130c3600 | 39 | if (bClearExisting) |
Bobty | 2:24fd130c3600 | 40 | ClearOutputs(); |
Bobty | 2:24fd130c3600 | 41 | *pPin = state; |
Bobty | 2:24fd130c3600 | 42 | if (msDuration != 0) |
Bobty | 2:24fd130c3600 | 43 | { |
Bobty | 2:24fd130c3600 | 44 | _msTimeout = msDuration; |
Bobty | 2:24fd130c3600 | 45 | _tickCount = 0; |
Bobty | 2:24fd130c3600 | 46 | } |
Bobty | 2:24fd130c3600 | 47 | } |
Bobty | 2:24fd130c3600 | 48 | |
Bobty | 2:24fd130c3600 | 49 | void CheckTimeouts(int tickMs) |
Bobty | 2:24fd130c3600 | 50 | { |
Bobty | 2:24fd130c3600 | 51 | // Couldn't get Timer to work so doing with a tick-count |
Bobty | 2:24fd130c3600 | 52 | if (_msTimeout != 0) |
Bobty | 2:24fd130c3600 | 53 | { |
Bobty | 2:24fd130c3600 | 54 | _tickCount++; |
Bobty | 2:24fd130c3600 | 55 | if ((_tickCount-1)*tickMs > _msTimeout) |
Bobty | 2:24fd130c3600 | 56 | { |
Bobty | 2:24fd130c3600 | 57 | ClearOutputs(); |
Bobty | 2:24fd130c3600 | 58 | _msTimeout = 0; |
Bobty | 2:24fd130c3600 | 59 | } |
Bobty | 2:24fd130c3600 | 60 | } |
Bobty | 2:24fd130c3600 | 61 | } |
Bobty | 2:24fd130c3600 | 62 | |
Bobty | 2:24fd130c3600 | 63 | void DoCommand(char* pDirn, char* pDuration) |
Bobty | 2:24fd130c3600 | 64 | { |
Bobty | 2:24fd130c3600 | 65 | // printf("DoCommand %s %s %s\n\r", _shadeName, pDirn, pDuration); |
Bobty | 2:24fd130c3600 | 66 | |
Bobty | 2:24fd130c3600 | 67 | // Duration and state |
Bobty | 2:24fd130c3600 | 68 | int state = false; |
Bobty | 2:24fd130c3600 | 69 | int msDuration = 0; |
Bobty | 2:24fd130c3600 | 70 | if (strcmp(pDuration, "on") == 0) |
Bobty | 2:24fd130c3600 | 71 | { |
Bobty | 2:24fd130c3600 | 72 | state = true; |
Bobty | 2:24fd130c3600 | 73 | msDuration = MAX_SHADE_ON_MILLSECS; |
Bobty | 2:24fd130c3600 | 74 | } |
Bobty | 2:24fd130c3600 | 75 | else if (strcmp(pDuration, "off") == 0) |
Bobty | 2:24fd130c3600 | 76 | { |
Bobty | 2:24fd130c3600 | 77 | state = false; |
Bobty | 2:24fd130c3600 | 78 | msDuration = 0; |
Bobty | 2:24fd130c3600 | 79 | } |
Bobty | 2:24fd130c3600 | 80 | else if (strcmp(pDuration, "pulse") == 0) |
Bobty | 2:24fd130c3600 | 81 | { |
Bobty | 2:24fd130c3600 | 82 | state = true; |
Bobty | 2:24fd130c3600 | 83 | msDuration = PULSE_ON_MILLISECS; |
Bobty | 2:24fd130c3600 | 84 | } |
Bobty | 2:24fd130c3600 | 85 | |
Bobty | 2:24fd130c3600 | 86 | // Direction & config |
Bobty | 2:24fd130c3600 | 87 | if (strcmp(pDirn, "up") == 0) |
Bobty | 2:24fd130c3600 | 88 | { |
Bobty | 2:24fd130c3600 | 89 | SetTimedOutput(_pUpPin, state, msDuration, true); |
Bobty | 2:24fd130c3600 | 90 | } |
Bobty | 2:24fd130c3600 | 91 | else if (strcmp(pDirn, "stop") == 0) |
Bobty | 2:24fd130c3600 | 92 | { |
Bobty | 2:24fd130c3600 | 93 | SetTimedOutput(_pStopPin, state, msDuration, true); |
Bobty | 2:24fd130c3600 | 94 | } |
Bobty | 2:24fd130c3600 | 95 | else if (strcmp(pDirn, "down") == 0) |
Bobty | 2:24fd130c3600 | 96 | { |
Bobty | 2:24fd130c3600 | 97 | SetTimedOutput(_pDownPin, state, msDuration, true); |
Bobty | 2:24fd130c3600 | 98 | } |
Bobty | 2:24fd130c3600 | 99 | else if (strcmp(pDirn, "setuplimit") == 0) |
Bobty | 2:24fd130c3600 | 100 | { |
Bobty | 2:24fd130c3600 | 101 | SetTimedOutput(_pStopPin, state, 0, true); |
Bobty | 2:24fd130c3600 | 102 | SetTimedOutput(_pDownPin, state, msDuration, false); |
Bobty | 2:24fd130c3600 | 103 | } |
Bobty | 2:24fd130c3600 | 104 | else if (strcmp(pDirn, "setdownlimit") == 0) |
Bobty | 2:24fd130c3600 | 105 | { |
Bobty | 2:24fd130c3600 | 106 | SetTimedOutput(_pStopPin, state, 0, true); |
Bobty | 2:24fd130c3600 | 107 | SetTimedOutput(_pUpPin, state, msDuration, false); |
Bobty | 2:24fd130c3600 | 108 | } |
Bobty | 2:24fd130c3600 | 109 | else if (strcmp(pDirn, "resetmemory") == 0) |
Bobty | 2:24fd130c3600 | 110 | { |
Bobty | 2:24fd130c3600 | 111 | SetTimedOutput(_pStopPin, state, 0, true); |
Bobty | 2:24fd130c3600 | 112 | SetTimedOutput(_pDownPin, state, 0, false); |
Bobty | 2:24fd130c3600 | 113 | SetTimedOutput(_pUpPin, state, msDuration, false); |
Bobty | 2:24fd130c3600 | 114 | } |
Bobty | 2:24fd130c3600 | 115 | |
Bobty | 2:24fd130c3600 | 116 | } |
Bobty | 2:24fd130c3600 | 117 | |
Bobty | 2:24fd130c3600 | 118 | char _shadeName[MAX_SHADE_NAME_LEN]; |
Bobty | 2:24fd130c3600 | 119 | DigitalOut* _pUpPin; |
Bobty | 2:24fd130c3600 | 120 | DigitalOut* _pStopPin; |
Bobty | 2:24fd130c3600 | 121 | DigitalOut* _pDownPin; |
Bobty | 2:24fd130c3600 | 122 | long _msTimeout; |
Bobty | 2:24fd130c3600 | 123 | int _tickCount; |
Bobty | 2:24fd130c3600 | 124 | |
Bobty | 2:24fd130c3600 | 125 | }; |
Bobty | 2:24fd130c3600 | 126 | |
Bobty | 2:24fd130c3600 | 127 | #endif |