-
Notifications
You must be signed in to change notification settings - Fork 23
/
gamepad.c
109 lines (91 loc) · 2.89 KB
/
gamepad.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
* SNESDev - User-space driver for the RetroPie GPIO Adapter for the Raspberry Pi.
*
* (c) Copyright 2012-2013 Florian Müller (contact@petrockblock.com)
*
* SNESDev homepage: https://github.com/petrockblog/SNESDev-RPi
*
* Permission to use, copy, modify and distribute SNESDev in both binary and
* source form, for non-commercial purposes, is hereby granted without fee,
* providing that this license information and copyright notice appear with
* all copies and any derived work.
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event shall the authors be held liable for any damages
* arising from the use of this software.
*
* SNESDev is freeware for PERSONAL USE only. Commercial users should
* seek permission of the copyright holders first. Commercial use includes
* charging money for SNESDev or software derived from SNESDev.
*
* The copyright holders request that bug fixes and improvements to the code
* should be forwarded to them so everyone can benefit from the modifications
* in future versions.
*
* Raspberry Pi is a trademark of the Raspberry Pi Foundation.
*/
#include <bcm2835.h>
#include <stdio.h>
#include <string.h>
#include "gamepad.h"
#include "GPIO.h"
int16_t gpad_open(GPAD_ST* const gpad) {
gpad->state = 0;
gpio_open(gpad->port, gpad->pin_strobe, GPIO_OUTPUT);
gpio_open(gpad->port, gpad->pin_clock, GPIO_OUTPUT);
gpio_open(gpad->port, gpad->pin_data, GPIO_INPUT);
gpio_write_pin(gpad->port, gpad->pin_strobe, GPIO_LOW);
gpio_write_pin(gpad->port, gpad->pin_clock, GPIO_LOW);
return 0;
}
int16_t gpad_close() {
return -1;
}
int16_t gpad_ioctrl() {
return -1;
}
int16_t gpad_read(GPAD_ST* const gpad) {
int16_t i;
gpio_write_pin(gpad->port, gpad->pin_strobe, GPIO_HIGH);
delayMicroseconds(2);
gpio_write_pin(gpad->port, gpad->pin_strobe, GPIO_LOW);
delayMicroseconds(2);
gpad->state = 0;
switch (gpad->type) {
case GPAD_TYPE_SNES:
for (i = 0; i < 16; i++) {
uint8_t curpin1 = gpio_read_pin(gpad->port, gpad->pin_data);
gpio_write_pin(gpad->port, gpad->pin_clock, GPIO_HIGH);
delayMicroseconds(2);
gpio_write_pin(gpad->port, gpad->pin_clock, GPIO_LOW);
delayMicroseconds(2);
if (curpin1 == GPIO_LOW) {
gpad->state |= (1 << i);
}
}
// set to 0 if the controller is not connected
if ((gpad->state & 0xFFF) == 0xFFF) {
gpad->state = 0;
}
break;
case GPAD_TYPE_NES:
for (i = 0; i < 8; i++) {
uint8_t curpin1 = gpio_read_pin(gpad->port, gpad->pin_data);
gpio_write_pin(gpad->port, gpad->pin_clock, GPIO_HIGH);
delayMicroseconds(2);
gpio_write_pin(gpad->port, gpad->pin_clock, GPIO_LOW);
delayMicroseconds(2);
if (curpin1 == GPIO_LOW) {
gpad->state |= (1 << i);
}
}
// set to 0 if the controller is not connected
if ((gpad->state & 0xFFF) == 0xFFF) {
gpad->state = 0;
}
break;
default:
break;
}
return gpad->state;
}