-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
481 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/*! | ||
* \file packetSerialWrapper.h | ||
* | ||
* 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 | ||
*/ | ||
|
||
#ifndef PACKETSERIALWRAPPER_H_ | ||
#define PACKETSERIALWRAPPER_H_ | ||
|
||
#include <Arduino.h> | ||
#include <PacketSerial.h> | ||
|
||
class PacketSerialWrapperInterface { | ||
public: | ||
virtual ~PacketSerialWrapperInterface() = default; | ||
|
||
// any methods that need to be mocked should go here | ||
virtual void begin(uint32_t speed) = 0; | ||
virtual void send(const uint8_t *buffer, size_t size) const = 0; | ||
virtual void setPacketHandler(SLIPPacketSerial::PacketHandlerFunction onPacketFunction) = 0; | ||
virtual void update() = 0; | ||
}; | ||
|
||
// Container class for the static method packetSerial. | ||
// Dependency injection is enabled using a pointer to a global instance of | ||
// either `PacketSerialWrapper` or `PacketSerialWrapperMock`, | ||
// both of which classes implement the | ||
// pure virtual methods of `PacketSerialWrapperInterface`. | ||
|
||
class GlobalPacketSerialWrapper final { | ||
private: | ||
// singleton class so private constructor is appropriate | ||
GlobalPacketSerialWrapper() = default; | ||
|
||
public: | ||
// pointer to global instance whose methods are implemented | ||
static PacketSerialWrapperInterface *m_instance; | ||
|
||
static void begin(uint32_t speed); | ||
static void send(const uint8_t *buffer, size_t size); | ||
static void setPacketHandler(SLIPPacketSerial::PacketHandlerFunction onPacketFunction); | ||
static void update(); | ||
}; | ||
|
||
/*! | ||
* \brief Wrapper for packetSerial method | ||
*/ | ||
class PacketSerialWrapper : public PacketSerialWrapperInterface { | ||
public: | ||
void begin(uint32_t speed) final; | ||
void send(const uint8_t *buffer, size_t size) const final; | ||
void setPacketHandler(SLIPPacketSerial::PacketHandlerFunction onPacketFunction) final; | ||
void update() final; | ||
|
||
private: | ||
SLIPPacketSerial m_packetSerial; | ||
}; | ||
|
||
#endif // PACKETSERIALWRAPPER_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/*!` | ||
* \file packetSerialWrapper_mock.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_mock.h> | ||
|
||
static PacketSerialWrapperMock *gPacketSerialWrapperMock = nullptr; | ||
PacketSerialWrapperMock *packetSerialWrapperMockInstance() { | ||
if (!gPacketSerialWrapperMock) { | ||
gPacketSerialWrapperMock = new PacketSerialWrapperMock(); | ||
} | ||
return gPacketSerialWrapperMock; | ||
} | ||
|
||
void releasePacketSerialWrapperMock() { | ||
if (gPacketSerialWrapperMock) { | ||
delete gPacketSerialWrapperMock; | ||
gPacketSerialWrapperMock = nullptr; | ||
} | ||
} | ||
|
||
void PacketSerialWrapper::begin(uint32_t speed) { | ||
assert(gPacketSerialWrapperMock != nullptr); | ||
gPacketSerialWrapperMock->begin(speed); | ||
} | ||
|
||
void PacketSerialWrapper::send(const uint8_t *buffer, size_t size) const { | ||
assert(gPacketSerialWrapperMock != nullptr); | ||
gPacketSerialWrapperMock->send(buffer, size); | ||
} | ||
|
||
void PacketSerialWrapper::setPacketHandler(SLIPPacketSerial::PacketHandlerFunction onPacketFunction) { | ||
assert(gPacketSerialWrapperMock != nullptr); | ||
gPacketSerialWrapperMock->setPacketHandler(onPacketFunction); | ||
} | ||
|
||
void PacketSerialWrapper::update() { | ||
assert(gPacketSerialWrapperMock != nullptr); | ||
gPacketSerialWrapperMock->update(); | ||
} |
Oops, something went wrong.