From 7e9bf58abc932c0c848477a9595904abc3ad9298 Mon Sep 17 00:00:00 2001 From: sanine-a Date: Fri, 12 Mar 2021 09:07:57 -0600 Subject: [PATCH 01/14] refactor: rename SerialController.hpp > SerialController.h --- Libraries/{SerialController.hpp => SerialController.h} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Libraries/{SerialController.hpp => SerialController.h} (100%) diff --git a/Libraries/SerialController.hpp b/Libraries/SerialController.h similarity index 100% rename from Libraries/SerialController.hpp rename to Libraries/SerialController.h From 3eedbf44a07909080507023acb12b41c0b488fa3 Mon Sep 17 00:00:00 2001 From: sanine-a Date: Fri, 12 Mar 2021 09:46:20 -0600 Subject: [PATCH 02/14] refactor: move SerialController member function definitions out of SerialController class definition --- Libraries/SerialController.h | 326 +++++++++++++++++++---------------- 1 file changed, 173 insertions(+), 153 deletions(-) diff --git a/Libraries/SerialController.h b/Libraries/SerialController.h index 1c18b85..0027479 100644 --- a/Libraries/SerialController.h +++ b/Libraries/SerialController.h @@ -16,180 +16,200 @@ typedef enum { N_PARSE_STATES } parseState; -static void cleanString(char* string); - // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ class SerialController { - private: - parseState state; - void (*callback)(char* messageKey, char* messageValue); - char key[MAX_STRING_LEN]; - char value[MAX_STRING_LEN]; - int keyIndex, valueIndex; +private: + parseState state; + void (*callback)(char* messageKey, char* messageValue); + char key[MAX_STRING_LEN]; + char value[MAX_STRING_LEN]; + int keyIndex, valueIndex; + + void waitForSerial(long baudrate); - public: - bool handshake; + void cleanString(char* string); + + +public: + bool handshake; SerialManager() { state = WAIT_FOR_START; callback = NULL; } - void setup(long baudrate, void (*callback_)(char*, char*)) { - waitForSerial(baudrate); - callback = callback_; - } + void setup(long baudrate, void (*callback_)(char*, char*)); + + void sendMessage(char* messageKey, char* messageValue); - void waitForSerial(long baudrate) { - Serial.begin(baudrate); - while(!Serial); + void sendMessage(char* messageKey, int messageValue); - while(!handshake) { - if (Serial.available() && Serial.read() == '{') { - handshake = true; - sendMessage("arduino-ready","1"); - } - } - } + void sendMessage(char* messageKey, long int messageValue); + + void sendMessage(char* messageKey, double messageValue); - // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + void update(); +}; - void sendMessage(char* messageKey, char* messageValue) { - cleanString(messageKey); - cleanString(messageValue); +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * + * SerialController public member functions + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + */ - char result[2*MAX_STRING_LEN+3]; - strcpy(result,"{"); - strcat(result,messageKey); - strcat(result,":"); - strcat(result,messageValue); - strcat(result,"}"); +void SerialController::setup(long baudrate, void (*callback_)(char*, char*)) { + waitForSerial(baudrate); + callback = callback_; +} - Serial.println(result); - } - void sendMessage(char* messageKey, int messageValue) { - char stringValue[MAX_STRING_LEN]; - snprintf(stringValue, MAX_STRING_LEN, "%d", messageValue); - sendMessage(messageKey, stringValue); - } +void SerialController::sendMessage(char* messageKey, char* messageValue) { + cleanString(messageKey); + cleanString(messageValue); - void sendMessage(char* messageKey, unsigned int messageValue) { - char stringValue[MAX_STRING_LEN]; - snprintf(stringValue, MAX_STRING_LEN, "%u", messageValue); - sendMessage(messageKey, stringValue); - } + char result[2*MAX_STRING_LEN+3]; + strcpy(result,"{"); + strcat(result,messageKey); + strcat(result,":"); + strcat(result,messageValue); + strcat(result,"}"); - void sendMessage(char* messageKey, long int messageValue) { - char stringValue[MAX_STRING_LEN]; - snprintf(stringValue, MAX_STRING_LEN, "%ld", messageValue); - sendMessage(messageKey, stringValue); - } + Serial.println(result); +} - void sendMessage(char* messageKey, long unsigned int messageValue) { - char stringValue[MAX_STRING_LEN]; - snprintf(stringValue, MAX_STRING_LEN, "%lu", messageValue); - sendMessage(messageKey, stringValue); - } - void sendMessage(char* messageKey, float messageValue) { - char stringValue[MAX_STRING_LEN]; - snprintf(stringValue, MAX_STRING_LEN, "%f", messageValue); - sendMessage(messageKey, stringValue); - } +void SerialController::sendMessage(char* messageKey, int messageValue) { + char stringValue[MAX_STRING_LEN]; + snprintf(stringValue, MAX_STRING_LEN, "%d", messageValue); + sendMessage(messageKey, stringValue); +} + + +void SerialController::sendMessage(char* messageKey, long int messageValue) { + char stringValue[MAX_STRING_LEN]; + snprintf(stringValue, MAX_STRING_LEN, "%ld", messageValue); + sendMessage(messageKey, stringValue); +} + + +void SerialController::sendMessage(char* messageKey, double messageValue) { + char stringValue[MAX_STRING_LEN]; + snprintf(stringValue, MAX_STRING_LEN, "%f", messageValue); + sendMessage(messageKey, stringValue); +} + + +void SerialController::update() { + while (Serial.available() > 0) { + char c = Serial.read(); + switch (state) { + case WAIT_FOR_START: + if (c == '{') { + state = PARSE_KEY; + strcpy(key,""); + strcpy(value,""); + keyIndex = 0; + valueIndex = 0; + } + break; + + case PARSE_KEY: + if (keyIndex == MAX_STRING_LEN-1) { + key[keyIndex] = 0; + state = PARSE_KEY_OVERFLOW; + } + else if (c == '{') { + strcpy(key,""); + strcpy(value,""); + keyIndex = 0; + valueIndex = 0; + } + else if (c == ':') { + key[keyIndex] = 0; + state = PARSE_VALUE; + } + else if (c == '}') { + key[keyIndex] = 0; + (*callback)(key,value); + state = WAIT_FOR_START; + } + else { + key[keyIndex] = c; + keyIndex++; + } + break; - void update() { - while (Serial.available() > 0) { - char c = Serial.read(); - switch (state) { - case WAIT_FOR_START: - if (c == '{') { - state = PARSE_KEY; - strcpy(key,""); - strcpy(value,""); - keyIndex = 0; - valueIndex = 0; - } - break; - - case PARSE_KEY: - if (keyIndex == MAX_STRING_LEN-1) { - key[keyIndex] = 0; - state = PARSE_KEY_OVERFLOW; - } - else if (c == '{') { - strcpy(key,""); - strcpy(value,""); - keyIndex = 0; - valueIndex = 0; - } - else if (c == ':') { - key[keyIndex] = 0; - state = PARSE_VALUE; - } - else if (c == '}') { - key[keyIndex] = 0; - (*callback)(key,value); - state = WAIT_FOR_START; - } - else { - key[keyIndex] = c; - keyIndex++; - } - break; - - case PARSE_VALUE: - if (valueIndex == MAX_STRING_LEN - 1) { - value[valueIndex] = 0; - state = PARSE_VALUE_OVERFLOW; - } - else if (c == '}') { - value[valueIndex] = 0; - (*callback)(key,value); - state = WAIT_FOR_START; - } - else { - value[valueIndex] = c; - valueIndex++; - } - break; - - case PARSE_KEY_OVERFLOW: - if (c == ':') { - state = PARSE_VALUE; - } - else if (c == '{') { - strcpy(key,""); - strcpy(value,""); - keyIndex = 0; - valueIndex = 0; - state = PARSE_KEY; - } - break; - - case PARSE_VALUE_OVERFLOW: - if (c == '{') { - strcpy(key,""); - strcpy(value,""); - keyIndex = 0; - valueIndex = 0; - state = PARSE_KEY; - } - else if (c == '}') { - (*callback)(key,value); - state = WAIT_FOR_START; - } - break; - - default: - // something's gone wrong, reset - state = WAIT_FOR_START; - break; + case PARSE_VALUE: + if (valueIndex == MAX_STRING_LEN - 1) { + value[valueIndex] = 0; + state = PARSE_VALUE_OVERFLOW; + } + else if (c == '}') { + value[valueIndex] = 0; + (*callback)(key,value); + state = WAIT_FOR_START; + } + else { + value[valueIndex] = c; + valueIndex++; + } + break; + + case PARSE_KEY_OVERFLOW: + if (c == ':') { + state = PARSE_VALUE; + } + else if (c == '{') { + strcpy(key,""); + strcpy(value,""); + keyIndex = 0; + valueIndex = 0; + state = PARSE_KEY; + } + break; + + case PARSE_VALUE_OVERFLOW: + if (c == '{') { + strcpy(key,""); + strcpy(value,""); + keyIndex = 0; + valueIndex = 0; + state = PARSE_KEY; } + else if (c == '}') { + (*callback)(key,value); + state = WAIT_FOR_START; + } + break; + + default: + // something's gone wrong, reset + state = WAIT_FOR_START; + break; } } -}; +} + + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * + * SerialController private member functions + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + */ + +void SerialController::waitForSerial(long baudrate) { + Serial.begin(baudrate); + while(!Serial); + + while(!handshake) { + if (Serial.available() && Serial.read() == '{') { + handshake = true; + sendMessage("arduino-ready","1"); + } + } +} -// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -void cleanString(char* string) { +void SerialController::cleanString(char* string) { // remove '{', '}', and ':' delimiter characters from a string char result[MAX_STRING_LEN]; @@ -197,8 +217,8 @@ void cleanString(char* string) { int j = 0; while(string[i] != 0) { if (string[i] == '{' - || string[i] == '}' - || string[i] == ':') { + || string[i] == '}' + || string[i] == ':') { i++; } else { From 19c77d6e01afdcadbe3858477eca4e425ffd25ae Mon Sep 17 00:00:00 2001 From: sanine-a Date: Fri, 12 Mar 2021 09:48:22 -0600 Subject: [PATCH 03/14] refactor: remove parseState typedef --- Libraries/SerialController.h | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/Libraries/SerialController.h b/Libraries/SerialController.h index 0027479..42372b4 100644 --- a/Libraries/SerialController.h +++ b/Libraries/SerialController.h @@ -5,22 +5,18 @@ #include -// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -typedef enum { - WAIT_FOR_START, - PARSE_KEY, - PARSE_KEY_OVERFLOW, - PARSE_VALUE, - PARSE_VALUE_OVERFLOW, - N_PARSE_STATES -} parseState; - -// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - class SerialController { private: - parseState state; + enum { + WAIT_FOR_START, + PARSE_KEY, + PARSE_KEY_OVERFLOW, + PARSE_VALUE, + PARSE_VALUE_OVERFLOW, + N_PARSE_STATES + } state; + + void (*callback)(char* messageKey, char* messageValue); char key[MAX_STRING_LEN]; char value[MAX_STRING_LEN]; From 6b674500224bcd4941e3b201143ca50b4d3290e7 Mon Sep 17 00:00:00 2001 From: sanine-a Date: Fri, 12 Mar 2021 09:58:44 -0600 Subject: [PATCH 04/14] refactor: use String key, value instead of char array --- Libraries/SerialController.h | 69 +++++++----------------------------- 1 file changed, 12 insertions(+), 57 deletions(-) diff --git a/Libraries/SerialController.h b/Libraries/SerialController.h index 42372b4..a10dff4 100644 --- a/Libraries/SerialController.h +++ b/Libraries/SerialController.h @@ -18,9 +18,7 @@ class SerialController { void (*callback)(char* messageKey, char* messageValue); - char key[MAX_STRING_LEN]; - char value[MAX_STRING_LEN]; - int keyIndex, valueIndex; + String key, value; void waitForSerial(long baudrate); @@ -99,80 +97,37 @@ void SerialController::update() { switch (state) { case WAIT_FOR_START: if (c == '{') { + key = ""; + value = ""; state = PARSE_KEY; - strcpy(key,""); - strcpy(value,""); - keyIndex = 0; - valueIndex = 0; } break; case PARSE_KEY: - if (keyIndex == MAX_STRING_LEN-1) { - key[keyIndex] = 0; - state = PARSE_KEY_OVERFLOW; - } - else if (c == '{') { - strcpy(key,""); - strcpy(value,""); - keyIndex = 0; - valueIndex = 0; + if (c == '{') { + key = ""; + value = ""; + state = PARSE_KEY; } else if (c == ':') { - key[keyIndex] = 0; state = PARSE_VALUE; } else if (c == '}') { - key[keyIndex] = 0; - (*callback)(key,value); + (*callback)(key.c_str(),value.c_str()); state = WAIT_FOR_START; } else { - key[keyIndex] = c; - keyIndex++; + key.concat(c); } break; case PARSE_VALUE: - if (valueIndex == MAX_STRING_LEN - 1) { - value[valueIndex] = 0; - state = PARSE_VALUE_OVERFLOW; - } - else if (c == '}') { - value[valueIndex] = 0; - (*callback)(key,value); + if (c == '}') { + (*callback)(key.c_str(),value.c_str()); state = WAIT_FOR_START; } else { - value[valueIndex] = c; - valueIndex++; - } - break; - - case PARSE_KEY_OVERFLOW: - if (c == ':') { - state = PARSE_VALUE; - } - else if (c == '{') { - strcpy(key,""); - strcpy(value,""); - keyIndex = 0; - valueIndex = 0; - state = PARSE_KEY; - } - break; - - case PARSE_VALUE_OVERFLOW: - if (c == '{') { - strcpy(key,""); - strcpy(value,""); - keyIndex = 0; - valueIndex = 0; - state = PARSE_KEY; - } - else if (c == '}') { - (*callback)(key,value); - state = WAIT_FOR_START; + value.concat(c); } break; From 6bd35b18411f33a6ffe41558710fabea5004c722 Mon Sep 17 00:00:00 2001 From: sanine-a Date: Fri, 12 Mar 2021 10:08:01 -0600 Subject: [PATCH 05/14] change all message-sending functions to use String instead of char array --- Libraries/SerialController.h | 69 ++++++++++++------------------------ 1 file changed, 22 insertions(+), 47 deletions(-) diff --git a/Libraries/SerialController.h b/Libraries/SerialController.h index a10dff4..1b683b7 100644 --- a/Libraries/SerialController.h +++ b/Libraries/SerialController.h @@ -22,7 +22,7 @@ class SerialController { void waitForSerial(long baudrate); - void cleanString(char* string); + void cleanString(String& string); public: @@ -31,13 +31,13 @@ class SerialController { SerialManager() { state = WAIT_FOR_START; callback = NULL; } void setup(long baudrate, void (*callback_)(char*, char*)); - void sendMessage(char* messageKey, char* messageValue); + void sendMessage(String messageKey, String messageValue); - void sendMessage(char* messageKey, int messageValue); + void sendMessage(String messageKey, int messageValue); - void sendMessage(char* messageKey, long int messageValue); - - void sendMessage(char* messageKey, double messageValue); + void sendMessage(String messageKey, long int messageValue); + + void sendMessage(String messageKey, double messageValue); void update(); }; @@ -55,39 +55,32 @@ void SerialController::setup(long baudrate, void (*callback_)(char*, char*)) { } -void SerialController::sendMessage(char* messageKey, char* messageValue) { +void SerialController::sendMessage(String messageKey, String messageValue) { cleanString(messageKey); cleanString(messageValue); - char result[2*MAX_STRING_LEN+3]; - strcpy(result,"{"); - strcat(result,messageKey); - strcat(result,":"); - strcat(result,messageValue); - strcat(result,"}"); + String result = "{"; + result.concat(messageKey); + result.concat(":"); + result.concat(messageValue); + result.concat("}"); Serial.println(result); } -void SerialController::sendMessage(char* messageKey, int messageValue) { - char stringValue[MAX_STRING_LEN]; - snprintf(stringValue, MAX_STRING_LEN, "%d", messageValue); - sendMessage(messageKey, stringValue); +void SerialController::sendMessage(String messageKey, int messageValue) { + sendMessage(messageKey, String(messageValue)); } -void SerialController::sendMessage(char* messageKey, long int messageValue) { - char stringValue[MAX_STRING_LEN]; - snprintf(stringValue, MAX_STRING_LEN, "%ld", messageValue); - sendMessage(messageKey, stringValue); +void SerialController::sendMessage(String messageKey, long int messageValue) { + sendMessage(messageKey, String(messageValue)); } -void SerialController::sendMessage(char* messageKey, double messageValue) { - char stringValue[MAX_STRING_LEN]; - snprintf(stringValue, MAX_STRING_LEN, "%f", messageValue); - sendMessage(messageKey, stringValue); +void SerialController::sendMessage(String messageKey, double messageValue) { + sendMessage(messageKey, String(messageValue)); } @@ -160,28 +153,10 @@ void SerialController::waitForSerial(long baudrate) { } -void SerialController::cleanString(char* string) { - // remove '{', '}', and ':' delimiter characters from a string - char result[MAX_STRING_LEN]; - - int i = 0; - int j = 0; - while(string[i] != 0) { - if (string[i] == '{' - || string[i] == '}' - || string[i] == ':') { - i++; - } - else { - result[j] = string[i]; - i++; j++; - } - } - - // close string - result[j] = 0; - - string = result; +void SerialController::cleanString(String& string) { + string.replace("{", ""); + string.replace("}", ""); + string.replace(":", ""); } #endif From 3dd464125f85c28199668b0f0a26ae6d744c44dc Mon Sep 17 00:00:00 2001 From: sanine-a Date: Fri, 12 Mar 2021 10:11:31 -0600 Subject: [PATCH 06/14] add steleProtocol switch --- Libraries/SerialController.h | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/Libraries/SerialController.h b/Libraries/SerialController.h index 1b683b7..0e919d7 100644 --- a/Libraries/SerialController.h +++ b/Libraries/SerialController.h @@ -19,6 +19,7 @@ class SerialController { void (*callback)(char* messageKey, char* messageValue); String key, value; + bool steleProtocol; void waitForSerial(long baudrate); @@ -29,7 +30,9 @@ class SerialController { bool handshake; SerialManager() { state = WAIT_FOR_START; callback = NULL; } - void setup(long baudrate, void (*callback_)(char*, char*)); + void setup(long baudrate, + void (*callback_)(char*, char*), + bool steleProtocol=true); void sendMessage(String messageKey, String messageValue); @@ -49,7 +52,10 @@ class SerialController { * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ -void SerialController::setup(long baudrate, void (*callback_)(char*, char*)) { +void SerialController::setup(long baudrate, + void (*callback_)(char*, char*), + bool steleProtocol) { + this->steleProtocol = steleProtocol; waitForSerial(baudrate); callback = callback_; } @@ -144,7 +150,8 @@ void SerialController::waitForSerial(long baudrate) { Serial.begin(baudrate); while(!Serial); - while(!handshake) { + // wait for handshake if using steleProtocol + while(!handshake && steleProtocol) { if (Serial.available() && Serial.read() == '{') { handshake = true; sendMessage("arduino-ready","1"); From 3ad2e8e5d18edb38ca854b7e8bea09217c3c7d55 Mon Sep 17 00:00:00 2001 From: sanine-a Date: Fri, 12 Mar 2021 10:19:54 -0600 Subject: [PATCH 07/14] add SerialResponse struct --- Libraries/SerialController.h | 58 ++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/Libraries/SerialController.h b/Libraries/SerialController.h index 0e919d7..e9253be 100644 --- a/Libraries/SerialController.h +++ b/Libraries/SerialController.h @@ -5,6 +5,64 @@ #include +typedef void (*stringResponse)(String); +typedef void (*intResponse)(int); +typedef void (*floatResponse)(float); + + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * + * SerialResponse + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + */ + +struct SerialResponse { + String messageName; + enum { + STRING, + INT, + FLOAT + } valueType; + union { + stringResponse s; + intResponse i; + floatResponse f; + } response; + + void respond(String value); +}; + + +void SerialResponse::respond(String value) { + switch (valueType) { + case STRING: + response.s(value); + break; + + case INT: + response.i(value.toInt()); + break; + + case FLOAT: + response.f(value.toFloat()); + break; + + default: + // bad valueType, do nothing + break; + } +} + + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * + * SerialController + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + */ + + class SerialController { private: enum { From e90366cd85f43ab1fef54db808919ec85d001e10 Mon Sep 17 00:00:00 2001 From: sanine-a Date: Fri, 12 Mar 2021 10:23:25 -0600 Subject: [PATCH 08/14] switch to float instead of double and remove sendMessage(String, long) --- Libraries/SerialController.h | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/Libraries/SerialController.h b/Libraries/SerialController.h index e9253be..a4e8adc 100644 --- a/Libraries/SerialController.h +++ b/Libraries/SerialController.h @@ -96,9 +96,7 @@ class SerialController { void sendMessage(String messageKey, int messageValue); - void sendMessage(String messageKey, long int messageValue); - - void sendMessage(String messageKey, double messageValue); + void sendMessage(String messageKey, float messageValue); void update(); }; @@ -138,12 +136,7 @@ void SerialController::sendMessage(String messageKey, int messageValue) { } -void SerialController::sendMessage(String messageKey, long int messageValue) { - sendMessage(messageKey, String(messageValue)); -} - - -void SerialController::sendMessage(String messageKey, double messageValue) { +void SerialController::sendMessage(String messageKey, float messageValue) { sendMessage(messageKey, String(messageValue)); } From 2738fecf6cdfbbd162c267221cede2439406c147 Mon Sep 17 00:00:00 2001 From: sanine-a Date: Fri, 12 Mar 2021 10:56:53 -0600 Subject: [PATCH 09/14] refactor: use unique callbacks for specific key strings --- Libraries/SerialController.h | 108 ++++++++++++++++++++++++++--------- 1 file changed, 80 insertions(+), 28 deletions(-) diff --git a/Libraries/SerialController.h b/Libraries/SerialController.h index a4e8adc..abae9ef 100644 --- a/Libraries/SerialController.h +++ b/Libraries/SerialController.h @@ -18,12 +18,14 @@ typedef void (*floatResponse)(float); */ struct SerialResponse { - String messageName; - enum { + String key; + typedef enum { STRING, INT, FLOAT - } valueType; + } ValueType; + + ValueType valueType; union { stringResponse s; intResponse i; @@ -61,7 +63,12 @@ void SerialResponse::respond(String value) { * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - + +// you can #define SERIALCONTROLLER_MAX_RESPONSES before including +// this file to adjust this parameter. +#ifndef SERIALCONTROLLER_MAX_RESPONSES +#define SERIALCONTROLLER_MAX_RESPONSES 16 +#endif class SerialController { private: @@ -75,7 +82,8 @@ class SerialController { } state; - void (*callback)(char* messageKey, char* messageValue); + int numResponses; + SerialResponse responses[SERIALCONTROLLER_MAX_RESPONSES]; String key, value; bool steleProtocol; @@ -83,14 +91,19 @@ class SerialController { void cleanString(String& string); - + void lookupAndRunCallback(); + + static void + public: bool handshake; - SerialManager() { state = WAIT_FOR_START; callback = NULL; } - void setup(long baudrate, - void (*callback_)(char*, char*), - bool steleProtocol=true); + SerialManager() { state = WAIT_FOR_START; numResponses = 0; } + void setup(long baudrate=115200, bool steleProtocol=true); + + void addResponse(String key, stringResponse response); + void addResponse(String key, intResponse response); + void addResponse(String key, floatResponse response); void sendMessage(String messageKey, String messageValue); @@ -101,22 +114,52 @@ class SerialController { void update(); }; -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - * - * SerialController public member functions - * - * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - */ -void SerialController::setup(long baudrate, - void (*callback_)(char*, char*), - bool steleProtocol) { +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +void SerialController::setup(long baudrate, bool steleProtocol) { this->steleProtocol = steleProtocol; waitForSerial(baudrate); - callback = callback_; } +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +void SerialController::addResponse(String key, stringResponse response) { + SerialResponse* r = responses + numResponses; + + r->key = key; + r->valueType = SerialResponse::ValueType::STRING; + r->response.s = response; + + numResponses += 1; +} + + +void SerialController::addResponse(String key, intResponse response) { + SerialResponse* r = responses + numResponses; + + r->key = key; + r->valueType = SerialResponse::ValueType::INT; + r->response.i = response; + + numResponses += 1; +} + + +void SerialController::addResponse(String key, floatResponse response) { + SerialResponse* r = responses + numResponses; + + r->key = key; + r->valueType = SerialResponse::ValueType::FLOAT; + r->response.f = response; + + numResponses += 1; +} + + +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + void SerialController::sendMessage(String messageKey, String messageValue) { cleanString(messageKey); cleanString(messageValue); @@ -141,6 +184,8 @@ void SerialController::sendMessage(String messageKey, float messageValue) { } +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + void SerialController::update() { while (Serial.available() > 0) { char c = Serial.read(); @@ -163,7 +208,7 @@ void SerialController::update() { state = PARSE_VALUE; } else if (c == '}') { - (*callback)(key.c_str(),value.c_str()); + lookupAndRunCallback(); state = WAIT_FOR_START; } else { @@ -173,7 +218,7 @@ void SerialController::update() { case PARSE_VALUE: if (c == '}') { - (*callback)(key.c_str(),value.c_str()); + lookupAndRunCallback(); state = WAIT_FOR_START; } else { @@ -190,12 +235,7 @@ void SerialController::update() { } -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - * - * SerialController private member functions - * - * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - */ +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ void SerialController::waitForSerial(long baudrate) { Serial.begin(baudrate); @@ -217,4 +257,16 @@ void SerialController::cleanString(String& string) { string.replace(":", ""); } + +void SerialController::lookupAndRunCallback() { + for (int i=0; i Date: Fri, 12 Mar 2021 11:04:03 -0600 Subject: [PATCH 10/14] add 'wake-arduino' response automatically if steleProtocol is true --- Libraries/SerialController.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Libraries/SerialController.h b/Libraries/SerialController.h index abae9ef..40653f5 100644 --- a/Libraries/SerialController.h +++ b/Libraries/SerialController.h @@ -93,8 +93,6 @@ class SerialController { void lookupAndRunCallback(); - static void - public: bool handshake; @@ -259,6 +257,11 @@ void SerialController::cleanString(String& string) { void SerialController::lookupAndRunCallback() { + if (steleProtocol && key == "wake-arduino") { + sendMessage("arduino-ready", "1"); + return; + } + for (int i=0; i Date: Fri, 12 Mar 2021 12:48:23 -0600 Subject: [PATCH 11/14] refactor: replace each 'response' with 'callback' --- Libraries/SerialController.h | 99 ++++++++++++++++++------------------ 1 file changed, 50 insertions(+), 49 deletions(-) diff --git a/Libraries/SerialController.h b/Libraries/SerialController.h index 40653f5..5a4f4b1 100644 --- a/Libraries/SerialController.h +++ b/Libraries/SerialController.h @@ -5,20 +5,21 @@ #include -typedef void (*stringResponse)(String); -typedef void (*intResponse)(int); -typedef void (*floatResponse)(float); +typedef void (*stringCallback)(String); +typedef void (*intCallback)(int); +typedef void (*floatCallback)(float); /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * - * SerialResponse + * SerialCallback * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ -struct SerialResponse { +struct SerialCallback { String key; + typedef enum { STRING, INT, @@ -26,28 +27,29 @@ struct SerialResponse { } ValueType; ValueType valueType; + union { - stringResponse s; - intResponse i; - floatResponse f; - } response; + stringCallback s; + intCallback i; + floatCallback f; + } callback; void respond(String value); }; -void SerialResponse::respond(String value) { +void SerialCallback::respond(String value) { switch (valueType) { case STRING: - response.s(value); + callback.s(value); break; case INT: - response.i(value.toInt()); + callback.i(value.toInt()); break; case FLOAT: - response.f(value.toFloat()); + callback.f(value.toFloat()); break; default: @@ -64,10 +66,8 @@ void SerialResponse::respond(String value) { * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ -// you can #define SERIALCONTROLLER_MAX_RESPONSES before including -// this file to adjust this parameter. -#ifndef SERIALCONTROLLER_MAX_RESPONSES -#define SERIALCONTROLLER_MAX_RESPONSES 16 +#ifndef SERIALCONTROLLER_MAX_CALLBACKS +#define SERIALCONTROLLER_MAX_CALLBACKS 16 #endif class SerialController { @@ -75,15 +75,13 @@ class SerialController { enum { WAIT_FOR_START, PARSE_KEY, - PARSE_KEY_OVERFLOW, PARSE_VALUE, - PARSE_VALUE_OVERFLOW, N_PARSE_STATES } state; - int numResponses; - SerialResponse responses[SERIALCONTROLLER_MAX_RESPONSES]; + int numCallbacks; + SerialCallback callbacks[SERIALCONTROLLER_MAX_CALLBACKS]; String key, value; bool steleProtocol; @@ -95,18 +93,18 @@ class SerialController { public: bool handshake; + bool errorResponse; + + SerialManager() { state = WAIT_FOR_START; numCallbacks = 0; } - SerialManager() { state = WAIT_FOR_START; numResponses = 0; } void setup(long baudrate=115200, bool steleProtocol=true); - void addResponse(String key, stringResponse response); - void addResponse(String key, intResponse response); - void addResponse(String key, floatResponse response); + void addCallback(String key, stringCallback callback); + void addCallback(String key, intCallback callback); + void addCallback(String key, floatCallback callback); void sendMessage(String messageKey, String messageValue); - void sendMessage(String messageKey, int messageValue); - void sendMessage(String messageKey, float messageValue); void update(); @@ -117,42 +115,43 @@ class SerialController { void SerialController::setup(long baudrate, bool steleProtocol) { this->steleProtocol = steleProtocol; + errorResponse = true; waitForSerial(baudrate); } // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -void SerialController::addResponse(String key, stringResponse response) { - SerialResponse* r = responses + numResponses; +void SerialController::addCallback(String key, stringCallback callback) { + SerialCallback* cb = callbacks + numCallbacks; - r->key = key; - r->valueType = SerialResponse::ValueType::STRING; - r->response.s = response; + cb->key = key; + cb->valueType = SerialCallback::ValueType::STRING; + cb->callback.s = callback; - numResponses += 1; + numCallbacks += 1; } -void SerialController::addResponse(String key, intResponse response) { - SerialResponse* r = responses + numResponses; +void SerialController::addCallback(String key, intCallback callback) { + SerialCallback* cb = callbacks + numCallbacks; - r->key = key; - r->valueType = SerialResponse::ValueType::INT; - r->response.i = response; + cb->key = key; + cb->valueType = SerialCallback::ValueType::INT; + cb->callback.i = callback; - numResponses += 1; + numCallbacks += 1; } -void SerialController::addResponse(String key, floatResponse response) { - SerialResponse* r = responses + numResponses; +void SerialController::addCallback(String key, floatCallback callback) { + SerialCallback* cb = callbacks + numCallbacks; - r->key = key; - r->valueType = SerialResponse::ValueType::FLOAT; - r->response.f = response; + cb->key = key; + cb->valueType = SerialCallback::ValueType::FLOAT; + cb->callback.f = callback; - numResponses += 1; + numCallbacks += 1; } @@ -262,14 +261,16 @@ void SerialController::lookupAndRunCallback() { return; } - for (int i=0; i Date: Fri, 12 Mar 2021 12:49:41 -0600 Subject: [PATCH 12/14] rename SERIAL_CONTROLLER_HPP to SERIAL_CONTROLLER_H --- Libraries/SerialController.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Libraries/SerialController.h b/Libraries/SerialController.h index 5a4f4b1..1f2258a 100644 --- a/Libraries/SerialController.h +++ b/Libraries/SerialController.h @@ -1,5 +1,5 @@ -#ifndef SERIAL_CONTROLLER_HPP -#define SERIAL_CONTROLLER_HPP +#ifndef SERIAL_CONTROLLER_H +#define SERIAL_CONTROLLER_H #define MAX_STRING_LEN 128 From e0cd7a66863a7b6ca16739e6c305abb188512315 Mon Sep 17 00:00:00 2001 From: sanine-a Date: Fri, 12 Mar 2021 12:52:45 -0600 Subject: [PATCH 13/14] Update SerialControllerExample --- examples/SerialControllerExample.ino | 71 ------------------- .../SerialControllerExample.ino | 56 +++++++++++++++ 2 files changed, 56 insertions(+), 71 deletions(-) delete mode 100644 examples/SerialControllerExample.ino create mode 100644 examples/SerialControllerExample/SerialControllerExample.ino diff --git a/examples/SerialControllerExample.ino b/examples/SerialControllerExample.ino deleted file mode 100644 index 48b2dea..0000000 --- a/examples/SerialControllerExample.ino +++ /dev/null @@ -1,71 +0,0 @@ -#include "arduino-base/Libraries/SerialController.hpp" - -SerialController serialController; - -long baudrate = 115200; -int blinkrate = 1000; -bool blinking = true; -bool led_on = false; -long blinkcounter = 0; - -#define ledpin 13 - -void setup() { - - // Enables/disables debug messaging from ArduinoJson - boolean arduinoJsonDebug = false; - - // Ensure Serial Port is open and ready to communicate - serialController.setup(baudrate, &onParse); - - // For every sketch, we need to set up our IO - // Setup digital pins and default modes as needed, analog inputs are setup by default - pinMode(ledpin, OUTPUT); -} - -void loop() { - // update SerialController and check for new data - serialController.update(); - - if (blinking) { - if ((millis() - blinkcounter) >= blinkrate) { - led_on = !led_on; - digitalWrite(ledpin,led_on); - blinkcounter = millis(); - - // put led_on into a string via snprintf() - char ledOn[5]; - snprintf(ledOn,5,"%d",led_on); - - // send data to stele. note that they must both be strings! - serialController.sendMessage("led-status", ledOn); - } - } -} - -// this function will run when serialController reads new data -void onParse(char* message, char* value) { - if (strcmp(message, "led") == 0) { - // Turn-on led - digitalWrite(ledpin, atoi(value)); - } - else if (strcmp(message, "blinkrate") == 0) { - // set blinkrate - // NOTE: atoi() converts a string containing a number to an integer. - // use atof() if you want a float - blinkrate = atoi(value); - } - else if (strcmp(message, "blinking") == 0) { - // turn blinking on or off - blinking = atoi(value); - } - else if (strcmp(message, "wake-arduino") == 0 && strcmp(value, "1") == 0) { - // you must respond to this message, or else - // stele will believe it has lost connection to the arduino - serialController.sendMessage("arduino-ready", "1"); - } - else { - // helpfully alert us if we've sent something wrong :) - serialController.sendMessage("unknown-command", "1"); - } -} diff --git a/examples/SerialControllerExample/SerialControllerExample.ino b/examples/SerialControllerExample/SerialControllerExample.ino new file mode 100644 index 0000000..dfea5a7 --- /dev/null +++ b/examples/SerialControllerExample/SerialControllerExample.ino @@ -0,0 +1,56 @@ +#include + +#include "SerialController.h" + +SerialController serialController; + +long baudrate = 115200; +int blinkrate = 1000; +bool blinking = true; +bool led_on = false; +long blinkcounter = 0; + +#define ledpin 13 + +// a callback function that will be linked with the serial key "blinkrate" +void onBlinkrate(int rate) { + blinkrate = rate; +} + +// a callback function that will be linked with the serial key "blinking" +void onBlinking(int state) { + blinking = state; +} + + +void setup() { + // Ensure Serial Port is open and ready to communicate + serialController.setup(baudrate); + + // register a callback function to the serial key "blinking" + serialController.addCallback("blinking", onBlinking); + // register a callback function to the serial key "blinkrate" + serialController.addCallback("blinkrate", onBlinkrate); + // register a lambda callback to the serial key "led" + serialController.addCallback("led", [](int s){ digitalWrite(ledpin, s); }); + + // For every sketch, we need to set up our IO + // Setup digital pins and default modes as needed, analog inputs are setup by default + pinMode(ledpin, OUTPUT); +} + +void loop() { + // update SerialController and check for new data + serialController.update(); + + if (blinking) { + if ((millis() - blinkcounter) >= blinkrate) { + led_on = !led_on; + digitalWrite(ledpin,led_on); + blinkcounter = millis(); + + // send data to stele + serialController.sendMessage("led-status", led_on); + } + } +} From 94f8ef22a75988978fd921830ba4bd0e3b2358d0 Mon Sep 17 00:00:00 2001 From: sanine-a Date: Thu, 27 May 2021 13:46:52 -0500 Subject: [PATCH 14/14] update to used fixed-size strings --- Libraries/SerialController.h | 390 ++++++++++++++++++----------------- 1 file changed, 201 insertions(+), 189 deletions(-) diff --git a/Libraries/SerialController.h b/Libraries/SerialController.h index 1f2258a..104d4c0 100644 --- a/Libraries/SerialController.h +++ b/Libraries/SerialController.h @@ -1,14 +1,68 @@ #ifndef SERIAL_CONTROLLER_H #define SERIAL_CONTROLLER_H -#define MAX_STRING_LEN 128 - #include +#include + +#ifndef MAX_KEY_LEN +#define MAX_KEY_LEN 32 +#endif -typedef void (*stringCallback)(String); +#ifndef MAX_VAL_LEN +#define MAX_VAL_LEN 32 +#endif + +#define MAX_MSG_LEN (MAX_KEY_LEN + MAX_VAL_LEN + 3) + +typedef void (*voidCallback)(); +typedef void (*stringCallback)(char*); typedef void (*intCallback)(int); typedef void (*floatCallback)(float); +struct StringF { + int maxSize; + int size; + char *str; + + StringF(int maxSize) : maxSize(maxSize), size(0) { + str = malloc(maxSize * sizeof(char)); + } + + ~StringF() { free(str); } + + void clear() { + memset(str, 0, maxSize); + size = 0; + } + + void append(char *string) { + int s = snprintf(str, maxSize, "%s%s", str, string); + size = s >= maxSize ? maxSize-1 : s; + } + + void append(char c) { + if (size < maxSize-1) { + str[size] = c; + size++; + } + } + + int toInt() { + return atoi(str); + } + + float toFloat() { + return atof(str); + } + + bool equals(StringF string) { + return strcmp(str, string.str) == 0; + } + + bool equals(char *string) { + return strcmp(str, string) == 0; + } +}; /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * @@ -18,9 +72,12 @@ typedef void (*floatCallback)(float); */ struct SerialCallback { - String key; + StringF key; + + SerialCallback() : key(MAX_KEY_LEN) {} typedef enum { + VOID, STRING, INT, FLOAT @@ -29,34 +86,36 @@ struct SerialCallback { ValueType valueType; union { + voidCallback v; stringCallback s; intCallback i; floatCallback f; } callback; - void respond(String value); -}; - - -void SerialCallback::respond(String value) { - switch (valueType) { - case STRING: - callback.s(value); - break; + void respond(StringF value) { + switch (valueType) { + case VOID: + callback.v(); + break; + + case STRING: + callback.s(value.str); + break; - case INT: - callback.i(value.toInt()); - break; + case INT: + callback.i(value.toInt()); + break; - case FLOAT: - callback.f(value.toFloat()); - break; + case FLOAT: + callback.f(value.toFloat()); + break; - default: - // bad valueType, do nothing - break; + default: + // bad valueType, do nothing + break; + } } -} +}; /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -70,6 +129,16 @@ void SerialCallback::respond(String value) { #define SERIALCONTROLLER_MAX_CALLBACKS 16 #endif +// helper to define repetitious addCallback overloads +#define addCallbackDef(cbType, valType, u) \ + void addCallback(const char *keyString, cbType callback) { \ + SerialCallback *cb = callbacks + numCallbacks; \ + cb->key.str = keyString; \ + cb->valueType = SerialCallback::ValueType::valType; \ + cb->callback.u = callback; \ + numCallbacks++; \ + } + class SerialController { private: enum { @@ -82,195 +151,138 @@ class SerialController { int numCallbacks; SerialCallback callbacks[SERIALCONTROLLER_MAX_CALLBACKS]; - String key, value; + StringF key; + StringF value; bool steleProtocol; - void waitForSerial(long baudrate); + void waitForSerial(long baudrate) { + Serial.begin(baudrate); + while(!Serial); - void cleanString(String& string); + // wait for handshake if using steleProtocol + while(!handshake && steleProtocol) { + if (Serial.available() && Serial.read() == '{') { + handshake = true; + sendMessage("arduino-ready","1"); + } + } + } - void lookupAndRunCallback(); -public: - bool handshake; - bool errorResponse; + void lookupAndRunCallback() { + if (steleProtocol && key.equals("wake-arduino")) { + sendMessage("arduino-ready", "1"); + return; + } - SerialManager() { state = WAIT_FOR_START; numCallbacks = 0; } + for (int i=0; isteleProtocol = steleProtocol; - errorResponse = true; - waitForSerial(baudrate); -} - +public: + bool handshake; + bool errorResponse; -// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + SerialController() : key(MAX_KEY_LEN), value(MAX_VAL_LEN) { state = WAIT_FOR_START; numCallbacks = 0; } -void SerialController::addCallback(String key, stringCallback callback) { - SerialCallback* cb = callbacks + numCallbacks; + void setup(long baudrate=115200, bool steleProtocol=true) { + this->steleProtocol = steleProtocol; + errorResponse = true; + waitForSerial(baudrate); + } - cb->key = key; - cb->valueType = SerialCallback::ValueType::STRING; - cb->callback.s = callback; - numCallbacks += 1; -} - - -void SerialController::addCallback(String key, intCallback callback) { - SerialCallback* cb = callbacks + numCallbacks; - - cb->key = key; - cb->valueType = SerialCallback::ValueType::INT; - cb->callback.i = callback; - - numCallbacks += 1; -} - - -void SerialController::addCallback(String key, floatCallback callback) { - SerialCallback* cb = callbacks + numCallbacks; - - cb->key = key; - cb->valueType = SerialCallback::ValueType::FLOAT; - cb->callback.f = callback; - - numCallbacks += 1; -} - - -// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -void SerialController::sendMessage(String messageKey, String messageValue) { - cleanString(messageKey); - cleanString(messageValue); - - String result = "{"; - result.concat(messageKey); - result.concat(":"); - result.concat(messageValue); - result.concat("}"); - - Serial.println(result); -} - - -void SerialController::sendMessage(String messageKey, int messageValue) { - sendMessage(messageKey, String(messageValue)); -} - - -void SerialController::sendMessage(String messageKey, float messageValue) { - sendMessage(messageKey, String(messageValue)); -} - - -// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -void SerialController::update() { - while (Serial.available() > 0) { - char c = Serial.read(); - switch (state) { - case WAIT_FOR_START: - if (c == '{') { - key = ""; - value = ""; - state = PARSE_KEY; - } - break; - - case PARSE_KEY: - if (c == '{') { - key = ""; - value = ""; - state = PARSE_KEY; - } - else if (c == ':') { - state = PARSE_VALUE; - } - else if (c == '}') { - lookupAndRunCallback(); - state = WAIT_FOR_START; - } - else { - key.concat(c); - } - break; - - case PARSE_VALUE: - if (c == '}') { - lookupAndRunCallback(); - state = WAIT_FOR_START; - } - else { - value.concat(c); - } - break; + // overloaded callback-adding functions + addCallbackDef(voidCallback, VOID, v) + addCallbackDef(stringCallback, STRING, s) + addCallbackDef(intCallback, INT, i) + addCallbackDef(floatCallback, FLOAT, f) - default: - // something's gone wrong, reset - state = WAIT_FOR_START; - break; - } + + // message sending + void sendMessage(char *messageKey, char *messageValue) { + char msg[MAX_MSG_LEN]; + int size = snprintf(msg, MAX_MSG_LEN, "{%s:%s}", messageKey, messageValue); + if (size > MAX_MSG_LEN) + Serial.println("{message-too-big:1}"); + else + Serial.println(msg); } -} - - -// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -void SerialController::waitForSerial(long baudrate) { - Serial.begin(baudrate); - while(!Serial); - // wait for handshake if using steleProtocol - while(!handshake && steleProtocol) { - if (Serial.available() && Serial.read() == '{') { - handshake = true; - sendMessage("arduino-ready","1"); - } + void sendMessage(char *messageKey) { + sendMessage(messageKey, "1"); } -} - -void SerialController::cleanString(String& string) { - string.replace("{", ""); - string.replace("}", ""); - string.replace(":", ""); -} + void sendMessage(char *messageKey, int messageValue) { + char value[MAX_VAL_LEN]; + snprintf(value, MAX_VAL_LEN, "%d", messageValue); + sendMessage(messageKey, value); + } -void SerialController::lookupAndRunCallback() { - if (steleProtocol && key == "wake-arduino") { - sendMessage("arduino-ready", "1"); - return; + void sendMessage(char *messageKey, float messageValue) { + char value[MAX_VAL_LEN]; + snprintf(value, MAX_VAL_LEN, "%f", messageValue); + sendMessage(messageKey, value); } - for (int i=0; i 0) { + char c = Serial.read(); + switch (state) { + case WAIT_FOR_START: + if (c == '{') { + key.clear(); + value.clear(); + state = PARSE_KEY; + } + break; + + case PARSE_KEY: + if (c == '{') { + state = PARSE_KEY; + } + else if (c == ':') { + state = PARSE_VALUE; + } + else if (c == '}') { + // malformed input, look for next token + state = WAIT_FOR_START; + } + else { + key.append(c); + } + break; + + case PARSE_VALUE: + if (c == '}') { + lookupAndRunCallback(); + state = WAIT_FOR_START; + } + else { + value.append(c); + } + break; + + default: + // something's gone wrong, reset + state = WAIT_FOR_START; + break; + } + } } -} +}; #endif