Skip to content

Commit

Permalink
uplift logic from WaterPumpController to AdjustedWaterPump
Browse files Browse the repository at this point in the history
  • Loading branch information
GreenWizard2015 committed Jan 30, 2024
1 parent 68fe43a commit 632f423
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 15 deletions.
3 changes: 1 addition & 2 deletions controller/tea_poor/lib/Arduino/WaterPumpController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions controller/tea_poor/lib/Arduino/WaterPumpController.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
26 changes: 26 additions & 0 deletions controller/tea_poor/lib/Core/AdjustedWaterPump.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef ADJUSTEDWATERPUMP_H
#define ADJUSTEDWATERPUMP_H
#include <IWaterPump.h>
#include <math.h>

// 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
2 changes: 1 addition & 1 deletion controller/tea_poor/lib/interfaces/IWaterPump.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 6 additions & 3 deletions controller/tea_poor/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <Arduino.h>
#include <memory>
#include <WaterPumpController.h>
#include <AdjustedWaterPump.h>
#include <WaterPumpScheduler.h>
#include <RemoteControl.h>
#include <CommandProcessor.h>
Expand All @@ -9,12 +10,14 @@
#include <sstream>
#include <ArduinoEnvironment.h>

const IEnvironmentPtr env = std::make_shared<ArduinoEnvironment>();
const auto env = std::make_shared<ArduinoEnvironment>();

// Setting up water pump
const auto waterPump = std::make_shared<WaterPumpScheduler>(
std::make_shared<WaterPumpController>(
WATER_PUMP_DIRECTION_PIN, WATER_PUMP_BRAKE_PIN, WATER_PUMP_POWER_PIN
std::make_shared<AdjustedWaterPump>(
std::make_shared<WaterPumpController>(
WATER_PUMP_DIRECTION_PIN, WATER_PUMP_BRAKE_PIN, WATER_PUMP_POWER_PIN
)
), env
);

Expand Down
1 change: 1 addition & 0 deletions controller/tea_poor/test/test_native/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <gtest/gtest.h>
#include "mocks/FakeWaterPump.h"
#include <AdjustedWaterPump.h>

// 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<FakeWaterPump>();
AdjustedWaterPump adjustedWaterPump(fakeWaterPump);
// list of pairs: (powerInPercents, expectedPower)
const std::vector<std::pair<int, int>> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 632f423

Please sign in to comment.