
15 changed files with 546 additions and 134 deletions
@ -0,0 +1,156 @@
@@ -0,0 +1,156 @@
|
||||
/* 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/>.
|
||||
*
|
||||
*/ |
||||
#include "Gamepad.h" |
||||
|
||||
static const uint8_t _hidReportDescriptor[] PROGMEM = { |
||||
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
|
||||
0x09, 0x04, // USAGE (Joystick)
|
||||
0xa1, 0x01, // COLLECTION (Application)
|
||||
0xa1, 0x00, // COLLECTION (Physical)
|
||||
|
||||
0x05, 0x09, // USAGE_PAGE (Button)
|
||||
0x19, 0x01, // USAGE_MINIMUM (Button 1)
|
||||
0x29, 0x0c, // USAGE_MAXIMUM (Button 12)
|
||||
0x15, 0x00, // LOGICAL_MINIMUM (0)
|
||||
0x25, 0x01, // LOGICAL_MAXIMUM (1)
|
||||
0x95, 0x0c, // REPORT_COUNT (12)
|
||||
0x75, 0x01, // REPORT_SIZE (1)
|
||||
0x81, 0x02, // INPUT (Data,Var,Abs)
|
||||
|
||||
0x95, 0x01, // REPORT_COUNT (1) ; pad out the bits into a number divisible by 8
|
||||
0x75, 0x04, // REPORT_SIZE (4)
|
||||
0x81, 0x03, // INPUT (Const,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)); |
||||
} |
||||
|
||||
uint8_t Gamepad_::getShortName(char *name) |
||||
{ |
||||
if(!next)
|
||||
{ |
||||
strcpy(name, gp_serial); |
||||
return strlen(name); |
||||
} |
||||
return 0; |
||||
} |
@ -0,0 +1,60 @@
@@ -0,0 +1,60 @@
|
||||
/* 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 "HID.h" |
||||
|
||||
extern const char* gp_serial; |
||||
|
||||
typedef struct { |
||||
uint16_t buttons : 12; |
||||
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); |
||||
uint8_t getShortName(char *name); |
||||
bool setup(USBSetup& setup); |
||||
|
||||
uint8_t epType[1]; |
||||
uint8_t protocol; |
||||
uint8_t idle; |
||||
|
||||
public: |
||||
GamepadReport _GamepadReport; |
||||
Gamepad_(void); |
||||
void reset(void); |
||||
void send(); |
||||
}; |
@ -0,0 +1,169 @@
@@ -0,0 +1,169 @@
|
||||
/* NeoGeo Controller to USB
|
||||
* 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/>.
|
||||
*
|
||||
*/ |
||||
|
||||
#include "Gamepad.h" |
||||
|
||||
#define DEBOUNCE 0 // 1=Diddly-squat-Delay-Debouncing™ activated, 0=Debounce deactivated
|
||||
#define DEBOUNCE_TIME 10 // Debounce time in milliseconds
|
||||
//#define DEBUG // Enables debugging (sends debug data to usb serial)
|
||||
|
||||
const char *gp_serial = "NeoGeo to USB"; |
||||
|
||||
Gamepad_ Gamepad; // Set up USB HID gamepad
|
||||
bool usbUpdate = false; // Should gamepad data be sent to USB?
|
||||
bool debounce = DEBOUNCE; // Debounce?
|
||||
uint8_t pin; // Used in for loops
|
||||
uint32_t millisNow = 0; // Used for Diddly-squat-Delay-Debouncing™
|
||||
|
||||
uint8_t axesDirect = 0x0f; |
||||
uint8_t axes = 0x0f; |
||||
uint8_t axesPrev = 0x0f; |
||||
uint8_t axesBits[4] = {0x10,0x20,0x40,0x80}; |
||||
uint32_t axesMillis[4]; |
||||
|
||||
uint16_t buttonsDirect = 0; |
||||
uint16_t buttons = 0; |
||||
uint16_t buttonsPrev = 0; |
||||
uint16_t buttonsBits[12] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x100,0x200,0x400,0x800}; |
||||
uint32_t buttonsMillis[12]; |
||||
|
||||
#ifdef DEBUG |
||||
char buf[16]; |
||||
uint32_t millisSent = 0; |
||||
#endif |
||||
|
||||
void setup()
|
||||
{ |
||||
// Axes
|
||||
DDRF &= ~B11110000; // Set A0-A3 as inputs
|
||||
PORTF |= B11110000; // Enable internal pull-up resistors
|
||||
|
||||
// Buttons
|
||||
DDRD &= ~B10011111; // Set PD0-PD4 and PD7 as inputs
|
||||
PORTD |= B10011111; // Enable internal pull-up resistors
|
||||
DDRB &= ~B01111110; // Set PB1-PB6 as inputs
|
||||
PORTB |= B01111110; // Enable internal pull-up resistors
|
||||
|
||||
// Debounce selector switch (currently disabled)
|
||||
DDRE &= ~B01000000; // Pin 7 as input
|
||||
PORTE |= B01000000; // Enable internal pull-up resistor
|
||||
|
||||
// Initialize debouncing timestamps
|
||||
for(pin=0; pin<4; pin++) |
||||
axesMillis[pin]=0; |
||||
for(pin=0; pin<12; pin++)
|
||||
buttonsMillis[pin]=0; |
||||
|
||||
#ifdef DEBUG |
||||
Serial.begin(115200); |
||||
#endif |
||||
} |
||||
|
||||
void loop()
|
||||
{ |
||||
// Get current time, the millis() function should take about 2µs to complete
|
||||
millisNow = millis(); |
||||
|
||||
for(uint8_t i=0; i<10; i++) // One iteration (when debounce is enabled) takes approximately 35µs to complete, so we don't need to check the time between every iteration
|
||||
{ |
||||
// Read axis and button inputs (bitwise NOT results in a 1 when button/axis pressed)
|
||||
axesDirect = ~(PINF & B11110000); |
||||
buttonsDirect = ~((PIND & B00011111) | ((PIND & B10000000) << 4) | ((PINB & B01111110) << 4)); |
||||
|
||||
if(debounce) |
||||
{ |
||||
// Debounce axes
|
||||
for(pin=0; pin<4; pin++) |
||||
{ |
||||
// Check if the current pin state is different to the stored state and that enough time has passed since last change
|
||||
if((axesDirect & axesBits[pin]) != (axes & axesBits[pin]) && (millisNow - axesMillis[pin]) > DEBOUNCE_TIME) |
||||
{ |
||||
// Toggle the pin, we can safely do this because we know the current state is different to the stored state
|
||||
axes ^= axesBits[pin]; |
||||
// Update the timestamp for the pin
|
||||
axesMillis[pin] = millisNow; |
||||
} |
||||
} |
||||
|
||||
// Debounce buttons
|
||||
for(pin=0; pin<12; pin++) |
||||
{ |
||||
// Check if the current pin state is different to the stored state and that enough time has passed since last change
|
||||
if((buttonsDirect & buttonsBits[pin]) != (buttons & buttonsBits[pin]) && (millisNow - buttonsMillis[pin]) > DEBOUNCE_TIME) |
||||
{ |
||||
// Toggle the pin, we can safely do this because we know the current state is different to the stored state
|
||||
buttons ^= buttonsBits[pin]; |
||||
// Update the timestamp for the pin
|
||||
buttonsMillis[pin] = millisNow; |
||||
} |
||||
} |
||||
} |
||||
else |
||||
{ |
||||
axes = axesDirect; |
||||
buttons = buttonsDirect; |
||||
} |
||||
|
||||
// Has axis inputs changed?
|
||||
if(axes != axesPrev) |
||||
{ |
||||
// UP + DOWN = UP, SOCD (Simultaneous Opposite Cardinal Directions) Cleaner
|
||||
if(axes & B10000000) |
||||
Gamepad._GamepadReport.Y = -1; |
||||
else if(axes & B01000000) |
||||
Gamepad._GamepadReport.Y = 1; |
||||
else |
||||
Gamepad._GamepadReport.Y = 0; |
||||
// UP + DOWN = NEUTRAL
|
||||
//Gamepad._GamepadReport.Y = ((axes & B01000000)>>6) - ((axes & B10000000)>>7);
|
||||
// LEFT + RIGHT = NEUTRAL
|
||||
Gamepad._GamepadReport.X = ((axes & B00010000)>>4) - ((axes & B00100000)>>5); |
||||
axesPrev = axes; |
||||
usbUpdate = true; |
||||
} |
||||
|
||||
// Has button inputs changed?
|
||||
if(buttons != buttonsPrev) |
||||
{ |
||||
Gamepad._GamepadReport.buttons = buttons; |
||||
buttonsPrev = buttons; |
||||
usbUpdate = true; |
||||
} |
||||
|
||||
// Should gamepad data be sent to USB?
|
||||
if(usbUpdate) |
||||
{ |
||||
Gamepad.send(); |
||||
usbUpdate = false; |
||||
|
||||
#ifdef DEBUG |
||||
sprintf(buf, "%06lu: %d%d%d%d", millisNow-millisSent, ((axes & 0x10)>>4), ((axes & 0x20)>>5), ((axes & 0x40)>>6), ((axes & 0x80)>>7) ); |
||||
Serial.print(buf); |
||||
sprintf(buf, " %d%d%d%d", (buttons & 0x01), ((buttons & 0x02)>>1), ((buttons & 0x04)>>2), ((buttons & 0x08)>>3) ); |
||||
Serial.println(buf); |
||||
millisSent = millisNow; |
||||
#endif |
||||
} |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue