Skip to content

Commit

Permalink
Modify Shell class API to allow dependency injection (#221)
Browse files Browse the repository at this point in the history
Signed-off-by: Javier Balloffet <[email protected]>
  • Loading branch information
jballoffet authored Feb 18, 2024
1 parent 664df3c commit abedf79
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
7 changes: 5 additions & 2 deletions andino_firmware/src/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand All @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions andino_firmware/src/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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_;

Expand Down
6 changes: 3 additions & 3 deletions andino_firmware/src/shell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand All @@ -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':
Expand Down
12 changes: 6 additions & 6 deletions andino_firmware/src/shell.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

#include <stddef.h>

#include "Arduino.h"
#include "serial_stream.h"

namespace andino {

Expand All @@ -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.
///
Expand Down Expand Up @@ -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};
Expand Down

0 comments on commit abedf79

Please sign in to comment.