generated from pimoroni/pico-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathledcontrol.h
138 lines (114 loc) · 3.72 KB
/
ledcontrol.h
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#ifndef LEDCONTROL_H
#define LEDCONTROL_H
#include <cstdint>
#include <cmath>
#include <common/pimoroni_common.hpp>
#include "button.hpp"
#include <drivers/plasma/ws2812.hpp>
#include "encoder.h"
namespace ledcontrol {
class LEDControl {
private:
enum MENU_MODE : uint8_t {
MENU_SELECT,
MENU_ADJUST,
MENU_COUNT
};
public:
LEDControl();
enum ENCODER_MODE : uint8_t {
OFF = 0,
COLOUR,
ANGLE,
BRIGHTNESS,
SPEED,
EFFECT,
MODE_COUNT
};
enum EFFECT_MODE : uint8_t {
HUE_CYCLE = 0,
WHITE_CHASE,
EFFECT_COUNT,
};
static const uint8_t SPEED_COUNT = 5;
// state of things
typedef struct {
float_t hue;
float_t angle;
float_t speed;
float_t brightness;
enum EFFECT_MODE effect;
enum ENCODER_MODE mode;
bool on;
bool stopped; // if we're not cycling (effective speed is 0)
bool absent; // used for presence detection
} state_t;
void init(Encoder *e);
uint32_t loop(); // returns: required sleep value in ms
void enable_state(state_t p_state);
state_t get_state();
void log_state(const char *prefix, state_t s);
// iot control helpers
const char* effect_to_str(EFFECT_MODE effect);
const char* speed_to_str(float_t speed);
float_t str_to_speed(const char *str);
int parse_effect_str(const char *str, EFFECT_MODE *effect, float_t *speed);
size_t get_effect_list(LEDControl::EFFECT_MODE *effects, size_t num_effects);
size_t get_speed_list(const char **speeds, size_t num_speeds);
// flashy things
int load_state_from_flash();
void save_state_to_flash();
void set_on_state_change_cb(void (*cb)(state_t new_state)) { _on_state_change_cb = cb; }
private:
state_t state;
uint32_t encoder_last_blink;
bool encoder_blink_state;
uint32_t encoder_last_activity;
uint32_t global_last_activity;
uint32_t start_time, stop_time;
uint32_t transition_start_time;
uint32_t transition_duration;
float_t transition_start_brightness;
float_t transition_target_brightness;
float_t eff_brightness = -1.0f;
plasma::WS2812 led_strip;
pimoroni::Button button_b;
pimoroni::Button button_c;
Encoder *enc = nullptr;
enum MENU_MODE menu_mode;
bool cycle{}, cycle_once{};
const char flash_save_magic[8] = "LEDCTRL";
typedef struct {
char magic[8];
size_t state_size;
state_t state;
} flash_state_t;
void (*_on_state_change_cb)(state_t new_state);
// private methods
void set_brightness(float_t brightness);
void cycle_loop(float hue, float t, float angle, bool refresh = false);
uint16_t get_paused_time();
float_t get_effective_brightness();
bool transition_loop(bool force);
void set_cycle(bool v);
uint32_t encoder_colour_by_mode(ENCODER_MODE mode);
void encoder_loop();
void set_encoder_state();
void encoder_blink_off();
bool get_effective_on_state(state_t s);
int _save_state_to_flash();
// strings
const char *effect_str[EFFECT_COUNT] = {
"hue_cycle",
"white_chase",
};
const char *speed_str[SPEED_COUNT] = {
"stopped",
"superslow",
"slow",
"medium",
"fast",
};
};
}
#endif //LEDCONTROL_H