Skip to content

Commit

Permalink
changes in how main() works and custom-platform setup
Browse files Browse the repository at this point in the history
- `uni_esp32_main` removed
- ESP32 `main` does not call `uni_esp32_main`, instead it calls the
  different "init" steps manually
- custom platform setup must call "uni_platform_set_custom" before
  "uni_main"
  • Loading branch information
ricardoquesada committed Nov 11, 2023
1 parent cb000a2 commit 4cb3d94
Show file tree
Hide file tree
Showing 13 changed files with 87 additions and 46 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `uni_main()` does not call `btstack_run_loop_execute()` automatically. Must be called by the user
- BT scan is OFF by default. Platform must call `uni_bt_enable_new_connections` to enable it.
- `platform->get_properties()` does nothing. To list/delete Bluetooth keys platform must call them explicitly.
- Custom platform:
- must call `uni_platform_set_custom(...)` before calling `uni_main()`
- `uni_platform_custom_create()` removed
- ESP32 main:
- `uni_esp32_main()` removed.
- User must call the different "init" steps manully. Added helper `uni_esp32_init()`
- Rationale: Pico W and ESP32 `main()` is almost the same. Easier to setup/customize/understand.
- All examples updated
- New "unsafe" functions, useful to be called from `platforom->on_init_complete()`
- `uni_bt_enable_new_connections_unsafe()`
- `uni_bt_del_keys_unsafe()`
Expand Down
4 changes: 0 additions & 4 deletions src/components/bluepad32/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,6 @@ else()
message(FATAL_ERROR "Define target")
endif()

# 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 Down
6 changes: 4 additions & 2 deletions src/components/bluepad32/include/uni_esp32.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ limitations under the License.
#ifndef UNI_ESP32_H
#define UNI_ESP32_H

int uni_esp32_main(void);
void uni_esp32_enable_uart_output(int enabled);
#include <stdbool.h>

void uni_esp32_init(void);
void uni_esp32_enable_uart_output(bool enabled);

#endif // UNI_ESP32_H
2 changes: 1 addition & 1 deletion src/components/bluepad32/include/uni_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ struct uni_platform {
void uni_platform_init(int argc, const char** argv);

struct uni_platform* uni_get_platform(void);
void uni_set_platform(struct uni_platform* platform);
void uni_platform_set_custom(struct uni_platform* platform);

#ifdef __cplusplus
}
Expand Down
40 changes: 12 additions & 28 deletions src/components/bluepad32/uni_esp32.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,39 +42,12 @@ limitations under the License.
_Static_assert(CONFIG_BTDM_CTRL_BR_EDR_MAX_ACL_CONN >= 2, "Max ACL must be >= 2");
#endif // UNI_ENABLE_BREDR

// This function should be called from "app_main"
int uni_esp32_main(void) {
// hci_dump_open(NULL, HCI_DUMP_STDOUT);

#ifdef CONFIG_BLUEPAD32_UART_OUTPUT_ENABLE
uni_esp32_enable_uart_output(1);
#else
// Adafruit Airlift modules have the UART RX/TX (GPIO 1 / 3) wired with the
// controller so they can't be used for logging. In fact they can generate
// noise and can break the communication with the controller.
uni_esp32_enable_uart_output(0);
#endif

// Configure BTstack for ESP32 VHCI Controller
btstack_init();

// hci_dump_init(hci_dump_embedded_stdout_get_instance());

// Init Bluepad32.
uni_main(0 /* argc */, NULL /* argv */);

// Does not return.
btstack_run_loop_execute();

return 0;
}

// Code taken from nina-fw
// https://github.com/adafruit/nina-fw/blob/master/main/sketch.ino.cpp
//
// Bluepad32 is compiled with UART RX/TX disabled by default.
// But can be enabled/disabled in runtime by calling this function.
void uni_esp32_enable_uart_output(int enabled) {
void uni_esp32_enable_uart_output(bool enabled) {
#ifdef CONFIG_IDF_TARGET_ESP32
if (enabled) {
PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[1], 0);
Expand Down Expand Up @@ -103,3 +76,14 @@ void uni_esp32_enable_uart_output(int enabled) {
}
#endif // CONFIG_IDF_TARGET_ESP32
}

void uni_esp32_init(void) {
#ifdef CONFIG_BLUEPAD32_UART_OUTPUT_ENABLE
uni_esp32_enable_uart_output(1);
#else
// Adafruit Airlift modules have the UART RX/TX (GPIO 1 / 3) wired with the
// controller so they can't be used for logging. In fact they can generate
// noise and can break the communication with the controller.
uni_esp32_enable_uart_output(0);
#endif
}
6 changes: 6 additions & 0 deletions src/components/bluepad32/uni_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ limitations under the License.
#include "uni_bt_setup.h"
#include "uni_config.h"
#include "uni_console.h"
#include "uni_esp32.h"
#include "uni_hid_device.h"
#include "uni_log.h"
#include "uni_platform.h"
Expand All @@ -32,6 +33,11 @@ limitations under the License.

// Main entry point, runs forever
int uni_main(int argc, const char** argv) {

#ifdef CONFIG_IDF_TARGET
uni_esp32_init();
#endif

// Disable stdout buffering
setbuf(stdout, NULL);

Expand Down
18 changes: 12 additions & 6 deletions src/components/bluepad32/uni_platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ void uni_platform_init(int argc, const char** argv) {
// 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
// "struct uni_platform" and calling "uni_platform_set_custom".

#ifdef CONFIG_BLUEPAD32_PLATFORM_UNIJOYSTICLE
_platform = uni_platform_unijoysticle_create();
Expand All @@ -52,9 +50,12 @@ void uni_platform_init(int argc, const char** argv) {
#elif defined(CONFIG_BLUEPAD32_PLATFORM_ARDUINO)
_platform = uni_platform_arduino_create();
#elif defined(CONFIG_BLUEPAD32_PLATFORM_CUSTOM)
_platform = uni_platform_custom_create();
if (!_platform) {
while (1)
loge("Error: call uni_platform_set_custom() before calling uni_main\n");
}
#else
#error "Platform not defined. Set PLATFORM environment variable"
#error "Platform not defined. Set CONFIG_BLUEPAD32_PLATFORM"
#endif

logi("Platform: %s\n", _platform->name);
Expand All @@ -65,6 +66,11 @@ struct uni_platform* uni_get_platform(void) {
return _platform;
}

void uni_set_platform(struct uni_platform* platform) {
void uni_platform_set_custom(struct uni_platform* platform) {
#ifdef CONFIG_BLUEPAD32_PLATFORM_CUSTOM
_platform = platform;
#else
while (1)
loge("Error: uni_platform_set_custom SHOULD only be called on 'custom' platform\n");
#endif
}
30 changes: 28 additions & 2 deletions src/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,43 @@ 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.
****************************************************************************/
#include <stdlib.h>

#include <btstack_port_esp32.h>
#include <btstack_run_loop.h>

#include "sdkconfig.h"
#include "uni_main.h"
#include "uni_platform.h"

#ifndef CONFIG_BLUEPAD32_PLATFORM_ARDUINO

// Not needed in Arduino platform since it has "autostart" code defined in
// uni_platform_arduino.

#include "uni_esp32.h"
// Defined in my_platform.c
struct uni_platform* get_my_platform(void);

int app_main(void) {
return uni_esp32_main();
// hci_dump_open(NULL, HCI_DUMP_STDOUT);

// Configure BTstack for ESP32 VHCI Controller
btstack_init();

// hci_dump_init(hci_dump_embedded_stdout_get_instance());

#ifdef CONFIG_BLUEPAD32_PLATFORM_CUSTOM
// Must be called before uni_main()
uni_platform_set_custom(get_my_platform());
#endif // CONFIG_BLUEPAD32_PLATFORM_CUSTOM

// Init Bluepad32.
uni_main(0 /* argc */, NULL /* argv */);

// Does not return.
btstack_run_loop_execute();

return 0;
}

#endif // CONFIG_BLUEPAD32_PLATFORM_ARDUINO
2 changes: 1 addition & 1 deletion src/main/my_platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ static void trigger_event_on_gamepad(uni_hid_device_t* d) {
//
// Entry Point
//
struct uni_platform* my_platform_create(void) {
struct uni_platform* get_my_platform(void) {
static struct uni_platform plat = {
.name = "custom",
.init = my_platform_init,
Expand Down
6 changes: 6 additions & 0 deletions tools/pc_debug/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@
#include "hci_transport_usb.h"
#include "sdkconfig.h"
#include "uni_main.h"
#include "uni_platform.h"

#include "my_platform.h"

// Sanity check
#ifndef CONFIG_BLUEPAD32_PLATFORM_CUSTOM
Expand Down Expand Up @@ -239,6 +242,9 @@ int main(int argc, const char* argv[]) {
// handle CTRL-c
signal(SIGINT, sigint_handler);

// Must be called before uni_main()
uni_platform_set_custom(get_my_platform());

// Initialize Bluepad32.
uni_main(argc, argv);

Expand Down
2 changes: 1 addition & 1 deletion tools/pc_debug/my_platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ static void trigger_event_on_gamepad(uni_hid_device_t* d) {
//
// Entry Point
//
struct uni_platform* uni_platform_custom_create(void) {
struct uni_platform* get_my_platform(void) {
static struct uni_platform plat = {
.name = "PC Debug",
.init = pc_debug_init,
Expand Down
7 changes: 7 additions & 0 deletions tools/pico_w/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,16 @@ limitations under the License.
#include "sdkconfig.h"
#include "uni_log.h"
#include "uni_main.h"
#include "uni_platform.h"

// Sanity check
#ifndef CONFIG_BLUEPAD32_PLATFORM_CUSTOM
#error "Pico W must use BLUEPAD32_PLATFORM_CUSTOM"
#endif

// Defined in my_platform.c
struct uni_platform* get_my_platform(void);

int main() {
stdio_init_all();

Expand All @@ -41,6 +45,9 @@ int main() {
// Turn-on LED. Turn it off once init is done.
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1);

// Must be called before uni_main()
uni_platform_set_custom(get_my_platform());

// Initialize BP32
uni_main(0, NULL);

Expand Down
2 changes: 1 addition & 1 deletion tools/pico_w/my_platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ static void trigger_event_on_gamepad(uni_hid_device_t* d) {
//
// Entry Point
//
struct uni_platform* uni_platform_custom_create(void) {
struct uni_platform* get_my_platform(void) {
static struct uni_platform plat = {
.name = "My Platform",
.init = my_platform_init,
Expand Down

0 comments on commit 4cb3d94

Please sign in to comment.