Switch2-Controllers icon indicating copy to clipboard operation
Switch2-Controllers copied to clipboard

NSO NS2 GameCube Controller Support

Open evanclue opened this issue 1 year ago • 39 comments

Hello. I tried to use the latest version of the script to connect an NSO NS2 GameCube Controller, and it doesn't seem to connect. I thought it was listed as supported, but on second glance I have to assume that it's referring to an original GameCube controller?

I would be so grateful if NSO GameCube support was added, so I can use it wirelessly on PC. Much love, and thank you for your efforts with the script!

evanclue avatar Jun 28 '25 23:06 evanclue

Hi! Thanks for your message. This script is actually made for the NSO NS2 GameCube Controller, and it does work over Bluetooth—but there are a few requirements:

Make sure you have both bleak and vgamepad installed. It only runs on Windows at the moment.

If it's not connecting, it would really help if you could share the error message or describe what exactly happens when you try to run it. That way, I can help you troubleshoot more easily! Much appreciated, and happy to help

Nohzockt avatar Jun 30 '25 20:06 Nohzockt

Thank you for the clarification!

I have all the dependencies installed, and the script runs fine, but it never manages to find the controller in any scan. The controller isn't already connected to any existing device, and it's definitely in pairing mode when the scan happens. There's no error message, it just searches endlessly.

I tried it on two different computers and got the same result. Both running Windows 11, Python version 3.13.

https://i.imgur.com/7t8tmmS.png

evanclue avatar Jun 30 '25 21:06 evanclue

That's strange. I can only think of two reasons. Maybe your controller does have another PID, and you would have to find that out and change it in the code. The other thing could be: do you own a Nintendo Switch 2? The controller needs a firmware update, as I am aware that can be done by connecting and using it on a Switch 2. I have heard of another option using documentation from here: connecting the controller via USB https://docs.handheldlegend.com/s/link-zone, but I personally didn't test this out and don't know if it works.

Nohzockt avatar Jun 30 '25 21:06 Nohzockt

Just for your information I am running windows 10 and python version 3.12 with the newest version of vigem bus and of vgamepad and with bleak version 0.21.1

Nohzockt avatar Jun 30 '25 21:06 Nohzockt

Thanks for the info. Regrettably, I do own an NS2, and the "Update Controllers" prompt states that all controllers are fully updated.

I also attempted to connect a Joycon2, no dice there either.

Do you know how I can find the PID for a Bluetooth LE device? I thought PID/VID were specifically a USB-HID thing.

evanclue avatar Jun 30 '25 21:06 evanclue

Well you can use a script like this to scan for Bluetooth devices. Run this script with your controller in pairing mode. Check the output to see if your controller is detected. If it appears in the list, you can look at the manufacturer data to find the part representing your controller (usually the Vendor ID and Product ID). Once you have those, you can proceed to add support for your controller in the main code.

If your controller does not show up with this simple script, there might be an issue with your Bluetooth module. :

import asyncio
from bleak import BleakScanner

async def main():
    devices = await BleakScanner.discover(timeout=10.0)
    for d in devices:
        print(f"{d.name} ({d.address}): {d.metadata.get('manufacturer_data', {})}")

asyncio.run(main())

Nohzockt avatar Jun 30 '25 22:06 Nohzockt

Long story short, your script didn't work for me, so I modified it a bit. Here's the one that ended up doing what I needed.

from bleak import BleakScanner

async def main():
    print("Scanning for Bluetooth devices...")
    devices = await BleakScanner.discover(timeout=10.0)
    
    for d in devices:
        print(f"{d.name or 'Unknown'} ({d.address})")

asyncio.run(main())

I ran a scan when the controller was in pairing mode, and a scan when it was not, and I was able to successfully isolate the MAC address. It's 3C:A9:AB:5F:9A:88. Good to know that it's not my Bluetooth module.

I was also able to pull the manufacturer data. {1363: b'\x01\x00\x03~\x05s \x00\x01\x00\x00\x00\x00\x00\x00\x00\x0f\x00\x00\x00\x00\x00\x00\x00'} Though, I'm unsure what to do with this information.

evanclue avatar Jun 30 '25 22:06 evanclue

You can try using this it is kind of hard troubleshooting this one hope this works for you

#!/usr/bin/env python3
"""
NS2 Bluetooth Monitor (Python) v1.6 - Patched for Specific MAC/Manufacturer Data
With calibration, improved rumble, player LED logic, and pairing logic!
Joy-Cons are only supported as single controllers.

This version is patched to specifically find and connect to a controller
with MAC address 3C:A9:AB:5F:9A:88 or matching manufacturer data.

If you want to connect to a different controller, update the CONTROLLER_MAC
or CONTROLLER_MANUFACTURER_DATA below.
"""

import asyncio
import signal
import sys
import platform
import argparse
import threading
import time
from enum import IntEnum
from bleak import BleakScanner, BleakClient
import vgamepad as vg

gamepad = vg.VX360Gamepad()

# --- PATCHED: Target controller identifiers ---
CONTROLLER_MAC = "3C:A9:AB:5F:9A:88"  # Uppercase, no trailing spaces
CONTROLLER_MANUFACTURER_KEY = 1363
CONTROLLER_MANUFACTURER_DATA = b'\x01\x00\x03~\x05s \x00\x01\x00\x00\x00\x00\x00\x00\x00\x0f\x00\x00\x00\x00\x00\x00\x00'

