86
86
* Private Function Prototypes
87
87
****************************************************************************/
88
88
89
- static bool has_input (int fd );
90
- static void timer_callback (lv_timer_t * timer );
91
89
static int create_widgets (void );
90
+ static void timer_callback (lv_timer_t * timer );
92
91
static void input_callback (lv_event_t * e );
92
+ static bool has_input (int fd );
93
93
static void remove_escape_codes (char * buf , int len );
94
94
95
95
/****************************************************************************
@@ -127,7 +127,21 @@ static lv_timer_t *g_timer;
127
127
* Private Functions
128
128
****************************************************************************/
129
129
130
- // Create an LVGL Terminal that will let us interact with NSH Shell
130
+ /****************************************************************************
131
+ * Name: create_terminal
132
+ *
133
+ * Description:
134
+ * Create the LVGL Terminal. Start the NSH Shell and redirect the NSH
135
+ * stdin, stdout and stderr to the LVGL Widgets.
136
+ *
137
+ * Input Parameters:
138
+ * None
139
+ *
140
+ * Returned Value:
141
+ * Zero (OK) on success; a negated errno value is returned on any failure.
142
+ *
143
+ ****************************************************************************/
144
+
131
145
static int create_terminal (void )
132
146
{
133
147
int ret ;
@@ -167,7 +181,7 @@ static int create_terminal(void)
167
181
dup2 (g_nsh_stdout [WRITE_PIPE ], 1 );
168
182
dup2 (g_nsh_stderr [WRITE_PIPE ], 2 );
169
183
170
- /* Start the NSH Shell and inherit the pipes */
184
+ /* Start the NSH Shell and inherit stdin, stdout and stderr */
171
185
172
186
pid_t pid = task_create ("NSH Console" ,
173
187
CONFIG_EXAMPLES_LVGLTERM_PRIORITY ,
@@ -199,57 +213,20 @@ static int create_terminal(void)
199
213
return OK ;
200
214
}
201
215
202
- // Callback Function for LVGL Timer.
203
- static void timer_callback (lv_timer_t * timer )
204
- {
205
- int ret ;
206
- static char buf [64 ];
207
-
208
- DEBUGASSERT (g_nsh_stdout [READ_PIPE ] != 0 );
209
- DEBUGASSERT (g_nsh_stderr [READ_PIPE ] != 0 );
210
-
211
- /* Poll NSH stdout to check if there's output to be processed */
212
-
213
- if (has_input (g_nsh_stdout [READ_PIPE ]))
214
- {
215
- /* Read the output from NSH stdout */
216
-
217
- ret = read (g_nsh_stdout [READ_PIPE ], buf , sizeof (buf ) - 1 );
218
-
219
- /* Add to NSH Output Text Area */
220
-
221
- if (ret > 0 )
222
- {
223
- buf [ret ] = 0 ;
224
- remove_escape_codes (buf , ret );
225
-
226
- DEBUGASSERT (g_output != NULL );
227
- lv_textarea_add_text (g_output , buf );
228
- }
229
- }
230
-
231
- /* Poll NSH stderr to check if there's output to be processed */
232
-
233
- if (has_input (g_nsh_stderr [READ_PIPE ]))
234
- {
235
- /* Read the output from NSH stderr */
236
-
237
- ret = read (g_nsh_stderr [READ_PIPE ], buf , sizeof (buf ) - 1 );
238
-
239
- /* Add to NSH Output Text Area */
240
-
241
- if (ret > 0 )
242
- {
243
- buf [ret ] = 0 ;
244
- remove_escape_codes (buf , ret );
245
-
246
- DEBUGASSERT (g_output != NULL );
247
- lv_textarea_add_text (g_output , buf );
248
- }
249
- }
250
- }
216
+ /****************************************************************************
217
+ * Name: create_widgets
218
+ *
219
+ * Description:
220
+ * Create the LVGL Widgets for LVGL Terminal.
221
+ *
222
+ * Input Parameters:
223
+ * None
224
+ *
225
+ * Returned Value:
226
+ * Zero (OK) on success; a negated errno value is returned on any failure.
227
+ *
228
+ ****************************************************************************/
251
229
252
- // Create the LVGL Widgets for the LVGL Terminal
253
230
static int create_widgets (void )
254
231
{
255
232
/* Set the Font Style for NSH Input and Output to a Monospaced Font */
@@ -297,7 +274,83 @@ static int create_widgets(void)
297
274
return OK ;
298
275
}
299
276
300
- // Callback Function for NSH Input Text Area.
277
+ /****************************************************************************
278
+ * Name: timer_callback
279
+ *
280
+ * Description:
281
+ * Callback Function for LVGL Timer. Poll NSH stdout and stderr for output
282
+ * and display the output.
283
+ *
284
+ * Input Parameters:
285
+ * timer - LVGL Timer for the callback
286
+ *
287
+ * Returned Value:
288
+ * None
289
+ *
290
+ ****************************************************************************/
291
+
292
+ static void timer_callback (lv_timer_t * timer )
293
+ {
294
+ int ret ;
295
+ static char buf [64 ];
296
+
297
+ DEBUGASSERT (g_nsh_stdout [READ_PIPE ] != 0 );
298
+ DEBUGASSERT (g_nsh_stderr [READ_PIPE ] != 0 );
299
+
300
+ /* Poll NSH stdout to check if there's output to be processed */
301
+
302
+ if (has_input (g_nsh_stdout [READ_PIPE ]))
303
+ {
304
+ /* Read the output from NSH stdout */
305
+
306
+ ret = read (g_nsh_stdout [READ_PIPE ], buf , sizeof (buf ) - 1 );
307
+ if (ret > 0 )
308
+ {
309
+ /* Add to NSH Output Text Area */
310
+
311
+ buf [ret ] = 0 ;
312
+ remove_escape_codes (buf , ret );
313
+
314
+ DEBUGASSERT (g_output != NULL );
315
+ lv_textarea_add_text (g_output , buf );
316
+ }
317
+ }
318
+
319
+ /* Poll NSH stderr to check if there's output to be processed */
320
+
321
+ if (has_input (g_nsh_stderr [READ_PIPE ]))
322
+ {
323
+ /* Read the output from NSH stderr */
324
+
325
+ ret = read (g_nsh_stderr [READ_PIPE ], buf , sizeof (buf ) - 1 );
326
+ if (ret > 0 )
327
+ {
328
+ /* Add to NSH Output Text Area */
329
+
330
+ buf [ret ] = 0 ;
331
+ remove_escape_codes (buf , ret );
332
+
333
+ DEBUGASSERT (g_output != NULL );
334
+ lv_textarea_add_text (g_output , buf );
335
+ }
336
+ }
337
+ }
338
+
339
+ /****************************************************************************
340
+ * Name: input_callback
341
+ *
342
+ * Description:
343
+ * Callback Function for NSH Input Text Area. If Enter Key was pressed,
344
+ * send the NSH Input Command to NSH stdin.
345
+ *
346
+ * Input Parameters:
347
+ * e - LVGL Event for the callback
348
+ *
349
+ * Returned Value:
350
+ * None
351
+ *
352
+ ****************************************************************************/
353
+
301
354
static void input_callback (lv_event_t * e )
302
355
{
303
356
int ret ;
@@ -349,12 +402,25 @@ static void input_callback(lv_event_t *e)
349
402
}
350
403
}
351
404
352
- // Return true if the File Descriptor has data to be read
405
+ /****************************************************************************
406
+ * Name: has_input
407
+ *
408
+ * Description:
409
+ * Return true if the File Descriptor has data to be read.
410
+ *
411
+ * Input Parameters:
412
+ * fd - File Descriptor to be checked
413
+ *
414
+ * Returned Value:
415
+ * True if File Descriptor has data to be read; False otherwise
416
+ *
417
+ ****************************************************************************/
418
+
353
419
static bool has_input (int fd )
354
420
{
355
421
int ret ;
356
422
357
- /* Poll the File Descriptor for Input */
423
+ /* Poll the File Descriptor for input */
358
424
359
425
struct pollfd fdp ;
360
426
fdp .fd = fd ;
@@ -365,28 +431,28 @@ static bool has_input(int fd)
365
431
366
432
if (ret > 0 )
367
433
{
368
- /* If Poll is OK and there is Input */
434
+ /* If poll is OK and there is input */
369
435
370
436
if ((fdp .revents & POLLIN ) != 0 )
371
437
{
372
- /* Report that there's Input */
438
+ /* Report that there is input */
373
439
374
440
return true;
375
441
}
376
442
377
- /* Else report No Input */
443
+ /* Else report no input */
378
444
379
445
return false;
380
446
}
381
447
else if (ret == 0 )
382
448
{
383
- /* If Timeout , report No Input */
449
+ /* If timeout , report no input */
384
450
385
451
return false;
386
452
}
387
453
else if (ret < 0 )
388
454
{
389
- /* Handle Error */
455
+ /* Handle error */
390
456
391
457
_err ("poll failed: %d, fd=%d\n" , ret , fd );
392
458
return false;
@@ -398,7 +464,21 @@ static bool has_input(int fd)
398
464
return false;
399
465
}
400
466
401
- // Remove Escape Codes from the string. Assumes that buf[len] is 0.
467
+ /****************************************************************************
468
+ * Name: remove_escape_codes
469
+ *
470
+ * Description:
471
+ * Remove ANSI Escape Codes from the string. Assumes that buf[len] is 0.
472
+ *
473
+ * Input Parameters:
474
+ * buf - String to be processed
475
+ * len - Length of string
476
+ *
477
+ * Returned Value:
478
+ * None
479
+ *
480
+ ****************************************************************************/
481
+
402
482
static void remove_escape_codes (char * buf , int len )
403
483
{
404
484
int i ;
@@ -428,7 +508,9 @@ static void remove_escape_codes(char *buf, int len)
428
508
* Name: main or lvglterm_main
429
509
*
430
510
* Description:
431
- * Create an LVGL Terminal that will let us interact with NSH Shell.
511
+ * Start an LVGL Terminal that runs interactive commands with NSH Shell.
512
+ * NSH Commands are entered through an LVGL Keyboard. NSH Output is
513
+ * rendered in an LVGL Widget.
432
514
*
433
515
* Input Parameters:
434
516
* Standard argc and argv
0 commit comments