Skip to content
This repository has been archived by the owner on Nov 18, 2020. It is now read-only.

Commit

Permalink
Refactor sendWebsiteData to getWebsiteData
Browse files Browse the repository at this point in the history
Moved website internal encoding logic out of mode definitions. The mode classes
now simply declare their name, HTML and javascript. The generic code in
Websockets.ino is asking for this data and formats it for the frontend.
  • Loading branch information
LarsMichelsen committed Dec 28, 2019
1 parent dcf2364 commit 5aff5ef
Show file tree
Hide file tree
Showing 12 changed files with 127 additions and 80 deletions.
23 changes: 14 additions & 9 deletions Mode Registry/ModeCircle.ino
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,25 @@ public:

virtual void applyConfig(JsonVariant &settings) {}

virtual void sendWebsiteData(WebSocketsServer &_webSocketServer)
virtual const char *getName()
{
const char *modeName = "Circle";
const char *tabHtml = PSTR("<h2>Circle Mode<\\/h2>"
"<p>A simple dot moving round the lamp.<\\/p>");
const char *tabScript = PSTR("messageEventList.push(handleCircleMessage);\\r\\n"
return "Circle";
}

virtual const char *getTabHtml()
{
return PSTR("<h2>Circle Mode<\\/h2>"
"<p>A simple dot moving round the lamp.<\\/p>");
}

virtual const char *getTabScript()
{
return PSTR("messageEventList.push(handleCircleMessage);\\r\\n"
"function handleCircleMessage(jsonMessage) {\\r\\n"
" if (\\\"Circle\\\" in jsonMessage) {\\r\\n"
" jsonMessage = jsonMessage.Circle\\r\\n"
" if (typeof jsonMessage === \\\"object\\\") {}\\r\\n"
" }\\r\\n"
" }\\r\\n");
String htmlJSON = String("{\"Tab\" : {") + "\"Name\": \"" + modeName + "\", \"tabHtml\" : \"" + tabHtml + "\", \"tabScript\" : \"" + tabScript + "\"}}";

_webSocketServer.broadcastTXT(htmlJSON.c_str());
}
};
};
19 changes: 12 additions & 7 deletions Mode Registry/ModeColorWipe.ino
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,23 @@ public:
settings["Speed"] = colorWipeSpeed = settings["Speed"] | colorWipeSpeed;
}

virtual void sendWebsiteData(WebSocketsServer &_webSocketServer)
virtual const char *getName()
{
const char *modeName = "Colour Wipe";
const char *tabHtml = PSTR("<h2>Color Wipe Mode<\\/h2>\\r\\n"
return "Colour Wipe";
}

virtual const char *getTabHtml()
{
return PSTR("<h2>Color Wipe Mode<\\/h2>\\r\\n"
"<p>Color Wipe will fill the light with a color in a wiping fashion then wipe the light away.<\\/p>\\r\\n"
"<div class=\\\"row my-3\\\">\\r\\n"
" <input id=\\\"colorWipeSelectButton\\\" class=\\\"color col mb-2 mx-2 btn btn-lg btn-outline-light\\\" value=\\\"rgb(0,0,0)\\\"><\\/input>\\r\\n"
"<\\/div>\\r\\n");
const char *tabScript = PSTR("var colorWipeLastMessage = \\\"\\\"\\r\\n"
}

virtual const char *getTabScript()
{
return PSTR("var colorWipeLastMessage = \\\"\\\"\\r\\n"
"var colorWipeRed = 0\\r\\n"
"var colorWipeGreen = 0\\r\\n"
"var colorWipeBlue = 0\\r\\n"
Expand Down Expand Up @@ -143,8 +151,5 @@ public:
" }\\r\\n"
" }\\r\\n"
"}\\r\\n");
String htmlJSON = String("{\"Tab\" : {") + "\"Name\": \"" + modeName + "\", \"tabHtml\" : \"" + tabHtml + "\", \"tabScript\" : \"" + tabScript + "\"}}";

_webSocketServer.broadcastTXT(htmlJSON.c_str());
}
};
22 changes: 13 additions & 9 deletions Mode Registry/ModeConfetti.ino
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,20 @@ public:
settings["Speed"] = confettiSpeed = settings["Speed"] | confettiSpeed;
}

virtual void sendWebsiteData(WebSocketsServer &_webSocketServer)
virtual const char *getName()
{
const char *modeName = "Confetti";
const char *tabHtml = PSTR("<h2>Confetti Mode<\\/h2>\\r\\n"
"<p>Confetti will flash random colors to emulate confetti.<\\/p>\\r\\n");
const char *tabScript = PSTR("var confettiLastMessage = \\\"\\\"\\r\\n"
return "Confetti";
}

virtual const char *getTabHtml()
{
return PSTR("<h2>Confetti Mode<\\/h2>\\r\\n"
"<p>Confetti will flash random colors to emulate confetti.<\\/p>\\r\\n");
}

virtual const char *getTabScript()
{
return PSTR("var confettiLastMessage = \\\"\\\"\\r\\n"
" var confettiRed = 0;\\r\\n"
" var confettiGreen = 0;\\r\\n"
" var confettiBlue = 0;\\r\\n"
Expand Down Expand Up @@ -144,9 +152,5 @@ public:
" sendMessage(msg)\\r\\n"
" }\\r\\n"
" }\\r\\n");

String htmlJSON = String("{\"Tab\" : {") + "\"Name\": \"" + modeName + "\", \"tabHtml\" : \"" + tabHtml + "\", \"tabScript\" : \"" + tabScript + "\"}}";

_webSocketServer.broadcastTXT(htmlJSON.c_str());
}
};
20 changes: 12 additions & 8 deletions Mode Registry/ModeSparkle.ino
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,23 @@ public:
settings["Speed"] = sparkleSpeed = settings["Speed"] | sparkleSpeed;
}

virtual void sendWebsiteData(WebSocketsServer &_webSocketServer)
virtual const char *getName()
{
const char *modeName = "Sparkle";
const char *tabHtml = PSTR("<h2>Sparkle Mode<\\/h2>\\r\\n"
return "Sparkle";
}

virtual const char *getTabHtml()
{
return PSTR("<h2>Sparkle Mode<\\/h2>\\r\\n"
"<p>This is the Sparkle mode.<\\/p>\\r\\n"
"<div class=\\\"row my-3\\\">\\r\\n"
" <input id=\\\"sparkleSelectButton\\\" class=\\\"color col mb-2 mx-2 btn btn-lg btn-outline-light\\\" value=\\\"rgb(0,0,0)\\\"><\\/input>\\r\\n"
"<\\/div>\\r\\n");
const char *tabScript = PSTR("var sparkleLastMessage = \\\"\\\"\\r\\n"
}

virtual const char *getTabScript()
{
return PSTR("var sparkleLastMessage = \\\"\\\"\\r\\n"
"var sparkleRed = 0;\\r\\n"
"var sparkleGreen = 0;\\r\\n"
"var sparkleBlue = 0;\\r\\n"
Expand Down Expand Up @@ -159,9 +167,5 @@ public:
" sendMessage(msg)\\r\\n"
" }\\r\\n"
"}\\r\\n");

String htmlJSON = String("{\"Tab\" : {") + "\"Name\": \"" + modeName + "\", \"tabHtml\" : \"" + tabHtml + "\", \"tabScript\" : \"" + tabScript + "\"}}";

_webSocketServer.broadcastTXT(htmlJSON.c_str());
}
};
20 changes: 12 additions & 8 deletions Mode Registry/ModeVisualiser.ino
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,14 @@ public:
settings["HueOffset"] = visualiserHueOffset = settings["HueOffset"] | visualiserHueOffset;
}

