Skip to content

Commit 40a20db

Browse files
author
Luc
committed
Code optimization / Typo fixes
use more F() macro to save space, fixe several typo issue Thanks @treepleks
1 parent 28891b3 commit 40a20db

File tree

2 files changed

+96
-83
lines changed

2 files changed

+96
-83
lines changed

esp8266/config.cpp

Lines changed: 95 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -31,45 +31,42 @@ bool CONFIG::read_string(int pos, char byte_buffer[], int size_max)
3131
//check if parameters are acceptable
3232
if (size_max==0 || pos+size_max+1 > EEPROM_SIZE || byte_buffer== NULL)return false;
3333
EEPROM.begin(EEPROM_SIZE);
34-
byte b=0;
34+
byte b = 13; // non zero for the while loop below
3535
int i=0;
36-
//read first byte
37-
b = EEPROM.read(pos + i);
38-
byte_buffer[i]=b;
39-
i++;
36+
4037
//read until max size is reached or \0 is found
41-
while (i<size_max+1 && b!=0)
38+
while (i < size_max && b != 0)
4239
{
4340
b = EEPROM.read(pos+i);
4441
byte_buffer[i]=b;
4542
i++;
4643
}
47-
//if it is a string be sure there is an 0 at the end
44+
45+
// Be sure there is a 0 at the end.
4846
if (b!=0)byte_buffer[i-1]=0x00;
4947
EEPROM.end();
48+
5049
return true;
5150
}
5251

5352
bool CONFIG::read_string(int pos, String & sbuffer, int size_max)
5453
{
5554
//check if parameters are acceptable
5655
if (size_max==0 || pos+size_max+1 > EEPROM_SIZE )return false;
57-
byte b=0;
56+
byte b = 13; // non zero for the while loop below
5857
int i=0;
5958
sbuffer="";
59+
6060
EEPROM.begin(EEPROM_SIZE);
61-
//read first byte
62-
b = EEPROM.read(pos + i);
63-
sbuffer+=char(b);
64-
i++;
6561
//read until max size is reached or \0 is found
66-
while (i<size_max+1 && b!=0)
62+
while (i < size_max && b != 0)
6763
{
6864
b = EEPROM.read(pos+i);
6965
sbuffer+=char(b);
7066
i++;
7167
}
7268
EEPROM.end();
69+
7370
return true;
7471
}
7572

@@ -182,161 +179,177 @@ bool CONFIG::reset_config()
182179

183180
void CONFIG::print_config()
184181
{
185-
//use bigest size for buffer
182+
//use biggest size for buffer
186183
char sbuf[MAX_PASSWORD_LENGTH+1];
187184
byte bbuf=0;
188185
int ibuf=0;
189186
if (CONFIG::read_byte(EP_WIFI_MODE, &bbuf ))
190187
{
191-
if (byte(bbuf)==CLIENT_MODE) Serial.println("Mode: Station");
192-
else if (byte(bbuf)==AP_MODE) Serial.println("Mode: Access Point");
193-
else Serial.println("Mode: ???");
188+
Serial.print(F("Mode: "));
189+
if (byte(bbuf) == CLIENT_MODE) Serial.println(F("Station"));
190+
else if (byte(bbuf)==AP_MODE) Serial.println(F("Access Point"));
191+
else Serial.println("???");
194192
}
195-
else Serial.println("Error reading mode");
193+
else Serial.println(F("Error reading mode"));
194+
196195
if (CONFIG::read_string(EP_SSID, sbuf , MAX_SSID_LENGTH))
197196
{
198-
Serial.print("SSID: ");
197+
Serial.print(F("SSID: "));
199198
Serial.println(sbuf);
200199
}
201-
else Serial.println("Error reading SSID");
200+
else Serial.println(F("Error reading SSID"));
202201
//if (CONFIG::read_string(EP_PASSWORD, sbuf , MAX_PASSWORD_LENGTH))Serial.println(sbuf);
203202

204203
if (CONFIG::read_byte(EP_IP_MODE, &bbuf ))
205204
{
206-
if (byte(bbuf)==STATIC_IP_MODE) Serial.println("IP Mode: Static");
207-
else if (byte(bbuf)==DHCP_MODE) Serial.println("IP Mode: DHCP");
208-
else Serial.println("IP mode: ???");
205+
Serial.print(F("IP Mode: "));
206+
if (byte(bbuf)==STATIC_IP_MODE) Serial.println(F("Static"));
207+
else if (byte(bbuf)==DHCP_MODE) Serial.println(F("DHCP"));
208+
else Serial.println(F("???"));
209209
}
210-
else Serial.println("Error reading IP mode");
210+
else Serial.println(F("Error reading IP mode"));
211+
211212
if (CONFIG::read_buffer(EP_IP_VALUE,(byte *)sbuf , IP_LENGTH))
212213
{
213-
Serial.print("IP: ");
214+
Serial.print(F("IP: "));
214215
Serial.println(wifi_config.ip2str((byte *)sbuf));
215216
}
216-
else Serial.println("Error reading IP");
217+
else Serial.println(F("Error reading IP"));
218+
217219
if (CONFIG::read_buffer(EP_MASK_VALUE, (byte *)sbuf , IP_LENGTH))
218220
{
219-
Serial.print("Subnet: ");
221+
Serial.print(F("Subnet: "));
220222
Serial.println(wifi_config.ip2str((byte *)sbuf));
221223
}
222-
else Serial.println("Error reading subnet");
224+
else Serial.println(F("Error reading subnet"));
225+
223226
if (CONFIG::read_buffer(EP_GATEWAY_VALUE, (byte *)sbuf , IP_LENGTH))
224227
{
225-
Serial.print("Gateway : ");
228+
Serial.print(F("Gateway: "));
226229
Serial.println(wifi_config.ip2str((byte *)sbuf));
227230
}
228-
else Serial.println("Error reading gateway");
231+
else Serial.println(F("Error reading gateway"));
232+
229233
if (CONFIG::read_buffer(EP_BAUD_RATE, (byte *)&ibuf , INTEGER_LENGTH))
230234
{
231-
Serial.print("Baud rate : ");
235+
Serial.print(F("Baud rate: "));
232236
Serial.println(ibuf);
233237
}
234-
else Serial.println("Error reading baud rate");
238+
else Serial.println(F("Error reading baud rate"));
239+
235240
if (CONFIG::read_byte(EP_PHY_MODE, &bbuf ))
236241
{
237-
Serial.print("Phy mode : ");
238-
if (byte(bbuf)==PHY_MODE_11B)Serial.println("11b");
239-
else if (byte(bbuf)==PHY_MODE_11G)Serial.println("11g");
240-
else if (byte(bbuf)==PHY_MODE_11N)Serial.println("11n");
241-
else Serial.println("???");
242+
Serial.print(F("Phy mode: "));
243+
if (byte(bbuf)==PHY_MODE_11B) Serial.println(F("11b"));
244+
else if (byte(bbuf)==PHY_MODE_11G) Serial.println(F("11g"));
245+
else if (byte(bbuf)==PHY_MODE_11N) Serial.println(F("11n"));
246+
else Serial.println(F("???"));
242247
}
243-
else Serial.println("Error reading phy mode");
248+
else Serial.println(F("Error reading phy mode"));
249+
244250
if (CONFIG::read_byte(EP_SLEEP_MODE, &bbuf ))
245251
{
246-
Serial.print("Sleep mode : ");
247-
if (byte(bbuf)==NONE_SLEEP_T)Serial.println("None");
248-
else if (byte(bbuf)==LIGHT_SLEEP_T)Serial.println("Light");
249-
else if (byte(bbuf)==MODEM_SLEEP_T)Serial.println("Modem");
250-
else Serial.println("???");
252+
Serial.print(F("Sleep mode: "));
253+
if (byte(bbuf)==NONE_SLEEP_T) Serial.println(F("None"));
254+
else if (byte(bbuf)==LIGHT_SLEEP_T) Serial.println(F("Light"));
255+
else if (byte(bbuf)==MODEM_SLEEP_T) Serial.println(F("Modem"));
256+
else Serial.println(F("???"));
251257
}
252-
else Serial.println("Error reading sleep mode");
258+
else Serial.println(F("Error reading sleep mode"));
259+
253260
if (CONFIG::read_byte(EP_CHANNEL, &bbuf ))
254261
{
255-
Serial.print("Channel : ");
262+
Serial.print(F("Channel: "));
256263
Serial.println(byte(bbuf));
257264
}
258-
else Serial.println("Error reading channel");
265+
else Serial.println(F("Error reading channel"));
259266

260267
if (CONFIG::read_byte(EP_AUTH_TYPE, &bbuf ))
261268
{
262-
Serial.print("Authentification : ");
263-
if (byte(bbuf)==AUTH_OPEN)Serial.println("None");
264-
else if (byte(bbuf)==AUTH_WEP)Serial.println("WEP");
265-
else if (byte(bbuf)==AUTH_WPA_PSK)Serial.println("WPA");
266-
else if (byte(bbuf)==AUTH_WPA2_PSK)Serial.println("WPA2");
267-
else if (byte(bbuf)==AUTH_WPA_WPA2_PSK)Serial.println("WPA/WPA2");
268-
else if (byte(bbuf)==AUTH_MAX)Serial.println("Max");
269-
else Serial.println("???");
269+
Serial.print(F("Authentification: "));
270+
if (byte(bbuf)==AUTH_OPEN) Serial.println(F("None"));
271+
else if (byte(bbuf)==AUTH_WEP) Serial.println(F("WEP"));
272+
else if (byte(bbuf)==AUTH_WPA_PSK) Serial.println(F("WPA"));
273+
else if (byte(bbuf)==AUTH_WPA2_PSK) Serial.println(F("WPA2"));
274+
else if (byte(bbuf)==AUTH_WPA_WPA2_PSK) Serial.println(F("WPA/WPA2"));
275+
else Serial.println(F("???"));
270276
}
271-
else Serial.println("Error reading authentification");
277+
else Serial.println(F("Error reading authentification"));
278+
272279
if (CONFIG::read_byte(EP_SSID_VISIBLE, &bbuf ))
273280
{
274-
Serial.print("SSID visibility: ");
275-
if (bbuf==0)Serial.println("Hidden");
276-
else if (bbuf==1)Serial.println("Visible");
281+
Serial.print(F("SSID visibility: "));
282+
if (bbuf==0) Serial.println(F("Hidden"));
283+
else if (bbuf==1) Serial.println(F("Visible"));
277284
else Serial.println(bbuf);
278285
}
279-
else Serial.println("Error reading SSID visibility");
286+
else Serial.println(F("Error reading SSID visibility"));
287+
280288
if (CONFIG::read_buffer(EP_WEB_PORT, (byte *)&ibuf , INTEGER_LENGTH))
281289
{
282-
Serial.print("Web port: ");
290+
Serial.print(F("Web port: "));
283291
Serial.println(ibuf);
284292
}
285-
else Serial.println("Error reading web port");
293+
else Serial.println(F("Error reading web port"));
294+
286295
if (CONFIG::read_buffer(EP_DATA_PORT, (byte *)&ibuf , INTEGER_LENGTH))
287296
{
288-
Serial.print("Data port: ");
297+
Serial.print(F("Data port: "));
289298
Serial.println(ibuf);
290299
}
291-
else Serial.println("Error reading data port");
300+
else Serial.println(F("Error reading data port"));
301+
292302
if (CONFIG::read_byte(EP_REFRESH_PAGE_TIME, &bbuf ))
293303
{
294-
Serial.print("Web page refresh time: ");
304+
Serial.print(F("Web page refresh time: "));
295305
Serial.println(byte(bbuf));
296306
}
297-
else Serial.println("Error reading refesh page");
307+
else Serial.println(F("Error reading refresh page"));
308+
298309
if (CONFIG::read_string(EP_HOSTNAME, sbuf , MAX_HOSTNAME_LENGTH))
299310
{
300-
Serial.print("Hostname : ");
311+
Serial.print(F("Hostname: "));
301312
Serial.println(sbuf);
302313
}
303-
else Serial.println("Error reading hostname");
314+
else Serial.println(F("Error reading hostname"));
304315

305316
if (CONFIG::read_buffer(EP_XY_FEEDRATE, (byte *)&ibuf , INTEGER_LENGTH))
306317
{
307-
Serial.print("XY feed rate: ");
318+
Serial.print(F("XY feed rate: "));
308319
Serial.println(ibuf);
309320
}
310-
else Serial.println("Error reading XY feed rate");
321+
else Serial.println(F("Error reading XY feed rate"));
322+
311323
if (CONFIG::read_buffer(EP_Z_FEEDRATE, (byte *)&ibuf , INTEGER_LENGTH))
312324
{
313-
Serial.print("Z feed rate: ");
325+
Serial.print(F("Z feed rate: "));
314326
Serial.println(ibuf);
315327
}
316-
else Serial.println("Error reading Z feed rate");
328+
else Serial.println(F("Error reading Z feed rate"));
329+
317330
if (CONFIG::read_buffer(EP_E_FEEDRATE, (byte *)&ibuf , INTEGER_LENGTH))
318331
{
319-
Serial.print("E feed rate: ");
332+
Serial.print(F("E feed rate: "));
320333
Serial.println(ibuf);
321334
}
322-
else Serial.println("Error reading E feed rate");
335+
else Serial.println(F("Error reading E feed rate"));
323336

324-
Serial.print("Captive portal : ");
337+
Serial.print(F("Captive portal: "));
325338
#ifdef CAPTIVE_PORTAL_FEATURE
326-
Serial.println("Enabled");
339+
Serial.println(F("Enabled"));
327340
#else
328-
Serial.println("Disabled");
341+
Serial.println(F("Disabled"));
329342
#endif
330-
Serial.print("SSDP : ");
343+
Serial.print(F("SSDP: "));
331344
#ifdef SSDP_FEATURE
332-
Serial.println("Enabled");
345+
Serial.println(F("Enabled"));
333346
#else
334-
Serial.println("Disabled");
347+
Serial.println(F("Disabled"));
335348
#endif
336-
Serial.print("mDNS : ");
349+
Serial.print(F("mDNS: "));
337350
#ifdef MDNS_FEATURE
338-
Serial.println("Enabled");
351+
Serial.println(F("Enabled"));
339352
#else
340-
Serial.println("Disabled");
353+
Serial.println(F("Disabled"));
341354
#endif
342355
}

esp8266/config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
//comment to disable
2222
//MDNS_FEATURE: this feature allow type the name defined
23-
//in web browser by default : http:\\esp8266.local and connect
23+
//in web browser by default: http:\\esp8266.local and connect
2424
#define MDNS_FEATURE
2525

2626
//SSDD_FEATURE: this feature is a discovery protocol, supported on Windows out of the box

0 commit comments

Comments
 (0)