Skip to content

Commit

Permalink
Serial communication during setup(), checksum on serial messages
Browse files Browse the repository at this point in the history
  • Loading branch information
snoutmate committed May 12, 2019
1 parent 6b48624 commit df43aa6
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 9 deletions.
10 changes: 7 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -616,8 +616,9 @@ void setupCommunication()


g_serial_comm = new SerialComm(s_protocol);

g_data_source->connect();
g_serial_comm->setupTickCallback([] { // temporary timed callback before we enter the main loop
g_serial_comm->loop();
});
}

void setupLogo()
Expand Down Expand Up @@ -796,6 +797,7 @@ void setup() {
setupMenu();

setupLogo();
setupCommunication();

CCLOGI("<Firmware> %s",Utils::getDeviceInfo("\n<Firmware> ").c_str());

Expand All @@ -817,7 +819,9 @@ void setup() {
g_display->replaceAction(g_price_action);

setupNTP();
setupCommunication();

g_data_source->connect();
g_serial_comm->detachTicker();
}

void loop() {
Expand Down
6 changes: 3 additions & 3 deletions src/protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ static const char *g_local_commands[] = {
"getDeviceInfo","addWiFiAP","removeWiFiAP","getWiFiAPs"
};

// local commands
// TODO: local commands
// getDeviceInfo (include screen IDs and info),
// eraseEEPROM,
// getdiagnostic info (wifi signal strenght etc.)
Expand Down Expand Up @@ -220,8 +220,8 @@ void CC_Protocol::setDefaultCallbacks()
if (m_is_remote == false) {
setCommandCallback("getDeviceInfo", [this](const JsonDocument& j) {
sendHello();
sendDiagnostics();
sendAllParameters();
// sendDiagnostics();
// sendAllParameters();
});
}
}
Expand Down
15 changes: 12 additions & 3 deletions src/serial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
#include "serial.hpp"
#include "log.hpp"

static const char* LOGTAG = "Serial";
//static const char* LOGTAG = "Serial";

void SerialComm::loop()
{
String line = m_prot->readyToSend();
if (line.length()>0)
Serial.printf_P(PSTR("|SERIALCOMM|%s\n"),line.c_str());
Serial.printf_P(PSTR("|SERIALCOMM|%s|%02X|\n"),line.c_str(), Utils::calculateChecksum(line.c_str()));

while(Serial.available()>0) {
int ch = Serial.read();
Expand All @@ -36,7 +36,7 @@ void SerialComm::loop()
continue;

if (ch == '\n') {
CCLOGD("data: '%s'", m_buffer.c_str());
// CCLOGD("data: '%s'", m_buffer.c_str());
m_prot->dataReceived(m_buffer.c_str(), m_buffer.length());
m_buffer = "";
} else {
Expand All @@ -45,3 +45,12 @@ void SerialComm::loop()
}
}

void SerialComm::setupTickCallback(Ticker::callback_t callback)
{
m_ticker.attach(c_milis_per_tick / 1000.0,callback);
}

void SerialComm::detachTicker()
{
m_ticker.detach();
}
5 changes: 5 additions & 0 deletions src/serial.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,13 @@ class SerialComm
SerialComm(CC_Protocol *prot): m_prot(prot), m_buffer("")
{}

void setupTickCallback(Ticker::callback_t callback);
void detachTicker();

void loop();
private:
Ticker m_ticker; // for communicating via serial during setup()
CC_Protocol *m_prot;
String m_buffer;
const int c_milis_per_tick = 333;
};
11 changes: 11 additions & 0 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,15 @@ String getDisconnectReason(WiFiDisconnectReason reason)
return F("Unknown");
}

// simple xor checksum
unsigned char calculateChecksum(const String& text)
{
unsigned char c = 0;
const char*s = text.c_str();
while(*s) {
c ^= *s++;
}
return c;
}

}
1 change: 1 addition & 0 deletions src/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ String uint64ToString(uint64_t input);
float getMemoryFragmentation();
String getDeviceInfo(const String& sep=" ");
String getDisconnectReason(WiFiDisconnectReason reason);
unsigned char calculateChecksum(const String& text);
}

0 comments on commit df43aa6

Please sign in to comment.