1
0

Streamline to other arduino projects, upgrade ArduinoJson

This commit is contained in:
Jens Christian True
2019-09-04 13:00:53 +02:00
parent 174d5082ed
commit 96fd2967b3
3 changed files with 71 additions and 45 deletions

112
src/main.cpp Normal file
View File

@ -0,0 +1,112 @@
#include "Arduino.h"
#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);
// Test if parsing succeeds.
if (error)
{
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.c_str());
return;
}
const char *command = doc["command"]; // "write"
if (strcmp("write", command) == 0)
{
const char *text = doc["text"]; // "1351824120"
int x = doc["x"]; // 13
int y = doc["y"]; // 10
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
osd.setDisplayOffsets(x, y);
return;
}
}
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();
}