Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pointers -> references #3

Open
wants to merge 2 commits into
base: wrap
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/ayab/analogReadAsyncWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class AnalogReadAsyncWrapperInterface {
};

// Container class for the static method analogReadAsync.
// Dependency injection is enabled using a pointer to a global instance of
// Dependency injection is enabled using a reference to a global instance of
// either `AnalogReadAsyncWrapper` or `AnalogReadAsyncWrapperMock`,
// both of which classes implement the
// pure virtual methods of `AnalogReadAsyncWrapperInterface`.
Expand All @@ -47,8 +47,8 @@ class GlobalAnalogReadAsyncWrapper final {
GlobalAnalogReadAsyncWrapper() = default;

public:
// pointer to global instance whose methods are implemented
static AnalogReadAsyncWrapperInterface *m_instance;
// reference to global instance whose methods are implemented
static AnalogReadAsyncWrapperInterface& m_instance;

static void analogReadAsyncWrapped(uint8_t pin, analogReadCompleteCallback_t cb = nullptr, const void *data = nullptr);
};
Expand Down
6 changes: 3 additions & 3 deletions src/ayab/beeper.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class BeeperInterface {
};

// Container class for the static methods that control the beeper.
// Dependency injection is enabled using a pointer to a global instance of
// Dependency injection is enabled using a reference to a global instance of
// either `Beeper` or `BeeperMock`, both of which classes implement the
// pure virtual methods of `BeeperInterface`.

