forked from tom-2015/rpi-ws2812-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
executable file
·1283 lines (1157 loc) · 43.9 KB
/
main.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 <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <signal.h>
#include <pthread.h>
#include <ctype.h>
#include "ws2811.h"
#define DEFAULT_DEVICE_FILE "/dev/ws281x"
#define DEFAULT_COMMAND_LINE_SIZE 1024
#define DEFAULT_BUFFER_SIZE 32768
#define MAX_KEY_LEN 255
#define MAX_VAL_LEN 255
#define MAX_LOOPS 32
#define MODE_STDIN 0
#define MODE_NAMED_PIPE 1
#define MODE_FILE 2
#define MODE_TCP 3
//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 init_hextable(){
unsigned int i;
for(i=0;i<256;i++){
if (i >= '0' && i<= '9') hextable[i]=i-'0';
if (i >= 'a' && i<= 'f') hextable[i]=i-'a'+10;
if (i >= 'A' && i<= 'F') hextable[i]=i-'A'+10;
}
}*/
typedef struct {
int do_pos;
int n_loops;
} do_loop;
FILE * input_file; //the named pipe handle
char * command_line; //current command line
char * named_pipe_file; //holds named pipe file name
int command_index; //current position
int command_line_size; //max bytes in command line
int exit_program=0; //set to 1 to exit the program
int mode; //mode we operate in (TCP, named pipe, file, stdin)
do_loop loops[MAX_LOOPS]={0}; //positions of 'do' in file loop, max 32 recursive loops
int loop_index=0; //current loop index
int debug=0; //set to 1 to enable debug output
//for TCP mode
int sockfd; //socket that listens
int active_socket; //current active connection socket
socklen_t clilen;
struct sockaddr_in serv_addr, cli_addr;
//for TCP/IP multithreading
char * thread_data=NULL; //holds command to execute in separate thread (TCP/IP only)
int thread_read_index=0; //read position
int thread_write_index=0; //write position
int thread_data_size=0; //buffer size
volatile int thread_running=0; //becomes 1 there is a thread running
int write_to_thread_buffer=0; //becomes 1 if we need to write to thread buffer
int start_thread=0; //becomes 1 after the thread_stop command and tells the program to start the thread on disconnect of the TCP/IP connection
//pthread_mutex_t mutex_fifo_queue;
pthread_t thread; //a thread that will repeat code after client closed connection
ws2811_t ledstring;
void process_character(char c);
//handles exit of program with CTRL+C
static void ctrl_c_handler(int signum){
exit_program=1;
}
static void setup_handlers(void){
struct sigaction sa;
sa.sa_handler = ctrl_c_handler,
sigaction(SIGKILL, &sa, NULL);
}
//allocates memory for command line
void malloc_command_line(int size){
if (command_line!=NULL) free(command_line);
command_line = (char *) malloc(size+1);
command_line_size = size;
command_index = 0;
}
//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;
}
//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);
}
}
//returns if channel is a valid led_string index number
int is_valid_channel_number(unsigned int channel){
return (channel >= 0) && (channel < RPI_PWM_CHANNELS) && ledstring.channel[channel].count>0 && ledstring.device!=NULL;
}
//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){
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){
size--;
if (*args==',') args++;
while (*args!=0 && *args!=','){
if (*args!=' ' && *args!='\t'){ //skip space
*value=*args;
value++;
size--;
if (size==0) break;
}
args++;
}
*value=0;
return args;
}
//reads color from string, returns string + 6 or 8 characters
//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;
*out_color = 0;
while (*args!=0 && 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]];
*out_color = color_rgbw(r,g,b,w);
}else{
*out_color = color(r,g,b);
}
return args;
}
//reads a hex brightness value
char * read_brightness(char * args, unsigned int * brightness){
unsigned int idx=0;
unsigned char str_brightness[2];
*brightness=0;
while (*args!=0 && idx<2){
if (*args!=' ' && *args!='\t'){ //skip space
brightness[idx]=*args;
idx++;
}
args++;
}
* brightness = (hextable[str_brightness[0]] << 4) + hextable[str_brightness[1]];
return args;
}
//initializes channels
//init <frequency>,<DMA>
void init_channels(char * args){
char value[MAX_VAL_LEN];
int frequency=WS2811_TARGET_FREQ, dma=5;
if (ledstring.device!=NULL) ws2811_fini(&ledstring);
if (args!=NULL){
args = read_val(args, value, MAX_VAL_LEN);
frequency=atoi(value);
if (*args!=0){
args = read_val(args, value, MAX_VAL_LEN);
dma=atoi(value);
}
}
ledstring.dmanum=dma;
ledstring.freq=frequency;
if (debug) printf("Init ws2811 %d,%d\n", frequency, dma);
ws2811_return_t ret;
if ((ret = ws2811_init(&ledstring))!= WS2811_SUCCESS){
fprintf(stderr, "ws2811_init failed: %s\n", ws2811_get_return_t_str(ret));
}
}
//changes the global channel brightness
//global_brightness <channel>,<value>
void global_brightness(char * args){
if (args!=NULL){
char value[MAX_VAL_LEN];
args = read_val(args, value, MAX_VAL_LEN);
int channel = atoi(value)-1;
if (*args!=0){
args = read_val(args, value, MAX_VAL_LEN);
int brightness = atoi(value);
if (is_valid_channel_number(channel)){
ledstring.channel[channel].brightness=brightness;
if(debug) printf("Global brightness %d, %d\n", channel, brightness);
}else{
fprintf(stderr,"Invalid channel number, did you call setup and init?\n");
}
}
}
}
//sets the ws2811 channels
//setup channel, led_count, type, invert, global_brightness, GPIO
void setup_ledstring(char * args){
char value[MAX_VAL_LEN];
int channel=0, led_count=10, type=0, invert=0, brightness=255, GPIO=18;
const int led_types[]={WS2811_STRIP_RGB, //0
WS2811_STRIP_RBG, //1
WS2811_STRIP_GRB, //2
WS2811_STRIP_GBR, //3
WS2811_STRIP_BRG, //4
WS2811_STRIP_BGR, //5
SK6812_STRIP_RGBW,//6
SK6812_STRIP_RBGW,//7
SK6812_STRIP_GRBW,//8
SK6812_STRIP_GBRW,//9
SK6812_STRIP_BRGW,//10
SK6812_STRIP_BGRW //11
};
if (args!=NULL){
args = read_val(args, value, MAX_VAL_LEN);
channel = atoi(value)-1;
if (channel==2) GPIO=13;
if (*args!=0){
args = read_val(args, value, MAX_VAL_LEN);
led_count = atoi(value);
if (*args!=0){
args = read_val(args, value, MAX_VAL_LEN);
type = atoi(value);
if (*args!=0){
args = read_val(args, value, MAX_VAL_LEN);
invert = atoi(value);
if (*args!=0){
args = read_val(args, value, MAX_KEY_LEN);
brightness = atoi(value);
if (*args!=0){
args = read_val(args, value, MAX_KEY_LEN);
GPIO = atoi(value);
}
}
}
}
}
}
if (channel >=0 && channel < RPI_PWM_CHANNELS){
if (debug) printf("Initialize channel %d,%d,%d,%d,%d,%d\n", channel, led_count, type, invert, brightness, GPIO);
int color_size = 4;
switch (type){
case WS2811_STRIP_RGB:
case WS2811_STRIP_RBG:
case WS2811_STRIP_GRB:
case WS2811_STRIP_GBR:
case WS2811_STRIP_BRG:
case WS2811_STRIP_BGR:
color_size=3;
break;
}
ledstring.channel[channel].gpionum = GPIO;
ledstring.channel[channel].invert = invert;
ledstring.channel[channel].count = led_count;
ledstring.channel[channel].strip_type=led_types[type];
ledstring.channel[channel].brightness=brightness;
ledstring.channel[channel].color_size=color_size;
int max_size=0,i;
for (i=0; i<RPI_PWM_CHANNELS;i++){
int color_count=4;
switch (ledstring.channel[i].strip_type){
case WS2811_STRIP_RGB:
case WS2811_STRIP_RBG:
case WS2811_STRIP_GRB:
case WS2811_STRIP_GBR:
case WS2811_STRIP_BRG:
case WS2811_STRIP_BGR:
color_count=3;
break;
}
int size = DEFAULT_COMMAND_LINE_SIZE + ledstring.channel[i].count * 2 * color_count;
if (size > max_size){
max_size = size;
}
}
malloc_command_line(max_size); //allocate memory for full render data
}else{
if (debug) printf("Channel number %d\n", channel);
fprintf(stderr,"Invalid channel number, use channels <number> to initialize total channels you want to use.\n");
}
}
//prints channel settings
void print_settings(){
unsigned int i;
printf("DMA Freq: %d\n", ledstring.freq);
printf("DMA Num: %d\n", ledstring.dmanum);
for (i=0;i<RPI_PWM_CHANNELS;i++){
printf("Channel %d:\n", i+1);
printf(" GPIO: %d\n", ledstring.channel[i].gpionum);
printf(" Invert: %d\n",ledstring.channel[i].invert);
printf(" Count: %d\n",ledstring.channel[i].count);
printf(" Colors: %d\n", ledstring.channel[i].color_size);
printf(" Type: %d\n", ledstring.channel[i].strip_type);
}
}
//sends the buffer to the leds
//render <channel>,0,AABBCCDDEEFF...
//optional the colors for leds:
//AABBCC are RGB colors for first led
//DDEEFF is RGB for second led,...
void render(char * args){
int channel=0;
int r,g,b,w;
int size;
int start;
char value[MAX_VAL_LEN];
char color_string[6];
if (debug) printf("Render %s\n", args);
if (args!=NULL){
args = read_val(args, value, MAX_VAL_LEN);
channel = atoi(value)-1;
if (is_valid_channel_number(channel)){
if (*args!=0){
args = read_val(args, value, MAX_VAL_LEN); //read start position
start = atoi(value);
while (*args!=0 && (*args==' ' || *args==',')) args++; //skip white space
if (debug) printf("Render channel %d selected start at %d leds %d\n", channel, start, ledstring.channel[channel].count);
size = strlen(args);
int led_count = ledstring.channel[channel].count;
int led_index = start % led_count;
int color_count = ledstring.channel[channel].color_size;
ws2811_led_t * leds = ledstring.channel[channel].leds;
while (*args!=0){
unsigned int color=0;
args = read_color(args, & color, color_count);
leds[led_index].color = color;
led_index++;
if (led_index>=led_count) led_index=0;
}
}
}else{
fprintf(stderr,"Invalid channel number, did you call setup and init?\n");
}
}
ws2811_render(&ledstring);
}
//shifts all colors 1 position
//rotate <channel>,<places>,<direction>,<new_color>,<new_brightness>
//if new color is set then the last led will have this color instead of the color of the first led
void rotate(char * args){
char value[MAX_VAL_LEN];
int channel=0, nplaces=1, direction=1;
unsigned int new_color=0, new_brightness=255;
int use_new_color=0;
if (args!=NULL){
args = read_val(args, value, MAX_VAL_LEN);
channel = atoi(value)-1;
if (*args!=0){
args = read_val(args, value, MAX_VAL_LEN);
nplaces = atoi(value);
if (*args!=0){
args = read_val(args, value, MAX_VAL_LEN);
direction = atoi(value);
if (*args!=0){
args = read_val(args, value, MAX_VAL_LEN);
if (strlen(value)>=6){
if (is_valid_channel_number(channel)){
read_color(value, & new_color, ledstring.channel[channel].color_size);
use_new_color=1;
args = read_val(args, value, MAX_VAL_LEN);
if (strlen(value)==2) read_brightness(value, & new_brightness);
}
}
}
}
}
}
if (debug) printf("Rotate %d %d %d %d\n", channel, nplaces, direction, new_color);
if (is_valid_channel_number(channel)){
ws2811_led_t tmp_led;
ws2811_led_t * leds = ledstring.channel[channel].leds;
unsigned int led_count = ledstring.channel[channel].count;
unsigned int n,i;
for(n=0;n<nplaces;n++){
if (direction==1){
tmp_led = leds[0];
for(i=1;i<led_count;i++){
leds[i-1] = leds[i];
}
if (use_new_color){
leds[led_count-1].color=new_color;
leds[led_count-1].brightness=new_brightness;
}else{
leds[led_count-1]=tmp_led;
}
}else{
tmp_led = leds[led_count-1];
for(i=led_count-1;i>0;i--){
leds[i] = leds[i-1];
}
if (use_new_color){
leds[0].color=new_color;
leds[0].brightness=new_brightness;
}else{
leds[0]=tmp_led;
}
}
}
}else{
fprintf(stderr,"Invalid channel number, did you call setup and init?\n");
}
}
//fills pixels with rainbow effect
//count tells how many rainbows you want
//rainbow <channel>,<count>,<startcolor>,<stopcolor>,<start>,<len>
//start and stop = color values on color wheel (0-255)
void rainbow(char * args) {
char value[MAX_VAL_LEN];
int channel=0, count=1,start=0,stop=255,startled=0, len=0;
if (is_valid_channel_number(channel)) len=ledstring.channel[channel].count;
if (args!=NULL){
args = read_val(args, value, MAX_VAL_LEN);
channel = atoi(value)-1;
if (is_valid_channel_number(channel)) len=ledstring.channel[channel].count;;
if (*args!=0){
args = read_val(args, value, MAX_VAL_LEN);
count = atoi(value);
if (*args!=0){
args = read_val(args, value, MAX_VAL_LEN);
start = atoi(value);
if (*args!=0){
args = read_val(args, value, MAX_VAL_LEN);
stop = atoi(value);
if (*args!=0){
args = read_val(args, value, MAX_VAL_LEN);
startled=atoi(value);
if(*args!=0){
args = read_val(args, value, MAX_VAL_LEN);
len=atoi(value);
}
}
}
}
}
}
if (is_valid_channel_number(channel)){
if (start<0 || start > 255) start=0;
if (stop<0 || stop > 255) stop = 255;
if (startled<0) startled=0;
if (startled+len> ledstring.channel[channel].count) len = ledstring.channel[channel].count-startled;
if (debug) printf("Rainbow %d,%d,%d,%d,%d,%d,%d\n", channel, count,start,stop,startled,len);
int numPixels = len; //ledstring.channel[channel].count;;
int i, j;
ws2811_led_t * leds = ledstring.channel[channel].leds;
for(i=0; i<numPixels; i++) {
leds[startled+i].color = deg2color(abs(stop-start) * i * count / numPixels + start);
}
}else{
fprintf(stderr,"Invalid channel number, did you call setup and init?\n");
}
}
//fills leds with certain color
//fill <channel>,<color>,<start>,<len>,<OR,AND,XOR,NOT,=>
void fill(char * args){
char value[MAX_VAL_LEN];
char op=0;
int channel=0,start=0,len=-1;
unsigned int fill_color=0;
if (args!=NULL){
args = read_val(args, value, MAX_VAL_LEN);
channel = atoi(value)-1;
if (*args!=0){
args = read_val(args, value, MAX_VAL_LEN);
if (strlen(value)>=6){
if (is_valid_channel_number(channel)) read_color(value, & fill_color, ledstring.channel[channel].color_size);
}else{
printf("Invalid color\n");
}
if (*args!=0){
args = read_val(args, value, MAX_VAL_LEN);
start = atoi(value);
if (*args!=0){
args = read_val(args, value, MAX_VAL_LEN);
len = atoi(value);
if (*args!=0){
args = read_val(args, value, MAX_VAL_LEN);
if (strcmp(value, "OR")==0) op=1;
else if (strcmp(value, "AND")==0) op=2;
else if (strcmp(value, "XOR")==0) op=3;
else if (strcmp(value, "NOT")==0) op=4;
else if (strcmp(value, "=")==0) op=0;
}
}
}
}
}
if (is_valid_channel_number(channel)){
if (start<0 || start>=ledstring.channel[channel].count) start=0;
if (len<=0 || (start+len)>ledstring.channel[channel].count) len=ledstring.channel[channel].count-start;
if (debug) printf("fill %d,%d,%d,%d,%d\n", channel, fill_color, start, len,op);
ws2811_led_t * leds = ledstring.channel[channel].leds;
unsigned int i;
for (i=start;i<start+len;i++){
switch (op){
case 0:
leds[i].color=fill_color;
break;
case 1:
leds[i].color|=fill_color;
break;
case 2:
leds[i].color&=fill_color;
break;
case 3:
leds[i].color^=fill_color;
break;
case 4:
leds[i].color=~leds[i].color;
break;
}
}
}else{
fprintf(stderr,"Invalid channel number, did you call setup and init?\n");
}
}
//dims leds
//brightness <channel>,<brightness>,<start>,<len> (brightness: 0-255)
void brightness(char * args){
char value[MAX_VAL_LEN];
int channel=0, brightness=255;
unsigned int start=0, len=0;
if (is_valid_channel_number(channel)){
len = ledstring.channel[channel].count;;
}
if (args!=NULL){
args = read_val(args, value, MAX_VAL_LEN);
channel = atoi(value)-1;
if (is_valid_channel_number(channel)) len = ledstring.channel[channel].count;;
if (*args!=0){
args = read_val(args, value, MAX_VAL_LEN);
brightness = atoi(value);
if(*args!=0){
args = read_val(args, value, MAX_VAL_LEN);
start = atoi(value);
if (*args!=0){
args = read_val(args, value, MAX_VAL_LEN);
len = atoi(value);
}
}
}
}
if (is_valid_channel_number(channel)){
if (brightness<0 || brightness>0xFF) brightness=255;
if (start>=ledstring.channel[channel].count) start=0;
if ((start+len)>ledstring.channel[channel].count) len=ledstring.channel[channel].count-start;
if (debug) printf("Changing brightness %d, %d, %d, %d\n", channel, brightness, start, len);
ws2811_led_t * leds = ledstring.channel[channel].leds;
unsigned int i;
for (i=start;i<start+len;i++){
leds[i].brightness=brightness;
}
}else{
fprintf(stderr,"Invalid channel number, did you call setup and init?\n");
}
}
//causes a fade effect in time
//fade <channel>,<startbrightness>,<endbrightness>,<delay>,<step>,<startled>,<len>
void fade (char * args){
char value[MAX_VAL_LEN];
int channel=0, brightness=255,step=1,startbrightness=0, endbrightness=255;
unsigned int start=0, len=0, delay=50;
if (is_valid_channel_number(channel)){
len = ledstring.channel[channel].count;;
}
if (args!=NULL){
args = read_val(args, value, MAX_VAL_LEN);
channel = atoi(value)-1;
if (is_valid_channel_number(channel)){
len = ledstring.channel[channel].count;;
}
if (*args!=0){
args = read_val(args, value, MAX_VAL_LEN);
startbrightness=atoi(value);
if(*args!=0){
args = read_val(args, value, MAX_VAL_LEN);
endbrightness=atoi(value);
if(*args!=0){
args = read_val(args, value, MAX_VAL_LEN);
delay = atoi(value);
if(*args!=0){
args = read_val(args, value, MAX_VAL_LEN);
step = atoi(value);
if(*args!=0){
args = read_val(args, value, MAX_VAL_LEN);
start=atoi(value);
if (*args!=0){
args = read_val(args, value, MAX_VAL_LEN);
len = atoi(value);
}
}
}
}
}
}
}
if (is_valid_channel_number(channel)){
if (startbrightness>0xFF) startbrightness=255;
if (endbrightness>0xFF) endbrightness=255;
if (start>=ledstring.channel[channel].count) start=0;
if ((start+len)>ledstring.channel[channel].count) len=ledstring.channel[channel].count-start;
if (step==0) step = 1;
if (startbrightness>endbrightness){ //swap
if (step > 0) step = -step;
}else{
if (step < 0) step = -step;
}
if (debug) printf("fade %d, %d, %d, %d, %d, %d, %d\n", channel, startbrightness, endbrightness, delay, step,start,len);
ws2811_led_t * leds = ledstring.channel[channel].leds;
int i,brightness;
for (brightness=startbrightness; (startbrightness > endbrightness ? brightness>=endbrightness: brightness<=endbrightness) ;brightness+=step){
for (i=start;i<start+len;i++){
leds[i].brightness=brightness;
}
ws2811_render(&ledstring);
usleep(delay * 1000);
}
}else{
fprintf(stderr,"Invalid channel number, did you call setup and init?\n");
}
}
//generates a brightness gradient pattern of a color component or brightness level
//gradient <channel>,<RGBWL>,<startlevel>,<endlevel>,<startled>,<len>
void gradient (char * args){
char value[MAX_VAL_LEN];
int channel=0, startlevel=0,endlevel=255;
unsigned int start=0, len=0;
char component='L'; //L is brightness level
if (is_valid_channel_number(channel)){
len = ledstring.channel[channel].count;;
}
if (args!=NULL){
args = read_val(args, value, MAX_VAL_LEN);
channel = atoi(value)-1;
if (is_valid_channel_number(channel)){
len = ledstring.channel[channel].count;;
}
if (*args!=0){
args = read_val(args, value, MAX_VAL_LEN);
component=toupper(value[0]);
if(*args!=0){
args = read_val(args, value, MAX_VAL_LEN);
startlevel=atoi(value);
if(*args!=0){
args = read_val(args, value, MAX_VAL_LEN);
endlevel = atoi(value);
if(*args!=0){
args = read_val(args, value, MAX_VAL_LEN);
start=atoi(value);
if (*args!=0){
args = read_val(args, value, MAX_VAL_LEN);
len = atoi(value);
}
}
}
}
}
}
if (is_valid_channel_number(channel)){
if (startlevel>0xFF) startlevel=255;
if (endlevel>0xFF) endlevel=255;
if (start>=ledstring.channel[channel].count) start=0;
if ((start+len)>ledstring.channel[channel].count) len=ledstring.channel[channel].count-start;
float step = 1.0*(endlevel-startlevel) / (float)(len-1);
if (debug) printf("gradient %d, %c, %d, %d, %d,%d\n", channel, component, startlevel, endlevel, start,len);
ws2811_led_t * leds = ledstring.channel[channel].leds;
float flevel = startlevel;
int i;
for (i=0; i<len;i++){
unsigned int level = (unsigned int) flevel;
if (i==len-1) level = endlevel;
switch (component){
case 'R':
leds[i+start].color = (leds[i+start].color & 0xFFFFFF00) | level;
break;
case 'G':
leds[i+start].color = (leds[i+start].color & 0xFFFF00FF) | (level << 8);
break;
case 'B':
leds[i+start].color = (leds[i+start].color & 0xFF00FFFF) | (level << 16);
break;
case 'W':
leds[i+start].color = (leds[i+start].color & 0x00FFFFFF) | (level << 24);
break;
case 'L':
leds[i+start].brightness=level;
break;
}
flevel+=step;
}
}else{
fprintf(stderr,"Invalid channel number, did you call setup and init?\n");
}
}
//generates random colors
//random <channel>,<start>,<len>,<RGBWL>
void add_random(char * args){
char value[MAX_VAL_LEN];
int channel=0;
unsigned int start=0, len=0;
char component='L'; //L is brightness level
int use_r=1, use_g=1, use_b=1, use_w=1, use_l=1;
if (is_valid_channel_number(channel)){
len = ledstring.channel[channel].count;;
}
if (args!=NULL){
args = read_val(args, value, MAX_VAL_LEN);
channel = atoi(value)-1;
if (is_valid_channel_number(channel)){
len = ledstring.channel[channel].count;;
}
if(*args!=0){
args = read_val(args, value, MAX_VAL_LEN);
start=atoi(value);
if (*args!=0){
args = read_val(args, value, MAX_VAL_LEN);
len = atoi(value);
if (*args!=0){
args = read_val(args, value, MAX_VAL_LEN);
use_r=0, use_g=0, use_b=0, use_w=0, use_l=0;
unsigned char i;
for (i=0;i<strlen(value);i++){
switch(toupper(value[i])){
case 'R':
use_r=1;
break;
case 'G':
use_g=1;
break;
case 'B':
use_b=1;
break;
case 'W':
use_w=1;
break;
case 'L':
use_l=1;
break;
}
}
}
}
}
}
if (is_valid_channel_number(channel)){
if (start>=ledstring.channel[channel].count) start=0;
if ((start+len)>ledstring.channel[channel].count) len=ledstring.channel[channel].count-start;
if (debug) printf("random %d,%d,%d\n", channel, start, len);
ws2811_led_t * leds = ledstring.channel[channel].leds;
//unsigned int colors = ledstring[channel].color_size;
unsigned char r=0,g=0,b=0,w=0,l=0;
unsigned int i;
for (i=0; i<len;i++){
if (use_r) r = rand() % 256;
if (use_g) g = rand() % 256;
if (use_b) b = rand() % 256;
if (use_w) w = rand() % 256;
if (use_l) l = rand() % 256;
if (use_r || use_g || use_b || use_w) leds[start+i].color = color_rgbw(r,g,b,w);
if (use_l) leds[start+i].brightness = l;
}
}else{
fprintf(stderr,"Invalid channel number, did you call setup and init?\n");
}
}
void start_loop (char * args){
if (mode==MODE_FILE){
if (loop_index>=MAX_LOOPS){
loop_index=MAX_LOOPS-1;
printf("Warning max nested loops reached!\n");
return;
}
if (debug) printf ("do %d\n", ftell(input_file));
loops[loop_index].do_pos = ftell(input_file);
loops[loop_index].n_loops=0;
loop_index++;
}else if (mode==MODE_TCP){
if (loop_index<MAX_LOOPS){
if (debug) printf ("do %d\n", thread_read_index);
loops[loop_index].do_pos = thread_read_index;
loops[loop_index].n_loops=0;
loop_index++;
}else{
printf("Warning max nested loops reached!\n");
}
}
}
void end_loop(char * args){
int max_loops = 0; //number of wanted loops
if (args!=NULL){
max_loops = atoi(args);
}
if (mode==MODE_FILE){
if (debug) printf ("loop %d \n", ftell(input_file));
if (loop_index==0){ //no do found!
fseek(input_file, 0, SEEK_SET);
}else{
loops[loop_index-1].n_loops++;
if (max_loops==0 || loops[loop_index-1].n_loops<max_loops){ //if number of loops is 0 = loop forever
fseek(input_file, loops[loop_index-1].do_pos,SEEK_SET);
}else{
if (loop_index>0) loop_index--; //exit loop
}
}
}else if (mode==MODE_TCP){
if (debug) printf("loop %d\n", thread_read_index);
if (loop_index==0){
thread_read_index=0;
}else{
loops[loop_index-1].n_loops++;
if (max_loops==0 || loops[loop_index-1].n_loops<max_loops){ //if number of loops is 0 = loop forever
thread_read_index = loops[loop_index-1].do_pos;
}else{
if (loop_index>0) loop_index--; //exit loop
}
}
}
}
//initializes the memory for a TCP/IP multithread buffer
void init_thread(char * data){
if (thread_data==NULL){
thread_data = (char *) malloc(DEFAULT_BUFFER_SIZE);
thread_data_size = DEFAULT_BUFFER_SIZE;
}
start_thread=0;
thread_read_index=0;
thread_write_index=0;
thread_running=0;
write_to_thread_buffer=1; //from now we save all commands to the thread buffer
}
//expands the TCP/IP multithread buffer (increase size by 2)
void expand_thread_data_buffer(){
thread_data_size = thread_data_size * 2;
char * tmp_buffer = (char *) malloc(thread_data_size);
memcpy((void*) tmp_buffer, (void*)thread_data, thread_data_size);
free(thread_data);
thread_data = tmp_buffer;
}
//adds data to the thread buffer
void write_thread_buffer (char c){
thread_data[thread_write_index] = c;
thread_write_index++;
if (thread_write_index==thread_data_size) expand_thread_data_buffer();
}
//this function can be run in other thread for TCP/IP to enable do ... loops (usefull for websites)
void thread_func (void * param){
thread_read_index=0;
if (debug) printf("Enter thread %d,%d,%d.\n", thread_running,thread_read_index,thread_write_index);
while (thread_running){
char c = thread_data[thread_read_index];
//if(debug) printf("Process char %c %d\n", c, thread_read_index);
process_character(c);
thread_read_index++;
if (thread_read_index>=thread_write_index) break; //exit loop if we are at the end of the file
}
thread_running=0;
if (debug) printf("Exit thread.\n");
pthread_exit(NULL); //exit the tread
}
//executes 1 command line
void execute_command(char * command_line){
if (command_line[0]=='#') return; //=comments
if (write_to_thread_buffer){
if (strncmp(command_line, "thread_stop", 11)==0){
if (mode==MODE_TCP){
write_to_thread_buffer=0;
if (debug) printf("Thread stop.\n");
if (thread_write_index>0) start_thread=1; //remember to start the thread when client closes the TCP/IP connection
}
}else{