2016-12-30 23:38:51 +00:00
|
|
|
/*
|
2018-04-11 21:50:35 +00:00
|
|
|
* Utility for SPIFFS filesystem & Serial console
|
2016-12-30 23:38:51 +00:00
|
|
|
*/
|
2018-04-11 21:50:35 +00:00
|
|
|
void handleSerial()
|
|
|
|
{
|
2018-11-09 16:00:36 +00:00
|
|
|
if (Serial.available() > 0) //support for Adalight protocol to high-speed control LEDs over serial
|
2018-04-11 21:50:35 +00:00
|
|
|
{
|
2018-11-09 16:00:36 +00:00
|
|
|
if (!Serial.find("Ada")) return;
|
|
|
|
|
|
|
|
if (!realtimeActive && bri == 0) strip.setBrightness(briLast);
|
|
|
|
arlsLock(realtimeTimeoutMs);
|
|
|
|
|
2019-03-07 22:22:52 +00:00
|
|
|
yield();
|
2018-11-09 16:00:36 +00:00
|
|
|
byte hi = Serial.read();
|
|
|
|
byte ledc = Serial.read();
|
|
|
|
byte chk = Serial.read();
|
|
|
|
if(chk != (hi ^ ledc ^ 0x55)) return;
|
|
|
|
if (ledCount < ledc) ledc = ledCount;
|
|
|
|
|
|
|
|
byte sc[3]; int t =-1; int to = 0;
|
|
|
|
for (int i=0; i < ledc; i++)
|
2018-04-11 21:50:35 +00:00
|
|
|
{
|
2018-11-09 16:00:36 +00:00
|
|
|
for (byte j=0; j<3; j++)
|
2018-04-11 21:50:35 +00:00
|
|
|
{
|
2018-11-09 16:00:36 +00:00
|
|
|
while (Serial.peek()<0) //no data yet available
|
2018-04-11 21:50:35 +00:00
|
|
|
{
|
2019-03-07 22:22:52 +00:00
|
|
|
yield();
|
2018-11-09 16:00:36 +00:00
|
|
|
to++;
|
2019-03-07 22:22:52 +00:00
|
|
|
if (to>15) {strip.show(); return;} //unexpected end of transmission
|
2018-04-11 21:50:35 +00:00
|
|
|
}
|
2018-11-09 16:00:36 +00:00
|
|
|
to = 0;
|
|
|
|
sc[j] = Serial.read();
|
2018-04-11 21:50:35 +00:00
|
|
|
}
|
2018-11-09 16:00:36 +00:00
|
|
|
setRealtimePixel(i,sc[0],sc[1],sc[2],0);
|
2018-04-11 21:50:35 +00:00
|
|
|
}
|
2018-11-09 16:00:36 +00:00
|
|
|
strip.show();
|
2018-04-11 21:50:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-08 19:00:06 +00:00
|
|
|
#ifdef USEFS
|
2016-12-30 23:38:51 +00:00
|
|
|
|
2016-11-19 18:39:17 +00:00
|
|
|
String formatBytes(size_t bytes){
|
|
|
|
if (bytes < 1024){
|
|
|
|
return String(bytes)+"B";
|
|
|
|
} else if(bytes < (1024 * 1024)){
|
|
|
|
return String(bytes/1024.0)+"KB";
|
|
|
|
} else if(bytes < (1024 * 1024 * 1024)){
|
|
|
|
return String(bytes/1024.0/1024.0)+"MB";
|
|
|
|
} else {
|
|
|
|
return String(bytes/1024.0/1024.0/1024.0)+"GB";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String getContentType(String filename){
|
2019-02-14 16:25:41 +00:00
|
|
|
if(server->hasArg("download")) return "application/octet-stream";
|
2016-11-19 18:39:17 +00:00
|
|
|
else if(filename.endsWith(".htm")) return "text/html";
|
|
|
|
else if(filename.endsWith(".html")) return "text/html";
|
|
|
|
else if(filename.endsWith(".css")) return "text/css";
|
|
|
|
else if(filename.endsWith(".js")) return "application/javascript";
|
|
|
|
else if(filename.endsWith(".png")) return "image/png";
|
|
|
|
else if(filename.endsWith(".gif")) return "image/gif";
|
|
|
|
else if(filename.endsWith(".jpg")) return "image/jpeg";
|
|
|
|
else if(filename.endsWith(".ico")) return "image/x-icon";
|
|
|
|
else if(filename.endsWith(".xml")) return "text/xml";
|
|
|
|
else if(filename.endsWith(".pdf")) return "application/x-pdf";
|
|
|
|
else if(filename.endsWith(".zip")) return "application/x-zip";
|
|
|
|
else if(filename.endsWith(".gz")) return "application/x-gzip";
|
|
|
|
return "text/plain";
|
|
|
|
}
|
|
|
|
|
|
|
|
bool handleFileRead(String path){
|
2017-01-14 23:24:28 +00:00
|
|
|
DEBUG_PRINTLN("handleFileRead: " + path);
|
2016-11-19 18:39:17 +00:00
|
|
|
if(path.endsWith("/")) path += "index.htm";
|
|
|
|
String contentType = getContentType(path);
|
|
|
|
String pathWithGz = path + ".gz";
|
|
|
|
if(SPIFFS.exists(pathWithGz) || SPIFFS.exists(path)){
|
|
|
|
if(SPIFFS.exists(pathWithGz))
|
|
|
|
path += ".gz";
|
|
|
|
File file = SPIFFS.open(path, "r");
|
2019-02-14 16:25:41 +00:00
|
|
|
size_t sent = server->streamFile(file, contentType);
|
2016-11-19 18:39:17 +00:00
|
|
|
file.close();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void handleFileUpload(){
|
2019-02-14 16:25:41 +00:00
|
|
|
if(server->uri() != "/edit") return;
|
|
|
|
HTTPUpload& upload = server->upload();
|
2016-11-19 18:39:17 +00:00
|
|
|
if(upload.status == UPLOAD_FILE_START){
|
|
|
|
String filename = upload.filename;
|
|
|
|
if(!filename.startsWith("/")) filename = "/"+filename;
|
2017-01-14 23:24:28 +00:00
|
|
|
DEBUG_PRINT("handleFileUpload Name: "); DEBUG_PRINTLN(filename);
|
2016-11-19 18:39:17 +00:00
|
|
|
fsUploadFile = SPIFFS.open(filename, "w");
|
|
|
|
filename = String();
|
|
|
|
} else if(upload.status == UPLOAD_FILE_WRITE){
|
2017-01-14 23:24:28 +00:00
|
|
|
//DEBUG_PRINT("handleFileUpload Data: "); DEBUG_PRINTLN(upload.currentSize);
|
2016-11-19 18:39:17 +00:00
|
|
|
if(fsUploadFile)
|
|
|
|
fsUploadFile.write(upload.buf, upload.currentSize);
|
|
|
|
} else if(upload.status == UPLOAD_FILE_END){
|
|
|
|
if(fsUploadFile)
|
|
|
|
fsUploadFile.close();
|
2017-01-14 23:24:28 +00:00
|
|
|
DEBUG_PRINT("handleFileUpload Size: "); DEBUG_PRINTLN(upload.totalSize);
|
2016-11-19 18:39:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void handleFileDelete(){
|
2019-02-14 16:25:41 +00:00
|
|
|
if(server->args() == 0) return server->send(500, "text/plain", "BAD ARGS");
|
|
|
|
String path = server->arg(0);
|
2017-01-14 23:24:28 +00:00
|
|
|
DEBUG_PRINTLN("handleFileDelete: " + path);
|
2016-11-19 18:39:17 +00:00
|
|
|
if(path == "/")
|
2019-02-14 16:25:41 +00:00
|
|
|
return server->send(500, "text/plain", "BAD PATH");
|
2016-11-19 18:39:17 +00:00
|
|
|
if(!SPIFFS.exists(path))
|
2019-02-14 16:25:41 +00:00
|
|
|
return server->send(404, "text/plain", "FileNotFound");
|
2016-11-19 18:39:17 +00:00
|
|
|
SPIFFS.remove(path);
|
2019-02-14 16:25:41 +00:00
|
|
|
server->send(200, "text/plain", "");
|
2016-11-19 18:39:17 +00:00
|
|
|
path = String();
|
|
|
|
}
|
|
|
|
|
2016-11-26 18:34:05 +00:00
|
|
|
void handleFileList() {
|
2019-02-14 16:25:41 +00:00
|
|
|
if(!server->hasArg("dir")) {server->send(500, "text/plain", "BAD ARGS"); return;}
|
2016-11-26 18:34:05 +00:00
|
|
|
|
2019-02-14 16:25:41 +00:00
|
|
|
String path = server->arg("dir");
|
2017-01-14 23:24:28 +00:00
|
|
|
DEBUG_PRINTLN("handleFileList: " + path);
|
2016-11-26 18:34:05 +00:00
|
|
|
Dir dir = SPIFFS.openDir(path);
|
|
|
|
path = String();
|
|
|
|
|
|
|
|
String output = "[";
|
|
|
|
while(dir.next()){
|
|
|
|
File entry = dir.openFile("r");
|
|
|
|
if (output != "[") output += ',';
|
|
|
|
bool isDir = false;
|
|
|
|
output += "{\"type\":\"";
|
|
|
|
output += (isDir)?"dir":"file";
|
|
|
|
output += "\",\"name\":\"";
|
|
|
|
output += String(entry.name()).substring(1);
|
|
|
|
output += "\"}";
|
|
|
|
entry.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
output += "]";
|
2019-02-14 16:25:41 +00:00
|
|
|
server->send(200, "text/json", output);
|
2016-11-26 18:34:05 +00:00
|
|
|
}
|
|
|
|
|
2016-11-19 18:39:17 +00:00
|
|
|
void handleFileCreate(){
|
2019-02-14 16:25:41 +00:00
|
|
|
if(server->args() == 0)
|
|
|
|
return server->send(500, "text/plain", "BAD ARGS");
|
|
|
|
String path = server->arg(0);
|
2017-01-14 23:24:28 +00:00
|
|
|
DEBUG_PRINTLN("handleFileCreate: " + path);
|
2016-11-19 18:39:17 +00:00
|
|
|
if(path == "/")
|
2019-02-14 16:25:41 +00:00
|
|
|
return server->send(500, "text/plain", "BAD PATH");
|
2016-11-19 18:39:17 +00:00
|
|
|
if(SPIFFS.exists(path))
|
2019-02-14 16:25:41 +00:00
|
|
|
return server->send(500, "text/plain", "FILE EXISTS");
|
2016-11-19 18:39:17 +00:00
|
|
|
File file = SPIFFS.open(path, "w");
|
|
|
|
if(file)
|
|
|
|
file.close();
|
|
|
|
else
|
2019-02-14 16:25:41 +00:00
|
|
|
return server->send(500, "text/plain", "CREATE FAILED");
|
|
|
|
server->send(200, "text/plain", "");
|
2016-11-19 18:39:17 +00:00
|
|
|
path = String();
|
|
|
|
}
|
2017-05-08 19:00:06 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
bool handleFileRead(String path){return false;}
|
|
|
|
#endif
|