MAJ BROSSARD

Dependencies:   mbed

Committer:
singularity
Date:
Wed Oct 22 23:26:41 2014 +0000
Revision:
3:8f2c0c324296
tentative convert sint16 -> char

Who changed what in which revision?

UserRevisionLine numberNew contents of line
singularity 3:8f2c0c324296 1 #include <stdlib.h>
singularity 3:8f2c0c324296 2 #include <SDL.h>
singularity 3:8f2c0c324296 3 #include <stdio.h>
singularity 3:8f2c0c324296 4 #include <termios.h>
singularity 3:8f2c0c324296 5 #include <sys/fcntl.h>
singularity 3:8f2c0c324296 6 #include <unistd.h>
singularity 3:8f2c0c324296 7
singularity 3:8f2c0c324296 8
singularity 3:8f2c0c324296 9 int initSerie() {
singularity 3:8f2c0c324296 10
singularity 3:8f2c0c324296 11 int descripteur;
singularity 3:8f2c0c324296 12 struct termios config;
singularity 3:8f2c0c324296 13 char const *port = "/dev/ttyUSB0"
singularity 3:8f2c0c324296 14 FILE* debugInitSerie = NULL;
singularity 3:8f2c0c324296 15 debugInitSerie = fopen("debugInitSerie.txt","w+");
singularity 3:8f2c0c324296 16
singularity 3:8f2c0c324296 17 if(debugInitSerie == NULL)
singularity 3:8f2c0c324296 18 return EXIT_FAILURE;
singularity 3:8f2c0c324296 19
singularity 3:8f2c0c324296 20 fprintf(debugInitSerie,"Debut\n");
singularity 3:8f2c0c324296 21
singularity 3:8f2c0c324296 22
singularity 3:8f2c0c324296 23 /* Ouverture de la liaison serie */
singularity 3:8f2c0c324296 24 if ( (descripteur=open(port,O_RDWR)) == -1 ) {
singularity 3:8f2c0c324296 25 perror("open");
singularity 3:8f2c0c324296 26 fprintf(debugInitSerie,"Erreur ouverture port serie\n");
singularity 3:8f2c0c324296 27 exit(-1);
singularity 3:8f2c0c324296 28 }
singularity 3:8f2c0c324296 29
singularity 3:8f2c0c324296 30 /* Lecture des parametres courants */
singularity 3:8f2c0c324296 31 tcgetattr(descripteur,&config);
singularity 3:8f2c0c324296 32
singularity 3:8f2c0c324296 33 /*--------------CONFIG-------------------*/
singularity 3:8f2c0c324296 34 /* On ignore les BREAK et les caracteres avec erreurs de parite */
singularity 3:8f2c0c324296 35 config.c_iflag = IGNBRK | IGNPAR;
singularity 3:8f2c0c324296 36 /* Pas de mode de sortie particulier */
singularity 3:8f2c0c324296 37 config.c_oflag = 0;
singularity 3:8f2c0c324296 38 /* Liaison a 9600 bps avec 7 bits de donnees et une parite paire */
singularity 3:8f2c0c324296 39 /*config.c_cflag = B9600 | CS7 | PARENB;*/
singularity 3:8f2c0c324296 40 config.c_cflag = CS7 | PARENB;
singularity 3:8f2c0c324296 41 /* Mode non-canonique avec echo */
singularity 3:8f2c0c324296 42 /*config.c_lflag = ECHO;*/
singularity 3:8f2c0c324296 43 config.c_lflag = 0; /*Pas d'echo pas canonique*/
singularity 3:8f2c0c324296 44 /* Caracteres immediatement disponibles */
singularity 3:8f2c0c324296 45 config.c_cc[VMIN] = 1;
singularity 3:8f2c0c324296 46 config.c_cc[VTIME] = 0;
singularity 3:8f2c0c324296 47 /*Vitesse reglage*/
singularity 3:8f2c0c324296 48 cfsetispeed(&config, B9600);
singularity 3:8f2c0c324296 49 cfsetospeed(&config, B9600);
singularity 3:8f2c0c324296 50
singularity 3:8f2c0c324296 51 /* Sauvegarde des nouveaux parametres(Modif immédiate TCSANOW)*/
singularity 3:8f2c0c324296 52 if(tcsetattr(descripteur,TCSANOW,&config) == -1) {
singularity 3:8f2c0c324296 53 fprintf(debugInitSerie,"Erreur tcsetattr\n");
singularity 3:8f2c0c324296 54 exit(-1);
singularity 3:8f2c0c324296 55 }
singularity 3:8f2c0c324296 56 fprintf(debugInitSerie,"InitSerie OK");
singularity 3:8f2c0c324296 57 fclose(debugInitSerie);
singularity 3:8f2c0c324296 58 return descripteur;
singularity 3:8f2c0c324296 59 }
singularity 3:8f2c0c324296 60
singularity 3:8f2c0c324296 61 void envoiCommande(char dir, char md, char mg, int fd){
singularity 3:8f2c0c324296 62 FILE* debugcommande = NULL;
singularity 3:8f2c0c324296 63 debugcommande = fopen("debugcommande.txt","w+");
singularity 3:8f2c0c324296 64 char commande[10];
singularity 3:8f2c0c324296 65 commande[0] = 'S';
singularity 3:8f2c0c324296 66 commande[1] = 'C';
singularity 3:8f2c0c324296 67 commande[2] = dir;
singularity 3:8f2c0c324296 68 commande[3] = md;
singularity 3:8f2c0c324296 69 commande[4] = mg;
singularity 3:8f2c0c324296 70 commande[5] ='\n';
singularity 3:8f2c0c324296 71 fprintf(debugcommande"%c%c%d%d%d",commande[0],commande[1],commande[2],commande[3],commande[4]);
singularity 3:8f2c0c324296 72 fclose(debugcommande);
singularity 3:8f2c0c324296 73 write(fd,commande,6);
singularity 3:8f2c0c324296 74 }
singularity 3:8f2c0c324296 75
singularity 3:8f2c0c324296 76 char convert(Sint16 a) {
singularity 3:8f2c0c324296 77 int16 b = (int16)a;
singularity 3:8f2c0c324296 78 int8 c = b>>8;
singularity 3:8f2c0c324296 79 char res = (char) c;
singularity 3:8f2c0c324296 80 return res;
singularity 3:8f2c0c324296 81 }
singularity 3:8f2c0c324296 82
singularity 3:8f2c0c324296 83 int main(int argc, char **argv)
singularity 3:8f2c0c324296 84 {
singularity 3:8f2c0c324296 85 FILE debug = NULL;
singularity 3:8f2c0c324296 86 debug = fopen("debug.txt","w+");
singularity 3:8f2c0c324296 87
singularity 3:8f2c0c324296 88 if(debug == NULL)
singularity 3:8f2c0c324296 89 return EXIT_FAILURE;
singularity 3:8f2c0c324296 90
singularity 3:8f2c0c324296 91 fprintf(debug,"Debut\n");
singularity 3:8f2c0c324296 92
singularity 3:8f2c0c324296 93 int fd = initSerie();
singularity 3:8f2c0c324296 94
singularity 3:8f2c0c324296 95 if(SDL_Init(SDL_INIT_JOYSTICK) < 0){ // initialise le joystick
singularity 3:8f2c0c324296 96 return EXIT_FAILURE;
singularity 3:8f2c0c324296 97 fprintf(debug,"Erreur init sdl\n");
singularity 3:8f2c0c324296 98 }
singularity 3:8f2c0c324296 99
singularity 3:8f2c0c324296 100
singularity 3:8f2c0c324296 101 SDL_Joystick *joystick; // on crée le joystick
singularity 3:8f2c0c324296 102 joystick = SDL_JoystickOpen(0); // on l'assigne au numéro 0
singularity 3:8f2c0c324296 103 SDL_Event evenements;
singularity 3:8f2c0c324296 104 Sint16 dir;
singularity 3:8f2c0c324296 105 Sint16 propulsion;
singularity 3:8f2c0c324296 106
singularity 3:8f2c0c324296 107 SDL_JoystickEventState(SDL_ENABLE);
singularity 3:8f2c0c324296 108 while(1)
singularity 3:8f2c0c324296 109 {
singularity 3:8f2c0c324296 110 while(SDL_PollEvent(&evenements)) // tant qu'il y a des évènements à traiter
singularity 3:8f2c0c324296 111 {
singularity 3:8f2c0c324296 112 switch(evenements.type)
singularity 3:8f2c0c324296 113 {
singularity 3:8f2c0c324296 114 case SDL_JOYAXISMOTION:
singularity 3:8f2c0c324296 115 if(evenements.jaxis.axis == 0 && evenements.jaxis.which == 0)
singularity 3:8f2c0c324296 116 {
singularity 3:8f2c0c324296 117 dir = evenements.jaxis.value;
singularity 3:8f2c0c324296 118 fprintf(debug,"%d",dir);
singularity 3:8f2c0c324296 119 }
singularity 3:8f2c0c324296 120 if(evenements.jaxis.axis == 1 && evenements.jaxis.which == 0)
singularity 3:8f2c0c324296 121 {
singularity 3:8f2c0c324296 122 propulsion = evenements.jaxis.value;
singularity 3:8f2c0c324296 123 fprintf(debug,"%d\n",propulsion);
singularity 3:8f2c0c324296 124 }
singularity 3:8f2c0c324296 125 break;
singularity 3:8f2c0c324296 126 envoiCommande(convert(dir),convert(propulsion),convert(propulsion),fd);
singularity 3:8f2c0c324296 127
singularity 3:8f2c0c324296 128 default:
singularity 3:8f2c0c324296 129 break;
singularity 3:8f2c0c324296 130 }
singularity 3:8f2c0c324296 131 }
singularity 3:8f2c0c324296 132 }
singularity 3:8f2c0c324296 133 /* Fermeture */
singularity 3:8f2c0c324296 134 SDL_JoystickClose(joystick);
singularity 3:8f2c0c324296 135 SDL_Quit();
singularity 3:8f2c0c324296 136 close(fd);
singularity 3:8f2c0c324296 137 close(debug);
singularity 3:8f2c0c324296 138 return EXIT_SUCCESS;
singularity 3:8f2c0c324296 139 }