~toykeeper/flashlight-firmware/fsm

« back to all changes in this revision

Viewing changes to ToyKeeper/spaghetti-monster/anduril/tempcheck-mode.c

  • Committer: Selene ToyKeeper
  • Date: 2023-11-04 15:09:10 UTC
  • mfrom: (483.1.175 anduril2)
  • Revision ID: bzr@toykeeper.net-20231104150910-ddd3afw4nhfvof2l
merged anduril2 branch -> fsm, with *years* of changes
(this also means this code is now Anduril 2 instead of Anduril 1)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// tempcheck-mode.c: Temperature check mode for Anduril.
 
2
// Copyright (C) 2017-2023 Selene ToyKeeper
 
3
// SPDX-License-Identifier: GPL-3.0-or-later
 
4
 
 
5
#pragma once
 
6
 
 
7
#include "tempcheck-mode.h"
 
8
 
 
9
uint8_t tempcheck_state(Event event, uint16_t arg) {
 
10
    // 1 click: off
 
11
    if (event == EV_1click) {
 
12
        set_state(off_state, 0);
 
13
        return EVENT_HANDLED;
 
14
    }
 
15
    // 2 clicks: next blinky mode
 
16
    else if (event == EV_2clicks) {
 
17
        #if defined(USE_BEACON_MODE)
 
18
        set_state(beacon_state, 0);
 
19
        #elif defined(USE_SOS_MODE) && defined(USE_SOS_MODE_IN_BLINKY_GROUP)
 
20
        set_state(sos_state, 0);
 
21
        #elif defined(USE_BATTCHECK)
 
22
        set_state(battcheck_state, 0);
 
23
        #endif
 
24
        return EVENT_HANDLED;
 
25
    }
 
26
    // 7H: thermal config mode
 
27
    else if (event == EV_click7_hold) {
 
28
        push_state(thermal_config_state, 0);
 
29
        return EVENT_HANDLED;
 
30
    }
 
31
    return EVENT_NOT_HANDLED;
 
32
}
 
33
 
 
34
void thermal_config_save(uint8_t step, uint8_t value) {
 
35
    if (value) {
 
36
        // item 1: calibrate room temperature
 
37
        if (step == 1) {
 
38
            int8_t rawtemp = temperature - cfg.therm_cal_offset;
 
39
            cfg.therm_cal_offset = value - rawtemp;
 
40
            adc_reset = 2;  // invalidate all recent temperature data
 
41
        }
 
42
 
 
43
        // item 2: set maximum heat limit
 
44
        else {
 
45
            cfg.therm_ceil = 30 + value - 1;
 
46
        }
 
47
    }
 
48
 
 
49
    if (cfg.therm_ceil > MAX_THERM_CEIL) cfg.therm_ceil = MAX_THERM_CEIL;
 
50
}
 
51
 
 
52
uint8_t thermal_config_state(Event event, uint16_t arg) {
 
53
    return config_state_base(event, arg,
 
54
                             2, thermal_config_save);
 
55
}
 
56