refactoring
This commit is contained in:
@@ -1,89 +1,8 @@
|
||||
#include <chrono>
|
||||
#include "Valve.h"
|
||||
#include "gpio_common.h"
|
||||
|
||||
#include <pico/stdlib.h>
|
||||
|
||||
void gpio_init_out(uint gpio) {
|
||||
gpio_set_dir(gpio, GPIO_OUT);
|
||||
gpio_put(gpio, 0);
|
||||
gpio_set_function(gpio, GPIO_FUNC_SIO);
|
||||
}
|
||||
|
||||
void gpio_init_input(uint gpio) {
|
||||
gpio_set_dir(gpio, GPIO_IN);
|
||||
gpio_pull_up(gpio);
|
||||
gpio_set_function(gpio, GPIO_FUNC_SIO);
|
||||
}
|
||||
|
||||
class Valve {
|
||||
public:
|
||||
enum class State {
|
||||
Unknown,
|
||||
Opening,
|
||||
Opened,
|
||||
Closing,
|
||||
Closed
|
||||
};
|
||||
|
||||
Valve(uint gpio_motor1, uint gpio_motor2, uint gpio_start, uint gpio_end)
|
||||
: motor1(gpio_motor1)
|
||||
, motor2(gpio_motor2)
|
||||
, start_pin(gpio_start)
|
||||
, end_pin(gpio_end) {
|
||||
gpio_init_out(motor1);
|
||||
gpio_init_out(motor2);
|
||||
gpio_init_input(start_pin);
|
||||
gpio_init_input(end_pin);
|
||||
start_time = std::chrono::steady_clock::now();
|
||||
}
|
||||
|
||||
void open() {
|
||||
if (state == State::Opening) {
|
||||
return;
|
||||
}
|
||||
gpio_put(motor2, 0);
|
||||
gpio_put(motor1, 1);
|
||||
state = State::Opening;
|
||||
start_time = std::chrono::steady_clock::now();
|
||||
}
|
||||
|
||||
void close() {
|
||||
if (state == State::Closing) {
|
||||
return;
|
||||
}
|
||||
gpio_put(motor1, 0);
|
||||
gpio_put(motor2, 1);
|
||||
state = State::Closing;
|
||||
start_time = std::chrono::steady_clock::now();
|
||||
}
|
||||
|
||||
State process() {
|
||||
if ((state == State::Opening) && (gpio_get(start_pin) == 0)) {
|
||||
gpio_put(motor1, 0);
|
||||
state = State::Opened;
|
||||
}
|
||||
if ((state == State::Closing) && (gpio_get(end_pin) == 0)) {
|
||||
gpio_put(motor2, 0);
|
||||
state = State::Closed;
|
||||
}
|
||||
if (state == State::Opening || state == State::Closing) {
|
||||
using namespace std::chrono;
|
||||
if (duration_cast<seconds>(steady_clock::now() - start_time) > timeout) {
|
||||
gpio_put(motor1, 0);
|
||||
gpio_put(motor2, 0);
|
||||
state = State::Unknown;
|
||||
}
|
||||
}
|
||||
return state;
|
||||
}
|
||||
|
||||
State getState() const { return state; }
|
||||
|
||||
private:
|
||||
uint motor1, motor2;
|
||||
uint start_pin, end_pin;
|
||||
State state = State::Unknown;
|
||||
std::chrono::seconds timeout = std::chrono::seconds(10);
|
||||
decltype(std::chrono::steady_clock::now()) start_time;
|
||||
};
|
||||
|
||||
int main() {
|
||||
Valve cold_valve{10, 11, 12, 13};
|
||||
@@ -108,6 +27,11 @@ int main() {
|
||||
sleep_ms(tick_ms);
|
||||
const auto cst = cold_valve.process();
|
||||
hot_valve.process();
|
||||
switch (cst) {
|
||||
case Valve::State::Opened:
|
||||
case Valve::State::Closed: gpio_put(PICO_DEFAULT_LED_PIN, 1); break;
|
||||
default: break;
|
||||
}
|
||||
if (cst == Valve::State::Opened || cst == Valve::State::Closed) {
|
||||
gpio_put(PICO_DEFAULT_LED_PIN, 1);
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user