-
Notifications
You must be signed in to change notification settings - Fork 0
SerialController.hpp
This library is used to handle sending and parsing serial data. It serves as a link between stele and exhibit-specific apps by sending and receiving JSON-like messages.
If you need/choose to use this library in your app, make sure to run the serialController.setup()
function before any other setup function to ensure anything dependent on SerialController.hpp
is available.
To setup a new SerialController
, you need to pass in two parameters:
-
long
, the serial baud rate -
void (*callback)(String, int)
, a callback function that accepts aString
and anint
.
#include "Libraries/SerialController.hpp"
SerialController serialController;
long baudRate = 115200;
void setup() {
serialController.setup(baudRate, [[](String message, int value) {
onParse(message, value);
});
}
void onParse(String message, int value) {
// This runs when the Arduino receives serial data
if (message == "specific-message" && value == 1)
// Do something
}
}
void loop() {
serialController.update();
}
To send a serial message from the Arduino, you can use the sendMessage
function like this:
// Arduino sends
serialController.sendMessage("some-message-type", 99);
...
// Computer/stele receives
{some-message-type:99}
In this example code, data sent and received by the Arduino is formatted in a JSON-like format. To start communication with the Arduino, send a "{" character, without the quotes.
All messages sent to the Arduino from the computer will be echoed back to the computer as verification that the message was correctly received.