107 lines
2.9 KiB
C++
107 lines
2.9 KiB
C++
#include "usb.h"
|
|
|
|
|
|
|
|
/**
|
|
* @brief HID generic IN report send handling
|
|
* */
|
|
static void hid_generic_mouse_action(hid_generic_mouse_action_t action, int8_t param)
|
|
{
|
|
CRITICAL_REGION_ENTER();
|
|
/*
|
|
* Update mouse state
|
|
*/
|
|
switch (action)
|
|
{
|
|
case HID_GENERIC_MOUSE_X:
|
|
m_mouse_state.acc_x += param;
|
|
break;
|
|
case HID_GENERIC_MOUSE_Y:
|
|
m_mouse_state.acc_y += param;
|
|
break;
|
|
case HID_GENERIC_MOUSE_BTN_RIGHT:
|
|
if(param == 1)
|
|
{
|
|
m_mouse_state.btn |= HID_BTN_RIGHT_MASK;
|
|
}
|
|
else
|
|
{
|
|
m_mouse_state.btn &= ~HID_BTN_RIGHT_MASK;
|
|
}
|
|
break;
|
|
case HID_GENERIC_MOUSE_BTN_LEFT:
|
|
if(param == 1)
|
|
{
|
|
m_mouse_state.btn |= HID_BTN_LEFT_MASK;
|
|
}
|
|
else
|
|
{
|
|
m_mouse_state.btn &= ~HID_BTN_LEFT_MASK;
|
|
}
|
|
break;
|
|
}
|
|
CRITICAL_REGION_EXIT();
|
|
}
|
|
|
|
/**
|
|
* @brief Get maximal allowed accumulated value
|
|
*
|
|
* Function gets maximal value from the accumulated input.
|
|
* @sa m_mouse_state::acc_x, m_mouse_state::acc_y
|
|
*/
|
|
static int8_t hid_acc_for_report_get(int16_t acc)
|
|
{
|
|
if(acc > INT8_MAX)
|
|
{
|
|
return INT8_MAX;
|
|
}
|
|
else if(acc < INT8_MIN)
|
|
{
|
|
return INT8_MIN;
|
|
}
|
|
else
|
|
{
|
|
return (int8_t)(acc);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief Internal function that process mouse state
|
|
*
|
|
* This function checks current mouse state and tries to send
|
|
* new report if required.
|
|
* If report sending was successful it clears accumulated positions
|
|
* and mark last button state that was transfered.
|
|
*/
|
|
static void hid_generic_mouse_process_state(void)
|
|
{
|
|
if (m_report_pending)
|
|
return;
|
|
if ((m_mouse_state.acc_x != 0) ||
|
|
(m_mouse_state.acc_y != 0) ||
|
|
(m_mouse_state.btn != m_mouse_state.last_btn))
|
|
{
|
|
ret_code_t ret;
|
|
static uint8_t report[HID_REP_SIZE];
|
|
/* We have some status changed that we need to transfer */
|
|
report[HID_BTN_IDX] = m_mouse_state.btn;
|
|
report[HID_X_IDX] = (uint8_t)hid_acc_for_report_get(m_mouse_state.acc_x);
|
|
report[HID_Y_IDX] = (uint8_t)hid_acc_for_report_get(m_mouse_state.acc_y);
|
|
/* Start the transfer */
|
|
ret = app_usbd_hid_generic_in_report_set(
|
|
&m_app_hid_generic,
|
|
report,
|
|
sizeof(report));
|
|
if (ret == NRF_SUCCESS)
|
|
{
|
|
m_report_pending = true;
|
|
m_mouse_state.last_btn = report[HID_BTN_IDX];
|
|
CRITICAL_REGION_ENTER();
|
|
/* This part of the code can fail if interrupted by BSP keys processing.
|
|
* Lock interrupts to be safe */
|
|
m_mouse_state.acc_x -= (int8_t)report[HID_X_IDX];
|
|
m_mouse_state.acc_y -= (int8_t)report[HID_Y_IDX];
|
|
CRITICAL_REGION_EXIT();
|
|
}
|
|
}
|
|
} |