application.c 23 KB

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