Files
Odri Tomas 6d116dbe2e Issue #19
Mouse doesn't move diagonally
2019-05-13 12:02:53 +02:00

277 lines
7.8 KiB
C++

/**
* @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 "aconnoUSBMouse.h"
#include "aconnoBsp.h"
//-------------------------------- MACROS -------------------------------------
//----------------------------- DATA TYPES ------------------------------------
//--------------------- PRIVATE FUNCTION PROTOTYPES ---------------------------
/**
* @brief Handler for the mouse down movement timer
*
* @param p_context
*/
static void mouseMoveDownTimerHandler(void * p_context);
/**
* @brief Handler for the mouse up movement timer
*
* @param p_context
*/
static void mouseMoveUpTimerHandler(void * p_context);
/**
* @brief Handler for the mouse left movement timer
*
* @param p_context
*/
static void mouseMoveLeftTimerHandler(void * p_context);
/**
* @brief Handler for the mouse right movement timer
*
* @param p_context
*/
static void mouseMoveRightTimerHandler(void * p_context);
/**
* @brief Class specific event handler.
*
* @param p_inst Class instance.
* @param event Class specific event.
* */
static void hidMouseUserEvHandler(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 usbdUserEvHandler(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(mouseMoveDownTimer);
/**
* @brief Timer to repeat mouse move up
*/
APP_TIMER_DEF(mouseMoveUpTimer);
/**
* @brief Timer to repeat mouse move to the left
*/
APP_TIMER_DEF(mouseMoveLeftTimer);
/**
* @brief Timer to repeat mouse move to the right
*/
APP_TIMER_DEF(mouseMoveRightTimer);
/**
* @brief Global HID mouse instance
*/
APP_USBD_HID_MOUSE_GLOBAL_DEF(appHidMouse,
APP_USBD_INTERFACE_MOUSE,
NRF_DRV_USBD_EPIN1,
CONFIG_MOUSE_BUTTON_COUNT,
hidMouseUserEvHandler,
APP_USBD_HID_SUBCLASS_BOOT
);
//--------------------- PUBLIC FUNCTIONS AND OBJECTS --------------------------
void hidGenericMouseProcessState(USB_DIR direction)
{
switch(direction)
{
case USB_DIR::UP:
{
hidGenericMouseStopState();
UNUSED_RETURN_VALUE(app_timer_start(mouseMoveUpTimer,
APP_TIMER_TICKS(CONFIG_MOUSE_MOVE_TIME_MS),
NULL));
break;
}
case USB_DIR::DOWN:
{
hidGenericMouseStopState();
UNUSED_RETURN_VALUE(app_timer_start(mouseMoveDownTimer,
APP_TIMER_TICKS(CONFIG_MOUSE_MOVE_TIME_MS),
NULL));
break;
}
case USB_DIR::LEFT:
{
hidGenericMouseStopState();
UNUSED_RETURN_VALUE(app_timer_start(mouseMoveLeftTimer,
APP_TIMER_TICKS(CONFIG_MOUSE_MOVE_TIME_MS),
NULL));
break;
}
case USB_DIR::RIGHT:
{
hidGenericMouseStopState();
UNUSED_RETURN_VALUE(app_timer_start(mouseMoveRightTimer,
APP_TIMER_TICKS(CONFIG_MOUSE_MOVE_TIME_MS),
NULL));
break;
}
default:
{
break;
}
}
}
void hidGenericMouseStopState(void)
{
UNUSED_RETURN_VALUE(app_timer_stop(mouseMoveDownTimer));
UNUSED_RETURN_VALUE(app_timer_stop(mouseMoveUpTimer));
UNUSED_RETURN_VALUE(app_timer_stop(mouseMoveLeftTimer));
UNUSED_RETURN_VALUE(app_timer_stop(mouseMoveRightTimer));
}
void usbSetup()
{
NRF_LOG_INFO("USB setup");
ret_code_t ret;
static const app_usbd_config_t usbdConfig = {
NULL,
usbdUserEvHandler
};
app_timer_create(&mouseMoveDownTimer, APP_TIMER_MODE_REPEATED,
mouseMoveDownTimerHandler);
app_timer_create(&mouseMoveUpTimer, APP_TIMER_MODE_REPEATED,
mouseMoveUpTimerHandler);
app_timer_create(&mouseMoveLeftTimer, APP_TIMER_MODE_REPEATED,
mouseMoveLeftTimerHandler);
app_timer_create(&mouseMoveRightTimer, APP_TIMER_MODE_REPEATED,
mouseMoveRightTimerHandler);
ret = app_usbd_init(&usbdConfig);
APP_ERROR_CHECK(ret);
app_usbd_class_inst_t const * classInstMouse;
classInstMouse = app_usbd_hid_mouse_class_inst_get(&appHidMouse);
ret = app_usbd_class_append(classInstMouse);
APP_ERROR_CHECK(ret);
NRF_LOG_INFO("No USB power detection enabled.");
NRF_LOG_INFO("Starting USB now.");
app_usbd_enable();
app_usbd_start();
}
//--------------------------- PRIVATE FUNCTIONS -------------------------------
static void mouseMoveDownTimerHandler(void * pContext)
{
UNUSED_PARAMETER(pContext);
UNUSED_RETURN_VALUE(app_usbd_hid_mouse_y_move(&appHidMouse,
CONFIG_MOUSE_MOVE_STEP));
}
static void mouseMoveUpTimerHandler(void * pContext)
{
UNUSED_PARAMETER(pContext);
UNUSED_RETURN_VALUE(app_usbd_hid_mouse_y_move(&appHidMouse,
-CONFIG_MOUSE_MOVE_STEP));
}
static void mouseMoveLeftTimerHandler(void * pContext)
{
UNUSED_PARAMETER(pContext);
UNUSED_RETURN_VALUE(app_usbd_hid_mouse_x_move(&appHidMouse,
-CONFIG_MOUSE_MOVE_STEP));
}
static void mouseMoveRightTimerHandler(void * pContext)
{
UNUSED_PARAMETER(pContext);
UNUSED_RETURN_VALUE(app_usbd_hid_mouse_x_move(&appHidMouse,
CONFIG_MOUSE_MOVE_STEP));
}
static void hidMouseUserEvHandler(app_usbd_class_inst_t const * pInst,
app_usbd_hid_user_event_t event)
{
UNUSED_PARAMETER(pInst);
switch (event) {
case APP_USBD_HID_USER_EVT_OUT_REPORT_READY:
/* No output report defined for HID mouse.*/
break;
case APP_USBD_HID_USER_EVT_IN_REPORT_DONE:
break;
case APP_USBD_HID_USER_EVT_SET_BOOT_PROTO:
UNUSED_RETURN_VALUE(hid_mouse_clear_buffer(pInst));
break;
case APP_USBD_HID_USER_EVT_SET_REPORT_PROTO:
UNUSED_RETURN_VALUE(hid_mouse_clear_buffer(pInst));
break;
default:
break;
}
}
static void usbdUserEvHandler(app_usbd_event_type_t event)
{
switch (event)
{
case APP_USBD_EVT_DRV_SOF:
break;
case APP_USBD_EVT_DRV_SUSPEND:
// Allow the library to put the peripheral into sleep mode
app_usbd_suspend_req();
break;
case APP_USBD_EVT_DRV_RESUME:
break;
case APP_USBD_EVT_STARTED:
break;
case APP_USBD_EVT_STOPPED:
app_usbd_disable();
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