Pulled the most up to date serial driver
This commit is contained in:
3
.gitmodules
vendored
Normal file
3
.gitmodules
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
[submodule "lib/SerialMessage"]
|
||||||
|
path = lib/SerialMessage
|
||||||
|
url = https://github.com/Cynopolis/SerialMessage.git
|
||||||
1
lib/SerialMessage
Submodule
1
lib/SerialMessage
Submodule
Submodule lib/SerialMessage added at 7cd43742b6
@@ -1,58 +0,0 @@
|
|||||||
#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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,31 +0,0 @@
|
|||||||
#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;
|
|
||||||
|
|
||||||
|
|
||||||
};
|
|
||||||
@@ -1,111 +0,0 @@
|
|||||||
/**
|
|
||||||
* @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();
|
|
||||||
}
|
|
||||||
@@ -1,87 +0,0 @@
|
|||||||
/**
|
|
||||||
* @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
|
|
||||||
12
src/main.cpp
12
src/main.cpp
@@ -25,7 +25,7 @@ enum Commands : uint8_t{
|
|||||||
// --------------------------------------------------
|
// --------------------------------------------------
|
||||||
// BluetoothSerial SerialBT;
|
// BluetoothSerial SerialBT;
|
||||||
// BluetoothSerialMessage serialMessageBT(&SerialBT);
|
// BluetoothSerialMessage serialMessageBT(&SerialBT);
|
||||||
SerialMessage serialMessage(&Serial);
|
SerialMessage<500> serialMessage(&Serial);
|
||||||
BoardLayout board(BOARD_WIDTH, BOARD_LENGTH, BOARD_HEIGHT, stacks);
|
BoardLayout board(BOARD_WIDTH, BOARD_LENGTH, BOARD_HEIGHT, stacks);
|
||||||
|
|
||||||
// Temporary thing until we can get bluetooth color management working on the quest
|
// Temporary thing until we can get bluetooth color management working on the quest
|
||||||
@@ -61,9 +61,9 @@ void printBoardState(){
|
|||||||
// SerialBT.println(";");
|
// SerialBT.println(";");
|
||||||
}
|
}
|
||||||
|
|
||||||
void setStackColor(int * args, int argsLength){
|
void setStackColor(uint32_t * args, int argsLength){
|
||||||
int stackNum = args[1];
|
uint32_t stackNum = args[1];
|
||||||
int numColors = (argsLength - 2) / 3;
|
uint32_t numColors = (argsLength - 2) / 3;
|
||||||
Color colors[numColors];
|
Color colors[numColors];
|
||||||
|
|
||||||
for(int i = 0; i < numColors; i++){
|
for(int i = 0; i < numColors; i++){
|
||||||
@@ -78,8 +78,8 @@ void setStackColor(int * args, int argsLength){
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void parseData(int * args, int argsLength){
|
void parseData(uint32_t * args, int argsLength){
|
||||||
int command = args[0];
|
uint32_t command = args[0];
|
||||||
switch(command){
|
switch(command){
|
||||||
case Commands::BoardState:
|
case Commands::BoardState:
|
||||||
printBoardState();
|
printBoardState();
|
||||||
|
|||||||
Reference in New Issue
Block a user