kevin eccli
/
Freescale
MAJ BROSSARD
joystick2.c@3:8f2c0c324296, 2014-10-22 (annotated)
- Committer:
- singularity
- Date:
- Wed Oct 22 23:26:41 2014 +0000
- Revision:
- 3:8f2c0c324296
- Parent:
- 2:2889a7a4b67a
tentative convert sint16 -> char
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
singularity | 2:2889a7a4b67a | 1 | #include <stdlib.h> |
singularity | 2:2889a7a4b67a | 2 | #include <SDL2/SDL.h> |
singularity | 2:2889a7a4b67a | 3 | #include <stdio.h> |
singularity | 2:2889a7a4b67a | 4 | #include <termios.h> |
singularity | 2:2889a7a4b67a | 5 | #include <sys/fcntl.h> |
singularity | 2:2889a7a4b67a | 6 | #include <unistd.h> |
singularity | 2:2889a7a4b67a | 7 | |
singularity | 2:2889a7a4b67a | 8 | |
singularity | 2:2889a7a4b67a | 9 | int initSerie() { |
singularity | 2:2889a7a4b67a | 10 | |
singularity | 2:2889a7a4b67a | 11 | int descripteur; |
singularity | 2:2889a7a4b67a | 12 | struct termios config; |
singularity | 2:2889a7a4b67a | 13 | char *port = "/dev/ttyUSB0" |
singularity | 2:2889a7a4b67a | 14 | |
singularity | 2:2889a7a4b67a | 15 | /* Ouverture de la liaison serie */ |
singularity | 2:2889a7a4b67a | 16 | if ( (fd=open(port,O_RDWR)) == -1 ) { |
singularity | 2:2889a7a4b67a | 17 | perror("open"); |
singularity | 2:2889a7a4b67a | 18 | exit(-1); |
singularity | 2:2889a7a4b67a | 19 | } |
singularity | 2:2889a7a4b67a | 20 | |
singularity | 2:2889a7a4b67a | 21 | /* Lecture des parametres courants */ |
singularity | 2:2889a7a4b67a | 22 | tcgetattr(fd,&config); |
singularity | 2:2889a7a4b67a | 23 | |
singularity | 2:2889a7a4b67a | 24 | /*--------------CONFIG-------------------*/ |
singularity | 2:2889a7a4b67a | 25 | /* On ignore les BREAK et les caracteres avec erreurs de parite */ |
singularity | 2:2889a7a4b67a | 26 | config.c_iflag = IGNBRK | IGNPAR; |
singularity | 2:2889a7a4b67a | 27 | /* Pas de mode de sortie particulier */ |
singularity | 2:2889a7a4b67a | 28 | config.c_oflag = 0; |
singularity | 2:2889a7a4b67a | 29 | /* Liaison a 9600 bps avec 7 bits de donnees et une parite paire */ |
singularity | 2:2889a7a4b67a | 30 | /*config.c_cflag = B9600 | CS7 | PARENB;*/ |
singularity | 2:2889a7a4b67a | 31 | config.c_cflag = CS7 | PARENB; |
singularity | 2:2889a7a4b67a | 32 | /* Mode non-canonique avec echo */ |
singularity | 2:2889a7a4b67a | 33 | /*config.c_lflag = ECHO;*/ |
singularity | 2:2889a7a4b67a | 34 | config.c_lflag = 0; /*Pas d'echo pas canonique*/ |
singularity | 2:2889a7a4b67a | 35 | /* Caracteres immediatement disponibles */ |
singularity | 2:2889a7a4b67a | 36 | config.c_cc[VMIN] = 1; |
singularity | 2:2889a7a4b67a | 37 | config.c_cc[VTIME] = 0; |
singularity | 2:2889a7a4b67a | 38 | /*Vitesse reglage*/ |
singularity | 2:2889a7a4b67a | 39 | cfsetispeed(&config, B9600); |
singularity | 2:2889a7a4b67a | 40 | cfsetospeed(&config, B9600); |
singularity | 2:2889a7a4b67a | 41 | |
singularity | 2:2889a7a4b67a | 42 | /* Sauvegarde des nouveaux parametres(Modif immédiate TCSANOW)*/ |
singularity | 2:2889a7a4b67a | 43 | tcsetattr(fd,TCSANOW,&config); |
singularity | 2:2889a7a4b67a | 44 | |
singularity | 2:2889a7a4b67a | 45 | return descripteur; |
singularity | 2:2889a7a4b67a | 46 | } |
singularity | 2:2889a7a4b67a | 47 | |
singularity | 2:2889a7a4b67a | 48 | void envoiCommande(char dir, char md, char mg, int fd){ |
singularity | 2:2889a7a4b67a | 49 | char commande[10]; |
singularity | 2:2889a7a4b67a | 50 | commande[0] = 'S'; |
singularity | 2:2889a7a4b67a | 51 | commande[1] = 'C'; |
singularity | 2:2889a7a4b67a | 52 | commande[2] = dir; |
singularity | 2:2889a7a4b67a | 53 | commande[3] = md; |
singularity | 2:2889a7a4b67a | 54 | commande[4] = mg; |
singularity | 2:2889a7a4b67a | 55 | commande[5] ='\n'; |
singularity | 2:2889a7a4b67a | 56 | write(fd,commande,6); |
singularity | 2:2889a7a4b67a | 57 | } |
singularity | 2:2889a7a4b67a | 58 | |
singularity | 2:2889a7a4b67a | 59 | int main(int argc, char **argv) |
singularity | 2:2889a7a4b67a | 60 | { |
singularity | 2:2889a7a4b67a | 61 | int fd = initSerie(); |
singularity | 2:2889a7a4b67a | 62 | |
singularity | 2:2889a7a4b67a | 63 | |
singularity | 2:2889a7a4b67a | 64 | if(SDL_Init(SDL_INIT_JOYSTICK) < 0) // initialise le joystick |
singularity | 2:2889a7a4b67a | 65 | return EXIT_FAILURE; |
singularity | 2:2889a7a4b67a | 66 | |
singularity | 2:2889a7a4b67a | 67 | |
singularity | 2:2889a7a4b67a | 68 | SDL_Joystick *joystick; // on crée le joystick |
singularity | 2:2889a7a4b67a | 69 | joystick = SDL_JoystickOpen(0); // on l'assigne au numéro 0 |
singularity | 2:2889a7a4b67a | 70 | SDL_Event evenements; |
singularity | 2:2889a7a4b67a | 71 | Sint16 dir; |
singularity | 2:2889a7a4b67a | 72 | Sint16 propulsion; |
singularity | 2:2889a7a4b67a | 73 | |
singularity | 2:2889a7a4b67a | 74 | //SDL_JoystickEventState(SDL_ENABLE); |
singularity | 2:2889a7a4b67a | 75 | while(1) |
singularity | 2:2889a7a4b67a | 76 | { |
singularity | 2:2889a7a4b67a | 77 | while(SDL_PollEvent(&evenements)) // tant qu'il y a des évènements à traiter |
singularity | 2:2889a7a4b67a | 78 | { |
singularity | 2:2889a7a4b67a | 79 | switch(evenements.type) |
singularity | 2:2889a7a4b67a | 80 | { |
singularity | 2:2889a7a4b67a | 81 | case SDL_JOYAXISMOTION: |
singularity | 2:2889a7a4b67a | 82 | if(evenements.jaxis.axis == 0 && evenements.jaxis.which == 1) |
singularity | 2:2889a7a4b67a | 83 | { |
singularity | 2:2889a7a4b67a | 84 | dir = evenements.jaxis.value; |
singularity | 2:2889a7a4b67a | 85 | } |
singularity | 2:2889a7a4b67a | 86 | if(evenements.jaxis.axis == 1 && evenements.jaxis.which == 2) |
singularity | 2:2889a7a4b67a | 87 | { |
singularity | 2:2889a7a4b67a | 88 | propulsion = evenements.jaxis.value; |
singularity | 2:2889a7a4b67a | 89 | } |
singularity | 2:2889a7a4b67a | 90 | break; |
singularity | 2:2889a7a4b67a | 91 | envoiCommande(dir,propulsion,propulsion,fd); |
singularity | 2:2889a7a4b67a | 92 | |
singularity | 2:2889a7a4b67a | 93 | default: |
singularity | 2:2889a7a4b67a | 94 | break; |
singularity | 2:2889a7a4b67a | 95 | } |
singularity | 2:2889a7a4b67a | 96 | } |
singularity | 2:2889a7a4b67a | 97 | } |
singularity | 2:2889a7a4b67a | 98 | /* Fermeture */ |
singularity | 2:2889a7a4b67a | 99 | SDL_JoystickClose(joystick); |
singularity | 2:2889a7a4b67a | 100 | SDL_Quit(); |
singularity | 2:2889a7a4b67a | 101 | close(fd); |
singularity | 2:2889a7a4b67a | 102 | return EXIT_SUCCESS; |
singularity | 2:2889a7a4b67a | 103 | } |