info.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #include <rtgui/rtgui.h>
  2. #include <rtgui/rtgui_server.h>
  3. #include <rtgui/rtgui_system.h>
  4. #include <rtgui/widgets/view.h>
  5. #include <rtgui/widgets/workbench.h>
  6. #include "adc.h"
  7. #include <rtthread.h>
  8. extern rt_uint16_t adc_value;
  9. static rt_bool_t view_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
  10. {
  11. if(event->type == RTGUI_EVENT_PAINT)
  12. {
  13. struct rtgui_dc* dc;
  14. struct rtgui_rect rect;
  15. dc = rtgui_dc_begin_drawing(widget);
  16. if(dc == RT_NULL)
  17. return RT_FALSE;
  18. rtgui_widget_get_rect(widget, &rect);
  19. rtgui_dc_fill_rect(dc, &rect);
  20. rect.x2 -= 1; rect.y2 -= 1;
  21. rtgui_dc_draw_hline(dc, rect.x1, rect.x2, rect.y1);
  22. rtgui_dc_draw_vline(dc, rect.x1, rect.y1, rect.y2);
  23. rtgui_dc_draw_hline(dc, rect.x1, rect.x2, rect.y2);
  24. rtgui_dc_draw_vline(dc, rect.x2, rect.y1, rect.y2 + 1);
  25. /* shrink border */
  26. rtgui_rect_inflate(&rect, -1);
  27. /* draw text */
  28. rtgui_widget_get_rect(widget, &rect);
  29. rect.x1 += 1;
  30. rect.y1 += 1;
  31. rtgui_dc_draw_text(dc, "FM3 Easy Kit Demo", &rect);
  32. rect.y1 += 10;
  33. rtgui_dc_draw_text(dc, "[rt-thread/RTGUI]", &rect);
  34. rtgui_dc_end_drawing(dc);
  35. return RT_FALSE;
  36. }
  37. else if(event->type == RTGUI_EVENT_KBD)
  38. {
  39. struct rtgui_event_kbd* ekbd = (struct rtgui_event_kbd*)event;
  40. if(ekbd->type == RTGUI_KEYDOWN)
  41. {
  42. char key_str[16];
  43. struct rtgui_dc* dc;
  44. struct rtgui_rect rect;
  45. switch(ekbd->key)
  46. {
  47. case RTGUIK_LEFT:
  48. rt_sprintf(key_str, "KEY = %s", "LEFT");
  49. break;
  50. case RTGUIK_RIGHT:
  51. rt_sprintf(key_str, "KEY = %s", "RIGHT");
  52. break;
  53. case RTGUIK_DOWN:
  54. rt_sprintf(key_str, "KEY = %s", "DOWN");
  55. break;
  56. case RTGUIK_UP:
  57. rt_sprintf(key_str, "KEY = %s", "UP");
  58. break;
  59. default:
  60. rt_sprintf(key_str, "KEY = %s", "UNKNOWN");
  61. break;
  62. }
  63. dc = rtgui_dc_begin_drawing(widget);
  64. if(dc == RT_NULL)
  65. return RT_FALSE;
  66. rect.x1 = 10;
  67. rect.y1 = 30;
  68. rect.x2 = 120;
  69. rect.y2 = 40;
  70. rtgui_dc_fill_rect(dc, &rect);
  71. rtgui_dc_draw_text(dc, key_str, &rect);
  72. rtgui_dc_end_drawing(dc);
  73. }
  74. }
  75. else if(event->type == RTGUI_EVENT_COMMAND)
  76. {
  77. char adc_str[10];
  78. struct rtgui_dc* dc;
  79. struct rtgui_rect rect;
  80. dc = rtgui_dc_begin_drawing(widget);
  81. if(dc == RT_NULL)
  82. return RT_FALSE;
  83. struct rtgui_event_command* ecmd = (struct rtgui_event_command*)event;
  84. switch(ecmd->command_id)
  85. {
  86. case ADC_UPDATE:
  87. rect.x1 = 10;
  88. rect.y1 = 40;
  89. rect.x2 = 120;
  90. rect.y2 = 50;
  91. rtgui_dc_fill_rect(dc, &rect);
  92. rt_sprintf(adc_str, "ADC = %d mv", adc_value);
  93. rtgui_dc_draw_text(dc, adc_str, &rect);
  94. rtgui_dc_end_drawing(dc);
  95. }
  96. }
  97. return rtgui_view_event_handler(widget, event);
  98. }
  99. static void info_entry(void* parameter)
  100. {
  101. rt_mq_t mq;
  102. struct rtgui_view* view;
  103. struct rtgui_workbench* workbench;
  104. mq = rt_mq_create("qInfo", 256, 4, RT_IPC_FLAG_FIFO);
  105. rtgui_thread_register(rt_thread_self(), mq);
  106. workbench = rtgui_workbench_create("info", "workbench");
  107. if(workbench == RT_NULL)
  108. return;
  109. view = rtgui_view_create("view");
  110. RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(view)) = white;
  111. RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(view)) = black;
  112. rtgui_widget_set_event_handler(RTGUI_WIDGET(view), view_event_handler);
  113. rtgui_workbench_add_view(workbench, view);
  114. /* this view can be focused */
  115. RTGUI_WIDGET(view)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE;
  116. /* set widget focus */
  117. rtgui_widget_focus(RTGUI_WIDGET(view));
  118. rtgui_view_show(view, RT_FALSE);
  119. rtgui_workbench_event_loop(workbench);
  120. rtgui_thread_deregister(rt_thread_self());
  121. rt_mq_delete(mq);
  122. }
  123. rt_thread_t info_tid;
  124. void info_init()
  125. {
  126. info_tid = rt_thread_create("info",
  127. info_entry, RT_NULL,
  128. 2048, 26, 10);
  129. if (info_tid != RT_NULL) rt_thread_startup(info_tid);
  130. }
  131. void rtgui_startup()
  132. {
  133. rtgui_rect_t rect;
  134. /* GUIϵͳ³õʼ»¯ */
  135. rtgui_system_server_init();
  136. /* register dock panel */
  137. rect.x1 = 0;
  138. rect.y1 = 0;
  139. rect.x2 = 128;
  140. rect.y2 = 64;
  141. rtgui_panel_register("info", &rect);
  142. rtgui_panel_set_default_focused("info");
  143. /* Æô¶¯info workbench */
  144. info_init();
  145. }