diff --git a/PartiePC/PC.c b/PartiePC/PC.c new file mode 100644 index 0000000..ef08e56 --- /dev/null +++ b/PartiePC/PC.c @@ -0,0 +1,172 @@ +#include +#include +#include +#include +#include + +// arduino ID vendor and product +#define ID_VENDOR 0x2341 +#define ID_PRODUCT 0x01 +#define ENDPOINTS_NUMBER 4 +#define TIMEOUT 50 +#define MAX_DATA 50 + +void init(libusb_context **context, libusb_device ***devices, ssize_t *devices_count){ + int status = libusb_init(context); + if(status != 0){perror("libusb_init"); exit(-1);} + + *devices_count = libusb_get_device_list(*context, devices); + if(*devices_count < 0){perror("libusb_get_device_list"); exit(-1);} +} + +libusb_device* searchArduino(libusb_device **devices, ssize_t devices_count){ + for(int i=0; ibConfigurationValue; + + // detach kernel + for(int j=0; j<(*config_desc)->bNumInterfaces; j++){ + int interface = (*config_desc)->interface[j].altsetting[0].bInterfaceNumber; + if(libusb_kernel_driver_active(*handle, interface)){ + status = libusb_detach_kernel_driver(*handle, interface); + if(status != 0){ perror("libusb_detach_kernel_driver"); exit(-1); } + } + } + + // use config + status = libusb_set_configuration(*handle, configuration); + if(status != 0){ perror("libusb_set_configuration"); exit(-1); } + + // claim interfaces + for(int j=0; j<(*config_desc)->bNumInterfaces; j++){ + struct libusb_interface_descriptor interface_desc = (*config_desc)->interface[j].altsetting[0]; + int interface = interface_desc.bInterfaceNumber; + + status = libusb_claim_interface(*handle, interface); + if(status != 0){ perror("libusb_claim_interface"); exit(-1); } + } +} + +void getEndpoints(struct libusb_config_descriptor *config_desc, struct libusb_endpoint_descriptor *endpoint_desc_list){ + int count = 0; + // in interfaces + for(int j=0; jbNumInterfaces; j++){ + // find endpoints + for(int k=0; kinterface[j].altsetting[0].bNumEndpoints; k++){ + if(count > ENDPOINTS_NUMBER){ printf("getEndpoints: Array out of bound :%d:", count); exit(-1); } + *(endpoint_desc_list + count) = config_desc->interface[j].altsetting[0].endpoint[k]; + count++; + } + } + //if(count != ENDPOINTS_NUMBER){ printf("Wrong number of endpoints.\nIs this the good device ?\n"); exit(-1); } +} + +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){ + // init + ssize_t devices_count; + init(context, devices, &devices_count); + + // get arduino device + libusb_device *arduino = searchArduino(*devices, devices_count); + if(arduino == NULL){ printf("Arduino device not found\n"); exit(-1); } + + // open connection, use config, detach kernel and claim interfaces + openConnection(arduino, handle, config_desc); + + // get enpoints + getEndpoints(*config_desc, endpoint_desc_list); +} + +void stop(struct libusb_config_descriptor *config_desc, libusb_device_handle *handle, libusb_device **devices, libusb_context *context){ + // release interfaces + for(int j=0; jbNumInterfaces; j++){ + struct libusb_interface_descriptor interface_desc = config_desc->interface[j].altsetting[0]; + int interface = interface_desc.bInterfaceNumber; + + int status = libusb_release_interface(handle, interface); + if(status != 0){ perror("libusb_release_interface"); exit(-1); } + } + + // free config + libusb_free_config_descriptor(config_desc); + + // close connection + libusb_close(handle); + + // free device list + libusb_free_device_list(devices, 1); + + // libusb exit + libusb_exit(context); +} + +void sendData(int endpoint_id, uint8_t data, libusb_device_handle *handle, struct libusb_endpoint_descriptor *endpoint_desc_list){ + if(endpoint_id < 0 || endpoint_id > 1){ printf("(sendData) Wrong endpoint !\nMust be 0 or 1\n"); return; } + int status = libusb_interrupt_transfer(handle, endpoint_desc_list[endpoint_id].bEndpointAddress, (unsigned char *) &data, 1, NULL, TIMEOUT); + if(status!=0 && status!=LIBUSB_ERROR_TIMEOUT){ perror("libusb_interrupt_transfer"); exit(-1); } +} + +int receiveData(int endpoint_id, uint8_t *data, libusb_device_handle *handle, struct libusb_endpoint_descriptor *endpoint_desc_list){ + if(endpoint_id < 2 || endpoint_id > 3){ printf("(sendData) Wrong endpoint !\nMust be 2 or 3\n"); return -1; } + int status = libusb_interrupt_transfer(handle, endpoint_desc_list[endpoint_id].bEndpointAddress, (unsigned char *) data, 1, NULL, TIMEOUT); + if(status!=0 && status!=LIBUSB_ERROR_TIMEOUT){ perror("libusb_interrupt_transfer"); exit(-1); } + return status; +} + +int go = 1; +void signalINT(int sig){ + if(sig == SIGINT){ + go = 0; + } +} +int main(){ + libusb_context *context; + libusb_device **devices; + libusb_device_handle *handle; + struct libusb_config_descriptor *config_desc; + struct libusb_endpoint_descriptor endpoint_desc_list[ENDPOINTS_NUMBER]; + + // start + printf("Starting...\n"); + start(&context, &devices, &handle, &config_desc, endpoint_desc_list); + printf("Start complete\n"); + + // singals + struct sigaction action; + action.sa_handler = signalINT; + sigaction(SIGINT, &action, NULL); + + uint8_t data; + while(go){ + int status = receiveData(2, &data, handle, endpoint_desc_list); + if(status == 0) + sendData(0, data, handle, endpoint_desc_list); + } + + // stop + printf("\nStopping...\n"); + stop(config_desc, handle, devices, context); + printf("Stop complete...\n"); +} diff --git a/PartiePC/a.out b/PartiePC/a.out index 4fc331a..50a93db 100755 Binary files a/PartiePC/a.out and b/PartiePC/a.out differ diff --git a/PartiePC/makefile b/PartiePC/makefile index a31e71f..29d9606 100644 --- a/PartiePC/makefile +++ b/PartiePC/makefile @@ -1,8 +1,9 @@ all: - gcc PCSend.c -l usb-1.0 -Wall -Wextra + gcc PC.c -l usb-1.0 -Wall -Wextra clean: rm -f *.o a.out exec: all ./a.out + diff --git a/atmega328p/Makefile b/atmega328p/Makefile new file mode 100755 index 0000000..1230694 --- /dev/null +++ b/atmega328p/Makefile @@ -0,0 +1,37 @@ +export CC = avr-gcc + +export MCU = atmega328p +export TARGET_ARCH = -mmcu=$(MCU) + +export CFLAGS = -Wall -I. -DF_CPU=16000000 -Os #-g +export LDFLAGS = -g $(TARGET_ARCH) -lm -Wl,--gc-sections # -Os + +TARGET = usb +TERM = /dev/ttyACM0 +CPPFLAGS = -mmcu=$(MCU) +PGMERISP = -c stk500v1 -b 115200 -P $(TERM) +ARVDUDECONF= -C /usr/local/arduino/arduino-0022/hardware/tools/avrdude.conf +export DUDE = /usr/bin/avrdude -F -v -p $(MCU) $(AVRDUDECONF) + +C_SRC = $(wildcard *.c) +OBJS = $(C_SRC:.c=.o) + +all: $(TARGET).hex + +clean: + rm -f *.o *.hex *.elf + +%.o:%.c + $(CC) -c $(CPPFLAGS) $(CFLAGS) $< -o $@ + +$(TARGET).elf: $(OBJS) + $(CC) $(LDFLAGS) -o $@ $(OBJS) + +$(TARGET).hex: $(TARGET).elf + avr-objcopy -j .text -j .data -O ihex $(TARGET).elf $(TARGET).hex + avr-objcopy -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 -O ihex $(TARGET).elf eeprom.hex + +upload: $(TARGET).hex + stty -F $(TERM) hupcl # reset + $(DUDE) $(PGMERISP) -U flash:w:$(TARGET).hex + diff --git a/atmega328p/main.c b/atmega328p/main.c new file mode 100755 index 0000000..7510a8e --- /dev/null +++ b/atmega328p/main.c @@ -0,0 +1,372 @@ +#include //I-O registers +#include +#include //_delay_ms + +#define NB_TICK 104 //1563 +#define CPU_FREQ 16000000L //Frequence du CPU +#define QUANTUM_ms 10 + +#define SAVE_CONTEXT() \ +asm volatile ( \ +"push r0 \n\t" \ +"in r0, __SREG__ \n\t" \ +"cli \n\t" \ +"push r0 \n\t" \ +"push r1 \n\t" \ +"clr r1 \n\t" \ +"push r2 \n\t" \ +"push r3 \n\t" \ +"push r4 \n\t" \ +"push r5 \n\t" \ +"push r6 \n\t" \ +"push r7 \n\t" \ +"push r8 \n\t" \ +"push r9 \n\t" \ +"push r10 \n\t" \ +"push r11 \n\t" \ +"push r12 \n\t" \ +"push r13 \n\t" \ +"push r14 \n\t" \ +"push r15 \n\t" \ +"push r16 \n\t" \ +"push r17 \n\t" \ +"push r18 \n\t" \ +"push r19 \n\t" \ +"push r20 \n\t" \ +"push r21 \n\t" \ +"push r22 \n\t" \ +"push r23 \n\t" \ +"push r24 \n\t" \ +"push r25 \n\t" \ +"push r26 \n\t" \ +"push r27 \n\t" \ +"push r28 \n\t" \ +"push r29 \n\t" \ +"push r30 \n\t" \ +"push r31 \n\t" \ +); + +#define RESTORE_CONTEXT() \ +asm volatile ( \ +"pop r31 \n\t" \ +"pop r30 \n\t" \ +"pop r29 \n\t" \ +"pop r28 \n\t" \ +"pop r27 \n\t" \ +"pop r26 \n\t" \ +"pop r25 \n\t" \ +"pop r24 \n\t" \ +"pop r23 \n\t" \ +"pop r22 \n\t" \ +"pop r21 \n\t" \ +"pop r20 \n\t" \ +"pop r19 \n\t" \ +"pop r18 \n\t" \ +"pop r17 \n\t" \ +"pop r16 \n\t" \ +"pop r15 \n\t" \ +"pop r14 \n\t" \ +"pop r13 \n\t" \ +"pop r12 \n\t" \ +"pop r11 \n\t" \ +"pop r10 \n\t" \ +"pop r9 \n\t" \ +"pop r8 \n\t" \ +"pop r7 \n\t" \ +"pop r6 \n\t" \ +"pop r5 \n\t" \ +"pop r4 \n\t" \ +"pop r3 \n\t" \ +"pop r2 \n\t" \ +"pop r1 \n\t" \ +"pop r0 \n\t" \ +"out __SREG__, r0 \n\t" \ +"pop r0 \n\t" \ +); + +/* +protocole de communication choisi +16u2 vers 328p (led) -> 1 bit = état d'une led + +328p vers 16u2 (boutons + joysticks) -> enfonction du bit 7 : +à 0 : boutons puis un bit = état du bouton +à 1 : joysticks -> bit 6 = direction x ou y puis le reste, la valeur sur 6 bits +*/ + +struct task{ + uint16_t sp_vise; + uint8_t state; +}; + +uint8_t cpt = 0; +uint8_t premier_lancement = 0; +struct task lecture_boutons = {0x300, 0, 0}; +struct task lecture_joystick = {0x0500, 0}; +struct task affiche_led = {0x0700, 0, 0}; + + + + + + + + + + +//debug +void init_debug(void)//permet de detecter un reboot +{ + DDRB |= 0x1F; + PORTB |= 0xFF; + _delay_ms(300); + PORTB &= 0x00; +} + + + + + + + + + + +//gestion de la liaison serie +void init_serial(int speed) +{ + UBRR0 = CPU_FREQ/(((unsigned long int)speed)<<4)-1;//Set baud rate + UCSR0B = (1< CLK/1024 prescaler + TCCR1B |= _BV(CS10); + OCR1A = NB_TICK; + TIMSK1 |= _BV(OCIE1A); +} + + + + + + + + + + +//gestion de l'initialisation des differentes taches +void init_tasks(void) +{ + DDRD |= 0x00; + PORTD |= 0xFF; + DDRB |= 0b00111111; +} + + + + + + + + + + +//gestion de l'execution des differentes taches +void task_lecture_boutons(void) +{ + uint8_t tmp = 0x00; + uint8_t tmp2; + while(1){ + tmp2 = 0x00; + if((PIND&(1<<2)) == 0) tmp2 |= 0b00000001; + if((PIND&(1<<3)) == 0) tmp2 |= 0b00000010; + if((PIND&(1<<4)) == 0) tmp2 |= 0b00000100; + if((PIND&(1<<5)) == 0) tmp2 |= 0b00001000; + if((PIND&(1<<6)) == 0) tmp2 |= 0b00010000; + if(tmp2 != tmp) { send_serial(tmp2); tmp = tmp2; } + _delay_ms(QUANTUM_ms); + } +} + +void task_lecture_joystick(void) +{ + uint8_t tmp = ADC_read(0)>>4;//code sur 6 bits (2 bits de descriptions necessaires) + uint8_t tmp2 = ADC_read(1)>>4; + uint8_t test = 0; + uint8_t tmp3; + uint8_t tmp4; + while(1){ + tmp3 = ADC_read(0)>>4; + tmp4 = ADC_read(1)>>4; + if(test == 0 && tmp3 != tmp) + { + send_serial(tmp3 | 0b10000000); + tmp = tmp3; + } + else if(test == 1 && tmp4 != tmp2) + { + send_serial(tmp4 | 0b11000000); + tmp2 = tmp4; + } + if(test == 0) test = 1; else test = 0; + _delay_ms(QUANTUM_ms); + } +} + +void task_affiche_led(void) +{ + while(1){ + PORTB = get_serial(); + _delay_ms(QUANTUM_ms); + } +} + + + + + + + + + + +//gestion du contexte +ISR(TIMER1_COMPA_vect) +{ + if(premier_lancement==0) + { + premier_lancement+= 1; + sei(); + SP = affiche_led.sp_vise; + task_affiche_led(); + } + else if(premier_lancement==1) + { + SAVE_CONTEXT(); + affiche_led.sp_vise = SP; + premier_lancement+=1; + sei(); + SP = lecture_joystick.sp_vise; + task_lecture_joystick(); + } + else if(premier_lancement==2) + { + SAVE_CONTEXT(); + lecture_joystick.sp_vise = SP; + premier_lancement+=1; + sei(); + SP = lecture_boutons.sp_vise; + task_lecture_boutons(); + } + + + else + { + if(cpt==0) + { + SAVE_CONTEXT(); + lecture_boutons.sp_vise = SP; + SP = affiche_led.sp_vise; + RESTORE_CONTEXT(); + cpt++; + } + else if(cpt==1) + { + SAVE_CONTEXT(); + affiche_led.sp_vise = SP; + SP = lecture_joystick.sp_vise; + RESTORE_CONTEXT(); + cpt++; + } + else if(cpt==2) + { + SAVE_CONTEXT(); + lecture_joystick.sp_vise = SP; + SP = lecture_boutons.sp_vise; + RESTORE_CONTEXT(); + cpt = 0; + } + } + sei(); +} + + + + + + + + + + +int main(void) +{ + init_debug();//allume toutes les leds au reset + init_serial(9600); + init_ADC(); + init_timer(); + + init_tasks(); + sei(); + + while(1) + {} + return 0; +} diff --git a/games/ninvaders/ninvaders-0.1.1/Makefile b/games/ninvaders/ninvaders-0.1.1/Makefile deleted file mode 100644 index d815e2f..0000000 --- a/games/ninvaders/ninvaders-0.1.1/Makefile +++ /dev/null @@ -1,16 +0,0 @@ -CC=gcc -CFLAGS=-O3 -Wall -LIBS=-lncurses - -CFILES=globals.c view.c aliens.c ufo.c player.c nInvaders.c -HFILES=globals.h view.h aliens.h ufo.h player.h nInvaders.h -OFILES=globals.o view.o aliens.o ufo.o player.o nInvaders.o -all: nInvaders - -nInvaders: $(OFILES) $(HFILES) - $(CC) $(LDFLAGS) -o$@ $(OFILES) $(LIBS) - -.c.o: - $(CC) -c -I. $(CFLAGS) $(OPTIONS) $< -clean: - rm -f nInvaders $(OFILES) diff --git a/games/ninvaders/ninvaders_0.1.1-3.diff.gz b/games/ninvaders/ninvaders_0.1.1-3.diff.gz deleted file mode 100644 index 12ee93d..0000000 Binary files a/games/ninvaders/ninvaders_0.1.1-3.diff.gz and /dev/null differ diff --git a/games/ninvaders/ninvaders_0.1.1-3.dsc b/games/ninvaders/ninvaders_0.1.1-3.dsc deleted file mode 100644 index 7a14302..0000000 --- a/games/ninvaders/ninvaders_0.1.1-3.dsc +++ /dev/null @@ -1,28 +0,0 @@ ------BEGIN PGP SIGNED MESSAGE----- -Hash: SHA1 - -Format: 1.0 -Source: ninvaders -Binary: ninvaders -Architecture: any -Version: 0.1.1-3 -Maintainer: Matthew Palmer -Standards-Version: 3.9.1 -Build-Depends: debhelper (>= 7), libncurses5-dev -Checksums-Sha1: - 5ab825694b108cbfa988377ca216188fa9a76e89 31275 ninvaders_0.1.1.orig.tar.gz - 38760243b3404fecb143f1b372f7c2cad76696f2 12831 ninvaders_0.1.1-3.diff.gz -Checksums-Sha256: - bfbc5c378704d9cf5e7fed288dac88859149bee5ed0850175759d310b61fd30b 31275 ninvaders_0.1.1.orig.tar.gz - 30297a890afd6ea21b11c514d55ebbfe26ab5e9dcbee3499f0f223a3fd52de4c 12831 ninvaders_0.1.1-3.diff.gz -Files: - 97b2c3fb082241ab5c56ab728522622b 31275 ninvaders_0.1.1.orig.tar.gz - 56a98ddce225b5f9f9302f64209916f0 12831 ninvaders_0.1.1-3.diff.gz - ------BEGIN PGP SIGNATURE----- -Version: GnuPG v1.4.9 (GNU/Linux) - -iD8DBQFMqpNMBEnrTWk1E4cRAl63AJ0bMEmA1mEyY2qE/1FJ5MPJ0wnppQCgo8F0 -fPFphbmDT6OBCUb7xtk++QQ= -=AcHr ------END PGP SIGNATURE----- diff --git a/games/ninvaders/ninvaders_0.1.1.orig.tar.gz b/games/ninvaders/ninvaders_0.1.1.orig.tar.gz deleted file mode 100644 index 2cec10d..0000000 Binary files a/games/ninvaders/ninvaders_0.1.1.orig.tar.gz and /dev/null differ -- libgit2 0.21.2