application.c 21.5 KB
Newer Older
M
Ming, Bai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
/***************************************************************************//**
 * @file    application.c
 * @brief   Demo application
 *  COPYRIGHT (C) 2012, RT-Thread Development Team
 * @author  Bernard, onelife
 * @version 1.0
 *******************************************************************************
 * @section License
 * The license and distribution terms for this file may be found in the file
 * LICENSE in this distribution or at http://www.rt-thread.org/license/LICENSE
 *******************************************************************************
 * @section Change Logs
 * Date         Author      Notes
 * 2009-01-05   Bernard     first version
 * 2010-12-29   onelife     Modify for EFM32
 * 2011-05-06   onelife     Add SPI Flash DEMO
 * 2011-07-15   onelife     Add accelerometer DEMO
 * 2011-07-27   onelife     Modify Ethernet DEMO
 * 2011-08-23   onelife     Modify Ethernet DEMO according to the changes of
 *  lwIP API in reversion 1668
 * 2011-12-20   onelife     Add LCD DEMO
 * 2012-02-16   onelife     Add photo frame DEMO
 * 2012-xx-xx   onelife     Add low energy test code
 * 2012-05-17   onelife     Modify photo frame DEMO according to new version of
 *  RTGUI
 ******************************************************************************/

/***************************************************************************//**
 * @addtogroup efm32
 * @{
 ******************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include <board.h>

#if defined(RT_USING_DFS)
/* dfs filesystem:ELM filesystem init */
#include <dfs_elm.h>
/* dfs Filesystem APIs */
#include <dfs_fs.h>
#endif

#include "dev_led.h"
#if defined(EFM32_USING_ACCEL)
#include "dev_accel.h"
#endif
#if defined(EFM32_USING_SFLASH)
#include "dev_sflash.h"
#endif
#if defined(EFM32_USING_SPISD)
#include "drv_sdcard.h"
#endif
#if defined(EFM32_USING_ETHERNET)
#include "drv_ethernet.h"
#endif
#if defined(EFM32_USING_LCD)
#include "dev_lcd.h"

#include <rtgui/rtgui_server.h>
#include <rtgui/rtgui_system.h>
#include <rtgui/rtgui_app.h>
#include <rtgui/widgets/widget.h>
#include <rtgui/widgets/label.h>
#include <rtgui/widgets/window.h>
#include <rtgui/widgets/box.h>
#include <rtgui/image.h>

mysterywolf's avatar
mysterywolf 已提交
68
#if defined(RTGUI_USING_DFS_FILERW)
M
Ming, Bai 已提交
69 70 71 72 73 74 75 76 77 78 79
 #include <dfs_posix.h>
 #define PATH_SEPARATOR     '/'
 #endif
#endif

/* Private typedef -----------------------------------------------------------*/
#if defined(RT_USING_RTGUI)
struct photo_event
{
    struct rtgui_event_win win;
    rt_uint32_t cmd;
mysterywolf's avatar
mysterywolf 已提交
80 81
    rt_uint8_t* path;
    rt_uint8_t* format;
M
Ming, Bai 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
};

/* Private defines -----------------------------------------------------------*/
#define APP_CMD_PHOTO_FRAME 0x00000001
#define APP_PHOTO_FRAME
#endif

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
volatile rt_uint32_t    rt_system_status = 0;

/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
#if defined(RT_USING_RTGUI)
static rt_bool_t pic_view_event_handler(rtgui_object_t *object, rtgui_event_t *event)
{
mysterywolf's avatar
mysterywolf 已提交
98
    rt_bool_t result;
M
Ming, Bai 已提交
99 100
    rt_bool_t load = RT_FALSE;

mysterywolf's avatar
mysterywolf 已提交
101
    result = rtgui_container_event_handler(object, event);
M
Ming, Bai 已提交
102 103 104 105 106 107 108 109 110

    switch(event->type)
    {
    case RTGUI_EVENT_PAINT:
        load = RT_TRUE;
        break;

    case RTGUI_EVENT_MOUSE_BUTTON:
        {
mysterywolf's avatar
mysterywolf 已提交
111
            struct rtgui_event_mouse *mouse = (struct rtgui_event_mouse *)event;
M
Ming, Bai 已提交
112

mysterywolf's avatar
mysterywolf 已提交
113 114
            if (mouse->button == RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_UP)
            {
M
Ming, Bai 已提交
115
                rt_kprintf("APP: left click (%x)\n", mouse->button);
mysterywolf's avatar
mysterywolf 已提交
116
            }
M
Ming, Bai 已提交
117 118 119 120 121
        }
        break;
    }

    if (load)
mysterywolf's avatar
mysterywolf 已提交
122 123 124
    {
        struct rtgui_dc* dc;
        rtgui_rect_t rect;
M
Ming, Bai 已提交
125 126 127 128 129
        rtgui_image_t* image;

        image = rtgui_image_create_from_file("jpg", "/test9.jpg", RT_FALSE);
//        image = rtgui_image_create_from_file("bmp", "/test_565.bmp", RT_FALSE);

mysterywolf's avatar
mysterywolf 已提交
130 131
        dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(object));
        if (dc == RT_NULL)
M
Ming, Bai 已提交
132 133 134 135 136 137 138 139 140
        {
            return result;
        }

        rtgui_widget_get_rect(RTGUI_WIDGET(object), &rect);
 //       rtgui_widget_rect_to_device(widget, &rect);
        rect.x1 +=10;
        rect.y1 +=10;

mysterywolf's avatar
mysterywolf 已提交
141
        if (image != RT_NULL)
M
Ming, Bai 已提交
142
        {
mysterywolf's avatar
mysterywolf 已提交
143
            rtgui_image_blit(image, dc, &rect);
M
Ming, Bai 已提交
144 145 146 147 148 149 150
            rtgui_image_destroy(image);
        }
        else
        {
            rt_kprintf("APP err: no image found!\n");
        }

mysterywolf's avatar
mysterywolf 已提交
151 152
        rtgui_dc_end_drawing(dc, RT_TRUE);
    }
M
Ming, Bai 已提交
153

mysterywolf's avatar
mysterywolf 已提交
154
    return result;
M
Ming, Bai 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
}

static void app_main(void *parameter)
{
    /* find lcd device */
    rt_device_t lcd = rt_device_find(LCD_DEVICE_NAME);
    if (lcd == RT_NULL)
    {
        rt_kprintf("Can't find LCD\n");
        return;
    }

    /* read LCD info */
    struct rt_device_graphic_info lcd_info;
    lcd->control(lcd, RTGRAPHIC_CTRL_GET_INFO, (void *)&lcd_info);
    rt_kprintf("LCD size: %dX%d\n", lcd_info.width, lcd_info.height);

mysterywolf's avatar
mysterywolf 已提交
172 173 174 175
    /* create application */
    struct rtgui_app *app;
    app = rtgui_app_create("gui_app");
    if (app == RT_NULL)
M
Ming, Bai 已提交
176 177 178 179 180
    {
        rt_kprintf("Create application \"gui_app\" failed!\n");
        return;
    }

mysterywolf's avatar
mysterywolf 已提交
181
    struct rtgui_rect rect1, rect2, rect3;
M
Ming, Bai 已提交
182
    struct rtgui_win *win_info, *win_main, *win_hello;
mysterywolf's avatar
mysterywolf 已提交
183
    struct rtgui_container *container;
M
Ming, Bai 已提交
184 185
    struct rtgui_label* label;

mysterywolf's avatar
mysterywolf 已提交
186
    rtgui_graphic_driver_get_rect(rtgui_graphic_driver_get_default(), &rect1);
M
Ming, Bai 已提交
187 188 189 190 191 192 193
    rect2.x1 = rect1.x1;
    rect2.y1 = 25;
    rect2.x2 = rect1.x2;
    rect2.y2 = rect1.y2;
    rect1.y2 = 25;

    /* create info window */
mysterywolf's avatar
mysterywolf 已提交
194
    win_info = rtgui_win_create(RT_NULL, "info",
M
Ming, Bai 已提交
195 196
                    &rect1,
                    RTGUI_WIN_STYLE_NO_BORDER | RTGUI_WIN_STYLE_NO_TITLE);
mysterywolf's avatar
mysterywolf 已提交
197 198
    if (win_info == RT_NULL)
    {
M
Ming, Bai 已提交
199
        rt_kprintf("Create window \"info\" failed!\n");
mysterywolf's avatar
mysterywolf 已提交
200
        rtgui_app_destroy(app);
M
Ming, Bai 已提交
201
        return;
mysterywolf's avatar
mysterywolf 已提交
202
    }
M
Ming, Bai 已提交
203 204

    /* create container in info window */
mysterywolf's avatar
mysterywolf 已提交
205 206 207
    container = rtgui_container_create();
    if (container == RT_NULL)
    {
M
Ming, Bai 已提交
208
        rt_kprintf("Create container failed!\n");
mysterywolf's avatar
mysterywolf 已提交
209 210 211
        return;
    }
    rtgui_widget_set_rect(RTGUI_WIDGET(container), &rect1);
M
Ming, Bai 已提交
212 213 214
    rtgui_container_add_child(RTGUI_CONTAINER(win_info), RTGUI_WIDGET(container));

    /* create lable in info window */
mysterywolf's avatar
mysterywolf 已提交
215
    label = rtgui_label_create("RT-Thread & RTGUI");
M
Ming, Bai 已提交
216 217 218 219 220 221
    if (label == RT_NULL)
    {
        rt_kprintf("Create lable failed!\n");
        return;
    }

mysterywolf's avatar
mysterywolf 已提交
222 223
    RTGUI_WIDGET_TEXTALIGN(RTGUI_WIDGET(label)) = RTGUI_ALIGN_LEFT;
    RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(label)) = red;
