Added comments to all functions
This commit is contained in:
@@ -25,53 +25,92 @@ private:
|
||||
void (*calibrationBeginHandler)(String args);
|
||||
void (*calibrationInteruptHandler)(String args);
|
||||
|
||||
// Convert each character of `cmd` to its hexadecimal representation
|
||||
// and return the concatenated hex string.
|
||||
/**
|
||||
* Convert a string into its hexadecimal representation.
|
||||
* @param cmd Input string to convert.
|
||||
* @return Hexadecimal representation of the input 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.
|
||||
/**
|
||||
* Convert a string into a numeric checksum used by the protocol.
|
||||
* @param cmd Input string to convert.
|
||||
* @return Integer checksum computed from the input string.
|
||||
*/
|
||||
int stringToCheckNum(String cmd);
|
||||
|
||||
// Return the index of character `ch` in `str`, or -1 if not found.
|
||||
/**
|
||||
* Find the index of a character inside a string.
|
||||
* @param ch Character to search for.
|
||||
* @param str String to search within.
|
||||
* @return Index of the character if found, -1 otherwise.
|
||||
*/
|
||||
int getCharIndex(char ch, String str);
|
||||
|
||||
// Parse an incoming framed message and return the command portion
|
||||
// (everything before the first '#').
|
||||
/**
|
||||
* Extract the command portion from an incoming raw message.
|
||||
* The message format is expected to be: CMD#ARGS#CHECK@ (terminator '@').
|
||||
* @param incomming Raw incoming message string.
|
||||
* @return The command portion of the incoming message.
|
||||
*/
|
||||
String getCommandFromIncomming(String incomming);
|
||||
|
||||
// Parse an incoming framed message and return the arguments portion
|
||||
// (text between the first and second '#').
|
||||
/**
|
||||
* Extract the arguments portion from an incoming raw message.
|
||||
* @param incomming Raw incoming message string.
|
||||
* @return The arguments portion of the incoming message.
|
||||
*/
|
||||
String getArgsFromIncomming(String incomming);
|
||||
|
||||
// Parse an incoming framed message and return the final check-bit
|
||||
// portion (everything after the second '#').
|
||||
/**
|
||||
* Extract the check bit (checksum) portion from an incoming raw message.
|
||||
* @param incomming Raw incoming message string.
|
||||
* @return The check bit (as string) of the incoming message.
|
||||
*/
|
||||
String getCheckBitFromIncomming(String incomming);
|
||||
|
||||
// Verify that the provided `checkBit` matches the computed check for
|
||||
// the combination of `cmd` and `args`. Returns true when valid.
|
||||
/**
|
||||
* Verify that the provided checkBit matches the computed checksum for cmd and args.
|
||||
* @param cmd Command portion.
|
||||
* @param args Arguments portion.
|
||||
* @param checkBit Integer check bit to verify against.
|
||||
* @return true if checksum matches, false otherwise.
|
||||
*/
|
||||
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.
|
||||
/**
|
||||
* Send a repeat request to the other side.
|
||||
* This will send the protocol message that instructs the peer to re-send.
|
||||
*/
|
||||
void repeat();
|
||||
|
||||
// Send an acknowledgement message containing `checkBit` back to the
|
||||
// remote side.
|
||||
/**
|
||||
* Send an acknowledgement for a received message.
|
||||
* @param checkBit Check bit value to include in the acknowledgement.
|
||||
*/
|
||||
void acknowledge(int checkBit);
|
||||
|
||||
// After sending a command, wait for and handle any follow-up messages
|
||||
// (repeat or acknowledgement) related to `cmd`.
|
||||
/**
|
||||
* After sending a command, read for the peer's reply (ACKG or RPT) and act accordingly.
|
||||
* @param cmd The full command string that was sent (including checksum and terminator).
|
||||
*/
|
||||
void afterSendCheck(String cmd);
|
||||
|
||||
public:
|
||||
/**
|
||||
* Periodically called to process incoming serial data and dispatch events.
|
||||
* Should be called frequently from the main loop.
|
||||
*/
|
||||
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));
|
||||
|
||||
/**
|
||||
* Send a command using the defined protocol. The function composes the message,
|
||||
* computes the checksum and sends the final string terminated with '@'.
|
||||
* @param cmd Command name (e.g., "READ").
|
||||
* @param args Arguments for the command.
|
||||
*/
|
||||
void sendCommand(String cmd, String args);
|
||||
};
|
||||
Reference in New Issue
Block a user