application.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * File : application.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2009 - 2011, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2011-02-24 Bernard the first version
  13. */
  14. /**
  15. * @addtogroup FM3
  16. */
  17. /*@{*/
  18. #include <rtthread.h>
  19. #include "board.h"
  20. #include "led.h"
  21. #include "key.h"
  22. #include "adc.h"
  23. #include "lcd.h"
  24. #include "cpuusage.h"
  25. #ifdef RT_USING_RTGUI
  26. #include <rtgui/rtgui.h>
  27. #include <rtgui/driver.h>
  28. extern void rtgui_startup();
  29. #endif
  30. struct rt_messagequeue mq;
  31. static char msg_pool[2048];
  32. void rt_init_thread_entry(void *parameter)
  33. {
  34. rt_device_t lcd;
  35. rt_hw_led_init();
  36. rt_hw_key_init();
  37. rt_hw_adc_init();
  38. rt_hw_lcd_init();
  39. rt_hw_cpu_init();
  40. /* re-init device driver */
  41. rt_device_init_all();
  42. #ifdef RT_USING_RTGUI
  43. /* find lcd device */
  44. lcd = rt_device_find("lcd");
  45. /* set lcd device as rtgui graphic driver */
  46. rtgui_graphic_set_device(lcd);
  47. /* startup rtgui */
  48. rtgui_startup();
  49. #else
  50. {
  51. char buf[20] = {'\0'};
  52. struct lcd_msg msg;
  53. rt_device_t device;
  54. device = rt_device_find("lcd");
  55. rt_device_control(device, RT_DEVICE_CTRL_LCD_CLEAR_SCR, RT_NULL);
  56. x = 1;
  57. y = 1;
  58. rt_device_control(device, RT_DEVICE_CTRL_LCD_PUT_STRING, "ADC");
  59. x = 1;
  60. y = 20;
  61. rt_device_control(device, RT_DEVICE_CTRL_LCD_PUT_STRING, "CPU");
  62. x = 1;
  63. y = 40;
  64. rt_device_control(device, RT_DEVICE_CTRL_LCD_PUT_STRING, "KEY");
  65. while(1)
  66. {
  67. if (rt_mq_recv(&mq, &msg, sizeof(msg), RT_WAITING_FOREVER) == RT_EOK)
  68. {
  69. switch(msg.type)
  70. {
  71. case ADC_MSG:
  72. x = 40;
  73. y = 1;
  74. rt_memset(buf, 0, sizeof(buf));
  75. rt_sprintf(buf, "%04d", msg.adc_value);
  76. rt_device_control(device, RT_DEVICE_CTRL_LCD_PUT_STRING, buf);
  77. break;
  78. case CPU_MSG:
  79. x = 40;
  80. y = 20;
  81. rt_memset(buf, 0, sizeof(buf));
  82. rt_sprintf(buf, "%03d %03d", msg.major, msg.minor);
  83. rt_device_control(device, RT_DEVICE_CTRL_LCD_PUT_STRING, buf);
  84. break;
  85. case KEY_MSG:
  86. x = 40;
  87. y = 40;
  88. rt_memset(buf, 0, sizeof(buf));
  89. switch(msg.key)
  90. {
  91. case KEY_DOWN:
  92. rt_sprintf(buf, "DOWN KEY ");
  93. break;
  94. case KEY_UP:
  95. rt_sprintf(buf, "UP KEY ");
  96. break;
  97. case KEY_RIGHT:
  98. rt_sprintf(buf, "RIGHT KEY");
  99. break;
  100. case KEY_LEFT:
  101. rt_sprintf(buf, "LEFT KEY ");
  102. break;
  103. case KEY_ENTER:
  104. rt_sprintf(buf, "ENTER KEY");
  105. break;
  106. default:
  107. rt_sprintf(buf, "NO KEY ");
  108. break;
  109. }
  110. rt_device_control(device, RT_DEVICE_CTRL_LCD_PUT_STRING, buf);
  111. break;
  112. }
  113. }
  114. }
  115. }
  116. #endif
  117. }
  118. int rt_application_init()
  119. {
  120. rt_thread_t init_thread;
  121. rt_mq_init(&mq, "mqt", &msg_pool[0], 128 - sizeof(void*), sizeof(msg_pool), RT_IPC_FLAG_FIFO);
  122. init_thread = rt_thread_create("init", rt_init_thread_entry, RT_NULL, 1024, 21, 20);
  123. if(init_thread != RT_NULL)
  124. rt_thread_startup(init_thread);
  125. return 0;
  126. }
  127. /*@}*/