chiark / gitweb /
wiringPi Version 2 - First commit (of v2)
[wiringPi.git] / devLib / piNes.c
1 /*
2  * piNes.c:
3  *      Driver for the NES Joystick controller on the Raspberry Pi
4  *      Copyright (c) 2012 Gordon Henderson
5  ***********************************************************************
6  * This file is part of wiringPi:
7  *      https://projects.drogon.net/raspberry-pi/wiringpi/
8  *
9  *    wiringPi is free software: you can redistribute it and/or modify
10  *    it under the terms of the GNU Lesser General Public License as
11  *    published by the Free Software Foundation, either version 3 of the
12  *    License, or (at your option) any later version.
13  *
14  *    wiringPi is distributed in the hope that it will be useful,
15  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *    GNU Lesser General Public License for more details.
18  *
19  *    You should have received a copy of the GNU Lesser General Public
20  *    License along with wiringPi.
21  *    If not, see <http://www.gnu.org/licenses/>.
22  ***********************************************************************
23  */
24
25 #include <wiringPi.h>
26
27 #include "piNes.h"
28
29 #define MAX_NES_JOYSTICKS       8
30
31 #define NES_RIGHT       0x01
32 #define NES_LEFT        0x02
33 #define NES_DOWN        0x04
34 #define NES_UP          0x08
35 #define NES_START       0x10
36 #define NES_SELECT      0x20
37 #define NES_B           0x40
38 #define NES_A           0x80
39
40
41 #define PULSE_TIME      25
42
43 // Data to store the pins for each controller
44
45 struct nesPinsStruct
46 {
47   unsigned int cPin, dPin, lPin ;
48 } ;
49
50 static struct nesPinsStruct nesPins [MAX_NES_JOYSTICKS] ;
51
52 static int joysticks = 0 ;
53
54
55 /*
56  * setupNesJoystick:
57  *      Create a new NES joystick interface, program the pins, etc.
58  *********************************************************************************
59  */
60
61 int setupNesJoystick (int dPin, int cPin, int lPin)
62 {
63   if (joysticks == MAX_NES_JOYSTICKS)
64     return -1 ;
65
66   nesPins [joysticks].dPin = dPin ;
67   nesPins [joysticks].cPin = cPin ;
68   nesPins [joysticks].lPin = lPin ;
69
70   digitalWrite (lPin, LOW) ;
71   digitalWrite (cPin, LOW) ;
72
73   pinMode (lPin, OUTPUT) ;
74   pinMode (cPin, OUTPUT) ;
75   pinMode (dPin, INPUT) ;
76
77   return joysticks++ ;
78 }
79
80
81 /*
82  * readNesJoystick:
83  *      Do a single scan of the NES Joystick.
84  *********************************************************************************
85  */
86
87 unsigned int readNesJoystick (int joystick)
88 {
89   unsigned int value = 0 ;
90   int  i ;
91
92   struct nesPinsStruct *pins = &nesPins [joystick] ;
93  
94 // Toggle Latch - which presents the first bit
95
96   digitalWrite (pins->lPin, HIGH) ; delayMicroseconds (PULSE_TIME) ;
97   digitalWrite (pins->lPin, LOW)  ; delayMicroseconds (PULSE_TIME) ;
98
99 // Read first bit
100
101   value = digitalRead (pins->dPin) ;
102
103 // Now get the next 7 bits with the clock
104
105   for (i = 0 ; i < 7 ; ++i)
106   {
107     digitalWrite (pins->cPin, HIGH) ; delayMicroseconds (PULSE_TIME) ;
108     digitalWrite (pins->cPin, LOW)  ; delayMicroseconds (PULSE_TIME) ;
109     value = (value << 1) | digitalRead (pins->dPin) ;
110   }
111
112   return value ^ 0xFF ;
113 }