1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
#include <errno.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "til.h"
#include "til_fb.h"
#include "til_module_context.h"
#include "til_stream.h"
#include "til_tap.h"
/* Dead-simple strobe light, initially made to try simulate this contraption:
* https://en.wikipedia.org/wiki/Dreamachine
*
* But it might actually have some general utility in compositing.
*/
/* Copyright (C) 2022 Vito Caputo <vcaputo@pengaru.com> */
/* TODO:
* - Make hz setting more flexible
*/
#define STROBE_DEFAULT_HZ 10
typedef struct strobe_setup_t {
til_setup_t til_setup;
float hz;
} strobe_setup_t;
typedef struct strobe_context_t {
til_module_context_t til_module_context;
strobe_setup_t *setup;
unsigned last_flash_ticks;
struct {
til_tap_t hz;
til_tap_t toggle;
} taps;
struct {
float hz;
float toggle;
} vars;
float *hz;
float *toggle;
unsigned flash:1;
unsigned flash_ready:1;
} strobe_context_t;
static void strobe_update_taps(strobe_context_t *ctxt, til_stream_t *stream, float dt)
{
if (!til_stream_tap_context(stream, &ctxt->til_module_context, NULL, &ctxt->taps.hz))
*ctxt->hz = ctxt->setup->hz;
else
ctxt->vars.hz = *ctxt->hz;
if (!til_stream_tap_context(stream, &ctxt->til_module_context, NULL, &ctxt->taps.toggle))
ctxt->vars.toggle = NAN;
else
ctxt->vars.toggle = *ctxt->toggle;
if (ctxt->vars.hz <= 0.f)
ctxt->vars.hz = 0.f;
}
static til_module_context_t * strobe_create_context(const til_module_t *module, til_stream_t *stream, unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup)
{
strobe_context_t *ctxt;
ctxt = til_module_context_new(module, sizeof(strobe_context_t), stream, seed, ticks, n_cpus, setup);
if (!ctxt)
return NULL;
ctxt->setup = (strobe_setup_t *)setup;
ctxt->last_flash_ticks = ticks;
ctxt->taps.hz = til_tap_init_float(ctxt, &ctxt->hz, 1, &ctxt->vars.hz, "hz");
ctxt->taps.toggle = til_tap_init_float(ctxt, &ctxt->toggle, 1, &ctxt->vars.toggle, "toggle");
strobe_update_taps(ctxt, stream, 0.f);
return &ctxt->til_module_context;
}
static void strobe_prepare_frame(til_module_context_t *context, til_stream_t *stream, unsigned ticks, til_fb_fragment_t **fragment_ptr, til_frame_plan_t *res_frame_plan)
{
strobe_context_t *ctxt = (strobe_context_t *)context;
*res_frame_plan = (til_frame_plan_t){ .fragmenter = til_fragmenter_slice_per_cpu_x16 };
strobe_update_taps(ctxt, stream, (ticks - context->last_ticks) * .001f);
if (!isnan(ctxt->vars.toggle)) {
ctxt->flash = rintf(ctxt->vars.toggle) >= 1 ? 1 : 0;
ctxt->flash_ready = !ctxt->flash; /* kind of pointless */
return;
}
if (ctxt->vars.hz <= 0.f) {
ctxt->flash = 0;
ctxt->flash_ready = 1;
return;
}
if (ctxt->flash_ready && (ticks - ctxt->last_flash_ticks >= (unsigned)((1.f / ctxt->vars.hz) * 1000.f))){
ctxt->flash = 1;
ctxt->flash_ready = 0;
} else {
ctxt->flash_ready = 1;
}
}
static void strobe_render_fragment(til_module_context_t *context, til_stream_t *stream, unsigned ticks, unsigned cpu, til_fb_fragment_t **fragment_ptr)
{
strobe_context_t *ctxt = (strobe_context_t *)context;
til_fb_fragment_t *fragment = *fragment_ptr;
if (!ctxt->flash)
return til_fb_fragment_clear(fragment);
til_fb_fragment_fill(fragment, TIL_FB_DRAW_FLAG_TEXTURABLE, 0xffffffff);
}
static void strobe_finish_frame(til_module_context_t *context, til_stream_t *stream, unsigned int ticks, til_fb_fragment_t **fragment_ptr)
{
strobe_context_t *ctxt = (strobe_context_t *)context;
if (!ctxt->flash)
return;
ctxt->flash = 0;
ctxt->last_flash_ticks = ticks;
}
static int strobe_setup(const til_settings_t *settings, til_setting_t **res_setting, const til_setting_desc_t **res_desc, til_setup_t **res_setup);
til_module_t strobe_module = {
.create_context = strobe_create_context,
.prepare_frame = strobe_prepare_frame,
.render_fragment = strobe_render_fragment,
.finish_frame = strobe_finish_frame,
.setup = strobe_setup,
.name = "strobe",
.description = "Strobe light (threaded)",
.author = "Vito Caputo <vcaputo@pengaru.com>",
.flags = TIL_MODULE_OVERLAYABLE,
};
static int strobe_setup(const til_settings_t *settings, til_setting_t **res_setting, const til_setting_desc_t **res_desc, til_setup_t **res_setup)
{
const char *hz;
const char *hz_values[] = {
"60",
"50",
"40",
"20",
"10",
"4",
"2",
"1",
NULL
};
int r;
r = til_settings_get_and_describe_value(settings,
&(til_setting_spec_t){
.name = "Strobe frequency in hz",
.key = "hz",
.regex = "\\.[0-9]+",
.preferred = TIL_SETTINGS_STR(STROBE_DEFAULT_HZ),
.values = hz_values,
.annotations = NULL
},
&hz,
res_setting,
res_desc);
if (r)
return r;
if (res_setup) {
strobe_setup_t *setup;
setup = til_setup_new(settings, sizeof(*setup), NULL, &strobe_module);
if (!setup)
return -ENOMEM;
sscanf(hz, "%f", &setup->hz);
*res_setup = &setup->til_setup;
}
return 0;
}
|