Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "lib/Pico-PIO-USB"]
path = lib/Pico-PIO-USB
url = https://github.com/sekigon-gonnoc/Pico-PIO-USB.git
99 changes: 12 additions & 87 deletions PIOKMbox.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,30 +39,6 @@
#include "tusb.h"
#endif

//--------------------------------------------------------------------+
// Type Definitions and Structures
//--------------------------------------------------------------------+

typedef struct {
uint32_t watchdog_status_timer;
uint32_t last_button_press_time;
bool button_pressed_last;
bool usb_reset_cooldown;
uint32_t usb_reset_cooldown_start;
} main_loop_state_t;

// Remove unused structures
//typedef struct {
// bool button_pressed;
// uint32_t current_time;
// uint32_t hold_duration;
//} button_state_t;

//--------------------------------------------------------------------+
// Constants and Configuration
//--------------------------------------------------------------------+
static const uint32_t WATCHDOG_STATUS_INTERVAL_MS = WATCHDOG_STATUS_REPORT_INTERVAL_MS;

//--------------------------------------------------------------------+
// Global state for flash operation coordination
//--------------------------------------------------------------------+
Expand All @@ -85,8 +61,6 @@ static void main_application_loop(void);
// Button handling functions
static void process_button_input(system_state_t* state, uint32_t current_time);

// Reporting functions
static void report_watchdog_status(uint32_t current_time, uint32_t* watchdog_status_timer);