virtual void sendWebsiteData(WebSocketsServer &_webSocketServer)
virtual const char *getName()
{
const char *modeName = "Visualiser";
const char *tabHtml = PSTR("<h2>Visualiser Mode<\\/h2>\\r\\n"
return "Visualiser";
}

virtual const char *getTabHtml()
{
return PSTR("<h2>Visualiser Mode<\\/h2>\\r\\n"
"<p> Here you can set the mode to Visualiser. This mode does an FFT on the ADC of the ESP8266 and maps the\\r\\n"
" frequencies\\r\\n"
" to the number of top and bottom LED's. To use this mode, an input source must be present on the ADC such\\r\\n"
Expand Down Expand Up @@ -186,7 +190,11 @@ public:
" has been set<\\/li>\\r\\n"
" <li><b>Hue Offset<\\/b> - The offset hue value from 0 for the start of the rainbow<\\/li>\\r\\n"
"<\\/ul>\\r\\n");
const char *tabScript = PSTR("var visualiserDebunce = Date.now()\\r\\n"
}

virtual const char *getTabScript()
{
return PSTR("var visualiserDebunce = Date.now()\\r\\n"
"var lastVisualiserMessaeg = \\\"\\\"\\r\\n"
"\\r\\n"
"messageEventList.push(handleVisualiserMessage)\\r\\n"
Expand Down Expand Up @@ -306,9 +314,5 @@ public:
" sendMessage(msg)\\r\\n"
" }\\r\\n"
"}\\r\\n");

String htmlJSON = String("{\"Tab\" : {") + "\"Name\": \"" + modeName + "\", \"tabHtml\" : \"" + tabHtml + "\", \"tabScript\" : \"" + tabScript + "\"}}";

_webSocketServer.broadcastTXT(htmlJSON.c_str());
}
};
20 changes: 12 additions & 8 deletions Super_Simple_RGB_WiFi_Lamp/ModeBellCurve.ino
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,24 @@ public:
settings["Blue"] = bellCurveBlue = settings["Blue"] | bellCurveBlue;
}

virtual void sendWebsiteData(WebSocketsServer &_webSocketServer)
virtual const char *getName()
{
const char *modeName = "Bell Curve";
const char *tabHtml = PSTR("<h2>Bell Curve Mode<\\/h2>\\r\\n"
return "Bell Curve";
}

virtual const char *getTabHtml()
{
return PSTR("<h2>Bell Curve Mode<\\/h2>\\r\\n"
"<p>In this mode the lamp will shape the light into a bell curve. This is meant to be more asthetically\\r\\n"
" pleasing than the regular colour mode.<\\/p>\\r\\n"
"<div class=\\\"row my-3\\\">\\r\\n"
" <input id=\\\"bellCurveSelectButton\\\" class=\\\"color col mb-2 mx-2 btn btn-lg btn-outline-light\\\" value=\\\"rgb(0,0,0)\\\">\\r\\n"
"<\\/div>\\r\\n");
const char *tabScript = PSTR("bellCurveDebunce = Date.now()\\r\\n"
}

virtual const char *getTabScript()
{
return PSTR("bellCurveDebunce = Date.now()\\r\\n"
"var bellRed = 0\\r\\n"
"var bellGreen = 0\\r\\n"
"var bellBlue = 0\\r\\n"
Expand Down Expand Up @@ -123,9 +131,5 @@ public:
" }\\r\\n"
" }\\r\\n"
"}\\r\\n");

String htmlJSON = String("{\"Tab\" : {") + "\"Name\": \"" + modeName + "\", \"tabHtml\" : \"" + tabHtml + "\", \"tabScript\" : \"" + tabScript + "\"}}";

_webSocketServer.broadcastTXT(htmlJSON.c_str());
}
};
19 changes: 12 additions & 7 deletions Super_Simple_RGB_WiFi_Lamp/ModeClock.ino
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,14 @@ public:
}
}

