Commit 9299dd745b26ca8a13e4e09b1f1338fbb82b319c

Authored by root
1 parent 58b09a0b

Programmation nInvaders

games/ninvaders/ninvaders-0.1.1/aliens.o 0 → 100644
No preview for this file type
games/ninvaders/ninvaders-0.1.1/arduino.c 0 → 100644
... ... @@ -0,0 +1,130 @@
  1 +#include <libusb-1.0/libusb.h>
  2 +#include <stdio.h>
  3 +#include <stdlib.h>
  4 +#include <signal.h>
  5 +#include <unistd.h>
  6 +#include "arduino.h"
  7 +
  8 +void init(libusb_context **context, libusb_device ***devices, ssize_t *devices_count){
  9 + int status = libusb_init(context);
  10 + if(status != 0){perror("libusb_init"); exit(-1);}
  11 +
  12 + *devices_count = libusb_get_device_list(*context, devices);
  13 + if(*devices_count < 0){perror("libusb_get_device_list"); exit(-1);}
  14 +}
  15 +
  16 +libusb_device* searchArduino(libusb_device **devices, ssize_t devices_count){
  17 + for(int i=0; i<devices_count; i++){
  18 + libusb_device *device = devices[i];
  19 +
  20 + // device description
  21 + struct libusb_device_descriptor desc;
  22 + int status = libusb_get_device_descriptor(device, &desc);
  23 + if(status != 0) continue;
  24 +
  25 + // search for device
  26 + if(desc.idVendor == ID_VENDOR
  27 + && desc.idProduct == ID_PRODUCT)
  28 + return device;
  29 + }
  30 + return NULL;
  31 +}
  32 +
  33 +void openConnection(libusb_device *arduino, libusb_device_handle **handle, struct libusb_config_descriptor **config_desc){
  34 + // open connection
  35 + int status = libusb_open(arduino, handle);
  36 + if(status != 0){ perror("libusb_open"); exit(-1); }
  37 +
  38 + // prepare config
  39 + status = libusb_get_config_descriptor(arduino, 0, config_desc);
  40 + if(status != 0){ perror("libusb_get_config_descriptor"); exit(-1); }
  41 + int configuration = (*config_desc)->bConfigurationValue;
  42 +
  43 + // detach kernel
  44 + for(int j=0; j<(*config_desc)->bNumInterfaces; j++){
  45 + int interface = (*config_desc)->interface[j].altsetting[0].bInterfaceNumber;
  46 + if(libusb_kernel_driver_active(*handle, interface)){
  47 + status = libusb_detach_kernel_driver(*handle, interface);
  48 + if(status != 0){ perror("libusb_detach_kernel_driver"); exit(-1); }
  49 + }
  50 + }
  51 +
  52 + // use config
  53 + status = libusb_set_configuration(*handle, configuration);
  54 + if(status != 0){ perror("libusb_set_configuration"); exit(-1); }
  55 +
  56 + // claim interfaces
  57 + for(int j=0; j<(*config_desc)->bNumInterfaces; j++){
  58 + struct libusb_interface_descriptor interface_desc = (*config_desc)->interface[j].altsetting[0];
  59 + int interface = interface_desc.bInterfaceNumber;
  60 +
  61 + status = libusb_claim_interface(*handle, interface);
  62 + if(status != 0){ perror("libusb_claim_interface"); exit(-1); }
  63 + }
  64 +}
  65 +
  66 +void getEndpoints(struct libusb_config_descriptor *config_desc, struct libusb_endpoint_descriptor *endpoint_desc_list){
  67 + int count = 0;
  68 + // in interfaces
  69 + for(int j=0; j<config_desc->bNumInterfaces; j++){
  70 + // find endpoints
  71 + for(int k=0; k<config_desc->interface[j].altsetting[0].bNumEndpoints; k++){
  72 + if(count > ENDPOINTS_NUMBER){ printf("getEndpoints: Array out of bound :%d:", count); exit(-1); }
  73 + *(endpoint_desc_list + count) = config_desc->interface[j].altsetting[0].endpoint[k];
  74 + count++;
  75 + }
  76 + }
  77 + //if(count != ENDPOINTS_NUMBER){ printf("Wrong number of endpoints.\nIs this the good device ?\n"); exit(-1); }
  78 +}
  79 +
  80 +void start(libusb_context **context, libusb_device ***devices, libusb_device_handle **handle, struct libusb_config_descriptor **config_desc, struct libusb_endpoint_descriptor *endpoint_desc_list){
  81 + // init
  82 + ssize_t devices_count;
  83 + init(context, devices, &devices_count);
  84 +
  85 + // get arduino device
  86 + libusb_device *arduino = searchArduino(*devices, devices_count);
  87 + if(arduino == NULL){ printf("Arduino device not found\n"); exit(-1); }
  88 +
  89 + // open connection, use config, detach kernel and claim interfaces
  90 + openConnection(arduino, handle, config_desc);
  91 +
  92 + // get enpoints
  93 + getEndpoints(*config_desc, endpoint_desc_list);
  94 +}
  95 +
  96 +void stop(struct libusb_config_descriptor *config_desc, libusb_device_handle *handle, libusb_device **devices, libusb_context *context){
  97 + // release interfaces
  98 + for(int j=0; j<config_desc->bNumInterfaces; j++){
  99 + struct libusb_interface_descriptor interface_desc = config_desc->interface[j].altsetting[0];
  100 + int interface = interface_desc.bInterfaceNumber;
  101 +
  102 + int status = libusb_release_interface(handle, interface);
  103 + if(status != 0){ perror("libusb_release_interface"); exit(-1); }
  104 + }
  105 +
  106 + // free config
  107 + libusb_free_config_descriptor(config_desc);
  108 +
  109 + // close connection
  110 + libusb_close(handle);
  111 +
  112 + // free device list
  113 + libusb_free_device_list(devices, 1);
  114 +
  115 + // libusb exit
  116 + libusb_exit(context);
  117 +}
  118 +
  119 +void sendData(int endpoint_id, uint8_t data, libusb_device_handle *handle, struct libusb_endpoint_descriptor *endpoint_desc_list){
  120 + if(endpoint_id < 0 || endpoint_id > 1){ printf("(sendData) Wrong endpoint !\nMust be 0 or 1\n"); return; }
  121 + int status = libusb_interrupt_transfer(handle, endpoint_desc_list[endpoint_id].bEndpointAddress, (unsigned char *) &data, 1, NULL, TIMEOUT);
  122 + if(status!=0 && status!=LIBUSB_ERROR_TIMEOUT){ perror("libusb_interrupt_transfer"); exit(-1); }
  123 +}
  124 +
  125 +int receiveData(int endpoint_id, uint8_t *data, libusb_device_handle *handle, struct libusb_endpoint_descriptor *endpoint_desc_list){
  126 + if(endpoint_id < 2 || endpoint_id > 3){ printf("(sendData) Wrong endpoint !\nMust be 2 or 3\n"); return -1; }
  127 + int status = libusb_interrupt_transfer(handle, endpoint_desc_list[endpoint_id].bEndpointAddress, (unsigned char *) data, 1, NULL, TIMEOUT);
  128 + if(status!=0 && status!=LIBUSB_ERROR_TIMEOUT){ perror("libusb_interrupt_transfer"); exit(-1); }
  129 + return status;
  130 +}
