switching to faster comms
parent
01855c6b66
commit
069f0c405c
104
HansonServo.ino
104
HansonServo.ino
|
|
@ -15,6 +15,7 @@
|
||||||
#define CMD_DELETE_FILE 0x04
|
#define CMD_DELETE_FILE 0x04
|
||||||
#define CMD_LOAD_FILE_CHUNK 0x05
|
#define CMD_LOAD_FILE_CHUNK 0x05
|
||||||
|
|
||||||
|
|
||||||
#define MAX_ANIMATION_SIZE (16 + MAX_FRAMES * NUM_CHANNELS * 2 + 512) // generous buffer
|
#define MAX_ANIMATION_SIZE (16 + MAX_FRAMES * NUM_CHANNELS * 2 + 512) // generous buffer
|
||||||
uint8_t animationBuffer[MAX_ANIMATION_SIZE];
|
uint8_t animationBuffer[MAX_ANIMATION_SIZE];
|
||||||
size_t receivedSize = 0;
|
size_t receivedSize = 0;
|
||||||
|
|
@ -132,50 +133,8 @@ void loop() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void HandleSerialRequests() {
|
|
||||||
if (Serial.available() >= 4) {
|
|
||||||
if (Serial.read() == HEADER1 && Serial.read() == HEADER2) {
|
|
||||||
uint8_t command = Serial.read();
|
|
||||||
uint8_t length = Serial.read();
|
|
||||||
|
|
||||||
String payload = "";
|
|
||||||
for (int i = 0; i < length; i++) {
|
|
||||||
while (!Serial.available())
|
|
||||||
;
|
|
||||||
payload += (char)Serial.read();
|
|
||||||
}
|
|
||||||
|
|
||||||
handleCommand(command, payload, length);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void handleCommand(uint8_t command, const String& payload, uint8_t length) {
|
|
||||||
switch (command) {
|
|
||||||
case CMD_ID_REQUEST:
|
|
||||||
sendIdPacket();
|
|
||||||
break;
|
|
||||||
|
|
||||||
case CMD_FILE_LIST:
|
|
||||||
sendFileList();
|
|
||||||
break;
|
|
||||||
|
|
||||||
case CMD_LOAD_FILE:
|
|
||||||
sendFileContent(payload);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case CMD_DELETE_FILE:
|
|
||||||
deleteFile(payload);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case CMD_LOAD_FILE_CHUNK:
|
|
||||||
handleAnimationChunk(payload, length);
|
|
||||||
break;
|
|
||||||
|
|
||||||
// default:
|
|
||||||
// Serial.println("{\"error\":\"Unknown command\"}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
void sendOkResponse(uint8_t command, const char* note = "chunk received") {
|
void sendOkResponse(uint8_t command, const char* note = "chunk received") {
|
||||||
String payload = String("{\"status\":\"ok\",\"note\":\"") + note + "\"}";
|
String payload = String("{\"status\":\"ok\",\"note\":\"") + note + "\"}";
|
||||||
|
|
||||||
|
|
@ -195,15 +154,66 @@ void sendOkResponse(uint8_t command, const char* note = "chunk received") {
|
||||||
Serial.write(checksum);
|
Serial.write(checksum);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void HandleSerialRequests() {
|
||||||
|
if (Serial.available() >= 5) {
|
||||||
|
if (Serial.read() == HEADER1 && Serial.read() == HEADER2) {
|
||||||
|
uint8_t command = Serial.read();
|
||||||
|
uint8_t lengthHigh = Serial.read();
|
||||||
|
uint8_t lengthLow = Serial.read();
|
||||||
|
uint16_t payloadLength = (lengthHigh << 8) | lengthLow;
|
||||||
|
|
||||||
void handleAnimationChunk(const String& payload, uint8_t length) {
|
while (Serial.available() < payloadLength)
|
||||||
|
;
|
||||||
|
|
||||||
|
uint8_t payload[payloadLength];
|
||||||
|
for (int i = 0; i < payloadLength; i++) {
|
||||||
|
payload[i] = Serial.read();
|
||||||
|
}
|
||||||
|
|
||||||
|
sendOkResponse(CMD_LOAD_FILE_CHUNK, String(command).c_str());
|
||||||
|
handleCommand(command, payload, payloadLength);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void handleCommand(uint8_t command, const uint8_t* payload, uint16_t length) {
|
||||||
|
sendOkResponse(CMD_LOAD_FILE_CHUNK, String(command).c_str());
|
||||||
|
switch (command) {
|
||||||
|
case CMD_ID_REQUEST:
|
||||||
|
sendIdPacket();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case CMD_FILE_LIST:
|
||||||
|
sendFileList();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case CMD_LOAD_FILE: {
|
||||||
|
String fileName = String((const char*)payload, length);
|
||||||
|
sendFileContent(fileName);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case CMD_DELETE_FILE: {
|
||||||
|
String fileName = String((const char*)payload, length);
|
||||||
|
deleteFile(fileName);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case CMD_LOAD_FILE_CHUNK:
|
||||||
|
handleAnimationChunk(payload, length);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void handleAnimationChunk(const uint8_t* data, uint16_t length) {
|
||||||
if (length < 4) {
|
if (length < 4) {
|
||||||
sendOkResponse(CMD_LOAD_FILE_CHUNK, "fail: too short");
|
sendOkResponse(CMD_LOAD_FILE_CHUNK, "fail: too short");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const uint8_t* data = (const uint8_t*)payload.c_str();
|
|
||||||
|
|
||||||
uint16_t offset = (data[0] << 8) | data[1];
|
uint16_t offset = (data[0] << 8) | data[1];
|
||||||
uint16_t totalSize = (data[2] << 8) | data[3];
|
uint16_t totalSize = (data[2] << 8) | data[3];
|
||||||
const uint8_t* chunk = &data[4];
|
const uint8_t* chunk = &data[4];
|
||||||
|
|
@ -231,7 +241,8 @@ void handleAnimationChunk(const String& payload, uint8_t length) {
|
||||||
currentAnimation.saveToFile("savey.anim");
|
currentAnimation.saveToFile("savey.anim");
|
||||||
sendOkResponse(CMD_LOAD_FILE_CHUNK, "final chunk");
|
sendOkResponse(CMD_LOAD_FILE_CHUNK, "final chunk");
|
||||||
} else {
|
} else {
|
||||||
sendOkResponse(CMD_LOAD_FILE_CHUNK, "chunk received");
|
String note = "receivedSize=" + String(receivedSize);
|
||||||
|
sendOkResponse(CMD_LOAD_FILE_CHUNK, note.c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -239,6 +250,7 @@ void handleAnimationChunk(const String& payload, uint8_t length) {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void loadAnimationFromBuffer(const uint8_t* buffer, size_t length) {
|
void loadAnimationFromBuffer(const uint8_t* buffer, size_t length) {
|
||||||
currentAnimation.clear();
|
currentAnimation.clear();
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue