Merge branch 'gw/serial-reader' into gateway
- use private library for serial reader - optimize memory usage - reduce memory needed for json serialization/deserialization - remove the need for array commands
This commit is contained in:
commit
2a591cb28f
@ -40,7 +40,7 @@ steps:
|
||||
- cd gateway
|
||||
- service ser2net stop
|
||||
- pio run -e pro-mini
|
||||
- echo -n 'reset' > /dev/ttyUSB0; sleep 1s; avrdude -patmega328p -carduino -P/dev/ttyUSB0 -b115200 -D -Uflash:w:.pio/build/pro-mini/firmware.hex:i -v
|
||||
- echo 'reset' > /dev/ttyUSB0; sleep 1s; avrdude -patmega328p -carduino -P/dev/ttyUSB0 -b115200 -D -Uflash:w:.pio/build/pro-mini/firmware.hex:i -v
|
||||
- service ser2net start
|
||||
when:
|
||||
target:
|
||||
|
||||
@ -22,6 +22,7 @@ lib_deps =
|
||||
bblanchon/ArduinoJson@6.19.4
|
||||
adafruit/Adafruit Unified Sensor@^1.1.4
|
||||
adafruit/DHT sensor library@1.3.10
|
||||
https://git.hodos.ro/arduino/lib_serial-reader.git@^1.0.0
|
||||
build_flags = -D DHT_SENSOR=0
|
||||
upload_port = /dev/ttyUSB0
|
||||
check_tool = cppcheck
|
||||
|
||||
@ -3,6 +3,7 @@
|
||||
#include "Dht.h"
|
||||
#include "Protocol_1.h"
|
||||
#include "Protocol_2.h"
|
||||
#include <SerialReader.h>
|
||||
|
||||
#define RESET_PIN 10
|
||||
#define SEND_PIN 11
|
||||
@ -10,6 +11,7 @@
|
||||
|
||||
|
||||
RCSwitch mySwitch = RCSwitch();
|
||||
SerialReader<200> serialReader;
|
||||
|
||||
void setup() {
|
||||
digitalWrite(RESET_PIN, HIGH);
|
||||
@ -60,47 +62,42 @@ void readRcSwitch() {
|
||||
}
|
||||
}
|
||||
|
||||
void runJsonCommands(const char* cmd) {
|
||||
StaticJsonDocument<512> jsonArray;
|
||||
DeserializationError err = deserializeJson(jsonArray, cmd);
|
||||
void handleJsonError(DeserializationError err, const char* cmd) {
|
||||
StaticJsonDocument<50> jsonError;
|
||||
JsonObject error = jsonError.createNestedObject("error");
|
||||
error["msg"] = err.c_str();
|
||||
error["orig_cmd"] = cmd;
|
||||
serializeJson(jsonError, Serial);
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
void runJsonCommand(char* cmd) {
|
||||
StaticJsonDocument<50> jsonDoc;
|
||||
DeserializationError err = deserializeJson(jsonDoc, cmd);
|
||||
if (err == DeserializationError::Ok) {
|
||||
JsonArray array = jsonArray.as<JsonArray>();
|
||||
for (JsonVariant jsonDoc : array) {
|
||||
if (jsonDoc.containsKey("rcSwitch")) {
|
||||
JsonObjectConst rcSwitch = jsonDoc["rcSwitch"];
|
||||
Protocol* p = findProtocol(rcSwitch["protocol"]);
|
||||
p->fromJson(rcSwitch, mySwitch);
|
||||
delete p;
|
||||
serializeJson(jsonDoc, Serial);
|
||||
Serial.println();
|
||||
}
|
||||
if (jsonDoc.containsKey("rcSwitch")) {
|
||||
JsonObjectConst rcSwitch = jsonDoc["rcSwitch"];
|
||||
Protocol* p = findProtocol(rcSwitch["protocol"]);
|
||||
p->fromJson(rcSwitch, mySwitch);
|
||||
delete p;
|
||||
serializeJson(jsonDoc, Serial);
|
||||
Serial.println();
|
||||
}
|
||||
} else {
|
||||
StaticJsonDocument<128> jsonError;
|
||||
JsonObject error = jsonError.createNestedObject("error");
|
||||
error["message"] = err.c_str();
|
||||
error["original_cmd"] = cmd;
|
||||
serializeJson(jsonError, Serial);
|
||||
Serial.println();
|
||||
|
||||
handleJsonError(err, cmd);
|
||||
}
|
||||
}
|
||||
|
||||
void readCommand() {
|
||||
if (Serial.available() > 0) {
|
||||
String cmd = Serial.readStringUntil('\n');
|
||||
if (cmd == "reset") {
|
||||
if (serialReader.readLine(Serial) > 0) {
|
||||
char* cmd = serialReader.getBuffer();
|
||||
if (strcmp("reset", cmd) == 0) {
|
||||
Serial.println("resetting...");
|
||||
delay(200);
|
||||
delay(1200);
|
||||
digitalWrite(RESET_PIN, LOW);
|
||||
Serial.println("resetting failed");
|
||||
}
|
||||
if (cmd.endsWith(",")) {
|
||||
cmd = cmd.substring(0, cmd.lastIndexOf(','));
|
||||
}
|
||||
char buffer[cmd.length()+3];
|
||||
sprintf(buffer, "[%s]", cmd.c_str());
|
||||
runJsonCommands(buffer);
|
||||
runJsonCommand(cmd);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user