LPCOpen Platform
LPCOpen Platform for NXP LPC Microcontrollers
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
example_emWin.c
Go to the documentation of this file.
1 /*
2  * @brief emWin graphics dual core example using emWin library
3  *
4  * @note
5  * Copyright(C) NXP Semiconductors, 2012
6  * All rights reserved.
7  *
8  * @par
9  * Software that is described herein is for illustrative purposes only
10  * which provides customers with programming information regarding the
11  * LPC products. This software is supplied "AS IS" without any warranties of
12  * any kind, and NXP Semiconductors and its licensor disclaim any and
13  * all warranties, express or implied, including all implied warranties of
14  * merchantability, fitness for a particular purpose and non-infringement of
15  * intellectual property rights. NXP Semiconductors assumes no responsibility
16  * or liability for the use of the software, conveys no license or rights under any
17  * patent, copyright, mask work right, or any other intellectual property rights in
18  * or to any products. NXP Semiconductors reserves the right to make changes
19  * in the software without notification. NXP Semiconductors also makes no
20  * representation or warranty that such application will be suitable for the
21  * specified use without further testing or modification.
22  *
23  * @par
24  * Permission to use, copy, modify, and distribute this software and its
25  * documentation is hereby granted, under NXP Semiconductors' and its
26  * licensor's relevant copyrights in the software, without fee, provided that it
27  * is used in conjunction with NXP Semiconductors microcontrollers. This
28  * copyright, permission, and disclaimer notice must appear in all copies of
29  * this code.
30  */
31 
38 #include "lpc43xx_dualcore_config.h"
39 #include "ipc_example.h"
40 #include "ipc_msg.h"
41 
42 #include "GUI.h"
43 #include "DIALOG.h"
44 
45 #ifdef OS_FREE_RTOS
46 #include "FreeRTOS.h"
47 #include "task.h"
48 #endif
49 
50 #ifdef OS_UCOS_III
51 #include "os.h"
52 
53 #define UCOS_LCD_STACK_SZ 1024
54 #define UCOS_TSC_STACK_SZ 512
55 
56 #define UCOS_LCD_TASK_PRIORITY (APP_CFG_TASK_START_PRIO + 1)
57 #define UCOS_TSC_TASK_PRIORITY (APP_CFG_TASK_START_PRIO - 1)
58 #endif
59 
74 /*****************************************************************************
75  * Private types/enumerations/variables
76  ****************************************************************************/
80 static volatile uint32_t host_ip_addr; /* Host IP address */
81 static volatile uint32_t remote_ip_addr; /* Remote IP address */
82 static volatile int start = 0; /* Start flag for counter */
83 static volatile short counter = 0; /* Count value */
84 static WM_HWIN hWin; /* emWin Widget Frame Window structure */
85 
86 #if (defined(OS_FREE_RTOS)) || (defined(OS_UCOS_III))
87 
90 static int16_t old_tmp_x = -1, old_tmp_y = -1;
91 #endif
92 
96 #define ID_FRAMEWIN_0 (GUI_ID_USER + 0x0A)
97 #define ID_TEXT_0 (GUI_ID_USER + 0x0B)
98 #define ID_BUTTON_0 (GUI_ID_USER + 0x0C)
99 #define ID_BUTTON_1 (GUI_ID_USER + 0x0D)
100 #define ID_BUTTON_2 (GUI_ID_USER + 0x0E)
101 #define ID_TEXT_1 (GUI_ID_USER + 0x10)
102 #define ID_TEXT_2 (GUI_ID_USER + 0x11)
103 #define ID_TEXT_3 (GUI_ID_USER + 0x12)
104 #define ID_TEXT_4 (GUI_ID_USER + 0x13)
105 #define ID_TEXT_5 (GUI_ID_USER + 0x14)
106 #define ID_EDIT_0 (GUI_ID_USER + 0x15)
107 #define ID_EDIT_1 (GUI_ID_USER + 0x16)
108 
112 #define TSC_CHECK_DELAY (20)
113 
117 static const GUI_WIDGET_CREATE_INFO _aDialogCreate[] = {
118  {FRAMEWIN_CreateIndirect, "emWin Demo", ID_FRAMEWIN_0, 8, 8, 310, 230, 0, 0, 0},
119  {TEXT_CreateIndirect, "NXP Semiconductors", ID_TEXT_0, 30, 10, 260, 100, TEXT_CF_HCENTER},
120  {TEXT_CreateIndirect, "0000", ID_TEXT_1, 100, 80, 100, 60, TEXT_CF_HCENTER},
121  {BUTTON_CreateIndirect, "START", ID_BUTTON_0, 36, 160, 70, 30, 0, 0, 0},
122  {BUTTON_CreateIndirect, "STOP", ID_BUTTON_1, 126, 160, 60, 30},
123  {BUTTON_CreateIndirect, "RESET", ID_BUTTON_2, 203, 160, 70, 30},
124 };
125 
126 /*****************************************************************************
127  * Public types/enumerations/variables
128  ****************************************************************************/
129 
133 volatile int tsc_init_done = 0;
138 #define GUI_BUF_ADDR 0x28050000
139 #define GUI_NUMBYTES ((1024 * 1024) * 2)
140 #define GUI_BLOCKSIZE (0x128)
141 #define GUI_BUF LOCATE_AT(GUI_BUF_ADDR)
143 GUI_BUF U32 GUI_Memory[GUI_NUMBYTES / sizeof(U32)];
144 U32 GUI_Memory_Size = GUI_NUMBYTES;
145 U32 GUI_Block_Size = GUI_BLOCKSIZE;
147 /*****************************************************************************
148  * Private functions
149  ****************************************************************************/
150 
153 static char *ipaddr_ntoa_r(uint32_t addr, char *buf, int buflen)
154 {
155  uint32_t s_addr;
156  char inv[3];
157  char *rp;
158  uint8_t *ap;
159  uint8_t rem;
160  uint8_t n;
161  uint8_t i;
162  int len = 0;
163 
164  s_addr = addr;
165  rp = buf;
166  ap = (uint8_t *) &s_addr;
167  for (n = 0; n < 4; n++) {
168  i = 0;
169  do {
170  rem = *ap % (uint8_t) 10;
171  *ap /= (uint8_t) 10;
172  inv[i++] = '0' + rem;
173  } while (*ap);
174  while (i--) {
175  if (len++ >= buflen) {
176  return NULL;
177  }
178  *rp++ = inv[i];
179  }
180  if (len++ >= buflen) {
181  return NULL;
182  }
183  *rp++ = '.';
184  ap++;
185  }
186  *--rp = 0;
187  return buf;
188 }
189 
193 static void lcd_update_values(WM_HWIN hWin_up)
194 {
195  WM_HWIN hItem;
196  static char *hostp, host_ip[16] = {0};
197  static char *remotep, remote_ip[16] = {0};
198  static uint32_t old_hostip = 1, old_remoteip = 1;
199 
200  if (old_hostip != host_ip_addr) {
201  /* Set Host IP Address */
202  hItem = WM_GetDialogItem(hWin_up, ID_EDIT_0);
203  EDIT_SetTextAlign(hItem, GUI_TA_HCENTER);
204  EDIT_SetBkColor(hItem, EDIT_CI_DISABLED, GUI_GREEN);
205  EDIT_SetTextColor(hItem, EDIT_CI_DISABLED, GUI_GREEN);
206  hostp = ipaddr_ntoa_r(host_ip_addr, host_ip, 16);
207  EDIT_SetText(hItem, hostp);
208  old_hostip = host_ip_addr;
209  }
210 
211  if (old_remoteip != remote_ip_addr) {
212  /* Set Remote IP address */
213  hItem = WM_GetDialogItem(hWin_up, ID_EDIT_1);
214  EDIT_SetTextAlign(hItem, GUI_TA_HCENTER);
215  EDIT_SetBkColor(hItem, EDIT_CI_DISABLED, GUI_RED);
216  EDIT_SetTextColor(hItem, EDIT_CI_DISABLED, GUI_RED);
217  remotep = ipaddr_ntoa_r(remote_ip_addr, remote_ip, 16);
218  EDIT_SetText(hItem, remotep);
219  old_remoteip = remote_ip_addr;
220  }
221 }
222 
226 static void _cbCallback(WM_MESSAGE *pMsg)
227 {
228  int NCode, Id;
229  WM_HWIN hDlg;
230  WM_HWIN hItem;
231  int fdig, sdig, tdig, frdig;
232  BUTTON_Handle hButton;
233  char acText[5] = {0};
234 
235  hDlg = pMsg->hWin;
236  switch (pMsg->MsgId) {
237  case WM_PAINT:
238  WM_DefaultProc(pMsg); /* Handle dialog items */
239  break;
240 
241  case WM_INIT_DIALOG:
242  FRAMEWIN_SetTextAlign(hDlg, GUI_TA_HCENTER | GUI_TA_VCENTER);
243  FRAMEWIN_SetFont(hDlg, GUI_FONT_24_ASCII);
244 
245  /* set the Text properties */
246  hItem = WM_GetDialogItem(pMsg->hWin, ID_TEXT_0);
247  TEXT_SetText(hItem, "NXP SEMICONDUCTORS");
248  TEXT_SetFont(hItem, &GUI_Font24B_ASCII);
249  TEXT_SetTextAlign(hItem, (GUI_TA_HCENTER));
250  TEXT_SetTextColor(hItem, GUI_YELLOW);
251 
252  hItem = WM_GetDialogItem(pMsg->hWin, ID_TEXT_1);
253  counter = counter % 10000;
254  fdig = counter / 1000;
255  sdig = (counter - (fdig * 1000)) / 100;
256  tdig = (counter - ((fdig * 1000) + (sdig * 100))) / 10;
257  frdig = counter % 10;
258  acText[0] = '0' + fdig;
259  acText[1] = '0' + sdig;
260  acText[2] = '0' + tdig;
261  acText[3] = '0' + frdig;
262  TEXT_SetFont(hItem, &GUI_Font8x16x1x2);
263  TEXT_SetTextAlign(hItem, (GUI_TA_HCENTER));
264  TEXT_SetTextColor(hItem, GUI_RED);
265  TEXT_SetText(hItem, acText);
266  break;
267 
268  case WM_KEY:
269  break;
270 
271  case WM_NOTIFY_PARENT:
272 
273  Id = WM_GetId(pMsg->hWinSrc); /* Id of widget */
274  NCode = pMsg->Data.v; /* Notification code */
275  switch (NCode) {
276  case WM_NOTIFICATION_RELEASED: /* React only if released */
277  if (Id == ID_BUTTON_0) { /* Start button */
278  hButton = WM_GetDialogItem(hDlg, ID_BUTTON_0);
279  start = 1;
280  WM_InvalidateWindow(hButton);
281  }
282 
283  if (Id == ID_BUTTON_1) { /* Stop button */
284  hButton = WM_GetDialogItem(hDlg, ID_BUTTON_1);
285  start = 0;
286  WM_InvalidateWindow(hButton);
287  }
288 
289  if (Id == ID_BUTTON_2) { /* Reset button */
290  hButton = WM_GetDialogItem(hDlg, ID_BUTTON_2);
291  counter = 0;
292  start = 0;
293  remote_ip_addr = 0;
294  WM_InvalidateWindow(hButton);
295  }
296  break;
297  }
298  break;
299 
300  default:
301  WM_DefaultProc(pMsg);
302  }
303 }
304 
308 static void lcd_update_remoteip(uint32_t new_remote_ip)
309 {
310  if (new_remote_ip != remote_ip_addr) {
311  remote_ip_addr = new_remote_ip;
312  }
313 }
314 
318 static void lcd_update_hostip(uint32_t host_ip)
319 {
320  WM_HWIN hItem;
321  static char name[] = {"HTTP Server Address"};
322  static char remote_name[] = {"Remote IP address"};
323 
324  if (host_ip != host_ip_addr) {
325  host_ip_addr = host_ip;
326  }
327 
328  /* Create HTTP server address Text box */
329  hItem = TEXT_CreateEx(24, 40, 150, 100, WM_GetClientWindow(hWin), WM_CF_SHOW, 0, ID_TEXT_2, name);
330  TEXT_SetFont(hItem, &GUI_Font16B_ASCII);
331  TEXT_SetTextAlign(hItem, GUI_TA_HCENTER);
332  TEXT_SetTextColor(hItem, GUI_DARKCYAN);
333 
334  /* Create Remote IP address Text box */
335  hItem = TEXT_CreateEx(24, 60, 150, 100, WM_GetClientWindow(hWin), WM_CF_SHOW, 0, ID_TEXT_3, remote_name);
336  TEXT_SetFont(hItem, &GUI_Font16B_ASCII);
337  TEXT_SetTextAlign(hItem, GUI_TA_HCENTER);
338  TEXT_SetTextColor(hItem, GUI_DARKGREEN);
339 
340  /* Create Host IP edit box */
341  hItem = EDIT_CreateEx(170, 40, 100, 20, WM_GetClientWindow(hWin), WM_CF_SHOW, 0, ID_EDIT_0, 16);
342 
343  /* Create Remote IP edit box */
344  hItem = EDIT_CreateEx(170, 60, 100, 20, WM_GetClientWindow(hWin), WM_CF_SHOW, 0, ID_EDIT_1, 16);
345 
346  /* Update values on LCD */
348 }
349 
353 #if (defined(OS_FREE_RTOS)) || (defined(OS_UCOS_III))
354 
355 /* LCd Display thread */
356 #ifdef OS_FREE_RTOS
357 static portTASK_FUNCTION(vLcdTask, pvParameters)
358 #elif OS_UCOS_III
359 static void lcd_app_task(void *arg)
360 #endif
361 {
362  WM_HWIN hItem;
363  int fdig, sdig, tdig, frdig;
364  char acText[5] = {0};
365 
366  GUI_Init();
367  WM_SetDesktopColor(GUI_GREEN);
368  WM_SetCreateFlags(WM_CF_MEMDEV);
369  hWin = GUI_CreateDialogBox(_aDialogCreate, GUI_COUNTOF(_aDialogCreate), &_cbCallback, 0, 0, 0);
370 
371  while (1) {
372  if (start) {
373  counter++;
374  if (counter > 9999) {
375  counter = 0;
376  }
377  }
378 
379  /* Print Count value */
380  hItem = WM_GetDialogItem(hWin, ID_TEXT_1);
381  counter = counter % 10000;
382  fdig = counter / 1000;
383  sdig = (counter - (fdig * 1000)) / 100;
384  tdig = (counter - ((fdig * 1000) + (sdig * 100))) / 10;
385  frdig = counter % 10;
386  acText[0] = '0' + fdig;
387  acText[1] = '0' + sdig;
388  acText[2] = '0' + tdig;
389  acText[3] = '0' + frdig;
390  TEXT_SetFont(hItem, &GUI_Font8x16x3x3);
391  TEXT_SetTextAlign(hItem, (GUI_TA_HCENTER));
392  TEXT_SetTextColor(hItem, GUI_RED);
393  TEXT_SetText(hItem, acText);
394 
396 
397  GUI_Delay(10);
398  }
399 }
400 
404 #ifdef OS_FREE_RTOS
405 static portTASK_FUNCTION(vTSCTask, pvParameters)
406 #elif OS_UCOS_III
407 static void tsc_app_task(void *arg)
408 #endif
409 {
410  int16_t tmp_x = -1, tmp_y = -1;
411  int16_t tmp_x1 = -1, tmp_y1 = -1;
412  static uint8_t pressed = 0;
413  bool touched;
414 #ifdef OS_UCOS_III
415  OS_ERR ret;
416 #endif
417 
418  while (1) {
419  /* Wait for TSC_CHECK_DELAY ms */
420 #ifdef OS_UCOS_III
421  OSTimeDlyHMSM(0, 0, TSC_CHECK_DELAY / 1000, TSC_CHECK_DELAY % 1000, OS_OPT_TIME_HMSM_STRICT, &ret);
422 #else
423  vTaskDelay(TSC_CHECK_DELAY);
424 #endif
425 
426  /* Check any Touch screen events */
427  touched = GetTouchPos((int16_t *) &tmp_x, (int16_t *) &tmp_y);
428  if (touched == true) {
429  if (pressed == 1) {
430  if ((tmp_x >= 0) && (tmp_y > 0) && ((tmp_x != old_tmp_x) || (tmp_y != old_tmp_y))) {
431  tmp_x1 = tmp_y;
432  tmp_y1 = tmp_x;
433  GUI_TOUCH_StoreState(320 - tmp_x1, tmp_y1);
434  old_tmp_x = tmp_x;
435  old_tmp_y = tmp_y;
436  }
437  }
438  else {
439  GUI_TOUCH_StoreState(320 - tmp_x1, tmp_y1);
440  old_tmp_x = tmp_x;
441  old_tmp_y = tmp_y;
442  pressed = 1;
443  }
444  }
445  else {
446  if (pressed == 1) {
447  GUI_TOUCH_StoreState(-1, -1);
448  pressed = 0;
449  }
450  }
451  }
452 }
453 
454 #endif
455 
456 /*****************************************************************************
457  * Public functions
458  ****************************************************************************/
459 
460 extern void SysTick_Enable(uint32_t period);
461 
462 /* emWin example initialization function */
463 void EMWIN_Init(void)
464 {
465  /* Initialise the LCD controller */
466  Board_LCD_Init();
472 
473  /* Register callbacks for LCD updates in IPC */
476 
477  #if !defined(OS_UCOS_III) && !defined(OS_FREE_RTOS)
478  /* Setup a 1mS sysTick for the primary time base */
479  SysTick_Enable(1);
480 
481  GUI_Init();
482 
483  WM_SetDesktopColor(GUI_GREEN);
484  WM_SetCreateFlags(WM_CF_MEMDEV);
485  hWin = GUI_CreateDialogBox(_aDialogCreate, GUI_COUNTOF(_aDialogCreate), &_cbCallback, 0, 0, 0);
486  GUI_Delay(10);
487 
488  #endif
489 
490  tsc_init_done = 1;
491 
492 }
493 
494 #if (defined(OS_FREE_RTOS)) || (defined(OS_UCOS_III))
495 /* OS specific dualcore emWin tasks */
496 void emwin_tasks(void)
497 {
498 #ifdef OS_FREE_RTOS
499  xTaskCreate(vTSCTask, (signed char *) "vTSCTask",
501  (xTaskHandle *) NULL);
502  xTaskCreate(vLcdTask, (signed char *) "vLCDTask",
504  (xTaskHandle *) NULL);
505 #elif OS_UCOS_III
506  OS_ERR os_err;
507  static OS_TCB lcd_app_taskTCB;
508  static CPU_STK lcd_app_taskSTK[UCOS_LCD_STACK_SZ];
509  static OS_TCB tsc_app_taskTCB;
510  static CPU_STK tsc_app_taskSTK[UCOS_TSC_STACK_SZ];
511 
512  OSTaskCreate((OS_TCB *) &lcd_app_taskTCB,
513  (CPU_CHAR *) "vLCDTask",
514  (OS_TASK_PTR) lcd_app_task,
515  (void *) 0,
516  (OS_PRIO) TASK_PRIO_LCD,
517  (CPU_STK *) &lcd_app_taskSTK[0],
518  (CPU_STK_SIZE) 32,
519  (CPU_STK_SIZE) UCOS_LCD_STACK_SZ,
520  (OS_MSG_QTY) 0u,
521  (OS_TICK) 0u,
522  (void *) 0,
523  (OS_OPT) (OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR),
524  (OS_ERR *) &os_err);
525  if (os_err != OS_ERR_NONE) {
526  printf("Unable to create vLCDTask task!\r\n");
527  while (1) ;
528  }
529  OSTaskCreate((OS_TCB *) &tsc_app_taskTCB,
530  (CPU_CHAR *) "vTSCTask",
531  (OS_TASK_PTR) tsc_app_task,
532  (void *) 0,
533  (OS_PRIO) TASK_PRIO_TOUCHSCREEN,
534  (CPU_STK *) &tsc_app_taskSTK[0],
535  (CPU_STK_SIZE) 32,
536  (CPU_STK_SIZE) UCOS_TSC_STACK_SZ,
537  (OS_MSG_QTY) 0u,
538  (OS_TICK) 0u,
539  (void *) 0,
540  (OS_OPT) (OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR),
541  (OS_ERR *) &os_err);
542  if (os_err != OS_ERR_NONE) {
543  printf("Unable to create vTSCTask task!\r\n");
544  while (1) ;
545  }
546 #endif
547 }
548 
549 #else
550 /* Standalone emWin dual core example task */
551 void emwin_tasks(void)
552 {
553  WM_HWIN hItem;
554  int fdig, sdig, tdig, frdig;
555  char acText[5] = {0};
556 
557  do {
558  if (start) {
559  counter++;
560  if (counter > 9999) {
561  counter = 0;
562  }
563  }
564 
565  /* Print Count value */
566  hItem = WM_GetDialogItem(hWin, ID_TEXT_1);
567  counter = counter % 10000;
568  fdig = counter / 1000;
569  sdig = (counter - (fdig * 1000)) / 100;
570  tdig = (counter - ((fdig * 1000) + (sdig * 100))) / 10;
571  frdig = counter % 10;
572  acText[0] = '0' + fdig;
573  acText[1] = '0' + sdig;
574  acText[2] = '0' + tdig;
575  acText[3] = '0' + frdig;
576  TEXT_SetFont(hItem, &GUI_Font8x16x3x3);
577  TEXT_SetTextAlign(hItem, (GUI_TA_HCENTER));
578  TEXT_SetTextColor(hItem, GUI_RED);
579  TEXT_SetText(hItem, acText);
580 
582  GUI_Exec();
583  } while (0);
584 }
585 
586 #endif
587