Skip to content

Commit 81d8086

Browse files
committed
Clean up. Tested OK
1 parent 2f6e842 commit 81d8086

File tree

1 file changed

+146
-64
lines changed

1 file changed

+146
-64
lines changed

examples/lvglterm/lvglterm.c

Lines changed: 146 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@
8686
* Private Function Prototypes
8787
****************************************************************************/
8888

89-
static bool has_input(int fd);
90-
static void timer_callback(lv_timer_t * timer);
9189
static int create_widgets(void);
90+
static void timer_callback(lv_timer_t * timer);
9291
static void input_callback(lv_event_t * e);
92+
static bool has_input(int fd);
9393
static void remove_escape_codes(char *buf, int len);
9494

9595
/****************************************************************************
@@ -127,7 +127,21 @@ static lv_timer_t *g_timer;
127127
* Private Functions
128128
****************************************************************************/
129129

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+
131145
static int create_terminal(void)
132146
{
133147
int ret;
@@ -167,7 +181,7 @@ static int create_terminal(void)
167181
dup2(g_nsh_stdout[WRITE_PIPE], 1);
168182
dup2(g_nsh_stderr[WRITE_PIPE], 2);
169183

170-
/* Start the NSH Shell and inherit the pipes */
184+
/* Start the NSH Shell and inherit stdin, stdout and stderr */
171185

172186
pid_t pid = task_create("NSH Console",
173187
CONFIG_EXAMPLES_LVGLTERM_PRIORITY,
@@ -199,57 +213,20 @@ static int create_terminal(void)
199213
return OK;
200214
}
201215

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+
****************************************************************************/
251229

252-
// Create the LVGL Widgets for the LVGL Terminal
253230
static int create_widgets(void)
254231
{
255232
/* Set the Font Style for NSH Input and Output to a Monospaced Font */
@@ -297,7 +274,83 @@ static int create_widgets(void)
297274
return OK;
298275
}
299276

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+
301354
static void input_callback(lv_event_t *e)
302355
{
303356
int ret;
@@ -349,12 +402,25 @@ static void input_callback(lv_event_t *e)
349402
}
350403
}
351404

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+
353419
static bool has_input(int fd)
354420
{
355421
int ret;
356422

357-
/* Poll the File Descriptor for Input */
423+
/* Poll the File Descriptor for input */
358424

359425
struct pollfd fdp;
360426
fdp.fd = fd;
@@ -365,28 +431,28 @@ static bool has_input(int fd)
365431

366432
if (ret > 0)
367433
{
368-
/* If Poll is OK and there is Input */
434+
/* If poll is OK and there is input */
369435

370436
if ((fdp.revents & POLLIN) != 0)
371437
{
372-
/* Report that there's Input */
438+
/* Report that there is input */
373439

374440
return true;
375441
}
376442

377-
/* Else report No Input */
443+
/* Else report no input */
378444

379445
return false;
380446
}
381447
else if (ret == 0)
382448
{
383-
/* If Timeout, report No Input */
449+
/* If timeout, report no input */
384450

385451
return false;
386452
}
387453
else if (ret < 0)
388454
{
389-
/* Handle Error */
455+
/* Handle error */
390456

391457
_err("poll failed: %d, fd=%d\n", ret, fd);
392458
return false;
@@ -398,7 +464,21 @@ static bool has_input(int fd)
398464
return false;
399465
}
400466

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+
402482
static void remove_escape_codes(char *buf, int len)
403483
{
404484
int i;
@@ -428,7 +508,9 @@ static void remove_escape_codes(char *buf, int len)
428508
* Name: main or lvglterm_main
429509
*
430510
* 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.
432514
*
433515
* Input Parameters:
434516
* Standard argc and argv

0 commit comments

Comments
 (0)