Skip to content
This repository was archived by the owner on Jul 25, 2022. It is now read-only.

Commit 3651a65

Browse files
committed
Added position plot to HTML.
HTML source is now rendered in background using buffering.
1 parent ed8b09b commit 3651a65

File tree

3 files changed

+55
-42
lines changed

3 files changed

+55
-42
lines changed

remote-website/robot-control.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
$image = imagecreatetruecolor(1000, 1000);
33
$default_color = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
44

5-
$data_server = "http://kps32.ddns.net/?positions";
6-
$positionString = file_get_contents($data_server);
7-
5+
$positionString = $_GET["positions"];
86

97
$positions = array();
108
if($positionString === FALSE) {

robot-control-src/control-mask.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<head>
44
<title>Robot Control</title>
55
<meta charset="utf-8" />
6-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.6">
77
</head>
88
<body>
99
<main>
@@ -16,6 +16,7 @@
1616
</tbody>
1717
</table>
1818
</form>
19+
<img style="max-width:90vw; max-height:100vh;" src="https://david.hebbeker.info/robot-control.php?positions=%s" />
1920
</main>
2021
</body>
21-
</html>
22+
</html>

robot-control-src/robot-control-src.ino

Lines changed: 51 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#include "board.hpp"
22
#include "Drives.hpp"
33
#include "wifi_ap.hpp"
4+
#include "library_extension.hpp"
45
#include <assert.h>
56
#include <ESP8266WebServer.h>
7+
#include <atomic>
68

79
static ESP8266WebServer server(80);
810

@@ -19,28 +21,53 @@ static constexpr std::size_t numberOfPositions = 50;
1921
static Position positions[numberOfPositions] = { {0,0} };
2022
static std::size_t positionIndex = 0;
2123

22-
constexpr auto htmlSource =
23-
"<!DOCTYPE html>\n"
24-
"<html lang=\"en\">\n"
25-
" <head>\n"
26-
" <title>Robot Control</title>\n"
27-
" <meta charset=\"utf-8\" />\n"
28-
" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.6\">\n"
29-
" </head>\n"
30-
" <body>\n"
31-
" <main>\n"
32-
" <form method=\"post\" action=\"/\">\n"
33-
" <table>\n"
34-
" <tbody align=center valign=middle>\n"
35-
" <tr><td></td><td><button type=\"submit\" name=\"forwards\" value=\"10\" title=\"+37,70mm\">&#8593;</button></td><td></td></tr>\n"
36-
" <tr><td><button type=\"submit\" name=\"left\" value=\"5\" title=\"-86,4&deg;\">&#8634;</button></td><td>&#x1F916;</td><td><button type=\"submit\" name=\"right\" value=\"5\" title=\"+86,4&deg;\">&#8635;</button></td></tr>\n"
37-
" <tr><td></td><td><button type=\"submit\" name=\"backwards\" value=\"10\" title=\"-37,70mm\">&#8595;</button></td><td></td></tr>\n"
38-
" </tbody>\n"
39-
" </table>\n"
40-
" </form>\n"
41-
" </main>\n"
42-
" </body>\n"
43-
"</html>";
24+
static constexpr char htmlSourceTemplate[] =
25+
"<!DOCTYPE html>\n"
26+
"<html lang=\"en\">\n"
27+
" <head>\n"
28+
" <title>Robot Control</title>\n"
29+
" <meta charset=\"utf-8\" />\n"
30+
" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.6\">\n"
31+
" </head>\n"
32+
" <body>\n"
33+
" <main>\n"
34+
" <form method=\"post\" action=\"/\">\n"
35+
" <table>\n"
36+
" <tbody align=center valign=middle>\n"
37+
" <tr><td></td><td><button type=\"submit\" name=\"forwards\" value=\"10\" title=\"+37,70mm\">&#8593;</button></td><td></td></tr>\n"
38+
" <tr><td><button type=\"submit\" name=\"left\" value=\"5\" title=\"-86,4&deg;\">&#8634;</button></td><td>&#x1F916;</td><td><button type=\"submit\" name=\"right\" value=\"5\" title=\"+86,4&deg;\">&#8635;</button></td></tr>\n"
39+
" <tr><td></td><td><button type=\"submit\" name=\"backwards\" value=\"10\" title=\"-37,70mm\">&#8595;</button></td><td></td></tr>\n"
40+
" </tbody>\n"
41+
" </table>\n"
42+
" </form>\n"
43+
" <img style=\"max-width:90vw; max-height:100vh;\" src=\"https://david.hebbeker.info/robot-control.php?positions=%s\" />\n"
44+
" </main>\n"
45+
" </body>\n"
46+
"</html>";
47+
48+
49+
constexpr std::size_t maxCharPerPosition = (5+1)*2; //!< when serializing the position, the number of characters maximum used per position
50+
constexpr std::size_t positionsStringMaxLength = maxCharPerPosition*numberOfPositions+1;
51+
52+
static char htmlSourceBackBufferA[size(htmlSourceTemplate) + positionsStringMaxLength] = {0};
53+
static char htmlSourceBackBufferB[size(htmlSourceTemplate) + positionsStringMaxLength] = {0};
54+
static std::atomic<const char *> htmlSourceFrontBuffer(htmlSourceTemplate);
55+
56+
void updateHtmlSource()
57+
{
58+
char * const backBuffer = (htmlSourceFrontBuffer.load() == htmlSourceBackBufferA) ? htmlSourceBackBufferB : htmlSourceBackBufferA;
59+
char positionStringBuffer[positionsStringMaxLength] = { 0 };
60+
std::size_t charPos = 0;
61+
for(std::size_t i=0; i<positionIndex; i++)
62+
{
63+
const int writtenCharacters = snprintf(&(positionStringBuffer[charPos]), maxCharPerPosition+1, "%i;%i;", positions[i].x, positions[i].y);
64+
assert(writtenCharacters>0);
65+
charPos += writtenCharacters;
66+
}
67+
const int writtenCharacters = snprintf(backBuffer, size(htmlSourceBackBufferA), htmlSourceTemplate, positionStringBuffer);
68+
assert(writtenCharacters>0);
69+
htmlSourceFrontBuffer = backBuffer;
70+
}
4471

4572
static void handleRoot()
4673
{
@@ -73,23 +100,9 @@ static void handleRoot()
73100
newTarget.isTargetNew = true;
74101
Serial.printf("Got right by %u!\n", newTarget.newRotate);
75102
}
76-
if(server.hasArg("positions"))
77-
{
78-
constexpr std::size_t charPerEntry = (5+1)*2;
79-
constexpr std::size_t bufferLength = charPerEntry*numberOfPositions+1;
80-
char positionStringBuffer[bufferLength] = { 0 };
81-
std::size_t charPos = 0;
82-
for(std::size_t i=0; i<positionIndex; i++)
83-
{
84-
const int writtenCharacters = snprintf(&(positionStringBuffer[charPos]), charPerEntry+1, "%i;%i;", positions[i].x, positions[i].y);
85-
assert(writtenCharacters>0);
86-
charPos += writtenCharacters;
87-
}
88-
server.send(200, "text/plain", positionStringBuffer);
89-
}
90103
else
91104
{
92-
server.send(200, "text/html", htmlSource);
105+
server.send(200, "text/html", htmlSourceFrontBuffer.load());
93106
}
94107
digitalWrite(board::debugLed, HIGH);
95108
}
@@ -151,4 +164,5 @@ void loop()
151164
}
152165
newTarget = { };
153166
}
167+
updateHtmlSource();
154168
}

0 commit comments

Comments
 (0)