Skip to content

Commit

Permalink
Fix for composite report HIDs (better mouse compatibility).
Browse files Browse the repository at this point in the history
  • Loading branch information
Franticware committed Dec 3, 2023
1 parent 2187999 commit 42ff6b7
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 7 deletions.
12 changes: 12 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ cmake_minimum_required(VERSION 3.12)
# Pull in SDK (must be before project)
include(pico_sdk_import.cmake)

include(pico_extras_import_optional.cmake)

project(pico_examples C CXX ASM)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
Expand All @@ -16,5 +18,15 @@ set(PICO_EXAMPLES_PATH ${PROJECT_SOURCE_DIR})
# Initialize the SDK
pico_sdk_init()

include(example_auto_set_url.cmake)

add_compile_options(-Wall
-Wno-format # int != int32_t as far as the compiler is concerned because gcc has int32_t as long int
-Wno-unused-function # we have some for the docs that aren't called
)
if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
add_compile_options(-Wno-maybe-uninitialized)
endif()

# Hardware-specific examples in subdirectories:
add_subdirectory(usb)
5 changes: 5 additions & 0 deletions example_auto_set_url.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
set(PICO_EXAMPLE_URL_BASE "https://github.com/raspberrypi/pico-examples/tree/HEAD")
macro(example_auto_set_url TARGET)
file(RELATIVE_PATH URL_REL_PATH "${PICO_EXAMPLES_PATH}" "${CMAKE_CURRENT_LIST_DIR}")
pico_set_program_url(${TARGET} "${PICO_EXAMPLE_URL_BASE}/${URL_REL_PATH}")
endmacro()
59 changes: 59 additions & 0 deletions pico_extras_import_optional.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# This is a copy of <PICO_EXTRAS_PATH>/external/pico_extras_import.cmake

# This can be dropped into an external project to help locate pico-extras
# It should be include()ed prior to project()

if (DEFINED ENV{PICO_EXTRAS_PATH} AND (NOT PICO_EXTRAS_PATH))
set(PICO_EXTRAS_PATH $ENV{PICO_EXTRAS_PATH})
message("Using PICO_EXTRAS_PATH from environment ('${PICO_EXTRAS_PATH}')")
endif ()

if (DEFINED ENV{PICO_EXTRAS_FETCH_FROM_GIT} AND (NOT PICO_EXTRAS_FETCH_FROM_GIT))
set(PICO_EXTRAS_FETCH_FROM_GIT $ENV{PICO_EXTRAS_FETCH_FROM_GIT})
message("Using PICO_EXTRAS_FETCH_FROM_GIT from environment ('${PICO_EXTRAS_FETCH_FROM_GIT}')")
endif ()

if (DEFINED ENV{PICO_EXTRAS_FETCH_FROM_GIT_PATH} AND (NOT PICO_EXTRAS_FETCH_FROM_GIT_PATH))
set(PICO_EXTRAS_FETCH_FROM_GIT_PATH $ENV{PICO_EXTRAS_FETCH_FROM_GIT_PATH})
message("Using PICO_EXTRAS_FETCH_FROM_GIT_PATH from environment ('${PICO_EXTRAS_FETCH_FROM_GIT_PATH}')")
endif ()

if (NOT PICO_EXTRAS_PATH)
if (PICO_EXTRAS_FETCH_FROM_GIT)
include(FetchContent)
set(FETCHCONTENT_BASE_DIR_SAVE ${FETCHCONTENT_BASE_DIR})
if (PICO_EXTRAS_FETCH_FROM_GIT_PATH)
get_filename_component(FETCHCONTENT_BASE_DIR "${PICO_EXTRAS_FETCH_FROM_GIT_PATH}" REALPATH BASE_DIR "${CMAKE_SOURCE_DIR}")
endif ()
FetchContent_Declare(
pico_extras
GIT_REPOSITORY https://github.com/raspberrypi/pico-extras
GIT_TAG master
)
if (NOT pico_extras)
message("Downloading Raspberry Pi Pico Extras")
FetchContent_Populate(pico_extras)
set(PICO_EXTRAS_PATH ${pico_extras_SOURCE_DIR})
endif ()
set(FETCHCONTENT_BASE_DIR ${FETCHCONTENT_BASE_DIR_SAVE})
else ()
if (PICO_SDK_PATH AND EXISTS "${PICO_SDK_PATH}/../pico-extras")
set(PICO_EXTRAS_PATH ${PICO_SDK_PATH}/../pico-extras)
message("Defaulting PICO_EXTRAS_PATH as sibling of PICO_SDK_PATH: ${PICO_EXTRAS_PATH}")
endif()
endif ()
endif ()

if (PICO_EXTRAS_PATH)
set(PICO_EXTRAS_PATH "${PICO_EXTRAS_PATH}" CACHE PATH "Path to the PICO EXTRAS")
set(PICO_EXTRAS_FETCH_FROM_GIT "${PICO_EXTRAS_FETCH_FROM_GIT}" CACHE BOOL "Set to ON to fetch copy of PICO EXTRAS from git if not otherwise locatable")
set(PICO_EXTRAS_FETCH_FROM_GIT_PATH "${PICO_EXTRAS_FETCH_FROM_GIT_PATH}" CACHE FILEPATH "location to download EXTRAS")

