Skip to content

Commit 4d1ae82

Browse files
Petr VanekPetr Vanek
authored andcommitted
perform port discovery in state machine during curses even loop
1 parent 8bdc993 commit 4d1ae82

File tree

2 files changed

+102
-89
lines changed

2 files changed

+102
-89
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ trainer:
88
$(CC) trainer.c $(CCFLAGS) $(LIBS) -o $@
99

1010
win32:
11-
i686-w64-mingw32-gcc -O2 -v trainer.c -o trainer_w32.exe -std=gnu11 -I /home/vanous/bin/projects/c/win/win32/include/artnet/ -I /home/vanous/bin/projects/c/win/win32/include/ncurses/ /home/vanous/bin/projects/c/win/win32/lib/libartnet.a /home/vanous/bin/projects/c/win/win32/lib/libncurses.a
11+
i686-w64-mingw32-gcc -O2 -v trainer.c -o trainer_w32.exe -std=gnu11 -static -mwindows -I /home/vanous/bin/projects/c/win/win32/include/artnet/ -I /home/vanous/bin/projects/c/win/win32/include/ncurses/ /home/vanous/bin/projects/c/win/win32/lib/libartnet.dll.a /home/vanous/bin/projects/c/win/win32/lib/libncurses.a
1212

1313
win64:
14-
x86_64-w64-mingw32-gcc -O2 -v trainer.c -o trainer_w64.exe -std=gnu11 -I /home/vanous/bin/projects/c/win/win64/include/artnet/ -I /home/vanous/bin/projects/c/win/win64/include/ncurses/ /home/vanous/bin/projects/c/win/win64/lib/libartnet.a /home/vanous/bin/projects/c/win/win64/lib/libncurses.a
14+
x86_64-w64-mingw32-gcc -O2 -v trainer.c -o trainer_w64.exe -std=gnu11 -static -mwindows -I /home/vanous/bin/projects/c/win/win64/include/artnet/ -I /home/vanous/bin/projects/c/win/win64/include/ncurses/ /home/vanous/bin/projects/c/win/win64/lib/libartnet.dll.a /home/vanous/bin/projects/c/win/win64/lib/libncurses.a
1515

1616
clean:
1717
rm -f .*.o .*.d trainer trainer_w64.exe trainer_w32.exe

trainer.c

Lines changed: 100 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ int fd=-1;
4747
HANDLE h; //serial port
4848
#endif
4949

50-
//#define ARTNET
50+
#define ARTNET
5151
//#define DEBUG
5252

5353
int calib[8][4]; // calibration values
@@ -67,10 +67,10 @@ DLF
6767
enum fixtures fixture = NONE;
6868

6969
int serial_port;
70-
unsigned char serial_alias[20];
70+
unsigned char serial_alias[42]="...searching...";
7171
char serial_name[20];
7272

73-
int uid; //is DEVICE detected?
73+
int uid=0; //is DEVICE detected?
7474

7575
const char* fixtures_print[] = {
7676
"press c to configure fixture",
@@ -268,11 +268,11 @@ void dmxusb_open_port(char *serial_name){
268268
}
269269

270270
#else
271-
printf("inside open port: %s",serial_name);
271+
//printf("inside open port: %s",serial_name);
272272
h = openSerialPort(serial_name,B19200,one,off);
273273
//h = openSerialPort(serial_name,B9600,one,off);
274274

275-
Sleep(100);
275+
//Sleep(100);
276276
#endif
277277
#ifndef WIN32
278278
#ifdef DEBUG
@@ -404,69 +404,118 @@ return success;
404404
}
405405

406406

