Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
dd374bc
Python: basic pybind11 interface to the microcontroller
pa-devbot Oct 18, 2026
871232d
Python: stop committing pip egg-info leftovers
pa-devbot Oct 18, 2026
0f9c150
Python: switch to cancel_all_and_wait(); stop miniz.c warnings breaki…
pa-devbot Oct 19, 2026
d34e550
Move the miniz.c warning suppression into pa_apply_base_target_proper…
pa-devbot Oct 19, 2026
7b07de2
Put the miniz.c warning suppression in the base CMakeLists.txt, befor…
pa-devbot Oct 19, 2026
6de7657
update pybind README
jw098 Oct 19, 2026
d4da646
Python bindings README: small fixes
pa-devbot Oct 19, 2026
7b58308
more README updates
jw098 Oct 19, 2026
babadf9
pybind CI
jw098 Oct 19, 2026
5b3eaa0
update CI
jw098 Oct 19, 2026
903085e
rename selftest to controller_test
jw098 Oct 19, 2026
253fa4f
rename files
jw098 Oct 20, 2026
e582f37
Python: label where the Python code references the C++ code
pa-devbot Oct 20, 2026
6c9ff5f
minor update to switch_connection_test.py
jw098 Oct 20, 2026
7370e4e
add comments
jw098 Oct 20, 2026
9e7b6b5
minor update to switch_connection_test.py
jw098 Oct 20, 2026
31533d9
add comments
jw098 Oct 20, 2026
26bfca3
Python: name SwitchController commands after the backend methods
pa-devbot Oct 20, 2026
3409e3e
Python: drop the repeat option from push_button()/push_dpad()
pa-devbot Oct 20, 2026
c0dadd7
add comments
jw098 Oct 20, 2026
8b66908
Python tests: add a short docstring to every test function
pa-devbot Oct 21, 2026
13ce1dd
Merge branch 'python-pybind-basic' of https://github.com/PokemonAutom…
jw098 Oct 21, 2026
c0ccd74
add comments
jw098 Oct 21, 2026
8f6d515
update comments
jw098 Oct 21, 2026
74385b8
Python tests: drop test_matches_cpp_enum, duplicate of test_python_co…
pa-devbot Oct 21, 2026
4884117
add comments
jw098 Oct 21, 2026
d231865
rename variables
jw098 Oct 21, 2026
d8913a3
Python: follow the duration_ms/hold_ms/bitfield renames through all c…
pa-devbot Oct 21, 2026
f7d16c4
Align pybind/Python controller API with the PBF API
pa-devbot Oct 21, 2026
b594981
push_*_joystick(): back to separate x, y arguments
pa-devbot Oct 21, 2026
0a7eb70
Python push_*_joystick(): accept an (x, y) joystick_position tuple
pa-devbot Oct 21, 2026
6ab581a
update comments. add kwargs
jw098 Oct 21, 2026
9c9a008
Python controller_state(): left_joystick/right_joystick as (x, y) tuples
pa-devbot Oct 21, 2026
04884c5
Python controller_state(): split buttons and dpad; push_dpad arg -> dpad
pa-devbot Oct 21, 2026
c1425e8
update comments
jw098 Oct 22, 2026
42b9a44
minor fix
jw098 Oct 22, 2026
f9a9070
update switch_connection_test.py
jw098 Oct 22, 2026
dc683a2
update comments
jw098 Oct 22, 2026
da17496
update README
jw098 Oct 22, 2026
7f515b4
update comments
jw098 Oct 22, 2026
4bfcec8
update comments
jw098 Oct 22, 2026
736d0eb
update pybind CI
jw098 Oct 22, 2026
f3a533f
PythonBindings.cmake: do not download pybind11; error with install steps
pa-devbot Oct 22, 2026
e014c47
update pybind CI
jw098 Oct 22, 2026
ce5ebac
update comments
jw098 Oct 22, 2026
2590c3a
Python bindings: fix doc typos and stale example kwargs
pa-devbot Oct 22, 2026
b1698ea
Fix bug: close() serialization
jw098 Oct 22, 2026
9bafba2
fix Empty port name: SerialPABotBase2_Descriptor::open_connection() r…
jw098 Oct 22, 2026
3d248db
Fix review bugs: destruction race, empty port
jw098 Oct 22, 2026
49d1cfa
update README
jw098 Oct 22, 2026
4bc8307
update README
jw098 Oct 23, 2026
c100ca0
Rename SwitchController to SwitchProController
pa-devbot Oct 24, 2026
54093a5
add comments
jw098 Oct 24, 2026
92e6cdb
buttons.py: remove the BUTTON_ALIASES table
pa-devbot Oct 24, 2026
8ec0607
fix comment
jw098 Nov 7, 2026
8dad423
update comments
jw098 Nov 7, 2026
fa5597a
minor update to error message
jw098 Nov 7, 2026
46581b7
update comments
jw098 Nov 7, 2026
d927703
minor fixes
jw098 Nov 7, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions .github/workflows/cpp-ci-python-bindings.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: C++ CI Python Bindings
on:
push:
branches: [main]
workflow_dispatch:
pull_request:
types: [opened, synchronize, reopened, ready_for_review]

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

# Builds the GUI-free core plus the _pa_core pybind11 module
# (Source/PythonBindings/README.md) and runs the package's pytest suite.
# This needs no Qt, OpenCV, ONNX Runtime, Tesseract, DPP or Discord SDK.

jobs:
build:
# if: github.event_name != 'pull_request' || github.event.pull_request.draft == false
runs-on: ubuntu-26.04

steps:
- name: Checkout Arduino-Source
uses: actions/checkout@v7
with:
path: 'Arduino-Source'
submodules: 'recursive'

# Step 1: Create a single environment for the entire job
- name: Set up Virtual Environment
run: |
python3 -m venv /tmp/pa-build-venv
/tmp/pa-build-venv/bin/pip install --upgrade pip
/tmp/pa-build-venv/bin/pip install pybind11 pytest

- name: Generate binaries
run: |
cd Arduino-Source/SerialPrograms
mkdir bin
cd bin
cmake .. -DPA_CORE_ONLY=ON -DPA_PYTHON_BINDINGS=ON -DPython_EXECUTABLE=/tmp/pa-build-venv/bin/python
cmake --build . --config RelWithDebInfo --parallel 10

- name: Run the Python tests
run: |
cd Arduino-Source/SerialPrograms/Source/PythonBindings
/tmp/pa-build-venv/bin/python -m pytest -q

# Note: the venv avoids PEP 668 issues with system-pip on newer runners.
# The runner's system Python needs dev headers (python3-dev is preinstalled on the images).
39 changes: 32 additions & 7 deletions SerialPrograms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,30 @@ find_package(Threads REQUIRED)
# Define a toggle option (ON by default)
option(COMMAND_LINE_EXE "Build the optional executable" ON)

# Build the _pa_core Python (pybind11) module; see Source/PythonBindings/README.md.
# OFF by default because it needs a Python interpreter with development headers
# and pybind11 (a system package or `pip install pybind11`).
option(PA_PYTHON_BINDINGS "Build the _pa_core Python module (Switch controller)" OFF)

# miniz is a third-party library compiled into every target that uses
# CORE_LIBRARY_SOURCES (CoreLib in both build modes, SerialProgramsLib in the
# full build): compile it with warnings off so its own code (and its header,
# which it includes from its own directory, so no SYSTEM include directory can
# shield it) can never trip -Werror / /WX, regardless of compiler or compiler
# version. Must run before the PA_CORE_ONLY early return below; source
# properties are global, so setting it once covers every target.
if (MSVC)
set_source_files_properties(
../3rdParty-Core/miniz-3.1.1/miniz.c
PROPERTIES COMPILE_OPTIONS "/W0"
)
else()
set_source_files_properties(
../3rdParty-Core/miniz-3.1.1/miniz.c
PROPERTIES COMPILE_OPTIONS "-w"
)
endif()

# Build only the GUI-free core: CoreLib and SerialProgramsCommandLine. This needs
# no Qt, OpenCV, ONNX Runtime, Tesseract, DPP or Discord SDK, so none of them are
# searched for.
Expand All @@ -84,6 +108,9 @@ if(PA_CORE_ONLY)
if(COMMAND_LINE_EXE)
include(Source/CommandLine/CommandLineExecutable.cmake)
endif()
if(PA_PYTHON_BINDINGS)
include(Source/PythonBindings/PythonBindings.cmake)
endif()
return()
endif()

Expand Down Expand Up @@ -283,13 +310,6 @@ endfunction()
apply_common_target_properties(SerialProgramsLib)
apply_common_target_properties(SerialPrograms)

