fork/GramFirmware

git clone https://git.julia.blue/fork/GramFirmware

CarVacrebase to configurator818d962

gram
2.9 KiB89 linesraw
1#ifndef _COMMS_NEOPIXELBACKEND_HPP
2#define _COMMS_NEOPIXELBACKEND_HPP
3
4#include "core/CommunicationBackend.hpp"
5
6#include <FastLED.h>
7#include <config.pb.h>
8#include <time.h>
9
10template <uint8_t data_pin, int led_count> class NeoPixelBackend : public CommunicationBackend {
11  public:
12    NeoPixelBackend(
13        InputState &inputs,
14        InputSource **input_sources,
15        size_t input_source_count,
16        const Button *button_mappings,
17        const RgbConfig *rgb_configs,
18        const size_t rgb_configs_count,
19        const uint8_t &brightness
20    )
21        : CommunicationBackend(inputs, input_sources, input_source_count),
22          _button_mappings(button_mappings),
23          _rgb_configs(rgb_configs),
24          _rgb_configs_count(rgb_configs_count),
25          _brightness(brightness) {
26        FastLED.addLeds<NEOPIXEL, data_pin>(_leds, led_count);
27        FastLED.setMaxPowerInVoltsAndMilliamps(5, 200);
28        FastLED.setMaxRefreshRate(0);
29    }
30
31    ~NeoPixelBackend() { FastLED.clear(true); }
32
33    virtual void SetGameMode(InputMode *gamemode) {
34        // Clear current button colors.
35        for (size_t i = 0; i < button_colors_count; i++) {
36            _button_colors[i] = 0;
37        }
38
39        if (gamemode == nullptr || gamemode->GetConfig() == nullptr) {
40            _config = nullptr;
41            return;
42        }
43
44        uint8_t rgb_config_id = gamemode->GetConfig()->rgb_config;
45        if (rgb_config_id == 0 || rgb_config_id > _rgb_configs_count) {
46            _config = nullptr;
47            return;
48        }
49        _config = &_rgb_configs[rgb_config_id - 1];
50
51        // Build new mapping array to give O(n) lookup later.
52        for (size_t i = 0; i < _config->button_colors_count; i++) {
53            ButtonToColorMapping mapping = _config->button_colors[i];
54            _button_colors[max(0, mapping.button - 1)] = mapping.color;
55        }
56    }
57
58    virtual void SendReport() {
59        // Use timeout to avoid refreshing too fast which results in FastLED library blocking.
60        if (!time_reached(_refresh_timeout)) {
61            return;
62        }
63        _refresh_timeout = make_timeout_time_ms(refresh_interval_ms);
64
65        for (int i = 0; i < led_count; i++) {
66            Button button = this->_button_mappings[i];
67            _leds[i] = _config != nullptr ? _button_colors[max(0, button - 1)] : 0;
68        }
69        FastLED.setBrightness(_brightness);
70        FastLED.show();
71    }
72
73  protected:
74    static constexpr size_t button_colors_count =
75        sizeof(RgbConfig::button_colors) / sizeof(ButtonToColorMapping);
76    static constexpr uint64_t refresh_interval_ms = 4; // 250Hz refresh rate
77
78    const Button *_button_mappings;
79    const RgbConfig *_rgb_configs;
80    const size_t _rgb_configs_count;
81    const RgbConfig *_config = nullptr;
82    const uint8_t &_brightness;
83
84    CRGB _leds[led_count];
85    uint32_t _button_colors[button_colors_count];
86    absolute_time_t _refresh_timeout = 0;
87};
88
89#endif