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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
/*
* simple.c: Yet another UI for the Emisar D3AA
* Copyright (C) 2026 git@apexo.de
* SPDX-License-Identifier: GPL-3.0-or-later
*/
/*
* States:
* - Off : all lights off, standby
* - Aux : aux RGB shows color-coded battery voltage
* - On : main LED on (one of 4 different brightness levels),
* aux RGB shows color-coded battery voltage
*
* Transitions:
* - Off + Click -> Aux
* - Aux + Click -> On
* - Aux + 5s -> Off
* - On + Click -> Aux
* - On + Hold -> cycle brightness levels, store on release
*/
#include "arch/mcu.h"
#include incfile(CFG_H)
#ifdef HWDEF_H
#include incfile(HWDEF_H)
#endif
#define USE_IDLE_MODE
#include "fsm/spaghetti-monster.h"
#ifdef HWDEF_C
#include incfile(HWDEF_C)
#endif
#define NUM_LEVELS 4
#define CEIL 100
static const uint8_t brightness_levels[NUM_LEVELS] = {
1,
1 * CEIL / 10,
3 * CEIL / 10,
CEIL,
};
#define HOLD_TICKS_PER_LEVEL 48 // cycle time 0.75s (48/64 Hz)
#define AUX_TIMEOUT_TICKS 320 // 5s
#define COLOR_TICKS 320 // 5s
uint8_t off_state(Event event, uint16_t arg);
uint8_t aux_state(Event event, uint16_t arg);
uint8_t on_state(Event event, uint16_t arg);
uint8_t color_state(Event event, uint16_t arg);
uint8_t current_level_idx = 0;
uint8_t current_level = 0;
uint8_t thermal_limit = CEIL;
static const uint8_t voltage_levels[] = {
0, 0, // black
#ifdef DUAL_VOLTAGE_FLOOR
// NiMH
9*dV, 0x01, // R
10*dV, 0x05, // R+G
11*dV, 0x04, // G
12*dV, 0x14, // G+B
13*dV, 0x10, // B
14*dV, 0x11, // R + B
16*dV, 0x15, // R+G+B
20*dV, 0, // black
#endif
// li-ion
29*dV, 0x01, // R
33*dV, 0x05, // R+G
35*dV, 0x04, // G
37*dV, 0x14, // G+B
39*dV, 0x10, // B
41*dV, 0x11, // R + B
44*dV, 0x15, // R+G+B
255, 0x15, // R+G+B
};
#define NUM_COLORS 12
static const uint8_t color_wheel[NUM_COLORS] = {
0b000010,
0b000110,
0b001010,
0b001001,
0b001000,
0b011000,
0b101000,
0b100100,
0b100000,
0b100001,
0b100010,
0b010010,
};
uint8_t color_idx = 0;
static uint8_t voltage_color(void) {
uint8_t v = voltage;
if (v == 0) return 0;
v -= 1;
uint8_t i;
for (i = 0; v > voltage_levels[i]; i += 2) {}
return voltage_levels[i - 1];
}
void set_level_therm(uint8_t value, uint16_t thermal_headroom, uint16_t thermal_overshoot) {
if (current_level == thermal_limit && thermal_headroom > 0) {
if (thermal_headroom >= CEIL - thermal_limit) {
thermal_limit = CEIL;
} else {
thermal_limit += thermal_headroom;
}
}
if (thermal_overshoot > 0) {
if (thermal_overshoot >= current_level) {
thermal_limit = 1;
} else {
thermal_limit = current_level - thermal_overshoot;
}
}
current_level = value <= thermal_limit ? value : thermal_limit;
set_level(current_level);
}
uint8_t off_state(Event event, uint16_t arg) {
if (event == EV_enter_state) {
set_level(0);
rgb_led_set(0);
button_led_set(0);
go_to_standby = 1;
return EVENT_HANDLED;
}
if (event == EV_1click) {
set_state(aux_state, 0);
return EVENT_HANDLED;
}
return EVENT_NOT_HANDLED;
}
uint8_t aux_state(Event event, uint16_t arg) {
if (event == EV_enter_state) {
set_level(0);
rgb_led_set(voltage_color() << 1);
button_led_set(1);
return EVENT_HANDLED;
}
if (event == EV_1click) {
set_state(on_state, 0);
return EVENT_HANDLED;
}
if (event == EV_3clicks) {
set_state(color_state, 0);
return EVENT_HANDLED;
}
if (event == EV_tick) {
if (arg >= AUX_TIMEOUT_TICKS) {
set_state(off_state, 0);
}
return EVENT_HANDLED;
}
return EVENT_NOT_HANDLED;
}
uint8_t on_state(Event event, uint16_t arg) {
if (event == EV_enter_state) {
set_level_therm(brightness_levels[current_level_idx], 0, 0);
rgb_led_set(voltage_color() << 1);
button_led_set(2);
return EVENT_HANDLED;
}
if (event == EV_tick) {
if ((arg & 63) == 0) {
rgb_led_set(voltage_color() << 1);
}
return EVENT_HANDLED;
}
if (event == EV_temperature_high) {
set_level_therm(brightness_levels[current_level_idx], 0, arg);
return EVENT_HANDLED;
}
if (event == EV_temperature_low) {
set_level_therm(brightness_levels[current_level_idx], arg, 0);
return EVENT_HANDLED;
}
if (event == EV_1click) {
set_state(aux_state, 0);
return EVENT_HANDLED;
}
if (event == EV_click1_hold) {
if (arg % HOLD_TICKS_PER_LEVEL == 0) {
current_level_idx = (current_level_idx + 1) % NUM_LEVELS;
set_level_therm(brightness_levels[current_level_idx], 0, 0);
}
return EVENT_HANDLED;
}
if (event == EV_click1_hold_release) {
eeprom[0] = current_level_idx;
save_eeprom();
return EVENT_HANDLED;
}
if (event == EV_3clicks) {
set_state(color_state, 0);
return EVENT_HANDLED;
}
return EVENT_NOT_HANDLED;
}
uint8_t color_state(Event event, uint16_t arg) {
if (color_idx >= NUM_COLORS) {
color_idx = 0;
}
if (event == EV_enter_state) {
set_level_therm(0, 0, 0);
rgb_led_set(color_wheel[color_idx]);
button_led_set(2);
return EVENT_HANDLED;
}
if (event == EV_1click) {
set_state(aux_state, 0);
return EVENT_HANDLED;
}
if (event == EV_tick) {
if (!(arg % COLOR_TICKS)) {
if (++color_idx == NUM_COLORS) color_idx = 0;
rgb_led_set(color_wheel[color_idx]);
}
return EVENT_HANDLED;
}
return EVENT_NOT_HANDLED;
}
void low_voltage(void) {
set_state(off_state, 0);
}
void setup(void) {
#ifdef USE_WEAK_BATTERY_PROTECTION
detect_weak_battery();
#endif
if (load_eeprom()) {
if (eeprom[0] < NUM_LEVELS)
current_level_idx = eeprom[0];
}
push_state(off_state, 0);
}
#ifndef BLINK_BRIGHTNESS
#define BLINK_BRIGHTNESS (MAX_LEVEL/6)
#endif
void blink_once() {
uint8_t brightness = actual_level;
uint8_t bump = brightness + BLINK_BRIGHTNESS;
if (bump > MAX_LEVEL) bump = BLIP_LEVEL;
set_level(bump);
delay_4ms(BLINK_ONCE_TIME/4);
set_level(brightness);
}
void loop(void) {
StatePtr state = current_state;
if (state == on_state || state == aux_state || state == color_state) {
idle_mode();
}
}
|