1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-12 01:53:07 +03:00

Merge pull request #278 from ficeto/esp8266

Make the web server not waste heap
This commit is contained in:
Ivan Grokhotkov
2015-05-20 10:53:06 +03:00
5 changed files with 34 additions and 26 deletions

1
.gitignore vendored
View File

@ -72,3 +72,4 @@ nbproject
build/macosx/esptool-*-osx.zip
build/macosx/dist/osx-xtensa-lx106-elf.tgz
/hardware/esp8266com/esp8266/tools

View File

@ -21,34 +21,13 @@
#include "wiring_private.h"
#include "pins_arduino.h"
extern uint16_t readvdd33(void);
void analogReference(uint8_t mode) {}
extern int __analogRead(uint8_t pin) {
if(pin == 17){
//return system_adc_read();
uint8_t i;
uint16_t data[8];
rom_i2c_writeReg_Mask(0x6C,2,0,5,5,1);
ESP8266_REG(0xD5C) |= (1 << 21);
while ((ESP8266_REG(0xD50) & (7 << 24)) > 0);
ESP8266_REG(0xD50) &= ~(1 << 1);
ESP8266_REG(0xD50) |= (1 << 1);
delayMicroseconds(2);
while ((ESP8266_REG(0xD50) & (7 << 24)) > 0);
read_sar_dout(data);
rom_i2c_writeReg_Mask(0x6C,2,0,5,5,1);
while ((ESP8266_REG(0xD50) & (7 << 24)) > 0);
ESP8266_REG(0xD5C) &= ~(1 << 21);
ESP8266_REG(0xD60) |= (1 << 0);
ESP8266_REG(0xD60) &= ~(1 << 0);
uint16_t tout = 0;
for (i = 0; i < 8; i++) tout += data[i];
return tout >> 4;//tout is 10 bits fraction
return readvdd33() >> 2; // readvdd33 is 12 bit
}
return digitalRead(pin) * 1023;
}

View File

@ -26,6 +26,7 @@ void ICACHE_RAM_ATTR hexdump(uint8_t *mem, uint32_t len, uint8_t cols) {
for(uint32_t i = 0; i < len; i++) {
if(i % cols == 0) {
os_printf("\n[0x%08X] 0x%08X: ", mem, i);
yield();
}
os_printf("%02X ", *mem);
mem++;

View File

@ -101,7 +101,8 @@ void ESP8266WebServer::handleClient()
#endif
// Wait for data from client to become available
while(client.connected() && !client.available()){
uint16_t maxWait = HTTP_MAX_DATA_WAIT;
while(client.connected() && !client.available() && maxWait--){
delay(1);
}
@ -136,7 +137,12 @@ void ESP8266WebServer::send(int code, const char* content_type, String content)
if (!content_type)
content_type = "text/html";
String len(content.length());
sendHeader("Content-Type", content_type, true);
sendHeader("Content-Length", len.c_str());
sendHeader("Connection", "close");
sendHeader("Access-Control-Allow-Origin", "*");
response += _responseHeaders;
response += "\r\n";
@ -145,6 +151,14 @@ void ESP8266WebServer::send(int code, const char* content_type, String content)
sendContent(response);
}
void ESP8266WebServer::send(int code, char* content_type, String content) {
send(code, (const char*)content_type, content);
}
void ESP8266WebServer::send(int code, String content_type, String content) {
send(code, (const char*)content_type.c_str(), content);
}
void ESP8266WebServer::sendContent(String content) {
size_t size_to_send = content.length();
size_t size_sent = 0;
@ -158,6 +172,10 @@ void ESP8266WebServer::sendContent(String content) {
break;
}
}
uint16_t maxWait = HTTP_MAX_CLOSE_WAIT;
while(_currentClient.connected() && maxWait--) {
delay(1);
}
}
String ESP8266WebServer::arg(const char* name) {

View File

@ -31,6 +31,8 @@ enum HTTPUploadStatus { UPLOAD_FILE_START, UPLOAD_FILE_WRITE, UPLOAD_FILE_END };
#define HTTP_DOWNLOAD_UNIT_SIZE 1460
#define HTTP_UPLOAD_BUFLEN 2048
#define HTTP_MAX_DATA_WAIT 1000 //ms to wait for the client to send the request
#define HTTP_MAX_CLOSE_WAIT 2000 //ms to wait for the client to close the connection
typedef struct {
HTTPUploadStatus status;
@ -73,6 +75,8 @@ public:
// content_type - HTTP content type, like "text/plain" or "image/png"
// content - actual content body
void send(int code, const char* content_type = NULL, String content = String(""));
void send(int code, char* content_type, String content);
void send(int code, String content_type, String content);
void sendHeader(String name, String value, bool first = false);
void sendContent(String content);
@ -87,7 +91,12 @@ template<typename T> size_t streamFile(T &file, String contentType){
head += "\r\n\r\n";
_currentClient.print(head);
head = String();
return _currentClient.write(file, HTTP_DOWNLOAD_UNIT_SIZE);
size_t res = _currentClient.write(file, HTTP_DOWNLOAD_UNIT_SIZE);
uint16_t maxWait = HTTP_MAX_CLOSE_WAIT;
while(_currentClient.connected() && maxWait--) {
delay(1);
}
return res;
}
protected: