Skip to content

Commit

Permalink
Started work on USB gamepads for RPI frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
andreas-jonsson committed Nov 19, 2024
1 parent 15f3279 commit f3bda6e
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 12 deletions.
2 changes: 1 addition & 1 deletion front/rpi/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ EXTRACLEAN = $(VXTHOME)/*.o $(VXTHOME)/*.d
OBJS_LIBC = $(LIBCHOME)/scanf.o
EXTRACLEAN += $(LIBCHOME)/*.o $(LIBCHOME)/*.d

OBJS = main.o kernel.o ethernet.o cga.o vga.o mouse.o ems.o adlib.o $(OBJS_VXT) $(OBJS_LIBC)
OBJS = main.o kernel.o ethernet.o cga.o vga.o mouse.o joystick.o ems.o adlib.o $(OBJS_VXT) $(OBJS_LIBC)

LIBS = $(CIRCLEHOME)/lib/usb/libusb.a \
$(CIRCLEHOME)/lib/sound/libsound.a \
Expand Down
5 changes: 3 additions & 2 deletions front/rpi/joystick.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
// 3. This notice may not be removed or altered from any source
// distribution.

#include "../../lib/scanf/scanf.h"
#include "../../modules/joystick/joystick.c"

bool joystick_push_event(struct vxt_peripheral *p, const struct frontend_joystick_event *ev) {
return push_event(p, ev);
}

struct vxt_peripheral *joystick_create(vxt_allocator *alloc, void *frontend, const char *args) {
return create(alloc, frontend, args);
struct vxt_peripheral *joystick_create(vxt_allocator *alloc, const char *args) {
return create(alloc, NULL, args);
}
55 changes: 48 additions & 7 deletions front/rpi/kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ FIL log_file = {0};
extern "C" {
// From joystick.c
bool joystick_push_event(struct vxt_peripheral *p, const struct frontend_joystick_event *ev);
struct vxt_peripheral *joystick_create(vxt_allocator *alloc, void *frontend, const char *args);
struct vxt_peripheral *joystick_create(vxt_allocator *alloc, const char *args);

// From mouse.c
struct vxt_peripheral *mouse_create(vxt_allocator *alloc, void *frontend, const char *args);
struct vxt_peripheral *mouse_create(vxt_allocator *alloc, const char *args);
bool mouse_push_event(struct vxt_peripheral *p, const struct frontend_mouse_event *ev);

// From ethernet.cpp
Expand Down Expand Up @@ -268,6 +268,9 @@ CKernel::CKernel(void)
if (!pFrameBuffer->Initialize())
delete pFrameBuffer;

for (int i = 0; i < MAX_GAMEPADS; i++)
m_pGamePad[i] = 0;

memset(&mouse_state, 0, sizeof(mouse_state));
memset(key_states, 0, sizeof(key_states));
memset(key_states_current, 0, sizeof(key_states_current));
Expand Down Expand Up @@ -319,7 +322,7 @@ TShutdownMode CKernel::Run(void) {
struct vxt_peripheral *ppi = 0;
struct vxt_peripheral *mouse = 0;
struct vxt_peripheral *disk = NULL;
//struct vxt_peripheral *joystick = NULL;
struct vxt_peripheral *joystick = NULL;

#ifdef LOGSERIAL
vxt_set_logger(&dev_logger);
Expand Down Expand Up @@ -383,16 +386,16 @@ TShutdownMode CKernel::Run(void) {
ems_create(&allocator, "lotech_ems"),
(ppi = vxtu_ppi_create(&allocator)),
(disk = vxtu_disk_create2(&allocator, &intrf)),
(mouse = mouse_create(&allocator, NULL, "0x3F8")),
//(joystick = joystick_create(&allocator, NULL, "0x201")),
(mouse = mouse_create(&allocator, "0x3F8")),
//(joystick = joystick_create(&allocator, "0x201")),
adlib_create(&allocator, &audio_adapter),
use_cga ? cga_card_create(&allocator, &video_adapter) : vga_card_create(&allocator, &video_adapter),
use_cga ? NULL : load_bios("vgabios.bin", 0xC0000),
NULL
};

cpu_frequency = m_Options.GetAppOptionDecimal("CPUFREQ", cpu_frequency);
VXT_LOG("CPU frequency: %02fMHz", (double)cpu_frequency / 1000000.0);
VXT_LOG("CPU frequency: %.2fMHz", (double)cpu_frequency / 1000000.0);

vxt_system *s = vxt_system_create(&allocator, (int)cpu_frequency, devices);
if (!s) {
Expand Down Expand Up @@ -456,6 +459,27 @@ TShutdownMode CKernel::Run(void) {
VXT_LOG("Mouse connected!");
}
}

if (joystick) {
for (unsigned u = 1; u <= MAX_GAMEPADS; u++) {
if (m_pGamePad[u - 1])
continue;

CUSBGamePadDevice *gp = (m_pGamePad[u - 1] = (CUSBGamePadDevice*)m_DeviceNameService.GetDevice("upad", u, FALSE));
if (!gp)
continue;

const TGamePadState *pState = gp->GetInitialState();
assert(pState != 0);

VXT_LOG("Gamepad %u: %d Button(s) %d Hat(s)", u, pState->nbuttons, pState->nhats);
for (int i = 0; i < pState->naxes; i++)
VXT_LOG("Gamepad %u: Axis %d: Minimum %d Maximum %d", u, i + 1, pState->axes[i].minimum, pState->axes[i].maximum);

gp->RegisterRemovedHandler(GamePadRemovedHandler, this);
gp->RegisterStatusHandler(GamePadStatusHandler);
}
}
}

if (m_pKeyboard)
Expand Down Expand Up @@ -597,7 +621,24 @@ void CKernel::MouseStatusHandlerRaw(unsigned nButtons, int nDisplacementX, int n
}

void CKernel::MouseRemovedHandler(CDevice *pDevice, void *pContext) {
assert(s_pThis != 0);
assert(s_pThis);
VXT_LOG("Mouse removed!");
s_pThis->m_pMouse = 0;
}

void CKernel::GamePadStatusHandler(unsigned nDeviceIndex, const TGamePadState *pState) {
// TODO
}

void CKernel::GamePadRemovedHandler(CDevice *pDevice, void *pContext) {
CKernel *pThis = (CKernel*)pContext;
assert(pThis);

for (int i = 0; i < MAX_GAMEPADS; i++) {
if (pThis->m_pGamePad[i] == (CUSBGamePadDevice*)pDevice) {
VXT_LOG("Gamepad %d removed!", i + 1);
pThis->m_pGamePad[i] = 0;
return;
}
}
}
7 changes: 7 additions & 0 deletions front/rpi/kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <circle/sound/usbsoundbasedevice.h>
#include <circle/usb/usbhcidevice.h>
#include <circle/usb/usbkeyboard.h>
#include <circle/usb/usbgamepad.h>
#include <circle/input/mouse.h>
#include <circle/bcm54213.h>
#include <circle/macb.h>
Expand All @@ -53,6 +54,8 @@
#undef NULL
#define NULL nullptr

#define MAX_GAMEPADS 2

enum TShutdownMode {
ShutdownNone,
ShutdownHalt,
Expand All @@ -77,6 +80,9 @@ class CKernel {
static void MouseStatusHandlerRaw(unsigned nButtons, int nDisplacementX, int nDisplacementY, int nWheelMove);
static void MouseRemovedHandler(CDevice *pDevice, void *pContext);

static void GamePadStatusHandler(unsigned nDeviceIndex, const TGamePadState *pState);
static void GamePadRemovedHandler(CDevice *pDevice, void *pContext);

// Do not change this order!
CKernelOptions m_Options;
CDeviceNameService m_DeviceNameService;
Expand All @@ -96,6 +102,7 @@ class CKernel {

CMouseDevice *volatile m_pMouse;
CUSBKeyboardDevice *volatile m_pKeyboard;
CUSBGamePadDevice * volatile m_pGamePad[MAX_GAMEPADS];
volatile TShutdownMode m_ShutdownMode;

CSoundBaseDevice *m_pSound;
Expand Down
4 changes: 2 additions & 2 deletions front/rpi/mouse.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
#include "../../lib/scanf/scanf.h"
#include "../../modules/mouse/mouse.c"

struct vxt_peripheral *mouse_create(vxt_allocator *alloc, void *frontend, const char *args) {
return create(alloc, frontend, args);
struct vxt_peripheral *mouse_create(vxt_allocator *alloc, const char *args) {
return create(alloc, NULL, args);
}

bool mouse_push_event(struct vxt_peripheral *p, const struct frontend_mouse_event *ev) {
Expand Down

0 comments on commit f3bda6e

Please sign in to comment.