application.c 21 KB

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