nshell/src/main.c

161 lines
3.9 KiB
C

#include <stdio.h>
#include <gint/cpu.h>
#include <gint/display.h>
#include <gint/drivers/r61524.h>
#include <gint/gint.h>
#include <gint/keyboard.h>
#include <gint/kmalloc.h>
#include <gint/mpu/power.h>
#include <gint/timer.h>
#include <wren.h>
#include "battery.h"
#include "term.h"
#include "ui.h"
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) {
switch (errorType) {
case WREN_ERROR_COMPILE:
term_printf("[%s line %d] [Error] %s\n", module, line, msg);
break;
case WREN_ERROR_STACK_TRACE:
term_eprintf("[%s line %d] in %s\n", module, line, msg);
break;
case WREN_ERROR_RUNTIME:
term_eprintf("[Runtime Error] %s\n", msg);
break;
default:
break;
}
}
static void *wn_reallocate(void *memory, size_t newSize, __attribute__((unused)) void *userData) {
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);
}
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_kprintf("key event: key=0x%x mod=%d shift=%d alpha=%d", kev.key, kev.mod, kev.shift, kev.alpha);
if (kev.key == KEY_SHIFT)
shift_state = !shift_state;
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();
}
}
int main(__attribute__((unused)) int isappli, __attribute__((unused)) int optnum) {
WrenConfiguration config;
wrenInitConfiguration(&config);
config.reallocateFn = &wn_reallocate;
config.initialHeapSize = 1024 * 4; // 4kiB
config.minHeapSize = 1024; // 1kiB
config.heapGrowthPercent = 20;
config.writeFn = &wn_write;
config.errorFn = &wn_error;
WrenVM *vm = wrenNewVM(&config);
wrenInterpret(vm, "my_module",
"System.print(\"I am running in a VM!\")\n"
"System.trigger_error_from_vm()");
wrenFreeVM(vm);
timer_redraw = timer_configure(TIMER_ANY, 31250, GINT_CALL(callback_redraw)); // 32 Hz
timer_start(timer_redraw);
while (1) {
check_keyevents();
if (off_state) {
sleep();
continue;
}
if (must_redraw) {
set_statusbar(tick_ctr, shift_state, alpha_state, get_battery_voltage());
set_menubar();
dclear(C_BLACK);
tgrid_display();
dupdate();
must_redraw = 0;
}
tick_ctr++;
}
return 1;
}