From abedf79d90040c7439d8b209f4247f6b194e4c6c Mon Sep 17 00:00:00 2001 From: Javier Balloffet Date: Sun, 18 Feb 2024 08:18:03 -0300 Subject: [PATCH] Modify Shell class API to allow dependency injection (#221) Signed-off-by: Javier Balloffet --- andino_firmware/src/app.cpp | 7 +++++-- andino_firmware/src/app.h | 4 ++++ andino_firmware/src/shell.cpp | 6 +++--- andino_firmware/src/shell.h | 12 ++++++------ 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/andino_firmware/src/app.cpp b/andino_firmware/src/app.cpp index 042a32c7..f9577468 100644 --- a/andino_firmware/src/app.cpp +++ b/andino_firmware/src/app.cpp @@ -75,10 +75,13 @@ #include "motor.h" #include "pid.h" #include "pwm_out_arduino.h" +#include "serial_stream_arduino.h" #include "shell.h" namespace andino { +SerialStreamArduino App::serial_stream_; + Shell App::shell_; DigitalOutArduino App::left_motor_enable_digital_out_(Hw::kLeftMotorEnableGpioPin); @@ -116,7 +119,7 @@ void App::setup() { // Required by Arduino libraries to work. init(); - Serial.begin(Constants::kBaudrate); + serial_stream_.begin(Constants::kBaudrate); left_encoder_.begin(); right_encoder_.begin(); @@ -130,7 +133,7 @@ void App::setup() { right_pid_controller_.reset(right_encoder_.read()); // Initialize command shell. - shell_.begin(Serial); + shell_.set_serial_stream(&serial_stream_); shell_.set_default_callback(cmd_unknown_cb); shell_.register_command(Commands::kReadAnalogGpio, cmd_read_analog_gpio_cb); shell_.register_command(Commands::kReadDigitalGpio, cmd_read_digital_gpio_cb); diff --git a/andino_firmware/src/app.h b/andino_firmware/src/app.h index 91633dc1..937c7827 100644 --- a/andino_firmware/src/app.h +++ b/andino_firmware/src/app.h @@ -35,6 +35,7 @@ #include "motor.h" #include "pid.h" #include "pwm_out_arduino.h" +#include "serial_stream_arduino.h" #include "shell.h" namespace andino { @@ -82,6 +83,9 @@ class App { /// Callback method for the `Commands::kSetPidsTuningGains` command. static void cmd_set_pid_tuning_gains_cb(int argc, char** argv); + /// Serial stream. + static SerialStreamArduino serial_stream_; + /// Application command shell. static Shell shell_; diff --git a/andino_firmware/src/shell.cpp b/andino_firmware/src/shell.cpp index ca72c759..61aea393 100644 --- a/andino_firmware/src/shell.cpp +++ b/andino_firmware/src/shell.cpp @@ -33,7 +33,7 @@ namespace andino { -void Shell::begin(Stream& stream) { stream_ = &stream; } +void Shell::set_serial_stream(const SerialStream* serial_stream) { serial_stream_ = serial_stream; } void Shell::set_default_callback(CommandCallback callback) { default_callback_ = callback; } @@ -49,8 +49,8 @@ void Shell::register_command(const char* name, CommandCallback callback) { } void Shell::process_input() { - while (stream_->available() > 0) { - const char input = stream_->read(); + while (serial_stream_->available() > 0) { + const char input = serial_stream_->read(); switch (input) { case '\r': diff --git a/andino_firmware/src/shell.h b/andino_firmware/src/shell.h index 1d1e0cff..f9c5d45d 100644 --- a/andino_firmware/src/shell.h +++ b/andino_firmware/src/shell.h @@ -31,7 +31,7 @@ #include -#include "Arduino.h" +#include "serial_stream.h" namespace andino { @@ -41,10 +41,10 @@ class Shell { /// @brief Command callback type. typedef void (*CommandCallback)(int argc, char** argv); - /// @brief Initializes the shell. + /// @brief Sets the serial stream to use. /// - /// @param stream Data stream. - void begin(Stream& stream); + /// @param serial_stream Serial stream. + void set_serial_stream(const SerialStream* serial_stream); /// @brief Sets the default callback for unknown commands. /// @@ -88,8 +88,8 @@ class Shell { /// Executes the corresponding command callback function. void execute_callback(int argc, char** argv); - /// Data stream. - Stream* stream_{nullptr}; + /// Serial stream. + const SerialStream* serial_stream_{nullptr}; /// Default callback for unknown commands. CommandCallback default_callback_{nullptr};