2016-12-31 00:38:51 +01:00
|
|
|
/*
|
2018-04-11 23:50:35 +02:00
|
|
|
* Utility for SPIFFS filesystem & Serial console
|
2016-12-31 00:38:51 +01:00
|
|
|
*/
|
2018-04-11 23:50:35 +02:00
|
|
|
void handleSerial()
|
|
|
|
{
|
|
|
|
if (Serial.available() > 0) //support for Adalight protocol to high-speed control LEDs over serial (gamma correction done by PC)
|
|
|
|
{
|
|
|
|
if (Serial.find("Ada"))
|
|
|
|
{
|
2018-09-15 17:29:01 +02:00
|
|
|
if (!realtimeActive){
|
2018-04-11 23:50:35 +02:00
|
|
|
if (bri == 0) strip.setBrightness(briLast);
|
|
|
|
strip.setRange(0, ledCount-1, 0);
|
|
|
|
strip.setMode(0);
|
|
|
|
}
|
2018-09-15 17:29:01 +02:00
|
|
|
arlsLock(realtimeTimeoutMs);
|
2018-04-11 23:50:35 +02:00
|
|
|
delay(1);
|
|
|
|
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++)
|
|
|
|
{
|
|
|
|
for (byte j=0; j<3; j++)
|
|
|
|
{
|
|
|
|
while (Serial.peek()<0) //no data yet available
|
|
|
|
{
|
|
|
|
delay(1);
|
|
|
|
to++;
|
|
|
|
if (to>5) {strip.show(); return;} //unexpected end of transmission
|
|
|
|
}
|
|
|
|
to = 0;
|
|
|
|
sc[j] = Serial.read();
|
|
|
|
}
|
2018-08-11 22:57:13 +02:00
|
|
|
setRealtimePixel(i,sc[0],sc[1],sc[2],0);
|
2018-04-11 23:50:35 +02:00
|
|
|
}
|
|
|
|
strip.show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-08 21:00:06 +02:00
|
|
|
#ifdef USEFS
|
2016-12-31 00:38:51 +01:00
|
|
|
|
2016-11-19 19:39:17 +01: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){
|
|
|
|
if(server.hasArg("download")) return "application/octet-stream";
|
|
|
|
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-15 00:24:28 +01:00
|
|
|
DEBUG_PRINTLN("handleFileRead: " + path);
|
2016-11-19 19:39:17 +01: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");
|
|
|
|
size_t sent = server.streamFile(file, contentType);
|
|
|
|
file.close();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void handleFileUpload(){
|
|
|
|
if(server.uri() != "/edit") return;
|
|
|
|
HTTPUpload& upload = server.upload();
|
|
|
|
if(upload.status == UPLOAD_FILE_START){
|
|
|
|
String filename = upload.filename;
|
|
|
|
if(!filename.startsWith("/")) filename = "/"+filename;
|
2017-01-15 00:24:28 +01:00
|
|
|
DEBUG_PRINT("handleFileUpload Name: "); DEBUG_PRINTLN(filename);
|
2016-11-19 19:39:17 +01:00
|
|
|
fsUploadFile = SPIFFS.open(filename, "w");
|
|
|
|
filename = String();
|
|
|
|
} else if(upload.status == UPLOAD_FILE_WRITE){
|
2017-01-15 00:24:28 +01:00
|
|
|
//DEBUG_PRINT("handleFileUpload Data: "); DEBUG_PRINTLN(upload.currentSize);
|
2016-11-19 19:39:17 +01:00
|
|
|
if(fsUploadFile)
|
|
|
|
fsUploadFile.write(upload.buf, upload.currentSize);
|
|
|
|
} else if(upload.status == UPLOAD_FILE_END){
|
|
|
|
if(fsUploadFile)
|
|
|
|
fsUploadFile.close();
|
2017-01-15 00:24:28 +01:00
|
|
|
DEBUG_PRINT("handleFileUpload Size: "); DEBUG_PRINTLN(upload.totalSize);
|
2016-11-19 19:39:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void handleFileDelete(){
|
|
|
|
if(server.args() == 0) return server.send(500, "text/plain", "BAD ARGS");
|
|
|
|
String path = server.arg(0);
|
2017-01-15 00:24:28 +01:00
|
|
|
DEBUG_PRINTLN("handleFileDelete: " + path);
|
2016-11-19 19:39:17 +01:00
|
|
|
if(path == "/")
|
|
|
|
return server.send(500, "text/plain", "BAD PATH");
|
|
|
|
if(!SPIFFS.exists(path))
|
|
|
|
return server.send(404, "text/plain", "FileNotFound");
|
|
|
|
SPIFFS.remove(path);
|
|
|
|
server.send(200, "text/plain", "");
|
|
|
|
path = String();
|
|
|
|
}
|
|
|
|
|
2016-11-26 19:34:05 +01:00
|
|
|
void handleFileList() {
|
|
|
|
if(!server.hasArg("dir")) {server.send(500, "text/plain", "BAD ARGS"); return;}
|
|
|
|
|
|
|
|
String path = server.arg("dir");
|
2017-01-15 00:24:28 +01:00
|
|
|
DEBUG_PRINTLN("handleFileList: " + path);
|
2016-11-26 19:34:05 +01: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 += "]";
|
|
|
|
server.send(200, "text/json", output);
|
|
|
|
}
|
|
|
|
|
2016-11-19 19:39:17 +01:00
|
|
|
void handleFileCreate(){
|
|
|
|
if(server.args() == 0)
|
|
|
|
return server.send(500, "text/plain", "BAD ARGS");
|
|
|
|
String path = server.arg(0);
|
2017-01-15 00:24:28 +01:00
|
|
|
DEBUG_PRINTLN("handleFileCreate: " + path);
|
2016-11-19 19:39:17 +01:00
|
|
|
if(path == "/")
|
|
|
|
return server.send(500, "text/plain", "BAD PATH");
|
|
|
|
if(SPIFFS.exists(path))
|
|
|
|
return server.send(500, "text/plain", "FILE EXISTS");
|
|
|
|
File file = SPIFFS.open(path, "w");
|
|
|
|
if(file)
|
|
|
|
file.close();
|
|
|
|
else
|
|
|
|
return server.send(500, "text/plain", "CREATE FAILED");
|
|
|
|
server.send(200, "text/plain", "");
|
|
|
|
path = String();
|
|
|
|
}
|
2017-05-08 21:00:06 +02:00
|
|
|
|
|
|
|
#else
|
|
|
|
bool handleFileRead(String path){return false;}
|
|
|
|
#endif
|