Joystick.c
#include <stdio.h>
#include "pico/stdlib.h"
#include "tusb.h"

typedef struct
{
    uint16_t buttons;
} hid_report_t;

static hid_report_t report;

void process_serial()
{
    if (!tud_cdc_available())
        return;

    char buf[32];
    uint32_t count = tud_cdc_read(buf, sizeof(buf));

    buf[count] = 0;

    int btn;

    if (sscanf(buf,"ON %d",&btn)==1)
        report.buttons |= (1<<(btn-1));

    if (sscanf(buf,"OFF %d",&btn)==1)
        report.buttons &= ~(1<<(btn-1));
}

void send_hid()
{
    if (!tud_hid_ready())
        return;

    tud_hid_report(0,&report,sizeof(report));
}

int main()
{
    stdio_init_all();

    tusb_init();

    while (1)
    {
        tud_task();

        process_serial();

        send_hid();

        sleep_ms(1);
    }
}