Skip to content

Commit

Permalink
Centralize command parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
gabekole committed Oct 8, 2024
1 parent 4e99b17 commit dd6f93f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 79 deletions.
25 changes: 1 addition & 24 deletions hardware/RF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ RF::Command RF::GetCommand() // Will check for commands and return the received
std::cout << "GOT: " << buffer;

std::string input_line(buffer);


tcflush(SerialFd, TCIFLUSH);

Expand All @@ -161,27 +160,5 @@ RF::Command RF::GetCommand() // Will check for commands and return the received
}
std::cout << std::endl;

// if statement for which command to return
if(input_line == "ABORT\n")
return RF::Command::ABORT;
else if(input_line == "Startup\n")
return RF::Command::Startup;
else if(input_line == "TestTVC\n")
return RF::Command::TestTVC;
else if(input_line == "GoIdle\n")
return RF::Command::GoIdle;
else if(input_line == "Ignite\n")
return RF::Command::Ignite;
else if(input_line == "Release\n")
return RF::Command::Release;
else if(input_line == "IncrementXTVC\n")
return RF::Command::IncrementXTVC;
else if(input_line == "IncrementYTVC\n")
return RF::Command::IncrementYTVC;
else if(input_line == "DecrementXTVC\n")
return RF::Command::DecrementXTVC;
else if(input_line == "DecrementYTVC\n")
return RF::Command::DecrementYTVC;
else
return RF::Command::None;
return ParseCommand(input_line);
}
28 changes: 1 addition & 27 deletions hardware_simulation/RF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,31 +61,5 @@ RF::Command RF::GetCommand() // Will check for commands and return the received
std::getline(std::cin, input_line);
std::cout << "GOT: " << input_line << "\n" << std::flush;

// if statement for which command to return
RF::Command ParsedCommand = RF::Command::None;

if(input_line == "ABORT")
ParsedCommand = RF::Command::ABORT;
else if(input_line == "Startup")
ParsedCommand = RF::Command::Startup;
else if(input_line == "TestTVC")
ParsedCommand = RF::Command::TestTVC;
else if(input_line == "GoIdle")
ParsedCommand = RF::Command::GoIdle;
else if(input_line == "Ignite")
ParsedCommand = RF::Command::Ignite;
else if(input_line == "IncrementYTVC")
ParsedCommand = RF::Command::IncrementYTVC;
else if(input_line == "IncrementXTVC")
ParsedCommand = RF::Command::IncrementXTVC;
else if(input_line == "DecrementXTVC")
ParsedCommand = RF::Command::DecrementXTVC;
else if(input_line == "DecrementYTVC")
ParsedCommand = RF::Command::DecrementYTVC;
else if(input_line == "Release")
ParsedCommand = RF::Command::Release;
else
ParsedCommand = RF::Command::None;

return ParsedCommand;
return ParseCommand(input_line);
}
28 changes: 1 addition & 27 deletions hardware_test/RF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,31 +61,5 @@ RF::Command RF::GetCommand() // Will check for commands and return the received
std::getline(std::cin, input_line);
std::cout << "GOT: " << input_line << "\n" << std::flush;

// if statement for which command to return
RF::Command ParsedCommand = RF::Command::None;

if(input_line == "ABORT")
ParsedCommand = RF::Command::ABORT;
else if(input_line == "Startup")
ParsedCommand = RF::Command::Startup;
else if(input_line == "TestTVC")
ParsedCommand = RF::Command::TestTVC;
else if(input_line == "GoIdle")
ParsedCommand = RF::Command::GoIdle;
else if(input_line == "Ignite")
ParsedCommand = RF::Command::Ignite;
else if(input_line == "IncrementYTVC")
ParsedCommand = RF::Command::IncrementYTVC;
else if(input_line == "IncrementXTVC")
ParsedCommand = RF::Command::IncrementXTVC;
else if(input_line == "DecrementXTVC")
ParsedCommand = RF::Command::DecrementXTVC;
else if(input_line == "DecrementYTVC")
ParsedCommand = RF::Command::DecrementYTVC;
else if(input_line == "Release")
ParsedCommand = RF::Command::Release;
else
ParsedCommand = RF::Command::None;

return ParsedCommand;
return ParseCommand(input_line);
}
37 changes: 36 additions & 1 deletion include/RF.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include <string>
#include <fstream>
#include <stdio.h>

#include <map>
#include <regex>

#define FRAME_MAGIC_NUMBER 0xDEADBEEF // signals a new data frame, used in RF transmission
#define STRING_MAGIC_NUMBER 0xBABAFACE // Signals a new string, used in RF transmission
Expand Down Expand Up @@ -33,6 +34,40 @@ class RF {
};


ParseCommand(std::string input_line){
// Define the regular expression to match leading and trailing whitespace/newline characters
std::regex pattern("^\\s+|\\s+$");

// Replace leading and trailing whitespace/newline characters with an empty string
input_line = std::regex_replace(input_line, pattern, "");

RF::Command ParsedCommand = RF::Command::None;
if(input_line == "ABORT")
ParsedCommand = RF::Command::ABORT;
else if(input_line == "Startup")
ParsedCommand = RF::Command::Startup;
else if(input_line == "TestTVC")
ParsedCommand = RF::Command::TestTVC;
else if(input_line == "GoIdle")
ParsedCommand = RF::Command::GoIdle;
else if(input_line == "Ignite")
ParsedCommand = RF::Command::Ignite;
else if(input_line == "IncrementYTVC")
ParsedCommand = RF::Command::IncrementYTVC;
else if(input_line == "IncrementXTVC")
ParsedCommand = RF::Command::IncrementXTVC;
else if(input_line == "DecrementXTVC")
ParsedCommand = RF::Command::DecrementXTVC;
else if(input_line == "DecrementYTVC")
ParsedCommand = RF::Command::DecrementYTVC;
else if(input_line == "Release")
ParsedCommand = RF::Command::Release;
else
ParsedCommand = RF::Command::None;

return ParsedCommand;
}

struct rfFrame // A structure derrived from telemFrame that only contains a subset of attributes in order to save space.
{
uint32_t magic_number;
Expand Down

0 comments on commit dd6f93f

Please sign in to comment.