-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathmodtulip.c
More file actions
1939 lines (1610 loc) · 69.4 KB
/
Copy pathmodtulip.c
File metadata and controls
1939 lines (1610 loc) · 69.4 KB
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <string.h>
#include "polyfills.h"
#include "py/runtime.h"
#include "py/mperrno.h"
#include "py/mphal.h"
#include "extmod/vfs.h"
#include "py/stream.h"
#ifndef __EMSCRIPTEN__
#include "amy.h"
#endif
#ifndef __EMSCRIPTEN__
#include "amy_midi.h"
#endif
#include "amy_connector.h"
#include "tsequencer.h"
#if !defined(AMYBOARD) && !defined(AMYBOARD_WEB)
#include "ui.h"
#include "keyscan.h"
#include "display.h"
#include "bresenham.h"
#endif
#include "genhdr/mpversion.h"
#ifdef ESP_PLATFORM
#include "tasks.h"
#include "driver/rtc_io.h"
#endif
#ifndef __EMSCRIPTEN__
// on web this will get shim in with js
STATIC mp_obj_t tulip_amy_ticks_ms(size_t n_args, const mp_obj_t *args) {
return mp_obj_new_int(amy_sysclock());
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_amy_ticks_ms_obj, 0, 0, tulip_amy_ticks_ms);
// Smoothed fraction of real time AMY spends rendering (0..1); ~1.0 means overloaded.
STATIC mp_obj_t tulip_amy_render_load(size_t n_args, const mp_obj_t *args) {
return mp_obj_new_float(amy_get_render_load());
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_amy_render_load_obj, 0, 0, tulip_amy_render_load);
STATIC mp_obj_t tulip_amy_set_render_load_threshold(size_t n_args, const mp_obj_t *args) {
// MicroPython version of amy/src/pyamy.c:set_render_load_threshold
amy_set_render_load_threshold(mp_obj_get_float(args[0]));
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_amy_set_render_load_threshold_obj, 1, 1, tulip_amy_set_render_load_threshold);
#endif
STATIC mp_obj_t tulip_ticks_ms(size_t n_args, const mp_obj_t *args) {
return mp_obj_new_int(get_ticks_ms());
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_ticks_ms_obj, 0, 0, tulip_ticks_ms);
STATIC mp_obj_t tulip_stderr_write(size_t n_args, const mp_obj_t *args) {
const char *msg = mp_obj_str_get_str(args[0]);
fprintf(stderr, "%s\n", msg);
fflush(stderr);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_stderr_write_obj, 1, 1, tulip_stderr_write);
STATIC mp_obj_t tulip_board(size_t n_args, const mp_obj_t *args) {
#ifdef TULIP4_R11
return mp_obj_new_str("TULIP4_R11", strlen("TULIP4_R11"));
#elif defined MATOUCH7
return mp_obj_new_str("MATOUCH7", strlen("MATOUCH7"));
#elif defined TDECK
return mp_obj_new_str("TDECK", strlen("TDECK"));
#elif defined N16R8
return mp_obj_new_str("N16R8", strlen("N16R8"));
#elif defined N32R8
return mp_obj_new_str("N32R8", strlen("N32R8"));
#elif defined TULIP_DESKTOP
return mp_obj_new_str("DESKTOP", strlen("DESKTOP"));
#elif defined TULIP_WEB
return mp_obj_new_str("WEB", strlen("WEB"));
#elif defined AMYBOARD_WEB
return mp_obj_new_str("AMYBOARD_WEB", strlen("AMYBOARD_WEB"));
#elif defined AMYBOARD
return mp_obj_new_str("AMYBOARD", strlen("AMYBOARD"));
#else
return mp_obj_new_str("OTHER", strlen("OTHER"));
#endif
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_board_obj, 0, 0, tulip_board);
mp_obj_t midi_callback = NULL;
STATIC mp_obj_t tulip_midi_callback(size_t n_args, const mp_obj_t *args) {
midi_callback = args[0];
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_midi_callback_obj, 1, 1, tulip_midi_callback);
// Called (via mp_sched_schedule from the AMY render task) when AMY's CPU
// overload failsafe trips, with the render load percent as a small int.
mp_obj_t amy_overload_callback = NULL;
STATIC mp_obj_t tulip_amy_overload_callback(size_t n_args, const mp_obj_t *args) {
amy_overload_callback = (args[0] == mp_const_none) ? NULL : args[0];
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_amy_overload_callback_obj, 1, 1, tulip_amy_overload_callback);
// tulip.external_midi_sync() is defined in tulip.py: it sends the AMY wire
// command (external_midi_sync / zC) so it also works on the web builds, where
// AMY is a separate WASM module reachable only via the wire protocol.
#ifndef __EMSCRIPTEN__
extern int amy_get_output_buffer(int16_t *samples);
extern int amy_get_input_buffer(int16_t *samples);
extern void amy_set_external_input_buffer(int16_t * samples);
mp_obj_t amy_block_done_callback = NULL;
STATIC mp_obj_t tulip_amy_block_done_callback(size_t n_args, const mp_obj_t *args) {
if(n_args==0) {
amy_block_done_callback = NULL;
} else {
amy_block_done_callback = args[0];
}
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_amy_block_done_callback_obj, 0, 1, tulip_amy_block_done_callback);
STATIC mp_obj_t tulip_amy_get_output_buffer(size_t n_args, const mp_obj_t *args) {
uint8_t buf[1024];
int n = amy_get_output_buffer((int16_t*)buf);
if (n == 0) return mp_const_none;
mp_obj_t output_bytes = mp_obj_new_bytes(buf, n);
return output_bytes;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_amy_get_output_buffer_obj, 0, 0, tulip_amy_get_output_buffer);
STATIC mp_obj_t tulip_amy_get_input_buffer(size_t n_args, const mp_obj_t *args) {
uint8_t buf[1024];
int n = amy_get_input_buffer((int16_t*)buf);
if (n == 0) return mp_const_none;
mp_obj_t input_bytes = mp_obj_new_bytes(buf, n);
return input_bytes;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_amy_get_input_buffer_obj, 0, 0, tulip_amy_get_input_buffer);
STATIC mp_obj_t tulip_amy_set_external_input_buffer(size_t n_args, const mp_obj_t *args) {
mp_buffer_info_t bufinfo;
mp_get_buffer(args[0], &bufinfo, MP_BUFFER_READ);
if(bufinfo.len!=1024) { mp_raise_ValueError(MP_ERROR_TEXT("bytes len must be 1024")); }
amy_set_external_input_buffer((int16_t*)bufinfo.buf);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_amy_set_external_input_buffer_obj, 1, 1, tulip_amy_set_external_input_buffer);
STATIC mp_obj_t tulip_amy_get_synth_commands(size_t n_args, const mp_obj_t *args) {
// MicroPython version of amy/src/pyamy.c:get_synth_commands, to retrieve synth config from MP.
// We know we have exactlu one arg from the MP_DEFINE_CONST_FUN declaration below.
char s[MAX_MESSAGE_LEN];
void *state = NULL;
int synth = mp_obj_get_int(args[0]);
bool include_fx = true;
if (n_args > 1) include_fx = mp_obj_get_int(args[1]);
// Make a new list object, append the retrieved (and converted) strings to it.
mp_obj_t list = mp_obj_new_list(0, NULL);
do {
state = yield_synth_commands(synth, s, MAX_MESSAGE_LEN, include_fx, state);
int slen = strlen(s);
if (slen)
mp_obj_list_append(list, mp_obj_new_str(s, slen));
} while (state != NULL);
return list;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_amy_get_synth_commands_obj, 1, 2, tulip_amy_get_synth_commands);
STATIC mp_obj_t tulip_sequencer_start(void) {
extern void sequencer_midi_start(void);
sequencer_midi_start();
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(tulip_sequencer_start_obj, tulip_sequencer_start);
STATIC mp_obj_t tulip_amy_dump_state(void) {
int len = 0;
char *buf = amy_dump_state_to_string(&len);
if (!buf || len <= 0) {
return mp_obj_new_str("", 0);
}
mp_obj_t result = mp_obj_new_str(buf, len);
free(buf);
return result;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(tulip_amy_dump_state_obj, tulip_amy_dump_state);
#endif
#ifdef ESP_PLATFORM
extern uint8_t * external_map;
STATIC mp_obj_t tulip_amy_set_external_channel(size_t n_args, const mp_obj_t *args) {
uint16_t osc = mp_obj_get_int(args[0]);
uint8_t ch = mp_obj_get_int(args[1]);
external_map[osc] = ch;
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_amy_set_external_channel_obj, 2, 2, tulip_amy_set_external_channel);
// Map a synth number to a CV output channel (1-based, 0 = clear)
#define MAX_CV_SYNTHS 32
extern uint8_t cv_synth_map[MAX_CV_SYNTHS];
STATIC mp_obj_t tulip_set_cv_synth(size_t n_args, const mp_obj_t *args) {
uint8_t synth = mp_obj_get_int(args[0]);
uint8_t cv_channel = mp_obj_get_int(args[1]); // 1 = CV1, 2 = CV2, 0 = clear
if (synth < MAX_CV_SYNTHS) {
cv_synth_map[synth] = cv_channel;
}
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_set_cv_synth_obj, 2, 2, tulip_set_cv_synth);
#endif
STATIC mp_obj_t tulip_defer(size_t n_args, const mp_obj_t *args) {
int8_t index = -1;
for(uint8_t slot=0;slot<DEFER_SLOTS;slot++) {
if(defer_callbacks[slot]==NULL) {
index = slot; slot = DEFER_SLOTS+1;
}
}
if(index>=0) {
defer_callbacks[index] = args[0];
defer_args[index] = args[1];
defer_sysclock[index] = get_ticks_ms() + mp_obj_get_int(args[2]);
} else {
mp_raise_ValueError(MP_ERROR_TEXT("No more defer slots available"));
}
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_defer_obj, 3, 3, tulip_defer);
STATIC mp_obj_t tulip_seq_add_callback(size_t n_args, const mp_obj_t *args) {
int8_t index = -1;
for(uint8_t slot=0;slot<SEQUENCER_SLOTS;slot++) {
if(sequencer_callbacks[slot]==NULL) {
index = slot; slot = SEQUENCER_SLOTS+1;
}
}
if(index>=0) {
sequencer_callbacks[index] = args[0];
sequencer_tick[index] = mp_obj_get_int(args[1]);
sequencer_period[index] = mp_obj_get_int(args[2]);
} else {
index = -1;
}
return mp_obj_new_int(index);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_seq_add_callback_obj, 3, 3, tulip_seq_add_callback);
STATIC mp_obj_t tulip_seq_remove_callback(size_t n_args, const mp_obj_t *args) {
int8_t index = mp_obj_get_int(args[0]);
if(index>=0 && index <SEQUENCER_SLOTS) {
sequencer_callbacks[index] = NULL;
sequencer_period[index] = 0;
sequencer_tick[index] = 0;
}
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_seq_remove_callback_obj, 1, 1, tulip_seq_remove_callback);
STATIC mp_obj_t tulip_seq_remove_callbacks(size_t n_args, const mp_obj_t *args) {
for(uint8_t i=0;i<SEQUENCER_SLOTS;i++) {
sequencer_callbacks[i] = NULL;
sequencer_period[i] = 0;
sequencer_tick[i] = 0;
}
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_seq_remove_callbacks_obj, 0, 0, tulip_seq_remove_callbacks);
STATIC mp_obj_t tulip_seq_ticks(size_t n_args, const mp_obj_t *args) {
#ifdef AMY_IS_EXTERNAL
return mp_obj_new_int(sequencer_tick_count);
#else
return mp_obj_new_int(amy_global.sequencer_tick_count);
#endif
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_seq_ticks_obj, 0, 0, tulip_seq_ticks);
extern uint8_t last_midi[MIDI_QUEUE_DEPTH][MAX_MIDI_BYTES_PER_MESSAGE];
extern uint8_t last_midi_len[MIDI_QUEUE_DEPTH];
extern int16_t midi_queue_head;
extern int16_t midi_queue_tail ;
STATIC mp_obj_t tulip_midi_in(size_t n_args, const mp_obj_t *args) {
if(midi_queue_head != midi_queue_tail) {
int16_t prev_head = midi_queue_head;
// Step on the head, hope no-one notices before we pop it.
midi_queue_head = (midi_queue_head + 1) % MIDI_QUEUE_DEPTH;
return mp_obj_new_bytes(last_midi[prev_head],
last_midi_len[prev_head]);
}
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_midi_in_obj, 0, 0, tulip_midi_in);
extern uint16_t sysex_len;
STATIC mp_obj_t tulip_sysex_in(size_t n_args, const mp_obj_t *args) {
if(sysex_len) {
mp_obj_t sysex_bytes = mp_obj_new_bytes(sysex_buffer, sysex_len);
sysex_len = 0; // consumed, move on
return sysex_bytes;
}
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_sysex_in_obj, 0, 0, tulip_sysex_in);
extern void tulip_send_midi_out(uint8_t *, uint16_t);
STATIC mp_obj_t tulip_midi_out(size_t n_args, const mp_obj_t *args) {
if(mp_obj_get_type(args[0]) == &mp_type_bytes) {
mp_buffer_info_t bufinfo;
mp_get_buffer(args[0], &bufinfo, MP_BUFFER_READ);
tulip_send_midi_out((uint8_t*)bufinfo.buf, bufinfo.len);
} else {
mp_obj_t *items;
size_t len;
mp_obj_get_array(args[0], &len, &items);
uint8_t *b = malloc_caps(len, MALLOC_CAP_INTERNAL);
for(uint16_t i=0;i<(uint16_t)len;i++) {
b[i] = mp_obj_get_int(items[i]);
}
tulip_send_midi_out(b, len);
free_caps(b);
}
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_midi_out_obj, 1, 1, tulip_midi_out);
// Send a message on the "local bus", as if it was received from physical midi in
STATIC mp_obj_t tulip_midi_local(size_t n_args, const mp_obj_t *args) {
if(mp_obj_get_type(args[0]) == &mp_type_bytes) {
mp_buffer_info_t bufinfo;
mp_get_buffer(args[0], &bufinfo, MP_BUFFER_READ);
midi_local((uint8_t*)bufinfo.buf, bufinfo.len);
} else {
mp_obj_t *items;
size_t len;
mp_obj_get_array(args[0], &len, &items);
uint8_t *b = malloc_caps(len, MALLOC_CAP_INTERNAL);
for(uint16_t i=0;i<(uint16_t)len;i++) {
b[i] = mp_obj_get_int(items[i]);
}
midi_local(b, len);
free_caps(b);
}
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_midi_local_obj, 1, 1, tulip_midi_local);
#ifndef __EMSCRIPTEN__
STATIC mp_obj_t tulip_amy_send(size_t n_args, const mp_obj_t *args) {
amy_add_message((char*)mp_obj_str_get_str(args[0]));
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_amy_send_obj, 1, 1, tulip_amy_send);
STATIC mp_obj_t tulip_amy_send_sysex(size_t n_args, const mp_obj_t *args) {
#if SYSEX_COPY_SLOTS > 0
// Read from the next slot in the sysex ring buffer. parse_sysex() writes
// to sequential slots so that a fast-arriving next sysex doesn't overwrite
// an unprocessed message (which happens when loop() is CPU-heavy and the
// mp_sched callback is delayed).
char *slot = sysex_message_copies[sysex_copy_read_idx];
sysex_copy_read_idx = (sysex_copy_read_idx + 1) % SYSEX_COPY_SLOTS;
#else
// No sysex copy ring on this platform: amy (parse_sysex) gates the ring to
// TULIP/AMYBOARD and dispatches sysex synchronously otherwise, so this
// scheduled callback is never armed here. Guard avoids a `% 0` (desktop build).
char *slot = NULL;
#endif
if (slot) {
// Use _from_sysex variant so that during a file transfer,
// this data is routed to parse_transfer_message. Internal
// amy.send() calls (from sketch loop) use amy_add_message
// directly and bypass the transfer routing.
amy_add_message_from_sysex(slot);
}
// ACK the message AFTER processing. The sender (web) waits for this
// ACK before sending the next message, ensuring only one message is
// in flight at a time and the ring buffer can't overflow.
{
extern void midi_out(uint8_t *bytes, uint16_t len);
uint8_t ack[] = { 0xF0, 0x00, 0x03, 0x45, 'A', 'K', 0xF7 };
midi_out(ack, sizeof(ack));
}
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_amy_send_sysex_obj, 0, 1, tulip_amy_send_sysex);
STATIC mp_obj_t tulip_pcm_load_file(size_t n_args, const mp_obj_t *args) {
int result = pcm_load_file();
return mp_obj_new_int(result);
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_pcm_load_file_obj, 0, 1, tulip_pcm_load_file);
#endif
#ifdef TULIP_USER_C_DSP
#include "user_c_dsp.h"
// install_c_process(name, src): compile a C effect —
// void process(int *buf, int frames, int chans) — AMY S8.23 samples
// (1.0 == 1<<23; helpers cos_lut/fxmul/to_int16/from_int16 are available) —
// and install it under name. src may be a full program or just the function
// body (the wrapper and helper prototypes are added for you).
STATIC mp_obj_t tulip_install_c_process(mp_obj_t name_obj, mp_obj_t src_obj) {
char err[512];
int slot = user_c_dsp_install(mp_obj_str_get_str(name_obj), mp_obj_str_get_str(src_obj), USER_C_DSP_KIND_EFFECT, err, sizeof(err));
if (slot < 0) mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("install_c_process: %s"), err);
return mp_obj_new_int(slot);
}
MP_DEFINE_CONST_FUN_OBJ_2(tulip_install_c_process_obj, tulip_install_c_process);
// install_c_osc(name, src): compile a C oscillator —
// void render(int *buf, int frames, int osc, int phase_inc_q16, int amp)
// — S8.23 samples, amp in S8.23, phase_inc_q16 = per-sample step with
// 65536 == one cycle. Install under name; src may be a full program or body.
STATIC mp_obj_t tulip_install_c_osc(mp_obj_t name_obj, mp_obj_t src_obj) {
char err[512];
int slot = user_c_dsp_install(mp_obj_str_get_str(name_obj), mp_obj_str_get_str(src_obj), USER_C_DSP_KIND_OSC, err, sizeof(err));
if (slot < 0) mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("install_c_osc: %s"), err);
return mp_obj_new_int(slot);
}
MP_DEFINE_CONST_FUN_OBJ_2(tulip_install_c_osc_obj, tulip_install_c_osc);
// c_osc(name, osc, on=True): replace AMY osc number osc's waveform with the
// named user oscillator (or restore it with on=False).
STATIC mp_obj_t tulip_c_osc(size_t n_args, const mp_obj_t *args) {
int on = (n_args > 2) ? mp_obj_is_true(args[2]) : 1;
int r = user_c_dsp_bind_osc(mp_obj_str_get_str(args[0]), mp_obj_get_int(args[1]), on);
if (r == -1) mp_raise_ValueError(MP_ERROR_TEXT("no such c_osc"));
if (r == -2) mp_raise_ValueError(MP_ERROR_TEXT("bad osc number"));
if (r == -3) mp_raise_ValueError(MP_ERROR_TEXT("not an osc (installed with install_c_process?)"));
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_c_osc_obj, 2, 3, tulip_c_osc);
// c_process(name, on, bus=0): enable/disable a named process on a bus.
STATIC mp_obj_t tulip_c_process(size_t n_args, const mp_obj_t *args) {
int bus = (n_args > 2) ? mp_obj_get_int(args[2]) : 0;
int r = user_c_dsp_set(mp_obj_str_get_str(args[0]), bus, mp_obj_is_true(args[1]));
if (r == -1) mp_raise_ValueError(MP_ERROR_TEXT("no such c_process"));
if (r == -2) mp_raise_ValueError(MP_ERROR_TEXT("bad bus"));
if (r == -3) mp_raise_ValueError(MP_ERROR_TEXT("not an effect (installed with install_c_osc?)"));
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_c_process_obj, 2, 3, tulip_c_process);
// c_process_calls(name): blocks processed since install (is my effect running?)
STATIC mp_obj_t tulip_c_process_calls(mp_obj_t name_obj) {
int64_t calls = user_c_dsp_calls(mp_obj_str_get_str(name_obj));
if (calls < 0) mp_raise_ValueError(MP_ERROR_TEXT("no such c_process"));
return mp_obj_new_int_from_ll(calls);
}
MP_DEFINE_CONST_FUN_OBJ_1(tulip_c_process_calls_obj, tulip_c_process_calls);
STATIC mp_obj_t tulip_uninstall_c_process(mp_obj_t name_obj) {
if (user_c_dsp_uninstall(mp_obj_str_get_str(name_obj)) < 0)
mp_raise_ValueError(MP_ERROR_TEXT("no such c_process"));
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_1(tulip_uninstall_c_process_obj, tulip_uninstall_c_process);
#endif // TULIP_USER_C_DSP
/*
#if !defined(AMYBOARD) && !defined(__EMSCRIPTEN__)
extern char * alles_local_ip;
STATIC mp_obj_t tulip_multicast_start(size_t n_args, const mp_obj_t *args) {
const char * local_ip = mp_obj_str_get_str(args[0]);
if(strlen(local_ip)>2) {
strcpy(alles_local_ip, local_ip);
}
alles_init_multicast();
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_multicast_start_obj, 1, 1, tulip_multicast_start);
extern uint8_t alive;
extern int16_t client_id;
extern int32_t clocks[255];
extern int32_t ping_times[255];
STATIC mp_obj_t tulip_alles_map(size_t n_args, const mp_obj_t *args) {
mp_obj_t list = mp_obj_new_list(0, NULL);
for(uint8_t i=0;i<255;i++) {
mp_obj_t tuple[3];
if(clocks[i]>0) {
tuple[0] = mp_obj_new_int(i);
tuple[1] = mp_obj_new_int(clocks[i]);
tuple[2] = mp_obj_new_int(ping_times[i]);
mp_obj_list_append(list, mp_obj_new_tuple(3, tuple));
}
}
// TODO - sort this so that client 0 is the earliest, etc. Maybe do the sort in python
return list;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_alles_map_obj, 0, 0, tulip_alles_map);
#endif
#ifndef AMYBOARD_WEB
extern uint8_t ipv4_quartet;
STATIC mp_obj_t tulip_set_quartet(size_t n_args, const mp_obj_t *args) {
ipv4_quartet = mp_obj_get_int(args[0]);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_set_quartet_obj, 1, 1, tulip_set_quartet);
#endif
*/
extern float compute_cpu_usage(uint8_t debug);
STATIC mp_obj_t tulip_cpu(size_t n_args, const mp_obj_t *args) {
// for now just printf to uart
float idle;
if(n_args > 0) {
idle = compute_cpu_usage(1);
} else {
idle = compute_cpu_usage(0);
}
return mp_obj_new_float_from_f(idle);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_cpu_obj, 0, 1, tulip_cpu);
#ifndef ESP_PLATFORM
#ifndef __linux__
#ifndef __EMSCRIPTEN__
extern char* get_tulip_home_path();
STATIC mp_obj_t tulip_app_path(size_t n_args, const mp_obj_t *args) {
char * path = get_tulip_home_path();
return mp_obj_new_str(path, strlen(path));
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_app_path_obj, 0, 0, tulip_app_path);
#endif
#endif
#endif
STATIC mp_obj_t tulip_build_strings(size_t n_args, const mp_obj_t *args) {
mp_obj_t tuple[3];
tuple[0] = mp_obj_new_str(MICROPY_GIT_TAG, strlen(MICROPY_GIT_TAG));
tuple[1] = mp_obj_new_str(MICROPY_GIT_HASH, strlen(MICROPY_GIT_HASH));
tuple[2] = mp_obj_new_str(MICROPY_BUILD_DATE, strlen(MICROPY_BUILD_DATE));
return mp_obj_new_tuple(3, tuple);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_build_strings_obj, 0,0, tulip_build_strings);
#if defined(AMYBOARD)
extern float cv_local_value[2];
extern uint8_t cv_local_override[2];
extern float cv_input_hook(uint16_t channel);
#endif
#if defined(AMYBOARD_WEB)
float cv_local_value[2] = {0, 0};
uint8_t cv_local_override[2] = {0, 0};
float cv_input_hook(uint16_t channel) {
return cv_local_value[channel];
}
#endif
#if defined(AMYBOARD) || defined(AMYBOARD_WEB)
STATIC mp_obj_t tulip_cv_local(size_t n_args, const mp_obj_t *args) {
uint8_t channel = mp_obj_get_int(args[0]);
if(n_args>1) {
cv_local_override[channel] = 1;
cv_local_value[channel] = mp_obj_get_float(args[1]);
} else {
cv_local_override[channel] = 0;
}
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_cv_local_obj, 1, 2, tulip_cv_local);
STATIC mp_obj_t tulip_cv_in(size_t n_args, const mp_obj_t *args) {
uint8_t channel = (n_args > 0) ? mp_obj_get_int(args[0]) : 0;
return mp_obj_new_float(cv_input_hook(channel));
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_cv_in_obj, 0, 1, tulip_cv_in);
#endif
#ifdef AMYBOARD_WEB
uint32_t expand_4bit_oled(uint8_t fourbit) {
if(fourbit==1) return 0xff111111;
if(fourbit==2) return 0xff222222;
if(fourbit==3) return 0xff333333;
if(fourbit==4) return 0xff444444;
if(fourbit==5) return 0xff555555;
if(fourbit==6) return 0xff666666;
if(fourbit==7) return 0xff777777;
if(fourbit==8) return 0xff888888;
if(fourbit==9) return 0xff999999;
if(fourbit==10) return 0xffAAAAAA;
if(fourbit==11) return 0xffBBBBBB;
if(fourbit==12) return 0xffCCCCCC;
if(fourbit==13) return 0xffDDDDDD;
if(fourbit==14) return 0xffEEEEEE;
if(fourbit==15) return 0xffFFFFFF;
return 0xff000000;
}
// Update the web framebuffer if there is one
STATIC mp_obj_t tulip_framebuf_web_update(size_t n_args, const mp_obj_t *args) {
mp_buffer_info_t bufinfo;
mp_get_buffer(args[0], &bufinfo, MP_BUFFER_READ);
uint8_t * p = (uint8_t*)bufinfo.buf;
uint32_t canvas_pixels[256*256];
uint16_t i = 0;
for(uint16_t y=0;y<256;y++) {
for(uint16_t x=0;x<64;x++) {
uint8_t two_pixels = p[(y/2)*64 + x];
uint32_t px0 = expand_4bit_oled((two_pixels >> 4) & 0x000F);
uint32_t px1 = expand_4bit_oled(two_pixels & 0x000F);
canvas_pixels[i++] = px0;
canvas_pixels[i++] = px0;
canvas_pixels[i++] = px1;
canvas_pixels[i++] = px1;
}
}
EM_ASM_({
let canvas = document.getElementById("canvas");
if (!canvas) return;
let context = canvas.getContext('2d');
let imageData = context.createImageData($1, $2);
// .slice() to copy from SharedArrayBuffer into a regular ArrayBuffer
let src = Module.HEAPU8.slice($0, $0 + $1 * $2 * 4);
imageData.data.set(src);
context.putImageData(imageData, 0, 0);
}, canvas_pixels, 256, 256);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_framebuf_web_update_obj, 1, 1, tulip_framebuf_web_update);
#endif
// Just AMYBOARD c code
#ifdef AMYBOARD
extern void run_amy(uint8_t);
STATIC mp_obj_t tulip_amyboard_start(size_t n_args, const mp_obj_t *args) {
uint8_t midi_out_pin = mp_obj_get_int(args[0]);
run_amy(midi_out_pin);
#ifdef AMYBOARD_USB_HOST
// AMY is up; let the USB MIDI host task (usb_host.c) feed it MIDI.
extern uint8_t tulip_ready;
tulip_ready = 1;
#endif
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_amyboard_start_obj, 1, 1, tulip_amyboard_start);
#if defined(AMYBOARD)
// Switch the MIDI OUT TRS standard live (Type A = pin 14, Type B = pin 15). See
// amyboard.set_midi_type() — AMY keeps running; only the UART TX line moves.
extern void amyboard_set_midi_out(uint8_t);
STATIC mp_obj_t tulip_amyboard_set_midi_out(mp_obj_t pin_obj) {
amyboard_set_midi_out((uint8_t)mp_obj_get_int(pin_obj));
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(tulip_amyboard_set_midi_out_obj, tulip_amyboard_set_midi_out);
#endif
STATIC mp_obj_t tulip_bootloader_mode(void) {
#if defined(AMYBOARD) && defined(ESP_PLATFORM)
extern uint32_t amyboard_bootloader_flag;
#define AMYBOARD_BOOTLOADER_MAGIC 0xABCD0001
if (amyboard_bootloader_flag == AMYBOARD_BOOTLOADER_MAGIC) {
amyboard_bootloader_flag = 0; // clear for next boot
return mp_const_true;
}
#endif
return mp_const_false;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(tulip_bootloader_mode_obj, tulip_bootloader_mode);
STATIC mp_obj_t tulip_amyboard_send(size_t n_args, const mp_obj_t *args) {
amy_add_message((char*)mp_obj_str_get_str(args[0]));
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_amyboard_send_obj, 1, 1, tulip_amyboard_send);
#ifdef ESP_PLATFORM
// Background I2C write queue (amyboard_support.c): the OLED drivers enqueue
// their transactions here so display refreshes don't block Python while the
// bytes clock out at 400kHz.
extern uint8_t amyboard_i2c_bg_write(uint8_t addr, const uint8_t *buf, uint32_t len);
extern uint32_t amyboard_i2c_bg_pending(void);
extern uint32_t amyboard_i2c_bg_errors(void);
STATIC mp_obj_t tulip_i2c_bg_write(mp_obj_t addr_obj, mp_obj_t buf_obj) {
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(buf_obj, &bufinfo, MP_BUFFER_READ);
amyboard_i2c_bg_write((uint8_t)mp_obj_get_int(addr_obj), (const uint8_t *)bufinfo.buf, (uint32_t)bufinfo.len);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(tulip_i2c_bg_write_obj, tulip_i2c_bg_write);
STATIC mp_obj_t tulip_i2c_bg_pending(void) {
return mp_obj_new_int(amyboard_i2c_bg_pending());
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(tulip_i2c_bg_pending_obj, tulip_i2c_bg_pending);
STATIC mp_obj_t tulip_i2c_bg_errors(void) {
return mp_obj_new_int(amyboard_i2c_bg_errors());
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(tulip_i2c_bg_errors_obj, tulip_i2c_bg_errors);
#endif // ESP_PLATFORM
#else
#ifndef AMYBOARD_WEB
/// Tulip-with-screen stuff only below
extern void unix_display_set_clock(uint8_t);
extern void display_start();
extern void display_stop();
extern void save_tfb();
extern void restore_tfb();
extern uint8_t tfb_active;
extern uint8_t tfb_font;
STATIC mp_obj_t tulip_display_clock(size_t n_args, const mp_obj_t *args) {
if(n_args==1) {
uint16_t mhz = mp_obj_get_int(args[0]);
display_set_clock(mhz);
return mp_const_none;
}
return mp_obj_new_int(PIXEL_CLOCK_MHZ);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_display_clock_obj, 0, 1, tulip_display_clock);
extern void esp32s3_display_restart();
STATIC mp_obj_t tulip_display_restart(size_t n_args, const mp_obj_t *args) {
#ifdef ESP_PLATFORM
esp32s3_display_restart();
#endif
//display_set_clock(PIXEL_CLOCK_MHZ);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_display_restart_obj, 0, 0, tulip_display_restart);
STATIC mp_obj_t tulip_display_stop(size_t n_args, const mp_obj_t *args) {
display_stop();
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_display_stop_obj, 0, 0, tulip_display_stop);
STATIC mp_obj_t tulip_display_start(size_t n_args, const mp_obj_t *args) {
display_start();
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_display_start_obj, 0, 0, tulip_display_start);
STATIC mp_obj_t tulip_tfb_stop(size_t n_args, const mp_obj_t *args) {
tfb_active = 0;
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_tfb_stop_obj, 0, 0, tulip_tfb_stop);
STATIC mp_obj_t tulip_tfb_start(size_t n_args, const mp_obj_t *args) {
tfb_active = 1;
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_tfb_start_obj, 0, 0, tulip_tfb_start);
STATIC mp_obj_t tulip_gpu_log(size_t n_args, const mp_obj_t *args) {
gpu_log = 1;
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_gpu_log_obj, 0, 0, tulip_gpu_log);
STATIC mp_obj_t tulip_tfb_save(size_t n_args, const mp_obj_t *args) {
save_tfb();
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_tfb_save_obj, 0, 0, tulip_tfb_save);
STATIC mp_obj_t tulip_tfb_restore(size_t n_args, const mp_obj_t *args) {
restore_tfb();
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_tfb_restore_obj, 0, 0, tulip_tfb_restore);
STATIC mp_obj_t tulip_tfb_update(size_t n_args, const mp_obj_t *args) {
display_tfb_update(-1);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_tfb_update_obj, 0, 0, tulip_tfb_update);
// tulip.tfb_font() -> current font number
// tulip.tfb_font(x) -> set font number (0=8x12, 1=portfolio, 2=12x16)
STATIC mp_obj_t tulip_tfb_font(size_t n_args, const mp_obj_t *args) {
if(n_args == 0) {
return mp_obj_new_int(tfb_font);
}
int font_no = mp_obj_get_int(args[0]);
if(font_no < TFB_FONT_8X12 || font_no > TFB_FONT_12X16) {
mp_raise_ValueError(MP_ERROR_TEXT("tfb_font must be 0, 1, or 2"));
}
tfb_font = (uint8_t)font_no;
uint8_t visible_cols = display_tfb_visible_cols();
uint8_t visible_rows = display_tfb_visible_rows();
if(visible_cols == 0) visible_cols = 1;
if(visible_rows == 0) visible_rows = 1;
if(tfb_x_col >= visible_cols) tfb_x_col = visible_cols - 1;
if(tfb_y_row >= visible_rows) tfb_y_row = visible_rows - 1;
display_tfb_update(-1);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_tfb_font_obj, 0, 1, tulip_tfb_font);
// fps = tulip.fps()
STATIC mp_obj_t tulip_fps(size_t n_args, const mp_obj_t *args) {
return mp_obj_new_float_from_f(reported_fps);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_fps_obj, 0, 0, tulip_fps);
// usage = tulip.gpu()
STATIC mp_obj_t tulip_gpu(size_t n_args, const mp_obj_t *args) {
return mp_obj_new_float_from_f(reported_gpu_usage);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_gpu_obj, 0, 0, tulip_gpu);
//tulip.bg_swap()
STATIC mp_obj_t tulip_bg_swap(size_t n_args, const mp_obj_t *args) {
display_swap();
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_bg_swap_obj, 0, 0, tulip_bg_swap);
// tulip.bg_pixel(x,y, pal_idx)
// (r,g,b) = tulip.bg_pixel(x,y)
STATIC mp_obj_t tulip_bg_pixel(size_t n_args, const mp_obj_t *args) {
uint16_t x = mp_obj_get_int(args[0]);
uint16_t y = mp_obj_get_int(args[1]);
if(n_args == 3) { // set
// Set the pixel
uint8_t pal_idx = mp_obj_get_int(args[2]);
display_set_bg_pixel_pal(x,y,pal_idx);
return mp_const_none;
} else { // get the pixel
return mp_obj_new_int( display_get_bg_pixel_pal(x,y));
}
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_bg_pixel_obj, 2, 3, tulip_bg_pixel);
// This version of bg_clear is 2.5x as fast as the old one, as it's just copying within SPIRAM.
// tulip.bg_clear(pal_idx)
// tulip.bg_clear() # uses default
STATIC mp_obj_t tulip_bg_clear(size_t n_args, const mp_obj_t *args) {
uint8_t pal_idx = bg_pal_color;
if(n_args == 1) {
pal_idx = mp_obj_get_int(args[0]);
}
// Set a single pixel
display_set_bg_pixel_pal(0,0,pal_idx);
// Copy that pixel
for (uint16_t j = 0; j < V_RES+OFFSCREEN_Y_PX; j++) {
for (uint16_t i = 0; i < H_RES+OFFSCREEN_X_PX; i++) {
(bg)[(((j*(H_RES+OFFSCREEN_X_PX) + i)*BYTES_PER_PIXEL) + 0)] = (bg)[0];
}
}
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_bg_clear_obj, 0, 1, tulip_bg_clear);
// tulip.bg_bitmap(x, y, w, h, bitmap) --> sets or gets bitmap to fb ram
STATIC mp_obj_t tulip_bg_bitmap(size_t n_args, const mp_obj_t *args) {
uint16_t x = mp_obj_get_int(args[0]);
uint16_t y = mp_obj_get_int(args[1]);
uint16_t w = mp_obj_get_int(args[2]);
uint16_t h = mp_obj_get_int(args[3]);
if(n_args == 5) {
// Set the rect with bitmap pixels
mp_buffer_info_t bufinfo;
if (mp_obj_get_type(args[4]) == &mp_type_bytes) {
mp_get_buffer(args[4], &bufinfo, MP_BUFFER_READ);
display_set_bg_bitmap_raw(x, y, w, h, (uint8_t*)bufinfo.buf);
}
return mp_const_none;
} else {
// return a bitmap
uint8_t bitmap[w*h*BYTES_PER_PIXEL];
display_get_bg_bitmap_raw(x,y,w,h, bitmap);
return mp_obj_new_bytes(bitmap, w*h*BYTES_PER_PIXEL);
}
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_bg_bitmap_obj, 4, 5, tulip_bg_bitmap);
// tulip.bg_blit(x, y, w, h, x1, y1) --> copies bitmap ram
STATIC mp_obj_t tulip_bg_blit(size_t n_args, const mp_obj_t *args) {
uint16_t x = mp_obj_get_int(args[0]);
uint16_t y = mp_obj_get_int(args[1]);
uint16_t w = mp_obj_get_int(args[2]);
uint16_t h = mp_obj_get_int(args[3]);
uint16_t x1 = mp_obj_get_int(args[4]);
uint16_t y1 = mp_obj_get_int(args[5]);
if(n_args > 6) {
display_bg_bitmap_blit_alpha(x,y,w,h,x1,y1);
} else {
display_bg_bitmap_blit(x,y,w,h,x1,y1);
}
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_bg_blit_obj, 6, 7, tulip_bg_blit);
// tulip.bg_png(bytes, x,y)
// tulip.bg_png(filename, x,y)
STATIC mp_obj_t tulip_bg_png(size_t n_args, const mp_obj_t *args) {
unsigned error;
unsigned char* image;
unsigned width, height;
uint16_t x = mp_obj_get_int(args[1]);
uint16_t y = mp_obj_get_int(args[2]);
mp_buffer_info_t bufinfo;
uint8_t file = 0;
if (mp_obj_get_type(args[0]) == &mp_type_bytes) {
mp_get_buffer(args[0], &bufinfo, MP_BUFFER_READ);
} else {
uint32_t fs = file_size(mp_obj_str_get_str(args[0]));
bufinfo.buf = malloc_caps(fs, MALLOC_CAP_SPIRAM);
bufinfo.len = fs;
read_file(mp_obj_str_get_str(args[0]), (uint8_t *)bufinfo.buf, -1, 1);
file = 1;
}
error = lodepng_decode_memory(&image, &width, &height, (uint8_t*)bufinfo.buf, bufinfo.len, LCT_RGBA, 8);
if(error) printf("error %u: %s\n", error, lodepng_error_text(error));
display_set_bg_bitmap_rgba(x,y,width,height,image);
free_caps(image);
if(file) {