virtual void sendWebsiteData(WebSocketsServer &_webSocketServer)
virtual const char *getName()
{
const char *modeName = "Clock";
const char *tabHtml = PSTR("<h2>Clock Mode<\\/h2>\\r\\n"
return "Clock";
}

virtual const char *getTabHtml()
{
return PSTR("<h2>Clock Mode<\\/h2>\\r\\n"
"<p>In this mode the light will display the current time in 12 hr format uisng the\\r\\n"
" top and bottom side of the light. On the top is the current hour, and the bottom is the minute. The left\\r\\n"
" of teh light represents 0 and the right represents either 12hr or 60mins. You can choose the colour of\\r\\n"
Expand All @@ -117,7 +121,11 @@ public:
" <button id=\\\"clockHourColourButton\\\" class=\\\"color col mb-2 mx-2 btn btn-lg btn-outline-light\\\">Hour Colour<\\/button>\\r\\n"
" <button id=\\\"clockMinuteColourButton\\\" class=\\\"color col mb-2 mx-2 btn btn-lg btn-outline-light\\\">Minute Colour<\\/button>\\r\\n"
"<\\/div>\\r\\n");
const char *tabScript = PSTR("clockHourDebunce = Date.now()\\r\\n"
}

virtual const char *getTabScript()
{
return PSTR("clockHourDebunce = Date.now()\\r\\n"
"clockMinDebunce = Date.now()\\r\\n"
"var currentHourRed = 0\\r\\n"
"var currentHourGreen = 0\\r\\n"
Expand Down Expand Up @@ -305,8 +313,5 @@ public:
" }\\r\\n"
" }\\r\\n"
"}\\r\\n");
String htmlJSON = String("{\"Tab\" : {") + "\"Name\": \"" + modeName + "\", \"tabHtml\" : \"" + tabHtml + "\", \"tabScript\" : \"" + tabScript + "\"}}";

_webSocketServer.broadcastTXT(htmlJSON.c_str());
}
};
20 changes: 12 additions & 8 deletions Super_Simple_RGB_WiFi_Lamp/ModeColour.ino
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ public:
settings["Blue"] = colourBlue = settings["Blue"] | colourBlue;
}