if(MSVC)
set_source_files_properties("../3rdParty-Core/miniz-3.1.1/miniz.c" PROPERTIES COMPILE_FLAGS "/W0")
else()
# For GCC/Clang
set_source_files_properties("../3rdParty-Core/miniz-3.1.1/miniz.c" PROPERTIES COMPILE_FLAGS "-w")
endif()

# REPO_ROOT_DIR: the root folder of the git repository, "Arduino-Source/"
set(PACKAGES_DIR "${REPO_ROOT_DIR}/Packages")
set(FIRMWARE_DIR "${PACKAGES_DIR}/Firmware")
Expand Down Expand Up @@ -871,3 +891,8 @@ if(COMMAND_LINE_EXE)
# Add command-line executable (GUI-free) from subdirectory
include(Source/CommandLine/CommandLineExecutable.cmake)
endif()

if(PA_PYTHON_BINDINGS)
# Python bindings (GUI-free) from subdirectory
include(Source/PythonBindings/PythonBindings.cmake)
endif()
2 changes: 1 addition & 1 deletion SerialPrograms/Source/CommandLine/CommandLine_Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ int main(int argc, char* argv[]){

for (int i = 0; i < 30; i++){
// Push A button: delay=0ms, hold=50ms, release=50ms, button=BUTTON_A
controller.push_button(0, 50, 50, static_cast<uint32_t>(BUTTON_A));
controller.push_button(static_cast<uint32_t>(BUTTON_A), 0, 50, 50);
}

// Wait for all button commands to complete
Expand Down
84 changes: 47 additions & 37 deletions SerialPrograms/Source/Integrations/PybindSwitchController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,25 @@ class PybindSwitchProControllerInternal final : public ControllerConnection::Sta
, m_descriptor(name)
, m_connection(m_descriptor.open_connection(m_logger))
{
m_connection->add_status_listener(*this);
// open_connection() returns nullptr for an empty port name.
if (m_connection){
m_connection->add_status_listener(*this);
}
}
~PybindSwitchProControllerInternal(){
m_connection->remove_status_listener(*this);
if (m_connection){
m_connection->remove_status_listener(*this);
// The connection's destructor joins the connection thread, which may still be
// inside post_connection_ready() and touching our members (m_controller,
// m_procon, m_lock, m_cv, ...). Destroy the connection now, before any of
// our own members are destroyed.
m_connection.reset();
Comment thread
jw098 marked this conversation as resolved.
Resolved
}
}

bool wait_for_ready(uint64_t timeout_millis){
bool wait_for_ready(uint64_t timeout_ms){
std::unique_lock<Mutex> lg(m_lock);
m_cv.wait_for(lg, Milliseconds(timeout_millis), [this]{
m_cv.wait_for(lg, Milliseconds(timeout_ms), [this]{
return m_connected;
});
return controller_if_ready() != nullptr;
Expand Down Expand Up @@ -85,9 +95,8 @@ class PybindSwitchProControllerInternal final : public ControllerConnection::Sta
ProController& controller(){
ProController* procon = controller_if_ready();
if (procon == nullptr){
throw InvalidConnectionStateException(
"Controller is not ready: " + m_connection->status_text()
);
std::string status = m_connection ? m_connection->status_text() : "No connection.";
throw InvalidConnectionStateException("Controller is not ready: " + status);
}
return *procon;
}
Expand Down Expand Up @@ -116,9 +125,9 @@ PybindSwitchProController::~PybindSwitchProController(){
delete (PybindSwitchProControllerInternal*)m_internals;
}

bool PybindSwitchProController::wait_for_ready(uint64_t timeout_millis){
bool PybindSwitchProController::wait_for_ready(uint64_t timeout_ms){
PybindSwitchProControllerInternal* internal = (PybindSwitchProControllerInternal*)m_internals;
return internal->wait_for_ready(timeout_millis);
return internal->wait_for_ready(timeout_ms);
}

bool PybindSwitchProController::is_ready() const{
Expand All @@ -127,7 +136,7 @@ bool PybindSwitchProController::is_ready() const{
}
std::string PybindSwitchProController::current_status() const{
PybindSwitchProControllerInternal* internal = (PybindSwitchProControllerInternal*)m_internals;
return internal->m_connection->raw_status_text();
return internal->m_connection ? internal->m_connection->raw_status_text() : "No connection.";
}
std::string PybindSwitchProController::controller_name() const{
PybindSwitchProControllerInternal* internal = (PybindSwitchProControllerInternal*)m_internals;
Expand All @@ -145,71 +154,72 @@ void PybindSwitchProController::wait_for_all_requests(){
}
controller->wait_for_all(nullptr);
}
bool PybindSwitchProController::cancel_all_and_wait(uint64_t timeout_millis){
bool PybindSwitchProController::cancel_all_and_wait(uint64_t timeout_ms){
PybindSwitchProControllerInternal* internal = (PybindSwitchProControllerInternal*)m_internals;
ProController* controller = internal->controller_if_ready();
if (controller == nullptr){
internal->m_logger.log("Controller is not ready.", COLOR_RED);
return false;
}
return controller->cancel_all_and_wait(Milliseconds(timeout_millis));
return controller->cancel_all_and_wait(Milliseconds(timeout_ms));
}
void PybindSwitchProController::wait(uint64_t duration){
void PybindSwitchProController::wait(uint64_t duration_ms){
PybindSwitchProControllerInternal* internal = (PybindSwitchProControllerInternal*)m_internals;
internal->controller().issue_nop(nullptr, Milliseconds(duration));
internal->controller().issue_nop(nullptr, Milliseconds(duration_ms));
}
void PybindSwitchProController::push_button(uint64_t delay, uint64_t hold, uint64_t release, uint32_t bitfield){
void PybindSwitchProController::push_button(uint32_t button_bitfield, uint64_t delay_ms, uint64_t hold_ms, uint64_t release_ms){
PybindSwitchProControllerInternal* internal = (PybindSwitchProControllerInternal*)m_internals;
internal->controller().issue_buttons(
nullptr,
Milliseconds(delay),
Milliseconds(hold),
Milliseconds(release),
(Button)bitfield
Milliseconds(delay_ms),
Milliseconds(hold_ms),
Milliseconds(release_ms),
(Button)button_bitfield
);
}
void PybindSwitchProController::push_dpad(uint64_t delay, uint64_t hold, uint64_t release, uint8_t position){
void PybindSwitchProController::push_dpad(uint8_t dpad_position, uint64_t delay_ms, uint64_t hold_ms, uint64_t release_ms){
PybindSwitchProControllerInternal* internal = (PybindSwitchProControllerInternal*)m_internals;
internal->controller().issue_dpad(
nullptr,
Milliseconds(delay),
Milliseconds(hold),
Milliseconds(release),
(DpadPosition)position
Milliseconds(delay_ms),
Milliseconds(hold_ms),
Milliseconds(release_ms),
(DpadPosition)dpad_position
);
}
void PybindSwitchProController::push_left_joystick(uint64_t delay, uint64_t hold, uint64_t release, double x, double y){
void PybindSwitchProController::push_left_joystick(double x, double y, uint64_t delay_ms, uint64_t hold_ms, uint64_t release_ms){
PybindSwitchProControllerInternal* internal = (PybindSwitchProControllerInternal*)m_internals;
internal->controller().issue_left_joystick(
nullptr,
Milliseconds(delay),
Milliseconds(hold),
Milliseconds(release),
{x, y}
Milliseconds(delay_ms),
Milliseconds(hold_ms),
Milliseconds(release_ms),
JoystickPosition(x, y)
);
}
void PybindSwitchProController::push_right_joystick(uint64_t delay, uint64_t hold, uint64_t release, double x, double y){
void PybindSwitchProController::push_right_joystick(double x, double y, uint64_t delay_ms, uint64_t hold_ms, uint64_t release_ms){
PybindSwitchProControllerInternal* internal = (PybindSwitchProControllerInternal*)m_internals;
internal->controller().issue_right_joystick(
nullptr,
Milliseconds(delay),
Milliseconds(hold),
Milliseconds(release),
{x, y}
Milliseconds(delay_ms),
Milliseconds(hold_ms),
Milliseconds(release_ms),
JoystickPosition(x, y)
);
}
void PybindSwitchProController::controller_state(
uint64_t duration,
uint32_t button_bitfield,
uint8_t dpad_position,
double left_x, double left_y,
double right_x, double right_y
double right_x, double right_y,
uint64_t duration_ms
){
PybindSwitchProControllerInternal* internal = (PybindSwitchProControllerInternal*)m_internals;
// Same as pbf_controller_state().
internal->controller().issue_full_controller_state(
nullptr,
true,
Milliseconds(duration),
Milliseconds(duration_ms),
(Button)button_bitfield,
(DpadPosition)dpad_position,
{left_x, left_y},
Expand Down
Loading
Loading