nshell/src/main.c

147 lines
4.1 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 volatile int key_poll_timeout;
static int callback_keypoll(void) {
key_poll_timeout = 0;
return TIMER_STOP;
}
static void wn_write(WrenVM *vm, const char *text) { term_print(text); }
static void wn_error(WrenVM *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;
}
}
static void *wn_reallocate(void *memory, size_t newSize, void *_) {
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);
}
int main(int isappli, int optnum) {
int tick_ctr = 0;
int shift_state = 0;
int alpha_state = 0;
int off_state = 0;
uint16_t backlight_save;
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);
WrenInterpretResult result = wrenInterpret(vm, "my_module",
"System.print(\"I am running in a VM!\")\n"
"System.trigger_error_from_vm()");
wrenFreeVM(vm);
set_statusbar(tick_ctr, shift_state, alpha_state, get_battery_voltage());
set_menubar();
tgrid_display();
while (1) {
const int timer = timer_configure(TIMER_ANY, 20000, GINT_CALL(callback_keypoll));
key_poll_timeout = 1;
key_event_t kev = getkey_opt(GETKEY_NONE, &key_poll_timeout);
timer_start(timer);
if (kev.type == KEYEV_DOWN) {
if (off_state) {
if (kev.key == KEY_ACON) {
off_state = 0;
// reset backlight
r61524_set(0x5a1, backlight_save & 0x00ff);
} else {
continue;
}
}
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) {
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;
continue;
}
if (kev.key == KEY_MENU && !shift_state && !alpha_state) {
// TODO: print pause menu
gint_osmenu();
}
if (kev.key == KEY_MENU && shift_state && !alpha_state) {
return 1;
}
term_printf("tick_ctr=%d key=0x%x mod=%d shift=%d alpha=%d\n", tick_ctr, kev.key, kev.mod, kev.shift, kev.alpha);
set_statusbar(tick_ctr, shift_state, alpha_state, get_battery_voltage());
set_menubar();
tgrid_display();
tick_ctr++;
}
}
return 1;
}