10 | class CircularBuffer
11 | {
12 | private:
13 |
14 | const unsigned int _capacity = LEN;
15 |
16 | //Index of the oldest element
17 | unsigned int _out = 0;
18 | //The next empty index we can write to
19 | unsigned int _nextIn = 0;
20 | T _buffer[LEN];
21 |
22 | public:
23 |
24 | void reset()
25 | {
26 | _nextIn = 0;
27 | _out = 0;
28 | }
29 |
30 | bool write(T value)
31 | {
32 | _buffer[_nextIn] = value;
33 | _nextIn = (_nextIn+1) % LEN;
34 |
35 | //If we've overwritten the first byte then move up the out value to keep overwriting the oldest stuff
36 | if (_nextIn == _out)
37 | _out = (_out+1) % LEN;
38 | }
39 |
40 | bool read(T& value)
41 | {
42 | if (_nextIn == _out)
43 | return false;
44 |
45 | value = _buffer[_out];
46 | _out = (_out+1) % LEN;
47 | return true;
48 | }
49 |
50 | T read()
51 | {
52 | if (_nextIn == _out)
53 | return 0;
54 |
55 | T value = _buffer[_out];
56 | _out = (_out+1) % LEN;
57 |
58 | return value;
59 | }
60 |
61 | unsigned int size()
62 | {
63 | unsigned int len = (_nextIn - _out) & 0x7FFF;
64 | }
65 | };
66 |
67 | #endif
68 |
--------------------------------------------------------------------------------
/src/Pages.ino:
--------------------------------------------------------------------------------
1 | //Pages: Holds pages to serve via HTTP
2 |
3 | String getNextToken(String& s, int& offset)
4 | {
5 | char c;
6 | String result = "";
7 |
8 | do
9 | {
10 | c = s[offset];
11 | ++offset;
12 |
13 | if ((c != 0) && (c != '&') && (c != '?') && (c != ' ') && (c != '\r') && (c != '\n'))
14 | {
15 | result += c;
16 | }
17 | else
18 | {
19 | return result;
20 | }
21 |
22 | } while(offset < s.length());
23 |
24 | return result;
25 | }
26 |
27 | void appendHttp200(String& s)
28 | {
29 | s += F("HTTP/1.1 200 OK\r\n");
30 | s += F("Content-Type: text/html\r\n\r\n");
31 | s += F("\r\n\r\n");
32 | }
33 |
34 | void serve404(WiFiClient& client)
35 | {
36 | String s = F("HTTP/1.1 404 Not Found\r\n");
37 | s += F("Content-Type: text/html\r\n\r\n");
38 | s += F("\r\n\r\n");
39 | s += F("Page not found");
40 | s += F("");
41 |
42 | client.print(s);
43 | delay(1);
44 |
45 | }
46 |
47 | //Wifi setup, requests AP list via AJAX
48 | void serveWifiSetupPage(WiFiClient& client)
49 | {
50 | String s = "";
51 | appendHttp200(s);
52 |
53 | //This thing was automatically generated from html source
54 | s += F("Wifi Setup
\r\nSearching for networks...
\r\n\r\n\r\n");
55 | s += F("");
65 |
66 | client.print(s);
67 | delay(1);
68 |
69 | }
70 |
71 | //AJAX reply with list of APs
72 | void serveWifiApList(WiFiClient& client)
73 | {
74 | String s = "";
75 |
76 | s += F("HTTP/1.1 200 OK\r\n");
77 | s += F("Content-Type: application/json; charset=utf-8\r\n");
78 | s += F("Access-Control-Allow-Origin: *\r\n\r\n");
79 |
80 | s += "[";
81 | int8_t n = WiFi.scanNetworks();
82 | for (int8_t i=0; i 0)
110 | {
111 | Serial1.println(index0);
112 | int index1 = req.indexOf("&pass=", index0);
113 | if (index1 > 0)
114 | {
115 | Serial1.println(index1);
116 | int index2 = req.indexOf(" HTTP/", index1);
117 | if (index2 == -1)
118 | index2 = req.length();
119 | String ssid = req.substring(index0+6, index1);
120 | String pass = req.substring(index1+6, index2);
121 |
122 | Serial1.println(index2);
123 | Serial1.println(ssid);
124 | Serial1.println(pass);
125 |
126 | _settings._wifiSsid = ssid;
127 | _settings._wifiPass = pass;
128 | _settings.save();
129 |
130 | s += F("Set access point to ");
131 | s += ssid;
132 | s += ":";
133 | s += pass;
134 | s += F("
Switching to client mode, this connection has now closed. Hopefully you'll find me again on ");
135 | s += F("
Back
");
136 | s += ssid;
137 |
138 | Serial1.println(s);
139 | requestApMode = WIFI_STA;
140 | clientReconnect = true;
141 | delay(1);
142 | //ESP.restart();
143 | }
144 | }
145 |
146 |
147 | //This thing was automatically generated from html source
148 | s += F("");
149 | s += F("