/** * @file mouse.cpp * @author Odri (odri@aconno.de) * @brief USB HID Mouse source file * @version 0.1 * @date 2019-05-09 * * @copyright Copyright (c) 2019 * */ #ifdef NRF52840_XXAA //------------------------------ INCLUDES ------------------------------------- #include "mouse.h" #include "aconnoBsp.h" //-------------------------------- MACROS ------------------------------------- //----------------------------- DATA TYPES ------------------------------------ //--------------------- PRIVATE FUNCTION PROTOTYPES --------------------------- /** * @brief Handler for the mouse down movement timer * * @param p_context */ static void mouse_move_down_timer_handler(void * p_context); /** * @brief Handler for the mouse up movement timer * * @param p_context */ static void mouse_move_up_timer_handler(void * p_context); /** * @brief Handler for the mouse left movement timer * * @param p_context */ static void mouse_move_left_timer_handler(void * p_context); /** * @brief Handler for the mouse right movement timer * * @param p_context */ static void mouse_move_right_timer_handler(void * p_context); /** * @brief Class specific event handler. * * @param p_inst Class instance. * @param event Class specific event. * */ static void hid_mouse_user_ev_handler(app_usbd_class_inst_t const * p_inst, app_usbd_hid_user_event_t event); /** * @brief USBD library specific event handler. * * @param event USBD library event. * */ static void usbd_user_ev_handler(app_usbd_event_type_t event); //----------------------- STATIC DATA & CONSTANTS ----------------------------- //------------------------------ GLOBAL DATA ---------------------------------- extern MCP23017 expdr; extern AconnoGPIO ld1; /** * @brief Timer to repeat mouse move down */ APP_TIMER_DEF(m_mouse_move_down_timer); /** * @brief Timer to repeat mouse move up */ APP_TIMER_DEF(m_mouse_move_up_timer); /** * @brief Timer to repeat mouse move to the left */ APP_TIMER_DEF(m_mouse_move_left_timer); /** * @brief Timer to repeat mouse move to the right */ APP_TIMER_DEF(m_mouse_move_right_timer); /** * @brief Global HID mouse instance */ APP_USBD_HID_MOUSE_GLOBAL_DEF(m_app_hid_mouse, APP_USBD_INTERFACE_MOUSE, NRF_DRV_USBD_EPIN1, CONFIG_MOUSE_BUTTON_COUNT, hid_mouse_user_ev_handler, APP_USBD_HID_SUBCLASS_BOOT ); //--------------------- PUBLIC FUNCTIONS AND OBJECTS -------------------------- void hid_generic_mouse_process_state(USB_DIR direction) { switch(direction) { case USB_DIR::UP: { //app_usbd_hid_mouse_y_move(&m_app_hid_mouse, -CONFIG_MOUSE_MOVE_STEP); UNUSED_RETURN_VALUE(app_timer_stop(m_mouse_move_down_timer)); UNUSED_RETURN_VALUE(app_timer_start(m_mouse_move_up_timer, APP_TIMER_TICKS(CONFIG_MOUSE_MOVE_TIME_MS), NULL)); break; } case USB_DIR::DOWN: { //app_usbd_hid_mouse_y_move(&m_app_hid_mouse, CONFIG_MOUSE_MOVE_STEP); UNUSED_RETURN_VALUE(app_timer_stop(m_mouse_move_up_timer)); UNUSED_RETURN_VALUE(app_timer_start(m_mouse_move_down_timer, APP_TIMER_TICKS(CONFIG_MOUSE_MOVE_TIME_MS), NULL)); break; } case USB_DIR::LEFT: { //app_usbd_hid_mouse_x_move(&m_app_hid_mouse, -CONFIG_MOUSE_MOVE_STEP); UNUSED_RETURN_VALUE(app_timer_stop(m_mouse_move_right_timer)); UNUSED_RETURN_VALUE(app_timer_start(m_mouse_move_left_timer, APP_TIMER_TICKS(CONFIG_MOUSE_MOVE_TIME_MS), NULL)); break; } case USB_DIR::RIGHT: { //app_usbd_hid_mouse_x_move(&m_app_hid_mouse, CONFIG_MOUSE_MOVE_STEP); UNUSED_RETURN_VALUE(app_timer_stop(m_mouse_move_left_timer)); UNUSED_RETURN_VALUE(app_timer_start(m_mouse_move_right_timer, APP_TIMER_TICKS(CONFIG_MOUSE_MOVE_TIME_MS), NULL)); break; } default: { break; } } } void hid_generic_mouse_stop_state(void) { UNUSED_RETURN_VALUE(app_timer_stop(m_mouse_move_down_timer)); UNUSED_RETURN_VALUE(app_timer_stop(m_mouse_move_up_timer)); UNUSED_RETURN_VALUE(app_timer_stop(m_mouse_move_left_timer)); UNUSED_RETURN_VALUE(app_timer_stop(m_mouse_move_right_timer)); } void usbSetup() { NRF_LOG_INFO("USB SETUP"); ret_code_t ret; static const app_usbd_config_t usbd_config = { NULL, usbd_user_ev_handler }; app_timer_create(&m_mouse_move_down_timer, APP_TIMER_MODE_REPEATED, mouse_move_down_timer_handler); app_timer_create(&m_mouse_move_up_timer, APP_TIMER_MODE_REPEATED, mouse_move_up_timer_handler); app_timer_create(&m_mouse_move_left_timer, APP_TIMER_MODE_REPEATED, mouse_move_left_timer_handler); app_timer_create(&m_mouse_move_right_timer, APP_TIMER_MODE_REPEATED, mouse_move_right_timer_handler); ret = app_usbd_init(&usbd_config); APP_ERROR_CHECK(ret); app_usbd_class_inst_t const * class_inst_mouse; class_inst_mouse = app_usbd_hid_mouse_class_inst_get(&m_app_hid_mouse); ret = app_usbd_class_append(class_inst_mouse); APP_ERROR_CHECK(ret); NRF_LOG_INFO("No USB power detection enabled\r\nStarting USB now"); app_usbd_enable(); app_usbd_start(); } //--------------------------- PRIVATE FUNCTIONS ------------------------------- static void mouse_move_down_timer_handler(void * p_context) { UNUSED_PARAMETER(p_context); UNUSED_RETURN_VALUE(app_usbd_hid_mouse_y_move(&m_app_hid_mouse, CONFIG_MOUSE_MOVE_STEP)); } static void mouse_move_up_timer_handler(void * p_context) { UNUSED_PARAMETER(p_context); UNUSED_RETURN_VALUE(app_usbd_hid_mouse_y_move(&m_app_hid_mouse, -CONFIG_MOUSE_MOVE_STEP)); } static void mouse_move_left_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)); } static void mouse_move_right_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)); } static 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; } } static void usbd_user_ev_handler(app_usbd_event_type_t event) { 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: break; } } //--------------------------- INTERRUPT HANDLERS ------------------------------ #endif