diff --git a/controller/tea_poor/lib/Arduino/WaterPumpController.cpp b/controller/tea_poor/lib/Arduino/WaterPumpController.cpp index e337625..4717f0d 100644 --- a/controller/tea_poor/lib/Arduino/WaterPumpController.cpp +++ b/controller/tea_poor/lib/Arduino/WaterPumpController.cpp @@ -20,8 +20,7 @@ void WaterPumpController::setup() { stop(); } -void WaterPumpController::start(int powerInPercents) { - const int power = map(powerInPercents, 0, 100, 0, _maxPower); +void WaterPumpController::start(int power) { _isRunning = true; digitalWrite(_brakePin, LOW); // release breaks analogWrite(_powerPin, power); diff --git a/controller/tea_poor/lib/Arduino/WaterPumpController.h b/controller/tea_poor/lib/Arduino/WaterPumpController.h index 2597987..9d492c7 100644 --- a/controller/tea_poor/lib/Arduino/WaterPumpController.h +++ b/controller/tea_poor/lib/Arduino/WaterPumpController.h @@ -7,14 +7,13 @@ class WaterPumpController: public IWaterPump { 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(int powerInPercents) override; + virtual void start(int power) override; virtual void stop() override; virtual bool isRunning() const override { return _isRunning; } diff --git a/controller/tea_poor/lib/Core/AdjustedWaterPump.h b/controller/tea_poor/lib/Core/AdjustedWaterPump.h new file mode 100644 index 0000000..6e2a38c --- /dev/null +++ b/controller/tea_poor/lib/Core/AdjustedWaterPump.h @@ -0,0 +1,26 @@ +#ifndef ADJUSTEDWATERPUMP_H +#define ADJUSTEDWATERPUMP_H +#include +#include + +// lightweight wrapper around IWaterPump +// its purpose is to adjust power value to the range of 0..255, for now +class AdjustedWaterPump: public IWaterPump { +private: + const IWaterPumpPtr _pump; +public: + AdjustedWaterPump(IWaterPumpPtr pump) : _pump(pump) {} + virtual ~AdjustedWaterPump() override {} + + virtual void setup() override { _pump->setup(); } + virtual void stop() override { _pump->stop(); } + virtual bool isRunning() const override { return _pump->isRunning(); } + + virtual void start(int powerInPercents) override { + // convert percents to 0..255 range, using float + const float power = (255.0f / 100.0f) * (float)powerInPercents; + _pump->start(floor(power)); + } +}; + +#endif \ No newline at end of file diff --git a/controller/tea_poor/lib/interfaces/IWaterPump.h b/controller/tea_poor/lib/interfaces/IWaterPump.h index 0ec2d48..251b144 100644 --- a/controller/tea_poor/lib/interfaces/IWaterPump.h +++ b/controller/tea_poor/lib/interfaces/IWaterPump.h @@ -8,7 +8,7 @@ class IWaterPump { virtual ~IWaterPump() {} virtual void setup() = 0; - virtual void start(int powerInPercents) = 0; + virtual void start(int power) = 0; virtual void stop() = 0; virtual bool isRunning() const = 0; diff --git a/controller/tea_poor/src/main.cpp b/controller/tea_poor/src/main.cpp index 6bacbd7..e6bce12 100644 --- a/controller/tea_poor/src/main.cpp +++ b/controller/tea_poor/src/main.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -9,12 +10,14 @@ #include #include -const IEnvironmentPtr env = std::make_shared(); +const auto env = std::make_shared(); // Setting up water pump const auto waterPump = std::make_shared( - std::make_shared( - WATER_PUMP_DIRECTION_PIN, WATER_PUMP_BRAKE_PIN, WATER_PUMP_POWER_PIN + std::make_shared( + std::make_shared( + WATER_PUMP_DIRECTION_PIN, WATER_PUMP_BRAKE_PIN, WATER_PUMP_POWER_PIN + ) ), env ); diff --git a/controller/tea_poor/test/test_native/main.cpp b/controller/tea_poor/test/test_native/main.cpp index b6f09e9..3245894 100644 --- a/controller/tea_poor/test/test_native/main.cpp +++ b/controller/tea_poor/test/test_native/main.cpp @@ -3,6 +3,7 @@ // include tests #include "tests/WaterPumpScheduler_test.h" #include "tests/CommandProcessor_test.h" +#include "tests/AdjustedWaterPump_test.h" int main(int argc, char **argv) { ::testing::InitGoogleTest(&argc, argv); diff --git a/controller/tea_poor/test/test_native/tests/AdjustedWaterPump_test.h b/controller/tea_poor/test/test_native/tests/AdjustedWaterPump_test.h new file mode 100644 index 0000000..b84fb82 --- /dev/null +++ b/controller/tea_poor/test/test_native/tests/AdjustedWaterPump_test.h @@ -0,0 +1,18 @@ +#include +#include "mocks/FakeWaterPump.h" +#include + +// test that pumps power passed as percents is converted to 0..255 range +TEST(AdjustedWaterPump, test_pumps_power_passed_as_percents_is_converted_to_0_255_range) { + const auto fakeWaterPump = std::make_shared(); + AdjustedWaterPump adjustedWaterPump(fakeWaterPump); + // list of pairs: (powerInPercents, expectedPower) + const std::vector> tests = { + {0, 0}, {1, 2}, {2, 5}, + {50, 127}, {100, 255} + }; + for(const auto& test: tests) { + adjustedWaterPump.start(test.first); + ASSERT_EQ(fakeWaterPump->power(), test.second); + } +} \ No newline at end of file diff --git a/controller/tea_poor/test/test_native/tests/WaterPumpScheduler_test.h b/controller/tea_poor/test/test_native/tests/WaterPumpScheduler_test.h index a959bc8..367d83c 100644 --- a/controller/tea_poor/test/test_native/tests/WaterPumpScheduler_test.h +++ b/controller/tea_poor/test/test_native/tests/WaterPumpScheduler_test.h @@ -14,7 +14,7 @@ TEST(WaterPumpScheduler, test_pump_stops_after_given_time) { // start water pump fakeEnvironment->time(0); waterPumpScheduler.start(runTimeMs, 1); - ASSERT_EQ(fakeWaterPump->powerInPercents(), 1); + ASSERT_EQ(fakeWaterPump->power(), 1); // check status auto status = waterPumpScheduler.status(); ASSERT_TRUE(status.isRunning); @@ -48,7 +48,7 @@ TEST(WaterPumpScheduler, test_pump_is_periodically_forced_to_stop_after_given_ti for(int i = 0; i < 10; i++) { // emulate that pump was started again fakeWaterPump->start(1); - ASSERT_EQ(fakeWaterPump->powerInPercents(), 1); + ASSERT_EQ(fakeWaterPump->power(), 1); fakeEnvironment->time(fakeEnvironment->time() + T); waterPumpScheduler.tick(); ASSERT_FALSE(fakeWaterPump->isRunning()); // pump should be stopped @@ -63,5 +63,5 @@ TEST(WaterPumpScheduler, test_pumps_power_is_set_to_specified_value) { waterPumpScheduler.setup(); const int power = 23; waterPumpScheduler.start(1, power); - ASSERT_EQ(fakeWaterPump->powerInPercents(), power); + ASSERT_EQ(fakeWaterPump->power(), power); } \ No newline at end of file diff --git a/controller/tea_poor/test/test_native/tests/mocks/FakeWaterPump.h b/controller/tea_poor/test/test_native/tests/mocks/FakeWaterPump.h index 681e9de..44972d8 100644 --- a/controller/tea_poor/test/test_native/tests/mocks/FakeWaterPump.h +++ b/controller/tea_poor/test/test_native/tests/mocks/FakeWaterPump.h @@ -7,17 +7,17 @@ class FakeWaterPump : public IWaterPump { private: bool _isRunning = false; - int _powerInPercents = 0; + int _power = 0; public: void setup() override { _isRunning = false; } void stop() override { _isRunning = false; } - void start(int powerInPercents) override { + void start(int power) override { _isRunning = true; - _powerInPercents = powerInPercents; + _power = power; } bool isRunning() const override { return _isRunning; } - int powerInPercents() const { return _powerInPercents; } + int power() const { return _power; } }; #endif // FAKE_WATER_PUMP_H \ No newline at end of file