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

Files at this revision

API Documentation at this revision

Comitter:
Bobty
Date:
Fri Oct 04 07:41:43 2013 +0000
Parent:
1:486b1571d1c4
Child:
3:6014671e50ef
Commit message:
Initial Revision Working

Changed in this revision

RdWebServer.lib Show annotated file Show diff for this revision Revisions of this file
WindowShade.cpp Show annotated file Show diff for this revision Revisions of this file
WindowShade.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/RdWebServer.lib	Thu Sep 19 12:06:43 2013 +0000
+++ b/RdWebServer.lib	Fri Oct 04 07:41:43 2013 +0000
@@ -1,1 +1,1 @@
-RdWebServer#75bb184de749
+RdWebServer#9d8793c23b46
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WindowShade.cpp	Fri Oct 04 07:41:43 2013 +0000
@@ -0,0 +1,1 @@
+#include "WindowShade.h"
--- /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
--- a/main.cpp	Thu Sep 19 12:06:43 2013 +0000
+++ b/main.cpp	Fri Oct 04 07:41:43 2013 +0000
@@ -3,6 +3,7 @@
 #include <stdio.h>
 #include <string.h>
 #include "RdWebServer.h"
+#include "WindowShade.h"
 
 #define PORT   80
 
@@ -17,6 +18,12 @@
 
 Ticker ledTick;
 
+const int TICK_MS = 250;
+const int MAX_WINDOW_SHADES = 5;
+int numWindowShades = 0;
+WindowShade* pWindowShades[MAX_WINDOW_SHADES];
+char* indexHtmName = "";
+
 void ledTickfunc()
 {
     if(webServer.isListening())
@@ -27,26 +34,75 @@
     {
         led1 = false;
     }
+    for (int shadeIdx = 0; shadeIdx < numWindowShades; shadeIdx++)
+        pWindowShades[shadeIdx]->CheckTimeouts(TICK_MS);
+}
+
+void handleCmd_blindControl(char* cmdStr, char* argStr)
+{
+    printf("BLINDS COMMAND %s %s\n\r", cmdStr, argStr);
+    if (argStr == NULL)
+        return;
+    char* pch = strtok (argStr,"/");
+    if (pch == NULL)
+        return;
+    int blindNum = pch[0] - '0';
+    if (blindNum < 1 || blindNum > MAX_WINDOW_SHADES)
+        return;
+    int blindIdx = blindNum-1;
+    char* pDirn = strtok (NULL,"/");
+    if (pDirn == NULL)
+        return;
+    char* pDuration = strtok (NULL,"/");
+    if (pDuration == NULL)
+        return;
+    pWindowShades[blindIdx]->DoCommand(pDirn, pDuration);
 }
 
-void handleCmd_Up(char* argStr)
+void setShadesConfig()
 {
-    printf("UP COMMAND %s\n\r", argStr);
+    printf("Rob Blinds Server - Configured for ");
+    // Check for a config file on the local file system
+    LocalFileSystem local("local");
+    FILE* fp = fopen("office.cnf", "r");
+    if (fp != NULL)
+    {
+        fclose(fp);
+        indexHtmName = "ixoffice.htm";
+        pWindowShades[0] = new WindowShade("Left", p15, p16, p17);
+        pWindowShades[1] = new WindowShade("Mid-left", p18, p19, p20);
+        pWindowShades[2] = new WindowShade("Mid-right", p21, p22, p23);
+        pWindowShades[3] = new WindowShade("Right", p24, p25, p26);
+        pWindowShades[4] = new WindowShade("Rob's", p27, p28, p29);
+        numWindowShades = 5;
+        printf("Office Blinds\n\r");
+    }
+    else
+    {
+        indexHtmName = "ixgames.htm";
+        pWindowShades[0] = new WindowShade("Blind1", p21, p22, p23);
+        pWindowShades[1] = new WindowShade("Blind2", p24, p25, p26);
+        numWindowShades = 2;    
+        printf("Playroom Blinds\n\r");
+    }
 }
 
 int main (void)
 {
     pc.baud(115200);
-    ledTick.attach(&ledTickfunc,0.5);
+    
+    setShadesConfig();
+    
+    ledTick.attach(&ledTickfunc,TICK_MS / 1000.0);
 
     // setup ethernet interface
     eth.init(); //Use DHCP
     eth.connect();
     printf("IP Address is %s\n\r", eth.getIPAddress());
 
-    webServer.addCommand("up", &handleCmd_Up);
+    webServer.addCommand("", RdWebServerCmdDef::CMD_LOCALFILE, NULL, indexHtmName, true);
+    webServer.addCommand("gear-gr.png", RdWebServerCmdDef::CMD_LOCALFILE, NULL, NULL, true);
+    webServer.addCommand("blind", RdWebServerCmdDef::CMD_CALLBACK, &handleCmd_blindControl);
     webServer.init(PORT, &led2);
     webServer.run();
-
-
 }