62 lines
1.5 KiB
C++
62 lines
1.5 KiB
C++
#include "Valve.h"
|
|
#include "gpio_common.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() {
|
|
Valve cold_valve{10, 11, 12, 13};
|
|
Valve hot_valve{21, 20, 19, 18};
|
|
|
|
const int button_open = 14;
|
|
const int button_close = 15;
|
|
gpio_init_out(PICO_DEFAULT_LED_PIN);
|
|
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) {
|
|
cold_valve.open();
|
|
hot_valve.open();
|
|
}
|
|
if (gpio_get(button_close) == 0) {
|
|
cold_valve.close();
|
|
hot_valve.close();
|
|
}
|
|
sleep_ms(tick_ms);
|
|
const auto cst = cold_valve.process();
|
|
hot_valve.process();
|
|
switch (cst) {
|
|
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;
|
|
}
|
|
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;
|
|
}
|