Blame view

atmega16u2/custom/lufa-LUFA-170418/PolytechLille/PAD/PAD.c 3.54 KB
8a7dc1f5   adorian   Seance 4
1
2
  #include "PAD.h"
  
8a7dc1f5   adorian   Seance 4
3
4
5
  /** Main program entry point. This routine contains the overall program flow, including initial
   *  setup of all components and the main program loop.
   */
97064000   root   16u2 desc terminé...
6
  int main(void){
8a7dc1f5   adorian   Seance 4
7
8
  	SetupHardware();
  
bfb1101b   pifou   com 16u2 <-> PC c...
9
  	LEDs_SetAllLEDs(LEDS_LED1 | LEDS_LED2);
8a7dc1f5   adorian   Seance 4
10
11
  	GlobalInterruptEnable();
  
97064000   root   16u2 desc terminé...
12
13
  	for (;;){
  		USB_USBTask();
bfb1101b   pifou   com 16u2 <-> PC c...
14
15
16
  		PAD_Task();
  	}
  }
97064000   root   16u2 desc terminé...
17
  
58b09a0b   root   Ca marche git status
18
  uint8_t led = 0;
bfb1101b   pifou   com 16u2 <-> PC c...
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
  void PAD_Task(void){
  	if (USB_DeviceState != DEVICE_STATE_Configured)
  		return;
  	LEDs_SetAllLEDs(0);
  
  	// receive from 328p & send to PC
  	/* Notre convention :
  	 *
  	 *  328p -> 12u2 : 0bXYZZZZZZ
  	 * X = 0 -> les Z sont les états de chaque boutons
  	 * X = 1 & Y = 0 -> les Z correspondent à la valeur X du joystick
  	 * X = 1 & Y = 1 -> les Z correspondent à la valeur Y du joystick
  	 * 
  	 * Cas 0b00ZZZZZZ : (boutons)
  	 *  12u2 -> PC (endpoint button 1) : 0b000ZZZZZ
  	 * Z : état d'un bouton
  	
  	 * Cas 0b1YZZZZZZ : (joystick)
58b09a0b   root   Ca marche git status
37
38
39
  	 *  12u2 -> PC (endpoint button 2) : 0bX0YYYYYY
  	 * X = 0 -> valeur en X du joystick
  	 * X = 1 -> valeur en Y du joystick
bfb1101b   pifou   com 16u2 <-> PC c...
40
41
42
43
44
45
  	 */
  	if(Serial_IsCharReceived()){
  		uint8_t byte = Serial_ReceiveByte();
  		if((byte & 0x80) == 0){ // bouttons
  			Endpoint_SelectEndpoint(PAD_IN_EP_BUTTON1);
  			if (Endpoint_IsINReady() && Endpoint_IsReadWriteAllowed()){
58b09a0b   root   Ca marche git status
46
  				LEDs_SetAllLEDs(LEDS_LED1); //LED Tx
bfb1101b   pifou   com 16u2 <-> PC c...
47
48
  				Endpoint_Write_8(byte);
  				Endpoint_ClearIN();
97064000   root   16u2 desc terminé...
49
  			}
bfb1101b   pifou   com 16u2 <-> PC c...
50
  		}else if((byte & 0x40) == 0){ // joystick X
58b09a0b   root   Ca marche git status
51
  			uint8_t toSend = (byte) & 0x3F;
bfb1101b   pifou   com 16u2 <-> PC c...
52
53
  			Endpoint_SelectEndpoint(PAD_IN_EP_BUTTON2);
  			if (Endpoint_IsINReady() && Endpoint_IsReadWriteAllowed()){
58b09a0b   root   Ca marche git status
54
55
  				LEDs_SetAllLEDs(LEDS_LED1); //LED Tx
  				Endpoint_Write_8(toSend);
bfb1101b   pifou   com 16u2 <-> PC c...
56
  				Endpoint_ClearIN();
bfb1101b   pifou   com 16u2 <-> PC c...
57
58
  			}
  		}else{ // joystick Y
58b09a0b   root   Ca marche git status
59
  			uint8_t toSend = ((byte) & 0x3F) | 0x80;
bfb1101b   pifou   com 16u2 <-> PC c...
60
61
  			Endpoint_SelectEndpoint(PAD_IN_EP_BUTTON2);
  			if (Endpoint_IsINReady() && Endpoint_IsReadWriteAllowed()){
58b09a0b   root   Ca marche git status
62
63
  				LEDs_SetAllLEDs(LEDS_LED1); //LED Tx
  				Endpoint_Write_8(toSend);
97064000   root   16u2 desc terminé...
64
  				Endpoint_ClearIN();
bfb1101b   pifou   com 16u2 <-> PC c...
65
  			}
97064000   root   16u2 desc terminé...
66
67
  		}
  	}
bfb1101b   pifou   com 16u2 <-> PC c...
68
69
70
71
72
73
74
75
76
77
78
  
  	// receive from PC & send to 328p
  	/* Notre convention :
  	 *
  	 *  PC (endpoint led 1) -> 16u2 : 0b000XXXXX
  	 * X = état d'une LED (LEDs 8 9 10 11 12)
  	 *
  	 *  PC (endpoint led 2) -> 16u2 : 0b0000000X
  	 * X = état d'une LED (LED 13)
  	 * 
  	 *  16u2 -> 328p : 0bXYYYYYYY
58b09a0b   root   Ca marche git status
79
  	 * X = 0 -> pour les LEDs 8 9 10 11 12 13 (Y : les données)
bfb1101b   pifou   com 16u2 <-> PC c...
80
81
82
  	 */
  	Endpoint_SelectEndpoint(PAD_OUT_EP_LED1);
  	if (Endpoint_IsOUTReceived() && Endpoint_IsReadWriteAllowed()){
58b09a0b   root   Ca marche git status
83
  		LEDs_SetAllLEDs(LEDS_LED2); //LED Rx
bfb1101b   pifou   com 16u2 <-> PC c...
84
  		uint8_t byte = Endpoint_Read_8();
58b09a0b   root   Ca marche git status
85
86
  		led = (led & 0x20) | byte;
  		Serial_SendByte(led);
bfb1101b   pifou   com 16u2 <-> PC c...
87
88
  		Endpoint_ClearOUT();
  	}
58b09a0b   root   Ca marche git status
89
  	Endpoint_SelectEndpoint(PAD_OUT_EP_LED2);
bfb1101b   pifou   com 16u2 <-> PC c...
90
  	if (Endpoint_IsOUTReceived() && Endpoint_IsReadWriteAllowed()){
58b09a0b   root   Ca marche git status
91
  		LEDs_SetAllLEDs(LEDS_LED2); //LED Rx
bfb1101b   pifou   com 16u2 <-> PC c...
92
  		uint8_t byte = Endpoint_Read_8();
58b09a0b   root   Ca marche git status
93
94
  		led = (led & 0x1F) | byte << 5;
  		Serial_SendByte(led);
bfb1101b   pifou   com 16u2 <-> PC c...
95
96
  		Endpoint_ClearOUT();
  	}
58b09a0b   root   Ca marche git status
97
98
99
  
  	//LED Reset
  	LEDs_SetAllLEDs(0);
8a7dc1f5   adorian   Seance 4
100
101
102
  }
  
  /** Configures the board hardware and chip peripherals for the project's functionality. */
