application.c 21 KB

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