Skip to content

Commit

Permalink
configurable beeper
Browse files Browse the repository at this point in the history
  • Loading branch information
t0mpr1c3 committed Sep 16, 2023
1 parent 4b8638d commit d1aecf2
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 38 deletions.
22 changes: 18 additions & 4 deletions src/ayab/beeper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,16 @@
/*!
* Initialize beeper
*/
void Beeper::init() {
void Beeper::init(bool enabled) {
m_currentState = BeepState::Idle;
m_enabled = enabled;
}

/*!
* Get beeper enabled flag
*/
bool Beeper::enabled() {
return m_enabled;
}

/*!
Expand All @@ -46,21 +54,27 @@ BeepState Beeper::getState() {
* Beep to indicate readiness
*/
void Beeper::ready() {
beep(BEEP_NUM_READY);
if (m_enabled) {
beep(BEEP_NUM_READY);
}
}

/*!
* Beep to indicate the end of a line
*/
void Beeper::finishedLine() {
beep(BEEP_NUM_FINISHEDLINE);
if (m_enabled) {
beep(BEEP_NUM_FINISHEDLINE);
}
}

/*!
* Beep to indicate the end the knitting pattern
*/
void Beeper::endWork() {
beep(BEEP_NUM_ENDWORK);
if (m_enabled) {
beep(BEEP_NUM_ENDWORK);
}
}

/*!
Expand Down
10 changes: 7 additions & 3 deletions src/ayab/beeper.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ class BeeperInterface {
virtual ~BeeperInterface() = default;

// any methods that need to be mocked should go here
virtual void init() = 0;
virtual void init(bool enabled) = 0;
virtual bool enabled() = 0;
virtual BeepState getState() = 0;
virtual void ready() = 0;
virtual void finishedLine() = 0;
Expand All @@ -65,7 +66,8 @@ class GlobalBeeper final {
// pointer to global instance whose methods are implemented
static BeeperInterface *m_instance;

static void init();
static void init(bool enabled);
static bool enabled();
static BeepState getState();
static void ready();
static void finishedLine();
Expand All @@ -78,7 +80,8 @@ class GlobalBeeper final {
*/
class Beeper : public BeeperInterface {
public:
void init() final;
void init(bool enabled) final;
bool enabled() final;
BeepState getState() final;
void ready() final;
void finishedLine() final;
Expand All @@ -92,6 +95,7 @@ class Beeper : public BeeperInterface {
BeepState m_nextState;
unsigned long m_nextTime;
uint8_t m_repeat;
bool m_enabled;
};

#endif // BEEPER_H_
5 changes: 3 additions & 2 deletions src/ayab/com.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ void Com::h_reqStart(const uint8_t *buffer, size_t size) {

uint8_t startNeedle = buffer[1];
uint8_t stopNeedle = buffer[2];
auto continuousReportingEnabled = static_cast<bool>(buffer[3]);
auto continuousReportingEnabled = static_cast<bool>(buffer[3] & 1);
auto beeperEnabled = static_cast<bool>(buffer[3] & 2);

uint8_t crc8 = buffer[4];
// Check crc on bytes 0-4 of buffer.
Expand All @@ -243,7 +244,7 @@ void Com::h_reqStart(const uint8_t *buffer, size_t size) {
return;
}

// TODO(who?): verify operation
GlobalBeeper::init(beeperEnabled);
memset(lineBuffer, 0xFF, MAX_LINE_BUFFER_LEN);

// Note (August 2020): the return value of this function has changed.
Expand Down
8 changes: 6 additions & 2 deletions src/ayab/global_beeper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@

// static member functions

void GlobalBeeper::init() {
m_instance->init();
void GlobalBeeper::init(bool enabled) {
m_instance->init(enabled);
}

bool GlobalBeeper::enabled() {
return m_instance->enabled();
}

void GlobalBeeper::ready() {
Expand Down
6 changes: 4 additions & 2 deletions src/ayab/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ TesterInterface *GlobalTester::m_instance = new Tester();
* Setup - do once before going to the main loop.
*/
void setup() {
GlobalBeeper::init();
GlobalBeeper::init(false);
GlobalCom::init();
GlobalFsm::init();
GlobalKnitter::init();
Expand All @@ -71,5 +71,7 @@ void setup() {
*/
void loop() {
GlobalFsm::dispatch();
GlobalBeeper::schedule();
if (GlobalBeeper::enabled()) {
GlobalBeeper::schedule();
}
}
9 changes: 7 additions & 2 deletions test/mocks/beeper_mock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,14 @@ void releaseBeeperMock() {
}
}

void Beeper::init() {
void Beeper::init(bool enabled) {
assert(gBeeperMock != nullptr);
gBeeperMock->init();
gBeeperMock->init(enabled);
}

bool Beeper::enabled() {
assert(gBeeperMock != nullptr);
return gBeeperMock->enabled();
}

BeepState Beeper::getState() {
Expand Down
3 changes: 2 additions & 1 deletion test/mocks/beeper_mock.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@

class BeeperMock : public BeeperInterface {
public:
MOCK_METHOD0(init, void());
MOCK_METHOD1(init, void(bool));
MOCK_METHOD0(enabled, bool());
MOCK_METHOD0(getState, BeepState());
MOCK_METHOD0(ready, void());
MOCK_METHOD0(finishedLine, void());
Expand Down
2 changes: 1 addition & 1 deletion test/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ cd ../..

GCOVR_ARGS="--exclude-unreachable-branches --exclude-throw-branches \
--exclude-directories 'test/build/arduino_mock$' \
-e test_* -e libraries* -e src/ayab/global_knitter.cpp \
-e test_* -e lib* -e src/ayab/global_knitter.cpp \
-e src/ayab/global_fsm.cpp"

if [[ $sonar -eq 1 ]]; then
Expand Down
64 changes: 43 additions & 21 deletions test/test_beeper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ class BeeperTest : public ::testing::Test {
protected:
void SetUp() override {
arduinoMock = arduinoMockInstance();

// start in BeepState::Idle
beeper->init();
}

void TearDown() override {
Expand All @@ -50,38 +47,63 @@ class BeeperTest : public ::testing::Test {
beeper->schedule();
}

void expectedBeepRepeats(uint8_t repeats) {
void expectedBeepRepeats(uint8_t repeats, bool enabled) {
if (enabled) {
ASSERT_EQ(beeper->getState(), BeepState::Wait);
for (uint8_t i = 0; i < repeats; i++) {
expectedBeepSchedule(BEEP_DELAY * 2 * i);
EXPECT_CALL(*arduinoMock, analogWrite(PIEZO_PIN, BEEP_ON_DUTY));
expectedBeepSchedule(BEEP_DELAY * 2 * i + 1);
expectedBeepSchedule(BEEP_DELAY * (2 * i + 1));
//ASSERT_EQ(beeper->getState(), BeepState::Off);
EXPECT_CALL(*arduinoMock, analogWrite(PIEZO_PIN, BEEP_OFF_DUTY));
expectedBeepSchedule(BEEP_DELAY * (2 * i + 1) + 1);
for (uint8_t i = 0; i < repeats; i++) {
expectedBeepSchedule(BEEP_DELAY * 2 * i);
EXPECT_CALL(*arduinoMock, analogWrite(PIEZO_PIN, BEEP_ON_DUTY));
expectedBeepSchedule(BEEP_DELAY * 2 * i + 1);
expectedBeepSchedule(BEEP_DELAY * (2 * i + 1));
EXPECT_CALL(*arduinoMock, analogWrite(PIEZO_PIN, BEEP_OFF_DUTY));
expectedBeepSchedule(BEEP_DELAY * (2 * i + 1) + 1);
}
expectedBeepSchedule(BEEP_DELAY * (2 * repeats));
EXPECT_CALL(*arduinoMock, analogWrite(PIEZO_PIN, BEEP_NO_DUTY));
expectedBeepSchedule(BEEP_DELAY * (2 * repeats) + 1);
}
expectedBeepSchedule(BEEP_DELAY * (2 * repeats));
EXPECT_CALL(*arduinoMock, analogWrite(PIEZO_PIN, BEEP_NO_DUTY));
expectedBeepSchedule(BEEP_DELAY * (2 * repeats) + 1);
ASSERT_EQ(beeper->getState(), BeepState::Idle);
}
};

TEST_F(BeeperTest, test_ready) {
TEST_F(BeeperTest, test_ready_enabled) {
beeper->init(true);
EXPECT_CALL(*arduinoMock, millis).WillOnce(Return(0U));
beeper->ready();
expectedBeepRepeats(BEEP_NUM_READY);
expectedBeepRepeats(BEEP_NUM_READY, true);
}

TEST_F(BeeperTest, test_finishedLine) {
TEST_F(BeeperTest, test_finishedLine_enabled) {
beeper->init(true);
EXPECT_CALL(*arduinoMock, millis).WillOnce(Return(0U));
beeper->finishedLine();
expectedBeepRepeats(BEEP_NUM_FINISHEDLINE);
expectedBeepRepeats(BEEP_NUM_FINISHEDLINE, true);
}

TEST_F(BeeperTest, test_endWork) {
TEST_F(BeeperTest, test_endWork_enabled) {
beeper->init(true);
EXPECT_CALL(*arduinoMock, millis).WillOnce(Return(0U));
beeper->endWork();
expectedBeepRepeats(BEEP_NUM_ENDWORK);
expectedBeepRepeats(BEEP_NUM_ENDWORK, true);
}

TEST_F(BeeperTest, test_ready_disabled) {
beeper->init(false);
EXPECT_CALL(*arduinoMock, millis).Times(0);
beeper->ready();
expectedBeepRepeats(BEEP_NUM_READY, false);
}

TEST_F(BeeperTest, test_finishedLine_disabled) {
beeper->init(false);
EXPECT_CALL(*arduinoMock, millis).Times(0);
beeper->finishedLine();
expectedBeepRepeats(BEEP_NUM_FINISHEDLINE, false);
}

TEST_F(BeeperTest, test_endWork_disabled) {
beeper->init(false);
EXPECT_CALL(*arduinoMock, millis).Times(0);
beeper->endWork();
expectedBeepRepeats(BEEP_NUM_ENDWORK, false);
}
1 change: 1 addition & 0 deletions test/test_com.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class ComTest : public ::testing::Test {
Mock::AllowLeak(fsmMock);
Mock::AllowLeak(knitterMock);

beeper->init(true);
expect_init();
com->init();
}
Expand Down
2 changes: 2 additions & 0 deletions test/test_tester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class TesterTest : public ::testing::Test {
// cause a memory leak. We must notify the test that this is not the case.
Mock::AllowLeak(fsmMock);
Mock::AllowLeak(knitterMock);

beeper->init(true);
}

void TearDown() override {
Expand Down

0 comments on commit d1aecf2

Please sign in to comment.