WLED/wled00/wled04_file.ino

122 lines
3.6 KiB
Arduino
Raw Normal View History

/*
* Utility for SPIFFS filesystem & Serial console
*/
2019-11-26 17:49:56 +01:00
enum class AdaState {
Header_A,
Header_d,
Header_a,
Header_CountHi,
Header_CountLo,
Header_CountCheck,
Data_Red,
Data_Green,
Data_Blue
};
void handleSerial()
{
2019-11-28 20:13:52 +01:00
#ifdef WLED_ENABLE_ADALIGHT
2019-11-26 17:49:56 +01:00
static auto state = AdaState::Header_A;
2019-11-28 19:25:04 +01:00
static uint16_t count = 0;
static uint16_t pixel = 0;
2019-11-26 17:49:56 +01:00
static byte check = 0x00;
static byte red = 0x00;
static byte green = 0x00;
2019-11-28 19:25:04 +01:00
2019-11-26 17:49:56 +01:00
while (Serial.available() > 0)
{
yield();
2019-11-26 17:49:56 +01:00
byte next = Serial.read();
switch (state) {
case AdaState::Header_A:
if (next == 'A') state = AdaState::Header_d;
break;
case AdaState::Header_d:
if (next == 'd') state = AdaState::Header_a;
else state = AdaState::Header_A;
break;
case AdaState::Header_a:
if (next == 'a') state = AdaState::Header_CountHi;
else state = AdaState::Header_A;
break;
case AdaState::Header_CountHi:
pixel = 0;
count = next * 0x100;
check = next;
state = AdaState::Header_CountLo;
break;
case AdaState::Header_CountLo:
count += next + 1;
check = check ^ next ^ 0x55;
state = AdaState::Header_CountCheck;
break;
case AdaState::Header_CountCheck:
if (check == next) state = AdaState::Data_Red;
else state = AdaState::Header_A;
break;
case AdaState::Data_Red:
red = next;
state = AdaState::Data_Green;
break;
case AdaState::Data_Green:
green = next;
state = AdaState::Data_Blue;
break;
case AdaState::Data_Blue:
2019-11-28 19:25:04 +01:00
byte blue = next;
2019-11-26 17:49:56 +01:00
setRealtimePixel(pixel++, red, green, blue, 0);
if (--count > 0) state = AdaState::Data_Red;
else {
2020-02-09 19:10:29 +01:00
if (!realtimeMode && bri == 0) strip.setBrightness(briLast);
arlsLock(realtimeTimeoutMs, REALTIME_MODE_ADALIGHT);
strip.show();
state = AdaState::Header_A;
}
2019-11-26 17:49:56 +01:00
break;
}
2019-11-28 19:25:04 +01:00
}
2019-11-28 20:13:52 +01:00
#endif
}
2019-03-16 02:09:37 +01:00
#if !defined WLED_DISABLE_FILESYSTEM && defined WLED_ENABLE_FS_SERVING
//Un-comment any file types you need
String getContentType(AsyncWebServerRequest* request, String filename){
if(request->hasArg("download")) return "application/octet-stream";
else if(filename.endsWith(".htm")) return "text/html";
else if(filename.endsWith(".html")) return "text/html";
2019-03-16 02:09:37 +01:00
// else if(filename.endsWith(".css")) return "text/css";
// else if(filename.endsWith(".js")) return "application/javascript";
else if(filename.endsWith(".json")) return "application/json";
else if(filename.endsWith(".png")) return "image/png";
2019-03-16 02:09:37 +01:00
// 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";
2019-03-16 02:09:37 +01:00
// 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";
}
2019-03-16 02:09:37 +01:00
bool handleFileRead(AsyncWebServerRequest* request, String path){
DEBUG_PRINTLN("FileRead: " + path);
if(path.endsWith("/")) path += "index.htm";
2019-03-16 02:09:37 +01:00
String contentType = getContentType(request, path);
String pathWithGz = path + ".gz";
2019-03-16 02:09:37 +01:00
if(SPIFFS.exists(pathWithGz)){
request->send(SPIFFS, pathWithGz, contentType);
return true;
}
2019-03-16 02:09:37 +01:00
if(SPIFFS.exists(path)) {
request->send(SPIFFS, path, contentType);
return true;
}
2019-03-16 02:09:37 +01:00
return false;
}
#else
2019-03-16 02:09:37 +01:00
bool handleFileRead(AsyncWebServerRequest*, String path){return false;}
#endif