nshell/src/main.c

161 lines
3.9 KiB
C
Raw Normal View History

2021-09-09 23:43:50 +02:00
#include <stdio.h>
2021-09-08 22:16:40 +02:00
#include <gint/cpu.h>
2021-09-02 19:07:14 +02:00
#include <gint/display.h>
2021-09-02 19:41:13 +02:00
#include <gint/drivers/r61524.h>
2021-09-01 18:25:59 +02:00
#include <gint/gint.h>
2021-08-31 15:52:53 +02:00
#include <gint/keyboard.h>
2021-09-10 00:48:53 +02:00
#include <gint/kmalloc.h>
2021-09-08 22:16:40 +02:00
#include <gint/mpu/power.h>
2021-08-31 15:52:53 +02:00
#include <gint/timer.h>
2021-09-09 23:43:50 +02:00
#include <wren.h>
2021-09-02 19:07:14 +02:00
#include "battery.h"
2021-08-31 15:52:53 +02:00
#include "term.h"
2021-09-01 11:04:31 +02:00
#include "ui.h"
2021-08-31 15:52:53 +02:00
2021-09-10 21:43:19 +02:00
static void wn_write(WrenVM __attribute__((unused)) * vm, const char *text) { term_print(text); }
static void wn_error(WrenVM __attribute__((unused)) * vm, WrenErrorType errorType, const char *module, const int line,
const char *msg) {
2021-09-09 23:43:50 +02:00
switch (errorType) {
2021-09-10 20:27:06 +02:00
case WREN_ERROR_COMPILE:
2021-09-09 23:43:50 +02:00
term_printf("[%s line %d] [Error] %s\n", module, line, msg);
2021-09-10 20:27:06 +02:00
break;
case WREN_ERROR_STACK_TRACE:
2021-09-09 23:43:50 +02:00
term_eprintf("[%s line %d] in %s\n", module, line, msg);
2021-09-10 20:27:06 +02:00
break;
case WREN_ERROR_RUNTIME:
2021-09-09 23:43:50 +02:00
term_eprintf("[Runtime Error] %s\n", msg);
2021-09-10 20:27:06 +02:00
break;
2021-09-10 21:43:19 +02:00
default:
break;
2021-09-09 23:43:50 +02:00
}
2021-09-08 19:22:36 +02:00
}
2021-09-02 19:41:13 +02:00
2021-09-10 21:43:19 +02:00
static void *wn_reallocate(void *memory, size_t newSize, __attribute__((unused)) void *userData) {
2021-09-10 00:48:53 +02:00
if (memory == NULL && newSize == 0)
return NULL;
if (memory == NULL)
return kmalloc(newSize, "_uram");
if (newSize == 0) {
kfree(memory);
return NULL;
}
return krealloc(memory, newSize);
}
2021-09-10 20:27:06 +02:00
static int tick_ctr = 0;
static int shift_state = 0;
static int alpha_state = 0;
static int off_state = 0;
static int timer_redraw;
static volatile int must_redraw;
static int callback_redraw(void) {
must_redraw = 1;
return TIMER_CONTINUE;
}
static void check_keyevents(void) {
static uint16_t backlight_save;
key_event_t kev = pollevent();
if (kev.type != KEYEV_DOWN)
return;
if (off_state) {
if (kev.key != KEY_ACON)
return;
off_state = 0;
// reset backlight
r61524_set(0x5a1, backlight_save & 0x00ff);
timer_start(timer_redraw);
}
term_printf("[%d] key event: key=0x%x mod=%d shift=%d alpha=%d\n", tick_ctr, kev.key, kev.mod, kev.shift, kev.alpha);
if (kev.key == KEY_SHIFT)
shift_state = !shift_state;
2021-08-31 15:52:53 +02:00
2021-09-10 20:27:06 +02:00
if (kev.key == KEY_ALPHA)
alpha_state = !alpha_state;
if (kev.key == KEY_ACON && shift_state) {
timer_pause(timer_redraw);
off_state = 1;
// save, then turn backlight level off:
// force to display something
dclear(C_WHITE);
dupdate();
// store the backlight pwm
backlight_save = r61524_get(0x5a1) & 0x00ff;
// force vram clear
dclear(C_BLACK);
dupdate();
dclear(C_BLACK);
dupdate();
// force backlight pwm to 0
r61524_set(0x5a1, 0);
shift_state = 0;
alpha_state = 0;
return;
}
if (kev.key == KEY_MENU && !shift_state && !alpha_state) {
// TODO: print pause menu
gint_osmenu();
}
}
2021-09-10 21:43:19 +02:00
int main(__attribute__((unused)) int isappli, __attribute__((unused)) int optnum) {
2021-09-09 23:43:50 +02:00
WrenConfiguration config;
wrenInitConfiguration(&config);
2021-09-10 00:48:53 +02:00
config.reallocateFn = &wn_reallocate;
config.initialHeapSize = 1024 * 4; // 4kiB
config.minHeapSize = 1024; // 1kiB
config.heapGrowthPercent = 20;
config.writeFn = &wn_write;
config.errorFn = &wn_error;
2021-09-09 23:43:50 +02:00
WrenVM *vm = wrenNewVM(&config);
2021-09-10 21:43:19 +02:00
wrenInterpret(vm, "my_module",
"System.print(\"I am running in a VM!\")\n"
"System.trigger_error_from_vm()");
2021-09-09 23:43:50 +02:00
wrenFreeVM(vm);
2021-09-10 20:27:06 +02:00
timer_redraw = timer_configure(TIMER_ANY, 31250, GINT_CALL(callback_redraw)); // 32 Hz
timer_start(timer_redraw);
2021-09-01 14:24:10 +02:00
2021-08-31 15:52:53 +02:00
while (1) {
2021-09-10 20:27:06 +02:00
check_keyevents();
if (off_state) {
sleep();
continue;
}
2021-09-02 19:04:01 +02:00
2021-09-10 20:27:06 +02:00
if (must_redraw) {
2021-09-02 19:04:01 +02:00
set_statusbar(tick_ctr, shift_state, alpha_state, get_battery_voltage());
2021-09-01 14:28:01 +02:00
set_menubar();
2021-09-02 19:04:01 +02:00
2021-09-10 20:27:06 +02:00
dclear(C_BLACK);
2021-09-01 14:28:01 +02:00
tgrid_display();
2021-09-10 20:27:06 +02:00
dupdate();
2021-09-01 14:24:10 +02:00
2021-09-10 20:27:06 +02:00
must_redraw = 0;
2021-09-01 14:24:10 +02:00
}
2021-09-10 20:27:06 +02:00
tick_ctr++;
2021-08-31 15:52:53 +02:00
}
return 1;
}