osc.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include <rtgui/rtgui.h>
  2. #include <rtgui/image.h>
  3. #include <rtgui/rtgui_system.h>
  4. #include <rtgui/widgets/view.h>
  5. #include <rtgui/widgets/workbench.h>
  6. #define POINT_LENGTH 320
  7. static rt_uint8_t points[POINT_LENGTH];
  8. static rt_uint8_t old_point, fudu = 1;
  9. static rt_uint16_t current_point = 0;
  10. static rtgui_view_t *osc_view = RT_NULL;
  11. static rtgui_timer_t *osc_timer;
  12. #include <math.h>
  13. void osc_timeout(struct rtgui_timer* timer, void* parameter)
  14. {
  15. struct rtgui_dc* dc;
  16. rtgui_color_t saved;
  17. const double PI=3.141592653589793238462643383279;
  18. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(osc_view));
  19. if (dc == RT_NULL) return ;
  20. saved = RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(osc_view));
  21. RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(osc_view)) =
  22. RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(osc_view));
  23. if (current_point != 0)
  24. rtgui_dc_draw_line(dc, current_point - 1, old_point, current_point, points[current_point]);
  25. else
  26. rtgui_dc_draw_point(dc, current_point, points[current_point]);
  27. RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(osc_view)) = saved;
  28. old_point = points[current_point];
  29. points[current_point] = 100 * sin((current_point * 4 * fudu) * PI / POINT_LENGTH) + 100;
  30. if (current_point != 0)
  31. rtgui_dc_draw_line(dc, current_point - 1, points[current_point - 1], current_point, points[current_point]);
  32. else
  33. rtgui_dc_draw_point(dc, current_point, points[current_point]);
  34. current_point ++;
  35. if (current_point == POINT_LENGTH)
  36. {
  37. current_point = 0;
  38. fudu ++;
  39. if (fudu == 4) fudu = 1;
  40. }
  41. rtgui_dc_end_drawing(dc);
  42. }
  43. static rt_bool_t osc_view_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
  44. {
  45. switch (event->type)
  46. {
  47. case RTGUI_EVENT_PAINT:
  48. {
  49. struct rtgui_dc* dc;
  50. struct rtgui_rect rect;
  51. int index;
  52. dc = rtgui_dc_begin_drawing(widget);
  53. if (dc == RT_NULL) return RT_FALSE;
  54. rtgui_widget_get_rect(widget, &rect);
  55. rtgui_dc_fill_rect(dc, &rect);
  56. for (index = 0; index < 320; index ++)
  57. rtgui_dc_draw_point(dc, index, points[index]);
  58. rtgui_dc_end_drawing(dc);
  59. return RT_FALSE;
  60. }
  61. case RTGUI_EVENT_KBD:
  62. {
  63. struct rtgui_event_kbd* ekbd = (struct rtgui_event_kbd*)event;
  64. if ((ekbd->type == RTGUI_KEYDOWN) && (ekbd->key == RTGUIK_RETURN))
  65. {
  66. rtgui_workbench_t* workbench;
  67. /* stop timer */
  68. rtgui_timer_destory(osc_timer);
  69. /* clean points */
  70. rt_memset(points, 0xff, sizeof(points));
  71. current_point = 0;
  72. /* close this view */
  73. workbench = RTGUI_WORKBENCH(RTGUI_WIDGET(osc_view)->parent);
  74. rtgui_workbench_remove_view(workbench, osc_view);
  75. rtgui_view_destroy(osc_view);
  76. osc_view = RT_NULL;
  77. fudu = 0;
  78. return RT_FALSE;
  79. }
  80. }
  81. }
  82. return rtgui_view_event_handler(widget, event);
  83. }
  84. rtgui_view_t *osc_view_create(struct rtgui_workbench* workbench)
  85. {
  86. if (osc_view != RT_NULL)
  87. {
  88. rtgui_view_show(osc_view, RT_FALSE);
  89. }
  90. else
  91. {
  92. /* create picture view */
  93. osc_view = rtgui_view_create("Oscilloscope");
  94. rtgui_widget_set_event_handler(RTGUI_WIDGET(osc_view),
  95. osc_view_event_handler);
  96. rtgui_workbench_add_view(workbench, osc_view);
  97. /* this view can be focused */
  98. RTGUI_WIDGET(osc_view)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE;
  99. rt_memset(points, 0xff, sizeof(points));
  100. osc_timer = rtgui_timer_create(8,
  101. RT_TIMER_FLAG_PERIODIC,
  102. osc_timeout, RT_NULL);
  103. rtgui_timer_start(osc_timer);
  104. }
  105. return osc_view;
  106. }