~toykeeper/flashlight-firmware/fsm

« back to all changes in this revision

Viewing changes to ToyKeeper/spaghetti-monster/anduril/tactical-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
// tactical-mode.c: Tactical (ish) mode for Anduril.
 
2
// Copyright (C) 2023 Selene ToyKeeper
 
3
// SPDX-License-Identifier: GPL-3.0-or-later
 
4
 
 
5
#pragma once
 
6
 
 
7
#include "tactical-mode.h"
 
8
 
 
9
 
 
10
uint8_t tactical_state(Event event, uint16_t arg) {
 
11
    // momentary(ish) tactical mode
 
12
    uint8_t mem_lvl = memorized_level;  // save this to restore it later
 
13
    uint8_t ret = EVENT_NOT_HANDLED;
 
14
 
 
15
    // button is being held
 
16
    if ((event & (B_CLICK | B_PRESS)) == (B_CLICK | B_PRESS)) {
 
17
        // 1H: 1st level
 
18
        // 2H: 2nd level
 
19
        // 3H: 3rd level
 
20
        // 4+: nothing
 
21
        momentary_active = 0;
 
22
        ret = EVENT_HANDLED;
 
23
        uint8_t click = event & 0x0f; // click number
 
24
        if (click <= 3) {
 
25
            momentary_active = 1;
 
26
            uint8_t lvl;
 
27
            lvl = cfg.tactical_levels[click-1];
 
28
            if ((1 <= lvl) && (lvl <= RAMP_SIZE)) {  // steady output
 
29
                memorized_level = lvl;
 
30
                momentary_mode = 0;
 
31
                #if NUM_CHANNEL_MODES > 1
 
32
                    // use ramp mode's channel
 
33
                    channel_mode = cfg.channel_mode;
 
34
                #endif
 
35
            } else {  // momentary strobe mode
 
36
                momentary_mode = 1;
 
37
                if (lvl > RAMP_SIZE) {
 
38
                    current_strobe_type = (lvl - RAMP_SIZE - 1) % strobe_mode_END;
 
39
                }
 
40
            }
 
41
        }
 
42
    }
 
43
    // button was released
 
44
    else if ((event & (B_CLICK | B_PRESS)) == (B_CLICK)) {
 
45
        momentary_active = 0;
 
46
        set_level(0);
 
47
        interrupt_nice_delays();  // stop animations in progress
 
48
    }
 
49
 
 
50
    // delegate to momentary mode while button is pressed
 
51
    if (momentary_active) {
 
52
        momentary_state(event, arg);
 
53
    }
 
54
 
 
55
    memorized_level = mem_lvl;  // restore temporarily overridden mem level
 
56
 
 
57
    // copy lockout mode's aux LED and sleep behaviors
 
58
    if (event == EV_enter_state) {
 
59
        lockout_state(event, arg);
 
60
    }
 
61
    else if (event == EV_tick) {
 
62
        if (! momentary_active) {
 
63
            return lockout_state(event, arg);
 
64
        }
 
65
        return EVENT_HANDLED;
 
66
    }
 
67
    else if (event == EV_sleep_tick) {
 
68
        return lockout_state(event, arg);
 
69
    }
 
70
 
 
71
    // 6 clicks: exit and turn off
 
72
    else if (event == EV_6clicks) {
 
73
        blink_once();
 
74
        set_state(off_state, 0);
 
75
        return EVENT_HANDLED;
 
76
    }
 
77
 
 
78
    ////////// Every action below here is blocked in the simple UI //////////
 
79
    // (unnecessary since this entire mode is blocked in simple UI)
 
80
    /*
 
81
    #ifdef USE_SIMPLE_UI
 
82
    if (cfg.simple_ui_active) {
 
83
        return EVENT_NOT_HANDLED;
 
84
    }
 
85
    #endif
 
86
    */
 
87
 
 
88
    // 7H: configure tactical mode
 
89
    else if (event == EV_click7_hold) {
 
90
        push_state(tactical_config_state, 0);
 
91
        return EVENT_HANDLED;
 
92
    }
 
93
 
 
94
    return ret;
 
95
}
 
96
 
 
97
void tactical_config_save(uint8_t step, uint8_t value) {
 
98
    // update tac mode values
 
99
    // 3 values
 
100
    // each value is 1 to 150, or other:
 
101
    // - 1..150 is a ramp level
 
102
    // - other means "strobe mode"
 
103
    cfg.tactical_levels[step - 1] = value;
 
104
}
 
105
 
 
106
uint8_t tactical_config_state(Event event, uint16_t arg) {
 
107
    return config_state_base(event, arg, 3, tactical_config_save);
 
108
}
 
109