guix_virtual_display_mono.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * 模块名称 : GUI单色屏虚拟设备
  3. * 文件名称 : guix_virtual_display_mono.c
  4. * 版 本 : V1.0
  5. * 说 明 :
  6. *
  7. * 修改记录 :
  8. * 版本号 日期 作者 说明
  9. * V1.0 2020-11-26 HelloByeAll 首版
  10. *
  11. *
  12. */
  13. #include <rtthread.h>
  14. #include <board.h>
  15. #include <gx_user.h>
  16. #ifdef GUIX_VIRTUAL_DISPLAY_MONO
  17. #ifndef GUIX_VIRTUAL_DISPLAY_SERIAL_DEV
  18. #define GUIX_VIRTUAL_DISPLAY_SERIAL_DEV "uart2"
  19. #endif // !GUIX_VIRTUAL_DISPLAY_SERIAL_DEV
  20. static rt_sem_t serial_sem;
  21. static rt_device_t uart_device;
  22. typedef struct
  23. {
  24. rt_uint8_t addr[2];
  25. rt_uint16_t len;
  26. } virtual_display_t;
  27. static virtual_display_t virtual_serial = {0xBB, 0xCC, 2048};
  28. void show_virtual_display(rt_uint8_t *p)
  29. {
  30. rt_device_write(uart_device, 0, &virtual_serial, 4);
  31. rt_device_write(uart_device, 0, p, 2048);
  32. }
  33. static void key_cb(rt_uint8_t key, rt_uint8_t state)
  34. {
  35. switch (key)
  36. {
  37. case 1:
  38. if (state)
  39. {
  40. /* 按键1长按 */
  41. }
  42. else
  43. {
  44. /* 按键1短按 */
  45. }
  46. break;
  47. case 2:
  48. if (state)
  49. {
  50. /* 按键2长按 */
  51. }
  52. else
  53. {
  54. /* 按键2短按 */
  55. }
  56. break;
  57. case 3:
  58. if (state)
  59. {
  60. /* 按键3长按 */
  61. }
  62. else
  63. {
  64. /* 按键3短按 */
  65. }
  66. break;
  67. }
  68. }
  69. static void send_coordinate_to_guix(rt_uint8_t x, rt_uint8_t y, rt_uint8_t state)
  70. {
  71. extern ULONG display_1_canvas_memory[512];
  72. static rt_uint8_t x1, y1, state1;
  73. GX_EVENT event;
  74. event.gx_event_type = 0;
  75. if (x == 0xEE)
  76. {
  77. key_cb(y, state);
  78. return;
  79. }
  80. if (x1 != x || y1 != y)
  81. {
  82. x1 = x;
  83. y1 = y;
  84. if (state1 == state && state == 1)
  85. {
  86. event.gx_event_type = GX_EVENT_PEN_DRAG;
  87. }
  88. }
  89. if (state1 != state)
  90. {
  91. state1 = state;
  92. if (state == 1)
  93. event.gx_event_type = GX_EVENT_PEN_DOWN;
  94. else
  95. event.gx_event_type = GX_EVENT_PEN_UP;
  96. }
  97. if (event.gx_event_type != 0)
  98. {
  99. event.gx_event_payload.gx_event_pointdata.gx_point_x = x1;
  100. event.gx_event_payload.gx_event_pointdata.gx_point_y = y1;
  101. event.gx_event_sender = 0;
  102. event.gx_event_target = 0;
  103. event.gx_event_display_handle = (ULONG *)display_1_canvas_memory;
  104. gx_system_event_send(&event);
  105. }
  106. // rt_kprintf("x = %d, y = %d state = %d\r\n", x, y, state);
  107. }
  108. static void check_data(rt_uint8_t *data)
  109. {
  110. rt_uint8_t sum = 0;
  111. for (size_t i = 0; i < 6; i++)
  112. {
  113. sum += data[i];
  114. }
  115. if (data[6] == sum)
  116. {
  117. send_coordinate_to_guix(data[3], data[4], data[5]);
  118. }
  119. }
  120. static void receive_data_prepare(rt_uint8_t ch)
  121. {
  122. static rt_uint8_t len;
  123. static rt_uint8_t date[10];
  124. date[len] = ch;
  125. if (len == 0 && ch == 0xBB)
  126. len++;
  127. else if (len == 1 && ch == 0xCC)
  128. len++;
  129. else if (len == 2 && ch == 0x02)
  130. len++;
  131. else if (len >= 3 && len <= 5)
  132. len++;
  133. else if (len == 6)
  134. {
  135. check_data(date);
  136. len = 0;
  137. }
  138. else
  139. len = 0;
  140. }
  141. static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
  142. {
  143. rt_sem_release(serial_sem);
  144. return RT_EOK;
  145. }
  146. static void virtual_serial_thread_entry(void *par)
  147. {
  148. char ch;
  149. while (1)
  150. {
  151. while (rt_device_read(uart_device, -1, &ch, 1) != 1)
  152. {
  153. rt_sem_take(serial_sem, RT_WAITING_FOREVER);
  154. }
  155. receive_data_prepare((rt_uint8_t)ch);
  156. }
  157. }
  158. static void rt_virtual_serial_init(char *name)
  159. {
  160. serial_sem = rt_sem_create("mono_sem", 0, RT_IPC_FLAG_FIFO);
  161. uart_device = rt_device_find(name);
  162. struct serial_configure use_config = {
  163. BAUD_RATE_921600, /* 921600 bits/s */
  164. DATA_BITS_8, /* 8 databits */
  165. STOP_BITS_1, /* 1 stopbit */
  166. PARITY_NONE, /* No parity */
  167. BIT_ORDER_LSB, /* LSB first sent */
  168. NRZ_NORMAL, /* Normal mode */
  169. RT_SERIAL_RB_BUFSZ, /* Buffer size */
  170. 0};
  171. rt_device_open(uart_device, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_DMA_RX | RT_DEVICE_FLAG_DMA_TX);
  172. if (RT_EOK != rt_device_control(uart_device, RT_DEVICE_CTRL_CONFIG, (void *)&use_config))
  173. {
  174. rt_kprintf("uart config baud rate failed.\n");
  175. }
  176. rt_device_set_rx_indicate(uart_device, uart_input);
  177. return;
  178. }
  179. static void virtual_display_init(void)
  180. {
  181. rt_virtual_serial_init(GUIX_VIRTUAL_DISPLAY_SERIAL_DEV);
  182. rt_thread_t thread = RT_NULL;
  183. /* Create background ticks thread */
  184. thread = rt_thread_create("virtual", virtual_serial_thread_entry, RT_NULL, 512, 15, 10);
  185. if (thread != RT_NULL)
  186. {
  187. rt_thread_startup(thread);
  188. }
  189. }
  190. INIT_APP_EXPORT(virtual_display_init);
  191. #endif // GUIX_VIRTUAL_DISPLAY_MONO
  192. /*************************************** (END OF FILE) ****************************************/