|
| 1 | +// app.c - NEOGEO2USB App Entry Point |
| 2 | +// NEOGEO native controller input to USB HID gamepad output adapter |
| 3 | +// |
| 4 | +// This app polls native NEOGEO controllers and routes input to USB device output. |
| 5 | + |
| 6 | +#include "app.h" |
| 7 | +#include "profiles.h" |
| 8 | +#include "core/router/router.h" |
| 9 | +#include "core/services/players/manager.h" |
| 10 | +#include "core/services/profiles/profile.h" |
| 11 | +#include "core/input_interface.h" |
| 12 | +#include "core/output_interface.h" |
| 13 | +#include "core/services/button/button.h" |
| 14 | +#include "usb/usbd/usbd.h" |
| 15 | +#include "native/host/arcade/arcade_host.h" |
| 16 | +#include <stdio.h> |
| 17 | + |
| 18 | +static arcade_config_t arcade_config[ARCADE_MAX_PORTS] = { |
| 19 | + [0] = { |
| 20 | + .pin_du = NEOGEO_PIN_DU, |
| 21 | + .pin_dd = NEOGEO_PIN_DD, |
| 22 | + .pin_dl = NEOGEO_PIN_DL, |
| 23 | + .pin_dr = NEOGEO_PIN_DR, |
| 24 | + |
| 25 | + // Action Buttons |
| 26 | + .pin_p1 = NEOGEO_PIN_B1, |
| 27 | + .pin_p2 = NEOGEO_PIN_B2, |
| 28 | + .pin_p3 = NEOGEO_PIN_B3, |
| 29 | + .pin_p4 = GPIO_DISABLED, |
| 30 | + .pin_k1 = NEOGEO_PIN_B4, |
| 31 | + .pin_k2 = NEOGEO_PIN_B5, |
| 32 | + .pin_k3 = NEOGEO_PIN_B6, |
| 33 | + .pin_k4 = GPIO_DISABLED, |
| 34 | + |
| 35 | + // Meta Buttons |
| 36 | + .pin_s1 = NEOGEO_PIN_S1, |
| 37 | + .pin_s2 = NEOGEO_PIN_S2, |
| 38 | + .pin_a1 = GPIO_DISABLED, |
| 39 | + .pin_a2 = GPIO_DISABLED, |
| 40 | + |
| 41 | + // Extra Buttons |
| 42 | + .pin_l3 = GPIO_DISABLED, |
| 43 | + .pin_r3 = GPIO_DISABLED, |
| 44 | + .pin_l4 = GPIO_DISABLED, |
| 45 | + .pin_r4 = GPIO_DISABLED, |
| 46 | + } |
| 47 | +}; |
| 48 | + |
| 49 | +// ============================================================================ |
| 50 | +// BUTTON EVENT HANDLER |
| 51 | +// ============================================================================ |
| 52 | + |
| 53 | +static void on_button_event(button_event_t event) |
| 54 | +{ |
| 55 | + switch (event) { |
| 56 | + case BUTTON_EVENT_CLICK: |
| 57 | + printf("[app:usb2neogeo] Button click - current mode: %s\n", |
| 58 | + usbd_get_mode_name(usbd_get_mode())); |
| 59 | + break; |
| 60 | + |
| 61 | + case BUTTON_EVENT_DOUBLE_CLICK: { |
| 62 | + // Double-click to cycle USB output mode |
| 63 | + printf("[app:usb2neogeo] Double-click - switching USB output mode...\n"); |
| 64 | + tud_task(); |
| 65 | + sleep_ms(50); |
| 66 | + tud_task(); |
| 67 | + |
| 68 | + usb_output_mode_t next = usbd_get_next_mode(); |
| 69 | + printf("[app:usb2neogeo] Switching to %s\n", usbd_get_mode_name(next)); |
| 70 | + usbd_set_mode(next); |
| 71 | + break; |
| 72 | + } |
| 73 | + |
| 74 | + case BUTTON_EVENT_TRIPLE_CLICK: |
| 75 | + // Triple-click to reset to default HID mode |
| 76 | + printf("[app:usb2neogeo] Triple-click - resetting to HID mode...\n"); |
| 77 | + if (!usbd_reset_to_hid()) { |
| 78 | + printf("[app:usb2neogeo] Already in HID mode\n"); |
| 79 | + } |
| 80 | + break; |
| 81 | + |
| 82 | + default: |
| 83 | + break; |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +// ============================================================================ |
| 88 | +// APP INPUT INTERFACES |
| 89 | +// ============================================================================ |
| 90 | + |
| 91 | +static const InputInterface* input_interfaces[] = { |
| 92 | + &arcade_input_interface, |
| 93 | +}; |
| 94 | + |
| 95 | +const InputInterface** app_get_input_interfaces(uint8_t* count) |
| 96 | +{ |
| 97 | + *count = sizeof(input_interfaces) / sizeof(input_interfaces[0]); |
| 98 | + return input_interfaces; |
| 99 | +} |
| 100 | + |
| 101 | +// ============================================================================ |
| 102 | +// APP OUTPUT INTERFACES |
| 103 | +// ============================================================================ |
| 104 | + |
| 105 | +static const OutputInterface* output_interfaces[] = { |
| 106 | + &usbd_output_interface, |
| 107 | +}; |
| 108 | + |
| 109 | +const OutputInterface** app_get_output_interfaces(uint8_t* count) |
| 110 | +{ |
| 111 | + *count = sizeof(output_interfaces) / sizeof(output_interfaces[0]); |
| 112 | + return output_interfaces; |
| 113 | +} |
| 114 | + |
| 115 | +// ============================================================================ |
| 116 | +// APP INITIALIZATION |
| 117 | +// ============================================================================ |
| 118 | + |
| 119 | +void app_init(void) |
| 120 | +{ |
| 121 | + printf("[app:neogeo2usb] Initializing NEOGEO2USB v%s\n", APP_VERSION); |
| 122 | + |
| 123 | + // Initialize button service |
| 124 | + button_init(); |
| 125 | + button_set_callback(on_button_event); |
| 126 | + |
| 127 | + // Initialize Arcade host driver input |
| 128 | + arcade_host_init_pins(&arcade_config[0]); |
| 129 | + |
| 130 | + // Configure router for NEOGEO → USB routing |
| 131 | + router_config_t router_cfg = { |
| 132 | + .mode = ROUTING_MODE, |
| 133 | + .merge_mode = MERGE_MODE, |
| 134 | + .max_players_per_output = { |
| 135 | + [OUTPUT_TARGET_USB_DEVICE] = USB_OUTPUT_PORTS, |
| 136 | + }, |
| 137 | + .merge_all_inputs = false, |
| 138 | + .transform_flags = TRANSFORM_NONE, |
| 139 | + .mouse_drain_rate = 8, |
| 140 | + }; |
| 141 | + router_init(&router_cfg); |
| 142 | + |
| 143 | + // Add route: Native NEOGEO → USB Device |
| 144 | + router_add_route(INPUT_SOURCE_NATIVE_ARCADE, OUTPUT_TARGET_USB_DEVICE, 0); |
| 145 | + |
| 146 | + // Configure player management |
| 147 | + player_config_t player_cfg = { |
| 148 | + .slot_mode = PLAYER_SLOT_MODE, |
| 149 | + .max_slots = MAX_PLAYER_SLOTS, |
| 150 | + .auto_assign_on_press = AUTO_ASSIGN_ON_PRESS, |
| 151 | + }; |
| 152 | + players_init_with_config(&player_cfg); |
| 153 | + |
| 154 | + // Initialize profile system with button combos (Select+Start=Home) |
| 155 | + static const profile_config_t profile_cfg = { |
| 156 | + .output_profiles = { NULL }, |
| 157 | + .shared_profiles = &neogeo2usb_profile_set, |
| 158 | + }; |
| 159 | + profile_init(&profile_cfg); |
| 160 | + |
| 161 | + printf("[app:neogeo2usb] Initialization complete\n"); |
| 162 | + printf("[app:neogeo2usb] Routing: NEOGEO → USB HID Gamepad\n"); |
| 163 | + printf("[app:neogeo2usb] NEOGEO pins: B1=%d B2=%d B3=%d B4=%d B5=%d B6=%d\n", |
| 164 | + arcade_config[0].pin_p1, arcade_config[0].pin_p2, arcade_config[0].pin_p3, |
| 165 | + arcade_config[0].pin_k1, arcade_config[0].pin_k2, arcade_config[0].pin_k3); |
| 166 | + printf("[app:neogeo2usb] NEOGEO pins: DU=%d DD=%d DL=%d DR=%d S1=%d S2=%d\n", |
| 167 | + arcade_config[0].pin_du, arcade_config[0].pin_dd, arcade_config[0].pin_dl, |
| 168 | + arcade_config[0].pin_dr, arcade_config[0].pin_s1, arcade_config[0].pin_s2); |
| 169 | +} |
| 170 | + |
| 171 | +// ============================================================================ |
| 172 | +// APP TASK |
| 173 | +// ============================================================================ |
| 174 | + |
| 175 | +void app_task(void) |
| 176 | +{ |
| 177 | + button_task(); |
| 178 | +} |
0 commit comments