Skip to content

Commit

Permalink
mock PacketSerial
Browse files Browse the repository at this point in the history
  • Loading branch information
t0mpr1c3 committed Oct 2, 2023
1 parent c023174 commit 4748717
Show file tree
Hide file tree
Showing 25 changed files with 508 additions and 193 deletions.
2 changes: 0 additions & 2 deletions src/ayab/analogReadAsyncWrapper.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
/*!
* \file analogReadAsyncWrapper.cpp
* \brief Class containing methods to actuate a analogReadAsyncWrapper connected
* to PIEZO_PIN.
*
* This file is part of AYAB.
*
Expand Down
58 changes: 0 additions & 58 deletions src/ayab/atomic.h

This file was deleted.

14 changes: 7 additions & 7 deletions src/ayab/com.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
* http://ayab-knitting.com
*/

#include "packetSerialWrapper.h"

#include "beeper.h"
#include "com.h"
#include "controller.h"
Expand All @@ -34,17 +36,15 @@
* \brief Initialize serial communication.
*/
void Com::init() {
m_packetSerial.begin(SERIAL_BAUDRATE);
#ifndef AYAB_TESTS
m_packetSerial.setPacketHandler(GlobalCom::onPacketReceived);
#endif // AYAB_TESTS
GlobalPacketSerialWrapper::begin(SERIAL_BAUDRATE);
GlobalPacketSerialWrapper::setPacketHandler(GlobalCom::onPacketReceived);
}

/*!
* \brief Service the serial connection.
*/
void Com::update() {
m_packetSerial.update();
GlobalPacketSerialWrapper::update();
}

/*!
Expand Down Expand Up @@ -96,7 +96,7 @@ void Com::send(uint8_t *payload, size_t length) const {
Serial.print(", Encoded as: ");
#endif
*/
m_packetSerial.send(payload, length);
GlobalPacketSerialWrapper::send(payload, length);
}

