1
0
Files
MAX7456JSON/src/main.cpp

112 lines
2.4 KiB
C++
Raw Normal View History

#include "Arduino.h"
2019-01-10 16:42:29 +01:00
#include <SPI.h>
#include "config.h"
#include "ArduinoJson.h"
#include "max7456.h"
Max7456 osd;
void handleRPC(char *cmd)
{
Serial.print("Handling: ");
Serial.println(cmd);
StaticJsonDocument<200> doc;
DeserializationError error = deserializeJson(doc, cmd);
2019-01-10 16:42:29 +01:00
// Test if parsing succeeds.
if (error)
{
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.c_str());
return;
}
2019-01-10 16:42:29 +01:00
const char *command = doc["command"]; // "write"
2019-01-10 16:42:29 +01:00
if (strcmp("write", command) == 0)
{
const char *text = doc["text"]; // "1351824120"
int x = doc["x"]; // 13
int y = doc["y"]; // 10
2019-01-10 16:42:29 +01:00
osd.print(text, x, y);
return;
}
if (strcmp("clear", command) == 0)
{
osd.clearScreen();
return;
}
if (strcmp("offset", command) == 0)
{
int x = doc["x"]; // 13
int y = doc["y"]; // 10
2019-01-10 16:42:29 +01:00
osd.setDisplayOffsets(x, y);
return;
2019-01-10 16:42:29 +01:00
}
}
void processSerial()
{
static byte ndx = 0;
char endMarker = '\n';
char rc;
const byte numChars = 128;
char receivedChars[numChars]; // an array to store the received data
while (Serial.available() > 0)
{
rc = Serial.read();
if (rc != endMarker)
{
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars)
{
ndx = numChars - 1;
}
}
else
{
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
handleRPC(receivedChars);
}
}
}
void setup()
{
Serial.begin(BAUDRATE);
Serial.println("================================================================================");
Serial.println("Firmware: " PROJECT_NAME);
Serial.println("Version: " VERSION_STRING);
Serial.println("Built: " __DATE__ ", " __TIME__);
Serial.println("================================================================================");
Serial.println("Initialize...");
SPI.begin();
osd.init(6);
osd.clearScreen();
osd.setDisplayOffsets(DISP_OFFSET_X, DISP_OFFSET_Y);
osd.setBlinkParams(_8fields, _BT_3BT);
osd.activateOSD();
osd.activateExternalVideo(false);
osd.print("==========================", 0, 0);
osd.print("Firmware: " PROJECT_NAME, 0, 1);
osd.print("Version: " VERSION_STRING, 0, 2);
osd.print("Built: " __DATE__ ", " __TIME__, 0, 3);
osd.print("==========================", 0, 4);
delay(3000);
osd.clearScreen();
Serial.println("Ready!");
}
void loop()
{
processSerial();
2019-01-10 16:42:29 +01:00
}