~toykeeper/flashlight-firmware/fsm

« back to all changes in this revision

Viewing changes to ToyKeeper/spaghetti-monster/anduril/momentary-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
// momentary-mode.c: Momentary 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 "momentary-mode.h"
 
8
 
 
9
uint8_t momentary_state(Event event, uint16_t arg) {
 
10
    // init strobe mode, if relevant
 
11
    #ifdef USE_STROBE_STATE
 
12
    if ((event == EV_enter_state) && (momentary_mode != 0)) {
 
13
        strobe_state(event, arg);
 
14
    }
 
15
    #endif
 
16
 
 
17
    // light up when the button is pressed; go dark otherwise
 
18
    // button is being held
 
19
    if ((event & (B_CLICK | B_PRESS)) == (B_CLICK | B_PRESS)) {
 
20
        momentary_active = 1;
 
21
        // 0 = ramping, 1 = strobes
 
22
        if (momentary_mode == 0) {
 
23
            set_level(memorized_level);
 
24
        }
 
25
        return EVENT_HANDLED;
 
26
    }
 
27
    // button was released
 
28
    else if ((event & (B_CLICK | B_PRESS)) == (B_CLICK)) {
 
29
        momentary_active = 0;
 
30
        set_level(0);
 
31
        //go_to_standby = 1;  // sleep while light is off
 
32
        return EVENT_HANDLED;
 
33
    }
 
34
 
 
35
    // Sleep, dammit!  (but wait a few seconds first)
 
36
    // (because standby mode uses such little power that it can interfere
 
37
    //  with exiting via tailcap loosen+tighten unless you leave power
 
38
    //  disconnected for several seconds, so we want to be awake when that
 
39
    //  happens to speed up the process)
 
40
    else if (event == EV_tick) {
 
41
        #ifdef USE_STROBE_STATE
 
42
        if (momentary_active) {
 
43
            // 0 = ramping, non-zero = strobes
 
44
            if (momentary_mode != 0) {
 
45
                return strobe_state(event, arg);
 
46
            }
 
47
        }
 
48
        else {
 
49
        #endif
 
50
            if (arg > TICKS_PER_SECOND*5) {  // sleep after 5 seconds
 
51
                go_to_standby = 1;  // sleep while light is off
 
52
                // turn off lighted button
 
53
                #ifdef USE_INDICATOR_LED
 
54
                indicator_led(0);
 
55
                #elif defined(USE_AUX_RGB_LEDS)
 
56
                rgb_led_update(0, 0);
 
57
                #endif
 
58
            }
 
59
        #ifdef USE_STROBE_STATE
 
60
        }
 
61
        #endif
 
62
        return EVENT_HANDLED;
 
63
    }
 
64
 
 
65
    return EVENT_NOT_HANDLED;
 
66
}
 
67