// Utility functions
static inline bool is_time_elapsed(uint32_t current_time, uint32_t last_time, uint32_t interval);
Expand All @@ -96,26 +70,6 @@ static inline bool is_time_elapsed(uint32_t current_time, uint32_t last_time, ui
//--------------------------------------------------------------------+

#if PIO_USB_AVAILABLE
// Separate initialization concerns into focused functions

typedef enum {
INIT_SUCCESS,
INIT_FAILURE,
INIT_RETRY_NEEDED
} init_result_t;

typedef struct {
int attempt;
int max_attempts;
uint32_t base_delay_ms;
uint32_t last_heartbeat_time;
} init_context_t;

typedef struct {
uint32_t last_heartbeat_ms;
uint32_t heartbeat_counter;
} core1_state_t;


static void core1_main(void) {
// Small delay to let core0 stabilize
Expand Down Expand Up @@ -245,8 +199,6 @@ static bool initialize_usb_device(void) {
return device_init_success;
}



//--------------------------------------------------------------------+
// Button Handling Functions
//--------------------------------------------------------------------+
Expand All @@ -271,11 +223,11 @@ static void process_button_input(system_state_t* state, uint32_t current_time) {
state->last_button_press_time = current_time;
} else {
// Button being held - check for reset trigger
if (is_time_elapsed(current_time, state->last_button_press_time, BUTTON_HOLD_TRIGGER_MS)) {
usb_stacks_reset();
state->usb_reset_cooldown = true;
state->usb_reset_cooldown_start = current_time;
}
if (is_time_elapsed(current_time, state->last_button_press_time, BUTTON_HOLD_TRIGGER_MS)) {
usb_stacks_reset();
state->usb_reset_cooldown = true;
state->usb_reset_cooldown_start = current_time;
}
}
} else if (state->button_pressed_last) {
// Button just released - check if it was a short press
Expand All @@ -291,22 +243,10 @@ static void process_button_input(system_state_t* state, uint32_t current_time) {
// Show mode with LED flash
uint32_t mode_color;
switch (new_mode) {
case HUMANIZATION_OFF:
mode_color = COLOR_HUMANIZATION_OFF;

break;
case HUMANIZATION_MICRO:
mode_color = COLOR_HUMANIZATION_MICRO;

break;
case HUMANIZATION_FULL:
mode_color = COLOR_HUMANIZATION_FULL;

break;
default:
mode_color = COLOR_ERROR;

break;
case HUMANIZATION_OFF: mode_color = COLOR_HUMANIZATION_OFF; break;
case HUMANIZATION_MICRO: mode_color = COLOR_HUMANIZATION_MICRO; break;
case HUMANIZATION_FULL: mode_color = COLOR_HUMANIZATION_FULL; break;
default: mode_color = COLOR_ERROR; break;
}

neopixel_set_color(mode_color);
Expand All @@ -317,20 +257,6 @@ static void process_button_input(system_state_t* state, uint32_t current_time) {
state->button_pressed_last = button_currently_pressed;
}

//--------------------------------------------------------------------+
// Reporting Functions
//--------------------------------------------------------------------+

static void report_watchdog_status(uint32_t current_time, uint32_t* watchdog_status_timer) {
if (!is_time_elapsed(current_time, *watchdog_status_timer, WATCHDOG_STATUS_INTERVAL_MS)) {
return;
}

*watchdog_status_timer = current_time;


}

//--------------------------------------------------------------------+
// Utility Functions
//--------------------------------------------------------------------+
Expand All @@ -343,7 +269,6 @@ static __force_inline bool is_time_elapsed(uint32_t current_time, uint32_t last_
// Main Application Loop
//--------------------------------------------------------------------+


static void main_application_loop(void) {
system_state_t* state = get_system_state();
system_state_init(state);
Expand All @@ -353,7 +278,7 @@ static void main_application_loop(void) {
const uint32_t visual_interval = VISUAL_TASK_INTERVAL_MS;
const uint32_t error_interval = ERROR_CHECK_INTERVAL_MS;
const uint32_t button_interval = BUTTON_DEBOUNCE_MS;
const uint32_t status_report_interval = WATCHDOG_STATUS_REPORT_INTERVAL_MS;
const uint32_t status_interval = WATCHDOG_STATUS_REPORT_INTERVAL_MS;

// Performance optimization: reduce time sampling frequency
uint32_t current_time = to_ms_since_boot(get_absolute_time());
Expand Down Expand Up @@ -414,7 +339,7 @@ static void main_application_loop(void) {
if ((current_time - state->last_button_time) >= button_interval) {
task_flags |= BUTTON_FLAG;
}
if ((current_time - state->watchdog_status_timer) >= status_report_interval) {
if ((current_time - state->watchdog_status_timer) >= status_interval) {
task_flags |= STATUS_FLAG;
}

Expand Down Expand Up @@ -448,7 +373,7 @@ static void main_application_loop(void) {
}

if (task_flags & STATUS_FLAG) {
report_watchdog_status(current_time, &state->watchdog_status_timer);
state->watchdog_status_timer = current_time;
// Send Xbox console mode status to bridge for TFT display
if (g_xbox_mode) {
kmbox_send_xbox_status_to_bridge();
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,12 @@ This allows bidirectional communication for KMBox commands and display data.
### 1. Clone and Build

```bash
git clone https://github.com/ramseymcgrath/RaspberryKMBox.git
git clone --recursive https://github.com/ramseymcgrath/RaspberryKMBox.git
cd RaspberryKMBox

# If you already cloned without --recursive:
git submodule update --init --recursive

# Build options:
./build.sh metro # Main KMBox for Metro RP2350
./build.sh bridge-metro # Bridge for Metro RP2350 with display
Expand Down
10 changes: 8 additions & 2 deletions bridge/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ set(PICO_PLATFORM rp2350-arm-s)
# Custom board headers (for adafruit_metro_rp2350)
set(PICO_BOARD_HEADER_DIRS ${CMAKE_CURRENT_LIST_DIR}/../boards)

# Import Pico SDK
set(PICO_SDK_PATH $ENV{HOME}/.pico-sdk/sdk/2.2.0-fresh)
# Import Pico SDK — prefer cmake -D, then env var, then default local path
if(NOT DEFINED PICO_SDK_PATH)
if(DEFINED ENV{PICO_SDK_PATH})
set(PICO_SDK_PATH $ENV{PICO_SDK_PATH})
else()
set(PICO_SDK_PATH $ENV{HOME}/.pico-sdk/sdk/2.2.0-fresh)
endif()
endif()
include(${PICO_SDK_PATH}/pico_sdk_init.cmake)

project(kmbox_bridge C CXX ASM)
Expand Down
6 changes: 6 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,12 @@ if [ -z "$TARGET" ]; then
TARGET="flash-metros"
fi

# Ensure submodules are initialized
if [ ! -f "$SCRIPT_DIR/lib/Pico-PIO-USB/CMakeLists.txt" ]; then
echo -e "${YELLOW}Initializing git submodules...${NC}"
git -C "$SCRIPT_DIR" submodule update --init --recursive
fi

# Execute build
cd "$SCRIPT_DIR"

Expand Down
25 changes: 6 additions & 19 deletions defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -412,10 +412,7 @@ typedef struct __attribute__((packed)) {

// Humanization mode colors (for button mode switching)
#define COLOR_HUMANIZATION_OFF 0xFF0000 // Red - no humanization
#define COLOR_HUMANIZATION_LOW 0xFFAA00 // Orange - low humanization (deprecated, use MICRO)
#define COLOR_HUMANIZATION_MICRO 0xFFFF00 // Yellow - micro-noise only (pre-humanized input)
#define COLOR_HUMANIZATION_MEDIUM 0xAAFF00 // Yellow-green - medium (deprecated, use FULL)
#define COLOR_HUMANIZATION_HIGH 0x00FF00 // Green - high (deprecated, use FULL)
#define COLOR_HUMANIZATION_FULL 0x00FF00 // Green - full humanization (raw input)

// Brightness constants
Expand Down Expand Up @@ -509,22 +506,12 @@ typedef struct __attribute__((packed)) {
// LOGGING CONFIGURATION
//--------------------------------------------------------------------+

#if BUILD_CONFIG == BUILD_CONFIG_PRODUCTION
#define ENABLE_VERBOSE_LOGGING 0
#define ENABLE_INIT_LOGGING 0
#define ENABLE_ERROR_LOGGING 0
#define ENABLE_STATS_LOGGING 0
#elif BUILD_CONFIG == BUILD_CONFIG_TESTING
#define ENABLE_VERBOSE_LOGGING 0
#define ENABLE_INIT_LOGGING 0
#define ENABLE_ERROR_LOGGING 0
#define ENABLE_STATS_LOGGING 0
#else // Development and Debug
#define ENABLE_VERBOSE_LOGGING 0
#define ENABLE_INIT_LOGGING 0
#define ENABLE_ERROR_LOGGING 0
#define ENABLE_STATS_LOGGING 0
#endif
// Logging flags — set to 1 to enable per-category printf output.
// All disabled by default to minimize code size and UART noise.
#define ENABLE_VERBOSE_LOGGING 0
#define ENABLE_INIT_LOGGING 0
#define ENABLE_ERROR_LOGGING 0
#define ENABLE_STATS_LOGGING 0

//--------------------------------------------------------------------+
// CONDITIONAL COMPILATION MACROS
Expand Down
15 changes: 0 additions & 15 deletions state_management.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,3 @@ system_state_t* get_system_state(void) {
return &g_system_state;
}

// Performance-optimized inline time check function
inline bool system_state_should_run_task(const system_state_t* state, uint32_t current_time,
uint32_t last_run_time, uint32_t interval_ms) {
(void)state; // Suppress unused parameter warning
return (current_time - last_run_time) >= interval_ms;
}

// Batch update function for performance - updates multiple timers at once
void system_state_batch_update_timers(system_state_t* state, uint32_t current_time,
uint8_t update_flags) {
if (update_flags & 0x01) state->last_watchdog_time = current_time;
if (update_flags & 0x02) state->last_visual_time = current_time;
if (update_flags & 0x04) state->last_button_time = current_time;
if (update_flags & 0x08) state->watchdog_status_timer = current_time;
}
8 changes: 0 additions & 8 deletions state_management.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,5 @@ void system_state_init(system_state_t* state);
// Get current system state (singleton pattern)
system_state_t* get_system_state(void);

// State update helpers
void system_state_update_timing(system_state_t* state, uint32_t current_time);
bool system_state_should_run_task(const system_state_t* state, uint32_t current_time,
uint32_t last_run_time, uint32_t interval_ms);

// Performance optimization: batch timer updates
void system_state_batch_update_timers(system_state_t* state, uint32_t current_time,
uint8_t update_flags);

#endif // STATE_MANAGEMENT_H
19 changes: 13 additions & 6 deletions tools/Makefile
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
# Mouse Counteraction Tool - macOS Build
# Tools Build

CC = clang
CFLAGS = -Wall -Wextra -O2
FRAMEWORKS = -framework CoreGraphics -framework ApplicationServices -framework Foundation
TARGET = mouse_counteract
INCLUDES = -I../lib/wire-protocol/include

all: $(TARGET)
all: mouse_counteract kmbox_relay

$(TARGET): mouse_counteract.c
mouse_counteract: mouse_counteract.c
$(CC) $(CFLAGS) -o $@ $< $(FRAMEWORKS)

kmbox_relay: kmbox_relay.c ../lib/wire-protocol/include/wire_protocol.h
$(CC) $(CFLAGS) $(INCLUDES) -o $@ $<

# Cross-compile for Windows (requires mingw-w64: brew install mingw-w64)
kmbox_relay.exe: kmbox_relay.c ../lib/wire-protocol/include/wire_protocol.h
x86_64-w64-mingw32-gcc $(CFLAGS) $(INCLUDES) -o $@ $< -lws2_32

clean:
rm -f $(TARGET)
rm -f mouse_counteract kmbox_relay kmbox_relay.exe

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The compiled binary tools/kmbox_relay was committed to the repository. This binary should be removed and added to .gitignore (similar to how tools/mouse_counteract is already excluded on line 16 of .gitignore). Committing binaries bloats the repository and makes it harder to track meaningful changes.

Fix it with Roo Code or mention @roomote and request a fix.


.PHONY: all clean
.PHONY: all clean
Binary file added tools/kmbox_relay
Binary file not shown.
Loading