Added modified version of LineNoise, fixed _read.

Signed-off-by: Yilin Sun <imi415@imi.moe>
This commit is contained in:
Yilin Sun 2023-03-07 09:37:00 +08:00
parent b792c9b695
commit 861159b916
Signed by: imi415
GPG Key ID: 17F01E106F9F5E0A
5 changed files with 1284 additions and 1 deletions

View File

@ -61,6 +61,7 @@ set(TARGET_SOURCES
"board/clock_config.c"
"board/peripherals.c"
"board/pin_mux.c"
"lib/linenoise/linenoise.c"
"src/app_syscalls.c"
"src/main.c"
)
@ -83,6 +84,7 @@ set(TARGET_C_INCLUDES
"SDK/devices/LPC55S69/utilities/str"
"board"
"include"
"lib/linenoise"
)
# Shared libraries linked with application

1168
lib/linenoise/linenoise.c Normal file

File diff suppressed because it is too large Load Diff

75
lib/linenoise/linenoise.h Normal file
View File

@ -0,0 +1,75 @@
/* linenoise.h -- VERSION 1.0
*
* Guerrilla line editing library against the idea that a line editing lib
* needs to be 20,000 lines of C code.
*
* See linenoise.c for more information.
*
* ------------------------------------------------------------------------
*
* Copyright (c) 2010-2014, Salvatore Sanfilippo <antirez at gmail dot com>
* Copyright (c) 2010-2013, Pieter Noordhuis <pcnoordhuis at gmail dot com>
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __LINENOISE_H
#define __LINENOISE_H
#ifdef __cplusplus
extern "C" {
#endif
typedef struct linenoiseCompletions {
size_t len;
char **cvec;
} linenoiseCompletions;
typedef void(linenoiseCompletionCallback)(const char *, linenoiseCompletions *);
typedef char*(linenoiseHintsCallback)(const char *, int *color, int *bold);
typedef void(linenoiseFreeHintsCallback)(void *);
void linenoiseSetCompletionCallback(linenoiseCompletionCallback *);
void linenoiseSetHintsCallback(linenoiseHintsCallback *);
void linenoiseSetFreeHintsCallback(linenoiseFreeHintsCallback *);
void linenoiseAddCompletion(linenoiseCompletions *, const char *);
char *linenoise(const char *prompt);
void linenoiseFree(void *ptr);
int linenoiseHistoryAdd(const char *line);
int linenoiseHistorySetMaxLen(int len);
int linenoiseHistorySave(const char *filename);
int linenoiseHistoryLoad(const char *filename);
void linenoiseClearScreen(void);
void linenoiseSetMultiLine(int ml);
void linenoisePrintKeyCodes(void);
void linenoiseMaskModeEnable(void);
void linenoiseMaskModeDisable(void);
#ifdef __cplusplus
}
#endif
#endif /* __LINENOISE_H */

View File

@ -144,13 +144,36 @@ void __malloc_unlock(struct _reent *reent) {
}
int _read(int file, char *ptr, int len) {
if (file != STDIN_FILENO) {
return 0;
}
xSemaphoreTake(s_stdio_rx_lock, portMAX_DELAY);
USART_SetRxFifoWatermark(APP_STDIO_UART_INSTANCE, 0);
USART_EnableInterrupts(APP_STDIO_UART_INSTANCE, kUSART_RxLevelInterruptEnable);
xEventGroupWaitBits(s_stdio_event, APP_STDIO_UART_EVENT_RX_DONE, pdTRUE, pdFALSE, portMAX_DELAY);
uint8_t rx_level = USART_GetRxFifoCount(APP_STDIO_UART_INSTANCE);
if (rx_level > len) {
rx_level = len;
}
for (uint8_t i = 0; i < rx_level; i++) {
ptr[i] = USART_ReadByte(APP_STDIO_UART_INSTANCE);
}
xSemaphoreGive(s_stdio_rx_lock);
return 0;
return rx_level;
}
int _write(int file, char *buf, int len) {
if (file != STDOUT_FILENO) {
return 0;
}
xSemaphoreTake(s_stdio_tx_lock, portMAX_DELAY);
int xfer_count = len / APP_STDIO_UART_TX_BEAT_SIZE;
@ -190,6 +213,10 @@ int _write(int file, char *buf, int len) {
return len;
}
int _open(const char *path, int flags, ...) {
return -1;
}
int _close(int file) {
errno = -EBADF;
return -1;

View File

@ -12,6 +12,9 @@
#include "FreeRTOS.h"
#include "task.h"
/* LineNoise */
#include "linenoise.h"
/* App */
#include "app_syscalls.h"
@ -87,6 +90,14 @@ static void app_mrb_runtime_task(void *parameters) {
mrb_show_version(mrb);
mrb_show_copyright(mrb);
char *line;
while ((line = linenoise("hello> ")) != NULL) {
linenoiseHistoryAdd(line);
printf("You entered: %s\n", line);
free(line);
}
char mrb_code[] = "name = 'LPCXpresso55S69'\nputs \"Greetings #{name}, from MRuby #{RUBY_VERSION}.\"";
struct mrb_parser_state *parser_state = mrb_parse_string(mrb, mrb_code, cxt);