device_info.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #include <rtgui/rtgui.h>
  2. #include <rtgui/rtgui_system.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <rtgui/widgets/view.h>
  6. #include <rtgui/widgets/workbench.h>
  7. #include <stm32f10x_dbgmcu.h>
  8. #ifdef RT_USING_LWIP
  9. #include <lwip/err.h>
  10. #include <lwip/dns.h>
  11. #include <lwip/netif.h>
  12. #endif
  13. static struct rtgui_view* device_view = RT_NULL;
  14. int get_thread_cnt()
  15. {
  16. int cnt = 0;
  17. struct rt_list_node *list, *node;
  18. extern struct rt_object_information rt_object_container[];
  19. list = &rt_object_container[RT_Object_Class_Thread].object_list;
  20. for (node = list->next; node != list; node = node->next)
  21. {
  22. cnt ++;
  23. }
  24. return cnt;
  25. }
  26. const static char *stm32_devname[] =
  27. {
  28. "STM32 MD", /* 0x410 */
  29. "STM32 LD", /* 0x412 */
  30. "STM32 HD", /* 0x414 */
  31. "Unknown" , /* 0x416 */
  32. "STM32 CL", /* 0x418 */
  33. };
  34. const static char *stm32_revname[] =
  35. {
  36. "Rev A",
  37. "Rev B",
  38. "Rev Z",
  39. "Rev Y",
  40. "Rev Unknown"
  41. };
  42. /*
  43. * Device Information View
  44. * Device: Win32 or Cortex-M3 etc
  45. * Memory:
  46. * Thread:
  47. * IP Address:
  48. * Gateway:
  49. * DNS:
  50. */
  51. static rt_bool_t view_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
  52. {
  53. switch (event->type)
  54. {
  55. case RTGUI_EVENT_PAINT:
  56. {
  57. struct rtgui_dc* dc;
  58. struct rtgui_rect rect;
  59. char* line;
  60. rt_uint32_t total, used, max_used;
  61. line = rtgui_malloc(256);
  62. if (line == RT_NULL) return RT_FALSE;
  63. dc = rtgui_dc_begin_drawing(widget);
  64. if (dc == RT_NULL)
  65. {
  66. rtgui_free(line);
  67. return RT_FALSE;
  68. }
  69. rtgui_widget_get_rect(widget, &rect);
  70. /* fill background */
  71. rtgui_dc_fill_rect(dc, &rect);
  72. rect.y2 = rect.y1 + 18;
  73. {
  74. rt_uint32_t dev_index, rev_index;
  75. dev_index = DBGMCU_GetDEVID();
  76. dev_index = (dev_index - 0x410)/2;
  77. rev_index = DBGMCU_GetREVID();
  78. switch (rev_index)
  79. {
  80. case 0x1000:
  81. case 0x0000:
  82. rev_index = 0; /* Revision A */
  83. break;
  84. case 0x1001:
  85. case 0x2001:
  86. rev_index = 3; /* Revision Z */
  87. break;
  88. case 0x2000:
  89. rev_index = 1; /* Revision B */
  90. break;
  91. case 0x2002:
  92. rev_index = 2; /* Revision Y */
  93. break;
  94. default:
  95. rev_index = 4; /* Unknown */
  96. break;
  97. };
  98. /* check device index */
  99. if (dev_index < 0 || dev_index > 4) dev_index = 3;
  100. /* draw each information */
  101. sprintf(line, "设备: %s %s",
  102. stm32_devname[dev_index],
  103. stm32_revname[rev_index]);
  104. rtgui_dc_draw_text(dc, line, &rect); rect.y1 += 16; rect.y2 += 16;
  105. }
  106. rt_memory_info(&total, &used, &max_used);
  107. sprintf(line, "内存: 当前使用 %d 字节", used);
  108. rtgui_dc_draw_text(dc, line, &rect); rect.y1 += 16; rect.y2 += 16;
  109. {
  110. rt_uint16_t rect_width;
  111. rtgui_color_t saved;
  112. rtgui_rect_t mem_rect = rect;
  113. rtgui_rect_inflate(&mem_rect, -2);
  114. rtgui_dc_draw_rect(dc, &mem_rect);
  115. rtgui_rect_inflate(&mem_rect, -1);
  116. rect_width = rtgui_rect_width(mem_rect);
  117. saved = RTGUI_WIDGET_BACKGROUND(widget);
  118. RTGUI_WIDGET_BACKGROUND(widget) = light_grey;
  119. mem_rect.x2 = mem_rect.x1 + (max_used * rect_width / total);
  120. rtgui_dc_fill_rect(dc, &mem_rect);
  121. RTGUI_WIDGET_BACKGROUND(widget) = blue;
  122. mem_rect.x2 = mem_rect.x1 + (used * rect_width / total);
  123. rtgui_dc_fill_rect(dc, &mem_rect);
  124. /* restore color */
  125. RTGUI_WIDGET_BACKGROUND(widget) = saved;
  126. }
  127. rect.y1 += 18; rect.y2 += 18;
  128. sprintf(line, "线程数: %d", get_thread_cnt());
  129. rtgui_dc_draw_text(dc, line, &rect); rect.y1 += 16; rect.y2 += 16;
  130. #ifdef RT_USING_LWIP
  131. {
  132. struct ip_addr ip_addr;
  133. struct _ip_addr
  134. {
  135. rt_uint8_t addr0, addr1, addr2, addr3;
  136. } *addr;
  137. addr = (struct _ip_addr*)&netif_default->ip_addr.addr;
  138. sprintf(line, "IP地址 : %d.%d.%d.%d", addr->addr0, addr->addr1, addr->addr2, addr->addr3);
  139. rtgui_dc_draw_text(dc, line, &rect); rect.y1 += 16; rect.y2 += 16;
  140. addr = (struct _ip_addr*)&netif_default->gw.addr;
  141. sprintf(line, "网关地址: %d.%d.%d.%d", addr->addr0, addr->addr1, addr->addr2, addr->addr3);
  142. rtgui_dc_draw_text(dc, line, &rect); rect.y1 += 16; rect.y2 += 16;
  143. addr = (struct _ip_addr*)&netif_default->netmask.addr;
  144. sprintf(line, "网络掩码: %d.%d.%d.%d", addr->addr0, addr->addr1, addr->addr2, addr->addr3);
  145. rtgui_dc_draw_text(dc, line, &rect); rect.y1 += 16; rect.y2 += 16;
  146. #if LWIP_DNS
  147. ip_addr = dns_getserver(0);
  148. addr = (struct _ip_addr*)&ip_addr;
  149. sprintf(line, "DNS地址 : %d.%d.%d.%d", addr->addr0, addr->addr1, addr->addr2, addr->addr3);
  150. rtgui_dc_draw_text(dc, line, &rect); rect.y1 += 16; rect.y2 += 16;
  151. #endif
  152. }
  153. #endif
  154. rtgui_dc_end_drawing(dc);
  155. rtgui_free(line);
  156. return RT_FALSE;
  157. }
  158. case RTGUI_EVENT_KBD:
  159. {
  160. struct rtgui_event_kbd* ekbd;
  161. ekbd = (struct rtgui_event_kbd*)event;
  162. if (ekbd->type == RTGUI_KEYDOWN && ekbd->key == RTGUIK_RETURN)
  163. {
  164. rtgui_workbench_t* workbench;
  165. workbench = RTGUI_WORKBENCH(RTGUI_WIDGET(device_view)->parent);
  166. rtgui_workbench_remove_view(workbench, device_view);
  167. rtgui_view_destroy(device_view);
  168. device_view = RT_NULL;
  169. }
  170. }
  171. return RT_FALSE;
  172. }
  173. /* use parent event handler */
  174. return rtgui_view_event_handler(widget, event);
  175. }
  176. rtgui_view_t *device_view_create(rtgui_workbench_t* workbench)
  177. {
  178. if (device_view != RT_NULL)
  179. {
  180. rtgui_view_show(device_view, RT_FALSE);
  181. }
  182. else
  183. {
  184. /* create a view */
  185. device_view = rtgui_view_create("Device Info");
  186. /* set view event handler */
  187. rtgui_widget_set_event_handler(RTGUI_WIDGET(device_view), view_event_handler);
  188. /* this view can be focused */
  189. RTGUI_WIDGET(device_view)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE;
  190. /* add view to workbench */
  191. rtgui_workbench_add_view(workbench, device_view);
  192. }
  193. return device_view;
  194. }