407-
void find_port(){
408407

409-
int i=0;
408+
void init_artnet()
410409

411-
#ifdef WIN32
412-
i=3;
413-
#endif
410+
{
411+
412+
char *ip_addr = NULL;
413+
int optc, subnet_addr = 0, port_addr = 0;
414+
int bcast_limit = 0;
415+
int an_sd;
416+
/* set up artnet node */
417+
node = artnet_new(ip_addr, verbose);;
418+
419+
if(node == NULL) {
420+
printf ("Unable to set up artnet node: %s\n", artnet_strerror() );
421+
//return 1;
422+
}
423+
424+
// set names and node type
425+
artnet_set_short_name(node, "TRAINER");
426+
artnet_set_long_name(node, "The calibRAtIoN submittER");
427+
artnet_set_node_type(node, ARTNET_SRV);
428+
429+
artnet_set_subnet_addr(node, subnet_addr);
430+
431+
// enable the first input port (1 universe only)
432+
artnet_set_port_type(node, 0, ARTNET_ENABLE_INPUT, ARTNET_PORT_DMX);
433+
artnet_set_port_addr(node, 0, ARTNET_INPUT_PORT, port_addr);
434+
artnet_set_bcast_limit(node, bcast_limit);
435+
436+
//start the node
437+
//artnet_start(node);
438+
if (artnet_start(node) != ARTNET_EOK) {
439+
printf("Failed to start: %s\n", artnet_strerror() );
440+
//goto error_destroy;
441+
// return 1;
442+
}
443+
444+
// store the sds
445+
an_sd = artnet_get_sd(node); //all as per examples
446+
}
447+
448+
449+
void find_port(){
450+
451+
static int i=0; //port iterator
414452

415-
for (i;i<255;i++){
453+
if ((i<255) && (!uid)){
416454

417455
#ifdef WIN32
418456
if (i<10){
419457
sprintf(serial_name,"COM%d",i);
420458
}else{
421459
sprintf(serial_name,"\\\\.\\COM%d",i);
422460
}
423-
#else
424-
sprintf(serial_name,"/dev/ttyUSB%d",i);
425-
#endif
426461

427-
printf("testing port %s\n",serial_name);
428-
if (file_exist(serial_name))
429-
{
430-
printf("opening port %s\n",serial_name);
462+
printf(".");
463+
//printf("opening port %s\n",serial_name);
431464
dmxusb_open_port(serial_name);
432465
uid=get_uid();
433466

434-
if (uid){ //0 OK, 1 fail
467+
if (uid){ //>0
468+
printf("\n");
435469
printf("found port: %s\n", serial_name);
436470
printf("found device: %s\n",serial_alias);
437-
break;
471+
mvwprintw(w,14,1," ");
472+
mvwprintw(w,14,1,"%s %s",uid ? serial_name:"", serial_alias);
473+
// break;
438474

439475
}else{
440-
printf("UID failed");
476+
//printf("UID failed");
441477

442478
}
443-
444-
445-
446-
}else{
447-
448-
#ifdef WIN32
449-
450-
printf("opening port %s\n",serial_name);
479+
#else
480+
sprintf(serial_name,"/dev/ttyUSB%d",i);
481+
if (file_exist(serial_name))
482+
{
483+
//printf("opening port %s\n",serial_name);
451484
dmxusb_open_port(serial_name);
452485
uid=get_uid();
453486

454-
if (uid){ //0 OK, 1 fail
487+
if (uid){ //>0
488+
printf("\n");
455489
printf("found port: %s\n", serial_name);
456490
printf("found device: %s\n",serial_alias);
457-
break;
491+
mvwprintw(w,14,1," ");
492+
mvwprintw(w,14,1,"%s %s",uid ? serial_name:"", serial_alias);
493+
// break;
458494

459495
}else{
460-
printf("UID failed");
496+
//printf("UID failed");
461497

462498
}
463-
#endif
464-
}
465-
466-
467499

500+
}
468501

502+
#endif
503+
i++;
504+
}else{
505+
if (!uid){ //uid should be 0 if no serial
506+
#ifdef ARTNET
507+
sprintf(serial_alias,"Art-Net Output enabled");
508+
mvwprintw(w,14,1," ");
509+
mvwprintw(w,14,1,"%s",serial_alias);
510+
init_artnet();
511+
uid=1;
512+
#else
513+
sprintf(serial_alias,"No DMX device found and Art-Net disabled");
514+
mvwprintw(w,14,1," ");
515+
mvwprintw(w,14,1,"%s",serial_alias);
516+
#endif
469517
}
518+
}
470519

471520
}
472521

@@ -597,6 +646,7 @@ void init_calib(){
597646

598647
void get_input(int r,int c,char * str,int msg) //get user input for each calibration value
599648
{
649+
nodelay(w,FALSE); //reenable getch
600650
echo(); // print characters as typed
601651
curs_set(2); // show cursor
602652
mvwprintw(w,r+3,c+19+(c*4),"%s"," ");
@@ -645,6 +695,8 @@ void get_input(int r,int c,char * str,int msg) //get user input for each calibra
645695

646696
noecho(); //edit is over, set terminal back to non edit mode - catch single char in main loop
647697
curs_set(0);
698+
nodelay(w,TRUE); //reenable getch
699+
648700
}
649701

650702
void msleep(long time) {
@@ -716,7 +768,9 @@ if (current_program != 0) { //send DMX only if any program is active
716768
printf("failed to send: %s\n", artnet_strerror() );
717769
}
718770
#else
771+
if(uid){
719772
dmxusb_send_dmx(dmx,MAXCHANNELS);
773+
}
720774
#endif
721775
msleep(40); //sleep dtto
722776

@@ -731,7 +785,7 @@ if (current_program != 0) { //send DMX only if any program is active
731785
current_program=0;
732786
program_step=0;
733787
current_step=0;
734-
nodelay(w,FALSE);
788+
//nodelay(w,FALSE);
735789
mvwprintw(w,14,1,"Finished ");
736790
#ifndef ARTNET
737791
dmxusb_mute_dmx();
@@ -819,56 +873,17 @@ void cleanup() { //on exit
819873
}
820874

821875

822-
823876
int main()
824877
{
825878

826879

827-
char *ip_addr = NULL;
828-
int optc, subnet_addr = 0, port_addr = 0;
829-
int bcast_limit = 0;
830-
int an_sd;
831-
832-
#ifdef ARTNET
833-
834-
/* set up artnet node */
835-
node = artnet_new(ip_addr, verbose);;
836-
837-
if(node == NULL) {
838-
printf ("Unable to set up artnet node: %s\n", artnet_strerror() );
839-
return 1;
840-
}
841-
842-
// set names and node type
843-
artnet_set_short_name(node, "TRAINER");
844-
artnet_set_long_name(node, "The calibRAtIoN submittER");
845-
artnet_set_node_type(node, ARTNET_SRV);
846-
847-
artnet_set_subnet_addr(node, subnet_addr);
848-
849-
// enable the first input port (1 universe only)
850-
artnet_set_port_type(node, 0, ARTNET_ENABLE_INPUT, ARTNET_PORT_DMX);
851-
artnet_set_port_addr(node, 0, ARTNET_INPUT_PORT, port_addr);
852-
artnet_set_bcast_limit(node, bcast_limit);
853-
854-
//start the node
855-
//artnet_start(node);
856-
if (artnet_start(node) != ARTNET_EOK) {
857-
printf("Failed to start: %s\n", artnet_strerror() );
858-
//goto error_destroy;
859-
return 1;
860-
}
861880

862-
// store the sds
863-
an_sd = artnet_get_sd(node); //all as per examples
864-
#else
865881

882+
find_port();
866883
//dmxusb_open_port("\\\\.\\COM35");
867884
//get_uid();
868885
//mvwprintw(w,18,1,"hledam port");
869-
find_port();
870886

871-
#endif
872887

873888
int c = 0;
874889
char strr[3];
@@ -893,10 +908,8 @@ int main()
893908
init_calib(); //fill calibrations with 128s
894909
draw_screen();//draw UI
895910
char e=' ';
896-
#ifdef ARTNET
897-
sprintf(serial_alias,"Art-Net");
898-
#endif
899911

912+
nodelay(w,TRUE);
900913
mvwprintw(w,14,1," ");
901914
mvwprintw(w,14,1,"%s %s",uid ? serial_name:"", serial_alias);
902915
/* main loop */
@@ -932,7 +945,7 @@ int main()
932945
get_input(menu_r,menu_c,strr,calib[menu_r][menu_c]);
933946
} else if ((event.x>11) && (event.x<18)){ //run
934947
if (fixture){
935-
nodelay(w,TRUE);
948+
//nodelay(w,TRUE);
936949
current_program=fixture;
937950
program_step=0;
938951
current_step=-1;
@@ -943,7 +956,7 @@ int main()
943956
}
944957
} else if ((event.x>18) && (event.x<26)){ //test
945958
if (fixture){
946-
nodelay(w,TRUE);
959+
//nodelay(w,TRUE);
947960
current_program=4;
948961
current_step=0;
949962
program_step=-1;
@@ -956,7 +969,7 @@ int main()
956969
current_program=0;
957970
current_step=-1;
958971
program_step=0;
959-
nodelay(w,FALSE); //reenable getch
972+
//nodelay(w,FALSE); //reenable getch
960973
//mvwprintw(w,14,1,"Stopped ");
961974
} else if ((event.x>34) && (event.x<42)){
962975
return 0;
@@ -1033,12 +1046,12 @@ int main()
10331046
current_program=0;
10341047
current_step=-1;
10351048
program_step=0;
1036-
nodelay(w,FALSE); //reenable getch
1049+
//nodelay(w,FALSE); //reenable getch
10371050
//mvwprintw(w,14,1,"Stopped ");
10381051
break;
10391052
case 't': //run test program
10401053
if (fixture){
1041-
nodelay(w,TRUE);//disable getch while running
1054+
//nodelay(w,TRUE);//disable getch while running
10421055
current_program=4;
10431056
program_step=0;
10441057
current_step=-1;
@@ -1049,7 +1062,7 @@ int main()
10491062
break;
10501063
case 'r': //run
10511064
if (fixture){
1052-
nodelay(w,TRUE);
1065+
//nodelay(w,TRUE);
10531066
current_program=fixture;
10541067
program_step=0;
10551068
current_step=-1;
@@ -1064,7 +1077,7 @@ int main()
10641077
break;
10651078

10661079
}
1067-
1080+
find_port();
10681081
draw_screen(); //draw screen UI each loop after user input
10691082
do_step(); //send DMX loop
10701083
refresh(); //refresh screen

0 commit comments

Comments
 (0)