Files
Serial-Comms-Protocol-Arduino/include/serialConnector.h
2026-01-25 15:36:43 +01:00

66 lines
2.2 KiB
C++

#pragma once
#include <Arduino.h>
class SerialConnector
{
private:
String previouSent;
String received;
String command;
void (*acknowledgeHandler)(String args);
void (*repeatHandler)(String args);
void (*calibrationBeginHandler)(String args);
void (*calibrationInteruptHandler)(String args);
// Convert each character of `cmd` to its hexadecimal representation
// and return the concatenated hex string.
String stringToHex(String cmd);
// Compute a numeric 'check' value from `cmd` by converting chars to
// their hex digits and summing/concatenating (project-specific logic).
// Returns an integer suitable for use as a check-bit value.
int stringToCheckNum(String cmd);
// Return the index of character `ch` in `str`, or -1 if not found.
int getCharIndex(char ch, String str);
// Parse an incoming framed message and return the command portion
// (everything before the first '#').
String getCommandFromIncomming(String incomming);
// Parse an incoming framed message and return the arguments portion
// (text between the first and second '#').
String getArgsFromIncomming(String incomming);
// Parse an incoming framed message and return the final check-bit
// portion (everything after the second '#').
String getCheckBitFromIncomming(String incomming);
// Verify that the provided `checkBit` matches the computed check for
// the combination of `cmd` and `args`. Returns true when valid.
bool verifyCheckBit(String cmd, String args, int checkBit);
// Send a repeat request to the remote side to indicate the last
// message should be re-sent.
void repeat();
// Send an acknowledgement message containing `checkBit` back to the
// remote side.
void acknowledge(int checkBit);
// After sending a command, wait for and handle any follow-up messages
// (repeat or acknowledgement) related to `cmd`.
void afterSendCheck(String cmd);
public:
void cycle();
void onAcknowledge(void (*handler)(String args));
void onRepeat(void (*handler)(String args));
void onCalibrationBegin(void (*handler)(String args));
void onCalibrationInterupt(void (*handler)(String args));
void sendCommand(String cmd, String args);
};