M
Ming, Bai 已提交
224 225
    RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(label)) = white;

mysterywolf's avatar
mysterywolf 已提交
226 227 228 229 230 231
    rect3.x1 = rect1.x1 + 5;
    rect3.y1 = rect1.y1 + 5;
    rect3.x2 = rect1.x2 - 5;
    rect3.y2 = rect1.y2 - 5;
    rtgui_widget_set_rect(RTGUI_WIDGET(label), &rect3);
    rtgui_container_add_child(container, RTGUI_WIDGET(label));
M
Ming, Bai 已提交
232 233 234


    /* create main window */
mysterywolf's avatar
mysterywolf 已提交
235
    win_main = rtgui_win_create(RT_NULL, "main",
M
Ming, Bai 已提交
236 237
                    &rect2,
                    RTGUI_WIN_STYLE_NO_BORDER | RTGUI_WIN_STYLE_NO_TITLE);
mysterywolf's avatar
mysterywolf 已提交
238 239
    if (win_main == RT_NULL)
    {
M
Ming, Bai 已提交
240
        rt_kprintf("Create window \"main\" failed!\n");
mysterywolf's avatar
mysterywolf 已提交
241
        rtgui_app_destroy(app);
M
Ming, Bai 已提交
242
        return;
mysterywolf's avatar
mysterywolf 已提交
243
    }
M
Ming, Bai 已提交
244 245

    /* create container in main window */
mysterywolf's avatar
mysterywolf 已提交
246 247 248
    container = rtgui_container_create();
    if (container == RT_NULL)
    {
M
Ming, Bai 已提交
249
        rt_kprintf("Create container failed!\n");
mysterywolf's avatar
mysterywolf 已提交
250 251
        return;
    }
M
Ming, Bai 已提交
252

mysterywolf's avatar
mysterywolf 已提交
253
    rtgui_widget_set_rect(RTGUI_WIDGET(container), &rect2);
M
Ming, Bai 已提交
254 255 256 257
    rtgui_object_set_event_handler(RTGUI_OBJECT(container), pic_view_event_handler);
    rtgui_container_add_child(RTGUI_CONTAINER(win_main), RTGUI_WIDGET(container));

    /* create lable in main window */
mysterywolf's avatar
mysterywolf 已提交
258
    label = rtgui_label_create("EFM32GG_DK3750 Kit");
M
Ming, Bai 已提交
259 260 261 262 263
    if (label == RT_NULL)
    {
        rt_kprintf("Create lable failed!\n");
        return;
    }
mysterywolf's avatar
mysterywolf 已提交
264 265
    RTGUI_WIDGET_TEXTALIGN(RTGUI_WIDGET(label)) = RTGUI_ALIGN_LEFT;
    RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(label)) = white;
M
Ming, Bai 已提交
266 267
    RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(label)) = blue;

mysterywolf's avatar
mysterywolf 已提交
268 269 270 271 272 273
    rect3.x1 = rect2.x1 + 5;
    rect3.y1 = rect2.y1 + 5;
    rect3.x2 = rect2.x2 - 5;
    rect3.y2 = rect2.y1 + 20;
    rtgui_widget_set_rect(RTGUI_WIDGET(label), &rect3);
    rtgui_container_add_child(container, RTGUI_WIDGET(label));
M
Ming, Bai 已提交
274 275 276


    /* create hello window */
mysterywolf's avatar
mysterywolf 已提交
277 278 279 280 281
    rect3.x1 = 80;
    rect3.y1 = 50;
    rect3.x2 = 320 - 80;
    rect3.y2 = 240 - 50;
    win_hello = rtgui_win_create(RT_NULL, "hello",
M
Ming, Bai 已提交
282 283
                    &rect3,
                    RTGUI_WIN_STYLE_DEFAULT);
mysterywolf's avatar
mysterywolf 已提交
284 285
    if (win_hello == RT_NULL)
    {
M
Ming, Bai 已提交
286
        rt_kprintf("Create window \"hello\" failed!\n");
mysterywolf's avatar
mysterywolf 已提交
287
        rtgui_app_destroy(app);
M
Ming, Bai 已提交
288
        return;
mysterywolf's avatar
mysterywolf 已提交
289
    }
M
Ming, Bai 已提交
290 291 292

    /* create a box */
    rtgui_box_t *box = rtgui_box_create(RTGUI_VERTICAL, RT_NULL);
mysterywolf's avatar
mysterywolf 已提交
293
    if(box == RT_NULL)
M
Ming, Bai 已提交
294 295 296 297 298 299
    {
        rt_kprintf("Create box failed!\n");
        return;
    }
//    rtgui_win_set_box(win_hello, box);

mysterywolf's avatar
mysterywolf 已提交
300 301
    label = rtgui_label_create("哈罗,盹胖!");
    if(label == RT_NULL)
M
Ming, Bai 已提交
302 303 304 305
    {
        rt_kprintf("Create lable failed!\n");
        return;
    }
mysterywolf's avatar
mysterywolf 已提交
306
    RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(label)) = white;
M
Ming, Bai 已提交
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
    RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(label)) = black;
    RTGUI_WIDGET(label)->align = RTGUI_ALIGN_CENTER_HORIZONTAL | RTGUI_ALIGN_CENTER_VERTICAL;
    rtgui_widget_set_miniwidth(RTGUI_WIDGET(label),130);
    rtgui_box_append(box, RTGUI_WIDGET(label));
    /* Auto layout */
    rtgui_box_layout(box);


    rtgui_win_show(win_info, RT_FALSE);
    rtgui_win_show(win_main, RT_FALSE);
    rtgui_win_show(win_hello, RT_FALSE);

    rtgui_app_run(app);
    rtgui_app_destroy(app);
}

static rt_bool_t photo_view_event_handler(rtgui_object_t *object, rtgui_event_t *event)
{
mysterywolf's avatar
mysterywolf 已提交
325
    rt_bool_t result = RT_FALSE;
M
Ming, Bai 已提交
326 327
    struct photo_event *photo_event = (struct photo_event *)event;

mysterywolf's avatar
mysterywolf 已提交
328
    result = rtgui_container_event_handler(object, event);
M
Ming, Bai 已提交
329 330
    rt_kprintf("container event %x\n", event->type);

mysterywolf's avatar
mysterywolf 已提交
331
    struct rtgui_event_win* wevent = (struct rtgui_event_win*)event;
M
Ming, Bai 已提交
332 333 334 335
    rt_kprintf("wevent->wid %x\n", wevent->wid);

    if ((event->type == RTGUI_EVENT_COMMAND) && \
        (photo_event->cmd == APP_CMD_PHOTO_FRAME))
mysterywolf's avatar
mysterywolf 已提交
336
    {
M
Ming, Bai 已提交
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
        rtgui_rect_t rect;
        rtgui_image_t* image;
        struct rtgui_dc* dc;

        rtgui_widget_get_rect(RTGUI_WIDGET(object), &rect);
        rt_kprintf(" (%d, %d) (%d, %d)\n", rect.x1, rect.y1, rect.x2, rect.y2);
//        rtgui_widget_rect_to_device(RTGUI_WIDGET(object), &rect);
        rect.y1 +=15;

        dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(object));
        if (dc == RT_NULL)
        {
            return result;
        }

        image = rtgui_image_create_from_file(photo_event->format,
            photo_event->path, RT_TRUE);
        if (image != RT_NULL)
        {
            rtgui_image_blit(image, dc, &rect);
            rtgui_image_destroy(image);
            return result;
        }

        return RT_TRUE;
    }

mysterywolf's avatar
mysterywolf 已提交
364
    return result;
M
Ming, Bai 已提交
365 366 367 368
}

static rt_bool_t photo_lable_event_handler(rtgui_object_t *object, rtgui_event_t *event)
{
mysterywolf's avatar
mysterywolf 已提交
369
    rt_bool_t result = RT_FALSE;
M
Ming, Bai 已提交
370

mysterywolf's avatar
mysterywolf 已提交
371
    result = rtgui_label_event_handler(object, event);
M
Ming, Bai 已提交
372 373 374
    rt_kprintf("lable event %x\n", event->type);

    if (event->type == RTGUI_EVENT_COMMAND)
mysterywolf's avatar
mysterywolf 已提交
375
    {
M
Ming, Bai 已提交
376 377 378 379 380 381
        struct photo_event *photo = (struct photo_event *)event;

        rtgui_label_set_text((rtgui_label_t *)object, photo->path);
        rt_kprintf("path %s\n", photo->path);
    }

mysterywolf's avatar
mysterywolf 已提交
382
    return result;
M
Ming, Bai 已提交
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
}

static void app_photo(void *parameter)
{
    struct photo_event *event = (struct photo_event *)parameter;

    /* find lcd device */
    rt_device_t lcd = rt_device_find(LCD_DEVICE_NAME);
    if (lcd == RT_NULL)
    {
        rt_kprintf("Can't find LCD\n");
        return;
    }

    /* read LCD info */
    struct rt_device_graphic_info lcd_info;
    lcd->control(lcd, RTGRAPHIC_CTRL_GET_INFO, (void *)&lcd_info);
    rt_kprintf("LCD size: %dX%d\n", lcd_info.width, lcd_info.height);

mysterywolf's avatar
mysterywolf 已提交
402 403 404 405
    /* create application */
    struct rtgui_app *app;
    app = rtgui_app_create("pho_app");
    if (app == RT_NULL)
M
Ming, Bai 已提交
406 407 408 409 410
    {
        rt_kprintf("Create application \"pho_app\" failed!\n");
        return;
    }

mysterywolf's avatar
mysterywolf 已提交
411
    struct rtgui_rect rect1, rect2;
M
Ming, Bai 已提交
412
    struct rtgui_win *window;
mysterywolf's avatar
mysterywolf 已提交
413
    struct rtgui_container *container;
M
Ming, Bai 已提交
414 415
    struct rtgui_label* label;

mysterywolf's avatar
mysterywolf 已提交
416
    rtgui_graphic_driver_get_rect(rtgui_graphic_driver_get_default(), &rect1);
M
Ming, Bai 已提交
417 418

    /* create window */
mysterywolf's avatar
mysterywolf 已提交
419
    window = rtgui_win_create(RT_NULL, "photo",
M
Ming, Bai 已提交
420 421
                    &rect1,
                    RTGUI_WIN_STYLE_NO_BORDER | RTGUI_WIN_STYLE_NO_TITLE);
mysterywolf's avatar
mysterywolf 已提交
422 423
    if (window == RT_NULL)
    {
M
Ming, Bai 已提交
424
        rt_kprintf("Create window \"photo\" failed!\n");
mysterywolf's avatar
mysterywolf 已提交
425
        rtgui_app_destroy(app);
M
Ming, Bai 已提交
426
        return;
mysterywolf's avatar
mysterywolf 已提交
427
    }
M
Ming, Bai 已提交
428 429 430
    event->win.wid = window;

    /* create container */
mysterywolf's avatar
mysterywolf 已提交
431 432 433
    container = rtgui_container_create();
    if (container == RT_NULL)
    {
M
Ming, Bai 已提交
434
        rt_kprintf("Create container failed!\n");
mysterywolf's avatar
mysterywolf 已提交
435 436 437
        return;
    }
    rtgui_widget_set_rect(RTGUI_WIDGET(container), &rect1);
M
Ming, Bai 已提交
438 439 440 441
    rtgui_object_set_event_handler(RTGUI_OBJECT(container), photo_view_event_handler);
    rtgui_container_add_child(RTGUI_CONTAINER(window), RTGUI_WIDGET(container));

    /* create lable in info window */
mysterywolf's avatar
mysterywolf 已提交
442
    label = rtgui_label_create("Photo Frame Demo");
M
Ming, Bai 已提交
443 444 445 446 447 448
    if (label == RT_NULL)
    {
        rt_kprintf("Create lable failed!\n");
        return;
    }

mysterywolf's avatar
mysterywolf 已提交
449 450
    RTGUI_WIDGET_TEXTALIGN(RTGUI_WIDGET(label)) = RTGUI_ALIGN_LEFT;
    RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(label)) = white;
M
Ming, Bai 已提交
451 452 453 454 455 456
    RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(label)) = blue;

    rect2.x1 = rect1.x1;
    rect2.y1 = rect1.y1;
    rect2.x2 = rect1.x2;
    rect2.y2 = 15;
mysterywolf's avatar
mysterywolf 已提交
457
    rtgui_widget_set_rect(RTGUI_WIDGET(label), &rect2);
M
Ming, Bai 已提交
458
    rtgui_object_set_event_handler(RTGUI_OBJECT(label), photo_lable_event_handler);
mysterywolf's avatar
mysterywolf 已提交
459
    rtgui_container_add_child(container, RTGUI_WIDGET(label));
M
Ming, Bai 已提交
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816

    rtgui_win_show(window, RT_FALSE);

    rtgui_app_run(app);
    rtgui_app_destroy(app);
}
#endif

void rt_demo_thread_entry(void* parameter)
{
    emu_all_disable();

#if 0 //defined(EFM32_USING_ACCEL)
{
    struct efm32_accel_result_t result;

    rt_kprintf(">>> waiting\n");
    rt_thread_sleep(6000);
    rt_kprintf(">>> start\n");
    while(1)
    {
        efm_accel_get_data(&result);
        rt_kprintf("Accel x: %x\n", result.x);
        rt_kprintf("Accel y: %x\n", result.y);
        rt_kprintf("Accel z: %x\n\n", result.z);
        rt_thread_sleep(200);
    }
}
#endif

#if defined(RT_USING_DFS)
{
    rt_kprintf("File system DEMO start...\n");
    /* Filesystem Initialization */
    dfs_init();

 #if defined(RT_USING_DFS_ELMFAT)
    /* init the elm chan FatFs filesystam*/
    elm_init();

  #if defined(EFM32_USING_SPISD)
    /* mount sd card fat partition 1 as root directory */
    if (dfs_mount(SPISD_DEVICE_NAME, "/", "elm", 0, 0) == 0)
    {
        rt_kprintf("FatFs init OK\n");
    }
    else
    {
        rt_kprintf("FatFs init failed!\n");
    }
  #endif
 #endif
    rt_kprintf("File system DEMO end.\n");
}
#endif

#ifdef EFM32_USING_SFLASH
{
    rt_kprintf("SPI Flash DEMO start...\n");

    rt_uint8_t i;
    rt_uint8_t test[] = "123456789ABCDEF";
    rt_uint8_t buf[30], buf2[30];

    efm_spiFlash_cmd(sflash_inst_rdid_l, EFM32_NO_DATA, buf, sizeof(buf));
    rt_kprintf("Manuf ID: %x\n", buf[0]);
    rt_kprintf("Memory type: %x\n", buf[1]);
    rt_kprintf("Memory capacity: %x\n", buf[2]);
    rt_kprintf("CFD length: %x\n", buf[3]);
    rt_kprintf("CFD: %x%x%x...%x%x\n", buf[4], buf[5], buf[6], buf[18], buf[19]);

    efm_spiFlash_cmd(sflash_inst_wren, EFM32_NO_DATA, EFM32_NO_POINTER, EFM32_NO_DATA);
    do
    {
        efm_spiFlash_cmd(sflash_inst_rdsr, EFM32_NO_DATA, buf2, sizeof(buf2));
        rt_kprintf("Status: %x\n", buf2[0]);
    } while (buf2[0] == 0xFF);
    rt_kprintf("Status: %x\n", buf2[0]);

    //efm_spiFash_cmd(sflash_inst_pp, 0x000003F8, test, sizeof(test) - 1);

    efm_spiFlash_cmd(sflash_inst_rdsr, EFM32_NO_DATA, buf2, sizeof(buf2));
    rt_kprintf("Status: %x\n", buf2[0]);

    efm_spiFlash_cmd(sflash_inst_read, 0x00000300, buf, sizeof(buf));
    rt_kprintf("READ: \n");
    for (i = 0; i < sizeof(buf); i++)
    {
        rt_kprintf("%c\n", buf[i]);
    }

    //efm_spiFlash_deinit();
    rt_kprintf("SPI Flash DEMO end.\n");
}
#endif

#if defined(EFM32_USING_ETHERNET)
{
    rt_kprintf("Ethernet DEMO start...\n");
    extern void lwip_sys_init(void);
    /* init lwip system */
    lwip_sys_init();
    rt_kprintf("TCP/IP stack init OK!\n");

 #if defined(EFM32_USING_ETH_HTTPD)
    extern void httpd_init(void);
    /* init http server */
    httpd_init();
    rt_kprintf("Http service init OK!\n");
 #endif /* defined(EFM32_USING_ETH_HTTPD) */
    rt_kprintf("Ethernet DEMO end.\n");
}
#endif /* defined(EFM32_USING_ETHERNET) */

#if (defined(EFM32_USING_LCD) && !defined(APP_PHOTO_FRAME))
{
    rt_kprintf("LCD DEMO start...\n");

    /* Create RTGUI application thread */
    rt_thread_t gui_app;
    gui_app = rt_thread_create(
        "gui_thd",
        app_main,
        RT_NULL,
        2048,
        25,
        10);
    if (gui_app != RT_NULL)
    {
        rt_thread_startup(gui_app);
    }
    else
    {
        rt_kprintf("Create gui_app thread failed!\n");
    }

    rt_kprintf("LCD DEMO end.\n");
}
#endif

#if defined(APP_PHOTO_FRAME)
{
    rt_kprintf("Photo frame DEMO start...\n");

    DIR* dir = opendir("/photo");
    struct photo_event event;
    struct dirent* dirent;
    rt_uint8_t path[100];
    const rt_uint8_t bmp[] = "bmp";
    const rt_uint8_t jpeg[] = "jpeg";

    RTGUI_EVENT_COMMAND_INIT(&event.win);
    event.cmd = APP_CMD_PHOTO_FRAME;
    event.path = path;

    /* find lcd device */
    rt_device_t lcd = rt_device_find(LCD_DEVICE_NAME);
    if (lcd == RT_NULL)
    {
        rt_kprintf("Can't find LCD\n");
    }

    /* read LCD info */
    struct rt_device_graphic_info lcd_info;
    lcd->control(lcd, RTGRAPHIC_CTRL_GET_INFO, (void *)&lcd_info);
    rt_kprintf("LCD size: %dX%d\n", lcd_info.width, lcd_info.height);

    /* Create photo frame thread */
    rt_thread_t photo_app;
    photo_app = rt_thread_create(
        "pho_thd",
        app_photo,
        (void *)&event,
        2048,
        25,
        10);
    if (photo_app != RT_NULL)
    {
        rt_thread_startup(photo_app);
    }
    else
    {
        rt_kprintf("Create photo frame thread failed!\n");
    }

    /* start display photos */
    rt_thread_sleep(100);
    do
    {
        /* get a photo */
        dirent = readdir(dir);
        if (dirent == RT_NULL)
        {
            break;
        }
        if ((strcmp(dirent->d_name, ".") == 0) || \
            (strcmp(dirent->d_name, "..") == 0))
        {
            continue;
        }
        rt_sprintf(path, "%s%c%s", "/photo", PATH_SEPARATOR, dirent->d_name);

        /* display it */
        if ((rt_strstr(path, ".bmp") != RT_NULL) || \
            (rt_strstr(path, ".BMP") != RT_NULL))
        {
            event.format = &bmp[0];
            rt_kprintf("bmp: %s\n", path);
        }
        else if ((rt_strstr(path, ".jpg") != RT_NULL) || \
            (rt_strstr(path, ".JPG") != RT_NULL))
        {
            event.format = &jpeg[0];
            rt_kprintf("jpeg: %s\n", path);
        }
        else
        {
            rt_kprintf("skip: %s\n", path);
            continue;
        }

        rtgui_send(photo_app, &event.win.parent, sizeof(event));
        rt_thread_sleep(2000);
    } while (dirent != RT_NULL);
    closedir(dir);

    rt_kprintf("Photo frame end.\n");
}

#endif

    rt_kprintf("All Demo end.\n");

    emu_em2_enable();
    while(1)
    {
        rt_thread_sleep(10);
    }
}

void rt_led_thread_entry(void* parameter)
{
    rt_uint8_t n = 0;

    rt_hw_led_on(0);
    rt_hw_led_on(1);
    rt_hw_led_on(2);
    rt_hw_led_on(3);

    while(1)
    {
        /* Toggle a led per second */
        rt_hw_led_toggle(n++);
        if (n == LEDS_MAX_NUMBER)
        {
            n =0;
        }
        rt_thread_delay(100);
    }
}

int rt_application_init()
{
    rt_thread_t demo_thread, led_thread;

#if defined(EFM32_USING_ACCEL)
    if (efm_accel_init() != RT_EOK)
    {
        rt_kprintf("*** Init accelerometer driver failed!");
        while(1); //Or do something?
    }
#endif

#if defined(EFM32_USING_SFLASH)
    if (efm_spiFlash_init() != RT_EOK)
    {
        rt_kprintf("*** Init SPI Flash driver failed!");
        while(1); //Or do something?
    }
#endif

#if defined(EFM32_USING_SPISD)
    if (efm_spiSd_init() != RT_EOK)
    {
        rt_kprintf("*** Init SD card driver failed!");
        while(1); //Or do something?
    }
#endif

    /* Initialize all device drivers (dev_?.c) */
    if (rt_hw_led_init() != RT_EOK)
    {
        rt_kprintf("*** Init LED driver failed!");
        while(1); //Or do something?
    }
#if defined(RT_USING_MISC)
    if (rt_hw_misc_init() != RT_EOK)
    {
        rt_kprintf("*** Init miscellaneous driver failed!");
        while(1); //Or do something?
    }
#endif

#if defined(RT_USING_LWIP)
    {
        /* Create Ethernet Threads */
        if (eth_system_device_init() != RT_EOK)
        {
            rt_kprintf("*** Create Ethernet threads failed!");
            while(1); //Or do something?
        }
 #if defined(EFM32_USING_ETHERNET)
        if (efm_hw_eth_init() != RT_EOK)
        {
            rt_kprintf("*** Init Ethernet driver failed!");
            while(1); //Or do something?
        }
 #endif
    }
#endif

#if (RT_THREAD_PRIORITY_MAX == 32)
    demo_thread = rt_thread_create(
        "demo",
        rt_demo_thread_entry,
        RT_NULL,
        1024,
        5,
        20);

    led_thread = rt_thread_create(
        "led",
        rt_led_thread_entry,
        RT_NULL,
        256,
        5,
        20);
#else
#endif

    if(demo_thread != RT_NULL)
    {
        rt_kprintf("demo sp:%x\n", demo_thread->sp);
        rt_thread_startup(demo_thread);
    }

    if(led_thread != RT_NULL)
    {
        rt_thread_startup(led_thread);
    }

    return 0;
}

/***************************************************************************//**
 * @}
 ******************************************************************************/