timer test
start transitions some fixes
This commit is contained in:
@@ -19,7 +19,7 @@ Valve::Valve(uint gpio_motor1, uint gpio_motor2, uint gpio_start, uint gpio_end)
|
||||
|
||||
|
||||
void Valve::open() {
|
||||
if (state == State::Opening) {
|
||||
if (state == State::Opening || pending_state == State::Opening) {
|
||||
return;
|
||||
}
|
||||
gpio_put(motor2, 0);
|
||||
@@ -30,7 +30,7 @@ void Valve::open() {
|
||||
|
||||
|
||||
void Valve::close() {
|
||||
if (state == State::Closing) {
|
||||
if (state == State::Closing || pending_state == State::Closing) {
|
||||
return;
|
||||
}
|
||||
gpio_put(motor1, 0);
|
||||
@@ -52,7 +52,7 @@ Valve::State Valve::process() {
|
||||
if (duration_cast<seconds>(steady_clock::now() - start_time) > timeout) {
|
||||
gpio_put(motor1, 0);
|
||||
gpio_put(motor2, 0);
|
||||
state = State::Unknown;
|
||||
state = pending_state = State::Unknown;
|
||||
}
|
||||
}
|
||||
return state;
|
||||
|
||||
7
Valve.h
7
Valve.h
@@ -24,7 +24,12 @@ public:
|
||||
private:
|
||||
uint motor1, motor2;
|
||||
uint start_pin, end_pin;
|
||||
State state = State::Unknown;
|
||||
|
||||
std::chrono::seconds timeout = std::chrono::seconds(10);
|
||||
std::chrono::milliseconds transition = std::chrono::milliseconds(1000);
|
||||
decltype(std::chrono::steady_clock::now()) start_time;
|
||||
decltype(std::chrono::steady_clock::now()) transition_time;
|
||||
|
||||
State state = State::Unknown;
|
||||
State pending_state = State::Unknown;
|
||||
};
|
||||
|
||||
@@ -3,6 +3,20 @@
|
||||
|
||||
#include <pico/stdlib.h>
|
||||
|
||||
enum class LedState {
|
||||
Off,
|
||||
On,
|
||||
Blink
|
||||
};
|
||||
|
||||
LedState led_state = LedState::Off;
|
||||
|
||||
bool led_timer_callback(struct repeating_timer * t) {
|
||||
if (led_state == LedState::Blink) {
|
||||
gpio_put(PICO_DEFAULT_LED_PIN, !gpio_get(PICO_DEFAULT_LED_PIN));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
int main() {
|
||||
Valve cold_valve{10, 11, 12, 13};
|
||||
@@ -14,6 +28,9 @@ int main() {
|
||||
gpio_init_input(button_open);
|
||||
gpio_init_input(button_close);
|
||||
|
||||
struct repeating_timer led_timer;
|
||||
add_repeating_timer_ms(-200, led_timer_callback, NULL, &led_timer);
|
||||
|
||||
const int tick_ms = 50;
|
||||
while (true) {
|
||||
if (gpio_get(button_open) == 0) {
|
||||
@@ -28,14 +45,16 @@ int main() {
|
||||
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;
|
||||
case Valve::State::Opened: led_state = LedState::On; break;
|
||||
case Valve::State::Closed: led_state = LedState::Off; break;
|
||||
case Valve::State::Opening: led_state = LedState::Blink; break;
|
||||
case Valve::State::Closing: led_state = LedState::Blink; break;
|
||||
case Valve::State::Unknown: led_state = LedState::Off; break;
|
||||
}
|
||||
if (cst == Valve::State::Opened || cst == Valve::State::Closed) {
|
||||
gpio_put(PICO_DEFAULT_LED_PIN, 1);
|
||||
} else {
|
||||
gpio_put(PICO_DEFAULT_LED_PIN, 0);
|
||||
switch (led_state) {
|
||||
case LedState::On: gpio_put(PICO_DEFAULT_LED_PIN, 1); break;
|
||||
case LedState::Off: gpio_put(PICO_DEFAULT_LED_PIN, 0); break;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
|
||||
@@ -2,10 +2,6 @@
|
||||
|
||||
#include <pico/stdlib.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
void gpio_init_out(uint gpio) {
|
||||
gpio_set_dir(gpio, GPIO_OUT);
|
||||
@@ -19,8 +15,3 @@ void gpio_init_input(uint gpio) {
|
||||
gpio_pull_up(gpio);
|
||||
gpio_set_function(gpio, GPIO_FUNC_SIO);
|
||||
}
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user