1
1
#include " board.hpp"
2
2
#include " Drives.hpp"
3
3
#include " wifi_ap.hpp"
4
+ #include " library_extension.hpp"
4
5
#include < assert.h>
5
6
#include < ESP8266WebServer.h>
7
+ #include < atomic>
6
8
7
9
static ESP8266WebServer server (80 );
8
10
@@ -19,28 +21,53 @@ static constexpr std::size_t numberOfPositions = 50;
19
21
static Position positions[numberOfPositions] = { {0 ,0 } };
20
22
static std::size_t positionIndex = 0 ;
21
23
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\" >↑</button></td><td></td></tr>\n "
36
- " <tr><td><button type=\" submit\" name=\" left\" value=\" 5\" title=\" -86,4°\" >↺</button></td><td>🤖</td><td><button type=\" submit\" name=\" right\" value=\" 5\" title=\" +86,4°\" >↻</button></td></tr>\n "
37
- " <tr><td></td><td><button type=\" submit\" name=\" backwards\" value=\" 10\" title=\" -37,70mm\" >↓</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\" >↑</button></td><td></td></tr>\n "
38
+ " <tr><td><button type=\" submit\" name=\" left\" value=\" 5\" title=\" -86,4°\" >↺</button></td><td>🤖</td><td><button type=\" submit\" name=\" right\" value=\" 5\" title=\" +86,4°\" >↻</button></td></tr>\n "
39
+ " <tr><td></td><td><button type=\" submit\" name=\" backwards\" value=\" 10\" title=\" -37,70mm\" >↓</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
+ }
44
71
45
72
static void handleRoot ()
46
73
{
@@ -73,23 +100,9 @@ static void handleRoot()
73
100
newTarget.isTargetNew = true ;
74
101
Serial.printf (" Got right by %u!\n " , newTarget.newRotate );
75
102
}
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
- }
90
103
else
91
104
{
92
- server.send (200 , " text/html" , htmlSource );
105
+ server.send (200 , " text/html" , htmlSourceFrontBuffer. load () );
93
106
}
94
107
digitalWrite (board::debugLed, HIGH);
95
108
}
@@ -151,4 +164,5 @@ void loop()
151
164
}
152
165
newTarget = { };
153
166
}
167
+ updateHtmlSource ();
154
168
}
0 commit comments