# Nintendo Switch Controller IDs
VENDOR_ID = 0x057E
PRODUCT_ID_PRO = 0x2009
PRODUCT_ID_L = 0x2006
PRODUCT_ID_R = 0x2007
PRODUCT_ID_GC = 0x2073
rumble_counter = 0

# --- Trigger deadzone configuration ---
TRIGGER_DEADZONE_L = 35
TRIGGER_DEADZONE_R = 35
TRIGGER_THRESHOLD_L = 209
TRIGGER_THRESHOLD_R = 209

# UUIDs
HID_SERVICE_UUID = "00001812-0000-1000-8000-00805f9b34fb"
NINTENDO_SERVICE_UUID = "ab7de9be-89fe-49ad-828f-118f09df7fd0"
NINTENDO_INPUT_UUID = "ab7de9be-89fe-49ad-828f-118f09df7fd2"

BT_HID_LED_DEV_ID_MAP = [0x01, 0x02, 0x04, 0x08, 0x03, 0x06, 0x0C, 0x0F]

class ControllerState(IntEnum):
    READ_INFO = 0
    READ_LTK = 1
    SET_BDADDR = 2
    READ_NEW_LTK = 3
    SET_LED = 4
    EN_REPORT = 5
    DONE = 6

class SW2(IntEnum):
    Y = 0
    X = 1
    B = 2
    A = 3
    R_SR = 4
    R_SL = 5
    R = 6
    ZR = 7
    MINUS = 8
    PLUS = 9
    RJ = 10
    LJ = 11
    HOME = 12
    CAPTURE = 13
    C = 14
    UNKNOWN = 15
    DOWN = 16
    UP = 17
    RIGHT = 18
    LEFT = 19
    L_SR = 20
    L_SL = 21
    L = 22
    ZL = 23
    GR = 24
    GL = 25

# GameCube Controller Button Mapping
GC_BUTTON_MAP = {
    SW2.A: "A",
    SW2.B: "B",
    SW2.X: "X",
    SW2.Y: "Y",
    SW2.PLUS: "Start",
    SW2.C: "C",
    SW2.HOME: "Home",
    SW2.CAPTURE: "Capture",
    SW2.ZL: "ZL",
    SW2.L: "L",
    SW2.ZR: "Z",
    SW2.R: "R",
    SW2.UP: "DPad-Up",
    SW2.DOWN: "DPad-Down",
    SW2.LEFT: "DPad-Left",
    SW2.RIGHT: "DPad-Right",
}

XBOX_BUTTON_MAP = {
    "A": vg.XUSB_BUTTON.XUSB_GAMEPAD_A,
    "B": vg.XUSB_BUTTON.XUSB_GAMEPAD_B,
    "X": vg.XUSB_BUTTON.XUSB_GAMEPAD_X,
    "Y": vg.XUSB_BUTTON.XUSB_GAMEPAD_Y,
    "Start": vg.XUSB_BUTTON.XUSB_GAMEPAD_START,
    "Z": vg.XUSB_BUTTON.XUSB_GAMEPAD_RIGHT_SHOULDER,
    "ZL": vg.XUSB_BUTTON.XUSB_GAMEPAD_LEFT_SHOULDER,
    "DPad-Up": vg.XUSB_BUTTON.XUSB_GAMEPAD_DPAD_UP,
    "DPad-Down": vg.XUSB_BUTTON.XUSB_GAMEPAD_DPAD_DOWN,
    "DPad-Left": vg.XUSB_BUTTON.XUSB_GAMEPAD_DPAD_LEFT,
    "DPad-Right": vg.XUSB_BUTTON.XUSB_GAMEPAD_DPAD_RIGHT,
}

# Switch Pro/Joy-Con Button Mapping
SWITCH_BUTTON_MAP = {
    SW2.A: "A",
    SW2.B: "B",
    SW2.X: "X",
    SW2.Y: "Y",
    SW2.PLUS: "Plus",
    SW2.MINUS: "Minus",
    SW2.C: "C",
    SW2.HOME: "Home",
    SW2.CAPTURE: "Capture",
    SW2.ZL: "ZL",
    SW2.L: "L",
    SW2.ZR: "ZR",
    SW2.R: "R",
    SW2.R_SR: "R-SR",
    SW2.R_SL: "R-SL",
    SW2.L_SR: "L-SR",
    SW2.L_SL: "L-SL",
    SW2.UP: "DPad-Up",
    SW2.DOWN: "DPad-Down",
    SW2.LEFT: "DPad-Left",
    SW2.RIGHT: "DPad-Right",
    SW2.LJ: "LStick",
    SW2.RJ: "RStick",
}

keep_running = True
debug_mode = False
verbose_mode = False
controller_state = None
output_characteristic = None
input_characteristic = None
nintendo_device_info = {}
current_state = ControllerState.READ_INFO
last_raw_data = None

# Calibration Defaults (Fallback, if not found)
default_calibration = {
    "LX": {"min": 746, "center": 1998, "max": 3249, "deadzone": 300},
    "LY": {"min": 746, "center": 1998, "max": 3249, "deadzone": 300},
    "RX": {"min": 746, "center": 1998, "max": 3249, "deadzone": 300},
    "RY": {"min": 746, "center": 1998, "max": 3249, "deadzone": 300},
}
controller_calibration = default_calibration.copy()

current_ble_client = None
rumble_event_loop = None
rumble_active = False

def handle_signal(signum, frame):
    global keep_running
    print("\nProgram is terminating...")
    keep_running = False

def log_debug(message):
    if debug_mode:
        print(f"[DEBUG] {message}")

def log_verbose(message):
    if verbose_mode:
        print(f"[VERBOSE] {message}")

def extract_nintendo_info(manufacturer_data):
    if not manufacturer_data:
        return None
    for company_id, data in manufacturer_data.items():
        if not data or len(data) < 6:
            continue
        if len(data) >= 5 and data[2] == 0x03 and data[3] == 0x7E:
            vendor_id = 0x057E
            product_id = (data[5] << 8) | data[4] if len(data) > 5 else 0
            return (vendor_id, product_id)
    return None

def is_target_controller(device):
    # Check by MAC address (case-insensitive)
    if device.address.replace("-", ":").upper() == CONTROLLER_MAC:
        return True
    # Check by manufacturer data
    manufacturer_data = getattr(device, "metadata", {}).get("manufacturer_data", {})
    for key, val in manufacturer_data.items():
        if key == CONTROLLER_MANUFACTURER_KEY and val == CONTROLLER_MANUFACTURER_DATA:
            return True
    return False

def is_nintendo_device(device):
    # Only return True if this is our target controller
    if is_target_controller(device):
        nintendo_device_info[device.address] = {
            'vendor_id': VENDOR_ID,
            'product_id': PRODUCT_ID_GC,  # Assume GameCube, can be modified if known
            'name': device.name or "NS2 Controller"
        }
        return True
    return False

def get_nintendo_device_name(device):
    if device.address not in nintendo_device_info:
        return device.name or "Nintendo device"
    info = nintendo_device_info[device.address]
    pid = info.get('product_id', 0)
    if pid == PRODUCT_ID_PRO:
        return "Nintendo Switch Pro Controller"
    elif pid == PRODUCT_ID_L:
        return "Nintendo Switch Joy-Con (L)"
    elif pid == PRODUCT_ID_R:
        return "Nintendo Switch Joy-Con (R)"
    elif pid == PRODUCT_ID_GC:
        return "Nintendo GameCube Controller"
    else:
        return f"Nintendo Controller (PID: 0x{pid:04X})"

def apply_trigger_deadzone_and_threshold(value, deadzone, threshold):
    if abs(value) < deadzone:
        return 0
    if value >= threshold:
        return 255
    return value

def extract_gc_triggers(data):
    left_trigger = 0
    right_trigger = 0
    if len(data) >= 62:
        left_trigger = data[60]
        right_trigger = data[61]
    elif len(data) >= 14:
        left_trigger = data[12]
        right_trigger = data[13]
    if left_trigger == 0 and len(data) >= 5:
        buttons = int.from_bytes(data[4:8], byteorder='little')
        if buttons & (1 << SW2.L):
            left_trigger = 255
    if right_trigger == 0 and len(data) >= 5:
        buttons = int.from_bytes(data[4:8], byteorder='little')
        if buttons & (1 << SW2.R):
            right_trigger = 255
    left_trigger = apply_trigger_deadzone_and_threshold(left_trigger, TRIGGER_DEADZONE_L, TRIGGER_THRESHOLD_L)
    right_trigger = apply_trigger_deadzone_and_threshold(right_trigger, TRIGGER_DEADZONE_R, TRIGGER_THRESHOLD_R)
    return left_trigger, right_trigger

def get_pressed_buttons_switch(button_value):
    return [name for bit, name in SWITCH_BUTTON_MAP.items() if button_value & (1 << bit)]

def get_pressed_buttons_gc(button_value):
    return [name for bit, name in GC_BUTTON_MAP.items() if button_value & (1 << bit)]

def print_raw_bytes(data):
    if not data or len(data) < 16:
        return
    raw_str = " ".join([f"{b:02X}" for b in data[:16]])
    return f"Raw: {raw_str}"

def update_xbox_gamepad(pressed_gc_buttons, L, R, LX, LY, RX, RY):
    gamepad.reset()
    for btn in pressed_gc_buttons:
        xb_btn = XBOX_BUTTON_MAP.get(btn)
        if xb_btn:
            gamepad.press_button(xb_btn)
    gamepad.left_trigger(value=L)
    gamepad.right_trigger(value=R)
    gamepad.left_joystick(x_value=LX, y_value=LY)
    gamepad.right_joystick(x_value=RX, y_value=RY)
    gamepad.update()

def parse_calibration_from_report(data):
    if len(data) < 28:
        return None
    try:
        lx = int.from_bytes(data[12:14], "little")
        ly = int.from_bytes(data[14:16], "little")
        rx = int.from_bytes(data[21:23], "little")
        ry = int.from_bytes(data[23:25], "little")
        return {
            "LX": {"min": max(lx-900, 0), "center": lx, "max": lx+900, "deadzone": 300},
            "LY": {"min": max(ly-900, 0), "center": ly, "max": ly+900, "deadzone": 300},
            "RX": {"min": max(rx-900, 0), "center": rx, "max": rx+900, "deadzone": 300},
            "RY": {"min": max(ry-900, 0), "center": ry, "max": ry+900, "deadzone": 300},
        }
    except Exception as e:
        log_debug(f"Could not parse calibration from report: {e}")
        return None

