mirror of
https://github.com/MickGyver/DaemonBite-Retro-Controllers-USB
synced 2024-11-14 05:15:24 -05:00
Two controller adapter added.
This commit is contained in:
parent
b414a311bd
commit
58ffc0c8b1
145
SegaTwoControllersUSB/Gamepad.cpp
Normal file
145
SegaTwoControllersUSB/Gamepad.cpp
Normal file
@ -0,0 +1,145 @@
|
||||
/* Gamepad.cpp
|
||||
*
|
||||
* Based on the advanced HID library for Arduino:
|
||||
* https://github.com/NicoHood/HID
|
||||
* Copyright (c) 2014-2015 NicoHood
|
||||
*
|
||||
* Copyright (c) 2020 Mikael Norrgård <http://daemonbite.com>
|
||||
*
|
||||
* GNU GENERAL PUBLIC LICENSE
|
||||
* Version 3, 29 June 2007
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Gamepad.h"
|
||||
|
||||
static const uint8_t _hidReportDescriptor[] PROGMEM = {
|
||||
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
|
||||
0x09, 0x04, // USAGE (Joystick) (Maybe change to gamepad? I don't think so but...)
|
||||
0xa1, 0x01, // COLLECTION (Application)
|
||||
0xa1, 0x00, // COLLECTION (Physical)
|
||||
|
||||
0x05, 0x09, // USAGE_PAGE (Button)
|
||||
0x19, 0x01, // USAGE_MINIMUM (Button 1)
|
||||
0x29, 0x08, // USAGE_MAXIMUM (Button 8)
|
||||
0x15, 0x00, // LOGICAL_MINIMUM (0)
|
||||
0x25, 0x01, // LOGICAL_MAXIMUM (1)
|
||||
0x95, 0x08, // REPORT_COUNT (8)
|
||||
0x75, 0x01, // REPORT_SIZE (1)
|
||||
0x81, 0x02, // INPUT (Data,Var,Abs)
|
||||
|
||||
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
|
||||
0x09, 0x01, // USAGE (pointer)
|
||||
0xa1, 0x00, // COLLECTION (Physical)
|
||||
0x09, 0x30, // USAGE (X)
|
||||
0x09, 0x31, // USAGE (Y)
|
||||
0x15, 0xff, // LOGICAL_MINIMUM (-1)
|
||||
0x25, 0x01, // LOGICAL_MAXIMUM (1)
|
||||
0x95, 0x02, // REPORT_COUNT (2)
|
||||
0x75, 0x08, // REPORT_SIZE (8)
|
||||
0x81, 0x02, // INPUT (Data,Var,Abs)
|
||||
0xc0, // END_COLLECTION
|
||||
|
||||
0xc0, // END_COLLECTION
|
||||
0xc0, // END_COLLECTION
|
||||
};
|
||||
|
||||
Gamepad_::Gamepad_(void) : PluggableUSBModule(1, 1, epType), protocol(HID_REPORT_PROTOCOL), idle(1)
|
||||
{
|
||||
epType[0] = EP_TYPE_INTERRUPT_IN;
|
||||
PluggableUSB().plug(this);
|
||||
}
|
||||
|
||||
int Gamepad_::getInterface(uint8_t* interfaceCount)
|
||||
{
|
||||
*interfaceCount += 1; // uses 1
|
||||
HIDDescriptor hidInterface = {
|
||||
D_INTERFACE(pluggedInterface, 1, USB_DEVICE_CLASS_HUMAN_INTERFACE, HID_SUBCLASS_NONE, HID_PROTOCOL_NONE),
|
||||
D_HIDREPORT(sizeof(_hidReportDescriptor)),
|
||||
D_ENDPOINT(USB_ENDPOINT_IN(pluggedEndpoint), USB_ENDPOINT_TYPE_INTERRUPT, USB_EP_SIZE, 0x01)
|
||||
};
|
||||
return USB_SendControl(0, &hidInterface, sizeof(hidInterface));
|
||||
}
|
||||
|
||||
int Gamepad_::getDescriptor(USBSetup& setup)
|
||||
{
|
||||
// Check if this is a HID Class Descriptor request
|
||||
if (setup.bmRequestType != REQUEST_DEVICETOHOST_STANDARD_INTERFACE) { return 0; }
|
||||
if (setup.wValueH != HID_REPORT_DESCRIPTOR_TYPE) { return 0; }
|
||||
|
||||
// In a HID Class Descriptor wIndex cointains the interface number
|
||||
if (setup.wIndex != pluggedInterface) { return 0; }
|
||||
|
||||
// Reset the protocol on reenumeration. Normally the host should not assume the state of the protocol
|
||||
// due to the USB specs, but Windows and Linux just assumes its in report mode.
|
||||
protocol = HID_REPORT_PROTOCOL;
|
||||
|
||||
return USB_SendControl(TRANSFER_PGM, _hidReportDescriptor, sizeof(_hidReportDescriptor));
|
||||
}
|
||||
|
||||
bool Gamepad_::setup(USBSetup& setup)
|
||||
{
|
||||
if (pluggedInterface != setup.wIndex) {
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8_t request = setup.bRequest;
|
||||
uint8_t requestType = setup.bmRequestType;
|
||||
|
||||
if (requestType == REQUEST_DEVICETOHOST_CLASS_INTERFACE)
|
||||
{
|
||||
if (request == HID_GET_REPORT) {
|
||||
// TODO: HID_GetReport();
|
||||
return true;
|
||||
}
|
||||
if (request == HID_GET_PROTOCOL) {
|
||||
// TODO: Send8(protocol);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (requestType == REQUEST_HOSTTODEVICE_CLASS_INTERFACE)
|
||||
{
|
||||
if (request == HID_SET_PROTOCOL) {
|
||||
protocol = setup.wValueL;
|
||||
return true;
|
||||
}
|
||||
if (request == HID_SET_IDLE) {
|
||||
idle = setup.wValueL;
|
||||
return true;
|
||||
}
|
||||
if (request == HID_SET_REPORT)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void Gamepad_::reset()
|
||||
{
|
||||
_GamepadReport.X = 0;
|
||||
_GamepadReport.Y = 0;
|
||||
_GamepadReport.buttons = 0;
|
||||
this->send();
|
||||
}
|
||||
|
||||
void Gamepad_::send()
|
||||
{
|
||||
USB_Send(pluggedEndpoint | TRANSFER_RELEASE, &_GamepadReport, sizeof(GamepadReport));
|
||||
}
|
80
SegaTwoControllersUSB/Gamepad.h
Normal file
80
SegaTwoControllersUSB/Gamepad.h
Normal file
@ -0,0 +1,80 @@
|
||||
/* Gamepad.h
|
||||
*
|
||||
* Based on the advanced HID library for Arduino:
|
||||
* https://github.com/NicoHood/HID
|
||||
* Copyright (c) 2014-2015 NicoHood
|
||||
*
|
||||
* Copyright (c) 2020 Mikael Norrgård <http://daemonbite.com>
|
||||
*
|
||||
* GNU GENERAL PUBLIC LICENSE
|
||||
* Version 3, 29 June 2007
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "HID.h"
|
||||
|
||||
// NOTE: To make this work on the MiSTer (or possibly other Linux distros),
|
||||
// you need to edit USBDesc.h like follows. Change:
|
||||
// #define ISERIAL 3
|
||||
// to
|
||||
// #define ISERIAL 0
|
||||
|
||||
// The numbers after colon are bit fields, meaning how many bits the field uses.
|
||||
// Remove those if there are problems
|
||||
typedef struct {
|
||||
union
|
||||
{
|
||||
struct {
|
||||
bool b0: 1 ;
|
||||
bool b1: 1 ;
|
||||
bool b2: 1 ;
|
||||
bool b3: 1 ;
|
||||
bool b4: 1 ;
|
||||
bool b5: 1 ;
|
||||
bool b6: 1 ;
|
||||
bool b7: 1 ;
|
||||
};
|
||||
uint8_t buttons;
|
||||
};
|
||||
int8_t X ;
|
||||
int8_t Y ;
|
||||
|
||||
} GamepadReport;
|
||||
|
||||
|
||||
class Gamepad_ : public PluggableUSBModule
|
||||
{
|
||||
private:
|
||||
uint8_t reportId;
|
||||
|
||||
protected:
|
||||
int getInterface(uint8_t* interfaceCount);
|
||||
int getDescriptor(USBSetup& setup);
|
||||
bool setup(USBSetup& setup);
|
||||
|
||||
uint8_t epType[1];
|
||||
uint8_t protocol;
|
||||
uint8_t idle;
|
||||
|
||||
public:
|
||||
GamepadReport _GamepadReport;
|
||||
Gamepad_(void);
|
||||
void reset(void);
|
||||
void send();
|
||||
};
|
237
SegaTwoControllersUSB/SegaControllers32U4.cpp
Normal file
237
SegaTwoControllersUSB/SegaControllers32U4.cpp
Normal file
@ -0,0 +1,237 @@
|
||||
//
|
||||
// SegaControllers32U4.cpp
|
||||
//
|
||||
// Authors:
|
||||
// Jon Thysell <thysell@gmail.com>
|
||||
// Mikael Norrgård <mick@daemonbite.com>
|
||||
//
|
||||
// (Based on the code by Jon Thysell, but the interfacing is almost completely
|
||||
// rewritten by Mikael Norrgård)
|
||||
//
|
||||
// Copyright (c) 2017 Jon Thysell <http://jonthysell.com>
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "SegaControllers32U4.h"
|
||||
|
||||
SegaControllers32U4::SegaControllers32U4(void)
|
||||
{
|
||||
// Setup select pin as output high (16, PB2)
|
||||
DDRB |= B00000100; // output
|
||||
PORTB |= B00000100; // high
|
||||
// Setup select pin as output high (5, PC6)
|
||||
DDRC |= B01000000; // output
|
||||
PORTC |= B01000000; // high
|
||||
|
||||
// Setup input pins (A0,A1,A2,A3,14,15,16 or PF7,PF6,PF5,PF4,PB3,PB1,PB2)
|
||||
DDRF &= ~B11110000; // input
|
||||
PORTF |= B11110000; // high to enable internal pull-up
|
||||
DDRB &= ~B00001010; // input
|
||||
PORTB |= B00001010; // high to enable internal pull-up
|
||||
// Setup input pins (TXO,RXI,2,3,4,6 or PD3,PD2,PD1,PD0,PD4,PD7)
|
||||
DDRD &= ~B10011111; // input
|
||||
PORTD |= B10011111; // high to enable internal pull-up
|
||||
|
||||
_inputReg1 = 0;
|
||||
_inputReg2 = 0;
|
||||
_inputReg3 = 0;
|
||||
for(byte i=0; i<=1; i++)
|
||||
{
|
||||
_currentState[i] = 0;
|
||||
_connected[i] = 0;
|
||||
_sixButtonMode[i] = false;
|
||||
_ignoreCycles[i] = 0;
|
||||
_pinSelect[i] = true;
|
||||
}
|
||||
}
|
||||
|
||||
word SegaControllers32U4::getStateMD1()
|
||||
{
|
||||
// "Normal" Six button controller reading routine, done a bit differently in this project
|
||||
// Cycle TH out TR in TL in D3 in D2 in D1 in D0 in
|
||||
// 0 LO Start A 0 0 Down Up
|
||||
// 1 HI C B Right Left Down Up
|
||||
// 2 LO Start A 0 0 Down Up (Check connected and read Start and A in this cycle)
|
||||
// 3 HI C B Right Left Down Up (Read B, C and directions in this cycle)
|
||||
// 4 LO Start A 0 0 0 0 (Check for six button controller in this cycle)
|
||||
// 5 HI C B Mode X Y Z (Read X,Y,Z and Mode in this cycle)
|
||||
// 6 LO --- --- --- --- --- ---
|
||||
// 7 HI --- --- --- --- --- ---
|
||||
|
||||
// Set the select pin low/high
|
||||
_pinSelect[0] = !_pinSelect[0];
|
||||
(!_pinSelect[0]) ? PORTB &= ~B00000100 : PORTB |= B00000100; // Set LOW on even cycle, HIGH on uneven cycle
|
||||
|
||||
// Short delay to stabilise outputs in controller
|
||||
delayMicroseconds(SC_CYCLE_DELAY);
|
||||
|
||||
// Read input register(s)
|
||||
_inputReg1 = PINF;
|
||||
_inputReg2 = PINB;
|
||||
|
||||
if(_ignoreCycles[0] <= 0)
|
||||
{
|
||||
if(_pinSelect[0]) // Select pin is HIGH
|
||||
{
|
||||
if(_connected[0])
|
||||
{
|
||||
// Check if six button mode is active
|
||||
if(_sixButtonMode[0])
|
||||
{
|
||||
// Read input pins for X, Y, Z, Mode
|
||||
(bitRead(_inputReg1, DB9_PIN1_BIT1) == LOW) ? _currentState[0] |= SC_BTN_Z : _currentState[0] &= ~SC_BTN_Z;
|
||||
(bitRead(_inputReg1, DB9_PIN2_BIT1) == LOW) ? _currentState[0] |= SC_BTN_Y : _currentState[0] &= ~SC_BTN_Y;
|
||||
(bitRead(_inputReg1, DB9_PIN3_BIT1) == LOW) ? _currentState[0] |= SC_BTN_X : _currentState[0] &= ~SC_BTN_X;
|
||||
(bitRead(_inputReg1, DB9_PIN4_BIT1) == LOW) ? _currentState[0] |= SC_BTN_MODE : _currentState[0] &= ~SC_BTN_MODE;
|
||||
_sixButtonMode[0] = false;
|
||||
_ignoreCycles[0] = 2; // Ignore the two next cycles (cycles 6 and 7 in table above)
|
||||
}
|
||||
else
|
||||
{
|
||||
// Read input pins for Up, Down, Left, Right, B, C
|
||||
(bitRead(_inputReg1, DB9_PIN1_BIT1) == LOW) ? _currentState[0] |= SC_BTN_UP : _currentState[0] &= ~SC_BTN_UP;
|
||||
(bitRead(_inputReg1, DB9_PIN2_BIT1) == LOW) ? _currentState[0] |= SC_BTN_DOWN : _currentState[0] &= ~SC_BTN_DOWN;
|
||||
(bitRead(_inputReg1, DB9_PIN3_BIT1) == LOW) ? _currentState[0] |= SC_BTN_LEFT : _currentState[0] &= ~SC_BTN_LEFT;
|
||||
(bitRead(_inputReg1, DB9_PIN4_BIT1) == LOW) ? _currentState[0] |= SC_BTN_RIGHT : _currentState[0] &= ~SC_BTN_RIGHT;
|
||||
(bitRead(_inputReg2, DB9_PIN6_BIT1) == LOW) ? _currentState[0] |= SC_BTN_B : _currentState[0] &= ~SC_BTN_B;
|
||||
(bitRead(_inputReg2, DB9_PIN9_BIT1) == LOW) ? _currentState[0] |= SC_BTN_C : _currentState[0] &= ~SC_BTN_C;
|
||||
}
|
||||
}
|
||||
else // No Mega Drive controller is connected, use SMS/Atari mode
|
||||
{
|
||||
// Clear current state
|
||||
_currentState[0] = 0;
|
||||
|
||||
// Read input pins for Up, Down, Left, Right, Fire1, Fire2
|
||||
if (bitRead(_inputReg1, DB9_PIN1_BIT1) == LOW) { _currentState[0] |= SC_BTN_UP; }
|
||||
if (bitRead(_inputReg1, DB9_PIN2_BIT1) == LOW) { _currentState[0] |= SC_BTN_DOWN; }
|
||||
if (bitRead(_inputReg1, DB9_PIN3_BIT1) == LOW) { _currentState[0] |= SC_BTN_LEFT; }
|
||||
if (bitRead(_inputReg1, DB9_PIN4_BIT1) == LOW) { _currentState[0] |= SC_BTN_RIGHT; }
|
||||
if (bitRead(_inputReg2, DB9_PIN6_BIT1) == LOW) { _currentState[0] |= SC_BTN_A; }
|
||||
if (bitRead(_inputReg2, DB9_PIN9_BIT1) == LOW) { _currentState[0] |= SC_BTN_B; }
|
||||
}
|
||||
}
|
||||
else // Select pin is LOW
|
||||
{
|
||||
// Check if a controller is connected
|
||||
_connected[0] = (bitRead(_inputReg1, DB9_PIN3_BIT1) == LOW && bitRead(_inputReg1, DB9_PIN4_BIT1) == LOW);
|
||||
|
||||
// Check for six button mode
|
||||
_sixButtonMode[0] = (bitRead(_inputReg1, DB9_PIN1_BIT1) == LOW && bitRead(_inputReg1, DB9_PIN2_BIT1) == LOW);
|
||||
|
||||
// Read input pins for A and Start
|
||||
if(_connected[0])
|
||||
{
|
||||
if(!_sixButtonMode[0])
|
||||
{
|
||||
(bitRead(_inputReg2, DB9_PIN6_BIT1) == LOW) ? _currentState[0] |= SC_BTN_A : _currentState[0] &= ~SC_BTN_A;
|
||||
(bitRead(_inputReg2, DB9_PIN9_BIT1) == LOW) ? _currentState[0] |= SC_BTN_START : _currentState[0] &= ~SC_BTN_START;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_ignoreCycles[0]--;
|
||||
}
|
||||
|
||||
return _currentState[0];
|
||||
}
|
||||
|
||||
word SegaControllers32U4::getStateMD2()
|
||||
{
|
||||
// Set the select pin low/high
|
||||
_pinSelect[1] = !_pinSelect[1];
|
||||
(!_pinSelect[1]) ? PORTC &= ~B01000000 : PORTC |= B01000000; // Set LOW on even cycle, HIGH on uneven cycle
|
||||
|
||||
// Short delay to stabilise outputs in controller
|
||||
delayMicroseconds(SC_CYCLE_DELAY);
|
||||
|
||||
// Read input register(s)
|
||||
_inputReg3 = PIND;
|
||||
|
||||
if(_ignoreCycles[1] <= 0)
|
||||
{
|
||||
if(_pinSelect[1]) // Select pin is HIGH
|
||||
{
|
||||
if(_connected[1])
|
||||
{
|
||||
// Check if six button mode is active
|
||||
if(_sixButtonMode[1])
|
||||
{
|
||||
// Read input pins for X, Y, Z, Mode
|
||||
(bitRead(_inputReg3, DB9_PIN1_BIT2) == LOW) ? _currentState[1] |= SC_BTN_Z : _currentState[1] &= ~SC_BTN_Z;
|
||||
(bitRead(_inputReg3, DB9_PIN2_BIT2) == LOW) ? _currentState[1] |= SC_BTN_Y : _currentState[1] &= ~SC_BTN_Y;
|
||||
(bitRead(_inputReg3, DB9_PIN3_BIT2) == LOW) ? _currentState[1] |= SC_BTN_X : _currentState[1] &= ~SC_BTN_X;
|
||||
(bitRead(_inputReg3, DB9_PIN4_BIT2) == LOW) ? _currentState[1] |= SC_BTN_MODE : _currentState[1] &= ~SC_BTN_MODE;
|
||||
_sixButtonMode[1] = false;
|
||||
_ignoreCycles[1] = 2; // Ignore the two next cycles (cycles 6 and 7 in table above)
|
||||
}
|
||||
else
|
||||
{
|
||||
// Read input pins for Up, Down, Left, Right, B, C
|
||||
(bitRead(_inputReg3, DB9_PIN1_BIT2) == LOW) ? _currentState[1] |= SC_BTN_UP : _currentState[1] &= ~SC_BTN_UP;
|
||||
(bitRead(_inputReg3, DB9_PIN2_BIT2) == LOW) ? _currentState[1] |= SC_BTN_DOWN : _currentState[1] &= ~SC_BTN_DOWN;
|
||||
(bitRead(_inputReg3, DB9_PIN3_BIT2) == LOW) ? _currentState[1] |= SC_BTN_LEFT : _currentState[1] &= ~SC_BTN_LEFT;
|
||||
(bitRead(_inputReg3, DB9_PIN4_BIT2) == LOW) ? _currentState[1] |= SC_BTN_RIGHT : _currentState[1] &= ~SC_BTN_RIGHT;
|
||||
(bitRead(_inputReg3, DB9_PIN6_BIT2) == LOW) ? _currentState[1] |= SC_BTN_B : _currentState[1] &= ~SC_BTN_B;
|
||||
(bitRead(_inputReg3, DB9_PIN9_BIT2) == LOW) ? _currentState[1] |= SC_BTN_C : _currentState[1] &= ~SC_BTN_C;
|
||||
}
|
||||
}
|
||||
else // No Mega Drive controller is connected, use SMS/Atari mode
|
||||
{
|
||||
// Clear current state
|
||||
_currentState[1] = 0;
|
||||
|
||||
// Read input pins for Up, Down, Left, Right, Fire1, Fire2
|
||||
if (bitRead(_inputReg3, DB9_PIN1_BIT2) == LOW) { _currentState[1] |= SC_BTN_UP; }
|
||||
if (bitRead(_inputReg3, DB9_PIN2_BIT2) == LOW) { _currentState[1] |= SC_BTN_DOWN; }
|
||||
if (bitRead(_inputReg3, DB9_PIN3_BIT2) == LOW) { _currentState[1] |= SC_BTN_LEFT; }
|
||||
if (bitRead(_inputReg3, DB9_PIN4_BIT2) == LOW) { _currentState[1] |= SC_BTN_RIGHT; }
|
||||
if (bitRead(_inputReg3, DB9_PIN6_BIT2) == LOW) { _currentState[1] |= SC_BTN_A; }
|
||||
if (bitRead(_inputReg3, DB9_PIN9_BIT2) == LOW) { _currentState[1] |= SC_BTN_B; }
|
||||
}
|
||||
}
|
||||
else // Select pin is LOW
|
||||
{
|
||||
// Check if a controller is connected
|
||||
_connected[1] = (bitRead(_inputReg3, DB9_PIN3_BIT2) == LOW && bitRead(_inputReg3, DB9_PIN4_BIT2) == LOW);
|
||||
|
||||
// Check for six button mode
|
||||
_sixButtonMode[1] = (bitRead(_inputReg3, DB9_PIN1_BIT2) == LOW && bitRead(_inputReg3, DB9_PIN2_BIT2) == LOW);
|
||||
|
||||
// Read input pins for A and Start
|
||||
if(_connected[1])
|
||||
{
|
||||
if(!_sixButtonMode[1])
|
||||
{
|
||||
(bitRead(_inputReg3, DB9_PIN6_BIT2) == LOW) ? _currentState[1] |= SC_BTN_A : _currentState[1] &= ~SC_BTN_A;
|
||||
(bitRead(_inputReg3, DB9_PIN9_BIT2) == LOW) ? _currentState[1] |= SC_BTN_START : _currentState[1] &= ~SC_BTN_START;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_ignoreCycles[1]--;
|
||||
}
|
||||
|
||||
return _currentState[1];
|
||||
}
|
88
SegaTwoControllersUSB/SegaControllers32U4.h
Normal file
88
SegaTwoControllersUSB/SegaControllers32U4.h
Normal file
@ -0,0 +1,88 @@
|
||||
//
|
||||
// SegaControllers32U4.h
|
||||
//
|
||||
// Authors:
|
||||
// Jon Thysell <thysell@gmail.com>
|
||||
// Mikael Norrgård <mick@daemonbite.com>
|
||||
//
|
||||
// Copyright (c) 2017 Jon Thysell <http://jonthysell.com>
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
#ifndef SegaController32U4_h
|
||||
#define SegaController32U4_h
|
||||
|
||||
enum
|
||||
{
|
||||
SC_CTL_ON = 1, // The controller is connected (not used)
|
||||
SC_BTN_UP = 2,
|
||||
SC_BTN_DOWN = 4,
|
||||
SC_BTN_LEFT = 8,
|
||||
SC_BTN_RIGHT = 16,
|
||||
SC_BTN_A = 32,
|
||||
SC_BTN_B = 64,
|
||||
SC_BTN_C = 128,
|
||||
SC_BTN_X = 256,
|
||||
SC_BTN_Y = 512,
|
||||
SC_BTN_Z = 1024,
|
||||
SC_BTN_START = 2048,
|
||||
SC_BTN_MODE = 4096,
|
||||
SC_BTN_1 = 64, // Master System compatibility
|
||||
SC_BTN_2 = 128, // Master System compatibility
|
||||
DB9_PIN1_BIT1 = 7,
|
||||
DB9_PIN2_BIT1 = 6,
|
||||
DB9_PIN3_BIT1 = 5,
|
||||
DB9_PIN4_BIT1 = 4,
|
||||
DB9_PIN6_BIT1 = 3,
|
||||
DB9_PIN9_BIT1 = 1,
|
||||
DB9_PIN1_BIT2 = 3,
|
||||
DB9_PIN2_BIT2 = 2,
|
||||
DB9_PIN3_BIT2 = 1,
|
||||
DB9_PIN4_BIT2 = 0,
|
||||
DB9_PIN6_BIT2 = 4,
|
||||
DB9_PIN9_BIT2 = 7
|
||||
};
|
||||
|
||||
const byte SC_INPUT_PINS = 6;
|
||||
|
||||
const byte SC_CYCLE_DELAY = 10; // Delay (µs) between setting the select pin and reading the button pins
|
||||
|
||||
class SegaControllers32U4 {
|
||||
public:
|
||||
SegaControllers32U4(void);
|
||||
|
||||
word getStateMD1();
|
||||
word getStateMD2();
|
||||
|
||||
private:
|
||||
word _currentState[2];
|
||||
|
||||
boolean _pinSelect[2];
|
||||
|
||||
byte _ignoreCycles[2];
|
||||
|
||||
boolean _connected[2];
|
||||
boolean _sixButtonMode[2];
|
||||
|
||||
byte _inputReg1;
|
||||
byte _inputReg2;
|
||||
byte _inputReg3;
|
||||
};
|
||||
|
||||
#endif
|
93
SegaTwoControllersUSB/SegaTwoControllersUSB.ino
Normal file
93
SegaTwoControllersUSB/SegaTwoControllersUSB.ino
Normal file
@ -0,0 +1,93 @@
|
||||
/* DaemonBite Sega USB Adapter
|
||||
* Author: Mikael Norrgård <mick@daemonbite.com>
|
||||
*
|
||||
* Copyright (c) 2020 Mikael Norrgård <http://daemonbite.com>
|
||||
*
|
||||
* GNU GENERAL PUBLIC LICENSE
|
||||
* Version 3, 29 June 2007
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
// NOTE: To make this work on the MiSTer (or possibly other Linux distros),
|
||||
// you need to edit USBDesc.h like follows. Change:
|
||||
// #define ISERIAL 3
|
||||
// to
|
||||
// #define ISERIAL 0
|
||||
|
||||
#include "SegaControllers32U4.h"
|
||||
#include "Gamepad.h"
|
||||
|
||||
// Controller DB9 pins (looking face-on to the end of the plug):
|
||||
//
|
||||
// 5 4 3 2 1
|
||||
// 9 8 7 6
|
||||
//
|
||||
// Connect pin 5 to +5V and pin 8 to GND
|
||||
// Connect the remaining pins to digital I/O pins (see below)
|
||||
// DB9 Arduino Pro Micro
|
||||
// --------------------------------------
|
||||
// 1 A0 PF7
|
||||
// 2 A1 PF6
|
||||
// 3 A2 PF5
|
||||
// 4 A3 PF4
|
||||
// 6 14 PB3
|
||||
// 7 16 PB2 (6 PD7)
|
||||
// 9 15 PB1
|
||||
|
||||
// 1 TXO PD3
|
||||
// 2 RXI PD2
|
||||
// 3 2 PD1
|
||||
// 4 3 PD0
|
||||
// 6 4 PD4
|
||||
// 7 5 PC6
|
||||
// 9 6 PD7
|
||||
|
||||
SegaControllers32U4 controllers;
|
||||
|
||||
// Set up USB HID gamepads
|
||||
Gamepad_ Gamepad[2];
|
||||
bool usbUpdate[2] = {false,false}; // Should gamepad data be sent to USB?
|
||||
|
||||
// Controller states
|
||||
word currentState[2] = {0,0};
|
||||
word lastState[2] = {1,1};
|
||||
|
||||
void setup()
|
||||
{
|
||||
Gamepad[0].reset();
|
||||
Gamepad[1].reset();
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
currentState[0] = controllers.getStateMD1();
|
||||
sendState(0);
|
||||
currentState[1] = controllers.getStateMD2();
|
||||
sendState(1);
|
||||
}
|
||||
|
||||
void sendState(byte gp)
|
||||
{
|
||||
// Only report controller state if it has changed
|
||||
if (currentState[gp] != lastState[gp])
|
||||
{
|
||||
Gamepad[gp]._GamepadReport.buttons = currentState[gp] >> 5;
|
||||
Gamepad[gp]._GamepadReport.Y = ((currentState[gp] & B00000100) >> 2) - ((currentState[gp] & B00000010) >> 1);
|
||||
Gamepad[gp]._GamepadReport.X = ((currentState[gp] & B00010000) >> 4) - ((currentState[gp] & B00001000) >> 3);
|
||||
Gamepad[gp].send();
|
||||
lastState[gp] = currentState[gp];
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user