summaryrefslogtreecommitdiffhomepage
path: root/tracker-trackhat/led.cpp
blob: db5ab6c23851fc44cacf4dea1fc0ebf3d995c376 (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
60
61
62
63
64
65
66
#include "trackhat.hpp"

namespace trackhat_impl {

void led_updater::update_(trackHat_Device_t* device, trackHat_SetLeds_t leds)
{
    if (leds_.ledRedState == leds.ledRedState &&
        leds_.ledGreenState == leds.ledGreenState &&
        leds_.ledBlueState == leds.ledBlueState)
        return;
    (void)trackHat_SetLeds(device, &leds);
    leds_ = leds;
}

trackHat_SetLeds_t led_updater::next_state(led_mode mode, led_state new_state)
{
    switch (mode)
    {
    case led_mode::off:
        state_ = led_state::stopped;
        timer_ = std::nullopt;
        return LED_off;
    default:
    case led_mode::constant:
        state_ = led_state::stopped;
        timer_ = std::nullopt;
        return LED_idle;
    case led_mode::dynamic:
        break;
    }

    if (new_state <= led_state::stopped)
    {
        state_ = new_state;
        timer_ = std::nullopt;
        return LED_idle;
    }
    else if (new_state == state_)
    {
        timer_ = std::nullopt;
        return leds_;
    }
    else if (!timer_)
    {
        timer_ = Timer{};
        return leds_;
    }
    else if (timer_->elapsed_ms() > SWITCH_TIME_MS)
    {
        state_ = new_state;
        timer_ = std::nullopt;
        return new_state == led_state::not_tracking
               ? LED_not_tracking
               : LED_tracking;
    }
    else
        return leds_;
}

void led_updater::update(trackHat_Device_t* device, led_mode mode, led_state new_state)
{
    auto leds = next_state(mode, new_state);
    update_(device, leds);
}

} // namespace trackhat_impl