def is_calibration_spi_reply(data):
    if len(data) < 16:
        return False
    spi_offset = int.from_bytes(data[8:12], "little")
    spi_len = int.from_bytes(data[6:8], "little")
    return spi_offset == 0x603D0 and spi_len == 0x18

def normalize_axis(value, axis_name="LX"):
    calib = controller_calibration.get(axis_name, default_calibration[axis_name])
    min_val = calib["min"]
    center_val = calib["center"]
    max_val = calib["max"]
    deadzone = calib["deadzone"]
    if value < min_val:
        value = min_val
    if value > max_val:
        value = max_val
    if abs(value - center_val) < deadzone:
        return 0
    if value >= center_val:
        return int(((value - center_val) / (max_val - center_val)) * 32767)
    else:
        return int(((value - center_val) / (center_val - min_val)) * 32768)

async def notification_callback(sender, data):
    global controller_state, last_raw_data, controller_calibration
    if not data or len(data) < 10:
        return
    last_raw_data = data
    if len(data) >= 32 and data[0] == 0x21 and data[1] == 0x01 and is_calibration_spi_reply(data):
        parsed = parse_calibration_from_report(data)
        if parsed:
            controller_calibration = parsed
            print("\n[INFO] Calibration data loaded:", controller_calibration)
    pid = controller_state.get('product_id', PRODUCT_ID_PRO) if controller_state else PRODUCT_ID_PRO
    if len(data) >= 8:
        button_data = int.from_bytes(data[4:8], byteorder='little')
    else:
        button_data = 0
    axes = [0, 0, 0, 0, 0, 0]
    if len(data) >= 16:
        axes_data = data[10:16]
        axes[0] = normalize_axis(axes_data[0] | ((axes_data[1] & 0xF) << 8), "LX")
        axes[1] = normalize_axis((axes_data[1] >> 4) | (axes_data[2] << 4), "LY")
        axes[2] = normalize_axis(axes_data[3] | ((axes_data[4] & 0xF) << 8), "RX")
        axes[3] = normalize_axis((axes_data[4] >> 4) | (axes_data[5] << 4), "RY")
    if pid == PRODUCT_ID_GC:
        left_trigger, right_trigger = extract_gc_triggers(data)
        axes[4] = left_trigger
        axes[5] = right_trigger
        pressed = get_pressed_buttons_gc(button_data)
        update_xbox_gamepad(pressed, axes[4], axes[5], axes[0], axes[1], axes[2], axes[3])
        btns_display = ", ".join(pressed) if pressed else "none"
        trigger_display = f" | L:{axes[4]:3d} R:{axes[5]:3d}"
        axes_display = f"LX:{axes[0]:3d} LY:{axes[1]:3d} RX:{axes[2]:3d} RY:{axes[3]:3d}"
        if debug_mode:
            raw_display = print_raw_bytes(data)
            print(f"\r[GC] Buttons: {btns_display:<30} | Sticks: {axes_display} {trigger_display} | {raw_display}", end="")
        else:
            print(f"\r[GC] Buttons: {btns_display:<30} | Sticks: {axes_display} {trigger_display}", end="")
    else:
        pressed = get_pressed_buttons_switch(button_data)
        btns_display = ", ".join(pressed) if pressed else "none"
        axes_display = f"LX:{axes[0]:3d} LY:{axes[1]:3d} RX:{axes[2]:3d} RY:{axes[3]:3d}"
        if debug_mode:
            raw_display = print_raw_bytes(data)
            print(f"\r[SW] Buttons: {btns_display:<30} | Axes: {axes_display} | {raw_display}", end="")
        else:
            print(f"\r[SW] Buttons: {btns_display:<30} | Axes: {axes_display}", end="")

async def send_command(client, command, retry=3):
    global output_characteristic
    if not output_characteristic:
        log_debug("No output characteristic found!")
        return False
    try:
        log_verbose(f"Sending command: {command.hex(' ')}")
        await client.write_gatt_char(output_characteristic, command)
        return True
    except Exception as e:
        if retry > 0:
            log_debug(f"Error sending (attempt {4 - retry}/3): {e}")
            await asyncio.sleep(0.1)
            return await send_command(client, command, retry - 1)
        else:
            log_debug(f"Sending failed after 3 attempts: {e}")
            return False

async def set_player_leds(client, player_num=1):
    if player_num < 1 or player_num > 8:
        player_num = 1
    led_value = BT_HID_LED_DEV_ID_MAP[player_num - 1]
    led_cmd = bytearray([
        0x30, 0x01, 0x00, 0x30, 0x00, 0x08, 0x00, 0x00,
        led_value, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    ])
    return await send_command(client, led_cmd)

async def set_rumble(client, on=True):
    global rumble_counter
    rumble_cmd = bytearray([
        0x50,
        0x50 | (rumble_counter & 0x0F),
        0x01 if on else 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00,
    ])
    rumble_counter = (rumble_counter + 1) & 0x0F
    log_verbose(
        f"Sending NS2 rumble command: {rumble_cmd.hex(' ')} (counter: {rumble_counter - 1 & 0x0F}, state: {'ON' if on else 'OFF'})")
    return await send_command(client, rumble_cmd)

