1
0
Fork 0
mirror of https://github.com/suchmememanyskill/TegraExplorer.git synced 2024-11-08 13:11:54 +00:00

[script] add some util functions

functions added: return, exit, printPos, clearscreen, drawBox, color, pause, exit
This commit is contained in:
suchmememanyskill 2021-01-02 12:43:15 +01:00
parent e0c62cbec4
commit e1491da4ad
3 changed files with 131 additions and 0 deletions

View file

@ -8,6 +8,9 @@
#include <storage/nx_sd.h>
#include "../fs/fsutils.h"
#include "../gfx/gfxutils.h"
#include "../hid/hid.h"
#include <utils/util.h>
#define scriptFunction(name) Variable_t name(scriptCtx_t *ctx, Variable_t *vars, u32 varLen)
@ -99,12 +102,126 @@ scriptFunction(funcWriteFile){
return newVar(IntType, 0, sd_save_to_file(vars[1].vectorType.data, vars[1].vectorType.count, vars[0].stringType));
}
// Args: vector(Byte)
scriptFunction(funcByteToStr){
char *str = calloc(vars[0].vectorType.count + 1, 1);
memcpy(str, vars[0].vectorType.data, vars[0].vectorType.count);
return newVar(StringType, 1, .stringType = str);
}
scriptFunction(funcReturn){
if (ctx->indentIndex > 0){
vecDefArray(indentInstructor_t*, instructors, ctx->indentInstructors);
for (int i = ctx->indentIndex - 1; i >= 0; i--){
indentInstructor_t ins = instructors[i];
if (ins.active && ins.jump && ins.function){
ctx->curPos = ins.jumpLoc - 1;
ins.active = 0;
ctx->indentIndex = i;
break;
}
}
}
return NullVar;
}
scriptFunction(funcExit){
return ErrVar(ERRESCSCRIPT);
}
/*
scriptFunction(funcContinue){
if (ctx->indentIndex > 0){
vecDefArray(indentInstructor_t*, instructors, ctx->indentInstructors);
for (int i = ctx->indentIndex - 1; i >= 0; i--){
indentInstructor_t ins = instructors[i];
if (ins.active && ins.jump && !ins.function){
ctx->curPos = ins.jumpLoc - 1;
ins.active = 0;
ctx->indentIndex = i;
break;
}
}
}
return NullVar;
}
*/
// Args: Int, Int
scriptFunction(funcSetPrintPos){
if (vars[0].integerType > 78 || vars[0].integerType < 0 || vars[1].integerType > 42 || vars[1].integerType < 0)
return ErrVar(ERRFATALFUNCFAIL);
gfx_con_setpos(vars[0].integerType * 16, vars[1].integerType * 16);
return NullVar;
}
scriptFunction(funcClearScreen){
gfx_clearscreen();
return NullVar;
}
int validRanges[] = {
1279,
719,
1279,
719
};
// Args: Int, Int, Int, Int, Int
scriptFunction(funcDrawBox){
for (int i = 0; i < ARR_LEN(validRanges); i++){
if (vars[i].integerType > validRanges[i] || vars[i].integerType < 0)
return ErrVar(ERRFATALFUNCFAIL);
}
gfx_box(vars[0].integerType, vars[1].integerType, vars[2].integerType, vars[3].integerType, vars[4].integerType);
return NullVar;
}
typedef struct {
char *name;
u32 color;
} ColorCombo_t;
ColorCombo_t combos[] = {
{"RED", COLOR_RED},
{"ORANGE", COLOR_ORANGE},
{"YELLOW", COLOR_YELLOW},
{"GREEN", COLOR_GREEN},
{"BLUE", COLOR_BLUE},
{"VIOLET", COLOR_VIOLET},
{"WHITE", COLOR_WHITE}
};
// Args: Str
scriptFunction(funcSetColor){
for (int i = 0; i < ARR_LEN(combos); i++){
if (!strcmp(combos[i].name, vars[0].stringType)){
SETCOLOR(combos[i].color, COLOR_DEFAULT);
break;
}
}
return NullVar;
}
scriptFunction(funcPause){
return newVar(IntType, 0, hidWait()->buttons);
}
// Args: Int
scriptFunction(funcWait){
u32 timer = get_tmr_ms();
while (timer + vars[0].integerType > get_tmr_ms()){
gfx_printf("<Wait %d seconds> \r", (vars[0].integerType - (get_tmr_ms() - timer)) / 1000);
}
gfx_putc('\n');
return NullVar;
}
u8 fiveInts[] = {IntType, IntType, IntType, IntType, IntType};
u8 singleIntArray[] = { IntArrayType };
u8 singleInt[] = { IntType };
@ -126,6 +243,15 @@ functionStruct_t scriptFunctions[] = {
{"fileWrite", funcWriteFile, 2, StrByteVec},
{"fileExists", funcFileExists, 1, singleStr},
{"bytesToStr", funcByteToStr, 1, singleByteArr},
{"return", funcReturn, 0, NULL},
{"exit", funcExit, 0, NULL},
//{"continue", funcContinue, 0, NULL},
{"printPos", funcSetPrintPos, 2, fiveInts},
{"clearscreen", funcClearScreen, 0, NULL},
{"drawBox", funcDrawBox, 5, fiveInts},
{"color", funcSetColor, 1, singleStr},
{"pause", funcPause, 0, NULL},
{"wait", funcWait, 1, singleInt},
};
Variable_t executeFunction(scriptCtx_t* ctx, char* func_name, lexarToken_t *start, u32 len) {

View file

@ -5,6 +5,7 @@
#include "../gfx/gfx.h"
#include "../utils/utils.h"
#include <mem/heap.h>
#include "../hid/hid.h"
#define scriptResultCreate(resCode, nearToken) (scriptResult_t) {resCode, nearToken, 1}
#define scriptResultCreateLen(resCode, nearToken, len) (scriptResult_t) {resCode, nearToken, len}
@ -101,6 +102,9 @@ void printToken(lexarToken_t* token) {
void printError(scriptResult_t res) {
if (res.resCode) {
if (res.resCode == ERRESCSCRIPT)
return;
gfx_printf("Error %d found!\nNear: ", res.resCode);
for (int i = 0; i < res.len; i++) {
printToken(&res.nearToken[i]);

View file

@ -61,6 +61,7 @@ enum Errors {
ERRINACTIVEINDENT,
ERRDIVBYZERO,
ERRFATALFUNCFAIL,
ERRESCSCRIPT,
};
enum Variables {