summaryrefslogtreecommitdiffstats
path: root/power-seq-software-seeed-rp2040/power-seq-software-seeed-rp2040.cpp
blob: c69dcf13ede0f22fa1f8d9bedab6637090865926 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <cstddef>
#include <cstdint>
#include <array>
#include "pico/stdlib.h"
#include "pico/sleep.h"
#include "hardware/gpio.h"

static constexpr std::size_t button_gpio = 6;
static constexpr std::size_t led_gpio = 7;
static constexpr std::array<std::size_t, 3> gpio_on_sequence { 2, 4, 3 };
static constexpr std::array<std::size_t, 3> gpio_off_sequence { 3, 4, 2 };
static constexpr std::size_t delay_ms = 2000;

static void init_dout(std::size_t gpio, bool state)
{
    gpio_init(gpio);
    gpio_set_dir(gpio, GPIO_OUT);
    gpio_put(gpio, state);
}

int main()
{
    bool state = false;

    // disable RGB LED (inverted logic)
    init_dout(25, true);
    init_dout(16, true);
    init_dout(17, true);

    // init button LED
    init_dout(led_gpio, state);

    // init button IO
    gpio_init(button_gpio);
    gpio_set_dir(button_gpio, GPIO_IN);

    // init relay GPIOs
    for (auto const & relay : gpio_off_sequence)
    {
        init_dout(relay, state);
    }

    while (true)
    {
        sleep_run_from_xosc();
        sleep_goto_dormant_until_pin(button_gpio, true, false); // edge, falling
        sleep_power_up();

        state = !state;
        gpio_put(led_gpio, state);

        auto const & sequence = state ? gpio_on_sequence : gpio_off_sequence;
        for (auto const & relay : sequence)
        {
            gpio_put(relay, state);
            sleep_ms(delay_ms);
        }
    }
}