Initial commit
This commit is contained in:
58
lib/SerialMessage/BluetoothSerialMessage.cpp
Normal file
58
lib/SerialMessage/BluetoothSerialMessage.cpp
Normal file
@@ -0,0 +1,58 @@
|
||||
#include "BluetoothSerialMessage.h"
|
||||
|
||||
BluetoothSerialMessage::BluetoothSerialMessage(BluetoothSerial *serial){
|
||||
this->serial = serial;
|
||||
}
|
||||
|
||||
void BluetoothSerialMessage::Init(unsigned int baud_rate){
|
||||
// Don't need to do anything here, just let the user init bluetooth serial
|
||||
}
|
||||
|
||||
void BluetoothSerialMessage::PrintArgs(){
|
||||
serial->print("Current number of args: ");
|
||||
serial->println(populated_args);
|
||||
for (int i = 0; i < populated_args; i++) {
|
||||
serial->print(args[i]);
|
||||
serial->print(" ");
|
||||
}
|
||||
serial->println();
|
||||
}
|
||||
|
||||
void BluetoothSerialMessage::readSerial(){
|
||||
boolean recvInProgress = false;
|
||||
byte ndx = 0;
|
||||
char c;
|
||||
|
||||
// read the incoming serial data:
|
||||
while (serial->available() > 0 && data_recieved == false) {
|
||||
// get the neext character in the serial buffer
|
||||
c = serial->read();
|
||||
Serial.print(c);
|
||||
|
||||
// only execute this if the startMarker has been received
|
||||
if (recvInProgress == true) {
|
||||
// if the incoming character is not the endMarker...
|
||||
if (c != endMarker) {
|
||||
// add it to the data array
|
||||
data[ndx] = c;
|
||||
ndx++; // increment the data array index
|
||||
// if the index is greater than the maximum data array size,
|
||||
// keep overwriting the last element until the endMarker is received.
|
||||
if (ndx >= num_chars) {
|
||||
ndx = num_chars - 1;
|
||||
}
|
||||
}
|
||||
// if the incoming character is the endMarker clean up and set the flags
|
||||
else {
|
||||
data[ndx] = '\0'; // terminate the string
|
||||
recvInProgress = false;
|
||||
ndx = 0;
|
||||
data_recieved = true;
|
||||
}
|
||||
}
|
||||
// if the incoming character is the startMarker, set the recvInProgress flag
|
||||
else if (c == startMarker) {
|
||||
recvInProgress = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
31
lib/SerialMessage/BluetoothSerialMessage.h
Normal file
31
lib/SerialMessage/BluetoothSerialMessage.h
Normal file
@@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
#include "SerialMessage.h"
|
||||
#include <BluetoothSerial.h>
|
||||
|
||||
class BluetoothSerialMessage : public SerialMessage{
|
||||
public:
|
||||
/**
|
||||
* @brief Construct a new Bluetooth Serial Message object
|
||||
*/
|
||||
BluetoothSerialMessage(BluetoothSerial *serial);
|
||||
|
||||
/**
|
||||
* @brief Initialize the BluetoothSerialMessage object
|
||||
*/
|
||||
void Init(unsigned int baud_rate = 115200) override;
|
||||
|
||||
/**
|
||||
* @brief prints the args array to the serial monitor
|
||||
*/
|
||||
void PrintArgs() override;
|
||||
|
||||
private:
|
||||
/**
|
||||
* @brief reads the serial data and stores it in the data array
|
||||
*/
|
||||
void readSerial() override;
|
||||
|
||||
BluetoothSerial *serial;
|
||||
|
||||
|
||||
};
|
||||
111
lib/SerialMessage/SerialMessage.cpp
Normal file
111
lib/SerialMessage/SerialMessage.cpp
Normal file
@@ -0,0 +1,111 @@
|
||||
/**
|
||||
* @file SerialMessage.cpp
|
||||
* @brief This file contains the SerialMessage class
|
||||
* @details This file contains the SerialMessage class which is used to parse serial messages
|
||||
* @version 1.0.0
|
||||
* @author Quinn Henthorne. Contact: quinn.henthorne@gmail.com
|
||||
*/
|
||||
|
||||
#include "SerialMessage.h"
|
||||
|
||||
SerialMessage::SerialMessage(HardwareSerial *serial) :
|
||||
serial(serial){}
|
||||
|
||||
void SerialMessage::Init(unsigned int baud_rate){
|
||||
serial->begin(baud_rate);
|
||||
}
|
||||
|
||||
void SerialMessage::readSerial(){
|
||||
char c;
|
||||
|
||||
// read the incoming serial data:
|
||||
while (serial->available() > 0 && data_recieved == false) {
|
||||
// get the neext character in the serial buffer
|
||||
c = serial->read();
|
||||
// only execute this if the startMarker has been received
|
||||
// if the incoming character is the endMarker clean up and set the flags
|
||||
if (recvInProgress == true) {
|
||||
if (c == endMarker) {
|
||||
data[ndx] = '\0'; // terminate the string
|
||||
recvInProgress = false;
|
||||
ndx = 0;
|
||||
data_recieved = true;
|
||||
}
|
||||
// if the incoming character is not the endMarker
|
||||
else {
|
||||
// add it to the data array
|
||||
data[ndx] = c;
|
||||
ndx++; // increment the data array index
|
||||
// if the index is greater than the maximum data array size,
|
||||
// keep overwriting the last element until the endMarker is received.
|
||||
if (ndx >= num_chars) {
|
||||
ndx = num_chars - 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
// if the incoming character is the startMarker, set the recvInProgress flag
|
||||
else if (c == startMarker) {
|
||||
recvInProgress = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void SerialMessage::parseData() { // split the data into its parts
|
||||
this->populated_args = 0; // reset the populated args counter
|
||||
char * indx; // this is used by strtok() as an index
|
||||
int i = 0;
|
||||
indx = strtok(temp_data, ","); // get the first part - the string
|
||||
while(indx != NULL){
|
||||
this->args[i] = atoi(indx);
|
||||
populated_args++;
|
||||
i++;
|
||||
indx = strtok(NULL, ","); // this continues where the previous call left off
|
||||
}
|
||||
}
|
||||
|
||||
void SerialMessage::Update(){
|
||||
readSerial();
|
||||
if (data_recieved == true) {
|
||||
// for debug only:
|
||||
// Serial.print("Received:");
|
||||
// Serial.print(data);
|
||||
// Serial.println(":End");
|
||||
strcpy(temp_data, data);
|
||||
// this temporary copy is necessary to protect the original data
|
||||
// because strtok() used in parseData() replaces the commas with \0
|
||||
parseData();
|
||||
//PrintArgs();
|
||||
data_recieved = false;
|
||||
new_data = true;
|
||||
}
|
||||
}
|
||||
|
||||
bool SerialMessage::IsNewData(){
|
||||
return new_data;
|
||||
}
|
||||
|
||||
void SerialMessage::ClearNewData(){
|
||||
new_data = false;
|
||||
}
|
||||
|
||||
int * SerialMessage::GetArgs(){
|
||||
return args;
|
||||
}
|
||||
|
||||
int SerialMessage::GetArgsLength(){
|
||||
return args_length;
|
||||
}
|
||||
|
||||
int SerialMessage::GetPopulatedArgs(){
|
||||
return populated_args;
|
||||
}
|
||||
|
||||
void SerialMessage::PrintArgs(){
|
||||
serial->print("Current number of args: ");
|
||||
serial->println(populated_args);
|
||||
for (int i = 0; i < populated_args; i++) {
|
||||
serial->print(args[i]);
|
||||
serial->print(" ");
|
||||
}
|
||||
serial->println();
|
||||
}
|
||||
87
lib/SerialMessage/SerialMessage.h
Normal file
87
lib/SerialMessage/SerialMessage.h
Normal file
@@ -0,0 +1,87 @@
|
||||
/**
|
||||
* @file SerialMessage.h
|
||||
* @brief This file contains the SerialMessage class
|
||||
* @details This file contains the SerialMessage class which is used to parse serial messages
|
||||
* @version 1.0.0
|
||||
* @author Quinn Henthorne. Contact: quinn.henthorne@gmail.com
|
||||
*/
|
||||
|
||||
#ifndef SERIALMESSAGE_H
|
||||
#define SERIALMESSAGE_H
|
||||
|
||||
#include "Arduino.h"
|
||||
|
||||
#define num_chars 500
|
||||
|
||||
class SerialMessage{
|
||||
public:
|
||||
/**
|
||||
* @brief Construct a new Serial Message object
|
||||
*/
|
||||
SerialMessage(HardwareSerial *serial = &Serial);
|
||||
|
||||
/**
|
||||
* @brief Initialize the SerialMessage object
|
||||
*/
|
||||
virtual void Init(unsigned int baud_rate = 115200);
|
||||
|
||||
/**
|
||||
* @brief Update the SerialMessage object and parse any data that's available
|
||||
*/
|
||||
void Update();
|
||||
|
||||
/**
|
||||
* @brief Returns true if there is new data available
|
||||
* @return true if there is new data available
|
||||
*/
|
||||
bool IsNewData();
|
||||
|
||||
/**
|
||||
* @brief Clears the new data flag
|
||||
*/
|
||||
virtual void ClearNewData();
|
||||
|
||||
/**
|
||||
* @brief Return a pointer to the args array
|
||||
* @return a pointer to the args array
|
||||
*/
|
||||
int * GetArgs();
|
||||
|
||||
/**
|
||||
* @brief Returns the number of args that have been populated for the current message
|
||||
* @return the number of args that have been populated for the current message
|
||||
*/
|
||||
int GetArgsLength();
|
||||
|
||||
/**
|
||||
* @brief Returns the number of args that have been populated for the current message
|
||||
* @return the number of args that have been populated for the current message
|
||||
*/
|
||||
int GetPopulatedArgs();
|
||||
|
||||
/**
|
||||
* @brief Prints the args array to the serial monitor
|
||||
*/
|
||||
virtual void PrintArgs();
|
||||
|
||||
protected:
|
||||
virtual void readSerial();
|
||||
virtual void parseData();
|
||||
|
||||
bool new_data = false;
|
||||
bool data_recieved = false;
|
||||
bool recvInProgress = false;
|
||||
char data[num_chars]; // an array to store the received data
|
||||
char temp_data[num_chars]; // an array that will be used with strtok()
|
||||
uint16_t ndx = 0;
|
||||
const static int args_length = 30;
|
||||
int populated_args = 0; // the number of args that have been populated for the current message
|
||||
int args[args_length];
|
||||
const char startMarker = '!';
|
||||
const char endMarker = ';';
|
||||
|
||||
private:
|
||||
HardwareSerial *serial;
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user