async def dump_raw_data():
    global last_raw_data
    if last_raw_data:
        print("\n\nRaw data of the last report:")
        print("---------------------------------")
        for i in range(0, len(last_raw_data), 8):
            group = last_raw_data[i:i + 8]
            hex_values = " ".join([f"{b:02X}" for b in group])
            ascii_values = "".join([chr(b) if 32 <= b <= 126 else "." for b in group])
            print(f"{i:04X}: {hex_values:<24} | {ascii_values}")
        print()

def async_rumble_handler(large_motor, small_motor):
    global current_ble_client, rumble_event_loop
    if not current_ble_client or not rumble_event_loop:
        log_debug("No BLE client or event loop available for rumble")
        return
    if rumble_active:
        log_debug("Rumble active, skipping.")
        return
    log_debug(f"Received rumble request - large: {large_motor}, small: {small_motor}")
    if large_motor > 0 or small_motor > 0:
        def run_rumble():
            try:
                future = asyncio.run_coroutine_threadsafe(perform_rumble_sequence(), rumble_event_loop)
                future.result(timeout=2.0)
            except Exception as e:
                log_debug(f"Error scheduling rumble: {e}")
        threading.Thread(target=run_rumble, daemon=True).start()

async def perform_rumble_sequence():
    global current_ble_client, rumble_active
    if rumble_active:
        return
    rumble_active = True
    try:
        if current_ble_client:
            log_debug("Starting rumble sequence")
            await set_rumble(current_ble_client, True)
            await asyncio.sleep(0.2)
            await set_rumble(current_ble_client, False)
            log_debug("Rumble sequence completed")
    except Exception as e:
        log_debug(f"Error in rumble sequence: {e}")
    finally:
        rumble_active = False

def vgamepad_notification_callback(client, target, large_motor, small_motor, led_number, user_data):
    async_rumble_handler(large_motor, small_motor)

def setup_vgamepad_callback():
    try:
        gamepad.register_notification(callback_function=vgamepad_notification_callback)
        log_debug("vgamepad notification callback registered successfully")
        return True
    except Exception as e:
        log_debug(f"Failed to register vgamepad callback: {e}")
        return False

async def handle_keyboard_input(client):
    await set_rumble(client, True)
    await asyncio.sleep(0.5)
    await set_rumble(client, False)
    global debug_mode, verbose_mode, keep_running
    while keep_running:
        try:
            if platform.system() != "Windows":
                import termios, fcntl, os
                fd = sys.stdin.fileno()
                oldterm = termios.tcgetattr(fd)
                newattr = termios.tcgetattr(fd)
                newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO
                termios.tcsetattr(fd, termios.TCSANOW, newattr)
                oldflags = fcntl.fcntl(fd, fcntl.F_GETFL)
                fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK)
                try:
                    while keep_running:
                        try:
                            c = sys.stdin.read(1)
                            if c:
                                if c == 'r':
                                    print("\n🎮 Rumble test...")
                                    await set_rumble(client, True)
                                    await asyncio.sleep(0.5)
                                    await set_rumble(client, False)
                                elif c >= '1' and c <= '8':
                                    player_num = int(c)
                                    print(f"\n💡 Set player LED to {player_num}...")
                                    await set_player_leds(client, player_num)
                                elif c == 'd':
                                    debug_mode = not debug_mode
                                    print(f"\nDebug mode {'enabled' if debug_mode else 'disabled'}")
                                elif c == 'v':
                                    verbose_mode = not verbose_mode
                                    print(f"\nVerbose mode {'enabled' if verbose_mode else 'disabled'}")
                                elif c == 'x':
                                    await dump_raw_data()
                                elif c == 'k':
                                    print("\nCalibration data:")
                                    print(controller_calibration)
                        except IOError:
                            pass
                        await asyncio.sleep(0.1)
                finally:
                    termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm)
                    fcntl.fcntl(fd, fcntl.F_SETFL, oldflags)
            else:
                await asyncio.sleep(0.5)
        except Exception as e:
            log_debug(f"Error in keyboard input: {e}")
            await asyncio.sleep(1)

async def find_characteristics(client):
    global output_characteristic, input_characteristic
    try:
        services = await client.get_services()
        for service in services:
            if service.uuid.lower() == NINTENDO_SERVICE_UUID.lower():
                for char in service.characteristics:
                    props = char.properties
                    if "notify" in props and not input_characteristic:
                        input_characteristic = char.uuid
                    if ("write-without-response" in props or "write" in props) and not output_characteristic:
                        output_characteristic = char.uuid
        if not input_characteristic or not output_characteristic:
            for service in services:
                if service.uuid.lower() == HID_SERVICE_UUID.lower():
                    for char in service.characteristics:
                        props = char.properties
                        if "notify" in props and not input_characteristic:
                            input_characteristic = char.uuid
                        if ("write" in props or "write-without-response" in props) and not output_characteristic:
                            output_characteristic = char.uuid
        return input_characteristic is not None and output_characteristic is not None
    except Exception as e:
        log_debug(f"Error finding characteristics: {e}")
        return False

async def send_spi_read_calibration(client):
    spi_read = bytearray([
        0x21, 0x01, 0x00, 0x10,
        0x00, 0x18, 0x00, 0x00,
        0xd0, 0x3d, 0x06, 0x00,
        0x00, 0x00, 0x00, 0x00
    ])
    await send_command(client, spi_read)