virtual void sendWebsiteData(WebSocketsServer &_webSocketServer)
virtual const char *getName()
{
const char *modeName = "Colour";
const char *tabHtml = PSTR("<h2>Colour Mode<\\/h2>\\r\\n"
return "Colour";
}

virtual const char *getTabHtml()
{
return PSTR("<h2>Colour Mode<\\/h2>\\r\\n"
"<p>Here you can set the light to any colour you desire. There are also a couple of buttons for setting the light to different shades of white<\\/p>\\r\\n"
"\\r\\n"
"<div class=\\\"row my-3\\\">\\r\\n"
Expand All @@ -43,7 +47,11 @@ public:
" <button id=\\\"3000Button\\\" class=\\\"col mb-2 mx-2 btn btn-lg btn-outline-light\\\">3000K<\\/button>\\r\\n"
" <button id=\\\"4000Button\\\" class=\\\"col mb-2 mx-2 btn btn-lg btn-outline-light\\\">4000K<\\/button>\\r\\n"
"<\\/div>\\r\\n");
const char *tabScript = PSTR("var currentRed = 0;\\r\\n"
}

virtual const char *getTabScript()
{
return PSTR("var currentRed = 0;\\r\\n"
"var currentGreen = 0;\\r\\n"
"var currentBlue = 0;\\r\\n"
"var colourDebunce = Date.now()\\r\\n"
Expand Down Expand Up @@ -154,9 +162,5 @@ public:
" }\\r\\n"
" }\\r\\n"
"}\\r\\n");

String htmlJSON = String("{\"Tab\" : {") + "\"Name\": \"" + modeName + "\", \"tabHtml\" : \"" + tabHtml + "\", \"tabScript\" : \"" + tabScript + "\"}}";

_webSocketServer.broadcastTXT(htmlJSON.c_str());
}
};
19 changes: 12 additions & 7 deletions Super_Simple_RGB_WiFi_Lamp/ModeNightRider.ino
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,22 @@ public:
{
}

virtual void sendWebsiteData(WebSocketsServer &_webSocketServer)
virtual const char *getName()
{
const char *modeName = "Night Rider";
const char *tabHtml = PSTR("<h2>Night Rider Mode<\\/h2>\\r\\n"
return "Night Rider";
}

virtual const char *getTabHtml()
{
return PSTR("<h2>Night Rider Mode<\\/h2>\\r\\n"
"<p>Knight Rider. A shadowy flight into the dangerous world of a man who does not exist.\\r\\n"
" Michael Knight: a young loner on a crusade to champion the cause of the innocent,\\r\\n"
" the helpless, the powerless, in a world of criminals who operate above the law.<\\/p>\\r\\n");
const char *tabScript = PSTR("messageEventList.push(handleNightRiderMessage)\\r\\n"
}

virtual const char *getTabScript()
{
return PSTR("messageEventList.push(handleNightRiderMessage)\\r\\n"
"\\r\\n"
"function handleNightRiderMessage(jsonMessage) {\\r\\n"
" if (\\\"Night Rider\\\" in jsonMessage) {\\r\\n"
Expand All @@ -53,8 +61,5 @@ public:
" }\\r\\n"
" }\\r\\n"
"}\\r\\n");
String htmlJSON = String("{\"Tab\" : {") + "\"Name\": \"" + modeName + "\", \"tabHtml\" : \"" + tabHtml + "\", \"tabScript\" : \"" + tabScript + "\"}}";

_webSocketServer.broadcastTXT(htmlJSON.c_str());
}
};
18 changes: 11 additions & 7 deletions Super_Simple_RGB_WiFi_Lamp/ModeRainbow.ino
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,14 @@ public:
settings["Brightness"] = rainbowBri = settings["Brightness"] | rainbowBri;
}

virtual void sendWebsiteData(WebSocketsServer &_webSocketServer)
virtual const char *getName()
{
const char *modeName = "Rainbow";
const char *tabHtml = PSTR("<h2>Rainbow Mode<\\/h2>\\r\\n"
return "Rainbow";
}

virtual const char *getTabHtml()
{
return PSTR("<h2>Rainbow Mode<\\/h2>\\r\\n"
"<p>Here you can set the mode to rainbow. This mode produces a rainbow all the way around the light and\\r\\n"
" slowly shifts the colours clockwise. On this page you can set the speed of this as well as the\\r\\n"
" brightness of the light<\\/p>\\r\\n"
Expand All @@ -71,8 +75,11 @@ public:
" <label for=\\\"rainbowSpeed\\\">Rainbow Speed: <span id=\\\"rainbowSpeedLabel\\\">10<\\/span> seconds<\\/label>\\r\\n"
" <input id=\\\"rainbowSpeed\\\" type=\\\"range\\\" min=\\\"0\\\" max=\\\"10\\\" step=\\\"1\\\" value=\\\"10\\\" class=\\\"form-control-range custom-range\\\">\\r\\n"
"<\\/div>\\r\\n");
}

const char *tabScript = PSTR("var rainbowDebunce = Date.now()\\r\\n"
virtual const char *getTabScript()
{
return PSTR("var rainbowDebunce = Date.now()\\r\\n"
"var rainbowLastMessage = \\\"\\\"\\r\\n"
"\\r\\n"
"messageEventList.push(handleRainbowMessage)\\r\\n"
Expand Down Expand Up @@ -171,8 +178,5 @@ public:
" sendMessage(msg)\\r\\n"
" }\\r\\n"
"}\\r\\n");
String htmlJSON = String("{\"Tab\" : {") + "\"Name\": \"" + modeName + "\", \"tabHtml\" : \"" + tabHtml + "\", \"tabScript\" : \"" + tabScript + "\"}}";

_webSocketServer.broadcastTXT(htmlJSON.c_str());
}
};
4 changes: 3 additions & 1 deletion Super_Simple_RGB_WiFi_Lamp/Super_Simple_RGB_WiFi_Lamp.ino
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ class ModeBase
public:
virtual void render();
virtual void applyConfig(JsonVariant& settings);
virtual void sendWebsiteData(WebSocketsServer& _webSocketServer);
virtual const char *getName();
virtual const char *getTabHtml();
virtual const char *getTabScript();
};

std::map<String, ModeBase*> modes;
Expand Down
Loading

0 comments on commit 5aff5ef

Please sign in to comment.