... ...
games/ninvaders/ninvaders-0.1.1/arduino.h 0 → 100644
... ... @@ -0,0 +1,19 @@
  1 +#ifndef ARDUINO
  2 +#define ARDUINO
  3 +
  4 +#define ID_VENDOR 0x2341
  5 +#define ID_PRODUCT 0x01
  6 +#define ENDPOINTS_NUMBER 4
  7 +#define TIMEOUT 50
  8 +#define MAX_DATA 50
  9 +
  10 +void init(libusb_context **context, libusb_device ***devices, ssize_t *devices_count);
  11 +libusb_device* searchArduino(libusb_device **devices, ssize_t devices_count);
  12 +void openConnection(libusb_device *arduino, libusb_device_handle **handle, struct libusb_config_descriptor **config_desc);
  13 +void getEndpoints(struct libusb_config_descriptor *config_desc, struct libusb_endpoint_descriptor *endpoint_desc_list);
  14 +void start(libusb_context **context, libusb_device ***devices, libusb_device_handle **handle, struct libusb_config_descriptor **config_desc, struct libusb_endpoint_descriptor *endpoint_desc_list);
  15 +void stop(struct libusb_config_descriptor *config_desc, libusb_device_handle *handle, libusb_device **devices, libusb_context *context);
  16 +void sendData(int endpoint_id, uint8_t data, libusb_device_handle *handle, struct libusb_endpoint_descriptor *endpoint_desc_list);
  17 +int receiveData(int endpoint_id, uint8_t *data, libusb_device_handle *handle, struct libusb_endpoint_descriptor *endpoint_desc_list);
  18 +
  19 +#endif
