application.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  1. /***************************************************************************//**
  2. * @file application.c
  3. * @brief Demo application
  4. * COPYRIGHT (C) 2012, RT-Thread Development Team
  5. * @author Bernard, onelife
  6. * @version 1.0
  7. *******************************************************************************
  8. * @section License
  9. * The license and distribution terms for this file may be found in the file
  10. * LICENSE in this distribution or at http://www.rt-thread.org/license/LICENSE
  11. *******************************************************************************
  12. * @section Change Logs
  13. * Date Author Notes
  14. * 2009-01-05 Bernard first version
  15. * 2010-12-29 onelife Modify for EFM32
  16. * 2011-05-06 onelife Add SPI Flash DEMO
  17. * 2011-07-15 onelife Add accelerometer DEMO
  18. * 2011-07-27 onelife Modify Ethernet DEMO
  19. * 2011-08-23 onelife Modify Ethernet DEMO according to the changes of
  20. * lwIP API in reversion 1668
  21. * 2011-12-20 onelife Add LCD DEMO
  22. * 2012-02-16 onelife Add photo frame DEMO
  23. * 2012-xx-xx onelife Add low energy test code
  24. * 2012-05-17 onelife Modify photo frame DEMO according to new version of
  25. * RTGUI
  26. ******************************************************************************/
  27. /***************************************************************************//**
  28. * @addtogroup efm32
  29. * @{
  30. ******************************************************************************/
  31. /* Includes ------------------------------------------------------------------*/
  32. #include <board.h>
  33. #if defined(RT_USING_DFS)
  34. /* dfs filesystem:ELM filesystem init */
  35. #include <dfs_elm.h>
  36. /* dfs Filesystem APIs */
  37. #include <dfs_fs.h>
  38. #endif
  39. #include "dev_led.h"
  40. #if defined(EFM32_USING_ACCEL)
  41. #include "dev_accel.h"
  42. #endif
  43. #if defined(EFM32_USING_SFLASH)
  44. #include "dev_sflash.h"
  45. #endif
  46. #if defined(EFM32_USING_SPISD)
  47. #include "drv_sdcard.h"
  48. #endif
  49. #if defined(EFM32_USING_ETHERNET)
  50. #include "drv_ethernet.h"
  51. #endif
  52. #if defined(EFM32_USING_LCD)
  53. #include "dev_lcd.h"
  54. #include <rtgui/rtgui_server.h>
  55. #include <rtgui/rtgui_system.h>
  56. #include <rtgui/rtgui_app.h>
  57. #include <rtgui/widgets/widget.h>
  58. #include <rtgui/widgets/label.h>
  59. #include <rtgui/widgets/window.h>
  60. #include <rtgui/widgets/box.h>
  61. #include <rtgui/image.h>
  62. #if defined(RTGUI_USING_DFS_FILERW)
  63. #include <dfs_posix.h>
  64. #define PATH_SEPARATOR '/'
  65. #endif
  66. #if defined(RTGUI_USING_DFS_FILERW)
  67. #include <dfs_file.h>
  68. #include <unistd.h>
  69. #include <stdio.h>
  70. #include <sys/stat.h>
  71. #include <sys/statfs.h>
  72. #define PATH_SEPARATOR '/'
  73. #endif
  74. #endif
  75. /* Private typedef -----------------------------------------------------------*/
  76. #if defined(RT_USING_RTGUI)
  77. struct photo_event
  78. {
  79. struct rtgui_event_win win;
  80. rt_uint32_t cmd;
  81. rt_uint8_t* path;
  82. rt_uint8_t* format;
  83. };
  84. /* Private defines -----------------------------------------------------------*/
  85. #define APP_CMD_PHOTO_FRAME 0x00000001
  86. #define APP_PHOTO_FRAME
  87. #endif
  88. /* Private macro -------------------------------------------------------------*/
  89. /* Private variables ---------------------------------------------------------*/
  90. volatile rt_uint32_t rt_system_status = 0;
  91. /* Private function prototypes -----------------------------------------------*/
  92. /* Private functions ---------------------------------------------------------*/
  93. #if defined(RT_USING_RTGUI)
  94. static rt_bool_t pic_view_event_handler(rtgui_object_t *object, rtgui_event_t *event)
  95. {
  96. rt_bool_t result;
  97. rt_bool_t load = RT_FALSE;
  98. result = rtgui_container_event_handler(object, event);
  99. switch(event->type)
  100. {
  101. case RTGUI_EVENT_PAINT:
  102. load = RT_TRUE;
  103. break;
  104. case RTGUI_EVENT_MOUSE_BUTTON:
  105. {
  106. struct rtgui_event_mouse *mouse = (struct rtgui_event_mouse *)event;
  107. if (mouse->button == RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_UP)
  108. {
  109. rt_kprintf("APP: left click (%x)\n", mouse->button);
  110. }
  111. }
  112. break;
  113. }
  114. if (load)
  115. {
  116. struct rtgui_dc* dc;
  117. rtgui_rect_t rect;
  118. rtgui_image_t* image;
  119. image = rtgui_image_create_from_file("jpg", "/test9.jpg", RT_FALSE);
  120. // image = rtgui_image_create_from_file("bmp", "/test_565.bmp", RT_FALSE);
  121. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(object));
  122. if (dc == RT_NULL)
  123. {
  124. return result;
  125. }
  126. rtgui_widget_get_rect(RTGUI_WIDGET(object), &rect);
  127. // rtgui_widget_rect_to_device(widget, &rect);
  128. rect.x1 +=10;
  129. rect.y1 +=10;
  130. if (image != RT_NULL)
  131. {
  132. rtgui_image_blit(image, dc, &rect);
  133. rtgui_image_destroy(image);
  134. }
  135. else
  136. {
  137. rt_kprintf("APP err: no image found!\n");
  138. }
  139. rtgui_dc_end_drawing(dc, RT_TRUE);
  140. }
  141. return result;
  142. }
  143. static void app_main(void *parameter)
  144. {
  145. /* find lcd device */
  146. rt_device_t lcd = rt_device_find(LCD_DEVICE_NAME);
  147. if (lcd == RT_NULL)
  148. {
  149. rt_kprintf("Can't find LCD\n");
  150. return;
  151. }
  152. /* read LCD info */
  153. struct rt_device_graphic_info lcd_info;
  154. lcd->control(lcd, RTGRAPHIC_CTRL_GET_INFO, (void *)&lcd_info);
  155. rt_kprintf("LCD size: %dX%d\n", lcd_info.width, lcd_info.height);
  156. /* create application */
  157. struct rtgui_app *app;
  158. app = rtgui_app_create("gui_app");
  159. if (app == RT_NULL)
  160. {
  161. rt_kprintf("Create application \"gui_app\" failed!\n");
  162. return;
  163. }
  164. struct rtgui_rect rect1, rect2, rect3;
  165. struct rtgui_win *win_info, *win_main, *win_hello;
  166. struct rtgui_container *container;
  167. struct rtgui_label* label;
  168. rtgui_graphic_driver_get_rect(rtgui_graphic_driver_get_default(), &rect1);
  169. rect2.x1 = rect1.x1;
  170. rect2.y1 = 25;
  171. rect2.x2 = rect1.x2;
  172. rect2.y2 = rect1.y2;
  173. rect1.y2 = 25;
  174. /* create info window */
  175. win_info = rtgui_win_create(RT_NULL, "info",
  176. &rect1,
  177. RTGUI_WIN_STYLE_NO_BORDER | RTGUI_WIN_STYLE_NO_TITLE);
  178. if (win_info == RT_NULL)
  179. {
  180. rt_kprintf("Create window \"info\" failed!\n");
  181. rtgui_app_destroy(app);
  182. return;
  183. }
  184. /* create container in info window */
  185. container = rtgui_container_create();
  186. if (container == RT_NULL)
  187. {
  188. rt_kprintf("Create container failed!\n");
  189. return;
  190. }
  191. rtgui_widget_set_rect(RTGUI_WIDGET(container), &rect1);
  192. rtgui_container_add_child(RTGUI_CONTAINER(win_info), RTGUI_WIDGET(container));
  193. /* create lable in info window */
  194. label = rtgui_label_create("RT-Thread & RTGUI");
  195. if (label == RT_NULL)
  196. {
  197. rt_kprintf("Create lable failed!\n");
  198. return;
  199. }
  200. RTGUI_WIDGET_TEXTALIGN(RTGUI_WIDGET(label)) = RTGUI_ALIGN_LEFT;
  201. RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(label)) = red;
  202. RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(label)) = white;
  203. rect3.x1 = rect1.x1 + 5;
  204. rect3.y1 = rect1.y1 + 5;
  205. rect3.x2 = rect1.x2 - 5;
  206. rect3.y2 = rect1.y2 - 5;
  207. rtgui_widget_set_rect(RTGUI_WIDGET(label), &rect3);
  208. rtgui_container_add_child(container, RTGUI_WIDGET(label));
  209. /* create main window */
  210. win_main = rtgui_win_create(RT_NULL, "main",
  211. &rect2,
  212. RTGUI_WIN_STYLE_NO_BORDER | RTGUI_WIN_STYLE_NO_TITLE);
  213. if (win_main == RT_NULL)
  214. {
  215. rt_kprintf("Create window \"main\" failed!\n");
  216. rtgui_app_destroy(app);
  217. return;
  218. }
  219. /* create container in main window */
  220. container = rtgui_container_create();
  221. if (container == RT_NULL)
  222. {
  223. rt_kprintf("Create container failed!\n");
  224. return;
  225. }
  226. rtgui_widget_set_rect(RTGUI_WIDGET(container), &rect2);
  227. rtgui_object_set_event_handler(RTGUI_OBJECT(container), pic_view_event_handler);
  228. rtgui_container_add_child(RTGUI_CONTAINER(win_main), RTGUI_WIDGET(container));
  229. /* create lable in main window */
  230. label = rtgui_label_create("EFM32GG_DK3750 Kit");
  231. if (label == RT_NULL)
  232. {
  233. rt_kprintf("Create lable failed!\n");
  234. return;
  235. }
  236. RTGUI_WIDGET_TEXTALIGN(RTGUI_WIDGET(label)) = RTGUI_ALIGN_LEFT;
  237. RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(label)) = white;
  238. RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(label)) = blue;
  239. rect3.x1 = rect2.x1 + 5;
  240. rect3.y1 = rect2.y1 + 5;
  241. rect3.x2 = rect2.x2 - 5;
  242. rect3.y2 = rect2.y1 + 20;
  243. rtgui_widget_set_rect(RTGUI_WIDGET(label), &rect3);
  244. rtgui_container_add_child(container, RTGUI_WIDGET(label));
  245. /* create hello window */
  246. rect3.x1 = 80;
  247. rect3.y1 = 50;
  248. rect3.x2 = 320 - 80;
  249. rect3.y2 = 240 - 50;
  250. win_hello = rtgui_win_create(RT_NULL, "hello",
  251. &rect3,
  252. RTGUI_WIN_STYLE_DEFAULT);
  253. if (win_hello == RT_NULL)
  254. {
  255. rt_kprintf("Create window \"hello\" failed!\n");
  256. rtgui_app_destroy(app);
  257. return;
  258. }
  259. /* create a box */
  260. rtgui_box_t *box = rtgui_box_create(RTGUI_VERTICAL, RT_NULL);
  261. if(box == RT_NULL)
  262. {
  263. rt_kprintf("Create box failed!\n");
  264. return;
  265. }
  266. // rtgui_win_set_box(win_hello, box);
  267. label = rtgui_label_create("哈罗,盹胖!");
  268. if(label == RT_NULL)
  269. {
  270. rt_kprintf("Create lable failed!\n");
  271. return;
  272. }
  273. RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(label)) = white;
  274. RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(label)) = black;
  275. RTGUI_WIDGET(label)->align = RTGUI_ALIGN_CENTER_HORIZONTAL | RTGUI_ALIGN_CENTER_VERTICAL;
  276. rtgui_widget_set_miniwidth(RTGUI_WIDGET(label),130);
  277. rtgui_box_append(box, RTGUI_WIDGET(label));
  278. /* Auto layout */
  279. rtgui_box_layout(box);
  280. rtgui_win_show(win_info, RT_FALSE);
  281. rtgui_win_show(win_main, RT_FALSE);
  282. rtgui_win_show(win_hello, RT_FALSE);
  283. rtgui_app_run(app);
  284. rtgui_app_destroy(app);
  285. }
  286. static rt_bool_t photo_view_event_handler(rtgui_object_t *object, rtgui_event_t *event)
  287. {
  288. rt_bool_t result = RT_FALSE;
  289. struct photo_event *photo_event = (struct photo_event *)event;
  290. result = rtgui_container_event_handler(object, event);
  291. rt_kprintf("container event %x\n", event->type);
  292. struct rtgui_event_win* wevent = (struct rtgui_event_win*)event;
  293. rt_kprintf("wevent->wid %x\n", wevent->wid);
  294. if ((event->type == RTGUI_EVENT_COMMAND) && \
  295. (photo_event->cmd == APP_CMD_PHOTO_FRAME))
  296. {
  297. rtgui_rect_t rect;
  298. rtgui_image_t* image;
  299. struct rtgui_dc* dc;
  300. rtgui_widget_get_rect(RTGUI_WIDGET(object), &rect);
  301. rt_kprintf(" (%d, %d) (%d, %d)\n", rect.x1, rect.y1, rect.x2, rect.y2);
  302. // rtgui_widget_rect_to_device(RTGUI_WIDGET(object), &rect);
  303. rect.y1 +=15;
  304. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(object));
  305. if (dc == RT_NULL)
  306. {
  307. return result;
  308. }
  309. image = rtgui_image_create_from_file(photo_event->format,
  310. photo_event->path, RT_TRUE);
  311. if (image != RT_NULL)
  312. {
  313. rtgui_image_blit(image, dc, &rect);
  314. rtgui_image_destroy(image);
  315. return result;
  316. }
  317. return RT_TRUE;
  318. }
  319. return result;
  320. }
  321. static rt_bool_t photo_lable_event_handler(rtgui_object_t *object, rtgui_event_t *event)
  322. {
  323. rt_bool_t result = RT_FALSE;
  324. result = rtgui_label_event_handler(object, event);
  325. rt_kprintf("lable event %x\n", event->type);
  326. if (event->type == RTGUI_EVENT_COMMAND)
  327. {
  328. struct photo_event *photo = (struct photo_event *)event;
  329. rtgui_label_set_text((rtgui_label_t *)object, photo->path);
  330. rt_kprintf("path %s\n", photo->path);
  331. }
  332. return result;
  333. }
  334. static void app_photo(void *parameter)
  335. {
  336. struct photo_event *event = (struct photo_event *)parameter;
  337. /* find lcd device */
  338. rt_device_t lcd = rt_device_find(LCD_DEVICE_NAME);
  339. if (lcd == RT_NULL)
  340. {
  341. rt_kprintf("Can't find LCD\n");
  342. return;
  343. }
  344. /* read LCD info */
  345. struct rt_device_graphic_info lcd_info;
  346. lcd->control(lcd, RTGRAPHIC_CTRL_GET_INFO, (void *)&lcd_info);
  347. rt_kprintf("LCD size: %dX%d\n", lcd_info.width, lcd_info.height);
  348. /* create application */
  349. struct rtgui_app *app;
  350. app = rtgui_app_create("pho_app");
  351. if (app == RT_NULL)
  352. {
  353. rt_kprintf("Create application \"pho_app\" failed!\n");
  354. return;
  355. }
  356. struct rtgui_rect rect1, rect2;
  357. struct rtgui_win *window;
  358. struct rtgui_container *container;
  359. struct rtgui_label* label;
  360. rtgui_graphic_driver_get_rect(rtgui_graphic_driver_get_default(), &rect1);
  361. /* create window */
  362. window = rtgui_win_create(RT_NULL, "photo",
  363. &rect1,
  364. RTGUI_WIN_STYLE_NO_BORDER | RTGUI_WIN_STYLE_NO_TITLE);
  365. if (window == RT_NULL)
  366. {
  367. rt_kprintf("Create window \"photo\" failed!\n");
  368. rtgui_app_destroy(app);
  369. return;
  370. }
  371. event->win.wid = window;
  372. /* create container */
  373. container = rtgui_container_create();
  374. if (container == RT_NULL)
  375. {
  376. rt_kprintf("Create container failed!\n");
  377. return;
  378. }
  379. rtgui_widget_set_rect(RTGUI_WIDGET(container), &rect1);
  380. rtgui_object_set_event_handler(RTGUI_OBJECT(container), photo_view_event_handler);
  381. rtgui_container_add_child(RTGUI_CONTAINER(window), RTGUI_WIDGET(container));
  382. /* create lable in info window */
  383. label = rtgui_label_create("Photo Frame Demo");
  384. if (label == RT_NULL)
  385. {
  386. rt_kprintf("Create lable failed!\n");
  387. return;
  388. }
  389. RTGUI_WIDGET_TEXTALIGN(RTGUI_WIDGET(label)) = RTGUI_ALIGN_LEFT;
  390. RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(label)) = white;
  391. RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(label)) = blue;
  392. rect2.x1 = rect1.x1;
  393. rect2.y1 = rect1.y1;
  394. rect2.x2 = rect1.x2;
  395. rect2.y2 = 15;
  396. rtgui_widget_set_rect(RTGUI_WIDGET(label), &rect2);
  397. rtgui_object_set_event_handler(RTGUI_OBJECT(label), photo_lable_event_handler);
  398. rtgui_container_add_child(container, RTGUI_WIDGET(label));
  399. rtgui_win_show(window, RT_FALSE);
  400. rtgui_app_run(app);
  401. rtgui_app_destroy(app);
  402. }
  403. #endif
  404. void rt_demo_thread_entry(void* parameter)
  405. {
  406. emu_all_disable();
  407. #if 0 //defined(EFM32_USING_ACCEL)
  408. {
  409. struct efm32_accel_result_t result;
  410. rt_kprintf(">>> waiting\n");
  411. rt_thread_sleep(6000);
  412. rt_kprintf(">>> start\n");
  413. while(1)
  414. {
  415. efm_accel_get_data(&result);
  416. rt_kprintf("Accel x: %x\n", result.x);
  417. rt_kprintf("Accel y: %x\n", result.y);
  418. rt_kprintf("Accel z: %x\n\n", result.z);
  419. rt_thread_sleep(200);
  420. }
  421. }
  422. #endif
  423. #if defined(RT_USING_DFS)
  424. {
  425. rt_kprintf("File system DEMO start...\n");
  426. /* Filesystem Initialization */
  427. dfs_init();
  428. #if defined(RT_USING_DFS_ELMFAT)
  429. /* init the elm chan FatFs filesystam*/
  430. elm_init();
  431. #if defined(EFM32_USING_SPISD)
  432. /* mount sd card fat partition 1 as root directory */
  433. if (dfs_mount(SPISD_DEVICE_NAME, "/", "elm", 0, 0) == 0)
  434. {
  435. rt_kprintf("FatFs init OK\n");
  436. }
  437. else
  438. {
  439. rt_kprintf("FatFs init failed!\n");
  440. }
  441. #endif
  442. #endif
  443. rt_kprintf("File system DEMO end.\n");
  444. }
  445. #endif
  446. #ifdef EFM32_USING_SFLASH
  447. {
  448. rt_kprintf("SPI Flash DEMO start...\n");
  449. rt_uint8_t i;
  450. rt_uint8_t test[] = "123456789ABCDEF";
  451. rt_uint8_t buf[30], buf2[30];
  452. efm_spiFlash_cmd(sflash_inst_rdid_l, EFM32_NO_DATA, buf, sizeof(buf));
  453. rt_kprintf("Manuf ID: %x\n", buf[0]);
  454. rt_kprintf("Memory type: %x\n", buf[1]);
  455. rt_kprintf("Memory capacity: %x\n", buf[2]);
  456. rt_kprintf("CFD length: %x\n", buf[3]);
  457. rt_kprintf("CFD: %x%x%x...%x%x\n", buf[4], buf[5], buf[6], buf[18], buf[19]);
  458. efm_spiFlash_cmd(sflash_inst_wren, EFM32_NO_DATA, EFM32_NO_POINTER, EFM32_NO_DATA);
  459. do
  460. {
  461. efm_spiFlash_cmd(sflash_inst_rdsr, EFM32_NO_DATA, buf2, sizeof(buf2));
  462. rt_kprintf("Status: %x\n", buf2[0]);
  463. } while (buf2[0] == 0xFF);
  464. rt_kprintf("Status: %x\n", buf2[0]);
  465. //efm_spiFash_cmd(sflash_inst_pp, 0x000003F8, test, sizeof(test) - 1);
  466. efm_spiFlash_cmd(sflash_inst_rdsr, EFM32_NO_DATA, buf2, sizeof(buf2));
  467. rt_kprintf("Status: %x\n", buf2[0]);
  468. efm_spiFlash_cmd(sflash_inst_read, 0x00000300, buf, sizeof(buf));
  469. rt_kprintf("READ: \n");
  470. for (i = 0; i < sizeof(buf); i++)
  471. {
  472. rt_kprintf("%c\n", buf[i]);
  473. }
  474. //efm_spiFlash_deinit();
  475. rt_kprintf("SPI Flash DEMO end.\n");
  476. }
  477. #endif
  478. #if defined(EFM32_USING_ETHERNET)
  479. {
  480. rt_kprintf("Ethernet DEMO start...\n");
  481. extern void lwip_sys_init(void);
  482. /* init lwip system */
  483. lwip_sys_init();
  484. rt_kprintf("TCP/IP stack init OK!\n");
  485. #if defined(EFM32_USING_ETH_HTTPD)
  486. extern void httpd_init(void);
  487. /* init http server */
  488. httpd_init();
  489. rt_kprintf("Http service init OK!\n");
  490. #endif /* defined(EFM32_USING_ETH_HTTPD) */
  491. rt_kprintf("Ethernet DEMO end.\n");
  492. }
  493. #endif /* defined(EFM32_USING_ETHERNET) */
  494. #if (defined(EFM32_USING_LCD) && !defined(APP_PHOTO_FRAME))
  495. {
  496. rt_kprintf("LCD DEMO start...\n");
  497. /* Create RTGUI application thread */
  498. rt_thread_t gui_app;
  499. gui_app = rt_thread_create(
  500. "gui_thd",
  501. app_main,
  502. RT_NULL,
  503. 2048,
  504. 25,
  505. 10);
  506. if (gui_app != RT_NULL)
  507. {
  508. rt_thread_startup(gui_app);
  509. }
  510. else
  511. {
  512. rt_kprintf("Create gui_app thread failed!\n");
  513. }
  514. rt_kprintf("LCD DEMO end.\n");
  515. }
  516. #endif
  517. #if defined(APP_PHOTO_FRAME)
  518. {
  519. rt_kprintf("Photo frame DEMO start...\n");
  520. DIR* dir = opendir("/photo");
  521. struct photo_event event;
  522. struct dirent* dirent;
  523. rt_uint8_t path[100];
  524. const rt_uint8_t bmp[] = "bmp";
  525. const rt_uint8_t jpeg[] = "jpeg";
  526. RTGUI_EVENT_COMMAND_INIT(&event.win);
  527. event.cmd = APP_CMD_PHOTO_FRAME;
  528. event.path = path;
  529. /* find lcd device */
  530. rt_device_t lcd = rt_device_find(LCD_DEVICE_NAME);
  531. if (lcd == RT_NULL)
  532. {
  533. rt_kprintf("Can't find LCD\n");
  534. }
  535. /* read LCD info */
  536. struct rt_device_graphic_info lcd_info;
  537. lcd->control(lcd, RTGRAPHIC_CTRL_GET_INFO, (void *)&lcd_info);
  538. rt_kprintf("LCD size: %dX%d\n", lcd_info.width, lcd_info.height);
  539. /* Create photo frame thread */
  540. rt_thread_t photo_app;
  541. photo_app = rt_thread_create(
  542. "pho_thd",
  543. app_photo,
  544. (void *)&event,
  545. 2048,
  546. 25,
  547. 10);
  548. if (photo_app != RT_NULL)
  549. {
  550. rt_thread_startup(photo_app);
  551. }
  552. else
  553. {
  554. rt_kprintf("Create photo frame thread failed!\n");
  555. }
  556. /* start display photos */
  557. rt_thread_sleep(100);
  558. do
  559. {
  560. /* get a photo */
  561. dirent = readdir(dir);
  562. if (dirent == RT_NULL)
  563. {
  564. break;
  565. }
  566. if ((strcmp(dirent->d_name, ".") == 0) || \
  567. (strcmp(dirent->d_name, "..") == 0))
  568. {
  569. continue;
  570. }
  571. rt_sprintf(path, "%s%c%s", "/photo", PATH_SEPARATOR, dirent->d_name);
  572. /* display it */
  573. if ((rt_strstr(path, ".bmp") != RT_NULL) || \
  574. (rt_strstr(path, ".BMP") != RT_NULL))
  575. {
  576. event.format = &bmp[0];
  577. rt_kprintf("bmp: %s\n", path);
  578. }
  579. else if ((rt_strstr(path, ".jpg") != RT_NULL) || \
  580. (rt_strstr(path, ".JPG") != RT_NULL))
  581. {
  582. event.format = &jpeg[0];
  583. rt_kprintf("jpeg: %s\n", path);
  584. }
  585. else
  586. {
  587. rt_kprintf("skip: %s\n", path);
  588. continue;
  589. }
  590. rtgui_send(photo_app, &event.win.parent, sizeof(event));
  591. rt_thread_sleep(2000);
  592. } while (dirent != RT_NULL);
  593. closedir(dir);
  594. rt_kprintf("Photo frame end.\n");
  595. }
  596. #endif
  597. rt_kprintf("All Demo end.\n");
  598. emu_em2_enable();
  599. while(1)
  600. {
  601. rt_thread_sleep(10);
  602. }
  603. }
  604. void rt_led_thread_entry(void* parameter)
  605. {
  606. rt_uint8_t n = 0;
  607. rt_hw_led_on(0);
  608. rt_hw_led_on(1);
  609. rt_hw_led_on(2);
  610. rt_hw_led_on(3);
  611. while(1)
  612. {
  613. /* Toggle a led per second */
  614. rt_hw_led_toggle(n++);
  615. if (n == LEDS_MAX_NUMBER)
  616. {
  617. n =0;
  618. }
  619. rt_thread_delay(100);
  620. }
  621. }
  622. int rt_application_init()
  623. {
  624. rt_thread_t demo_thread, led_thread;
  625. #if defined(EFM32_USING_ACCEL)
  626. if (efm_accel_init() != RT_EOK)
  627. {
  628. rt_kprintf("*** Init accelerometer driver failed!");
  629. while(1); //Or do something?
  630. }
  631. #endif
  632. #if defined(EFM32_USING_SFLASH)
  633. if (efm_spiFlash_init() != RT_EOK)
  634. {
  635. rt_kprintf("*** Init SPI Flash driver failed!");
  636. while(1); //Or do something?
  637. }
  638. #endif
  639. #if defined(EFM32_USING_SPISD)
  640. if (efm_spiSd_init() != RT_EOK)
  641. {
  642. rt_kprintf("*** Init SD card driver failed!");
  643. while(1); //Or do something?
  644. }
  645. #endif
  646. /* Initialize all device drivers (dev_?.c) */
  647. if (rt_hw_led_init() != RT_EOK)
  648. {
  649. rt_kprintf("*** Init LED driver failed!");
  650. while(1); //Or do something?
  651. }
  652. #if defined(RT_USING_MISC)
  653. if (rt_hw_misc_init() != RT_EOK)
  654. {
  655. rt_kprintf("*** Init miscellaneous driver failed!");
  656. while(1); //Or do something?
  657. }
  658. #endif
  659. #if defined(RT_USING_LWIP)
  660. {
  661. /* Create Ethernet Threads */
  662. if (eth_system_device_init() != RT_EOK)
  663. {
  664. rt_kprintf("*** Create Ethernet threads failed!");
  665. while(1); //Or do something?
  666. }
  667. #if defined(EFM32_USING_ETHERNET)
  668. if (efm_hw_eth_init() != RT_EOK)
  669. {
  670. rt_kprintf("*** Init Ethernet driver failed!");
  671. while(1); //Or do something?
  672. }
  673. #endif
  674. }
  675. #endif
  676. #if (RT_THREAD_PRIORITY_MAX == 32)
  677. demo_thread = rt_thread_create(
  678. "demo",
  679. rt_demo_thread_entry,
  680. RT_NULL,
  681. 1024,
  682. 5,
  683. 20);
  684. led_thread = rt_thread_create(
  685. "led",
  686. rt_led_thread_entry,
  687. RT_NULL,
  688. 256,
  689. 5,
  690. 20);
  691. #else
  692. #endif
  693. if(demo_thread != RT_NULL)
  694. {
  695. rt_kprintf("demo sp:%x\n", demo_thread->sp);
  696. rt_thread_startup(demo_thread);
  697. }
  698. if(led_thread != RT_NULL)
  699. {
  700. rt_thread_startup(led_thread);
  701. }
  702. return 0;
  703. }
  704. /***************************************************************************//**
  705. * @}
  706. ******************************************************************************/