~toykeeper/flashlight-firmware/fsm

« back to all changes in this revision

Viewing changes to ToyKeeper/spaghetti-monster/fsm-channels.h

  • 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
// fsm-channels.h: Channel mode functions for SpaghettiMonster.
 
2
// Copyright (C) 2023 Selene ToyKeeper
 
3
// SPDX-License-Identifier: GPL-3.0-or-later
 
4
 
 
5
#pragma once
 
6
 
 
7
// always enable channel modes, even if there is only one
 
8
#define USE_CHANNEL_MODES
 
9
 
 
10
// typedefs
 
11
typedef void SetLevelFunc(uint8_t level);
 
12
typedef SetLevelFunc * SetLevelFuncPtr;
 
13
 
 
14
typedef bool GradualTickFunc(uint8_t gt);
 
15
typedef GradualTickFunc * GradualTickFuncPtr;
 
16
 
 
17
// TODO: implement custom 3H handlers
 
18
typedef void ChannelArgFunc();
 
19
typedef ChannelArgFunc * ChannelArgFuncPtr;
 
20
 
 
21
typedef struct Channel {
 
22
    SetLevelFuncPtr set_level;
 
23
    #ifdef USE_SET_LEVEL_GRADUALLY
 
24
        GradualTickFuncPtr gradual_tick;
 
25
    #endif
 
26
    #ifdef USE_CUSTOM_3H_HANDLERS
 
27
        // TODO: implement custom 3H handlers
 
28
        ChannelArgFuncPtr ramp_channel_arg;
 
29
    #endif
 
30
    #ifdef USE_CHANNEL_MODE_ARGS
 
31
        bool has_args;
 
32
        //uint8_t arg;  // is in the config struct, not here
 
33
    #endif
 
34
} Channel;
 
35
 
 
36
Channel channels[];  // values are defined in the hwdef-*.c
 
37
 
 
38
// TODO: size-optimize the case with only 1 channel mode?
 
39
// (the arrays and stuff shouldn't be needed)
 
40
 
 
41
#if NUM_CHANNEL_MODES > 1
 
42
    #define USE_CHANNEL_MODES
 
43
    // current multi-channel mode
 
44
    uint8_t channel_mode = DEFAULT_CHANNEL_MODE;
 
45
#else
 
46
    #define channel_mode 0
 
47
#endif
 
48
 
 
49
#ifdef USE_CUSTOM_CHANNEL_3H_MODES
 
50
// different 3H behavior per channel?
 
51
// TODO: move to progmem
 
52
// TODO: move to Anduril, not FSM
 
53
StatePtr channel_3H_modes[NUM_CHANNEL_MODES];
 
54
#endif
 
55
 
 
56
//#ifdef USE_CHANNEL_MODE_TOGGLES
 
57
#if NUM_CHANNEL_MODES > 1
 
58
// user can take unwanted modes out of the rotation
 
59
// bitmask
 
60
#ifdef USE_CFG
 
61
    #define channel_mode_enabled(n) ((cfg.channel_modes_enabled >> n) & 1)
 
62
    #define channel_mode_enable(n)  cfg.channel_modes_enabled |= (1 << n)
 
63
    #define channel_mode_disable(n) cfg.channel_modes_enabled &= ((1 << n) ^ 0xff)
 
64
#else
 
65
    uint16_t channel_modes_enabled = CHANNEL_MODES_ENABLED;
 
66
    #define channel_mode_enabled(n) ((channel_modes_enabled >> n) & 1)
 
67
    #define channel_mode_enable(n)  channel_modes_enabled |= (1 << n)
 
68
    #define channel_mode_disable(n) channel_modes_enabled &= ((1 << n) ^ 0xff)
 
69
    #endif
 
70
#endif
 
71
 
 
72
#ifdef USE_CHANNEL_MODE_ARGS
 
73
    #ifndef USE_CFG
 
74
    // one byte of extra data per channel mode, like for tint value
 
75
    uint8_t channel_mode_args[NUM_CHANNEL_MODES] = { CHANNEL_MODE_ARGS };
 
76
    #endif
 
77
    // which modes respond to their "arg", and which don't?
 
78
    //const uint8_t channel_has_args = CHANNEL_HAS_ARGS;
 
79
    //#define channel_has_args(n) ((CHANNEL_HAS_ARGS >> n) & 1)
 
80
    // struct member
 
81
    #define channel_has_args(n) (channels[n].has_args)
 
82
#endif
 
83
 
 
84
#if NUM_CHANNEL_MODES > 1
 
85
void set_channel_mode(uint8_t mode);
 
86
#endif
 
87
 
 
88
#ifdef USE_CALC_2CH_BLEND
 
89
void calc_2ch_blend(
 
90
    PWM_DATATYPE *warm,
 
91
    PWM_DATATYPE *cool,
 
92
    PWM_DATATYPE brightness,
 
93
    PWM_DATATYPE top,
 
94
    uint8_t blend);
 
95
#endif
 
96
 
 
97
#ifdef USE_HSV2RGB
 
98
typedef struct RGB_t {
 
99
    uint16_t r;
 
100
    uint16_t g;
 
101
    uint16_t b;
 
102
} RGB_t;
 
103
RGB_t hsv2rgb(uint8_t h, uint8_t s, uint16_t v);
 
104
#endif  // ifdef USE_HSV2RGB
 
105
 
 
106
 
 
107
#ifdef USE_SET_LEVEL_1CH
 
108
// TODO: remove this
 
109
void set_level_1ch(uint8_t level);
 
110
#endif
 
111
 
 
112
#ifdef USE_SET_LEVEL_2CH_STACKED
 
113
// TODO: remove this
 
114
void set_level_2ch_stacked(uint8_t level);
 
115
#endif
 
116
 
 
117
#ifdef USE_SET_LEVEL_3CH_STACKED
 
118
// TODO: remove this
 
119
void set_level_3ch_stacked(uint8_t level);
 
120
#endif
 
121
 
 
122
#if defined(USE_TINT_RAMPING) && (!defined(TINT_RAMP_TOGGLE_ONLY))
 
123
// TODO: remove this
 
124
void set_level_2ch_blend();
 
125
#endif
 
126
 
 
127
#ifdef USE_GRADUAL_TICK_1CH
 
128
// TODO: remove this
 
129
void gradual_tick_1ch();
 
130
#endif
 
131
 
 
132
#ifdef USE_GRADUAL_TICK_2CH_STACKED
 
133
// TODO: remove this
 
134
void gradual_tick_2ch_stacked();
 
135
#endif
 
136
 
 
137
#ifdef USE_GRADUAL_TICK_3CH_STACKED
 
138
// TODO: remove this
 
139
void gradual_tick_3ch_stacked();
 
140
#endif
 
141