dev_keys.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2011-12-29 onelife Initial creation for EFM32GG_DK3750 board
  9. */
  10. /***************************************************************************//**
  11. * @addtogroup EFM32GG_DK3750
  12. * @{
  13. ******************************************************************************/
  14. /* Includes ------------------------------------------------------------------*/
  15. #include "board.h"
  16. #include "hdl_interrupt.h"
  17. #include "dev_keys.h"
  18. #if defined(EFM32_USING_KEYS)
  19. #if defined(RT_USING_RTGUI)
  20. #include <rtgui/event.h>
  21. #endif
  22. /* Private typedef -----------------------------------------------------------*/
  23. /* Private define ------------------------------------------------------------*/
  24. /* Private macro -------------------------------------------------------------*/
  25. #ifdef EFM32_KEYS_DEBUG
  26. #define keys_debug(format,args...) rt_kprintf(format, ##args)
  27. #else
  28. #define keys_debug(format,args...)
  29. #endif
  30. /* Private function prototypes -----------------------------------------------*/
  31. /* Private variables ---------------------------------------------------------*/
  32. static struct efm32_joy_device joy;
  33. static struct rt_device joy_dev;
  34. static struct rtgui_event_mouse mouse;
  35. static rt_bool_t click;
  36. /* Private functions ---------------------------------------------------------*/
  37. /***************************************************************************//**
  38. * @brief
  39. * Keys interrupt handler
  40. *
  41. * @details
  42. *
  43. * @note
  44. *
  45. * @param[in] device
  46. * Pointer to device descriptor
  47. ******************************************************************************/
  48. static void efm32_keys_isr(rt_device_t dev)
  49. {
  50. rt_uint16_t flag, joystick;
  51. /* Clear DEK interrupt */
  52. flag = DVK_getInterruptFlags();
  53. DVK_clearInterruptFlags(flag);
  54. if (flag & BC_INTFLAG_PB)
  55. {
  56. }
  57. if (flag & BC_INTFLAG_DIP)
  58. {
  59. }
  60. if (flag & BC_INTFLAG_JOYSTICK)
  61. {
  62. joystick = DVK_getJoystick();
  63. keys_debug("Keys: joystick %x\n", joystick);
  64. #ifdef RT_USING_RTGUI
  65. switch (joystick)
  66. {
  67. case BC_UIF_JOYSTICK_RIGHT:
  68. joy.x += 2;
  69. if (joy.x > joy.max_x)
  70. {
  71. joy.x = joy.max_x;
  72. }
  73. break;
  74. case BC_UIF_JOYSTICK_LEFT:
  75. joy.x -= 2;
  76. if (joy.x < joy.min_x)
  77. {
  78. joy.x = joy.min_x;
  79. }
  80. break;
  81. case BC_UIF_JOYSTICK_DOWN:
  82. joy.y += 2;
  83. if (joy.y > joy.max_y)
  84. {
  85. joy.y = joy.max_y;
  86. }
  87. break;
  88. case BC_UIF_JOYSTICK_UP:
  89. joy.y -= 2;
  90. if (joy.y < joy.min_y)
  91. {
  92. joy.y = joy.min_y;
  93. }
  94. break;
  95. case BC_UIF_JOYSTICK_CENTER:
  96. break;
  97. default:
  98. break;
  99. }
  100. #endif
  101. if (joystick)
  102. {
  103. if (joystick != BC_UIF_JOYSTICK_CENTER)
  104. {
  105. mouse.parent.type = RTGUI_EVENT_MOUSE_MOTION;
  106. mouse.x = joy.x;
  107. mouse.y = joy.y;
  108. rtgui_server_post_event((&mouse.parent), sizeof(mouse));
  109. rt_timer_start(&joy.timer);
  110. }
  111. else
  112. {
  113. click = RT_TRUE;
  114. mouse.parent.type = RTGUI_EVENT_MOUSE_BUTTON;
  115. mouse.button = RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_DOWN;
  116. rtgui_server_post_event((&mouse.parent), sizeof(mouse));
  117. }
  118. }
  119. else
  120. {
  121. if (click)
  122. {
  123. click = RT_FALSE;
  124. mouse.parent.type = RTGUI_EVENT_MOUSE_BUTTON;
  125. mouse.button = RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_UP;
  126. rtgui_server_post_event((&mouse.parent), sizeof(mouse));
  127. }
  128. else
  129. {
  130. rt_timer_stop(&joy.timer);
  131. }
  132. }
  133. }
  134. if (flag & BC_INTFLAG_AEM)
  135. {
  136. }
  137. }
  138. /***************************************************************************//**
  139. * @brief
  140. * Keys timeout handler
  141. *
  142. * @details
  143. *
  144. * @note
  145. *
  146. * @param[in] param
  147. * Parameter
  148. ******************************************************************************/
  149. static void efm32_keys_timer_isr(void *param)
  150. {
  151. rt_uint16_t joystick;
  152. joystick = DVK_getJoystick();
  153. #ifdef RT_USING_RTGUI
  154. switch (joystick)
  155. {
  156. case BC_UIF_JOYSTICK_RIGHT:
  157. joy.x += 2;
  158. if (joy.x > joy.max_x)
  159. {
  160. joy.x = joy.max_x;
  161. }
  162. break;
  163. case BC_UIF_JOYSTICK_LEFT:
  164. joy.x -= 2;
  165. if (joy.x < joy.min_x)
  166. {
  167. joy.x = joy.min_x;
  168. }
  169. break;
  170. case BC_UIF_JOYSTICK_DOWN:
  171. joy.y += 2;
  172. if (joy.y > joy.max_y)
  173. {
  174. joy.y = joy.max_y;
  175. }
  176. break;
  177. case BC_UIF_JOYSTICK_UP:
  178. joy.y -= 2;
  179. if (joy.y < joy.min_y)
  180. {
  181. joy.y = joy.min_y;
  182. }
  183. break;
  184. }
  185. if (joystick)
  186. {
  187. mouse.parent.type = RTGUI_EVENT_MOUSE_MOTION;
  188. mouse.x = joy.x;
  189. mouse.y = joy.y;
  190. rtgui_server_post_event((&mouse.parent), sizeof(mouse));
  191. }
  192. #endif
  193. }
  194. /***************************************************************************//**
  195. * @brief
  196. * Initialize keys device
  197. *
  198. * @details
  199. *
  200. * @note
  201. *
  202. * @param[in] dev
  203. * Pointer to device descriptor
  204. *
  205. * @return
  206. * Error code
  207. ******************************************************************************/
  208. static rt_err_t efm32_keys_init (rt_device_t dev)
  209. {
  210. struct rt_device_graphic_info lcd_info;
  211. rt_device_t lcd;
  212. lcd = rt_device_find(LCD_DEVICE_NAME);
  213. if (lcd == RT_NULL)
  214. {
  215. keys_debug("Keys err: Can't find LCD\n");
  216. return -RT_ERROR;
  217. }
  218. lcd->control(lcd, RTGRAPHIC_CTRL_GET_INFO, (void *)&lcd_info);
  219. click = RT_FALSE;
  220. joy.x = 0;
  221. joy.y = 0;
  222. joy.min_x = 0;
  223. joy.max_x = lcd_info.width;
  224. joy.min_y = 0;
  225. joy.max_y = lcd_info.height;
  226. mouse.parent.sender = RT_NULL;
  227. mouse.wid = RT_NULL;
  228. mouse.button = 0;
  229. return RT_EOK;
  230. }
  231. /***************************************************************************//**
  232. * @brief
  233. * Initialize keys related hardware and register joystic device to kernel
  234. *
  235. * @details
  236. *
  237. * @note
  238. *
  239. ******************************************************************************/
  240. void efm32_hw_keys_init(void)
  241. {
  242. /* Configure joystick interrupt pin */
  243. GPIO_PinModeSet(KEYS_INT_PORT, KEYS_INT_PIN, gpioModeInputPullFilter, 1);
  244. /* Enable joystick interrupt */
  245. GPIO_IntConfig(KEYS_INT_PORT, KEYS_INT_PIN, true, true, true);
  246. efm32_irq_hook_init_t hook;
  247. hook.type = efm32_irq_type_gpio;
  248. hook.unit = KEYS_INT_PIN;
  249. hook.cbFunc = efm32_keys_isr;
  250. hook.userPtr = RT_NULL;
  251. efm32_irq_hook_register(&hook);
  252. if ((rt_uint8_t)KEYS_INT_PIN % 2)
  253. {
  254. NVIC_ClearPendingIRQ(GPIO_ODD_IRQn);
  255. NVIC_SetPriority(GPIO_ODD_IRQn, EFM32_IRQ_PRI_DEFAULT);
  256. NVIC_EnableIRQ(GPIO_ODD_IRQn);
  257. }
  258. else
  259. {
  260. NVIC_ClearPendingIRQ(GPIO_EVEN_IRQn);
  261. NVIC_SetPriority(GPIO_EVEN_IRQn, EFM32_IRQ_PRI_DEFAULT);
  262. NVIC_EnableIRQ(GPIO_EVEN_IRQn);
  263. }
  264. /* Enable DVK joystick interrupt */
  265. DVK_enableInterrupt(BC_INTEN_JOYSTICK);
  266. rt_timer_init(&joy.timer,
  267. "joy_tmr",
  268. efm32_keys_timer_isr,
  269. RT_NULL,
  270. KEYS_POLL_TIME,
  271. RT_TIMER_FLAG_PERIODIC);
  272. joy_dev.init = efm32_keys_init;
  273. joy_dev.open = RT_NULL;
  274. joy_dev.close = RT_NULL;
  275. joy_dev.read = RT_NULL;
  276. joy_dev.write = RT_NULL;
  277. joy_dev.control = RT_NULL;
  278. joy_dev.user_data = (void *)&joy;
  279. /* register joy stick device */
  280. rt_device_register(&joy_dev, "joy", RT_DEVICE_FLAG_RDWR);
  281. keys_debug("Keys: H/W init OK!\n");
  282. }
  283. #endif /* defined(EFM32_USING_KEYS) */
  284. /***************************************************************************//**
  285. * @}
  286. ******************************************************************************/