Skip to content

Commit

Permalink
Add debug handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Snoothy committed Feb 25, 2020
1 parent b074164 commit 0fa49db
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
2 changes: 1 addition & 1 deletion examples/Traxxas/Traxxas.ino
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <Arduino.h>
#include "UCR.h"
#include <UCR.h>
#include <Servo.h>

#ifndef STASSID
Expand Down
31 changes: 18 additions & 13 deletions src/UCR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
#include <WiFiUdp.h>
#include <ArduinoJson.h>

#ifdef DEBUG
#define D(x) (x)
#else
#define D(x) do{}while(0)
#endif

#define BUTTONS "Buttons"
#define AXES "Axes"
#define DELTAS "Deltas"
Expand Down Expand Up @@ -41,7 +47,7 @@ void UCR::begin()

void UCR::setupWiFi()
{
Serial.println("Setup WiFi");
D(Serial.println("Setup WiFi"));

if (strlen(_name) == 0)
{
Expand All @@ -52,8 +58,8 @@ void UCR::setupWiFi()
strncpy(_hostString, _name, (sizeof(_hostString) - 1));
}

Serial.print("Hostname: ");
Serial.println(_hostString);
D(Serial.print("Hostname: "));
D(Serial.println(_hostString));

WiFi.hostname(_hostString);
WiFi.mode(WIFI_STA);
Expand All @@ -62,7 +68,7 @@ void UCR::setupWiFi()
while (WiFi.status() != WL_CONNECTED)
{
delay(250);
Serial.print(".");
D(Serial.print("."));
}

Serial.println();
Expand Down Expand Up @@ -180,26 +186,25 @@ bool UCR::receiveUdp()
{
incomingPacketBuffer[len] = 0;
}
Serial.printf("UDP packet contents: %s\n", incomingPacketBuffer);
D(Serial.printf("UDP packet contents: %s\n", incomingPacketBuffer));

// Parse packet
StaticJsonDocument<500> request;
DeserializationError error = deserializeMsgPack(request, incomingPacketBuffer);

if (error)
{
Serial.print("deserializeMsgPack() failed: ");
Serial.println(error.c_str());
D(Serial.print("deserializeMsgPack() failed: "));
D(Serial.println(error.c_str()));
return false;
}

int msgType = request["MsgType"];

Serial.println("Got good packet!");
Serial.print("MsgType: ");
Serial.println(msgType);
D(Serial.println("Got good packet!"));
D(Serial.print("MsgType: "));
D(Serial.println(msgType));

// Respond
StaticJsonDocument<500> response;
response["MsgType"] = msgType;

Expand All @@ -210,8 +215,8 @@ bool UCR::receiveUdp()
addDescriptorList(&response, DELTAS, _deltaList, DELTA_COUNT);
addDescriptorList(&response, EVENTS, _eventList, EVENT_COUNT);

serializeJsonPretty(response, Serial);
Serial.println();
D(serializeJsonPretty(response, Serial));
D(Serial.println());
}

if (msgType == 2)
Expand Down

0 comments on commit 0fa49db

Please sign in to comment.