Files
ValveController/Valve.cpp
Andrey Bychkov 5a3a79bba2 timer test
start transitions
some fixes
2024-02-05 20:28:24 +03:00

60 lines
1.4 KiB
C++

#include "Valve.h"
#include "gpio_common.h"
using namespace std::chrono;
Valve::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 = steady_clock::now();
}
void Valve::open() {
if (state == State::Opening || pending_state == State::Opening) {
return;
}
gpio_put(motor2, 0);
gpio_put(motor1, 1);
state = State::Opening;
start_time = steady_clock::now();
}
void Valve::close() {
if (state == State::Closing || pending_state == State::Closing) {
return;
}
gpio_put(motor1, 0);
gpio_put(motor2, 1);
state = State::Closing;
start_time = steady_clock::now();
}
Valve::State Valve::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) {
if (duration_cast<seconds>(steady_clock::now() - start_time) > timeout) {
gpio_put(motor1, 0);
gpio_put(motor2, 0);
state = pending_state = State::Unknown;
}
}
return state;
}