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() {
|
void Valve::open() {
|
||||||
if (state == State::Opening) {
|
if (state == State::Opening || pending_state == State::Opening) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
gpio_put(motor2, 0);
|
gpio_put(motor2, 0);
|
||||||
@@ -30,7 +30,7 @@ void Valve::open() {
|
|||||||
|
|
||||||
|
|
||||||
void Valve::close() {
|
void Valve::close() {
|
||||||
if (state == State::Closing) {
|
if (state == State::Closing || pending_state == State::Closing) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
gpio_put(motor1, 0);
|
gpio_put(motor1, 0);
|
||||||
@@ -52,7 +52,7 @@ Valve::State Valve::process() {
|
|||||||
if (duration_cast<seconds>(steady_clock::now() - start_time) > timeout) {
|
if (duration_cast<seconds>(steady_clock::now() - start_time) > timeout) {
|
||||||
gpio_put(motor1, 0);
|
gpio_put(motor1, 0);
|
||||||
gpio_put(motor2, 0);
|
gpio_put(motor2, 0);
|
||||||
state = State::Unknown;
|
state = pending_state = State::Unknown;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return state;
|
return state;
|
||||||
|
|||||||
9
Valve.h
9
Valve.h
@@ -24,7 +24,12 @@ public:
|
|||||||
private:
|
private:
|
||||||
uint motor1, motor2;
|
uint motor1, motor2;
|
||||||
uint start_pin, end_pin;
|
uint start_pin, end_pin;
|
||||||
State state = State::Unknown;
|
|
||||||
std::chrono::seconds timeout = std::chrono::seconds(10);
|
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()) 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>
|
#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() {
|
int main() {
|
||||||
Valve cold_valve{10, 11, 12, 13};
|
Valve cold_valve{10, 11, 12, 13};
|
||||||
@@ -14,6 +28,9 @@ int main() {
|
|||||||
gpio_init_input(button_open);
|
gpio_init_input(button_open);
|
||||||
gpio_init_input(button_close);
|
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;
|
const int tick_ms = 50;
|
||||||
while (true) {
|
while (true) {
|
||||||
if (gpio_get(button_open) == 0) {
|
if (gpio_get(button_open) == 0) {
|
||||||
@@ -28,14 +45,16 @@ int main() {
|
|||||||
const auto cst = cold_valve.process();
|
const auto cst = cold_valve.process();
|
||||||
hot_valve.process();
|
hot_valve.process();
|
||||||
switch (cst) {
|
switch (cst) {
|
||||||
case Valve::State::Opened:
|
case Valve::State::Opened: led_state = LedState::On; break;
|
||||||
case Valve::State::Closed: gpio_put(PICO_DEFAULT_LED_PIN, 1); break;
|
case Valve::State::Closed: led_state = LedState::Off; break;
|
||||||
default: 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) {
|
switch (led_state) {
|
||||||
gpio_put(PICO_DEFAULT_LED_PIN, 1);
|
case LedState::On: gpio_put(PICO_DEFAULT_LED_PIN, 1); break;
|
||||||
} else {
|
case LedState::Off: gpio_put(PICO_DEFAULT_LED_PIN, 0); break;
|
||||||
gpio_put(PICO_DEFAULT_LED_PIN, 0);
|
default: break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@@ -2,10 +2,6 @@
|
|||||||
|
|
||||||
#include <pico/stdlib.h>
|
#include <pico/stdlib.h>
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
extern "C" {
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
void gpio_init_out(uint gpio) {
|
void gpio_init_out(uint gpio) {
|
||||||
gpio_set_dir(gpio, GPIO_OUT);
|
gpio_set_dir(gpio, GPIO_OUT);
|
||||||
@@ -19,8 +15,3 @@ void gpio_init_input(uint gpio) {
|
|||||||
gpio_pull_up(gpio);
|
gpio_pull_up(gpio);
|
||||||
gpio_set_function(gpio, GPIO_FUNC_SIO);
|
gpio_set_function(gpio, GPIO_FUNC_SIO);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|||||||
Reference in New Issue
Block a user