application.c 21 KB

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