Expand All @@ -63,8 +63,8 @@ class GlobalBeeper final {
GlobalBeeper() = default;

public:
// pointer to global instance whose methods are implemented
static BeeperInterface *m_instance;
// reference to global instance whose methods are implemented
static BeeperInterface& m_instance;

static void init(bool enabled);
static void update();
Expand Down
17 changes: 6 additions & 11 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 @@ -152,16 +152,15 @@ void Com::send_indState(Err_t error) const {
send(static_cast<uint8_t *>(payload), INDSTATE_LEN);
}

/*! GCOVR_EXCL_START
/*!
*
* \brief Callback for PacketSerial.
* \param buffer A pointer to a data buffer.
* \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);
}
// GCOVR_EXCL_STOP

// Serial command handling

Expand Down Expand Up @@ -191,7 +190,7 @@ void Com::h_reqInit(const uint8_t *buffer, size_t size) {
}

GlobalController::setMachineType(machineType);
GlobalController::setState(GlobalOpInit::m_instance);
GlobalController::setState(&GlobalOpInit::m_instance);
send_cnfInit(Err_t::Success);
}

Expand Down Expand Up @@ -277,8 +276,6 @@ void Com::h_cnfLine(const uint8_t *buffer, size_t size) {

/*!
* \brief Handle `reqInfo` (request information) command.
* \param buffer A pointer to a data buffer.
* \param size The number of bytes in the data buffer.
*/
void Com::h_reqInfo() const {
send_cnfInfo();
Expand All @@ -288,22 +285,21 @@ void Com::h_reqInfo() const {
* \brief Handle `reqTest` (request hardware test) command.
*/
void Com::h_reqTest() const {
GlobalController::setState(GlobalOpTest::m_instance);
GlobalController::setState(&GlobalOpTest::m_instance);
send_cnfTest(Err_t::Success);
}

/*!
* \brief Handle `quitCmd` (cancel) command.
*/
void Com::h_quitCmd() const {
GlobalController::setState(GlobalOpInit::m_instance);
GlobalController::setState(&GlobalOpInit::m_instance);
}

/*!
* \brief Handle unrecognized command.
*/
void Com::h_unrecognized() const {
// do nothing
}

/*!
Expand Down Expand Up @@ -334,7 +330,6 @@ void Com::send_cnfInit(Err_t error) const {
send(payload, 2);
}


/*!
* \brief Send `cnfStart` message.
* \param error Error code (0 = success, other values = error).
Expand Down
6 changes: 3 additions & 3 deletions src/ayab/com.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class ComInterface {
};

// Container class for the static methods that implement the serial API.
// Dependency injection is enabled using a pointer to a global instance of
// Dependency injection is enabled using a reference to a global instance of
// either `Com` or `ComMock`, both of which classes implement the
// pure virtual methods of `ComInterface`.

Expand All @@ -112,8 +112,8 @@ class GlobalCom final {
GlobalCom() = default;

public:
// pointer to global instance whose methods are implemented
static ComInterface *m_instance;
// reference to global instance whose methods are implemented
static ComInterface& m_instance;

static void init();
static void update();
Expand Down
20 changes: 17 additions & 3 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 @@ -53,8 +60,15 @@ void Controller::init() {
m_hallActive = Direction_t::NoDirection;
m_beltShift = BeltShift_t::Unknown;
m_position = 0;
m_currentState = GlobalOpIdle::m_instance;
m_nextState = GlobalOpIdle::m_instance;
m_currentState = &GlobalOpIdle::m_instance;
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);
}

/*!
Expand Down Expand Up @@ -109,7 +123,7 @@ void Controller::setState(OpInterface *state) {
* \brief Get machine state.
* \return Current state of Finite State Machine.
*/
OpInterface *Controller::getState() {
OpInterface* Controller::getState() {
return m_currentState;
}

Expand Down
18 changes: 12 additions & 6 deletions src/ayab/controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ 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;
virtual OpInterface *getState() = 0;
virtual OpInterface* getState() = 0;
virtual void setMachineType(Machine_t) = 0;
virtual Machine_t getMachineType() = 0;
virtual BeltShift_t getBeltShift() = 0;
Expand All @@ -49,7 +51,7 @@ class ControllerInterface {
};

// Singleton container class for static methods.
// Dependency injection is enabled using a pointer
// Dependency injection is enabled using a reference
// to a global instance of either `Controller` or `ControllerMock`
// both of which classes implement the pure virtual methods
// of the `ControllerInterface` class.
Expand All @@ -60,14 +62,16 @@ class GlobalController final {
GlobalController() = default;

public:
// pointer to global instance whose methods are implemented
static ControllerInterface *m_instance;
// 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);
static OpInterface *getState();
static OpInterface* getState();
static void setMachineType(Machine_t);
static Machine_t getMachineType();
static BeltShift_t getBeltShift();
Expand All @@ -79,11 +83,13 @@ 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;
OpInterface *getState() final;
OpInterface* getState() final;
void setMachineType(Machine_t) final;
Machine_t getMachineType() final;
BeltShift_t getBeltShift() final;
Expand Down
6 changes: 3 additions & 3 deletions src/ayab/encoders.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class EncodersInterface {
};

// Container class for the static methods for the encoders.
// Dependency injection is enabled using a pointer to a global instance of
// Dependency injection is enabled using a reference to a global instance of
// either `Encoders` or `EncodersMock`, both of which classes implement the
// pure virtual methods of `EncodersInterface`.

Expand All @@ -145,8 +145,8 @@ class GlobalEncoders final {
GlobalEncoders() = default;

public:
// pointer to global instance whose methods are implemented
static EncodersInterface *m_instance;
// reference to global instance whose methods are implemented
static EncodersInterface& m_instance;

static void init(Machine_t machineType);
static void setUpInterrupt();
Expand Down
12 changes: 6 additions & 6 deletions src/ayab/global_OpError.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,25 @@
// static member functions

OpState_t GlobalOpError::state() {
return m_instance->state();
return m_instance.state();
}

void GlobalOpError::init() {
m_instance->init();
m_instance.init();
}

void GlobalOpError::begin() {
m_instance->begin();
m_instance.begin();
}

void GlobalOpError::update() {
m_instance->update();
m_instance.update();
}

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

void GlobalOpError::end() {
m_instance->end();
m_instance.end();
}
12 changes: 6 additions & 6 deletions src/ayab/global_OpIdle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,25 @@
// static member functions

OpState_t GlobalOpIdle::state() {
return m_instance->state();
return m_instance.state();
}

void GlobalOpIdle::init() {
m_instance->init();
m_instance.init();
}

void GlobalOpIdle::begin() {
m_instance->begin();
m_instance.begin();
}

void GlobalOpIdle::update() {
m_instance->update();
m_instance.update();
}

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

void GlobalOpIdle::end() {
m_instance->end();
m_instance.end();
}
14 changes: 7 additions & 7 deletions src/ayab/global_OpInit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,30 @@
// static member functions

OpState_t GlobalOpInit::state() {
return m_instance->state();
return m_instance.state();
}

void GlobalOpInit::init() {
m_instance->init();
m_instance.init();
}

void GlobalOpInit::begin() {
m_instance->begin();
m_instance.begin();
}

void GlobalOpInit::update() {
m_instance->update();
m_instance.update();
}

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

void GlobalOpInit::end() {
m_instance->end();
m_instance.end();

}

bool GlobalOpInit::isReady() {
return m_instance->isReady();
return m_instance.isReady();
}
Loading