/*!
Expand All @@ -110,7 +110,7 @@ void Com::sendMsg(API_t id, const char *msg) {
while (*msg) {
msgBuffer[length++] = static_cast<uint8_t>(*msg++);
}
m_packetSerial.send(msgBuffer, length);
GlobalPacketSerialWrapper::send(msgBuffer, length);
}
void Com::sendMsg(API_t id, char *msg) {
sendMsg(id, static_cast<const char *>(msg));
Expand Down
4 changes: 0 additions & 4 deletions src/ayab/com.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,6 @@ class GlobalCom final {
static void h_reqTest();
static void h_quitCmd();
static void h_unrecognized();

private:
static SLIPPacketSerial m_packetSerial;
};

class Com : public ComInterface {
Expand All @@ -158,7 +155,6 @@ class Com : public ComInterface {
void h_unrecognized() const final;

private:
SLIPPacketSerial m_packetSerial;
uint8_t lineBuffer[MAX_LINE_BUFFER_LEN] = {0};
uint8_t msgBuffer[MAX_MSG_BUFFER_LEN] = {0};

Expand Down
11 changes: 11 additions & 0 deletions src/ayab/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@
* \brief Initialize Finite State Machine.
*/
void Controller::init() {
pinMode(ENC_PIN_A, INPUT);
pinMode(ENC_PIN_B, INPUT);
pinMode(ENC_PIN_C, INPUT);
pinMode(LED_PIN_A, OUTPUT);
pinMode(LED_PIN_B, OUTPUT);
digitalWrite(LED_PIN_A, 1);
digitalWrite(LED_PIN_B, 1);
#if DBG_NOMACHINE
pinMode(DBG_BTN_PIN, INPUT);
#endif

m_machineType = Machine_t::NoMachine;
m_carriage = Carriage_t::NoCarriage;
m_direction = Direction_t::NoDirection;
Expand Down
2 changes: 0 additions & 2 deletions src/ayab/global_analogReadAsyncWrapper.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
/*!
* \file global_analogReadAsyncWrapper.cpp
* \brief Singleton class containing methods to actuate a analogReadAsyncWrapper
* connected to PIEZO_PIN.
*
* This file is part of AYAB.
*
Expand Down
42 changes: 42 additions & 0 deletions src/ayab/global_packetSerialWrapper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*!
* \file global_packetSerialWrapper.cpp
*
* This file is part of AYAB.
*
* AYAB is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* AYAB is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with AYAB. If not, see <http://www.gnu.org/licenses/>.
*
* Original Work Copyright 2013 Christian Obersteiner, Andreas Müller
* Modified Work Copyright 2020-3 Sturla Lange, Tom Price
* http://ayab-knitting.com
*/

#include "packetSerialWrapper.h"

// static member functions

void GlobalPacketSerialWrapper::begin(uint32_t speed) {
m_instance->begin(speed);
}

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

void GlobalPacketSerialWrapper::setPacketHandler(SLIPPacketSerial::PacketHandlerFunction onPacketFunction) {
m_instance->setPacketHandler(onPacketFunction);
}

void GlobalPacketSerialWrapper::update() {
m_instance->update();
}
7 changes: 6 additions & 1 deletion src/ayab/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include <Arduino.h>

#include "analogReadAsyncWrapper.h"
#include "packetSerialWrapper.h"

#include "beeper.h"
#include "com.h"
#include "controller.h"
Expand All @@ -42,6 +44,8 @@
// Each of the following is a pointer to a singleton class
// containing static methods.
constexpr GlobalAnalogReadAsyncWrapper *analogReadAsyncWrapper;
constexpr GlobalPacketSerialWrapper *packetSerialWrapper;

constexpr GlobalBeeper *beeper;
constexpr GlobalCom *com;
constexpr GlobalController *controller;
Expand All @@ -60,6 +64,8 @@ constexpr GlobalOpError *opError;
// that implements a public interface. When testing, a pointer
// to an instance of a mock class can be substituted.
AnalogReadAsyncWrapperInterface *GlobalAnalogReadAsyncWrapper::m_instance = new AnalogReadAsyncWrapper();
PacketSerialWrapperInterface *GlobalPacketSerialWrapper::m_instance = new PacketSerialWrapper();

BeeperInterface *GlobalBeeper::m_instance = new Beeper();
ComInterface *GlobalCom::m_instance = new Com();
EncodersInterface *GlobalEncoders::m_instance = new Encoders();
Expand All @@ -82,7 +88,6 @@ void setup() {
GlobalCom::init();
GlobalController::init();
GlobalSolenoids::init();

GlobalOpKnit::init();
}

Expand Down
13 changes: 0 additions & 13 deletions src/ayab/opKnit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,6 @@ OpState_t OpKnit::state() {
* Initialize the solenoids as well as pins and interrupts.
*/
void OpKnit::init() {
pinMode(ENC_PIN_A, INPUT);
pinMode(ENC_PIN_B, INPUT);
pinMode(ENC_PIN_C, INPUT);
pinMode(LED_PIN_A, OUTPUT);
pinMode(LED_PIN_B, OUTPUT);
digitalWrite(LED_PIN_A, 1);
digitalWrite(LED_PIN_B, 1);
#if DBG_NOMACHINE
pinMode(DBG_BTN_PIN, INPUT);
#endif

GlobalSolenoids::init();

// explicitly initialize members

// job parameters
Expand Down
67 changes: 67 additions & 0 deletions src/ayab/packetSerialWrapper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*!
* \file packetSerialWrapper.cpp
*
* This file is part of AYAB.
*
* AYAB is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* AYAB is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with AYAB. If not, see <http://www.gnu.org/licenses/>.
*
* Original Work Copyright 2013 Christian Obersteiner, Andreas Müller
* Modified Work Copyright 2020-3 Sturla Lange, Tom Price
* http://ayab-knitting.com
*/

#include "packetSerialWrapper.h"

/*!
* \brief Wrapper for PacketSerial::begin
*/
void PacketSerialWrapper::begin(uint32_t speed) {
#ifndef AYAB_TESTS
m_packetSerial.begin(speed);
#else
(void) speed;
#endif
}

/*!
* \brief Wrapper for PacketSerial::send
*/
void PacketSerialWrapper::send(const uint8_t *buffer, size_t size) const {
#ifndef AYAB_TESTS
m_packetSerial.send(buffer, size);
#else
(void) buffer;
(void) size;
#endif
}

/*!
* \brief Wrapper for PacketSerial::setPacketHandler
*/
void PacketSerialWrapper::setPacketHandler(SLIPPacketSerial::PacketHandlerFunction onPacketFunction) {
#ifndef AYAB_TESTS
m_packetSerial.setPacketHandler(onPacketFunction);
#else
(void) onPacketFunction;
#endif
}

/*!
* \brief Wrapper for PacketSerial::update
*/
void PacketSerialWrapper::update() {
#ifndef AYAB_TESTS
m_packetSerial.update();
#endif
}
Loading

0 comments on commit 4748717

Please sign in to comment.