... ...
games/ninvaders/ninvaders-0.1.1/globals.o 0 → 100644
No preview for this file type
games/ninvaders/ninvaders-0.1.1/makefile 0 → 100644
... ... @@ -0,0 +1,20 @@
  1 +CC=gcc
  2 +CFLAGS=-O3 -Wall
  3 +LIBS=-l ncurses usb-1.0
  4 +
  5 +CFILES=globals.c view.c aliens.c ufo.c player.c ardunio.c nInvaders.c
  6 +HFILES=globals.h view.h aliens.h ufo.h player.h ardunio.h nInvaders.h
  7 +OFILES=globals.o view.o aliens.o ufo.o player.o ardunio.o nInvaders.o
  8 +all: nInvaders
  9 + rm -f $(OFILES)
  10 +
  11 +nInvaders: $(OFILES) $(HFILES)
  12 + $(CC) $(LDFLAGS) -o$@ $(OFILES) $(LIBS)
  13 +
  14 +.c.o:
  15 + $(CC) -c -I. $(CFLAGS) $(OPTIONS) $<
  16 +clean:
  17 + rm -f nInvaders $(OFILES)
  18 +
  19 +exec: all
  20 + ./nInvaders
... ...
games/ninvaders/ninvaders-0.1.1/nInvaders.c
... ... @@ -29,6 +29,11 @@
29 29 #include "player.h"
30 30 #include "aliens.h"
31 31 #include "ufo.h"
  32 +#include "arduino.h"
  33 +#include <libusb-1.0/libusb.h>
  34 +#include <stdlib.h>
  35 +#include <signal.h>
  36 +#include <unistd.h>
32 37  
33 38 #define FPS 50
34 39  
... ... @@ -131,12 +136,22 @@ void drawscore()
131 136 /**
132 137 * reads input from keyboard and do action
133 138 */
134   -void readInput()
  139 +void readInput(libusb_device_handle *handle, struct libusb_endpoint_descriptor *endpoint_desc_list)
135 140 {
136 141 int ch;
137 142 static int lastmove;
138 143  
  144 + timeout(100);
139 145 ch = getch(); // get key pressed
  146 + if(ch == -1){
  147 + uint8_t data;
  148 + int status = receiveData(2, &data, handle, endpoint_desc_list);
  149 + if(status == 0){
  150 + if(data & 0x10 == 0x10) ch = KEY_LEFT;
  151 + else if(data & 0x08 == 0x08) ch = ' ';
  152 + else if(data & 0x04 == 0x04) ch = KEY_RIGHT;
  153 + }
  154 + }
140 155  
141 156 switch (status) {
142 157  
... ... @@ -326,10 +341,31 @@ void setUpTimer()
326 341 }
327 342  
328 343  
  344 +int go = 1;
  345 +void signalINT(int sig){
  346 + if(sig == SIGINT){
  347 + go = 0;
  348 + }
  349 +}
329 350 int main(int argc, char **argv)
330 351 {
331   - weite = 0;
332   - score = 0;
  352 + libusb_context *context;
  353 + libusb_device **devices;
  354 + libusb_device_handle *handle;
  355 + struct libusb_config_descriptor *config_desc;
  356 + struct libusb_endpoint_descriptor endpoint_desc_list[ENDPOINTS_NUMBER];
  357 +
  358 + // start
  359 + start(&context, &devices, &handle, &config_desc, endpoint_desc_list);
  360 +
  361 + // singals
  362 + struct sigaction action;
  363 + action.sa_handler = signalINT;
  364 + sigaction(SIGINT, &action, NULL);
  365 +
  366 + // nInvaders
  367 + weite = 0;
  368 + score = 0;
333 369 lives = 3;
334 370 level = 0;
335 371 skill_level = 1;
... ... @@ -344,8 +380,11 @@ int main(int argc, char **argv)
344 380 // read keyboard input
345 381 do {
346 382 // do movements and key-checking
347   - readInput();
348   - } while (0 == 0);
  383 + readInput(handler, endpoint_desc_list);
  384 + } while (go);
  385 +
  386 + // stop
  387 + stop(config_desc, handle, devices, context);
349 388  
350 389 return 0;
351 390 }
... ...
games/ninvaders/ninvaders-0.1.1/player.o 0 → 100644
No preview for this file type
games/ninvaders/ninvaders-0.1.1/ufo.o 0 → 100644
No preview for this file type
games/ninvaders/ninvaders-0.1.1/view.o 0 → 100644
No preview for this file type