async def initialize_controller(client, device):
    global current_state, controller_state, input_characteristic, output_characteristic, current_ble_client, rumble_event_loop, controller_calibration
    controller_info = nintendo_device_info.get(device.address, {})
    pid = controller_info.get('product_id', PRODUCT_ID_GC)
    controller_state = {'product_id': pid, 'connected': True}
    current_ble_client = client
    rumble_event_loop = asyncio.get_event_loop()
    if not await find_characteristics(client):
        print("❌ Could not find suitable characteristics.")
        return False
    print("⏳ Initializing controller...")
    current_state = ControllerState.DONE
    await client.start_notify(input_characteristic, notification_callback)
    await send_spi_read_calibration(client)
    callback_success = setup_vgamepad_callback()
    print(f"✅ Controller successfully initialized! ({get_nintendo_device_name(device)})")
    if callback_success:
        print("🎮 Rumble callback registered - games should be able to rumble the controller!")
    else:
        print("⚠️ Rumble callback registration failed - manual rumble only")
    print("\n📊 Receiving controller data...")
    print("📍 Move sticks and press buttons to see the data...")
    print("   - Press Ctrl+C to quit")
    print("   - r: Rumble test")
    print("   - 1-8: Set player LED")
    print("   - d: Toggle debug mode")
    print("   - v: Toggle verbose mode")
    print("   - x: Show raw data (byte values)")
    print("   - k: Show calibration data")
    if callback_success:
        print("   - Rumble from games should work automatically!")
    asyncio.create_task(handle_keyboard_input(client))
    return True

async def connect_to_device(device):
    global controller_state, current_ble_client, rumble_event_loop
    device_name = get_nintendo_device_name(device)
    print(f"\n🔄 Connecting to {device_name} ({device.address})...")
    try:
        async with BleakClient(device) as client:
            print(f"✅ Connected to {device_name}!")
            if await initialize_controller(client, device):
                while keep_running and client.is_connected:
                    await asyncio.sleep(0.1)
                print("\n🔌 Controller disconnected.")
            else:
                print(f"❌ Controller initialization failed.")
    except Exception as e:
        print(f"❌ Connection error: {e}")
        controller_state = None
    finally:
        current_ble_client = None
        rumble_event_loop = None

async def scan_for_nintendo_devices():
    print("\n🔍 Searching for the specified controller (5 seconds)...")
    try:
        devices = await BleakScanner.discover(timeout=5.0)
        nintendo_devices = []
        for device in devices:
            if is_nintendo_device(device):
                nintendo_devices.append(device)
                print(f"✅ Target device found: {get_nintendo_device_name(device)} ({device.address})")
        if not nintendo_devices:
            print("❌ Target controller not found.")
            print("\n📌 Make sure that:")
            print("   1. The controller is in pairing mode (LEDs blinking)")
            print("   2. Bluetooth is enabled on your device")
            print("   3. The controller is not connected to another device")
        return nintendo_devices
    except Exception as e:
        print(f"❌ Error scanning: {e}")
        return []

async def main():
    print("\n🎮 NS2 Bluetooth Enabler (Python) v1.6 [Targeted Mode]")
    print("======================================")
    print(f"🖥️  Platform: {platform.system()} {platform.release()}")
    print(f"🐍 Python: {platform.python_version()}")
    print("\nThis tool ONLY connects to the controller at:")
    print(f"   MAC: {CONTROLLER_MAC}")
    print(f"   Manufacturer Data Key: {CONTROLLER_MANUFACTURER_KEY}")
    print(f"   Manufacturer Data: {CONTROLLER_MANUFACTURER_DATA.hex(' ')}")
    print("\nSupports Pro Controller, Joy-Con and GameCube Controller.\n")
    signal.signal(signal.SIGINT, handle_signal)
    signal.signal(signal.SIGTERM, handle_signal)
    print("📋 Pairing instructions:")
    print("1. Put your controller in pairing mode:")
    print("   - Pro Controller: Small button on top of the controller")
    print("   - Joy-Con: Pairing button on the side")
    print("   - GameCube Controller: Pairing button on the top")
    print("2. Make sure the controller is not already connected to another device.\n")
    while keep_running:
        try:
            nintendo_devices = await scan_for_nintendo_devices()
            if nintendo_devices:
                await connect_to_device(nintendo_devices[0])
            if keep_running:
                print("\n⏳ Waiting 5 seconds before next scan...")
                for i in range(5, 0, -1):
                    if keep_running:
                        print(f"   Next scan in {i} seconds...", end="\r")
                        await asyncio.sleep(1)
                print(" " * 40, end="\r")
        except KeyboardInterrupt:
            break
        except Exception as e:
            print(f"❌ Error: {e}")
            await asyncio.sleep(2)
    print("\n👋 Program ended.")

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='NS2 Bluetooth Enabler (Python)')
    parser.add_argument('-d', '--debug', action='store_true', help='Enable debug output')
    parser.add_argument('-v', '--verbose', action='store_true', help='Enable verbose output')
    args = parser.parse_args()
    debug_mode = args.debug
    verbose_mode = args.verbose
    try:
        asyncio.run(main())
    except KeyboardInterrupt:
        print("\n👋 Program terminated by user.")
    except Exception as e:
        print(f"❌ Unexpected error: {e}")
    finally:
        print("✅ Daemon terminated.")