get_filename_component(PICO_EXTRAS_PATH "${PICO_EXTRAS_PATH}" REALPATH BASE_DIR "${CMAKE_BINARY_DIR}")
if (NOT EXISTS ${PICO_EXTRAS_PATH})
message(FATAL_ERROR "Directory '${PICO_EXTRAS_PATH}' not found")
endif ()

set(PICO_EXTRAS_PATH ${PICO_EXTRAS_PATH} CACHE PATH "Path to the PICO EXTRAS" FORCE)
add_subdirectory(${PICO_EXTRAS_PATH} pico_extras)
endif()
2 changes: 1 addition & 1 deletion usb/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
if (TARGET tinyusb_host)
add_subdirectory(host)
else ()
message("Cannot build, TinyUSB unavailable")
message("Skipping TinyUSB host examples as TinyUSB is unavailable")
endif ()
2 changes: 2 additions & 0 deletions usb/host/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
set(FAMILY rp2040)
set(BOARD pico_sdk)
set(TINYUSB_FAMILY_PROJECT_NAME_PREFIX "tinyusb_host_")
# Hack as some host examples use $TOP in their path
set(TOP ${PICO_TINYUSB_PATH})
add_subdirectory(${PICO_TINYUSB_PATH}/examples/host tinyusb_host_examples)

add_subdirectory(usb_to_ps1_mouse)
6 changes: 6 additions & 0 deletions usb/host/usb_to_ps1_mouse/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,9 @@ target_include_directories(usb_to_ps1_mouse PUBLIC
target_link_libraries(usb_to_ps1_mouse PUBLIC pico_stdlib tinyusb_host tinyusb_board pico_multicore)

pico_add_extra_outputs(usb_to_ps1_mouse)

pico_enable_stdio_usb(usb_to_ps1_mouse 0)
pico_enable_stdio_uart(usb_to_ps1_mouse 1)

# add url via pico_set_program_url
example_auto_set_url(usb_to_ps1_mouse)
28 changes: 22 additions & 6 deletions usb/host/usb_to_ps1_mouse/hid_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ static struct
}hid_info[CFG_TUH_HID];

static void process_kbd_report(hid_keyboard_report_t const *report);
static void process_mouse_report(hid_mouse_report_t const * report);
static void process_mouse_report(uint8_t const* report, uint16_t len);
static void process_generic_report(uint8_t dev_addr, uint8_t instance, uint8_t const* report, uint16_t len);

void hid_app_task(void)
Expand Down Expand Up @@ -81,17 +81,28 @@ void tuh_hid_umount_cb(uint8_t dev_addr, uint8_t instance)
void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* report, uint16_t len)
{
uint8_t const itf_protocol = tuh_hid_interface_protocol(dev_addr, instance);
uint8_t const rpt_count = tuh_hid_instance_count(dev_addr);

uint8_t const* reportAdj = report;
uint16_t lenAdj = len;

if ( rpt_count != 1 )
{
// Composite report, 1st byte is report ID, data starts from 2nd byte
reportAdj++;
lenAdj--;
}

switch (itf_protocol)
{
case HID_ITF_PROTOCOL_KEYBOARD:
TU_LOG2("HID receive boot keyboard report\r\n");
process_kbd_report( (hid_keyboard_report_t const*) report );
process_kbd_report( (hid_keyboard_report_t const*) reportAdj );
break;

case HID_ITF_PROTOCOL_MOUSE:
TU_LOG2("HID receive boot mouse report\r\n");
process_mouse_report( (hid_mouse_report_t const*) report );
process_mouse_report(reportAdj, lenAdj);
break;

default:
Expand Down Expand Up @@ -155,9 +166,14 @@ static void process_kbd_report(hid_keyboard_report_t const *report)
// Mouse
//--------------------------------------------------------------------+

static void process_mouse_report(hid_mouse_report_t const * report)
static void process_mouse_report(uint8_t const* report, uint16_t len)
{
int8_t arr[4] = {report->buttons, report->x, report->y, report->wheel};
int8_t arr[4] = {0};
for (uint16_t i = 0; i != len; ++i)
{
arr[i] = report[i];
}
//printf("%d %d %d %d\r\n", (int)report[0], (int)report[1], (int)report[2], (int)report[3]); fflush(stdout);
uint32_t mouseData = 0;
memcpy(&mouseData, arr, 4);
multicore_fifo_push_blocking(mouseData);
Expand Down Expand Up @@ -223,7 +239,7 @@ static void process_generic_report(uint8_t dev_addr, uint8_t instance, uint8_t c
case HID_USAGE_DESKTOP_MOUSE:
TU_LOG1("HID receive mouse report\r\n");
// Assume mouse follow boot report layout
process_mouse_report( (hid_mouse_report_t const*) report );
process_mouse_report(report, len);
break;

default: break;
Expand Down
Binary file removed usb_to_ps1_mouse.uf2
Binary file not shown.

0 comments on commit 42ff6b7

Please sign in to comment.