-
Notifications
You must be signed in to change notification settings - Fork 3
/
3do.c
79 lines (62 loc) · 1.5 KB
/
3do.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
79
#include <avr/io.h>
#include <util/delay.h>
#include "report.h"
#include "3do.h"
/* D0 - GND OUT
D3 - 5V OUT
D4 - Audio.1 IN
D5 - Audio.2 IN
D6 - P/S OUT
D7 - Clock OUT
B0 - Data IN
*/
#define PS (1<<6)
#define CLK (1<<7)
#define DAT (1<<0)
void Read3DO(report_t *reportBuffer)
{
uchar temp;
PORTD |= 0b11111000; // See table above
PORTD &= 0b11111110;
DDRD |= 0b11001001;
DDRD &= 0b11001111;
DDRB &= 0b11111110; // Data as input with pull-up
PORTB |= 0b00000001;
PORTD &= ~PS; // P/S low to start reading bits
// Bit 0 1 2 3 4 5 6 7
// Lf Rt Up Dn 0 0 1 0
temp = TDOReadByte();
// Check for controller ID
if ((temp & 0xf0) != 0b01000000)
return;
if (temp & (1<<0)) reportBuffer->x = -127;
if (temp & (1<<1)) reportBuffer->x = 127;
if (temp & (1<<2)) reportBuffer->y = -127;
if (temp & (1<<3)) reportBuffer->y = 127;
// Bit 0 1 2 3 4 5 6 7
// 0 L R X P C B A
temp = TDOReadByte();
if (temp & (1<<1)) reportBuffer->b1 |= (1<<6);
if (temp & (1<<2)) reportBuffer->b1 |= (1<<7);
if (temp & (1<<3)) reportBuffer->b2 |= (1<<1);
if (temp & (1<<4)) reportBuffer->b2 |= (1<<0);
if (temp & (1<<5)) reportBuffer->b1 |= (1<<2);
if (temp & (1<<6)) reportBuffer->b1 |= (1<<1);
if (temp & (1<<7)) reportBuffer->b1 |= (1<<0);
PORTD |= PS; // End of data transfer
}
uchar TDOReadByte()
{
uchar i, temp;
temp = 0;
for (i = 0; i<8; i++)
{
temp = temp << 1;
if (PINB & DAT) temp |= 1;
PORTD &= ~CLK;
_delay_us(4);
PORTD |= CLK;
_delay_us(4);
}
return temp;
}