85 lines
1.7 KiB
C
85 lines
1.7 KiB
C
#include <malloc.h>
|
|
|
|
/* FreeRTOS */
|
|
#include "FreeRTOS.h"
|
|
#include "task.h"
|
|
|
|
/* LN */
|
|
#include "linenoise.h"
|
|
|
|
/* mruby */
|
|
#include "mruby.h"
|
|
#include "mruby/compile.h"
|
|
|
|
/* Private */
|
|
#include "app_mrb_repl.h"
|
|
|
|
static mrb_state *mrb;
|
|
static mrbc_context *cxt;
|
|
static struct mrb_parser_state *parser_state;
|
|
|
|
static char *prompt_str;
|
|
|
|
#define APP_B_TO_KF(x) ((float)x / 1024.0f)
|
|
|
|
static void app_mrb_repl_heap_usage(uint32_t *used, uint32_t *total) {
|
|
struct mallinfo m_info = mallinfo();
|
|
|
|
*used = m_info.uordblks;
|
|
*total = m_info.fordblks + m_info.uordblks;
|
|
}
|
|
|
|
int app_mrb_repl_init(void) {
|
|
prompt_str = malloc(64);
|
|
if (prompt_str == NULL) {
|
|
return -1;
|
|
}
|
|
|
|
mrb = mrb_open();
|
|
cxt = mrbc_context_new(mrb);
|
|
|
|
mrb_show_version(mrb);
|
|
mrb_show_copyright(mrb);
|
|
|
|
char mrb_code[] = "name = 'LPCXpresso55S69'\nputs \"Greetings #{name}, from MRuby #{RUBY_VERSION}.\"";
|
|
|
|
parser_state = mrb_parse_string(mrb, mrb_code, cxt);
|
|
|
|
mrb_load_exec(mrb, parser_state, cxt);
|
|
|
|
return 0;
|
|
}
|
|
|
|
int app_mrb_repl_deinit(void) {
|
|
linenoiseAtExit();
|
|
|
|
free(prompt_str);
|
|
|
|
mrbc_cleanup_local_variables(mrb, cxt);
|
|
mrbc_context_free(mrb, cxt);
|
|
mrb_close(mrb);
|
|
|
|
return 0;
|
|
}
|
|
|
|
int app_mrb_repl_exec(bool *exit) {
|
|
char *line;
|
|
uint32_t heap_used, heap_total;
|
|
|
|
app_mrb_repl_heap_usage(&heap_used, &heap_total);
|
|
snprintf(prompt_str, 64, "[%4.02lfkiB/%4.02lfkiB](irb)> ", APP_B_TO_KF(heap_used), APP_B_TO_KF(heap_total));
|
|
|
|
line = linenoise(prompt_str);
|
|
|
|
if(line == NULL) {
|
|
*exit = true;
|
|
return 0;
|
|
}
|
|
|
|
linenoiseHistoryAdd(line);
|
|
printf("You entered: %s\n", line);
|
|
free(line);
|
|
|
|
*exit = false;
|
|
return 0;
|
|
} |