first commit
This commit is contained in:
53
src/main.cpp
Normal file
53
src/main.cpp
Normal file
@@ -0,0 +1,53 @@
|
||||
#include <Arduino.h>
|
||||
#include <serialConnector.h>
|
||||
|
||||
SerialConnector *conn = new SerialConnector();
|
||||
void calibrationBegin(String args);
|
||||
|
||||
void setup()
|
||||
{
|
||||
pinMode(11, OUTPUT);
|
||||
digitalWrite(11, HIGH);
|
||||
|
||||
pinMode(2, INPUT);
|
||||
pinMode(3, INPUT);
|
||||
pinMode(4, INPUT);
|
||||
|
||||
Serial.begin(9600);
|
||||
|
||||
// Trigered when a CAL-INT command is received
|
||||
conn->onCalibrationInterupt(&calibrationBegin);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
conn->cycle();
|
||||
|
||||
if (!digitalRead(2))
|
||||
{
|
||||
conn->sendCommand("CAL-NXT", "hdsfhdsl");
|
||||
}
|
||||
|
||||
if (!digitalRead(3))
|
||||
{
|
||||
conn->sendCommand("CAL-INT", "dgsyy");
|
||||
}
|
||||
|
||||
if (!digitalRead(4))
|
||||
{
|
||||
conn->sendCommand("READ", "nvcmxznm");
|
||||
}
|
||||
|
||||
delay(300);
|
||||
}
|
||||
|
||||
void calibrationBegin(String args)
|
||||
{
|
||||
for (int i = 0; i < args.toInt(); i++)
|
||||
{
|
||||
digitalWrite(11, LOW);
|
||||
delay(300);
|
||||
digitalWrite(11, HIGH);
|
||||
delay(300);
|
||||
}
|
||||
}
|
||||
257
src/serialConnector.cpp
Normal file
257
src/serialConnector.cpp
Normal file
@@ -0,0 +1,257 @@
|
||||
#include <serialConnector.h>
|
||||
|
||||
// Main polling loop: read incoming serial frames, validate the check-bit,
|
||||
// acknowledge valid messages and dispatch handlers based on the command.
|
||||
void SerialConnector::cycle()
|
||||
{
|
||||
if (Serial.available() > 3)
|
||||
{
|
||||
String raw = Serial.readStringUntil('@');
|
||||
String cmd = getCommandFromIncomming(raw);
|
||||
String args = getArgsFromIncomming(raw);
|
||||
|
||||
if (!verifyCheckBit(cmd, args, getCheckBitFromIncomming(raw).toInt()))
|
||||
{
|
||||
repeat();
|
||||
return;
|
||||
}
|
||||
|
||||
acknowledge(getCheckBitFromIncomming(raw).toInt());
|
||||
if (cmd == "CAL-BGN")
|
||||
{
|
||||
if (calibrationBeginHandler)
|
||||
calibrationBeginHandler(args);
|
||||
}
|
||||
else if (cmd == "CAL-INT")
|
||||
{
|
||||
if (calibrationInteruptHandler)
|
||||
calibrationInteruptHandler(args);
|
||||
}
|
||||
else
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Convert each character in `cmd` into its ASCII hexadecimal representation
|
||||
// and return the concatenated hex string. Example: "A" -> "41".
|
||||
String SerialConnector::stringToHex(String cmd)
|
||||
{
|
||||
int hex_dec;
|
||||
String output = "";
|
||||
|
||||
for (const auto &item : cmd)
|
||||
{
|
||||
|
||||
hex_dec = int(item);
|
||||
char hexaDeciNum[100];
|
||||
int i = 0;
|
||||
|
||||
while (hex_dec != 0)
|
||||
{
|
||||
int temp = 0;
|
||||
temp = hex_dec % 16;
|
||||
if (temp < 10)
|
||||
{
|
||||
hexaDeciNum[i] = temp + 48;
|
||||
i++;
|
||||
}
|
||||
else
|
||||
{
|
||||
hexaDeciNum[i] = temp + 55;
|
||||
i++;
|
||||
}
|
||||
hex_dec = hex_dec / 16;
|
||||
}
|
||||
for (int j = i - 1; j >= 0; j--)
|
||||
{
|
||||
output += hexaDeciNum[j];
|
||||
}
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
// Produce a numeric check value for `cmd` by converting characters to
|
||||
// their hexadecimal digits and summing their digit characters into an
|
||||
// integer representation. This mirrors the protocol used for check-bits.
|
||||
int SerialConnector::stringToCheckNum(String cmd)
|
||||
{
|
||||
int hex_dec;
|
||||
int output = 0;
|
||||
|
||||
for (const auto &item : cmd)
|
||||
{
|
||||
|
||||
hex_dec = int(item);
|
||||
char hexaDeciNum[100];
|
||||
int i = 0;
|
||||
|
||||
while (hex_dec != 0)
|
||||
{
|
||||
int temp = 0;
|
||||
temp = hex_dec % 16;
|
||||
if (temp < 10)
|
||||
{
|
||||
hexaDeciNum[i] = temp + 48;
|
||||
i++;
|
||||
}
|
||||
else
|
||||
{
|
||||
hexaDeciNum[i] = temp + 55;
|
||||
i++;
|
||||
}
|
||||
hex_dec = hex_dec / 16;
|
||||
}
|
||||
for (int j = i - 1; j >= 0; j--)
|
||||
{
|
||||
output += hexaDeciNum[j];
|
||||
}
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
// Return the index of `ch` inside `str`. Returns -1 if `ch` is not found.
|
||||
int SerialConnector::getCharIndex(char ch, String str)
|
||||
{
|
||||
for (int i = 0; i < str.length(); i++)
|
||||
{
|
||||
if (str.charAt(i) == ch)
|
||||
return i;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Extract the command portion from an incoming framed message. The
|
||||
// protocol format is: <CMD>#<ARGS>#<CHECK>@ — this returns <CMD>.
|
||||
String SerialConnector::getCommandFromIncomming(String incomming)
|
||||
{
|
||||
int cmdEnd = getCharIndex('#', incomming);
|
||||
return incomming.substring(0, cmdEnd);
|
||||
}
|
||||
|
||||
// Extract the arguments portion from an incoming message: returns the
|
||||
// text between the first and second '#' characters.
|
||||
String SerialConnector::getArgsFromIncomming(String incomming)
|
||||
{
|
||||
int cmdEnd = getCharIndex('#', incomming);
|
||||
String cm = incomming.substring(0, cmdEnd);
|
||||
|
||||
String afterCmd = incomming.substring(cmdEnd + 1, incomming.length());
|
||||
int cmdArgsEnd = getCharIndex('#', afterCmd);
|
||||
return afterCmd.substring(0, cmdArgsEnd);
|
||||
}
|
||||
|
||||
// Extract the check-bit (final field) from an incoming message: the text
|
||||
// after the second '#' character (up to the '@' frame terminator).
|
||||
String SerialConnector::getCheckBitFromIncomming(String incomming)
|
||||
{
|
||||
int cmdEnd = getCharIndex('#', incomming);
|
||||
String cm = incomming.substring(0, cmdEnd);
|
||||
|
||||
String afterCmd = incomming.substring(cmdEnd + 1, incomming.length());
|
||||
int cmdArgsEnd = getCharIndex('#', afterCmd);
|
||||
return afterCmd.substring(cmdArgsEnd + 1, afterCmd.length());
|
||||
}
|
||||
|
||||
// Verify that `checkBit` equals the computed check for `<cmd>#<args>`.
|
||||
// Returns true when they match, false otherwise.
|
||||
bool SerialConnector::verifyCheckBit(String cmd, String args, int checkBit)
|
||||
{
|
||||
String toCheck = cmd + "#" + args;
|
||||
|
||||
if (stringToCheckNum(toCheck) == checkBit)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
// Send a repeat (RPT) frame asking the remote to resend the last message.
|
||||
void SerialConnector::repeat()
|
||||
{
|
||||
String cmd = "RPT##410@";
|
||||
Serial.print(cmd);
|
||||
}
|
||||
|
||||
// Send an acknowledgement (ACKG) frame that includes the original
|
||||
// command's check-bit so the sender can confirm delivery.
|
||||
void SerialConnector::acknowledge(int checkBit)
|
||||
{
|
||||
String cmd = "ACKG";
|
||||
String toSend = cmd + "#" + (String)checkBit;
|
||||
int checkNum = stringToCheckNum(toSend);
|
||||
String final = toSend + "#" + (String)checkNum + "@";
|
||||
Serial.print(final);
|
||||
}
|
||||
|
||||
// Build and send a framed command with `cmd` and `args`. Appends a computed
|
||||
// check-bit and frame terminator, then waits for follow-up (ack/repeat).
|
||||
void SerialConnector::sendCommand(String cmd, String args)
|
||||
{
|
||||
String toSend = cmd + "#" + args;
|
||||
int checkNum = stringToCheckNum(toSend);
|
||||
String final = toSend + "#" + (String)checkNum + "@";
|
||||
Serial.print(final);
|
||||
|
||||
afterSendCheck(final);
|
||||
}
|
||||
|
||||
// After sending a command, read the next frame and handle either a
|
||||
// repeat (RPT) request or an acknowledgement (ACKG). For RPT, resend the
|
||||
// original command. For ACKG, verify the returned check-bit matches.
|
||||
void SerialConnector::afterSendCheck(String cmd)
|
||||
{
|
||||
String raw = Serial.readStringUntil('@');
|
||||
if (raw == "")
|
||||
afterSendCheck(cmd);
|
||||
|
||||
String comm = getCommandFromIncomming(raw);
|
||||
|
||||
if (comm == "RPT")
|
||||
{
|
||||
sendCommand(getCommandFromIncomming(cmd), getArgsFromIncomming(cmd));
|
||||
}
|
||||
else if (comm == "ACKG")
|
||||
{
|
||||
int check = getArgsFromIncomming(raw).toInt();
|
||||
int cmdCheck = getCheckBitFromIncomming(cmd).toInt();
|
||||
|
||||
if (check != cmdCheck)
|
||||
{
|
||||
// TODO: Implement a command to resend previous command
|
||||
}
|
||||
else
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Register a callback to be invoked when an acknowledgement frame is
|
||||
// received. The callback receives the acknowledgement's args string.
|
||||
void SerialConnector::onAcknowledge(void (*handler)(String args))
|
||||
{
|
||||
acknowledgeHandler = handler;
|
||||
}
|
||||
|
||||
// Register a callback to be invoked when a repeat (RPT) frame is
|
||||
// received. The handler receives the repeat frame's args.
|
||||
void SerialConnector::onRepeat(void (*handler)(String args))
|
||||
{
|
||||
repeatHandler = handler;
|
||||
}
|
||||
|
||||
// Register a callback for the 'CAL-BGN' command. The handler receives the
|
||||
// calibration arguments string when calibration begins.
|
||||
void SerialConnector::onCalibrationBegin(void (*handler)(String args))
|
||||
{
|
||||
calibrationBeginHandler = handler;
|
||||
}
|
||||
|
||||
// Register a callback for the 'CAL-INT' command. The handler receives the
|
||||
// calibration interrupt arguments string when an interrupt occurs.
|
||||
void SerialConnector::onCalibrationInterupt(void (*handler)(String args))
|
||||
{
|
||||
calibrationInteruptHandler = handler;
|
||||
}
|
||||
Reference in New Issue
Block a user