~toykeeper/flashlight-firmware/fsm

« back to all changes in this revision

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

  • Committer: Selene ToyKeeper
  • Date: 2023-11-02 17:05:02 UTC
  • mfrom: (483.12.159 multi-channel)
  • mto: This revision was merged to the branch mainline in revision 491.
  • Revision ID: bzr@toykeeper.net-20231102170502-sinkm18qjxlorsxa
merged multi-channel branch with a major refactor and half a year of updates

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * config-mode.c: Config mode base functions for Anduril.
3
 
 *
4
 
 * Copyright (C) 2017 Selene Scriven
5
 
 *
6
 
 * This program is free software: you can redistribute it and/or modify
7
 
 * it under the terms of the GNU General Public License as published by
8
 
 * the Free Software Foundation, either version 3 of the License, or
9
 
 * (at your option) any later version.
10
 
 *
11
 
 * This program is distributed in the hope that it will be useful,
12
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
 * GNU General Public License for more details.
15
 
 *
16
 
 * You should have received a copy of the GNU General Public License
17
 
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 
 */
 
1
// config-mode.c: Config mode base functions for Anduril.
 
2
// Copyright (C) 2017-2023 Selene ToyKeeper
 
3
// SPDX-License-Identifier: GPL-3.0-or-later
19
4
 
20
 
#ifndef CONFIG_MODE_C
21
 
#define CONFIG_MODE_C
 
5
#pragma once
22
6
 
23
7
#include "config-mode.h"
24
8
 
28
12
volatile uint8_t number_entry_value;
29
13
 
30
14
 
 
15
#if defined(USE_CONFIG_COLORS) && (NUM_CHANNEL_MODES > 1)
 
16
// TODO: promote this to fsm-channels.c ?
 
17
void set_chan_if(bool cond, uint8_t chan) {
 
18
    if ((cond) && (chan != channel_mode))
 
19
        set_channel_mode(chan);
 
20
}
 
21
#endif
 
22
 
31
23
// allow the user to set a new value for a config option
32
24
// can be called two ways:
33
25
//   - with a "click" action: Configures first menu item only.
45
37
        void (*savefunc)(uint8_t step, uint8_t value)) {
46
38
 
47
39
    static uint8_t config_step;
 
40
    #ifdef USE_CONFIG_COLORS
 
41
    static uint8_t orig_channel;
 
42
    #endif
48
43
    if (event == EV_enter_state) {
 
44
        #if defined(USE_CONFIG_COLORS) && (NUM_CHANNEL_MODES > 1)
 
45
        orig_channel = channel_mode;
 
46
        #endif
49
47
        config_step = 0;
50
48
        set_level(0);
51
49
        // if button isn't held, configure first menu item
62
60
    #define B_ANY_HOLD_RELEASE (B_CLICK|B_HOLD|B_RELEASE|B_TIMEOUT)
63
61
    else if ((event & B_CLICK_FLAGS) == B_ANY_HOLD) {
64
62
        if (config_step <= num_config_steps) {
65
 
            if (2 == (arg % (TICKS_PER_SECOND*3/2))) {
 
63
            #if defined(USE_CONFIG_COLORS) && (NUM_CHANNEL_MODES > 1)
 
64
                uint8_t chan = config_step - 1;
 
65
                if (chan < NUM_CHANNEL_MODES)
 
66
                    set_chan_if(config_color_per_step, chan);
 
67
            #endif
 
68
            if ((TICKS_PER_SECOND/10) == (arg % (TICKS_PER_SECOND*3/2))) {
66
69
                config_step ++;
67
70
                // blink when config step advances
68
 
                if (config_step <= num_config_steps)
 
71
                if (config_step <= num_config_steps) {
 
72
                    #ifdef CONFIG_BLINK_CHANNEL
 
73
                    set_chan_if(!config_color_per_step, CONFIG_BLINK_CHANNEL);
 
74
                    #endif
69
75
                    set_level(RAMP_SIZE * 3 / 8);
 
76
                }
70
77
            }
71
78
            else {
72
79
                // stay on at a low level to indicate menu is active
 
80
                #ifdef CONFIG_WAITING_CHANNEL
 
81
                set_chan_if(!config_color_per_step, CONFIG_WAITING_CHANNEL);
 
82
                #endif
73
83
                set_level(RAMP_SIZE * 1 / 8);
74
84
            }
75
85
        } else {
82
92
    else if ((event & B_CLICK_FLAGS) == B_ANY_HOLD_RELEASE) {
83
93
        // ask the user for a number, if they selected a menu item
84
94
        if (config_step && config_step <= num_config_steps) {
 
95
            #if defined(USE_CONFIG_COLORS) && (NUM_CHANNEL_MODES > 1)
 
96
                // put the colors back how they were
 
97
                set_channel_mode(orig_channel);
 
98
            #endif
85
99
            push_state(number_entry_state, 0);
86
100
        }
87
101
        // exit after falling out of end of menu
99
113
        pop_state();
100
114
    }
101
115
 
 
116
    #if defined(USE_CONFIG_COLORS) && (NUM_CHANNEL_MODES > 1)
 
117
    else if (event == EV_leave_state) {
 
118
        // put the colors back how they were
 
119
        set_channel_mode(orig_channel);
 
120
    }
 
121
    #endif
 
122
 
102
123
    // eat all other events; don't pass any through to parent
103
124
    return EVENT_HANDLED;
104
125
}
135
156
            // (flicker every other frame,
136
157
            //  except first frame (so we can see flashes after each click))
137
158
            else if (arg) {
 
159
                #ifdef CONFIG_WAITING_CHANNEL
 
160
                set_chan_if(1, CONFIG_WAITING_CHANNEL);
 
161
                #endif
138
162
                set_level( (RAMP_SIZE/8)
139
 
                           + ((arg&2)<<1) );
 
163
                           + ((arg&2)<<2) );
140
164
            }
141
165
        }
142
166
        // all done, save result and return to parent state
143
167
        else {
144
168
            pop_state();
145
169
        }
146
 
        return MISCHIEF_MANAGED;
 
170
        return EVENT_HANDLED;
147
171
    }
148
172
 
149
173
    // count clicks: click = +1, hold = +10
159
183
        #endif
160
184
        number_entry_value ++;  // update the result
161
185
        empty_event_sequence();  // reset FSM's click count
 
186
        #ifdef CONFIG_BLINK_CHANNEL
 
187
        set_channel_mode(CONFIG_BLINK_CHANNEL);
 
188
        #endif
162
189
        set_level(RAMP_SIZE/2);  // flash briefly
163
 
        return MISCHIEF_MANAGED;
 
190
        return EVENT_HANDLED;
164
191
    }
165
192
 
166
193
    // eat all other events; don't pass any through to parent
167
194
    return EVENT_HANDLED;
168
195
}
169
196
 
170
 
 
171
 
#endif
172