Merge lp:~pakutrai/flashlight-firmware/fsm into lp:~toykeeper/flashlight-firmware/fsm

Proposed by 0-8-15 User
Status: Needs review
Proposed branch: lp:~pakutrai/flashlight-firmware/fsm
Merge into: lp:~toykeeper/flashlight-firmware/fsm
Diff against target: 1043 lines (+181/-445)
21 files modified
ToyKeeper/spaghetti-monster/anduril/anduril.c (+24/-62)
ToyKeeper/spaghetti-monster/anduril/cfg-emisar-d18.h (+0/-2)
ToyKeeper/spaghetti-monster/anduril/cfg-emisar-d4.h (+0/-4)
ToyKeeper/spaghetti-monster/anduril/cfg-emisar-d4s.h (+0/-5)
ToyKeeper/spaghetti-monster/anduril/cfg-emisar-d4sv2-219.h (+3/-0)
ToyKeeper/spaghetti-monster/anduril/cfg-emisar-d4sv2.h (+2/-14)
ToyKeeper/spaghetti-monster/anduril/cfg-emisar-d4v2-219.h (+3/-0)
ToyKeeper/spaghetti-monster/anduril/cfg-emisar-d4v2.h (+2/-12)
ToyKeeper/spaghetti-monster/anduril/cfg-ff-pl47.h (+0/-5)
ToyKeeper/spaghetti-monster/anduril/cfg-ff-pl47g2.h (+0/-5)
ToyKeeper/spaghetti-monster/anduril/cfg-ff-rot66.h (+0/-3)
ToyKeeper/spaghetti-monster/anduril/cfg-mateminco-mf01-mini.h (+0/-5)
ToyKeeper/spaghetti-monster/anduril/cfg-mateminco-mf01s.h (+0/-2)
ToyKeeper/spaghetti-monster/anduril/cfg-sofirn-sp36.h (+0/-7)
ToyKeeper/spaghetti-monster/fsm-adc.c (+115/-267)
ToyKeeper/spaghetti-monster/fsm-adc.h (+18/-30)
ToyKeeper/spaghetti-monster/fsm-misc.c (+1/-1)
ToyKeeper/spaghetti-monster/fsm-misc.h (+1/-1)
ToyKeeper/spaghetti-monster/fsm-standby.c (+0/-1)
ToyKeeper/spaghetti-monster/fsm-wdt.c (+9/-10)
ToyKeeper/spaghetti-monster/spaghetti-monster.txt (+3/-9)
To merge this branch: bzr merge lp:~pakutrai/flashlight-firmware/fsm
Reviewer Review Type Date Requested Status
Selene ToyKeeper Pending
Review via email: mp+377134@code.launchpad.net
To post a comment you must log in.
lp:~pakutrai/flashlight-firmware/fsm updated
465. By 0-8-15 User

New thermal regulation

Unmerged revisions

465. By 0-8-15 User

