Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved library integration and extensibility #46

Merged
merged 7 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
9 changes: 8 additions & 1 deletion src/components/bluepad32/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ idf_component_register(SRCS "${srcs}"
INCLUDE_DIRS "include"
REQUIRES ${requires})

# adding uni_platform_custom_create to the undefined symbol list, as it
# needs to be provided by the user externally to this component
set(ext_symbols "-u uni_platform_custom_create")

# See: https://gitlab.com/ricardoquesada/bluepad32/-/issues/9
if(DEFINED ENV{BLUEPAD32_ARDUINO})
# Bluepad32 contains reference to setup() and loop(). If we add main
Expand All @@ -89,5 +93,8 @@ if(DEFINED ENV{BLUEPAD32_ARDUINO})
# linker will always include them.
#
# (As they are C++ symbol, we need to add the C++ mangled names.)
target_link_libraries(${COMPONENT_LIB} INTERFACE "-u _Z6setupv -u _Z4loopv")
list(APPEND ext_symbols "-u _Z6setupv -u _Z4loopv")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: indentation

endif()

string(REPLACE ";" " " ext_symbols "${ext_symbols}")
target_link_libraries(${COMPONENT_LIB} INTERFACE ${ext_symbols})
19 changes: 19 additions & 0 deletions src/components/bluepad32/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ menu "Bluepad32"
help
Targets Unijoysticle 2 / Unijoysticle 2+ devices.

config BLUEPAD32_PLATFORM_CUSTOM
bool "Custom"
help
Custom platform, provided by the user/vendor

config BLUEPAD32_PLATFORM_MAKEFILE
bool "Decided by Makefile"
help
Expand Down Expand Up @@ -76,6 +81,20 @@ menu "Bluepad32"
Highly recommended for debugging purposes.
But must be disabled on AirLift / NINA platforms.

config UNI_LOG_ERROR
int "Enable error-level logging"
default 1

config UNI_LOG_INFO
int "Enable info-level logging"
default 1

config UNI_LOG_DEBUG
int "Enable debug-level logging"
default 0
help
It is safe to leave UNI_LOG_DEBUG disabled (unless you are a developer).

config BLUEPAD32_USB_CONSOLE_ENABLE
bool "Enable USB Console"
default y
Expand Down
6 changes: 0 additions & 6 deletions src/components/bluepad32/include/uni_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,4 @@ limitations under the License.
// For more configurations, please look at the Kconfig file, or just do:
// "idf.py menuconfig" -> "Component config" -> "Bluepad32"

// How verbose the logging should be.
// It is safe to leave UNI_LOG_DEBUG disabled (unless you are a developer).
#define UNI_LOG_ERROR 1
#define UNI_LOG_INFO 1
#define UNI_LOG_DEBUG 0

#endif // UNI_CONFIG_H
6 changes: 3 additions & 3 deletions src/components/bluepad32/include/uni_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ void uni_logv(const char* fmt, va_list args);

#define loge(fmt, ...) \
do { \
if (UNI_LOG_ERROR) \
if (CONFIG_UNI_LOG_ERROR) \
uni_log(fmt, ##__VA_ARGS__); \
} while (0)

#define logi(fmt, ...) \
do { \
if (UNI_LOG_INFO) \
if (CONFIG_UNI_LOG_INFO) \
uni_log(fmt, ##__VA_ARGS__); \
} while (0)

#define logd(fmt, ...) \
do { \
if (UNI_LOG_DEBUG) \
if (CONFIG_UNI_LOG_DEBUG) \
uni_log(fmt, ##__VA_ARGS__); \
} while (0)

Expand Down
24 changes: 24 additions & 0 deletions src/components/bluepad32/include/uni_platform_custom.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/****************************************************************************
http://retro.moe/unijoysticle2

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
****************************************************************************/

#ifndef UNI_PLATFORM_CUSTOM_H
#define UNI_PLATFORM_CUSTOM_H

#include "uni_platform.h"

extern struct uni_platform* uni_platform_custom_create(void);

#endif // UNI_PLATFORM_CUSTOM_H
13 changes: 11 additions & 2 deletions src/components/bluepad32/uni_platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ limitations under the License.
#include "uni_platform_mightymiggy.h"
#include "uni_platform_nina.h"
#include "uni_platform_pc_debug.h"
#include "uni_platform_custom.h"

#ifdef CONFIG_BLUEPAD32_PLATFORM_UNIJOYSTICLE
#include "uni_platform_unijoysticle.h"
Expand All @@ -33,8 +34,14 @@ limitations under the License.
static struct uni_platform* _platform;

void uni_platform_init(int argc, const char** argv) {
// Each vendor must create its own. These CONFIG_BLUEPAD32_PLATFORM_ defines
// are defined in the Makefile and Kconfig files.
// These CONFIG_BLUEPAD32_PLATFORM_ defines are defined in the Makefile
// and Kconfig files.

// Premade platforms are available as part of this library. Vendors/users
// may create a "custom" platform by providing an implementation of
// uni_platform_custom_create() within a project file (for instance
// uni_platform_custom.c) and select the CONFIG_BLUEPAD32_PLATFORM_CUSTOM
// from the Makefile or the Kconfig file

#ifdef CONFIG_BLUEPAD32_PLATFORM_UNIJOYSTICLE
_platform = uni_platform_unijoysticle_create();
Expand All @@ -48,6 +55,8 @@ void uni_platform_init(int argc, const char** argv) {
_platform = uni_platform_nina_create();
#elif defined(CONFIG_BLUEPAD32_PLATFORM_ARDUINO)
_platform = uni_platform_arduino_create();
#elif defined(CONFIG_BLUEPAD32_PLATFORM_CUSTOM)
_platform = uni_platform_custom_create();
#else
#error "Platform not defined. Set PLATFORM environment variable"
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
set(srcs "main.c")
set(srcs "uni_platform_custom.c" "main.c")

set(requires "bluepad32")

Expand Down
218 changes: 218 additions & 0 deletions src/main/uni_platform_custom.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
/****************************************************************************
http://retro.moe/unijoysticle2

Copyright 2019 Ricardo Quesada

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
****************************************************************************/

// This example platform can be used as a starting point to create a new custom
// vendor/user/project-specific platform

#include "uni_platform_custom.h"

#include <stdio.h>
#include <string.h>

#include "uni_bt.h"
#include "uni_gamepad.h"
#include "uni_hid_device.h"
#include "uni_log.h"

//
// Globals
//
static int g_delete_keys = 0;

// PC Debug "instance"
typedef struct custom_instance_s {
uni_gamepad_seat_t gamepad_seat; // which "seat" is being used
} custom_instance_t;

// Declarations
static void trigger_event_on_gamepad(uni_hid_device_t* d);
static custom_instance_t* get_custom_instance(uni_hid_device_t* d);

//
// Platform Overrides
//
static void custom_init(int argc, const char** argv) {

ARG_UNUSED(argc);
ARG_UNUSED(argv);

logi("custom: init()\n");

#if 0
uni_gamepad_mappings_t mappings = GAMEPAD_DEFAULT_MAPPINGS;

// Inverted axis with inverted Y in RY.
mappings.axis_x = UNI_GAMEPAD_MAPPINGS_AXIS_RX;
mappings.axis_y = UNI_GAMEPAD_MAPPINGS_AXIS_RY;
mappings.axis_ry_inverted = true;
mappings.axis_rx = UNI_GAMEPAD_MAPPINGS_AXIS_X;
mappings.axis_ry = UNI_GAMEPAD_MAPPINGS_AXIS_Y;

// Invert A & B
mappings.button_a = UNI_GAMEPAD_MAPPINGS_BUTTON_B;
mappings.button_b = UNI_GAMEPAD_MAPPINGS_BUTTON_A;

uni_gamepad_set_mappings(&mappings);
#endif
}

static void custom_on_init_complete(void) {
logi("custom: on_init_complete()\n");
}

static void custom_on_device_connected(uni_hid_device_t* d) {
logi("custom: device connected: %p\n", d);
}

static void custom_on_device_disconnected(uni_hid_device_t* d) {
logi("custom: device disconnected: %p\n", d);
}

static uni_error_t custom_on_device_ready(uni_hid_device_t* d) {
logi("custom: device ready: %p\n", d);
custom_instance_t* ins = get_custom_instance(d);
ins->gamepad_seat = GAMEPAD_SEAT_A;

trigger_event_on_gamepad(d);
return UNI_ERROR_SUCCESS;
}

static void custom_on_controller_data(uni_hid_device_t* d, uni_controller_t* ctl) {
static uint8_t leds = 0;
static uint8_t enabled = true;
static uni_controller_t prev = {0};
uni_gamepad_t* gp;

if (memcmp(&prev, ctl, sizeof(*ctl)) == 0) {
return;
}
prev = *ctl;
// Print device Id before dumping gamepad.
logi("(%p) ", d);
uni_controller_dump(ctl);

switch (ctl->klass) {
case UNI_CONTROLLER_CLASS_GAMEPAD:
gp = &ctl->gamepad;

// Debugging
// Axis ry: control rumble
if ((gp->buttons & BUTTON_A) && d->report_parser.set_rumble != NULL) {
d->report_parser.set_rumble(d, 128, 128);
}
// Buttons: Control LEDs On/Off
if ((gp->buttons & BUTTON_B) && d->report_parser.set_player_leds != NULL) {
d->report_parser.set_player_leds(d, leds++ & 0x0f);
}
// Axis: control RGB color
if ((gp->buttons & BUTTON_X) && d->report_parser.set_lightbar_color != NULL) {
uint8_t r = (gp->axis_x * 256) / 512;
uint8_t g = (gp->axis_y * 256) / 512;
uint8_t b = (gp->axis_rx * 256) / 512;
d->report_parser.set_lightbar_color(d, r, g, b);
}

// Toggle Bluetooth connections
if ((gp->buttons & BUTTON_SHOULDER_L) && enabled) {
logi("*** Disabling Bluetooth connections\n");
uni_bt_enable_new_connections_safe(false);
enabled = false;
}
if ((gp->buttons & BUTTON_SHOULDER_R) && !enabled) {
logi("*** Enabling Bluetooth connections\n");
uni_bt_enable_new_connections_safe(true);
enabled = true;
}
break;
default:
break;
}
}

static int32_t custom_get_property(uni_platform_property_t key) {
logi("custom: get_property(): %d\n", key);
if (key != UNI_PLATFORM_PROPERTY_DELETE_STORED_KEYS)
return -1;
return g_delete_keys;
}

static void custom_on_oob_event(uni_platform_oob_event_t event, void* data) {
logi("custom: on_device_oob_event(): %d\n", event);

if (event != UNI_PLATFORM_OOB_GAMEPAD_SYSTEM_BUTTON) {
logi("custom_on_device_gamepad_event: unsupported event: 0x%04x\n", event);
return;
}

uni_hid_device_t* d = data;

if (d == NULL) {
loge("ERROR: custom_on_device_gamepad_event: Invalid NULL device\n");
return;
}

custom_instance_t* ins = get_custom_instance(d);
ins->gamepad_seat = ins->gamepad_seat == GAMEPAD_SEAT_A ? GAMEPAD_SEAT_B : GAMEPAD_SEAT_A;

trigger_event_on_gamepad(d);
}

//
// Helpers
//
static custom_instance_t* get_custom_instance(uni_hid_device_t* d) {
return (custom_instance_t*)&d->platform_data[0];
}

static void trigger_event_on_gamepad(uni_hid_device_t* d) {
custom_instance_t* ins = get_custom_instance(d);

if (d->report_parser.set_rumble != NULL) {
d->report_parser.set_rumble(d, 0x80 /* value */, 15 /* duration */);
}

if (d->report_parser.set_player_leds != NULL) {
d->report_parser.set_player_leds(d, ins->gamepad_seat);
}

if (d->report_parser.set_lightbar_color != NULL) {
uint8_t red = (ins->gamepad_seat & 0x01) ? 0xff : 0;
uint8_t green = (ins->gamepad_seat & 0x02) ? 0xff : 0;
uint8_t blue = (ins->gamepad_seat & 0x04) ? 0xff : 0;
d->report_parser.set_lightbar_color(d, red, green, blue);
}
}

//
// Entry Point
//
struct uni_platform* uni_platform_custom_create(void) {
static struct uni_platform plat = {
.name = "custom",
.init = custom_init,
.on_init_complete = custom_on_init_complete,
.on_device_connected = custom_on_device_connected,
.on_device_disconnected = custom_on_device_disconnected,
.on_device_ready = custom_on_device_ready,
.on_oob_event = custom_on_oob_event,
.on_controller_data = custom_on_controller_data,
.get_property = custom_get_property,
};

return &plat;
}
Loading