adding support for loading files throught script

master
Clyne Sullivan 7 years ago
parent efd1e11475
commit 93396d69eb

@ -65,4 +65,6 @@ char *initrd_getfile(const char *name);
*/ */
uint32_t initrd_getfilesize(const char *name); uint32_t initrd_getfilesize(const char *name);
char *initrd_getnfile(unsigned int index);
#endif // INITRD_H_ #endif // INITRD_H_

@ -0,0 +1,8 @@
while (1) {
print("> ")
input = gets()
answer = solve(input)
print("\n")
print(answer)
print("\n")
}

@ -1,8 +1,3 @@
while (1) { choice = filemenu
print("> ") print("\nChoice: ")
input = gets() print(choice)
answer = solve(input)
print("\n")
print(answer)
print("\n")
}

@ -90,6 +90,23 @@ initrd_file *initrd_getfileptr(const char *name)
return 0; return 0;
} }
char *initrd_getnfile(unsigned int index)
{
initrd_file *file = (initrd_file *)((uint8_t *)initrd_start + sizeof(initrd_header));
uint32_t offset = sizeof(initrd_header);
for (unsigned int i = 0; i < index; i++) {
uint32_t size = initrd_getsize(file) + sizeof(initrd_file);
offset += size;
file = (initrd_file *)((uint8_t *)file + size);
if (file->name[0] == '\n')
file = (initrd_file *)((uint32_t)file + 1);
}
if ((uint32_t)file >= (uint32_t)initrd_start + initrd_size)
return 0;
return file->name;
}
char *initrd_getfile(const char *name) char *initrd_getfile(const char *name)
{ {
initrd_file *file = initrd_getfileptr(name); initrd_file *file = initrd_getfileptr(name);

@ -45,7 +45,7 @@ int main(void)
{ {
asm("cpsid i"); asm("cpsid i");
// disable cached writes for precise debug info // disable cached writes for precise debug info
*((uint32_t *)0xE000E008) |= 2; //*((uint32_t *)0xE000E008) |= 2;
// prepare flash latency for 80MHz operation // prepare flash latency for 80MHz operation
FLASH->ACR &= ~(FLASH_ACR_LATENCY); FLASH->ACR &= ~(FLASH_ACR_LATENCY);
@ -77,20 +77,7 @@ void kmain(void)
keypad_start(); keypad_start();
task_start(task_interpreter, 4096); task_start(task_interpreter, 4096);
/*char buf[2];
flash_init();
buf[0] = 'A';
flash_write(buf, 0, 1);
buf[0] = 0;
flash_read(buf, 0x00000000, 1);
buf[0] += ' ';
buf[1] = '\0';
dsp_puts(buf);*/
while (1) { while (1) {
//gpio_dout(GPIOA, 5,
// (keypad_isdown(K0)));
//delay(10);
gpio_dout(GPIOA, 5, 1); gpio_dout(GPIOA, 5, 1);
delay(250); delay(250);
gpio_dout(GPIOA, 5, 0); gpio_dout(GPIOA, 5, 0);
@ -140,26 +127,3 @@ end:
delay(10); delay(10);
} }
// for interactive use
/*int ret = 0;
char *linebuf = malloc(100), c[2] = {0, 0};
while (1) {
uint16_t index = 0;
if (it.indent > 0)
dsp_puts(">");
dsp_puts("> ");
do {
c[0] = serial_get();
if (c[0] >= ' ' || c[0] == '\r') {
linebuf[index] = c[0];
if (c[0] >= ' ')
dsp_puts(c);
}
} while (linebuf[index] != '\r' && index++ < 100);
linebuf[index] = '\0';
dsp_puts("\n");
ret = idoline(&it, linebuf);
if (ret < 0)
break;
}*/

@ -25,6 +25,7 @@
#include <display.h> #include <display.h>
#include <display_draw.h> #include <display_draw.h>
#include <heap.h> #include <heap.h>
#include <initrd.h>
#include <random.h> #include <random.h>
#include <serial.h> #include <serial.h>
#include <stdlib.h> #include <stdlib.h>
@ -46,6 +47,7 @@ int script_rand(instance *it);
int script_getkey(instance *it); int script_getkey(instance *it);
int script_pixel(instance *it); int script_pixel(instance *it);
int script_menu(instance *it); int script_menu(instance *it);
int script_filemenu(instance *it);
void script_loadlib(instance *it) void script_loadlib(instance *it)
{ {
@ -64,6 +66,7 @@ void script_loadlib(instance *it)
inew_cfunc(it, "delay", script_delay); inew_cfunc(it, "delay", script_delay);
inew_cfunc(it, "menu", script_menu); inew_cfunc(it, "menu", script_menu);
inew_cfunc(it, "filemenu", script_filemenu);
} }
int script_menu(instance *it) int script_menu(instance *it)
@ -83,6 +86,23 @@ int script_menu(instance *it)
return 0; return 0;
} }
int script_filemenu(instance *it)
{
char listbuf[4];
char *buf = calloc(17, 1);
char *fname;
strncpy(listbuf, " : \0", 4);
dsp_puts("Choose a file: \n");
for (unsigned int i = 0; (fname = initrd_getnfile(i)) != 0; i++) {
listbuf[0] = i + '0';
dsp_puts(listbuf);
dsp_puts(strncpy(buf, fname, 16));
dsp_puts("\n");
}
free(buf);
return script_gets(it);
}
int script_puts(instance *it) int script_puts(instance *it)
{ {
variable *v = igetarg(it, 0); variable *v = igetarg(it, 0);

Loading…
Cancel
Save