forked from boqs/pd-grid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
meadowphysics.c
383 lines (309 loc) · 10.1 KB
/
meadowphysics.c
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#include "meadowphysics.h"
// LED intensity levels
#define L2 15
#define L1 9
#define L0 5
static void op_mp_in_step(op_mp_t* op, float f);
static void op_mp_in_size(op_mp_t* op, float f);
/// monome event handler
static void op_mp_handler(t_monome* op_monome, u8 x, u8 y, u8 z);
static void op_mp_redraw(t_monome* op_monome);
static void op_mp_trigger(op_mp_t* op_monome, u8 n);
static u32 rnd(void);
// pickles
static void op_mp_pickle(op_mp_t* mgrid, FILE *f);
static void op_mp_unpickle(op_mp_t* mgrid, FILE *f);
const u8 glyph[8][8] = {{0,0,0,0,0,0,0,0}, // o
{0,24,24,126,126,24,24,0}, // +
{0,0,0,126,126,0,0,0}, // -
{0,96,96,126,126,96,96,0}, // >
{0,6,6,126,126,6,6,0}, // <
{0,102,102,24,24,102,102,0}, // * rnd
{0,120,120,102,102,30,30,0}, // <> up/down
{0,126,126,102,102,126,126,0}}; // [] return
u32 a1 = 0x19660d;
u32 c1 = 0x3c6ef35f;
u32 x1 = 1234567; // seed
u32 a2 = 0x19660d;
u32 c2 = 0x3c6ef35f;
u32 x2 = 7654321; // seed
void cascades_copy_init_u(u8 *dest, u8 *src);
void cascades_copy_init_s(s8 *dest, s8 *src);
void cascades_copy_init_u(u8 *dest, u8 *src) {
int i;
for(i=0; i<8; i++) {
dest[i] = src[i];
}
}
void cascades_copy_init_s(s8 *dest, s8 *src) {
int i;
for(i=0; i<8; i++) {
dest[i] = src[i];
}
}
static t_class *op_mp_class;
void *mp_new(t_symbol *s, int argc, t_atom *argv);
void mp_free(void* op);
void meadowphysics_setup (void) {
net_monome_setup();
op_mp_class = class_new(gensym("meadowphysics"),
(t_newmethod)mp_new,
(t_method)mp_free,
sizeof(op_mp_t),
CLASS_DEFAULT,
A_GIMME, 0);
net_monome_add_focus_methods(op_mp_class);
class_addbang(op_mp_class, (t_method)op_mp_in_step);
class_addmethod(op_mp_class,
(t_method)op_mp_in_size, gensym("size"), A_DEFFLOAT, 0);
/* class_sethelpsymbol(counter_class, gensym("help-counter")); */
}
void *mp_new(t_symbol *s, int argc, t_atom *argv) {
(void) s;
(void) argc;
(void) argv;
op_mp_t *op = (op_mp_t *)pd_new(op_mp_class);
net_monome_init(&op->monome, (monome_handler_t)&op_mp_handler,
(monome_pickle_t)op_mp_pickle,
(monome_pickle_t)op_mp_unpickle);
op->out = outlet_new((t_object *) op, &s_float);
op->key_count = 0;
op->mode = 0;
op->prev_mode = 0;
op->edit_row = op->key_count = op->mode = op->prev_mode = 0;
op->size = net_monome_size_x();
op->XSIZE = net_monome_size_x();
op->dummy = 0;
s8 positions_init[8] = {3,1,2,2,3,3,5,7};
cascades_copy_init_s(op->positions, positions_init);
s8 points_init[8] = {3,1,2,2,3,3,5,7};
cascades_copy_init_s(op->points, points_init);
s8 points_save_init[8] = {3,1,2,2,3,3,5,7};
cascades_copy_init_s(op->points_save, points_save_init);
u8 triggers_init[8] = {0,0,0,0,0,0,0,0};
cascades_copy_init_u(op->triggers, triggers_init);
u8 trig_dests_init[8] = {0,0,0,0,0,0,0,0};
cascades_copy_init_u(op->trig_dests, trig_dests_init);
u8 rules_init[8] = {0,0,0,0,0,0,0,0};
cascades_copy_init_u(op->rules, rules_init);
u8 rules_dests_init[8] = {0,1,2,3,4,5,6,7};
cascades_copy_init_u(op->rule_dests, rules_dests_init);
// init monome drawing
op_mp_redraw(&(op->monome));
return op;
}
// de-init
void mp_free(void* op) {
// release focus
net_monome_deinit((t_monome *) op);
}
//-------------------------------------------------
//----- static function definition
static void op_mp_in_size(op_mp_t* op, float f) {
if(f < 9.0) op->size = 8;
else op->size = 16;
// FIXME: auto-detect size, this doesn't get set in unpickling
op->XSIZE = op->size;
}
static void op_mp_in_step(op_mp_t* op, float f) {
(void) f;
u8 i;
// clear last round
for(i=0;i<8;i++)
op->triggers[i] = 0;
// main
op_mp_trigger(op, 0);
// ensure bounds, output triggers
for(i=0;i<8;i++) {
if(op->positions[i] < 0)
op->positions[i] = 0;
else if(op->positions[i] > op->points[i])
op->positions[i] = op->points[i];
// send out
if(op->triggers[i]) {
outlet_float(op->out, (float)i);
}
}
op_mp_redraw(&op->monome);
}
static void op_mp_trigger(op_mp_t *op, u8 n) {
u8 m;
op->positions[n]--;
// ****** the trigger # check is so we don't cause a trigger/rules multiple times per NEXT
// a rules-based jump to position-point does not current cause a trigger. should it?
if(op->positions[n] < 0 && op->triggers[n] == 0) {
op->triggers[n]++;
if(op->rules[n] == 1) { // inc
if(op->points[op->rule_dests[n]] < (op->XSIZE-1)) {
op->points[op->rule_dests[n]]++;
op->positions[op->rule_dests[n]] = op->points[op->rule_dests[n]];
}
}
else if(op->rules[n] == 2) { // dec
if(op->points[op->rule_dests[n]] > 0) {
op->points[op->rule_dests[n]]--;
op->positions[op->rule_dests[n]] = op->points[op->rule_dests[n]];
}
}
else if(op->rules[n] == 3) { // max
op->points[op->rule_dests[n]] = (op->XSIZE-1);
op->positions[op->rule_dests[n]] = op->points[op->rule_dests[n]];
}
else if(op->rules[n] == 4) { // min
op->points[op->rule_dests[n]] = 0;
op->positions[op->rule_dests[n]] = op->points[op->rule_dests[n]];
}
else if(op->rules[n] == 5) { // rnd
op->points[op->rule_dests[n]] = rnd() % op->XSIZE;
op->positions[op->rule_dests[n]] = op->points[op->rule_dests[n]];
}
else if(op->rules[n] == 6) { // up/down
op->points[op->rule_dests[n]] += rnd() % 3;
op->points[op->rule_dests[n]]--;
if(op->points[op->rule_dests[n]] < 0) op->points[op->rule_dests[n]] = 0;
else if(op->points[op->rule_dests[n]] > (op->XSIZE-1)) op->points[op->rule_dests[n]] = op->XSIZE-1;
op->positions[op->rule_dests[n]] = op->points[op->rule_dests[n]];
}
else if(op->rules[n] == 7) { // return
op->points[op->rule_dests[n]] = op->points_save[op->rule_dests[n]];
}
//reset
op->positions[n] += op->points[n] + 1;
//triggers
for(m=0;m<8;m++)
if((op->trig_dests[n] & (1<<m)) != 0)
op_mp_trigger(op, m);
// post("\ntrigger",n," -> ", m);
}
}
// process monome key input
static void op_mp_handler(t_monome* op_monome, u8 x, u8 y, u8 z) {
op_mp_t *op = (op_mp_t *) op_monome;
u8 dirty_grid = 0;
op->prev_mode = op->mode;
// mode check
if(x == 0) {
op->key_count += (z<<1)-1;
if(op->key_count == 1 && z == 1)
op->mode = 1;
else if(op->key_count == 0)
op->mode = 0;
if(z == 1 && op->mode == 1) {
op->edit_row = y;
dirty_grid = 1;
// post("edit row:",op->edit_row);
}
}
else if(x == 1 && op->mode != 0) {
if(op->mode == 1 && z == 1)
op->mode = 2;
else if(op->mode == 2 && z == 0)
op->mode = 1;
}
else if(op->mode == 0 && z == 1) {
op->points[y] = x;
op->points_save[y] = x;
op->positions[y] = x;
dirty_grid = 1;
}
else if(op->mode == 1 && z == 1) {
if(y != op->edit_row) { // filter out self-triggering
op->trig_dests[op->edit_row] ^= (1<<y);
dirty_grid = 1;
// post("\ntrig_dests", op->edit_row, ":", op->trig_dests[op->edit_row]);
}
}
else if(op->mode == 2 && z == 1) {
if(x > 1 && x < 6) {
op->rule_dests[op->edit_row] = y;
dirty_grid = 1;
// post("\nrule_dests", op->edit_row, ":", op->rule_dests[op->edit_row]);
}
else if(x > 5) {
op->rules[op->edit_row] = y;
dirty_grid = 1;
// post("\nrules", op->edit_row, ":", op->rules[op->edit_row]);
}
}
if(op->mode != op->prev_mode) {
dirty_grid = 1;
// post("\nnew mode", mode);
}
if(dirty_grid)
op_mp_redraw(op_monome);
}
// redraw monome grid
static void op_mp_redraw(t_monome* op_monome) {
op_mp_t *op = (op_mp_t *) op_monome;
u8 i1, i2, i3;
// clear grid
for(i1=0;i1<128;i1++)
op_monome->opLedBuffer[i1] = 0;
// SET POSITIONS
if(op->mode == 0) {
for(i1=0;i1<8;i1++) {
for(i2=op->positions[i1];i2<=op->points[i1];i2++)
op_monome->opLedBuffer[i1*16 + i2] = L1;
op_monome->opLedBuffer[i1*16 + op->positions[i1]] = L2;
}
}
// SET ROUTING
else if(op->mode == 1) {
op_monome->opLedBuffer[op->edit_row * 16] = L1;
op_monome->opLedBuffer[op->edit_row * 16 + 1] = L1;
for(i1=0;i1<8;i1++) {
if((op->trig_dests[op->edit_row] & (1<<i1)) != 0) {
for(i2=0;i2<=op->points[i1];i2++)
op_monome->opLedBuffer[i1*16 + i2] = L2;
}
op_monome->opLedBuffer[i1*16 + op->positions[i1]] = L0;
}
}
// SET RULES
else if(op->mode == 2) {
op_monome->opLedBuffer[op->edit_row * 16] = L1;
op_monome->opLedBuffer[op->edit_row * 16 + 1] = L1;
for(i1=2;i1<7;i1++)
op_monome->opLedBuffer[op->rule_dests[op->edit_row] * 16 + i1] = L2;
for(i1=8;i1<16;i1++)
op_monome->opLedBuffer[op->rules[op->edit_row] * 16 + i1] = L0;
for(i1=0;i1<8;i1++)
op_monome->opLedBuffer[i1*16 + op->positions[i1]] = L0;
for(i1=0;i1<8;i1++) {
i3 = glyph[op->rules[op->edit_row]][i1];
for(i2=0;i2<8;i2++) {
if((i3 & (1<<i2)) != 0)
op_monome->opLedBuffer[i1*16 + 8 + i2] = L2;
}
}
op_monome->opLedBuffer[op->rules[op->edit_row] * 16 + 7] = L2;
}
}
static u32 rnd() {
x1 = x1 * c1 + a1;
x2 = x2 * c2 + a2;
return (x1>>16) | (x2>>16);
}
// pickle / unpickle
void op_mp_pickle(op_mp_t* mgrid, FILE *f) {
fwrite(&mgrid->size, sizeof(mgrid->size), 1, f);
fwrite(mgrid->positions, sizeof(s8), 8, f);
fwrite(mgrid->points, sizeof(s8), 8, f);
fwrite(mgrid->points_save, sizeof(s8), 8, f);
fwrite(mgrid->triggers, sizeof(u8), 8, f);
fwrite(mgrid->trig_dests, sizeof(u8), 8, f);
fwrite(mgrid->rules, sizeof(u8), 8, f);
fwrite(mgrid->rule_dests, sizeof(u8), 8, f);
fwrite(&mgrid->XSIZE, sizeof(u8), 1, f);
}
void op_mp_unpickle(op_mp_t* mgrid, FILE *f) {
fread(&mgrid->size, sizeof(mgrid->size), 1, f);
fread(mgrid->positions, sizeof(s8), 8, f);
fread(mgrid->points, sizeof(s8), 8, f);
fread(mgrid->points_save, sizeof(s8), 8, f);
fread(mgrid->triggers, sizeof(u8), 8, f);
fread(mgrid->trig_dests, sizeof(u8), 8, f);
fread(mgrid->rules, sizeof(u8), 8, f);
fread(mgrid->rule_dests, sizeof(u8), 8, f);
fread(&mgrid->XSIZE, sizeof(u8), 1, f);
}