97064000   root   16u2 desc terminé...
103
  void SetupHardware(void){
8a7dc1f5   adorian   Seance 4
104
105
106
107
108
109
110
111
112
113
114
  #if (ARCH == ARCH_AVR8)
  	/* Disable watchdog if enabled by bootloader/fuses */
  	MCUSR &= ~(1 << WDRF);
  	wdt_disable();
  
  	/* Disable clock division */
  	clock_prescale_set(clock_div_1);
  #endif
  
  	/* Hardware Initialization */
  	USB_Init();
bfb1101b   pifou   com 16u2 <-> PC c...
115
  	LEDs_Init();
97064000   root   16u2 desc terminé...
116
117
  	Serial_Init(9600, 0);
  }
8a7dc1f5   adorian   Seance 4
118
  
97064000   root   16u2 desc terminé...
119
  // endpoints creation
bfb1101b   pifou   com 16u2 <-> PC c...
120
  void EVENT_USB_Device_ConfigurationChanged(void){
97064000   root   16u2 desc terminé...
121
122
  	/* Setup HID Report Endpoints */
  	Endpoint_ConfigureEndpoint(PAD_OUT_EP_LED1  , EP_TYPE_INTERRUPT, PAD_EPSIZE, 1);
58b09a0b   root   Ca marche git status
123
  	Endpoint_ConfigureEndpoint(PAD_OUT_EP_LED2  , EP_TYPE_INTERRUPT, PAD_EPSIZE, 1);
97064000   root   16u2 desc terminé...
124
125
  	Endpoint_ConfigureEndpoint(PAD_IN_EP_BUTTON1, EP_TYPE_INTERRUPT, PAD_EPSIZE, 1);
  	Endpoint_ConfigureEndpoint(PAD_IN_EP_BUTTON2, EP_TYPE_INTERRUPT, PAD_EPSIZE, 1);
8a7dc1f5   adorian   Seance 4
126
  }