Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <stdio.h>
- #include <termios.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/mman.h>
- #include <sys/stat.h>
- #include <ncurses.h>
- #include <time.h>
- #define TRAIL1 54 //C1
- #define TRAIL2 55 //C2
- #define DIVER 56 //PA
- #define VACUUM 57 //PV
- #define ARM 58 //AA
- #define GRABBER 59 //GA
- #define HORIZONTALDIVER 60//CP, can be 0, 1, 2 or 3
- #define BIN 61
- #define TRASH 62
- #define CONVEYOR1 63
- #define CONVEYOR2 64
- struct ACTUATEURS
- {
- unsigned CP : 2;
- unsigned C1 : 1;
- unsigned C2 : 1;
- unsigned PV : 1;
- unsigned PA : 1;
- unsigned AA : 1;
- unsigned GA : 1;
- } ;
- union
- {
- struct ACTUATEURS act ;
- unsigned char byte ;
- } u_act ;
- struct CAPTEURS
- {
- unsigned L1 : 1;
- unsigned L2 : 1;
- unsigned T : 1; /* cablÈ H */
- unsigned S : 1;
- unsigned CS : 1;
- unsigned AP : 1;
- unsigned PP : 1;
- unsigned DE : 1;
- /* unsigned H1 : 1;
- unsigned H2 : 1;
- unsigned H3 : 1;
- unsigned H4 : 1;
- unsigned H5 : 1;
- unsigned H6 : 1;
- unsigned : 2; */
- } ;
- union
- {
- struct CAPTEURS capt ;
- unsigned char byte ;
- } u_capt ;
- void AfficheActu(int);
- void AfficheCapt(int);
- int kbhit(void);
- void setActu(int actu);
- void setHorizontalDiver(int position);
- int fd_petra_out;
- int main ()
- {
- int fd_petra_in;
- struct timespec tps;
- char action='n';
- tps.tv_sec=0;
- tps.tv_nsec=150000000;
- u_act.byte = 0x00 ;
- fd_petra_out = open ( "/dev/actuateursPETRA", O_WRONLY );
- if ( fd_petra_out == -1 )
- {
- perror ( "MAIN : Erreur ouverture PETRA_OUT" );
- return 1;
- }
- else
- printf ("MAIN: PETRA_OUT opened\n");
- fd_petra_in = open ( "/dev/capteursPETRA", O_RDONLY );
- if ( fd_petra_in == -1 )
- {
- perror ( "MAIN : Erreur ouverture PETRA_IN" );
- return 1;
- }
- else
- printf ("MAIN: PETRA_IN opened\n");
- do
- {
- printf("DERNIERE TOUCHE %c\n\n\n",action);
- AfficheCapt(fd_petra_in);
- AfficheActu(fd_petra_out);
- nanosleep(&tps, NULL);
- printf("\033[2J");
- fflush(stdin);
- if(kbhit())
- {
- action=getchar();
- switch(action)
- {
- case '0' :
- setHorizontalDiver(BIN);
- break;
- case '1' :
- setHorizontalDiver(CONVEYOR1);
- break;
- case '2' :
- setHorizontalDiver(TRASH);
- break;
- case '3' :
- setHorizontalDiver(CONVEYOR2);
- break;
- case 'e':
- setActu(TRAIL1);
- break;
- case 'r':
- setActu(TRAIL2);
- break;
- case 't':
- setActu(VACUUM);
- break;
- case 'y':
- setActu(DIVER);
- break;
- case 'u':
- setActu(ARM);
- break;
- case 'i':
- setActu(GRABBER);
- break;
- default:
- break;
- };
- }
- }while(action!='q');
- u_act.byte = 0x00;
- write ( fd_petra_out , &u_act.byte ,1 );
- close ( fd_petra_in );
- close ( fd_petra_out );
- return 1;
- }
- void setActu(int actu)
- {
- read ( fd_petra_out , &u_act.byte , 1 );
- switch(actu)
- {
- case TRAIL1 :
- u_act.act.C1 ^= 1;
- break;
- case TRAIL2 :
- u_act.act.C2 ^= 1;
- break;
- case GRABBER :
- u_act.act.GA ^= 1;
- break;
- case DIVER :
- u_act.act.PA ^=1;
- break;
- case VACUUM :
- u_act.act.PV ^=1;
- break;
- case ARM :
- u_act.act.AA ^= 1;
- break;
- }
- write ( fd_petra_out , &u_act.byte ,1 );
- }
- void setHorizontalDiver(int position)
- {
- read ( fd_petra_out , &u_act.byte , 1 );
- switch(position)
- {
- case BIN :
- u_act.act.CP = 0;
- break;
- case TRASH :
- u_act.act.CP = 2;
- break;
- case CONVEYOR1 :
- u_act.act.CP = 1;
- break;
- case CONVEYOR2 :
- u_act.act.CP = 3;
- break;
- }
- write ( fd_petra_out , &u_act.byte ,1 );
- }
- void AfficheActu(int fd_petra_out)
- {
- read ( fd_petra_out , &u_act.byte , 1 );
- printf("-----ACTUATEURS-----\n");
- printf(" 0-3: CP : %d\n",u_act.act.CP);
- printf(" e: C1 : %d\n",u_act.act.C1);
- printf(" r: C2 : %d\n",u_act.act.C2);
- printf(" t: PV : %d\n",u_act.act.PV);
- printf(" y: PA : %d\n",u_act.act.PA);
- printf(" u: AA : %d\n",u_act.act.AA);
- printf(" i: GA : %d\n",u_act.act.GA);
- }
- void AfficheCapt(int fd_petra_in)
- {
- read ( fd_petra_in , &u_capt.byte , 1 );
- printf("-----CAPTEURS-----\n");
- printf(" L1 : %d\n",u_capt.capt.L1);
- printf(" L2 : %d\n",u_capt.capt.L2);
- printf(" T : %d\n",u_capt.capt.T);
- printf(" S : %d\n",u_capt.capt.S);
- printf(" CS : %d\n",u_capt.capt.CS);
- printf(" AP : %d\n",u_capt.capt.AP);
- printf(" PP : %d\n",u_capt.capt.PP);
- printf(" DE : %d\n",u_capt.capt.DE);
- }
- int kbhit(void)
- {
- struct termios oldt, newt;
- int ch;
- int oldf;
- tcgetattr(STDIN_FILENO, &oldt);
- newt = oldt;
- newt.c_lflag &= ~(ICANON | ECHO);
- tcsetattr(STDIN_FILENO, TCSANOW, &newt);
- oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
- fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
- ch = getchar();
- tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
- fcntl(STDIN_FILENO, F_SETFL, oldf);
- if(ch == EOF)
- return 0;
- ungetc(ch, stdin);
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement