-
Notifications
You must be signed in to change notification settings - Fork 1
/
gc_control.c
78 lines (67 loc) · 1.9 KB
/
gc_control.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <avr/interrupt.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include "usb_rawhid.h"
#include "usb_rawhid_settings.h"
#define ENABLE_INT0 (EIMSK |= (1<<0))
#define DISABLE_INT0 (EIMSK &= ~(1<<0))
#define DISABLE_INT0_ASM "cbi 0x1d,0\n\t"
#define CLEAR_INT0_FLAG (EIFR |= (1<<0))
// Receive buffer, will need at most 24 bits
volatile uint8_t gc_rx_buf[3];
// Send buffers
uint8_t const id_status[] = {0x09, 0x00, 0x20};
uint8_t const origins_buf[] = {
0x00, 0x80, // No buttons pressed
0x80, 0x80, // Joystick neutral
0x80, 0x80, // C-stick neutral
0x00, 0x00, // Shoulder L & R not pressed
0x02, 0x02 // unknown
};
uint8_t controller_status_buf[] = {
0x00, // 0 0 0 START Y X B A
0x80, // 1 LSHOULDER RSHOULDER Z UP DOWN RIGHT LEFT
0x80, 0x80, // Joystick X & Y
0x80, 0x80, // C-stick X & Y
0x00, 0x00 // Shoulder button positions
};
#define LED_ON (PORTD |= (1<<6))
#define LED_OFF (PORTD &= ~(1<<6))
#define LED_TOGGLE (PORTD ^= (1<<6))
#define CPU_PRESCALE(n) (CLKPR=0x80, CLKPR=(n))
#define DEBUG_PORT PORTC
#define DEBUG_PIN PINC
#define DEBUG_DDR DDRC
#define CONFIG_DEBUG_PORT (DEBUG_DDR = 0xff, DEBUG_PORT=0x00)
//led=output, data=input w/o pull-up:
#define CONFIG_DATA_AND_LED (DDRD=(1<<6), PORTD=0x00)
// PD0(INT0)=GC_DATA PD6=LED
int main(void)
{
CPU_PRESCALE(0);
CONFIG_DATA_AND_LED;
LED_OFF;
CONFIG_DEBUG_PORT;
usb_init();
while(!usb_configured());
EICRA = 0x02; // Falling edge of GC_DATA(INT0) causes interrupt
CLEAR_INT0_FLAG;
ENABLE_INT0;
sei();
while(1) {
uint8_t recv_bytes;
uint8_t recv_buf[RAWHID_RX_SIZE];
recv_bytes = usb_rawhid_recv(recv_buf, 0);
if (recv_bytes > 0) {
cli();
controller_status_buf[0] = recv_buf[0];
controller_status_buf[1] = recv_buf[1];
sei();
if (recv_buf[0] == 0x00 && recv_buf[1] == 0x80)
LED_OFF;
else
LED_ON;
}
}
}