Nohzockt avatar Jun 30 '25 22:06 Nohzockt

Successfully detected, but failed to initialize?

✅ Target device found: Nintendo GameCube Controller (3C:A9:AB:5F:9A:88)

🔄 Connecting to Nintendo GameCube Controller (3C:A9:AB:5F:9A:88)...
✅ Connected to Nintendo GameCube Controller!
❌ Could not find suitable characteristics.
❌ Controller initialization failed.

evanclue avatar Jun 30 '25 22:06 evanclue

Well, now I’m really not sure what to do. The initialisation was taken over from how the guys from Blueretro did it, and I’m sorry, but for now I don’t know what to do. ):

Nohzockt avatar Jun 30 '25 22:06 Nohzockt

I've been trying to troubleshoot this, and I've had no luck. From dumping the GATT and hardcoding the UUIDs, I was able to get the script to run, but no inputs show in the terminal, and nothing is passed to the virtual X360 driver. I can't figure out why.

Image

Here is the GATT dump, just in case it ends up being useful to anyone else.

Connecting to 3C:A9:AB:5F:9A:88...
Connected.

[Service] 00c5af5d-1964-4e30-8f51-1956f96bd280
  [Characteristic] 00c5af5d-1964-4e30-8f51-1956f96bd281 | Properties: read
  [Characteristic] 00c5af5d-1964-4e30-8f51-1956f96bd282 | Properties: write
  [Characteristic] 00c5af5d-1964-4e30-8f51-1956f96bd283 | Properties: read

[Service] ab7de9be-89fe-49ad-828f-118f09df7fd0
  [Characteristic] ab7de9be-89fe-49ad-828f-118f09df7fd2 | Properties: read, notify
  [Characteristic] 8261cba1-9435-420c-84d6-f0c75a2c8e4d | Properties: read, notify
  [Characteristic] 3f8fb670-ab25-45bf-b540-38c72834d064 | Properties: write-without-response
  [Characteristic] 649d4ac9-8eb7-4e6c-af44-1ea54fe5f005 | Properties: write-without-response
  [Characteristic] af95885e-44b3-4a24-9cf0-483cc129469a | Properties: write-without-response
  [Characteristic] 4147423d-fdae-4df7-a4f7-d23e5df59f8d | Properties: write-without-response
  [Characteristic] c765a961-d9d8-4d36-a20a-5315b111836a | Properties: notify
  [Characteristic] 46f6ad29-cdaf-4569-a2fe-339020b94604 | Properties: notify
  [Characteristic] d3bd69d2-841c-4241-ab15-f86f406d2a80 | Properties: notify
  [Characteristic] ab7de9be-89fe-49ad-828f-118f09df7fde | Properties: read, notify
  [Characteristic] ab7de9be-89fe-49ad-828f-118f09df7fdf | Properties: write-without-response

[Service] 00001800-0000-1000-8000-00805f9b34fb
  [Characteristic] 00002a00-0000-1000-8000-00805f9b34fb | Properties: read
  [Characteristic] 00002a01-0000-1000-8000-00805f9b34fb | Properties: read

[Service] 00001801-0000-1000-8000-00805f9b34fb

evanclue avatar Jul 01 '25 00:07 evanclue

I am also having this issue, all dependencies are installed, the script runs fine, but it never manages to find the controller in any scan. I am also running windows 10 and python version 3.12 with the newest version of vigem bus, vgamepad and with bleak version 0.21.1. I am trying to pull my own controllers MAC address and the manufacturer data to see if it matches the one provided by Nohzockt, evanclue, or neither (the controller was also cleared from shipping mode since I own a Switch 2 and have already paired it to the console). I am pretty inexperienced with coding and both scripts provided above did not work for me. Any help would be greatly appreciated!

chalz276 avatar Jul 01 '25 02:07 chalz276

Not that this info would be of much use but I don't have a Switch 2 and my controller interacts with the program on PC as intended. Just saying hopefully the issue doesn't have to do with the shipping mode flag already being cleared, though I figure if that were the case then there'd have been reports of that by now.

HotTownJohnny avatar Jul 01 '25 03:07 HotTownJohnny

I am trying to pull my own controllers MAC address and the manufacturer data to see if it matches the one provided by Nohzockt, evanclue, or neither

Just in case it gleans something, here's the script I used to dump my GameCube controller's manufacturer data.

import asyncio
from bleak import BleakScanner

TARGET_PREFIX = "3C:A9:AB"

def detection_callback(device, advertisement_data):
    if device.address.upper().startswith(TARGET_PREFIX):
        print(f"\n🎮 Found device: {device.name or 'Unknown'} ({device.address})")
        print(f"Manufacturer data: {advertisement_data.manufacturer_data}")

async def main():
    print("Scanning for Nintendo controllers (3C:A9:AB)...")
    scanner = BleakScanner(detection_callback)
    await scanner.start()
    await asyncio.sleep(10.0)
    await scanner.stop()

asyncio.run(main())

and here's what I used to dump the GATT characteristics.

import asyncio
from bleak import BleakClient

ADDRESS = "3C:A9:AB:5F:9A:88"

