1
0
MAX7456JSON/lib/CommandHandler/CommandHandler.cpp

69 lines
1.5 KiB
C++
Raw Normal View History

2019-09-04 13:28:38 +00:00
#include "CommandHandler.h"
#include "Arduino.h"
#include "ArduinoJson.h"
CommandHandler::CommandHandler(DisplayProxy *display)
{
this->display = display;
}
2021-11-03 10:13:25 +00:00
bool CommandHandler::parseJSON(const char *cmd)
2019-09-04 13:28:38 +00:00
{
2021-11-09 09:00:30 +00:00
debugSerial("parseJSON", cmd);
2021-11-03 12:00:05 +00:00
StaticJsonDocument<32> json;
DeserializationError error = deserializeJson(json, cmd);
2019-09-04 13:28:38 +00:00
// Test if parsing succeeds.
if (error)
{
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.c_str());
return false;
}
2021-11-03 12:00:05 +00:00
const char *command = json["cmd"];
if (strcmp("write", command) == 0 && json["text"].is<const char*>() && json["x"].is<int>() && json["y"].is<int>())
2019-09-04 13:28:38 +00:00
{
2021-11-03 12:00:05 +00:00
const char *text = json["text"];
int x = json["x"];
int y = json["y"];
2019-09-04 13:28:38 +00:00
2021-11-09 09:00:30 +00:00
this->debugSerial(command, text);
2019-09-04 13:28:38 +00:00
this->display->write(x, y, text);
return true;
}
if (strcmp("clear", command) == 0)
{
this->display->clear();
2021-11-09 09:00:30 +00:00
return true;
2019-09-04 13:28:38 +00:00
}
if (strcmp("offset", command) == 0)
{
2021-11-03 12:00:05 +00:00
int x = json["x"];
int y = json["y"];
2021-11-09 09:00:30 +00:00
debugSerial("offset unhandled");
2021-10-28 14:44:20 +00:00
//this->display->setOffset(x, y);
2021-11-09 09:00:30 +00:00
return true;
2019-09-04 13:28:38 +00:00
}
2021-11-09 09:00:30 +00:00
return false;
}
void CommandHandler::debugSerial(const char *text)
{
Serial.print(F("CommandHandler: "));
Serial.println(text);
2019-09-04 13:28:38 +00:00
}
2021-11-09 09:00:30 +00:00
void CommandHandler::debugSerial(const char *command, const char *text)
2019-09-04 13:28:38 +00:00
{
2021-11-09 09:00:30 +00:00
Serial.print(F("CommandHandler: "));
Serial.print(command);
Serial.print(" > ");
2019-09-04 13:28:38 +00:00
Serial.println(text);
2021-11-09 09:00:30 +00:00
}