-
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.
The goal is to test the AYAB firmware at a higher level by looking at the Arduino interactions instead of mocking parts of the firmware.
- Loading branch information
1 parent
85d0842
commit 849cd02
Showing
20 changed files
with
1,563 additions
and
25 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
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,84 @@ | ||
/*! | ||
* \file io_expanders_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 | ||
* http://ayab-knitting.com | ||
*/ | ||
|
||
#include "io_expanders_mock.h" | ||
|
||
#include <Adafruit_MCP23008.h> | ||
#include <gmock/gmock.h> | ||
|
||
#include <array> | ||
|
||
using namespace ::testing; | ||
|
||
IOExpandersMock::IOExpandersMock(WireMock *wireMock) { | ||
EXPECT_CALL(*wireMock, beginTransmission(_)) | ||
.Times(AnyNumber()) | ||
.WillRepeatedly(Invoke(this, &IOExpandersMock::beginTransmission)); | ||
EXPECT_CALL(*wireMock, write(An<uint8_t>())) | ||
.Times(AnyNumber()) | ||
.WillRepeatedly(Invoke(this, &IOExpandersMock::write)); | ||
|
||
// We're not interested in calls to the following methods, but they | ||
// happen and if we don't set up expectations for them GoogleMock will | ||
// emit warnings. | ||
EXPECT_CALL(*wireMock, begin()).Times(AnyNumber()); | ||
EXPECT_CALL(*wireMock, endTransmission()).Times(AnyNumber()); | ||
EXPECT_CALL(*wireMock, requestFrom(_, _)).Times(AnyNumber()); | ||
EXPECT_CALL(*wireMock, read).Times(AnyNumber()); | ||
} | ||
|
||
std::array<bool, 16> IOExpandersMock::gpioState() { | ||
std::array<bool, 16> result; | ||
for (int i = 0; i < 8; i++) { | ||
result[i] = lowByte & (1 << i); | ||
result[i + 8] = highByte & (1 << i); | ||
} | ||
return result; | ||
} | ||
|
||
void IOExpandersMock::beginTransmission(uint8_t address) { | ||
i2c_address = address; | ||
i2c_byteIndex = i2c_register = 0; | ||
} | ||
|
||
uint8_t IOExpandersMock::write(uint8_t data) { | ||
switch (i2c_byteIndex++) { | ||
case 0: | ||
i2c_register = data; | ||
break; | ||
case 1: | ||
switch (i2c_register) { | ||
case MCP23008_GPIO: | ||
case MCP23008_OLAT: | ||
switch (i2c_address & ~MCP23008_ADDRESS) { | ||
case 0: | ||
lowByte = data; | ||
break; | ||
case 1: | ||
highByte = data; | ||
break; | ||
} | ||
break; | ||
} | ||
} | ||
return 0; | ||
} |
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,66 @@ | ||
/*! | ||
* \file io_expanders_mock.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 | ||
* http://ayab-knitting.com | ||
*/ | ||
|
||
#ifndef IO_EXPANDERS_MOCK_H_ | ||
#define IO_EXPANDERS_MOCK_H_ | ||
|
||
#include <Wire.h> | ||
#include <array> | ||
|
||
/*! \brief Simulate I2C I/O expanders | ||
* | ||
* This class connects to arduino-mock's I2C (Wire) support and | ||
* intercepts writes to the simulated I2C bus, emulating a pair of | ||
* MCP23008 I/O expanders. | ||
* | ||
* Of the MCP23008 protocol, only the bare minimum necessary to support | ||
* GPIO writes is implemented. | ||
* | ||
* The current GPIO state can be retrieved at any time by calling | ||
* gpioState(). The result is the state of the 16 digital outputs, | ||
* in the order that they are normally connected to the knitting | ||
* machine's solenoids, i.e. starting from the leftmost solenoid. | ||
* | ||
* Note that a KH270 machine only has 12 solenoids, but since this | ||
* class simulates the I/O expanders, that always have 16 outputs, | ||
* that fact is not relevant here. | ||
* | ||
* Creating an instance of this class takes over WireMock, no expectations | ||
* should be setup on it from outside until that instance is destroyed | ||
* and releaseWireMock is called. | ||
*/ | ||
class IOExpandersMock { | ||
public: | ||
IOExpandersMock(WireMock *wireMock); | ||
|
||
std::array<bool, 16> gpioState(); | ||
|
||
private: | ||
uint8_t i2c_address = 0, i2c_byteIndex = 0, i2c_register = 0; | ||
uint8_t highByte = 0, lowByte = 0; | ||
|
||
void beginTransmission(uint8_t address); | ||
|
||
uint8_t write(uint8_t data); | ||
}; | ||
|
||
#endif // IO_EXPANDERS_MOCK_H_ |
Oops, something went wrong.