async def main():
    print(f"Connecting to {ADDRESS}...")
    async with BleakClient(ADDRESS) as client:
        print("Connected.")

        # Wait until services are discovered
        services = client.services
        if services is None:
            # Discover services explicitly if not already done
            services = await client.get_services()

        for service in services:
            print(f"\n[Service] {service.uuid}")
            for char in service.characteristics:
                props = ", ".join(char.properties)
                print(f"  [Characteristic] {char.uuid} | Properties: {props}")

asyncio.run(main())

evanclue avatar Jul 01 '25 03:07 evanclue

Just in case it gleans something, here's the script I used to dump my GameCube controller's manufacturer data.

Doing that, I was able to get MAC address and manufacturer data and it is different 🎮 Found device: Unknown (3C:A9:AB:5E:81:12) Manufacturer data: {1363: b'\x01\x00\x03~\x05s \x00\x01\x00\xf370U\xe2\x98\x0f\x00\x00\x00\x00\x00\x00\x00'}

I put the new information in the patched ns2-ble-cgamepad.py, it does find the device, but still does not work 🔄 Connecting to Nintendo GameCube Controller (3C:A9:AB:5E:81:12)... ❌ Connection error: Could not get GATT services: Unreachable

When I use the script provided to dump the GATT characteristics, it attempts to connect, but crashes once it does.

chalz276 avatar Jul 01 '25 04:07 chalz276

When I use the script provided to dump the GATT characteristics, it attempts to connect, but crashes once it does.

Just to clarify, I am using the latest version of bleak, version 1.0.0. My script was written with that version in mind, since it handles metadata a little bit differently.

That being said, should I downgrade? I haven't tried that yet.

evanclue avatar Jul 01 '25 04:07 evanclue

That being said, should I downgrade? I haven't tried that yet.

I believe mine is bleak v1.0.1, idk how much of a difference its gonna make but I also have no idea what I'm doing lol

chalz276 avatar Jul 01 '25 04:07 chalz276

I believe mine is bleak v1.0.1, idk how much of a difference its gonna make but I also have no idea what I'm doing lol

pip freeze will show you what version you're running

evanclue avatar Jul 01 '25 05:07 evanclue

So after messing around with stuff, I realized that the reason for why the GATT services were unreachable, as well as the GATT dumper crashing. It was because the controller was synced to my computer as a device while running the scripts (I wasn't sure if that was something you had to do for the scripts to work). After running each script again, I got similar results to evanclue with the patched ns2-ble-cgamepad.py

✅ Target device found: Nintendo GameCube Controller (3C:A9:AB:5E:81:12)

🔄 Connecting to Nintendo GameCube Controller (3C:A9:AB:5E:81:12)... ✅ Connected to Nintendo GameCube Controller! ❌ Could not find suitable characteristics. ❌ Controller initialization failed.

This is the result from my GATT dump

Connecting to 3C:A9:AB:5E:81:12... Connected.

[Service] 00c5af5d-1964-4e30-8f51-1956f96bd280 [Characteristic] 00c5af5d-1964-4e30-8f51-1956f96bd281 | Properties: read [Characteristic] 00c5af5d-1964-4e30-8f51-1956f96bd282 | Properties: write [Characteristic] 00c5af5d-1964-4e30-8f51-1956f96bd283 | Properties: read

[Service] ab7de9be-89fe-49ad-828f-118f09df7fd0 [Characteristic] ab7de9be-89fe-49ad-828f-118f09df7fd2 | Properties: notify, read [Characteristic] 8261cba1-9435-420c-84d6-f0c75a2c8e4d | Properties: notify, read [Characteristic] 3f8fb670-ab25-45bf-b540-38c72834d064 | Properties: write-without-response [Characteristic] 649d4ac9-8eb7-4e6c-af44-1ea54fe5f005 | Properties: write-without-response [Characteristic] af95885e-44b3-4a24-9cf0-483cc129469a | Properties: write-without-response [Characteristic] 4147423d-fdae-4df7-a4f7-d23e5df59f8d | Properties: write-without-response [Characteristic] c765a961-d9d8-4d36-a20a-5315b111836a | Properties: notify [Characteristic] 46f6ad29-cdaf-4569-a2fe-339020b94604 | Properties: notify [Characteristic] d3bd69d2-841c-4241-ab15-f86f406d2a80 | Properties: notify [Characteristic] ab7de9be-89fe-49ad-828f-118f09df7fde | Properties: notify, read [Characteristic] ab7de9be-89fe-49ad-828f-118f09df7fdf | Properties: write-without-response

[Service] 00001800-0000-1000-8000-00805f9b34fb [Characteristic] 00002a00-0000-1000-8000-00805f9b34fb | Properties: read [Characteristic] 00002a01-0000-1000-8000-00805f9b34fb | Properties: read

[Service] 00001801-0000-1000-8000-00805f9b34fb

chalz276 avatar Jul 01 '25 05:07 chalz276

pip freeze will show you what version you're running

Yeah it is v1.0.1

EDIT Also idk if its worth mentioning that my controller does work with handheldlegend's ProCon 2 Enabler Tool https://handheldlegend.github.io/procon2tool/

I'm not sure how the shipping mode thing is handled there but it does work (although missing some features compared to this script)

chalz276 avatar Jul 01 '25 05:07 chalz276