mouse_button.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /******************************************************************************
  2. *
  3. * Freescale Semiconductor Inc.
  4. * (c) Copyright 2004-2010 Freescale Semiconductor, Inc.
  5. * ALL RIGHTS RESERVED.
  6. *
  7. **************************************************************************//*!
  8. *
  9. * @file mouse_button.c
  10. *
  11. * @author
  12. *
  13. * @version
  14. *
  15. * @date May-28-2009
  16. *
  17. * @brief The file emulates a mouse with buttons
  18. * 4 buttons are used on the demo board for the emulation
  19. * PTG0--------- for left click(For JS16 PTG0 is inactive)
  20. * PTG1--------- for right click
  21. * PTG2--------- if macro "UP_LEFT" is defined mouse moves "left"
  22. * else mouse moves "right"
  23. * PTG3--------- if macro "UP_LEFT" is defined mouse moves "up"
  24. * else mouse moves "down"
  25. *
  26. * NOTE: key pressed == LOGIC 0
  27. * key released == LOGIC 1
  28. *****************************************************************************/
  29. /******************************************************************************
  30. * Includes
  31. *****************************************************************************/
  32. #include "hidef.h" /* for EnableInterrupts macro */
  33. #include "derivative.h" /* include peripheral declarations */
  34. #include "types.h" /* User Defined Data Types */
  35. #include "usb_hid.h" /* USB HID Class Header File */
  36. #include "mouse_button.h" /* Mouse Application Header File */
  37. #if HIGH_SPEED_DEVICE
  38. //#include "usbhs_common.h"
  39. //#include "usbhs_host_device.h"
  40. #endif
  41. #if (defined _MCF51MM256_H) || (defined _MCF51JE256_H)
  42. #include "exceptions.h"
  43. #endif
  44. /* skip the inclusion in dependency state */
  45. #ifndef __NO_SETJMP
  46. #include <stdio.h>
  47. #endif
  48. #include <stdlib.h>
  49. #include <string.h>
  50. /*****************************************************************************
  51. * Constant and Macro's - None
  52. *****************************************************************************/
  53. /*****************************************************************************
  54. * Global Functions Prototypes
  55. *****************************************************************************/
  56. void TestApp_Init(void);
  57. /****************************************************************************
  58. * Global Variables - None
  59. ****************************************************************************/
  60. /*****************************************************************************
  61. * Local Types - None
  62. *****************************************************************************/
  63. /*****************************************************************************
  64. * Local Functions Prototypes
  65. *****************************************************************************/
  66. static void USB_App_Callback(
  67. uint_8 controller_ID,
  68. uint_8 event_type,
  69. void* val);
  70. static uint_8 USB_App_Param_Callback(
  71. uint_8 request,
  72. uint_16 value,
  73. uint_8_ptr* data,
  74. USB_PACKET_SIZE* size);
  75. static void Emulate_Mouse_WithButton(void);
  76. /*****************************************************************************
  77. * Local Variables
  78. *****************************************************************************/
  79. static boolean mouse_init=FALSE; /* Application Init Flag */
  80. static uint_8 rpt_buf[MOUSE_BUFF_SIZE]; /* Mouse Event Report Buffer
  81. Key press */
  82. static uint_8 null_buf[MOUSE_BUFF_SIZE]; /* Mouse Event Report Buffer
  83. Key Release */
  84. static uint_8 g_app_request_params[2]; /* for get/set idle and protocol
  85. requests*/
  86. /*****************************************************************************
  87. * Local Functions
  88. *****************************************************************************/
  89. /******************************************************************************
  90. *
  91. * @name Emulate_Mouse_WithButton
  92. *
  93. * @brief This function gets the input from mouse movement, the mouse
  94. * will move if the any button are pushed,otherwise USB gets NAK
  95. *
  96. * @param None
  97. *
  98. * @return None
  99. *
  100. *****************************************************************************
  101. * This function sends the mouse data depending on which key was pressed on
  102. * the board
  103. *****************************************************************************/
  104. static void Emulate_Mouse_WithButton(void)
  105. {
  106. if(kbi_stat > 0)
  107. {
  108. switch(kbi_stat & KBI_STAT_MASK)
  109. {
  110. case LEFT_CLICK : /* PTG0 (left click) is pressed */
  111. rpt_buf[0] = LEFT_CLICK;
  112. rpt_buf[1] = 0x00;
  113. rpt_buf[2] = 0x00;
  114. break;
  115. case RIGHT_CLICK : /* PTG1 (right click) is pressed */
  116. rpt_buf[0] = RIGHT_CLICK;
  117. rpt_buf[1] = 0x00;
  118. rpt_buf[2] = 0x00;
  119. break;
  120. case MOVE_LEFT_RIGHT : /* PTG2 (left
  121. or right movement--depends on
  122. UP_LEFT macro) is pressed*/
  123. rpt_buf[1] = SHIFT_VAL;
  124. rpt_buf[0] = 0x00;
  125. rpt_buf[2] = 0x00;
  126. break;
  127. case MOVE_UP_DOWN : /* PTG3 (up or down
  128. movement--depends on
  129. UP_LEFT macro) is pressed*/
  130. rpt_buf[2] = SHIFT_VAL;
  131. rpt_buf[1] = 0x00;
  132. rpt_buf[0] = 0x00;
  133. break;
  134. default:break; /* otherwise */
  135. }
  136. kbi_stat = 0x00; /* reset status after servicing interrupt*/
  137. (void)USB_Class_HID_Send_Data(CONTROLLER_ID,HID_ENDPOINT,rpt_buf,
  138. MOUSE_BUFF_SIZE);
  139. if(rpt_buf[0])
  140. {
  141. /* required to send Click Release for Click Press Events */
  142. (void)USB_Class_HID_Send_Data(CONTROLLER_ID,HID_ENDPOINT,null_buf,
  143. MOUSE_BUFF_SIZE);
  144. }
  145. }
  146. return;
  147. }
  148. /******************************************************************************
  149. *
  150. * @name USB_App_Callback
  151. *
  152. * @brief This function handles the callback
  153. *
  154. * @param controller_ID : Controller ID
  155. * @param event_type : value of the event
  156. * @param val : gives the configuration value
  157. *
  158. * @return None
  159. *
  160. *****************************************************************************
  161. * This function is called from the class layer whenever reset occurs or enum
  162. * is complete. after the enum is complete this function sets a variable so
  163. * that the application can start
  164. *****************************************************************************/
  165. void USB_App_Callback(
  166. uint_8 controller_ID,/* [IN] Controller ID */
  167. uint_8 event_type, /* [IN] value of the event*/
  168. void* val /* [IN] gives the configuration value*/
  169. )
  170. {
  171. UNUSED (controller_ID)
  172. UNUSED (val)
  173. if((event_type == USB_APP_BUS_RESET) || (event_type == USB_APP_CONFIG_CHANGED))
  174. {
  175. mouse_init=FALSE;
  176. }
  177. else if(event_type == USB_APP_ENUM_COMPLETE)
  178. { /* if enumeration is complete set mouse_init
  179. so that application can start */
  180. mouse_init=TRUE;
  181. }
  182. return;
  183. }
  184. /******************************************************************************
  185. *
  186. * @name USB_App_Param_Callback
  187. *
  188. * @brief This function handles callbacks for USB HID Class request
  189. *
  190. * @param request : request type
  191. * @param value : give report type and id
  192. * @param data : pointer to the data
  193. * @param size : size of the transfer
  194. *
  195. * @return status
  196. * USB_OK : if successful
  197. * else return error
  198. *
  199. *****************************************************************************
  200. * This function is called whenever a HID class request is received. This
  201. * function handles these class requests
  202. *****************************************************************************/
  203. uint_8 USB_App_Param_Callback(
  204. uint_8 request, /* [IN] request type */
  205. uint_16 value, /* [IN] report type and ID */
  206. uint_8_ptr* data, /* [OUT] pointer to the data */
  207. USB_PACKET_SIZE* size /* [OUT] size of the transfer */
  208. )
  209. {
  210. uint_8 status = USB_OK;
  211. uint_8 index = (uint_8)((request - 2) & USB_HID_REQUEST_TYPE_MASK);
  212. /* index == 0 for get/set idle,
  213. index == 1 for get/set protocol */
  214. *size = 0;
  215. /* handle the class request */
  216. switch(request)
  217. {
  218. case USB_HID_GET_REPORT_REQUEST :
  219. *data = &rpt_buf[0]; /* point to the report to send */
  220. *size = MOUSE_BUFF_SIZE; /* report size */
  221. break;
  222. case USB_HID_SET_REPORT_REQUEST :
  223. for(index = 0; index < MOUSE_BUFF_SIZE ; index++)
  224. { /* copy the report sent by the host */
  225. rpt_buf[index] = *(*data + index);
  226. }
  227. break;
  228. case USB_HID_GET_IDLE_REQUEST :
  229. /* point to the current idle rate */
  230. *data = &g_app_request_params[index];
  231. *size = REQ_DATA_SIZE;
  232. break;
  233. case USB_HID_SET_IDLE_REQUEST :
  234. /* set the idle rate sent by the host */
  235. g_app_request_params[index] =(uint_8)((value & MSB_MASK) >>
  236. HIGH_BYTE_SHIFT);
  237. break;
  238. case USB_HID_GET_PROTOCOL_REQUEST :
  239. /* point to the current protocol code
  240. 0 = Boot Protocol
  241. 1 = Report Protocol*/
  242. *data = &g_app_request_params[index];
  243. *size = REQ_DATA_SIZE;
  244. break;
  245. case USB_HID_SET_PROTOCOL_REQUEST :
  246. /* set the protocol sent by the host
  247. 0 = Boot Protocol
  248. 1 = Report Protocol*/
  249. g_app_request_params[index] = (uint_8)(value);
  250. break;
  251. }
  252. return status;
  253. }
  254. /******************************************************************************
  255. *
  256. * @name TestApp_Init
  257. *
  258. * @brief This function is the entry for Mouse Application
  259. *
  260. * @param None
  261. *
  262. * @return None
  263. *
  264. *****************************************************************************
  265. * This function starts the Mouse Application
  266. *****************************************************************************/
  267. void TestApp_Init(void)
  268. {
  269. uint_8 error;
  270. rpt_buf[3] = 0x00; /* always zero */
  271. *((uint_32_ptr)rpt_buf) = 0;
  272. *((uint_32_ptr)null_buf) = 0;
  273. DisableInterrupts;
  274. #if (defined _MCF51MM256_H) || (defined _MCF51JE256_H)
  275. usb_int_dis();
  276. #endif
  277. /* Initialize the USB interface */
  278. error = USB_Class_HID_Init(CONTROLLER_ID, USB_App_Callback, NULL,
  279. USB_App_Param_Callback);
  280. UNUSED(error);
  281. EnableInterrupts;
  282. #if (defined _MCF51MM256_H) || (defined _MCF51JE256_H)
  283. usb_int_en();
  284. #endif
  285. }
  286. /******************************************************************************
  287. *
  288. * @name TestApp_Task
  289. *
  290. * @brief Application task function. It is called from the main loop
  291. *
  292. * @param None
  293. *
  294. * @return None
  295. *
  296. *****************************************************************************
  297. * Application task function. It is called from the main loop
  298. *****************************************************************************/
  299. void TestApp_Task(void)
  300. {
  301. /* call the periodic task function */
  302. USB_Class_HID_Periodic_Task();
  303. if(mouse_init) /*check whether enumeration is
  304. complete or not */
  305. {
  306. /* run the button emulation code */
  307. Emulate_Mouse_WithButton();
  308. }
  309. }