-
Notifications
You must be signed in to change notification settings - Fork 1
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
1 parent
090fd4e
commit aff9e62
Showing
11 changed files
with
245 additions
and
52 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#ifndef IWATERPUMP_H | ||
#define IWATERPUMP_H | ||
|
||
class IWaterPump { | ||
public: | ||
virtual ~IWaterPump() {} | ||
|
||
virtual void setup() = 0; | ||
virtual void start() = 0; | ||
virtual void stop() = 0; | ||
|
||
virtual bool isRunning() const = 0; | ||
}; | ||
|
||
#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
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,23 @@ | ||
#ifndef WATERPUMPCONTROLLER_H | ||
#define WATERPUMPCONTROLLER_H | ||
#include "IWaterPump.h" | ||
|
||
class WaterPumpController: public IWaterPump { | ||
private: | ||
const int _directionPin; | ||
const int _brakePin; | ||
const int _powerPin; | ||
const int _maxPower = 255; | ||
bool _isRunning = false; | ||
public: | ||
WaterPumpController(int directionPin, int brakePin, int powerPin); | ||
virtual ~WaterPumpController() override; | ||
|
||
virtual void setup() override; | ||
virtual void start() override; | ||
virtual void stop() override; | ||
|
||
virtual bool isRunning() const override { return _isRunning; } | ||
}; | ||
|
||
#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,36 @@ | ||
#include "WaterPumpScheduler.h" | ||
|
||
WaterPumpScheduler::WaterPumpScheduler(IWaterPump* waterPump, unsigned long forceStopIntervalMs) { | ||
} | ||
|
||
WaterPumpScheduler::~WaterPumpScheduler() { | ||
delete _waterPump; | ||
} | ||
|
||
void WaterPumpScheduler::setup() { | ||
_waterPump->setup(); | ||
} | ||
|
||
void WaterPumpScheduler::start(unsigned long runTimeMs, unsigned long currentTimeMs) { | ||
_stopTime = currentTimeMs + runTimeMs; | ||
_waterPump->start(); | ||
} | ||
|
||
void WaterPumpScheduler::stop() { | ||
_waterPump->stop(); | ||
_stopTime = 0; // a bit of paranoia :) | ||
} | ||
|
||
void WaterPumpScheduler::tick(unsigned long currentTimeMs) { | ||
if (_stopTime <= currentTimeMs) { | ||
stop(); | ||
_stopTime = currentTimeMs + _forceStopIntervalMs; // force stop after X milliseconds | ||
} | ||
} | ||
|
||
WaterPumpScheduler::WaterPumpStatus WaterPumpScheduler::status() { | ||
return { | ||
_waterPump->isRunning(), | ||
_stopTime | ||
}; | ||
} |
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,33 @@ | ||
#ifndef WATERPUMPSCHEDULER_H | ||
#define WATERPUMPSCHEDULER_H | ||
|
||
#include "IWaterPump.h" | ||
|
||
// This class is responsible for scheduling water pump | ||
// It is used to make sure that water pump is running for a limited time | ||
// It is also ensuring that water pump is stopped if not needed | ||
class WaterPumpScheduler { | ||
private: | ||
IWaterPump* _waterPump; | ||
unsigned long _stopTime = 0; | ||
// each X milliseconds will force stop water pump | ||
unsigned long _forceStopIntervalMs; | ||
public: | ||
WaterPumpScheduler(IWaterPump* waterPump, unsigned long forceStopIntervalMs); | ||
WaterPumpScheduler(IWaterPump* waterPump) : WaterPumpScheduler(waterPump, 1000) {} | ||
~WaterPumpScheduler(); | ||
|
||
void setup(); | ||
void stop(); | ||
// for simplicity and testability we are passing current time as parameter | ||
void start(unsigned long runTimeMs, unsigned long currentTimeMs); | ||
void tick(unsigned long currentTimeMs); | ||
|
||
// pump status | ||
struct WaterPumpStatus { | ||
bool isRunning; | ||
unsigned long stopTime; | ||
}; | ||
WaterPumpStatus status(); | ||
}; | ||
#endif |
28 changes: 0 additions & 28 deletions
28
controller/tea_poor/lib/WaterPumpController/WaterPumpController.h
This file was deleted.
Oops, something went wrong.
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
69 changes: 69 additions & 0 deletions
69
controller/tea_poor/test/test_local/WaterPumpScheduler_test.cpp
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,69 @@ | ||
// I wasn't able to run tests at all. Run them locally and confirm that they are working. | ||
// Its either a local problem or a problem with the configuration of the project. | ||
// Further goes a sketch of the tests, but I wasn't able to run them. | ||
#include <gtest/gtest.h> | ||
#include <WaterPumpScheduler.h> | ||
|
||
// Fake water pump | ||
class FakeWaterPump : public IWaterPump { | ||
private: | ||
bool _isRunning = false; | ||
public: | ||
void setup() override { _isRunning = false; } | ||
void start() override { _isRunning = true; } | ||
void stop() override { _isRunning = false; } | ||
|
||
bool isRunning() override { return _isRunning; } | ||
}; | ||
// End of fake water pump | ||
|
||
// test that pump is stopping after given time | ||
TEST(WaterPumpScheduler, test_pump_stops_after_given_time) { | ||
// random time between 1 and 10 seconds | ||
const unsigned long runTimeMs = 1000 + (rand() % 10) * 1000; | ||
FakeWaterPump fakeWaterPump; | ||
WaterPumpScheduler waterPumpScheduler(&fakeWaterPump); | ||
waterPumpScheduler.setup(); | ||
// start water pump | ||
unsigned long currentTimeMs = 0; | ||
waterPumpScheduler.start(runTimeMs, currentTimeMs); | ||
// check status | ||
auto status = waterPumpScheduler.status(); | ||
ASSERT_TRUE(status.isRunning); | ||
ASSERT_EQ(status.stopTime, runTimeMs); | ||
|
||
while (currentTimeMs < runTimeMs) { | ||
waterPumpScheduler.tick(currentTimeMs); | ||
ASSERT_TRUE(fakeWaterPump->isRunning()); | ||
currentTimeMs += 100; | ||
} | ||
// pump should be stopped after given time | ||
waterPumpScheduler.tick(runTimeMs + 1); | ||
ASSERT_FALSE(fakeWaterPump->isRunning()); | ||
} | ||
|
||
// test that pump is periodically forced to stop after given time | ||
TEST(WaterPumpScheduler, test_pump_is_periodically_forced_to_stop_after_given_time) { | ||
FakeWaterPump fakeWaterPump; | ||
WaterPumpScheduler waterPumpScheduler(&fakeWaterPump, 1000); // force stop each 1 second | ||
waterPumpScheduler.setup(); | ||
// start water pump | ||
unsigned long currentTimeMs = 0; | ||
waterPumpScheduler.start(1, currentTimeMs); | ||
currentTimeMs += 1; | ||
waterPumpScheduler.tick(currentTimeMs); | ||
ASSERT_FALSE(fakeWaterPump->isRunning()); // pump should be stopped after given time | ||
|
||
for(int i = 0; i < 10; i++) { | ||
// emulate that pump was started again | ||
fakeWaterPump.start(); | ||
currentTimeMs += 1000; | ||
waterPumpScheduler.tick(currentTimeMs); | ||
ASSERT_FALSE(fakeWaterPump->isRunning()); // pump should be stopped | ||
} | ||
} | ||
|
||
int main(int argc, char **argv) { | ||
::testing::InitGoogleTest(&argc, argv); | ||
return RUN_ALL_TESTS(); | ||
} |