New thermal regulation

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'ToyKeeper/spaghetti-monster/anduril/anduril.c'
2--- ToyKeeper/spaghetti-monster/anduril/anduril.c 2019-12-04 09:00:16 +0000
3+++ ToyKeeper/spaghetti-monster/anduril/anduril.c 2020-01-30 10:15:24 +0000
4@@ -22,11 +22,10 @@
5 // Anduril config file name (set it here or define it at the gcc command line)
6 //#define CONFIGFILE cfg-blf-q8.h
7
8-#define USE_LVP // FIXME: won't build when this option is turned off
9+#define USE_LVP
10
11 // parameters for this defined below or per-driver
12 #define USE_THERMAL_REGULATION
13-#define DEFAULT_THERM_CEIL 45 // try not to get hotter than this
14
15 #define USE_FACTORY_RESET
16 //#define USE_SOFT_FACTORY_RESET // only needed on models which can't use hold-button-at-boot
17@@ -957,80 +956,41 @@
18 if (arg == TICKS_PER_SECOND) ramp_direction = 1;
19 #endif
20 #ifdef USE_SET_LEVEL_GRADUALLY
21- // make thermal adjustment speed scale with magnitude
22- // also, adjust slower when going up
23- if ((arg & 1) &&
24- ((actual_level < THERM_FASTER_LEVEL) ||
25- (actual_level < gradual_target))) {
26- return MISCHIEF_MANAGED; // adjust slower when not a high mode
27- }
28- #ifdef THERM_HARD_TURBO_DROP
29- else if ((! (actual_level < THERM_FASTER_LEVEL))
30- && (actual_level > gradual_target)) {
31- gradual_tick();
32- }
33- else {
34- #endif
35- // [int(62*4 / (x**0.8)) for x in (1,2,4,8,16,32,64,128)]
36- //uint8_t intervals[] = {248, 142, 81, 46, 26, 15, 8, 5};
37- // [int(62*4 / (x**0.9)) for x in (1,2,4,8,16,32,64,128)]
38- //uint8_t intervals[] = {248, 132, 71, 38, 20, 10, 5, 3};
39- // [int(62*4 / (x**0.95)) for x in (1,2,4,8,16,32,64,128)]
40- uint8_t intervals[] = {248, 128, 66, 34, 17, 9, 4, 2};
41- uint8_t diff;
42- static uint8_t ticks_since_adjust = 0;
43- if (gradual_target > actual_level) {
44- // rise at half speed (skip half the frames)
45- if (arg & 2) return MISCHIEF_MANAGED;
46- diff = gradual_target - actual_level;
47- } else {
48- diff = actual_level - gradual_target;
49- }
50- ticks_since_adjust ++;
51- // if there's any adjustment to be made, make it
52+ int16_t diff = gradual_target - actual_level;
53+ static uint16_t ticks_since_adjust = 0;
54+ ticks_since_adjust++;
55 if (diff) {
56- uint8_t magnitude = 0;
57- #ifndef THERM_HARD_TURBO_DROP
58- // if we're on a really high mode, drop faster
59- if ((actual_level >= THERM_FASTER_LEVEL)
60- && (actual_level > gradual_target)) { magnitude ++; }
61- #endif
62+ uint16_t ticks_per_adjust = 256;
63+ if (diff < 0) {
64+ if (actual_level > THERM_FASTER_LEVEL) {
65+ #ifdef THERM_HARD_TURBO_DROP
66+ ticks_per_adjust >>= 2;
67+ #endif
68+ ticks_per_adjust >>= 2;
69+ }
70+ } else {
71+ // rise at half speed
72+ ticks_per_adjust <<= 1;
73+ }
74 while (diff) {
75- magnitude ++;
76- diff >>= 1;
77+ ticks_per_adjust >>= 1;
78+ diff /= 2; // must not be shifted right!
79 }
80- uint8_t ticks_per_adjust = intervals[magnitude];
81 if (ticks_since_adjust > ticks_per_adjust)
82 {
83 gradual_tick();
84 ticks_since_adjust = 0;
85 }
86- //if (!(arg % ticks_per_adjust)) gradual_tick();
87- }
88- #ifdef THERM_HARD_TURBO_DROP
89- }
90- #endif
91+ }
92 #endif // ifdef USE_SET_LEVEL_GRADUALLY
93 return MISCHIEF_MANAGED;
94 }
95 #endif
96 #ifdef USE_THERMAL_REGULATION
97- // overheating: drop by an amount proportional to how far we are above the ceiling
98 else if (event == EV_temperature_high) {
99 #if 0
100 blip();
101 #endif
102- #ifdef THERM_HARD_TURBO_DROP
103- //if (actual_level > THERM_FASTER_LEVEL) {
104- if (actual_level == MAX_LEVEL) {
105- #ifdef USE_SET_LEVEL_GRADUALLY
106- set_level_gradually(THERM_FASTER_LEVEL);
107- target_level = THERM_FASTER_LEVEL;
108- #else
109- set_level_and_therm_target(THERM_FASTER_LEVEL);
110- #endif
111- } else
112- #endif
113 if (actual_level > MIN_THERM_STEPDOWN) {
114 int16_t stepdown = actual_level - arg;
115 if (stepdown < MIN_THERM_STEPDOWN) stepdown = MIN_THERM_STEPDOWN;
116@@ -1043,8 +1003,6 @@
117 }
118 return MISCHIEF_MANAGED;
119 }
120- // underheating: increase slowly if we're lower than the target
121- // (proportional to how low we are)
122 else if (event == EV_temperature_low) {
123 #if 0
124 blip();
125@@ -1598,11 +1556,13 @@
126 set_state(off_state, 0);
127 return MISCHIEF_MANAGED;
128 }
129+ #ifdef USE_BATTCHECK
130 // 2 clicks: battcheck mode
131 else if (event == EV_2clicks) {
132 set_state(battcheck_state, 0);
133 return MISCHIEF_MANAGED;
134 }
135+ #endif
136 // 4 clicks: thermal config mode
137 else if (event == EV_4clicks) {
138 push_state(thermal_config_state, 0);
139@@ -1628,7 +1588,7 @@
140 set_state(sos_state, 0);
141 #elif defined(USE_THERMAL_REGULATION)
142 set_state(tempcheck_state, 0);
143- #else
144+ #elif defined(USE_BATTCHECK)
145 set_state(battcheck_state, 0);
146 #endif
147 return MISCHIEF_MANAGED;
148@@ -2010,6 +1970,7 @@
149 return MISCHIEF_MANAGED;
150 }
151 #endif
152+ #ifdef USE_LVP
153 // low voltage is handled specially in muggle mode
154 else if(event == EV_voltage_low) {
155 uint8_t lvl = (actual_level >> 1) + (actual_level >> 2);
156@@ -2020,6 +1981,7 @@
157 }
158 return MISCHIEF_MANAGED;
159 }
160+ #endif
161
162 return EVENT_NOT_HANDLED;
163 }
164
165=== modified file 'ToyKeeper/spaghetti-monster/anduril/cfg-emisar-d18.h'
166--- ToyKeeper/spaghetti-monster/anduril/cfg-emisar-d18.h 2019-11-25 04:35:40 +0000
167+++ ToyKeeper/spaghetti-monster/anduril/cfg-emisar-d18.h 2020-01-30 10:15:24 +0000
168@@ -42,5 +42,3 @@
169
170 // stop panicking at about ~40% power or ~5000 lm
171 #define THERM_FASTER_LEVEL 125
172-// optional, makes initial turbo step-down faster so first peak isn't as hot
173-//#define THERM_HARD_TURBO_DROP
174
175=== modified file 'ToyKeeper/spaghetti-monster/anduril/cfg-emisar-d4.h'
176--- ToyKeeper/spaghetti-monster/anduril/cfg-emisar-d4.h 2019-08-05 07:40:20 +0000
177+++ ToyKeeper/spaghetti-monster/anduril/cfg-emisar-d4.h 2020-01-30 10:15:24 +0000
178@@ -24,8 +24,4 @@
179
180 // stop panicking at ~30% power or ~1200 lm
181 #define THERM_FASTER_LEVEL 105
182-// respond to thermal changes faster
183-#define THERMAL_WARNING_SECONDS 3
184-#define THERMAL_UPDATE_SPEED 1
185-#define THERM_PREDICTION_STRENGTH 4
186
187
188=== modified file 'ToyKeeper/spaghetti-monster/anduril/cfg-emisar-d4s.h'
189--- ToyKeeper/spaghetti-monster/anduril/cfg-emisar-d4s.h 2019-11-15 02:28:32 +0000
190+++ ToyKeeper/spaghetti-monster/anduril/cfg-emisar-d4s.h 2020-01-30 10:15:24 +0000
191@@ -42,8 +42,3 @@
192 #ifdef THERM_HARD_TURBO_DROP
193 #undef THERM_HARD_TURBO_DROP
194 #endif
195-
196-#define THERMAL_WARNING_SECONDS 3
197-#define THERMAL_UPDATE_SPEED 2
198-#define THERM_PREDICTION_STRENGTH 4
199-
200
201=== modified file 'ToyKeeper/spaghetti-monster/anduril/cfg-emisar-d4sv2-219.h'
202--- ToyKeeper/spaghetti-monster/anduril/cfg-emisar-d4sv2-219.h 2019-08-29 10:57:54 +0000
203+++ ToyKeeper/spaghetti-monster/anduril/cfg-emisar-d4sv2-219.h 2020-01-30 10:15:24 +0000
204@@ -8,3 +8,6 @@
205 #define PWM1_LEVELS 1,1,2,2,3,3,4,5,5,6,7,8,9,10,11,12,13,17,18,19,20,21,22,24,26,28,30,33,35,38,41,44,47,50,54,57,61,65,69,74,79,84,89,94,100,106,113,119,126,134,142,150,158,167,176,186,196,207,218,230,242,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
206 #define PWM2_LEVELS 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,12,16,21,25,30,35,41,46,52,58,64,71,77,84,92,99,107,115,124,133,142,151,161,172,182,193,205,217,229,242,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
207 #define PWM3_LEVELS 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,4,6,7,9,10,12,13,15,17,18,20,22,24,26,28,30,33,35,37,40,43,45,48,50,53,56,59,62,65,69,72,76,80,83,87,91,95,99,104,108,113,117,123,127,132,138,143,148,154,160,166,172,178,185,192
208+
209+#undef THERM_LOOKAHEAD
210+#define THERM_LOOKAHEAD 3
211
212=== modified file 'ToyKeeper/spaghetti-monster/anduril/cfg-emisar-d4sv2.h'
213--- ToyKeeper/spaghetti-monster/anduril/cfg-emisar-d4sv2.h 2019-11-15 02:28:32 +0000
214+++ ToyKeeper/spaghetti-monster/anduril/cfg-emisar-d4sv2.h 2020-01-30 10:15:24 +0000
215@@ -42,16 +42,6 @@
216 // stop panicking at ~50% power or ~2000 lm
217 #define THERM_FASTER_LEVEL 130
218
219-// no need to be extra-careful on this light
220-#ifdef THERM_HARD_TURBO_DROP
221-#undef THERM_HARD_TURBO_DROP
222-#endif
223-
224-// respond to thermal changes faster
225-#define THERMAL_WARNING_SECONDS 3
226-#define THERMAL_UPDATE_SPEED 2
227-#define THERM_PREDICTION_STRENGTH 4
228-
229 // easier access to thermal config mode, for Emisar
230 #define USE_TENCLICK_THERMAL_CONFIG
231
232@@ -59,9 +49,7 @@
233 #undef BLINK_AT_RAMP_MIDDLE
234 #endif
235
236+#define THERM_LOOKAHEAD 6
237+
238 // seems relevant on attiny1634
239 #define THERM_CAL_OFFSET 5
240-
241-// attiny1634 has enough space to smooth out voltage readings
242-#define USE_VOLTAGE_LOWPASS
243-
244
245=== modified file 'ToyKeeper/spaghetti-monster/anduril/cfg-emisar-d4v2-219.h'
246--- ToyKeeper/spaghetti-monster/anduril/cfg-emisar-d4v2-219.h 2019-08-05 07:40:20 +0000
247+++ ToyKeeper/spaghetti-monster/anduril/cfg-emisar-d4v2-219.h 2020-01-30 10:15:24 +0000
248@@ -6,3 +6,6 @@
249 #undef PWM2_LEVELS
250 #define PWM1_LEVELS 1,1,2,2,3,3,4,4,5,6,7,8,9,10,12,13,14,15,17,19,20,22,24,26,29,31,34,36,39,42,45,48,51,55,59,62,66,70,75,79,84,89,93,99,104,110,115,121,127,134,140,147,154,161,168,176,184,192,200,209,217,226,236,245,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
251 #define PWM2_LEVELS 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,3,5,6,6,8,9,10,11,12,14,15,16,18,18,20,21,23,24,26,27,29,30,32,33,36,37,39,41,43,44,46,48,50,52,54,56,58,61,63,65,67,70,72,74,77,79,82,84,86,89,92,95,97,100,103,106,108,111,114,117,120,124,127,130,133,137,140,144,147,151,154,157,161,165,169,172,176,180,184,188,192
252+
253+#undef THERM_LOOKAHEAD
254+#define THERM_LOOKAHEAD 4
255
256=== modified file 'ToyKeeper/spaghetti-monster/anduril/cfg-emisar-d4v2.h'
257--- ToyKeeper/spaghetti-monster/anduril/cfg-emisar-d4v2.h 2019-11-24 23:21:40 +0000
258+++ ToyKeeper/spaghetti-monster/anduril/cfg-emisar-d4v2.h 2020-01-30 10:15:24 +0000
259@@ -37,22 +37,12 @@
260 #define RAMP_DISCRETE_CEIL RAMP_SMOOTH_CEIL
261 #define RAMP_DISCRETE_STEPS 7
262
263-// optional, makes initial turbo step-down faster so first peak isn't as hot
264-// the D4 runs very very hot, so be extra careful
265-//#define THERM_HARD_TURBO_DROP
266-
267 // stop panicking at ~30% power or ~1200 lm
268 #define THERM_FASTER_LEVEL 105
269-// respond to thermal changes faster
270-#define THERMAL_WARNING_SECONDS 3
271-#define THERMAL_UPDATE_SPEED 1
272-#define THERM_PREDICTION_STRENGTH 4
273-//#define THERM_RESPONSE_MAGNITUDE 128
274
275 // easier access to thermal config mode, for Emisar
276 #define USE_TENCLICK_THERMAL_CONFIG
277
278+#define THERM_LOOKAHEAD 6
279+
280 #define THERM_CAL_OFFSET 5
281-
282-// attiny1634 has enough space to smooth out voltage readings
283-#define USE_VOLTAGE_LOWPASS
284
285=== modified file 'ToyKeeper/spaghetti-monster/anduril/cfg-ff-pl47.h'
286--- ToyKeeper/spaghetti-monster/anduril/cfg-ff-pl47.h 2019-05-22 20:42:02 +0000
287+++ ToyKeeper/spaghetti-monster/anduril/cfg-ff-pl47.h 2020-01-30 10:15:24 +0000
288@@ -61,11 +61,6 @@
289 // regulate down faster when the FET is active, slower otherwise
290 #define THERM_FASTER_LEVEL 135 // throttle back faster when high
291
292-// play it safe, don't try to regulate above the recommended safe level
293-#ifdef THERM_HARD_TURBO_DROP
294-#undef THERM_HARD_TURBO_DROP
295-#endif
296-
297 // don't do this
298 #undef BLINK_AT_RAMP_MIDDLE
299 #undef BLINK_AT_RAMP_CEILING
300
301=== modified file 'ToyKeeper/spaghetti-monster/anduril/cfg-ff-pl47g2.h'
302--- ToyKeeper/spaghetti-monster/anduril/cfg-ff-pl47g2.h 2019-12-11 07:59:20 +0000
303+++ ToyKeeper/spaghetti-monster/anduril/cfg-ff-pl47g2.h 2020-01-30 10:15:24 +0000
304@@ -49,11 +49,6 @@
305 // regulate down faster when the FET is active, slower otherwise
306 #define THERM_FASTER_LEVEL 135 // throttle back faster when high
307
308-// hard drop doesn't seem to be needed on this light
309-#ifdef THERM_HARD_TURBO_DROP
310-#undef THERM_HARD_TURBO_DROP
311-#endif
312-
313 // don't do this
314 #undef BLINK_AT_RAMP_MIDDLE
315 #undef BLINK_AT_RAMP_CEILING
316
317=== modified file 'ToyKeeper/spaghetti-monster/anduril/cfg-ff-rot66.h'
318--- ToyKeeper/spaghetti-monster/anduril/cfg-ff-rot66.h 2019-05-23 00:23:25 +0000
319+++ ToyKeeper/spaghetti-monster/anduril/cfg-ff-rot66.h 2020-01-30 10:15:24 +0000
320@@ -38,9 +38,6 @@
321 // regulate down faster when the FET is active, slower otherwise
322 #define THERM_FASTER_LEVEL 130 // throttle back faster when high
323
324-// play it safe, don't try to regulate above the recommended safe level
325-//#define THERM_HARD_TURBO_DROP
326-
327 // don't do this
328 #undef BLINK_AT_RAMP_MIDDLE
329 #undef BLINK_AT_RAMP_CEILING
330
331=== modified file 'ToyKeeper/spaghetti-monster/anduril/cfg-mateminco-mf01-mini.h'
332--- ToyKeeper/spaghetti-monster/anduril/cfg-mateminco-mf01-mini.h 2019-11-25 04:35:40 +0000
333+++ ToyKeeper/spaghetti-monster/anduril/cfg-mateminco-mf01-mini.h 2020-01-30 10:15:24 +0000
334@@ -48,8 +48,3 @@
335
336 #define USE_TENCLICK_THERMAL_CONFIG // by request
337 #define THERM_FASTER_LEVEL 130 // throttle back faster when high
338-//#define THERM_HARD_TURBO_DROP // this light is massively overpowered
339-#define THERMAL_WARNING_SECONDS 1 // FIXME: increase by 2 after merging newer code
340-//#define THERMAL_UPDATE_SPEED 1
341-//#define THERM_PREDICTION_STRENGTH 4
342-
343
344=== modified file 'ToyKeeper/spaghetti-monster/anduril/cfg-mateminco-mf01s.h'
345--- ToyKeeper/spaghetti-monster/anduril/cfg-mateminco-mf01s.h 2019-11-25 04:35:40 +0000
346+++ ToyKeeper/spaghetti-monster/anduril/cfg-mateminco-mf01s.h 2020-01-30 10:15:24 +0000
347@@ -41,5 +41,3 @@
348
349 #define USE_TENCLICK_THERMAL_CONFIG // by request
350 #define THERM_FASTER_LEVEL 125 // throttle back faster when high (>6000 lm)
351-#define THERM_HARD_TURBO_DROP // this light is massively overpowered
352-
353
354=== modified file 'ToyKeeper/spaghetti-monster/anduril/cfg-sofirn-sp36.h'
355--- ToyKeeper/spaghetti-monster/anduril/cfg-sofirn-sp36.h 2019-03-18 10:06:37 +0000
356+++ ToyKeeper/spaghetti-monster/anduril/cfg-sofirn-sp36.h 2020-01-30 10:15:24 +0000
357@@ -28,10 +28,3 @@
358 #undef THERM_FASTER_LEVEL
359 #endif
360 #define THERM_FASTER_LEVEL 130
361-
362-// be extra-careful at high levels
363-// (or not... this host seems to heat up pretty slowly)
364-//#ifndef THERM_HARD_TURBO_DROP
365-//#define THERM_HARD_TURBO_DROP
366-//#endif
367-
368
369=== modified file 'ToyKeeper/spaghetti-monster/fsm-adc.c'
370--- ToyKeeper/spaghetti-monster/fsm-adc.c 2019-12-18 01:17:58 +0000
371+++ ToyKeeper/spaghetti-monster/fsm-adc.c 2020-01-30 10:15:24 +0000
372@@ -20,6 +20,7 @@
373 #ifndef FSM_ADC_C
374 #define FSM_ADC_C
375
376+#include <stdlib.h>
377
378 static inline void set_admux_therm() {
379 #if (ATTINY == 25) || (ATTINY == 45) || (ATTINY == 85) || (ATTINY == 1634)
380@@ -30,7 +31,6 @@
381 #else
382 #error Unrecognized MCU type
383 #endif
384- adc_channel = 1;
385 }
386
387 inline void set_admux_voltage() {
388@@ -53,7 +53,6 @@
389 #else
390 #error Unrecognized MCU type
391 #endif
392- adc_channel = 0;
393 }
394
395 inline void ADC_start_measurement() {
396@@ -64,6 +63,14 @@
397 #endif
398 }
399
400+inline void ADC_stop_measurement() {
401+ #if (ATTINY == 25) || (ATTINY == 45) || (ATTINY == 85) || (ATTINY == 841) || (ATTINY == 1634)
402+ ADCSRA &= ~(1 << ADIF | 1 << ADIE);
403+ #else
404+ #error unrecognized MCU type
405+ #endif
406+}
407+
408 // set up ADC for reading battery voltage
409 inline void ADC_on()
410 {
411@@ -98,57 +105,63 @@
412 ADCSRA &= ~(1<<ADEN); //ADC off
413 }
414
415-#ifdef USE_VOLTAGE_DIVIDER
416-static inline uint8_t calc_voltage_divider(uint16_t value) {
417- // use 9.7 fixed-point to get sufficient precision
418- uint16_t adc_per_volt = ((ADC_44<<7) - (ADC_22<<7)) / (44-22);
419- // incoming value is 8.2 fixed-point, so shift it 2 bits less
420- uint8_t result = ((value<<5) / adc_per_volt) + VOLTAGE_FUDGE_FACTOR;
421- return result;
422-}
423-#endif
424-
425-// Each full cycle runs 15.6X per second with just voltage enabled,
426-// or 7.8X per second with voltage and temperature.
427-#if defined(USE_LVP) && defined(USE_THERMAL_REGULATION)
428-#define ADC_CYCLES_PER_SECOND 8
429-#else
430-#define ADC_CYCLES_PER_SECOND 16
431-#endif
432-
433-#ifdef USE_THERMAL_REGULATION
434-#define ADC_STEPS 2
435-#else
436-#define ADC_STEPS 1
437-#endif
438-
439 // happens every time the ADC sampler finishes a measurement
440 ISR(ADC_vect) {
441- #ifdef USE_PSEUDO_RAND
442- // real-world entropy makes this a true random, not pseudo
443- pseudo_rand_seed += ADCL;
444- #endif
445-
446- if (irq_adc_stable) { // skip first result; it's junk
447- adc_values[adc_channel] = ADC; // save this for later use
448- irq_adc = 1; // a value was saved, so trigger deferred logic
449- }
450- irq_adc_stable = 1;
451-
452- // start another measurement
453- // (is explicit because it otherwise doesn't seem to happen during standby mode)
454- ADC_start_measurement();
455+ #define ADC_PLUS_BITS 8 // oversampling strength
456+ static uint8_t adc_stable = 0;
457+ static uint16_t adc_sum_counter = 0;
458+ static uint32_t adc_sum = 0;
459+
460+ ADC_start_measurement(); // mandatory in standby mode
461+
462+ if (!adc_stable) {
463+ adc_stable = 1;
464+ #ifdef USE_PSEUDO_RAND
465+ // real-world entropy makes this a true random, not pseudo
466+ pseudo_rand_seed += ADCL;
467+ #endif
468+ return;
469+ }
470+
471+ if (adc_sum_counter < 10 * (1 << ADC_PLUS_BITS)) {
472+ adc_sum += ADC; // latest 10-bit ADC reading
473+ adc_sum_counter++;
474+ if (!go_to_standby) return;
475+ }
476+ adc_10x = go_to_standby ? ADC * 10 : adc_sum >> ADC_PLUS_BITS;
477+ adc_sum_counter = adc_sum = 0;
478+
479+ adc_stable = 0; // set the dirty flag for the next sequence
480+ irq_adc = 1; // enough samples accumulated, trigger deferred logic
481+
482+ ADC_stop_measurement(); // abort free running mode
483 }
484
485 void ADC_inner() {
486 irq_adc = 0; // event handled
487
488- // the ADC triggers repeatedly when it's on, but we only want one value
489- // (so ignore everything after the first value, until it's manually reset)
490- if (! adcint_enable) return;
491+ // what is being measured? 0 = battery voltage, 1 = temperature
492+ static uint8_t adc_type = 0;
493
494- // disable after one iteration
495- adcint_enable = 0;
496+ if (!adc_type) { // voltage
497+ #if defined(USE_LVP) || defined(USE_SLEEP_LVP) || defined(USE_BATTCHECK)
498+ ADC_voltage_handler();
499+ #endif
500+ #ifdef USE_THERMAL_REGULATION
501+ if (!go_to_standby) { // only measure battery voltage while asleep
502+ set_admux_therm();
503+ adc_type = 1;
504+ }
505+ #endif
506+ } else { // temperature
507+ #ifdef USE_THERMAL_REGULATION
508+ ADC_temperature_handler();
509+ #if defined(USE_LVP) || defined(USE_SLEEP_LVP) || defined(USE_BATTCHECK)
510+ set_admux_voltage();
511+ #endif
512+ adc_type = 0; // always skip one cycle, even if we don't measure battery voltage
513+ #endif
514+ }
515
516 #ifdef TICK_DURING_STANDBY
517 // in sleep mode, turn off after just one measurement
518@@ -156,74 +169,17 @@
519 // (and the usual standby level is only ~20 uA)
520 if (go_to_standby) ADC_off();
521 #endif
522-
523- // what is being measured? 0 = battery voltage, 1 = temperature
524- static uint8_t adc_step = 0;
525-
526- #ifdef USE_LVP
527- if (0 == adc_step) { // voltage
528- ADC_voltage_handler();
529- }
530- #endif
531-
532- #ifdef USE_THERMAL_REGULATION
533- else if (1 == adc_step) { // temperature
534- ADC_temperature_handler();
535- }
536- #endif
537-
538- #if defined(TICK_DURING_STANDBY) && defined(USE_SLEEP_LVP)
539- // only measure battery voltage while asleep
540- if (go_to_standby) adc_step = 0;
541- else
542- #endif
543-
544- adc_step = (adc_step + 1) & (ADC_STEPS-1);
545-
546- // set the correct type of measurement for next time
547- #ifdef USE_THERMAL_REGULATION
548- #ifdef USE_LVP
549- if (0 == adc_step) set_admux_voltage();
550- else set_admux_therm();
551- #else
552- //set_admux_therm();
553- #error "USE_THERMAL_REGULATION set without USE_LVP"
554- #endif
555- #else
556- #ifdef USE_LVP
557- set_admux_voltage();
558- #endif
559- #endif
560-
561- irq_adc_stable = 0; // first result is unstable
562 }
563
564-
565-#ifdef USE_LVP
566+#if defined(USE_LVP) || defined(USE_SLEEP_LVP) || defined(USE_BATTCHECK)
567+// Runs once per second (unless in standby)
568 static inline void ADC_voltage_handler() {
569- static uint8_t lvp_timer = 0;
570- static uint8_t lvp_lowpass = 0;
571- #define LVP_TIMER_START (VOLTAGE_WARNING_SECONDS*ADC_CYCLES_PER_SECOND) // N seconds between LVP warnings
572- #define LVP_LOWPASS_STRENGTH ADC_CYCLES_PER_SECOND // lowpass for one second
573-
574- uint16_t measurement = adc_values[0]; // latest 10-bit ADC reading
575-
576- #ifdef USE_VOLTAGE_LOWPASS
577- static uint16_t prev_measurement = 0;
578-
579- // prime on first execution, or while asleep
580- if (go_to_standby || (! prev_measurement)) prev_measurement = measurement;
581-
582- // only allow raw value to go up or down by 1 per iteration
583- if (measurement > prev_measurement) measurement = prev_measurement + 1;
584- else if (measurement < prev_measurement) measurement = prev_measurement - 1;
585-
586- // remember for later
587- prev_measurement = measurement;
588- #endif // no USE_VOLTAGE_LOWPASS
589-
590+ uint16_t measurement = (adc_10x + 5) / 10;
591 #ifdef USE_VOLTAGE_DIVIDER
592- voltage = calc_voltage_divider(measurement);
593+ // use 9.7 fixed-point to get sufficient precision
594+ uint16_t adc_per_volt = ((ADC_44<<7) - (ADC_22<<7)) / (44-22);
595+ // incoming value is 8.2 fixed-point, so shift it 2 bits less
596+ voltage = ((measurement<<5) / adc_per_volt) + VOLTAGE_FUDGE_FACTOR;
597 #else
598 // calculate actual voltage: volts * 10
599 // ADC = 1.1 * 1024 / volts
600@@ -231,178 +187,70 @@
601 //voltage = (uint16_t)(1.1*1024*10)/measurement + VOLTAGE_FUDGE_FACTOR;
602 voltage = ((uint16_t)(2*1.1*1024*10)/measurement + VOLTAGE_FUDGE_FACTOR) >> 1;
603 #endif
604-
605- // if low, callback EV_voltage_low / EV_voltage_critical
606- // (but only if it has been more than N ticks since last call)
607- if (lvp_timer) {
608- lvp_timer --;
609- } else { // it has been long enough since the last warning
610- if (voltage < VOLTAGE_LOW) {
611- if (lvp_lowpass < LVP_LOWPASS_STRENGTH) {
612- lvp_lowpass ++;
613- } else {
614- // try to send out a warning
615- //uint8_t err = emit(EV_voltage_low, 0);
616- //uint8_t err = emit_now(EV_voltage_low, 0);
617- emit(EV_voltage_low, 0);
618- //if (!err) {
619- // on successful warning, reset counters
620- lvp_timer = LVP_TIMER_START;
621- lvp_lowpass = 0;
622- //}
623- }
624- } else {
625- // voltage not low? reset count
626- lvp_lowpass = 0;
627- }
628+ #if defined(USE_LVP) || defined(USE_SLEEP_LVP)
629+ if (voltage < VOLTAGE_LOW) {
630+ emit(EV_voltage_low, 0);
631 }
632+ #endif // ifdef USE_LVP
633 }
634 #endif
635
636
637 #ifdef USE_THERMAL_REGULATION
638+// Runs once per second (unless in standby)
639 static inline void ADC_temperature_handler() {
640- // thermal declarations
641- #ifndef THERMAL_UPDATE_SPEED
642- #define THERMAL_UPDATE_SPEED 2
643+ #define NUM_HISTORY_TEMPS 4 // hardcoded, not intended to be changed
644+ #ifndef THERM_LOOKAHEAD
645+ #define THERM_LOOKAHEAD 5 // might be lowered with lower power to mass ratios
646 #endif
647- #define NUM_THERMAL_VALUES_HISTORY 8
648- static uint8_t history_step = 0; // don't update history as often
649- static int16_t temperature_history[NUM_THERMAL_VALUES_HISTORY];
650- static uint8_t temperature_timer = 0;
651- static uint8_t overheat_lowpass = 0;
652- static uint8_t underheat_lowpass = 0;
653- #define TEMPERATURE_TIMER_START ((THERMAL_WARNING_SECONDS-2)*ADC_CYCLES_PER_SECOND) // N seconds between thermal regulation events
654- #define OVERHEAT_LOWPASS_STRENGTH (ADC_CYCLES_PER_SECOND*2) // lowpass for 2 seconds
655- #define UNDERHEAT_LOWPASS_STRENGTH (ADC_CYCLES_PER_SECOND*2) // lowpass for 2 seconds
656-
657- // TODO: left-shift this so the lowpass can get higher resolution
658- // TODO: increase the sampling rate, to keep the lowpass from lagging
659- uint16_t measurement = adc_values[1]; // latest 10-bit ADC reading
660-
661- // Convert ADC units to Celsius (ish)
662- int16_t temp = measurement - 275 + THERM_CAL_OFFSET + (int16_t)therm_cal_offset;
663+ const int16_t temp_target = therm_ceil * 10 - 5;
664+ static int16_t temp_history[NUM_HISTORY_TEMPS];
665+ static uint8_t temp_history_index = 0;
666+ static uint8_t temp_record_offset = 0;
667+ static int16_t temp_offset_100x = 0;
668+
669+ int16_t temp_10x = adc_10x + 10 * (THERM_CAL_OFFSET + therm_cal_offset - 275);
670+ temperature = (temp_10x + 5) / 10; // save temperature C (ish) for later use in the ui
671
672 // prime on first execution
673 if (reset_thermal_history) {
674 reset_thermal_history = 0;
675- temperature = temp;
676- for(uint8_t i=0; i<NUM_THERMAL_VALUES_HISTORY; i++)
677- temperature_history[i] = temp;
678- } else { // update our current temperature estimate
679- // crude lowpass filter
680- // (limit rate of change to 1 degree per measurement)
681- if (temp > temperature) {
682- temperature ++;
683- } else if (temp < temperature) {
684- temperature --;
685- }
686- }
687-
688- // guess what the temperature will be in a few seconds
689- int16_t pt;
690- {
691- int16_t diff;
692- int16_t t = temperature;
693-
694- // algorithm tweaking; not really intended to be modified
695- // how far ahead should we predict?
696- #ifndef THERM_PREDICTION_STRENGTH
697- #define THERM_PREDICTION_STRENGTH 4
698- #endif
699- // how proportional should the adjustments be? (not used yet)
700- #ifndef THERM_RESPONSE_MAGNITUDE
701- #define THERM_RESPONSE_MAGNITUDE 128
702- #endif
703- // acceptable temperature window size in C
704- #define THERM_WINDOW_SIZE 5
705- // highest temperature allowed
706- #define THERM_CEIL ((int16_t)therm_ceil)
707- // bottom of target temperature window
708- #define THERM_FLOOR (THERM_CEIL - THERM_WINDOW_SIZE)
709-
710- // if it's time to rotate the thermal history, do it
711- history_step ++;
712- #if (THERMAL_UPDATE_SPEED == 4) // new value every 4s
713- #define THERM_HISTORY_STEP_MAX (4*ADC_CYCLES_PER_SECOND)
714- #elif (THERMAL_UPDATE_SPEED == 2) // new value every 2s
715- #define THERM_HISTORY_STEP_MAX (2*ADC_CYCLES_PER_SECOND)
716- #elif (THERMAL_UPDATE_SPEED == 1) // new value every 1s
717- #define THERM_HISTORY_STEP_MAX (ADC_CYCLES_PER_SECOND)
718- #elif (THERMAL_UPDATE_SPEED == 0) // new value every 0.5s
719- #define THERM_HISTORY_STEP_MAX (ADC_CYCLES_PER_SECOND/2)
720- #endif
721- if (THERM_HISTORY_STEP_MAX == history_step) {
722- history_step = 0;
723- // rotate measurements and add a new one
724- for (uint8_t i=0; i<NUM_THERMAL_VALUES_HISTORY-1; i++) {
725- temperature_history[i] = temperature_history[i+1];
726- }
727- temperature_history[NUM_THERMAL_VALUES_HISTORY-1] = t;
728- }
729-
730- // guess what the temp will be several seconds in the future
731- // diff = rate of temperature change
732- //diff = temperature_history[NUM_THERMAL_VALUES_HISTORY-1] - temperature_history[0];
733- diff = t - temperature_history[0];
734- // slight bias toward zero; ignore very small changes (noise)
735- for (uint8_t z=0; z<3; z++) {
736- if (diff < 0) diff ++;
737- if (diff > 0) diff --;
738- }
739- // projected_temperature = current temp extended forward by amplified rate of change
740- //projected_temperature = temperature_history[NUM_THERMAL_VALUES_HISTORY-1] + (diff<<THERM_PREDICTION_STRENGTH);
741- pt = projected_temperature = t + (diff<<THERM_PREDICTION_STRENGTH);
742- }
743-
744- // cancel counters if appropriate
745- if (pt > THERM_FLOOR) {
746- underheat_lowpass = 0; // we're probably not too cold
747- }
748- if (pt < THERM_CEIL) {
749- overheat_lowpass = 0; // we're probably not too hot
750- }
751-
752- if (temperature_timer) {
753- temperature_timer --;
754- } else { // it has been long enough since the last warning
755-
756- // Too hot?
757- if (pt > THERM_CEIL) {
758- if (overheat_lowpass < OVERHEAT_LOWPASS_STRENGTH) {
759- overheat_lowpass ++;
760- } else {
761- // reset counters
762- overheat_lowpass = 0;
763- temperature_timer = TEMPERATURE_TIMER_START;
764- // how far above the ceiling?
765- //int16_t howmuch = (pt - THERM_CEIL) * THERM_RESPONSE_MAGNITUDE / 128;
766- int16_t howmuch = pt - THERM_CEIL;
767- // try to send out a warning
768- emit(EV_temperature_high, howmuch);
769- }
770- }
771-
772- // Too cold?
773- else if (pt < THERM_FLOOR) {
774- if (underheat_lowpass < UNDERHEAT_LOWPASS_STRENGTH) {
775- underheat_lowpass ++;
776- } else {
777- // reset counters
778- underheat_lowpass = 0;
779- temperature_timer = TEMPERATURE_TIMER_START;
780- // how far below the floor?
781- //int16_t howmuch = (THERM_FLOOR - pt) * THERM_RESPONSE_MAGNITUDE / 128;
782- int16_t howmuch = THERM_FLOOR - pt;
783- // try to send out a warning (unless voltage is low)
784- // (LVP and underheat warnings fight each other)
785- if (voltage > VOLTAGE_LOW)
786- emit(EV_temperature_low, howmuch);
787- }
788- }
789+ temp_record_offset = 0;
790+ temp_offset_100x = 0;
791+ for (uint8_t i=0; i<NUM_HISTORY_TEMPS; i++)
792+ temp_history[i] = temp_10x;
793+ }
794+
795+ int16_t temp_slope = temp_10x - temp_history[temp_history_index];
796+ temp_history[temp_history_index] = temp_10x;
797+ temp_history_index = (temp_history_index + 1) % NUM_HISTORY_TEMPS;
798+
799+ int16_t predicted_temp = temp_10x + temp_slope * THERM_LOOKAHEAD;
800+ int16_t offset = predicted_temp - temp_target + temp_offset_100x / 10;
801+
802+ if (offset > 5 && temp_slope >= 0) {
803+ temp_record_offset = 5;
804+ int16_t howmuch = (offset / 6) + (temp_slope >> 1);
805+ emit(EV_temperature_high, howmuch);
806+ } else if (temp_record_offset) {
807+ temp_record_offset--;
808+ int16_t to_100x = 4 * (temp_target - temp_10x);
809+ if (!temp_record_offset && to_100x > temp_offset_100x) {
810+ temp_offset_100x = to_100x;
811+ }
812+ } else if ((offset < -15 && temp_slope < -15) || (temp_slope <= 0 && offset < -50)) {
813+ #ifdef USE_LVP
814+ if (voltage > VOLTAGE_LOW)
815+ #endif
816+ {
817+ int16_t howmuch = (-offset / 12) + (-temp_slope >> 1);
818+ emit(EV_temperature_low, howmuch);
819+ }
820+ } else if (temp_offset_100x > 0) {
821+ temp_offset_100x--;
822 }
823 }
824-#endif
825+#endif // ifdef USE_THERMAL_REGULATION
826
827
828 #ifdef USE_BATTCHECK
829
830=== modified file 'ToyKeeper/spaghetti-monster/fsm-adc.h'
831--- ToyKeeper/spaghetti-monster/fsm-adc.h 2019-11-15 01:00:54 +0000
832+++ ToyKeeper/spaghetti-monster/fsm-adc.h 2020-01-30 10:15:24 +0000
833@@ -21,11 +21,17 @@
834 #define FSM_ADC_H
835
836
837-#ifdef USE_LVP
838-// default 5 seconds between low-voltage warning events
839-#ifndef VOLTAGE_WARNING_SECONDS
840-#define VOLTAGE_WARNING_SECONDS 5
841-#endif
842+volatile uint16_t adc_10x = 0; // oversampled ADC result
843+volatile uint8_t irq_adc = 0; // new ADC result available?
844+void ADC_inner(); // do the actual ADC-related calculations
845+
846+inline void ADC_on();
847+inline void ADC_off();
848+inline void ADC_start_measurement();
849+inline void ADC_stop_measurement();
850+
851+
852+#if defined(USE_LVP) || defined(USE_BATTCHECK) || defined(USE_AUX_RGB_LEDS)
853 // low-battery threshold in volts * 10
854 #ifndef VOLTAGE_LOW
855 #define VOLTAGE_LOW 29
856@@ -38,18 +44,11 @@
857 #define VOLTAGE_FUDGE_FACTOR 5
858 #endif
859 #endif
860-
861-volatile uint8_t irq_adc = 0; // ADC interrupt happened?
862-volatile uint8_t irq_adc_stable = 0; // have we passed the 1st junk value yet?
863-uint8_t adc_channel = 0; // 0=voltage, 1=temperature
864-uint16_t adc_values[2]; // last ADC measurements (0=voltage, 1=temperature)
865-uint8_t adcint_enable = 0; // is the current ADC result needed?
866-void ADC_inner(); // do the actual ADC-related calculations
867-
868 static inline void ADC_voltage_handler();
869-volatile uint8_t voltage = 0;
870+uint8_t voltage = 0;
871+#ifdef USE_LVP
872 void low_voltage();
873-
874+#endif
875 #ifdef USE_BATTCHECK
876 void battcheck();
877 #ifdef BATTCHECK_VpT
878@@ -63,9 +62,8 @@
879
880
881 #ifdef USE_THERMAL_REGULATION
882-// default 5 seconds between thermal regulation events
883-#ifndef THERMAL_WARNING_SECONDS
884-#define THERMAL_WARNING_SECONDS 5
885+#ifndef USE_BLINK_NUM
886+#define USE_BLINK_NUM
887 #endif
888 // try to keep temperature below 45 C
889 #ifndef DEFAULT_THERM_CEIL
890@@ -79,22 +77,12 @@
891 #ifndef THERM_CAL_OFFSET
892 #define THERM_CAL_OFFSET 0
893 #endif
894-// temperature now, in C (ish) * 2 (14.1 fixed-point)
895-volatile int16_t temperature;
896-// temperature in a few seconds, in C (ish) * 2 (14.1 fixed-point)
897-volatile int16_t projected_temperature; // Fight the future!
898+int8_t temperature = 0; // C (ish)
899 uint8_t therm_ceil = DEFAULT_THERM_CEIL;
900 int8_t therm_cal_offset = 0;
901-//void low_temperature();
902-//void high_temperature();
903-volatile uint8_t reset_thermal_history = 1;
904+uint8_t reset_thermal_history = 1;
905 static inline void ADC_temperature_handler();
906 #endif // ifdef USE_THERMAL_REGULATION
907
908
909-inline void ADC_on();
910-inline void ADC_off();
911-inline void ADC_start_measurement();
912-
913-
914 #endif
915
916=== modified file 'ToyKeeper/spaghetti-monster/fsm-misc.c'
917--- ToyKeeper/spaghetti-monster/fsm-misc.c 2019-10-15 08:34:47 +0000
918+++ ToyKeeper/spaghetti-monster/fsm-misc.c 2020-01-30 10:15:24 +0000
919@@ -40,7 +40,7 @@
920 }
921 #endif
922
923-#if defined(USE_BLINK_NUM) || defined(USE_BLINK_DIGIT)
924+#if defined(USE_BLINK_NUM) || defined(USE_BLINK_DIGIT) || defined(USE_VERSION_CHECK)
925 #define BLINK_SPEED 1000
926 uint8_t blink_digit(uint8_t num) {
927 //StatePtr old_state = current_state;
928
929=== modified file 'ToyKeeper/spaghetti-monster/fsm-misc.h'
930--- ToyKeeper/spaghetti-monster/fsm-misc.h 2019-09-06 10:38:00 +0000
931+++ ToyKeeper/spaghetti-monster/fsm-misc.h 2020-01-30 10:15:24 +0000
932@@ -24,7 +24,7 @@
933 void auto_clock_speed();
934 #endif
935
936-#if defined(USE_BLINK_NUM) || defined(USE_BLINK_DIGIT)
937+#if defined(USE_BLINK_NUM) || defined(USE_BLINK_DIGIT) || defined(USE_VERSION_CHECK)
938 #ifndef BLINK_BRIGHTNESS
939 #define BLINK_BRIGHTNESS (MAX_LEVEL/6)
940 #endif
941
942=== modified file 'ToyKeeper/spaghetti-monster/fsm-standby.c'
943--- ToyKeeper/spaghetti-monster/fsm-standby.c 2019-11-20 22:29:51 +0000
944+++ ToyKeeper/spaghetti-monster/fsm-standby.c 2020-01-30 10:15:24 +0000
945@@ -73,7 +73,6 @@
946 go_to_standby = 0;
947 }
948 if (irq_adc) { // ADC done measuring
949- adcint_enable = 1;
950 ADC_inner();
951 //ADC_off(); // takes care of itself
952 //irq_adc = 0; // takes care of itself
953
954=== modified file 'ToyKeeper/spaghetti-monster/fsm-wdt.c'
955--- ToyKeeper/spaghetti-monster/fsm-wdt.c 2019-11-19 07:06:41 +0000
956+++ ToyKeeper/spaghetti-monster/fsm-wdt.c 2020-01-30 10:15:24 +0000
957@@ -86,10 +86,12 @@
958 }
959
960 void WDT_inner() {
961+ #if defined(USE_LVP) || defined(USE_SLEEP_LVP) || defined(USE_THERMAL_REGULATION)
962+ static uint8_t adc_trigger = 0;
963+ #endif
964+
965 irq_wdt = 0; // WDT event handled; reset flag
966
967- static uint8_t adc_trigger = 0;
968-
969 // cache this here to reduce ROM size, because it's volatile
970 uint16_t ticks_since_last = ticks_since_last_event;
971 // increment, but loop from max back to half
972@@ -119,9 +121,9 @@
973 return; // no sleep LVP needed if nothing drains power while off
974 #else
975 // stop here, usually... but proceed often enough for sleep LVP to work
976- if (0 != (ticks_since_last & 0x7f)) return;
977+ if (0 != (ticks_since_last & 0x1ff) && voltage) return;
978
979- adc_trigger = 255; // make sure a measurement will happen
980+ adc_trigger = 0; // make sure a measurement will happen
981 ADC_on(); // enable ADC voltage measurement functions temporarily
982 #endif
983 }
984@@ -177,14 +179,11 @@
985 }
986 #endif
987
988- #if defined(USE_LVP) || defined(USE_THERMAL_REGULATION)
989- // start a new ADC measurement every 4 ticks
990- adc_trigger ++;
991- if (0 == (adc_trigger & 3)) {
992+ #if defined(USE_LVP) || defined(USE_SLEEP_LVP) || defined(USE_THERMAL_REGULATION)
993+ if (!adc_trigger) {
994 ADC_start_measurement();
995- irq_adc_stable = 0;
996- adcint_enable = 1;
997 }
998+ adc_trigger = (adc_trigger + 1) % (TICKS_PER_SECOND / 2);
999 #endif
1000 }
1001
1002
1003=== modified file 'ToyKeeper/spaghetti-monster/spaghetti-monster.txt'
1004--- ToyKeeper/spaghetti-monster/spaghetti-monster.txt 2018-11-11 23:01:40 +0000
1005+++ ToyKeeper/spaghetti-monster/spaghetti-monster.txt 2020-01-30 10:15:24 +0000
1006@@ -120,17 +120,16 @@
1007 LVP and thermal regulation:
1008
1009 - EV_voltage_low: Sent whenever the input power drops below the
1010- VOLTAGE_LOW threshold. Minimum of VOLTAGE_WARNING_SECONDS
1011- between events.
1012+ VOLTAGE_LOW threshold. Minimum of one second between events.
1013
1014 - EV_temperature_high: Sent whenever the MCU's projected temperature
1015- is higher than therm_ceil. Minimum of THERMAL_WARNING_SECONDS
1016+ is higher than therm_ceil. Minimum of one second
1017 between events. The 'arg' indicates how far the temperature
1018 exceeds the limit.
1019
1020 - EV_temperature_low: Sent whenever the MCU's projected temperature
1021 is lower than (therm_ceil - THERMAL_WINDOW_SIZE). Minimum of
1022- THERMAL_WARNING_SECONDS between events. The 'arg' indicates how
1023+ one second between events. The 'arg' indicates how
1024 far the temperature exceeds the limit.
1025
1026 Button presses:
1027@@ -290,16 +289,11 @@
1028 to compensate for voltage drop across the reverse-polarity
1029 diode.
1030
1031- - VOLTAGE_WARNING_SECONDS: How long to wait between LVP events.
1032-
1033 - USE_THERMAL_REGULATION: Enable thermal regulation
1034
1035 - DEFAULT_THERM_CEIL: Set the temperature limit to use by default
1036 when the user hasn't configured anything.
1037
1038- - THERMAL_WARNING_SECONDS: How long to wait between temperature
1039- events.
1040-
1041 - USE_RAMPING: Enable smooth ramping helpers.
1042
1043 - RAMP_LENGTH: Pick a pre-defined ramp by length. Defined sizes

Subscribers

People subscribed via source and target branches