Skip to content

Commit

Permalink
control
Browse files Browse the repository at this point in the history
  • Loading branch information
t0mpr1c3 committed Oct 4, 2023
1 parent 5e21c6e commit e209150
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/ayab/com.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ void Com::send_indState(Err_t error) const {
uint8_t payload[INDSTATE_LEN] = {
static_cast<uint8_t>(API_t::indState),
static_cast<uint8_t>(error),
static_cast<uint8_t>(GlobalController::getState()->state()),
static_cast<uint8_t>(GlobalController::state()),
highByte(leftHallValue),
lowByte(leftHallValue),
highByte(rightHallValue),
Expand All @@ -159,7 +159,7 @@ void Com::send_indState(Err_t error) const {
* \param size The number of bytes in the data buffer.
*/
void Com::onPacketReceived(const uint8_t *buffer, size_t size) {
GlobalController::getState()->com(buffer, size);
GlobalController::com(buffer, size);
}

// Serial command handling
Expand Down
14 changes: 14 additions & 0 deletions src/ayab/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@

// Public methods

/*!
* \brief Return current machine state
*/
OpState_t Controller::state() {
return m_currentState->state();
}

/*!
* \brief Initialize Finite State Machine.
*/
Expand All @@ -57,6 +64,13 @@ void Controller::init() {
m_nextState = &GlobalOpIdle::m_instance;
}

/*!
* \brief Call communication method of current machine state
*/
void Controller::com(const uint8_t *buffer, size_t size) {
m_currentState->com(buffer, size);
}

/*!
* \brief Dispatch on machine state; update machine state
*/
Expand Down
6 changes: 6 additions & 0 deletions src/ayab/controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ class ControllerInterface {
virtual ~ControllerInterface() = default;

// any methods that need to be mocked should go here
virtual OpState_t state() = 0;
virtual void init() = 0;
virtual void com(const uint8_t *buffer, size_t size) = 0;
virtual void update() = 0;
virtual void cacheEncoders() = 0;
virtual void setState(OpInterface *state) = 0;
Expand Down Expand Up @@ -63,7 +65,9 @@ class GlobalController final {
// reference to global instance whose methods are implemented
static ControllerInterface& m_instance;

static OpState_t state();
static void init();
static void com(const uint8_t *buffer, size_t size);
static void update();
static void cacheEncoders();
static void setState(OpInterface *state);
Expand All @@ -79,7 +83,9 @@ class GlobalController final {

class Controller : public ControllerInterface {
public:
OpState_t state() final;
void init() final;
void com(const uint8_t *buffer, size_t size) final;
void update() final;
void cacheEncoders() final;
void setState(OpInterface *state) final;
Expand Down
8 changes: 8 additions & 0 deletions src/ayab/global_controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,18 @@

// static member functions

OpState_t GlobalController::state() {
return m_instance.state();
}

void GlobalController::init() {
m_instance.init();
}

void GlobalController::com(const uint8_t *buffer, size_t size) {
m_instance.com(buffer, size);
}

void GlobalController::update() {
m_instance.update();
}
Expand Down
26 changes: 13 additions & 13 deletions src/ayab/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,21 @@
// Global definitions: references elsewhere must use `extern`.
// Each of the following is a pointer to a singleton class
// containing static methods.
constexpr GlobalAnalogReadAsyncWrapper *analogReadasyncWrapper;
constexpr GlobalPacketSerialWrapper *packetSerialWrapper;
const GlobalAnalogReadAsyncWrapper *analogReadasyncWrapper;
const GlobalPacketSerialWrapper *packetSerialWrapper;

constexpr GlobalBeeper *beeper;
constexpr GlobalCom *com;
constexpr GlobalController *controller;
constexpr GlobalEncoders *encoders;
constexpr GlobalSolenoids *solenoids;
const GlobalBeeper *beeper;
const GlobalCom *com;
const GlobalController *controller;
const GlobalEncoders *encoders;
const GlobalSolenoids *solenoids;

constexpr GlobalOpIdle *opIdle;
constexpr GlobalOpInit *opInit;
constexpr GlobalOpReady *opReady;
constexpr GlobalOpKnit *opKnit;
constexpr GlobalOpTest *opTest;
constexpr GlobalOpError *opError;
const GlobalOpIdle *opIdle;
const GlobalOpInit *opInit;
const GlobalOpReady *opReady;
const GlobalOpKnit *opKnit;
const GlobalOpTest *opTest;
const GlobalOpError *opError;

// Initialize static members.
// Each singleton class contains a reference to a static instance
Expand Down
10 changes: 10 additions & 0 deletions test/mocks/controller_mock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,21 @@ void releaseControllerMock() {
}
}

OpState_t Controller::state() {
assert(gControllerMock != nullptr);
return gControllerMock->state();
}

void Controller::init() {
assert(gControllerMock != nullptr);
gControllerMock->init();
}

void Controller::com(const uint8_t *buffer, size_t size) {
assert(gControllerMock != nullptr);
gControllerMock->com(buffer, size);
}

void Controller::update() {
assert(gControllerMock != nullptr);
gControllerMock->update();
Expand Down
2 changes: 2 additions & 0 deletions test/mocks/controller_mock.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@

class ControllerMock : public ControllerInterface {
public:
MOCK_METHOD0(state, OpState_t());
MOCK_METHOD0(init, void());
MOCK_METHOD2(com, void(const uint8_t *buffer, size_t size));
MOCK_METHOD0(update, void());
MOCK_METHOD0(cacheEncoders, void());
MOCK_METHOD1(setState, void(OpInterface *state));
Expand Down
4 changes: 2 additions & 2 deletions test/test_OpError.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ TEST_F(OpErrorTest, test_update) {
EXPECT_CALL(*arduinoMock, digitalWrite(LED_PIN_A, LOW));
EXPECT_CALL(*arduinoMock, digitalWrite(LED_PIN_B, HIGH));
// send_indState
EXPECT_CALL(*controllerMock, getState).WillOnce(Return(&opError));
EXPECT_CALL(*controllerMock, state);
EXPECT_CALL(*controllerMock, getCarriage);
EXPECT_CALL(*controllerMock, getPosition);
EXPECT_CALL(*controllerMock, getDirection);
Expand All @@ -116,7 +116,7 @@ TEST_F(OpErrorTest, test_update) {
EXPECT_CALL(*arduinoMock, digitalWrite(LED_PIN_A, HIGH));
EXPECT_CALL(*arduinoMock, digitalWrite(LED_PIN_B, LOW));
// send_indState
EXPECT_CALL(*controllerMock, getState).WillOnce(Return(&opError));
EXPECT_CALL(*controllerMock, state);
EXPECT_CALL(*controllerMock, getCarriage);
EXPECT_CALL(*controllerMock, getPosition);
EXPECT_CALL(*controllerMock, getDirection);
Expand Down
4 changes: 2 additions & 2 deletions test/test_OpInit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class OpInitTest : public ::testing::Test {

void expect_ready(bool ready) {
if (ready) {
EXPECT_CALL(*controllerMock, getState).WillOnce(Return(&opInit));
EXPECT_CALL(*controllerMock, state);
}
ASSERT_EQ(opInit.isReady(), ready);
}
Expand Down Expand Up @@ -144,7 +144,7 @@ TEST_F(OpInitTest, test_updateF) {
TEST_F(OpInitTest, test_updateT) {
// isReady() == true
expect_update(get_position_past_left(Machine_t::Kh910), Direction_t::Right, Direction_t::Left);
EXPECT_CALL(*controllerMock, getState).WillOnce(Return(&opInit));
EXPECT_CALL(*controllerMock, state);
EXPECT_CALL(*controllerMock, setState(&opReady));
opInit.update();

Expand Down
8 changes: 5 additions & 3 deletions test/test_com.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,11 @@ class ComTest : public ::testing::Test {

void expected_write_onPacketReceived(uint8_t *buffer, size_t size,
bool once) {
expect_send(once);
EXPECT_CALL(*controllerMock, getState).WillOnce(Return(&opTest));
EXPECT_CALL(*controllerMock, com);
com.onPacketReceived(buffer, size);

expect_send(once);
opTest.com(buffer, size);
}

void reqInit(Machine_t machine) {
Expand Down Expand Up @@ -469,7 +471,7 @@ TEST_F(ComTest, test_send_reqLine) {
TEST_F(ComTest, test_send_indState) {
EXPECT_CALL(*arduinoMock, analogRead(EOL_PIN_L));
EXPECT_CALL(*arduinoMock, analogRead(EOL_PIN_R));
EXPECT_CALL(*controllerMock, getState).WillOnce(Return(&opInit));
EXPECT_CALL(*controllerMock, state);
EXPECT_CALL(*controllerMock, getCarriage);
EXPECT_CALL(*controllerMock, getPosition);
EXPECT_CALL(*controllerMock, getDirection);
Expand Down
17 changes: 17 additions & 0 deletions test/test_controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,14 @@ class ControllerTest : public ::testing::Test {
}
};

TEST_F(ControllerTest, test_state) {
EXPECT_CALL(*opIdleMock, state);
controller.state();

// test expectations without destroying instance
ASSERT_TRUE(Mock::VerifyAndClear(opIdleMock));
}

TEST_F(ControllerTest, test_init) {
EXPECT_CALL(*arduinoMock, pinMode(ENC_PIN_A, INPUT));
EXPECT_CALL(*arduinoMock, pinMode(ENC_PIN_B, INPUT));
Expand Down Expand Up @@ -285,6 +293,15 @@ TEST_F(ControllerTest, test_update_test) {
ASSERT_TRUE(Mock::VerifyAndClear(opTestMock));
}

TEST_F(ControllerTest, test_com) {
const uint8_t buffer[] = {0xFF};
EXPECT_CALL(*opIdleMock, com);
controller.com(buffer, 1);

// test expectations without destroying instance
ASSERT_TRUE(Mock::VerifyAndClear(opIdleMock));
}

TEST_F(ControllerTest, test_error_state) {
controller.setState(&opError);
expected_update_idle();
Expand Down

0 comments on commit e209150

Please sign in to comment.