Skip to content

Commit

Permalink
remove OpKnit::encodePosition()
Browse files Browse the repository at this point in the history
  • Loading branch information
t0mpr1c3 committed Oct 4, 2023
1 parent 3807822 commit 2cb5e82
Show file tree
Hide file tree
Showing 18 changed files with 68 additions and 91 deletions.
2 changes: 1 addition & 1 deletion src/ayab/com.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,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
2 changes: 1 addition & 1 deletion src/ayab/com.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class Com : public ComInterface {
public:
void init() final;
void update() final;
uint8_t CRC8(const uint8_t *buffer, size_t len) const;
uint8_t CRC8(const uint8_t *buffer, size_t len) const final;
void send(uint8_t *payload, size_t length) const final;
void sendMsg(API_t id, const char *msg) final;
void sendMsg(API_t id, char *msg) final;
Expand Down
21 changes: 14 additions & 7 deletions src/ayab/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ void Controller::update() {
m_currentState = m_nextState;
}

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

/*!
* \brief Cache Encoder values
* The code that saves the Encoder values is bookended by macros
Expand Down Expand Up @@ -109,7 +116,7 @@ void Controller::setState(OpInterface *state) {
* \brief Get machine state.
* \return Current state of Finite State Machine.
*/
OpInterface *Controller::getState() {
OpInterface *Controller::getState() const {
return m_currentState;
}

Expand All @@ -125,46 +132,46 @@ void Controller::setMachineType(Machine_t machineType) {
* \brief Get knitting machine type.
* \return Machine type.
*/
Machine_t Controller::getMachineType() {
Machine_t Controller::getMachineType() const {
return m_machineType;
}

/*!
* \brief Get cached beltShift value.
* \return Cached beltShift value.
*/
BeltShift_t Controller::getBeltShift() {
BeltShift_t Controller::getBeltShift() const {
return m_beltShift;
}

/*!
* \brief Get cached carriage value.
* \return Cached carriage value.
*/
Carriage_t Controller::getCarriage() {
Carriage_t Controller::getCarriage() const {
return m_carriage;
}

/*!
* \brief Get cached direction value.
* \return Cached direction value.
*/
Direction_t Controller::getDirection() {
Direction_t Controller::getDirection() const {
return m_direction;
}

/*!
* \brief Get cached hallActive value.
* \return Cached hallActive value.
*/
Direction_t Controller::getHallActive() {
Direction_t Controller::getHallActive() const {
return m_hallActive;
}

/*!
* \brief Get cached position value.
* \return Cached position value.
*/
uint8_t Controller::getPosition() {
uint8_t Controller::getPosition() const {
return m_position;
}
31 changes: 17 additions & 14 deletions src/ayab/controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,17 @@ class ControllerInterface {
// any methods that need to be mocked should go here
virtual void init() = 0;
virtual void update() = 0;
virtual void com(const uint8_t *buffer, size_t size) const = 0;
virtual void cacheEncoders() = 0;
virtual void setState(OpInterface *state) = 0;
virtual OpInterface *getState() = 0;
virtual OpInterface *getState() const = 0;
virtual void setMachineType(Machine_t) = 0;
virtual Machine_t getMachineType() = 0;
virtual BeltShift_t getBeltShift() = 0;
virtual Carriage_t getCarriage() = 0;
virtual Direction_t getDirection() = 0;
virtual Direction_t getHallActive() = 0;
virtual uint8_t getPosition() = 0;
virtual Machine_t getMachineType() const = 0;
virtual BeltShift_t getBeltShift() const = 0;
virtual Carriage_t getCarriage() const = 0;
virtual Direction_t getDirection() const = 0;
virtual Direction_t getHallActive() const = 0;
virtual uint8_t getPosition() const = 0;
};

// Singleton container class for static methods.
Expand All @@ -65,6 +66,7 @@ class GlobalController final {

static void init();
static void update();
static void com(const uint8_t *buffer, size_t size);
static void cacheEncoders();
static void setState(OpInterface *state);
static OpInterface *getState();
Expand All @@ -81,16 +83,17 @@ class Controller : public ControllerInterface {
public:
void init() final;
void update() final;
void com(const uint8_t *buffer, size_t size) const final;
void cacheEncoders() final;
void setState(OpInterface *state) final;
OpInterface *getState() final;
OpInterface *getState() const final;
void setMachineType(Machine_t) final;
Machine_t getMachineType() final;
BeltShift_t getBeltShift() final;
Carriage_t getCarriage() final;
Direction_t getDirection() final;
Direction_t getHallActive() final;
uint8_t getPosition() final;
Machine_t getMachineType() const final;
BeltShift_t getBeltShift() const final;
Carriage_t getCarriage() const final;
Direction_t getDirection() const final;
Direction_t getHallActive() const final;
uint8_t getPosition() const final;

// machine state
OpInterface *m_currentState;
Expand Down
4 changes: 0 additions & 4 deletions src/ayab/global_OpKnit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,6 @@ Err_t GlobalOpKnit::startKnitting(uint8_t startNeedle,
pattern_start, continuousReportingEnabled);
}

void GlobalOpKnit::encodePosition() {
m_instance->encodePosition();
}

void GlobalOpKnit::knit() {
m_instance->knit();
}
Expand Down
2 changes: 0 additions & 2 deletions src/ayab/global_com.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,9 @@ void GlobalCom::send_indState(Err_t error) {
m_instance->send_indState(error);
}

// GCOVR_EXCL_START
void GlobalCom::onPacketReceived(const uint8_t *buffer, size_t size) {
m_instance->onPacketReceived(buffer, size);
}
// GCOVR_EXCL_STOP

void GlobalCom::h_reqInit(const uint8_t *buffer, size_t size) {
m_instance->h_reqInit(buffer, size);
Expand Down
4 changes: 4 additions & 0 deletions src/ayab/global_controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ void GlobalController::update() {
m_instance->update();
}

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

void GlobalController::cacheEncoders() {
m_instance->cacheEncoders();
}
Expand Down
16 changes: 0 additions & 16 deletions src/ayab/opKnit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,22 +167,6 @@ Err_t OpKnit::startKnitting(uint8_t startNeedle,
return Err_t::Success;
}

/*!
* \brief Record current encoder position.
*
* Used in hardware test procedure.
*/
void OpKnit::encodePosition() {
auto position = GlobalController::getPosition();
if (m_sOldPosition != position) {
// only act if there is an actual change of position
// store current encoder position for next call of this function
m_sOldPosition = position;
(void) calculatePixelAndSolenoid();
GlobalCom::send_indState(Err_t::Unspecified_failure); // FIXME is this the right error code?
}
}

/*!
* \brief Function that is repeatedly called during state `OpKnit`
*/
Expand Down
4 changes: 0 additions & 4 deletions src/ayab/opKnit.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ class OpKnitInterface : public OpInterface {
virtual Err_t startKnitting(uint8_t startNeedle,
uint8_t stopNeedle, uint8_t *pattern_start,
bool continuousReportingEnabled) = 0;
virtual void encodePosition() = 0;
virtual void knit() = 0;
virtual uint8_t getStartOffset(const Direction_t direction) = 0;
virtual bool setNextLine(uint8_t lineNumber) = 0;
Expand Down Expand Up @@ -67,7 +66,6 @@ class GlobalOpKnit final {
static Err_t startKnitting(uint8_t startNeedle,
uint8_t stopNeedle, uint8_t *pattern_start,
bool continuousReportingEnabled);
static void encodePosition();
static void knit();
static uint8_t getStartOffset(const Direction_t direction);
static bool setNextLine(uint8_t lineNumber);
Expand All @@ -86,7 +84,6 @@ class OpKnit : public OpKnitInterface {
Err_t startKnitting(uint8_t startNeedle,
uint8_t stopNeedle, uint8_t *pattern_start,
bool continuousReportingEnabled) final;
void encodePosition() final;
void knit() final;
uint8_t getStartOffset(const Direction_t direction) final;
bool setNextLine(uint8_t lineNumber) final;
Expand Down Expand Up @@ -120,7 +117,6 @@ class OpKnit : public OpKnitInterface {

#if AYAB_TESTS
// Note: ideally tests would only rely on the public interface.
FRIEND_TEST(OpKnitTest, test_encodePosition);
FRIEND_TEST(OpKnitTest, test_getStartOffset);
FRIEND_TEST(OpKnitTest, test_knit_lastLine_and_no_req);
FRIEND_TEST(OpKnitTest, test_calculatePixelAndSolenoid);
Expand Down
1 change: 0 additions & 1 deletion src/ayab/opTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ void OpTest::begin() {
*/
void OpTest::update() {
if (enabled()) {
GlobalOpKnit::encodePosition(); // FIXME is this even necessary?
uint32_t now = millis();
if (now - m_lastTime >= TEST_LOOP_DELAY) {
m_lastTime = now;
Expand Down
19 changes: 12 additions & 7 deletions test/mocks/controller_mock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ void Controller::update() {
gControllerMock->update();
}

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

void Controller::cacheEncoders() {
assert(gControllerMock != nullptr);
gControllerMock->cacheEncoders();
Expand All @@ -60,7 +65,7 @@ void Controller::setState(OpInterface *state) {
gControllerMock->setState(state);
}

OpInterface *Controller::getState() {
OpInterface *Controller::getState() const {
assert(gControllerMock != nullptr);
return gControllerMock->getState();
}
Expand All @@ -70,32 +75,32 @@ void Controller::setMachineType(Machine_t machineType) {
gControllerMock->setMachineType(machineType);
}

Machine_t Controller::getMachineType() {
Machine_t Controller::getMachineType() const {
assert(gControllerMock != nullptr);
return gControllerMock->getMachineType();
}

BeltShift_t Controller::getBeltShift() {
BeltShift_t Controller::getBeltShift() const {
assert(gControllerMock != nullptr);
return gControllerMock->getBeltShift();
}

Carriage_t Controller::getCarriage() {
Carriage_t Controller::getCarriage() const {
assert(gControllerMock != nullptr);
return gControllerMock->getCarriage();
}

Direction_t Controller::getDirection() {
Direction_t Controller::getDirection() const {
assert(gControllerMock != nullptr);
return gControllerMock->getDirection();
}

Direction_t Controller::getHallActive() {
Direction_t Controller::getHallActive() const {
assert(gControllerMock != nullptr);
return gControllerMock->getHallActive();
}

uint8_t Controller::getPosition() {
uint8_t Controller::getPosition() const {
assert(gControllerMock != nullptr);
return gControllerMock->getPosition();
}
15 changes: 8 additions & 7 deletions test/mocks/controller_mock.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,17 @@ class ControllerMock : public ControllerInterface {
public:
MOCK_METHOD0(init, void());
MOCK_METHOD0(update, void());
MOCK_CONST_METHOD2(com, void(const uint8_t *buffer, size_t size));
MOCK_METHOD0(cacheEncoders, void());
MOCK_METHOD1(setState, void(OpInterface *state));
MOCK_METHOD0(getState, OpInterface*());
MOCK_CONST_METHOD0(getState, OpInterface*());
MOCK_METHOD1(setMachineType, void(Machine_t machineType));
MOCK_METHOD0(getMachineType, Machine_t());
MOCK_METHOD0(getBeltShift, BeltShift_t());
MOCK_METHOD0(getCarriage, Carriage_t());
MOCK_METHOD0(getDirection, Direction_t());
MOCK_METHOD0(getHallActive, Direction_t());
MOCK_METHOD0(getPosition, uint8_t());
MOCK_CONST_METHOD0(getMachineType, Machine_t());
MOCK_CONST_METHOD0(getBeltShift, BeltShift_t());
MOCK_CONST_METHOD0(getCarriage, Carriage_t());
MOCK_CONST_METHOD0(getDirection, Direction_t());
MOCK_CONST_METHOD0(getHallActive, Direction_t());
MOCK_CONST_METHOD0(getPosition, uint8_t());
};

ControllerMock *controllerMockInstance();
Expand Down
5 changes: 0 additions & 5 deletions test/mocks/opKnit_mock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,6 @@ Err_t OpKnit::startKnitting(uint8_t startNeedle,
pattern_start, continuousReportingEnabled);
}

void OpKnit::encodePosition() {
assert(gOpKnitMock != nullptr);
gOpKnitMock->encodePosition();
}

void OpKnit::knit() {
assert(gOpKnitMock != nullptr);
gOpKnitMock->knit();
Expand Down
1 change: 0 additions & 1 deletion test/mocks/opKnit_mock.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ class OpKnitMock : public OpKnitInterface {
MOCK_METHOD4(startKnitting, Err_t(uint8_t startNeedle,
uint8_t stopNeedle, uint8_t *pattern_start,
bool continuousReportingEnabled));
MOCK_METHOD0(encodePosition, void());
MOCK_METHOD0(knit, void());
MOCK_METHOD1(getStartOffset, uint8_t(const Direction_t direction));
MOCK_METHOD1(setNextLine, bool(uint8_t lineNumber));
Expand Down
13 changes: 0 additions & 13 deletions test/test_OpKnit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,19 +308,6 @@ TEST_F(OpKnitTest, test_com) {
ASSERT_TRUE(Mock::VerifyAndClear(comMock));
}

TEST_F(OpKnitTest, test_encodePosition) {
opKnit->m_sOldPosition = controller->getPosition();
EXPECT_CALL(*comMock, send_indState).Times(0);
opKnit->encodePosition();

opKnit->m_sOldPosition += 1;
EXPECT_CALL(*comMock, send_indState).Times(1);
opKnit->encodePosition();

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

TEST_F(OpKnitTest, test_cacheISR) {
expected_cacheISR();

Expand Down
Loading

0 comments on commit 2cb5e82

Please sign in to comment.