11/*
22 * Sega Controller Reader
33 * Author: Jon Thysell <thysell@gmail.com>
4- * Version: 1.0
5- * Date: 7/26 /2014
4+ * Version: 1.1
5+ * Date: 9/29 /2014
66 *
77 * Reads buttons presses from Sega Genesis 3/6 button controllers
88 * and reports their state via the Serial connection. Handles hot
1111 *
1212 */
1313
14+ const int PLAYERS = 2 ;
15+
1416// Controller Button Flags
1517const int ON = 1 ;
1618const int UP = 2 ;
@@ -24,6 +26,7 @@ const int C = 256;
2426const int X = 512 ;
2527const int Y = 1024 ;
2628const int Z = 2048 ;
29+ const int MODE = 4096 ;
2730
2831// Controller DB9 Pin 7 Mappings
2932const int SELECT[] = { 8 , 9 };
@@ -40,20 +43,20 @@ typedef struct
4043// Controller DB9 Pin to Button Flag Mappings
4144// First column is the controller index, second column
4245// is the Arduino pin that the controller's DB9 pin is
43- // attached to
46+ // attached to, remaing columns are the button flags
4447input inputMap[] = {
45- { 0 , 2 , UP, UP, Z}, // P0 DB9 Pin 1
46- { 0 , 3 , DOWN, DOWN, Y}, // P0 DB9 Pin 2
47- { 0 , 4 , ON, LEFT, X}, // P0 DB9 Pin 3
48- { 0 , 5 , ON, RIGHT, 0 }, // P0 DB9 Pin 4
49- { 0 , 6 , A, B, 0 }, // P0 DB9 Pin 6
50- { 0 , 7 , START, C, 0 }, // P0 DB9 Pin 9
51- { 1 , A0, UP, UP, Z}, // P1 DB9 Pin 1
52- { 1 , A1, DOWN, DOWN, Y}, // P1 DB9 Pin 2
53- { 1 , A2, ON, LEFT, X}, // P1 DB9 Pin 3
54- { 1 , A3, ON, RIGHT, 0 }, // P1 DB9 Pin 4
55- { 1 , A4, A, B, 0 }, // P1 DB9 Pin 6
56- { 1 , A5, START, C, 0 } // P1 DB9 Pin 9
48+ { 0 , 2 , UP, UP, Z }, // P0 DB9 Pin 1
49+ { 0 , 3 , DOWN, DOWN, Y }, // P0 DB9 Pin 2
50+ { 0 , 4 , ON, LEFT, X }, // P0 DB9 Pin 3
51+ { 0 , 5 , ON, RIGHT, MODE }, // P0 DB9 Pin 4
52+ { 0 , 6 , A, B, 0 }, // P0 DB9 Pin 6
53+ { 0 , 7 , START, C, 0 }, // P0 DB9 Pin 9
54+ { 1 , A0, UP, UP, Z }, // P1 DB9 Pin 1
55+ { 1 , A1, DOWN, DOWN, Y }, // P1 DB9 Pin 2
56+ { 1 , A2, ON, LEFT, X }, // P1 DB9 Pin 3
57+ { 1 , A3, ON, RIGHT, MODE }, // P1 DB9 Pin 4
58+ { 1 , A4, A, B, 0 }, // P1 DB9 Pin 6
59+ { 1 , A5, START, C, 0 } // P1 DB9 Pin 9
5760};
5861
5962// Controller State
@@ -71,14 +74,14 @@ void setup()
7174 pinMode (inputMap[i].pin , INPUT);
7275 digitalWrite (inputMap[i].pin , HIGH);
7376 }
74-
77+
7578 // Setup select pins
76- for (int i = 0 ; i < 2 ; i++)
79+ for (int i = 0 ; i < PLAYERS ; i++)
7780 {
7881 pinMode (SELECT[i], OUTPUT);
7982 digitalWrite (SELECT[i], HIGH);
8083 }
81-
84+
8285 Serial.begin (9600 );
8386}
8487
@@ -90,7 +93,7 @@ void loop()
9093
9194void readButtons ()
9295{
93- for (int i = 0 ; i < 2 ; i++)
96+ for (int i = 0 ; i < PLAYERS ; i++)
9497 {
9598 resetState (i);
9699 if (sixButtonMode[i])
@@ -148,49 +151,49 @@ void read3buttons(int player)
148151 {
149152 sixButtonMode[player] = false ;
150153 }
151-
154+
152155 delayMicroseconds (20 );
153156}
154157
155158void read6buttons (int player)
156- {
159+ {
157160 // Poll for three-button states twice
158161 read3buttons (player);
159162 read3buttons (player);
160-
163+
161164 // After two three-button polls, pulse the SELECT line
162165 // so the six-button reports the higher button states
163166 digitalWrite (SELECT[player], LOW);
164167 delayMicroseconds (20 );
165168 digitalWrite (SELECT[player], HIGH);
166-
169+
167170 for (int i = 0 ; i < sizeof (inputMap) / sizeof (input); i++)
168171 {
169172 if (inputMap[i].player == player && digitalRead (inputMap[i].pin ) == LOW)
170173 {
171174 currentState[player] |= inputMap[i].pulse3Flag ;
172175 }
173176 }
174-
177+
175178 delayMicroseconds (1000 );
176179}
177180
178181void sendStates ()
179182{
180183 // Only report controller states if at least one has changed
181184 boolean hasChanged = false ;
182-
183- for (int i = 0 ; i < 2 ; i++)
185+
186+ for (int i = 0 ; i < PLAYERS ; i++)
184187 {
185188 if (currentState[i] != lastState[i])
186189 {
187190 hasChanged = true ;
188191 }
189192 }
190-
193+
191194 if (hasChanged)
192- {
193- for (int i = 0 ; i < 2 ; i++)
195+ {
196+ for (int i = 0 ; i < PLAYERS ; i++)
194197 {
195198 Serial.print ((currentState[i] & ON) == ON ? " +" : " -" );
196199 Serial.print ((currentState[i] & UP) == UP ? " U" : " 0" );
@@ -204,10 +207,10 @@ void sendStates()
204207 Serial.print ((currentState[i] & X) == X ? " X" : " 0" );
205208 Serial.print ((currentState[i] & Y) == Y ? " Y" : " 0" );
206209 Serial.print ((currentState[i] & Z) == Z ? " Z" : " 0" );
210+ Serial.print ((currentState[i] & MODE) == MODE ? " M" : " 0" );
207211
208212 Serial.print ((i == 0 ) ? " ," : " \n " );
209213 lastState[i] = currentState[i];
210214 }
211215 }
212216}
213-
0 commit comments