/*
 * Copyright 2022 MindMotion Microelectronics Co., Ltd.
 * All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include "board_init.h"
#include "tusb.h"

/*
 * Macros.
 */

/*
 * Variables.
 */

/*
 * Declerations.
 */
void process_mouse_report(hid_mouse_report_t const * report);

/*
 * Functions.
 */
int main(void)
{
    BOARD_Init();

    printf("tuh_hid_mouse example.\r\n");

    tuh_init(BOARD_TUH_RHPORT); /* init tinyusb with host mode. */

    while (1)
    {
        tuh_task(); /* tinyusb host task. */
    }
}

/* Invoked when device attached & recognised. */
void tuh_mount_cb(uint8_t dev_addr)
{
    printf("a device with address %d is mounted\r\n", dev_addr);
}

/* Invoked when device detached. */
void tuh_umount_cb(uint8_t dev_addr)
{
    printf("a device with address %d is unmounted \r\n", dev_addr);
}

/*
 * Invoked when device with hid interface is mounted
 * Report descriptor is also available for use. tuh_hid_parse_report_descriptor()
 * can be used to parse common/simple enough descriptor.
 * Note: if report descriptor length > CFG_TUH_ENUMERATION_BUFSIZE, it will be skipped
 * therefore report_desc = NULL, desc_len = 0
 */
void tuh_hid_mount_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* desc_report, uint16_t desc_len)
{
    printf("hid device address = %d, instance = %d is mounted\r\n", dev_addr, instance);
    uint8_t const itf_protocol = tuh_hid_interface_protocol(dev_addr, instance);

    if (HID_ITF_PROTOCOL_MOUSE == itf_protocol)
    {
        printf("the instance is a mouse.\r\n");
    }
    else
    {
        printf("the instance is not a mouse.\r\n");
    }

    /* start recv hid report. */
    if ( !tuh_hid_receive_report(dev_addr, instance) )
    {
        printf("error: cannot request to receive report\r\n");
    }
}

/* Invoked when device with hid interface is un-mounted. */
void tuh_hid_umount_cb(uint8_t dev_addr, uint8_t instance)
{
  printf("hid device address = %d, instance = %d is unmounted\r\n", dev_addr, instance);
}

/* Invoked when received report from device via interrupt endpoint. */
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);

    switch (itf_protocol)
    {
        case HID_ITF_PROTOCOL_MOUSE:
            process_mouse_report( (hid_mouse_report_t const*) report );
            break;
        default:
            break;
    }

    /* start recv next hid report. */
    if ( !tuh_hid_receive_report(dev_addr, instance) )
    {
        printf("error: cannot request to receive report\r\n");
    }
}

/* mouse report. */
void process_mouse_report(hid_mouse_report_t const * report)
{
    if ( 0 != report->buttons)
    {
        printf(" %c%c%c ",
            report->buttons & MOUSE_BUTTON_LEFT   ? 'L' : '-',
            report->buttons & MOUSE_BUTTON_MIDDLE ? 'M' : '-',
            report->buttons & MOUSE_BUTTON_RIGHT  ? 'R' : '-');
    }

    printf("(%d %d %d)\r\n", report->x, report->y, report->wheel);
}

/* EOF. */
