Skip to content

Commit 5370f51

Browse files
committed
Add optional buffered input for terminal
1 parent ac7f5f9 commit 5370f51

File tree

3 files changed

+106
-1
lines changed

3 files changed

+106
-1
lines changed

minichlink/minichlink.c

+87
Original file line numberDiff line numberDiff line change
@@ -366,13 +366,72 @@ int main( int argc, char ** argv )
366366

367367
CaptureKeyboardInput();
368368

369+
#if TERMINAL_INPUT_BUFFER
370+
char pline_buf[256]; // Buffer that contains current line that is being printed to
371+
char input_buf[128]; // Buffer that contains user input until it is sent out
372+
memset( pline_buf, 0, sizeof( pline_buf ) );
373+
memset( input_buf, 0, sizeof( input_buf ) );
374+
uint8_t input_pos = 0;
375+
uint8_t to_send = 0;
376+
#endif
377+
printf( "Terminal started\n\n" );
369378
uint32_t appendword = 0;
370379
do
371380
{
372381
uint8_t buffer[256];
382+
#if TERMINAL_INPUT_BUFFER
383+
char print_buf[300]; // Buffer that is filled with everything and will be written to stdout (basically it's for formatting)
384+
uint8_t update = 0;
385+
#endif
373386
if( !IsGDBServerInShadowHaltState( dev ) )
374387
{
375388
// Handle keyboard input.
389+
#if TERMINAL_INPUT_BUFFER
390+
if( IsKBHit() && to_send == 0 )
391+
{
392+
uint8_t c = ReadKBByte();
393+
if ( c == 8 || c == 127 )
394+
{
395+
input_buf[input_pos - 1] = 0;
396+
if ( input_pos > 0 ) input_pos--;
397+
}
398+
else if ( c > 31 && c < 127 )
399+
{
400+
input_buf[input_pos] = c;
401+
input_pos++;
402+
}
403+
else if ( c == '\n' || c == 10 )
404+
{
405+
to_send = input_pos;
406+
}
407+
update = 1;
408+
}
409+
// Process incomming buffer during sending
410+
if( to_send > 0 && appendword == 0 )
411+
{
412+
for( int i = 0; i < 3; i++ )
413+
{
414+
appendword |= input_buf[input_pos - to_send] << ( i * 8 + 8 );
415+
to_send--;
416+
if ( to_send == 0 ) break;
417+
}
418+
if( to_send == 0 )
419+
{
420+
strcpy( print_buf, TERMINAL_CLEAR_CUR );
421+
strcat( print_buf, TERMIANL_INPUT_SENT );
422+
strcat( print_buf, input_buf );
423+
strcat( print_buf, "\n" );
424+
strcat( print_buf, pline_buf );
425+
strcat( print_buf, TERMINAL_SEND_LABEL );
426+
fwrite( print_buf, strlen( print_buf ), 1, stdout );
427+
fflush( stdout );
428+
input_pos = 0;
429+
input_buf[0] = 0;
430+
// memset( input_buf, 0, sizeof( input_buf ) );
431+
}
432+
appendword |= i + 4;
433+
}
434+
#else
376435
if( appendword == 0 )
377436
{
378437
int i;
@@ -383,7 +442,20 @@ int main( int argc, char ** argv )
383442
}
384443
appendword |= i+4; // Will go into DATA0.
385444
}
445+
#endif
446+
386447
int r = MCF.PollTerminal( dev, buffer, sizeof( buffer ), appendword, 0 );
448+
#if TERMINAL_INPUT_BUFFER
449+
if( ( r == -1 || r == 0 ) && update > 0 )
450+
{
451+
strcpy( print_buf, TERMINAL_CLEAR_CUR );
452+
if ( to_send > 0 ) strcat( print_buf, TERMINAL_DIM );
453+
strcat( print_buf, TERMINAL_SEND_LABEL );
454+
strcat( print_buf, input_buf );
455+
fwrite( print_buf, strlen( print_buf ), 1, stdout );
456+
fflush( stdout );
457+
}
458+
#endif
387459
if( r < -5 )
388460
{
389461
fprintf( stderr, "Terminal dead. code %d\n", r );
@@ -396,7 +468,22 @@ int main( int argc, char ** argv )
396468
}
397469
else if( r > 0 )
398470
{
471+
#if TERMINAL_INPUT_BUFFER
472+
uint8_t new_line = 0;
473+
if( buffer[r - 1] == '\n' ) new_line = 1;
474+
if( new_line == 0 ) strcpy( print_buf, TERMINAL_CLEAR_PREV ); // Go one line up and erase it
475+
else strcpy( print_buf, TERMINAL_CLEAR_CUR ); // Go to the start of the line and erase it
476+
strncat( pline_buf, (char *)buffer, r ); // Add newely received chars to line buffer
477+
strcat( print_buf, pline_buf ); // Add line to buffer
478+
if( to_send > 0 ) strcat( print_buf, TERMINAL_DIM );
479+
strcat( print_buf, TERMINAL_SEND_LABEL ); // Print styled "Send" label
480+
strcat( print_buf, input_buf ); // Print current input
481+
fwrite( print_buf, strlen( print_buf ), 1, stdout );
482+
print_buf[0] = 0;
483+
if( new_line == 1 ) pline_buf[0] = 0;
484+
#else
399485
fwrite( buffer, r, 1, stdout );
486+
#endif
400487
fflush( stdout );
401488
// Otherwise it's basically just an ack for appendword.
402489
appendword = 0;

minichlink/minichlink.h

+18
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,24 @@ struct InternalState
195195
#define DLLDECORATE
196196
#endif
197197

198+
#define TERMINAL_INPUT_BUFFER 1
199+
200+
#define STR_(x) #x
201+
#define STR(x) STR_(x)
202+
203+
#ifndef TERMINAL_ACCENT_COLOR
204+
#define TERMINAL_ACCENT_COLOR 5;208 // Chose color from predefined palette
205+
// #define TERMINAL_ACCENT_COLOR 2;180;11;64 // Use R;G;B for color (can't be dimmed though)
206+
#endif
207+
208+
#define TERMIANL_INPUT_SENT "\x1b[1F\x1b[2K\x1b[2K\033[38;" STR(TERMINAL_ACCENT_COLOR) "m> "
209+
#define TERMINAL_SEND_LABEL_N "\033[7m\033[1m\033[38;" STR(TERMINAL_ACCENT_COLOR) "mSend:\x1b[0m "
210+
#define TERMINAL_SEND_LABEL "\n\x1b[2K\033[7m\033[1m\033[38;" STR(TERMINAL_ACCENT_COLOR) "mSend:\x1b[0m "
211+
#define TERMINAL_SEND_BUSY "\n\x1b[2K\033[7m\033[1m\033[2m\033[38;" STR(TERMINAL_ACCENT_COLOR) "mSend:\x1b[0m "
212+
#define TERMINAL_CLEAR_PREV "\x1b[1F\x1b[2K"
213+
#define TERMINAL_CLEAR_CUR "\x1b[2K\x1b[F"
214+
#define TERMINAL_DIM "\x1b[2m"
215+
198216
/* initialization hints for init functions */
199217
/* could be expanded with more in the future (e.g., PID/VID hints, priorities, ...)*/
200218
/* not all init functions currently need these hints. */

minichlink/terminalhelp.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ static int ReadKBByte()
9696

9797
static void CtrlC()
9898
{
99-
fprintf( stderr, "Minichlink Closing\n" );
99+
fprintf( stderr, "\nMinichlink Closing\n" );
100100
ResetKeyboardInput();
101101
exit( 0 );
102102
}

0 commit comments

Comments
 (0)