-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
ws2812svr.c
2114 lines (1845 loc) · 71 KB
/
ws2812svr.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
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 "ws2812svr.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <math.h>
#include <errno.h>
#include <signal.h>
#include <pthread.h>
#include <ctype.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <sys/types.h>
#include <fcntl.h>
#include "gpio.h"
#include "rpihw.h"
#include "mailbox.h"
//include all effects
#include "master_slave.h"
#include "effects/rotate.h"
#include "effects/rainbow.h"
#include "effects/fill.h"
#include "effects/brightness.h"
#include "effects/fade.h"
#include "effects/blink.h"
#include "effects/gradient.h"
#include "effects/add_random.h"
#include "effects/random_fade_in_out.h"
#include "effects/chaser.h"
#include "effects/color_change.h"
#include "effects/fly_out.h"
#include "effects/fly_in.h"
#include "effects/progress.h"
#include "effects/ambilight.h"
#include "effects/read_jpg.h"
#include "effects/read_png.h"
#include "2D/set_pixel.h"
#include "2D/screenshot.h"
#include "2D/print_text.h"
#include "2D/cls.h"
#include "2D/message_board.h"
#include "2D/rectangle.h"
#include "2D/line.h"
#include "2D/circle.h"
#include "2D/image.h"
#include "2D/init_layer.h"
#include "2D/change_layer.h"
#include "2D/text_input.h"
#include "2D/camera.h"
#include "audio/record.h"
#include "audio/pulses.h"
#include "audio/light_organ.h"
#include "audio/vu_meter.h"
#include "audio/wave.h"
void malloc_command_line(thread_context * context, int size);
thread_context threads[MAX_THREADS+1];
ws2811_t ws2811_ledstring; //led string object for WS2811 and compatible chips (DATA ONLY)
sk9822_t sk9822_ledstring; //led string object for SK9822 chips (CLOCK + DATA)
channel_info led_channels[MAX_CHANNELS];
pthread_mutex_t ws2812_render_mutex; //for ws2812 only one thread should render any channel at the same time!
volatile int debug=0; //set to 1 to enable debug output
volatile int exit_program=0; //set to 1 to exit the program
extern bool enable_system_cmd;
void (* write_to_output_func)(char *) =NULL;
//for easy and fast converting asci hex to integer
char hextable[256] = {
[0 ... 255] = 0, // bit aligned access into this table is considerably
['0'] = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, // faster for most modern processors,
['A'] = 10, 11, 12, 13, 14, 15, // for the space conscious, reduce to
['a'] = 10, 11, 12, 13, 14, 15 // signed char.
};
void set_output_function (void (* output_func)(char *)){
write_to_output_func = output_func;
}
bool init_ws2812svr (){
int i;
srand (time(NULL));
memset(led_channels, 0, sizeof(led_channels));
reset_led_strings();
pthread_mutex_init(&ws2812_render_mutex, NULL);
for (i=0;i<MAX_CHANNELS;i++){
pthread_mutex_init(& led_channels[i].render_mutex, NULL);
}
for (i=0;i<MAX_THREADS;i++){
threads[i].thread=0;
pthread_mutex_init(&threads[i].sync_mutex,NULL);
pthread_cond_init(&threads[i].sync_cond,NULL);
threads[i].end_current_command=0;
threads[i].thread_data=NULL;
threads[i].command_line=NULL;
threads[i].command_line_size=0;
threads[i].write_to_thread_buffer=0;
threads[i].write_to_own_buffer=0;
threads[i].thread_read_index=0;
threads[i].thread_write_index=0;
threads[i].thread_data_size=0;
threads[i].thread_running=0;
threads[i].command_index=0; //current position
threads[i].loop_index=0; //current loop index
threads[i].thread_join_type=JOIN_THREAD_CANCEL;
threads[i].id=i;
threads[i].do_count=0;
threads[i].loop_count=0;
threads[i].write_to_own_buffer=0;
threads[i].execute_main_do_loop=0;
threads[i].ft_init = false;
memset(& threads[i].audio_capture, 0, sizeof(capture_options));
int j;
for(j=0;j<MAX_LOOPS;j++){
threads[i].loops[j].do_pos=0; //positions of 'do' in file loop, max 32 loops
threads[i].loops[j].n_loops=0;
}
}
malloc_command_line(&threads[0],DEFAULT_COMMAND_LINE_SIZE);
}
void terminate_ws2812svr(){
int i;
for (i=0; i<MAX_THREADS;i++){
if (i>0 && threads[i].thread_running){
threads[i].thread_running=0;
threads[i].end_current_command=1;
if (threads[i].waiting_signal){
pthread_cond_signal (&threads[i].sync_cond);
}
pthread_join(threads[i].thread,NULL);
}
if (threads[i].thread_data!=NULL) free(threads[i].thread_data);
if (threads[i].command_line!=NULL) free(threads[i].command_line);
threads[i].thread_data=NULL;
threads[i].command_line=NULL;
pthread_mutex_destroy(&threads[i].sync_mutex);
pthread_cond_destroy(&threads[i].sync_cond);
}
for (i=0;i<MAX_CHANNELS;i++){
pthread_mutex_destroy(& led_channels[i].render_mutex);
}
//if (thread_data!=NULL) free(thread_data);
if (ws2811_ledstring.device!=NULL) ws2811_fini(&ws2811_ledstring);
sk9822_fini(&sk9822_ledstring);
}
//checks if a physical channel_index of channel_type is free or used
bool is_channel_free(int type, int channel_index) {
switch (type) {
case CHANNEL_TYPE_SK9822:
return sk9822_ledstring.channels[channel_index].count == 0;
break;
case CHANNEL_TYPE_WS2811:
return ws2811_ledstring.channel[channel_index].count == 0;
break;
}
}
//returns next free channel index for a physical channel type
//returns -1 if all used
int get_free_channel_index(int type) {
int i = 0;
int count = 0;
switch (type) {
case CHANNEL_TYPE_SK9822:
count = SK9822_MAX_CHANNELS;
break;
case CHANNEL_TYPE_WS2811:
count = RPI_PWM_CHANNELS;
break;
}
for (i = 0; i < count; i++) {
if (is_channel_free(type, i)) return i;
}
return -1;
}
//returns the number of leds in channel_nr
int get_led_count(int channel_nr) {
return led_channels[channel_nr].led_count;
}
//returns number of colors for each LED in a channel
int get_color_size(int channel_nr) {
return led_channels[channel_nr].color_size;
}
//returns led string as led type array, for direct access to leds
ws2811_led_t* get_led_string(int channel_nr) {
return led_channels[channel_nr].ledstring_1D;
}
//returns true if freetype library is initialized
bool init_ft_lib(thread_context* context) {
if (context->ft_init == false) {
FT_Error status;
status = FT_Init_FreeType(& context->ft_lib);
if (status != 0) {
fprintf(stderr, "Error %d opening freetype library.\n", status);
return false;
} else {
context->ft_init = true;
}
}
return context->ft_init;
}
ws2811_led_t*** get_2D_led_string(int channel_nr) {
return led_channels[channel_nr].ledstring_2D;
}
//updates the cairo buffer after changes have been made with 1D functions
//paints what's in the actual led buffers
void cairo_update_buffer(int channel) {
if (led_channels[channel].surface != NULL) { //if channel 2D surface is not NULL we must update buffer first
cairo_surface_flush(led_channels[channel].surface);
unsigned int* pixels = (unsigned int*)cairo_image_surface_get_data(led_channels[channel].surface); //get pointer to pixel data
ws2811_led_t*** led_string_2D = get_2D_led_string(channel);
int width = led_channels[channel].width;
int height = led_channels[channel].height;
unsigned int stride = (unsigned int)led_channels[channel].pixel_stride;
for (unsigned int y = 0;y < led_channels[channel].height;y++) { //update all pixels at the corresponding led
unsigned int* pixel_row = &pixels[y * stride];
for (unsigned int x = 0;x < led_channels[channel].width;x++) {
if (led_string_2D[y][x]) *pixel_row = convert_cairo_color( led_string_2D[y][x]->color);
pixel_row++;
}
}
cairo_surface_mark_dirty(led_channels[channel].surface);
}
}
//paints all the layer surfaces to the main channel surface
//layer 0 is the bottom
//https://cpp.hotexamples.com/examples/-/-/cairo_set_source_surface/cpp-cairo_set_source_surface-function-examples.html
void cairo_paint_layers(channel_info* chan) {
cairo_save(chan->main_cr);
cairo_identity_matrix(chan->main_cr);
for (int i = 0;i < CAIRO_MAX_LAYERS;i++) {
if (chan->layers[i].surface != NULL) {
cairo_set_source_surface(chan->main_cr, chan->layers[i].surface, chan->layers[i].x, chan->layers[i].y);
//if (chan->layers[i].type == LAYER_TYPE_CLIP) {
// cairo_clip(chan->main_cr); //does not work
// cairo_paint(chan->main_cr);
//} else {
cairo_set_operator(chan->main_cr, chan->layers[i].op);
cairo_rectangle(chan->main_cr, 0, 0, chan->width, chan->height);
cairo_fill(chan->main_cr);
//}
}
}
cairo_restore(chan->main_cr);
}
//resets all layers
void cairo_reset_layers(channel_info* chan) {
for (int i = 0; i < CAIRO_MAX_LAYERS;i++) {
if (chan->layers[i].surface != NULL) {
cairo_surface_destroy(chan->layers[i].surface);
chan->layers[i].surface = NULL;
}
if (chan->layers[i].cr != NULL) {
cairo_destroy(chan->layers[i].cr);
chan->layers[i].cr = NULL;
}
chan->layers[i].op = CAIRO_OPERATOR_OVER;
chan->layers[i].type = LAYER_TYPE_NORMAL;
}
}
//converts x and y to create sharp lines in strokes
//https://www.cairographics.org/FAQ/#sharp_lines.
void cairo_stroke_rounding(double width, double * x1, double * y1) {
if (((int)width % 2) != 0) {
*x1 = *x1 + 0.5;
*y1 = *y1 + 0.5;
}
}
//renders a channel
void render_channel(int channel) {
pthread_mutex_lock(&led_channels[channel].render_mutex);
if (led_channels[channel].surface != NULL) { //if channel 2D surface is not NULL we must update buffer first
cairo_paint_layers(&led_channels[channel]);
cairo_surface_flush(led_channels[channel].surface);
unsigned int * pixels = (unsigned int *)cairo_image_surface_get_data(led_channels[channel].surface); //get pointer to pixel data
ws2811_led_t*** led_string_2D = get_2D_led_string(channel);
int width = led_channels[channel].width;
int height = led_channels[channel].height;
unsigned int stride = (unsigned int)led_channels[channel].pixel_stride;
for (unsigned int y = 0;y < led_channels[channel].height;y++) { //update all pixels at the corresponding led
unsigned int * pixel_row = & pixels[y * stride];
for (unsigned int x = 0;x < led_channels[channel].width;x++) {
if (led_string_2D[y][x]) {
led_string_2D[y][x]->color = convert_cairo_color(*pixel_row) & 0xFFFFFF; // ((led_color << 16) | (led_color >> 16) | (led_color & 0xFF00)) & 0xFFFFFF;
}
pixel_row++;
}
}
}
if ((led_channels[channel].flags & CHANNEL_FLAG_SKIP_RENDER)==0){ //skip render to prevent multiple threads to render same time and slow things down (usefull for virtual and slave channels)
switch (led_channels[channel].channel_type) {
case CHANNEL_TYPE_SK9822:
;
sk9822_return_t sk9822_res = sk9822_render_channel(led_channels[channel].sk9822_channel);
if (sk9822_res != SK9822_SUCCESS) {
fprintf(stderr, "sk9822 render failed on channel %d: %s\n", channel, sk9822_get_return_t_str(sk9822_res));
}
break;
case CHANNEL_TYPE_WS2811:
pthread_mutex_lock(&ws2812_render_mutex);
ws2811_return_t ws8211_res = ws2811_render(&ws2811_ledstring, channel);
pthread_mutex_unlock(&ws2812_render_mutex);
if (ws8211_res != WS2811_SUCCESS) {
fprintf(stderr, "ws2811 render failed on channel %d: %s\n", channel, ws2811_get_return_t_str(ws8211_res));
}
break;
case CHANNEL_TYPE_VIRTUAL:
if (led_channels[channel].virtual_channel->parent_channel_index !=-1) {
render_channel(led_channels[channel].virtual_channel->parent_channel_index);
}
break;
case CHANNEL_TYPE_SLAVE:
render_slave_channel(&led_channels[channel]);
break;
}
}
pthread_mutex_unlock(&led_channels[channel].render_mutex);
}
//returns if channel is a valid led_string index number
bool is_valid_channel_number(unsigned int channel) {
return (channel >= 0) && (channel < MAX_CHANNELS) && led_channels[channel].channel_type!=CHANNEL_TYPE_NONE && led_channels[channel].initialized;
}
bool is_valid_2D_channel_number(unsigned int channel) {
return is_valid_channel_number(channel) && led_channels[channel].surface != NULL;
}
channel_info * get_channel(int channel_index){
return &led_channels[channel_index];
}
thread_context * get_thread(int thread_index){
return &threads[thread_index];
}
//writes text to the output depending on the mode (TCP=write to socket)
void write_to_output(char * text){
if (write_to_output_func!=NULL) write_to_output_func(text);
}
unsigned char get_red(int color){
return color & 0xFF;
}
unsigned char get_green(int color){
return (color >> 8) & 0xFF;
}
unsigned char get_blue(int color){
return (color >> 16) & 0xFF;
}
unsigned char get_white(int color){
return (color >> 24) & 0xFF;
}
//returns a color from RGB value
//note that the ws281x stores colors as GRB
int color (unsigned char r, unsigned char g, unsigned char b){
return (b << 16) + (g << 8) + r;
}
//converts cairo pixel to internal LED color OR internal LED color to cairo pixel color
//cairo has different color mapping :( ARGB, led color has ABGR. here we don't use the A
inline unsigned int convert_cairo_color(unsigned int color) {
return ((((color << 16) | (color >> 16) & 0xFF00FF) | (color & 0xFF00)) & 0xFFFFFF) | 0xFF000000;
}
//converts cairo pixel to internal LED color OR internal LED color to cairo pixel color
//cairo has different color mapping :( ARGB, led color has ABGR.
inline unsigned int convert_cairo_color_rgba(unsigned int color) {
return (((color << 16) | (color >> 16) & 0xFF00FF) | (color & 0xFF00)) & 0xFFFFFF | (color & 0xFF000000);
}
//sets cairo_source_rgb using internal color GBR
void set_cairo_color_rgb(cairo_t * cr, unsigned int color) {
unsigned int r, g, b;
r = color & 0xFF;
g = (color >> 8) & 0xFF;
b = (color >> 16) & 0xFF;
cairo_set_source_rgb(cr, ((double)r) / 255.0, ((double)g) / 255.0, ((double)b) / 255.0);
}
void set_cairo_color_rgba(cairo_t* cr, unsigned int color) {
unsigned int r, g, b, a;
r = color & 0xFF;
g = (color >> 8) & 0xFF;
b = (color >> 16) & 0xFF;
a = 255-(color >> 24) & 0xFF;
cairo_set_source_rgba(cr, ((double)r) / 255.0, ((double)g) / 255.0, ((double)b) / 255.0, ((double)a) / 255.0);
}
//returns new colorcomponent based on alpha number for component1 and background component
//all values are unsigned char 0-255
unsigned char alpha_component(unsigned int component, unsigned int bgcomponent, unsigned int alpha){
return component * alpha / 255 + bgcomponent * (255-alpha)/255;
}
//returns a color from RGBW value
//note that the ws281x stores colors as GRB(W)
int color_rgbw (unsigned char r, unsigned char g, unsigned char b, unsigned char w){
return (w<<24)+(b << 16) + (g << 8) + r;
}
//returns a color from a 'color wheel' where wheelpos is the 'angle' 0-255
int deg2color(unsigned char WheelPos) {
if(WheelPos < 85) {
return color(255 - WheelPos * 3,WheelPos * 3 , 0);
} else if(WheelPos < 170) {
WheelPos -= 85;
return color(0, 255 - WheelPos * 3, WheelPos * 3);
} else {
WheelPos -= 170;
return color(WheelPos * 3, 0, 255 - WheelPos * 3);
}
}
//reads key from argument buffer
//example: channel_1_count=10,
//returns channel_1_count in key buffer, then use read_val to read the 10
char * read_key(char * args, char * key, size_t size){
if (args!=NULL && *args!=0){
size--;
if (*args==',') args++;
while (*args!=0 && *args!='=' && *args!=','){
if (*args!=' ' && *args!='\t'){ //skip space
*key=*args; //add character to key value
key++;
size--;
if (size==0) break;
}
args++;
}
*key=0;
}
return args;
}
//read value from command argument buffer (see read_key)
char * read_val(char * args, char * value, size_t size){
bool in_str = false;
bool escaped = false;
if (args!=NULL && *args!=0){
size--;
if (*args==',') args++;
while (*args!=0 && (*args!=',' || in_str)){
if (in_str) { //in string, \ acts as an escape character
if (escaped) { //tells the current character is an escaped character
if (size > 0) {
switch (*args) {
case '"':
*value = '"';
break;
case '\\':
*value = '\\';
break;
case 't':
*value = '\t';
break;
case 'n':
*value = '\n';
break;
default:
*value = *args;
}
value++;
size--;
}
escaped = false;
} else {
if (*args == '\\') { //next character is escaped
escaped = true;
} else if (*args == '"'){ //end of quoted string
in_str = false;
} else if (size > 0) { //add character
*value = *args;
value++;
size--;
}
}
} else {
if (*args != ' ' && *args != '\t') { //skip space
if (*args == '"') { //start of string, skip character
in_str = true;
escaped = false;
} else if (size > 0) { //add normal character
*value = *args;
value++;
size--;
}
}
}
args++;
}
*value=0;
}
return args;
}
//reads integer from command argument buffer
char * read_int(char * args, int * value){
char svalue[MAX_VAL_LEN];
if (args!=NULL && *args!=0){
args = read_val(args, svalue, MAX_VAL_LEN);
if (svalue[0]) *value = atoi(svalue);
}
return args;
}
char * read_float(char * args, float * value){
char svalue[MAX_VAL_LEN];
if (args!=NULL && *args!=0){
args = read_val(args, svalue, MAX_VAL_LEN);
if (svalue[0]) *value = atof(svalue);
}
return args;
}
char* read_double(char* args, double* value) {
char svalue[MAX_VAL_LEN];
if (args != NULL && *args != 0) {
args = read_val(args, svalue, MAX_VAL_LEN);
if (svalue[0]) *value = atof(svalue);
}
return args;
}
char * read_str(char * args, char * dst, size_t size){
return read_val(args, dst, size);
}
//reads unsigned integer from command argument buffer
char * read_uint(char * args, unsigned int * value){
char svalue[MAX_VAL_LEN];
if (args!=NULL && *args!=0){
args = read_val(args, svalue, MAX_VAL_LEN);
if (svalue[0]) *value = (unsigned int) (strtoul(svalue,NULL, 10) & 0xFFFFFFFF);
}
return args;
}
//reads channel number from command line
//automatically adjusts the channel number to 0 based index
char * read_channel(char * args, int * value){
if (args!=NULL && *args!=0){
args = read_int(args, value);
(*value)--;
}
return args;
}
//reads color from string, returns string + 6 or 8 characters
//!! args must be hex format only: XXXXXX, USE read_color_arg to read from command line!
//color_size = 3 (RGB format) or 4 (RGBW format)
char * read_color(char * args, unsigned int * out_color, unsigned int color_size){
unsigned char r,g,b,w;
unsigned char color_string[8];
unsigned int color_string_idx=0;
if (args!=NULL && *args!=0){
//*out_color = 0;
while (*args!=0 && *args!=',' && color_string_idx<color_size*2){
if (*args!=' ' && *args!='\t'){ //skip space
color_string[color_string_idx]=*args;
color_string_idx++;
}
args++;
}
r = (hextable[color_string[0]]<<4) + hextable[color_string[1]];
g = (hextable[color_string[2]]<<4) + hextable[color_string[3]];
b = (hextable[color_string[4]]<<4) + hextable[color_string[5]];
if (color_size==4){
w = (hextable[color_string[6]]<<4) + hextable[color_string[7]];
if (color_string_idx) *out_color = color_rgbw(r,g,b,w);
}else{
if (color_string_idx) *out_color = color(r,g,b);
}
}
return args;
}
char * read_color_arg(char * args, unsigned int * out_color, unsigned int color_size){
char value[MAX_VAL_LEN];
args = read_val(args, value, MAX_VAL_LEN);
if (*value!=0) read_color(value, out_color, color_size);
return args;
}
char * read_color_arg_empty(char* args, unsigned int* out_color, unsigned int color_size, bool * arg_empty) {
char value[MAX_VAL_LEN];
args = read_val(args, value, MAX_VAL_LEN);
if (*value != 0) {
read_color(value, out_color, color_size);
*arg_empty = false;
} else {
*arg_empty = true;
}
return args;
}
//reads a hex brightness value
char * read_brightness(char * args, unsigned int * brightness){
return read_uint(args, brightness);
/*unsigned int idx=0;
unsigned char str_brightness[2];
if (args!=NULL && *args!=0){
while (*args!=0 && idx<2){
if (*args!=' ' && *args!='\t'){ //skip space
brightness[idx]=*args;
idx++;
}
args++;
}
if (idx!=0) * brightness = (hextable[str_brightness[0]] << 4) + hextable[str_brightness[1]];
}
return args;*/
}
char * read_operation(char * args, char * op){
char value[MAX_VAL_LEN];
if (args!=NULL && *args!=0){
args = read_val(args, value, MAX_VAL_LEN);
if (strcmp(value, "OR")==0) *op=OP_OR;
else if (strcmp(value, "AND")==0) *op=OP_AND;
else if (strcmp(value, "XOR")==0) *op=OP_XOR;
else if (strcmp(value, "NOT")==0) *op=OP_NOT;
else if (strcmp(value, "=")==0) *op=OP_EQUAL;
}
return args;
}
//returns time stamp in ms
unsigned long long time_ms(){
struct timeval tp;
gettimeofday(&tp, NULL);
return tp.tv_sec * 1000 + tp.tv_usec / 1000;
}
//expands thread command buffer (increase size by 2 times)
//thread_context is the context that needs memory expansion
void expand_thread_data_buffer(thread_context * context){
if (context->thread_data_size==0){
context->thread_data_size=DEFAULT_BUFFER_SIZE;
context->thread_data = (char *) malloc(context->thread_data_size);
}else{
context->thread_data_size = context->thread_data_size * 2;
char * tmp_buffer = (char *) malloc(context->thread_data_size);
memcpy((void*) tmp_buffer, (void*)context->thread_data, context->thread_data_size);
free(context->thread_data);
context->thread_data = tmp_buffer;
}
}
//adds data to the thread buffer
//thread_context is the thread to write to
void write_thread_buffer (thread_context * context, char c){
if (context->thread_data_size==0) expand_thread_data_buffer(context);
context->thread_data[context->thread_write_index] = c;
context->thread_write_index++;
if (context->thread_write_index==context->thread_data_size) expand_thread_data_buffer(context);
context->thread_data[context->thread_write_index]=0;
}
//initializes channels
//depending on channel chip type it will initialize the proper hardware for all channels
//init <frequency>,<DMA>
void init_channels(thread_context* context, char* args) {
int i, parent_index;
char value[MAX_VAL_LEN];
int frequency = WS2811_TARGET_FREQ, dma = 10;
args = read_int(args, &frequency);
args = read_int(args, &dma);
bool use_ws2812 = false;
bool use_sk9822 = false;
ws2811_return_t ws2811_ret;
sk9822_return_t sk9822_ret;
//check which interface to init
for (i = 0;i < MAX_CHANNELS; i++) {
led_channels[i].initialized = false;
switch (led_channels[i].channel_type) {
case CHANNEL_TYPE_WS2811:
use_ws2812 = true;
break;
case CHANNEL_TYPE_SK9822:
use_sk9822 = true;
break;
}
}
//init sk9822
if (use_sk9822) {
sk9822_fini(&sk9822_ledstring);
if (debug) printf("Init SPI\n");
if ((sk9822_ret = sk9822_init(&sk9822_ledstring)) != SK9822_SUCCESS) {
fprintf(stderr, "sk9822 init SPI failed: %d, %s\n", sk9822_ret, sk9822_get_return_t_str(sk9822_ret));
}
}
//init ws2812
if (use_ws2812) {
if (ws2811_ledstring.device != NULL) ws2811_fini(&ws2811_ledstring);
ws2811_ledstring.dmanum = dma;
ws2811_ledstring.freq = frequency;
if (debug) printf("Init ws2811 %d,%d\n", frequency, dma);
if ((ws2811_ret = ws2811_init(&ws2811_ledstring)) != WS2811_SUCCESS) {
fprintf(stderr, "ws2811_init failed: %s\n", ws2811_get_return_t_str(ws2811_ret));
}
}
//assign the initialized channels to the global channel array depending on type
for (i = 0;i < MAX_CHANNELS; i++) {
led_channels[i].ledstring_1D = NULL;
led_channels[i].width = 0;
led_channels[i].height = 0;
if (led_channels[i].ledstring_2D != NULL) {
for (int k = 0;k < led_channels[i].height;k++) {
free(led_channels[i].ledstring_2D[k]);
}
free(led_channels[i].ledstring_2D);
}
/*for (int k = 0;k < CAIRO_MAX_LAYERS;k++) {
if (led_channels[i].layers[k].cr != NULL) cairo_destroy(led_channels[i].layers[k].cr);
if (led_channels[i].layers[k].surface != NULL) cairo_surface_destroy(led_channels[i].layers[k].surface);
led_channels[i].layers[k].x = 0;
led_channels[i].layers[k].y = 0;
}*/
if (led_channels[i].main_cr !=NULL) cairo_destroy(led_channels[i].main_cr);
if (led_channels[i].surface != NULL) cairo_surface_destroy(led_channels[i].surface);
if (led_channels[i].surface_data != NULL) free(led_channels[i].surface_data);
if (led_channels[i].channel_type != CHANNEL_TYPE_NONE) {
switch (led_channels[i].channel_type) {
case CHANNEL_TYPE_WS2811:
led_channels[i].initialized = ws2811_ret == WS2811_SUCCESS;
led_channels[i].ws2811_channel = &ws2811_ledstring.channel[led_channels[i].channel_index];
led_channels[i].ledstring_1D = led_channels[i].ws2811_channel->leds;
if (debug) printf("Init WS2811 for channel: %d, res=%d.\n", i, led_channels[i].initialized ? 1 : 0);
break;
case CHANNEL_TYPE_SK9822:
led_channels[i].initialized = sk9822_ret == SK9822_SUCCESS;
led_channels[i].sk9822_channel = &sk9822_ledstring.channels[led_channels[i].channel_index];
led_channels[i].ledstring_1D = (ws2811_led_t*)led_channels[i].sk9822_channel->leds;
if (debug) printf("Init SK9822 for channel: %d, res=%d.\n", i, led_channels[i].initialized ? 1 : 0);
break;
case CHANNEL_TYPE_VIRTUAL:
parent_index = led_channels[i].virtual_channel->parent_channel_index;
led_channels[i].initialized = led_channels[parent_index].initialized;
led_channels[i].ledstring_1D = get_led_string(parent_index) + led_channels[i].virtual_channel->parent_offset;
led_channels[i].virtual_channel->count = led_channels[i].led_count;
if (debug) printf("Init virtual channel: %d, parent channel %d, offset %d.\n", i, parent_index, led_channels[i].virtual_channel->parent_offset);
break;
case CHANNEL_TYPE_SLAVE:
if (led_channels[i].ledstring_1D!=NULL) free(led_channels[i].ledstring_1D);
led_channels[i].ledstring_1D = (ws2811_led_t*) malloc(sizeof(ws2811_led_t)*led_channels[i].led_count);
memset(led_channels[i].ledstring_1D, 0, sizeof(ws2811_led_t) * led_channels[i].led_count);
connect_slave_channel(&led_channels[i]);
led_channels[i].virtual_channel->count = led_channels[i].led_count;
led_channels[i].virtual_channel->wait_for_packet = false;
led_channels[i].initialized = true;
if(debug) printf("Init slave channel: %s:%d color_size:%d\n",led_channels[i].virtual_channel->remote_address, led_channels[i].virtual_channel->remote_port, led_channels[i].virtual_channel->color_size);
}
led_channels[i].ledstring_2D = NULL;
led_channels[i].surface = NULL;
led_channels[i].surface_data = NULL;
led_channels[i].cr = NULL;
led_channels[i].main_cr = NULL;
//initialize the default brightness of each led
ws2811_led_t* leds = get_led_string(i);
if (leds != NULL) {
int count = get_led_count(i);
for (int j = 0;j < count ;j++) {
leds[j].brightness = 0xFF;
}
}
}
}
}
//initialize 2D channel
//config_2D <channel>,<width>,<height>,<panel_type>,<panel_size_x>,<panel_size_y>,<start_led>,<map_file>
void config_2D(thread_context* context, char* args) {
int channel = 0;
int width = 0, height = 0, panel_type = 0, panel_size_x = 0, panel_size_y = 0, len=0, start_led=0;
char filename[MAX_VAL_LEN];
if (is_valid_channel_number(channel)) {
len = get_led_count(channel);
}
args = read_channel(args, &channel);
args = read_int(args, &width);
args = read_int(args, &height);
args = read_int(args, &panel_type);
args = read_int(args, &panel_size_x);
args = read_int(args, &panel_size_y);
args = read_int(args, &start_led);
args = read_str(args, filename, sizeof(filename));
if (is_valid_channel_number(channel)) {
len = get_led_count(channel);
if (len < width * height + start_led) {
fprintf(stderr, "Error: width * height + start_led must be at least the length of the led string.\n");
return;
}
if (panel_size_x == 0) panel_size_x = width;
if (panel_size_y == 0) panel_size_y = height;
if ((width % panel_size_x) != 0) {
fprintf(stderr, "Error: width must be a multiple of panel_size_x.\n");
return;
}
if ((height % panel_size_y) != 0) {
fprintf(stderr, "Error: height must be multiple of panel_size_y.\n");
return;
}
int panels_x = width / panel_size_x;
int panels_y = height / panel_size_y;
channel_info* led_channel = &led_channels[channel];
ws2811_led_t*** ledstring_2D;
ws2811_led_t* ledstring_1D;
if (led_channel->ledstring_2D != NULL) {
for (int k = 0;k < led_channel->height;k++) {
free(led_channel->ledstring_2D[k]);
}
free(led_channel->ledstring_2D);
}
if (led_channel->cr != NULL) cairo_destroy(led_channel->cr);
if (led_channel->surface != NULL) cairo_surface_destroy(led_channel->surface);
if (led_channel->surface_data != NULL) free(led_channel->surface_data);
led_channel->width = width;
led_channel->height = height;
ledstring_1D = get_led_string(channel);
ledstring_1D += start_led; //move to starting position
ledstring_2D = (ws2811_led_t***)malloc(height * sizeof(ws2811_led_t**));
for (int i = 0; i < height;i++) {
ledstring_2D[i] = (ws2811_led_t**)malloc(width * sizeof(ws2811_led_t*));
memset(ledstring_2D[i], 0, width * sizeof(ws2811_led_t*));
}
led_channel->ledstring_2D = ledstring_2D;
int x, y, offset;
switch (panel_type) {
case 0: //normal type, row by row
offset = 0;
for (y = 0;y < height;y++) {
for (x = 0;x < width; x++) {
ledstring_2D[y][x] = &ledstring_1D[offset];
offset++;
}
}
break;
case 1: //normal type, row by row but odd rows are reverse connected meaning leds connected from right to left, display starts at left top
offset = 0;
for (y = 0;y < height;y++) {
for (x = 0;x < width; x++) {
if ((y % 2) == 0) {
ledstring_2D[y][x] = &ledstring_1D[offset];
} else {
ledstring_2D[y][width - x - 1] = &ledstring_1D[offset];
}
offset++;
}
}
break;
case 2: //panels with vertical rows for each panel starting left top of each panel row
for (y = 0;y < height;y++) {
for (x = 0;x < width; x++) {
offset = (y / panel_size_y) * panel_size_y * width;
offset += x * panel_size_y;
if ((x & 1) != 0) offset += (panel_size_y - (y % panel_size_y) - 1);
else offset += (y % panel_size_y);
ledstring_2D[y][x] = &ledstring_1D[offset];
}
}
break;
case 3: //panels with individual vertical rows starting left top of each panel row but odd panel rows start from the right bottom.
for (y = 0;y < height;y++) {
for (x = 0;x < width; x++) {
int panel_row_index = y / panel_size_y;
offset = panel_row_index * panel_size_y * width;
if ((panel_row_index % 2) == 0) { //even panel row same as in case 1
offset += x * panel_size_y;
if ((x & 1) != 0) offset += (panel_size_y - (y % panel_size_y) - 1);
else offset += (y % panel_size_y);
ledstring_2D[y][x] = &ledstring_1D[offset];
} else { //odd panel rows, start from the right top, complicated but easy to connect multiple panels :) ...
offset += (width - x - 1) * panel_size_y;
if ((x & 1) == 0) offset += (y % panel_size_y);
else offset += (panel_size_y - (y % panel_size_y) - 1);
ledstring_2D[y][x] = &ledstring_1D[offset];
}
}
}
break;
case 4: //custom map from file, file format: X, Y = LED_INDEX \n
;
FILE * file = fopen(filename, "r");
if (debug) printf("Reading led map file %s\n", filename);
if (file == NULL) {
perror("Error loading map file ");
return;
}
x = 0;
y = 0;
char line[1024];
while (!feof(file)) {
int offset = 0;
fscanf(file, "%d %d %d", &x, &y, &offset);
if ((x >= 0 && x < width) && (y >= 0 && y < height)) {
ledstring_2D[y][x] = &ledstring_1D[offset];
}
}
fclose(file);
break;
default:
fprintf(stderr, "Error invalid panel_type\n");
break;
}
led_channel->surface_stride = cairo_format_stride_for_width(CAIRO_FORMAT_RGB24, width);
led_channel->pixel_stride = led_channel->surface_stride / sizeof(unsigned int);
led_channel->surface_data = (unsigned char*)malloc(led_channel->surface_stride * height * sizeof(unsigned char));
led_channel->surface = cairo_image_surface_create_for_data(led_channel->surface_data, CAIRO_FORMAT_RGB24, width , height, led_channel->surface_stride);