145 lines
3.8 KiB
C++
145 lines
3.8 KiB
C++
#include "usb.h"
|
|
#include "aconnoBsp.h"
|
|
|
|
extern MCP23017 expdr;
|
|
extern AconnoGPIO ld1;
|
|
|
|
|
|
static int8_t hid_acc_for_report_get(int16_t acc);
|
|
|
|
static void mouse_move_timer_handler(void * p_context);
|
|
|
|
static void usbd_user_ev_handler(app_usbd_event_type_t event);
|
|
|
|
|
|
void usbSetup()
|
|
{
|
|
NRF_LOG_INFO("USB SETUP");
|
|
ret_code_t ret;
|
|
static const app_usbd_config_t usbd_config = {
|
|
.ev_state_proc = usbd_user_ev_handler
|
|
};
|
|
|
|
app_timer_create(&m_mouse_move_timer, APP_TIMER_MODE_REPEATED,
|
|
mouse_move_timer_handler);
|
|
APP_ERROR_CHECK(ret);
|
|
|
|
//NRF_LOG_INFO("BEFORE USB INIT");
|
|
ret = app_usbd_init(&usbd_config);
|
|
APP_ERROR_CHECK(ret);
|
|
//NRF_LOG_INFO("AFTER USB INIT");
|
|
|
|
app_usbd_class_inst_t const * class_inst_mouse;
|
|
#if CONFIG_HAS_MOUSE
|
|
class_inst_mouse = app_usbd_hid_mouse_class_inst_get(&m_app_hid_mouse);
|
|
#endif
|
|
|
|
ret = app_usbd_class_append(class_inst_mouse);
|
|
APP_ERROR_CHECK(ret);
|
|
|
|
if (USBD_POWER_DETECTION)
|
|
{
|
|
ret = app_usbd_power_events_enable();
|
|
APP_ERROR_CHECK(ret);
|
|
NRF_LOG_INFO("USB POWER DETECTION");
|
|
}
|
|
else
|
|
{
|
|
NRF_LOG_INFO("No USB power detection enabled\r\nStarting USB now");
|
|
|
|
app_usbd_enable();
|
|
app_usbd_start();
|
|
}
|
|
}
|
|
|
|
static void mouse_move_timer_handler(void * p_context)
|
|
{
|
|
UNUSED_PARAMETER(p_context);
|
|
UNUSED_RETURN_VALUE(app_usbd_hid_mouse_x_move(&m_app_hid_mouse, CONFIG_MOUSE_MOVE_STEP));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* @brief Class specific event handler.
|
|
*
|
|
* @param p_inst Class instance.
|
|
* @param event Class specific event.
|
|
* */
|
|
void hid_mouse_user_ev_handler(app_usbd_class_inst_t const * p_inst,
|
|
app_usbd_hid_user_event_t event)
|
|
{
|
|
UNUSED_PARAMETER(p_inst);
|
|
switch (event) {
|
|
case APP_USBD_HID_USER_EVT_OUT_REPORT_READY:
|
|
/* No output report defined for HID mouse.*/
|
|
//ASSERT(0);
|
|
break;
|
|
case APP_USBD_HID_USER_EVT_IN_REPORT_DONE:
|
|
ld1.clear();
|
|
break;
|
|
case APP_USBD_HID_USER_EVT_SET_BOOT_PROTO:
|
|
UNUSED_RETURN_VALUE(hid_mouse_clear_buffer(p_inst));
|
|
|
|
break;
|
|
case APP_USBD_HID_USER_EVT_SET_REPORT_PROTO:
|
|
UNUSED_RETURN_VALUE(hid_mouse_clear_buffer(p_inst));
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief USBD library specific event handler.
|
|
*
|
|
* @param event USBD library event.
|
|
* */
|
|
static void usbd_user_ev_handler(app_usbd_event_type_t event)
|
|
{
|
|
//NRF_LOG_INFO("USBD user handler");
|
|
switch (event)
|
|
{
|
|
case APP_USBD_EVT_DRV_SOF:
|
|
//NRF_LOG_INFO("APP_USBD_EVT_DRV_SOF");
|
|
break;
|
|
case APP_USBD_EVT_DRV_SUSPEND:
|
|
//NRF_LOG_INFO("APP_USBD_EVT_DRV_SUSPEND");
|
|
app_usbd_suspend_req(); // Allow the library to put the peripheral into sleep mode
|
|
ld1.set();
|
|
break;
|
|
case APP_USBD_EVT_DRV_RESUME:
|
|
//NRF_LOG_INFO("APP_USBD_EVT_DRV_RESUME");
|
|
ld1.clear();
|
|
break;
|
|
case APP_USBD_EVT_STARTED:
|
|
//NRF_LOG_INFO("APP_USBD_EVT_STARTED");
|
|
ld1.clear();;
|
|
break;
|
|
case APP_USBD_EVT_STOPPED:
|
|
//NRF_LOG_INFO("APP_USBD_EVT_STOPPED");
|
|
app_usbd_disable();
|
|
ld1.set();
|
|
break;
|
|
case APP_USBD_EVT_POWER_DETECTED:
|
|
NRF_LOG_INFO("USB power detected");
|
|
if (!nrf_drv_usbd_is_enabled())
|
|
{
|
|
app_usbd_enable();
|
|
}
|
|
break;
|
|
case APP_USBD_EVT_POWER_REMOVED:
|
|
NRF_LOG_INFO("USB power removed");
|
|
app_usbd_stop();
|
|
break;
|
|
case APP_USBD_EVT_POWER_READY:
|
|
NRF_LOG_INFO("USB ready");
|
|
app_usbd_start();
|
|
break;
|
|
default:
|
|
//NRF_LOG_INFO("DEFAULT");
|
|
break;
|
|
}
|
|
}
|