From 7eb572702ca012a89370cc94a297eeb346a8dee9 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Thu, 5 Apr 2018 11:25:18 -0400 Subject: [PATCH] font on flash, ftostr --- include/stdlib.h | 2 + initrd/graph | 4 +- src/display_draw.c | 8 +++- src/flash.c | 32 ++++++++++++---- src/{inconsolata24.c => inconsolata24.c.bak} | 0 src/main.c | 17 ++++----- src/script.c | 5 ++- src/stdlib.c | 40 +++++++++++++++++++- 8 files changed, 86 insertions(+), 22 deletions(-) rename src/{inconsolata24.c => inconsolata24.c.bak} (100%) diff --git a/include/stdlib.h b/include/stdlib.h index ada39cd..c26934d 100644 --- a/include/stdlib.h +++ b/include/stdlib.h @@ -41,6 +41,8 @@ char *snprintf(char *buf, unsigned int max, const char *format, ...); */ float strtof(const char *s, char **endptr); +char *ftostr(char *buf, float f); + /** * Attempts to convert the given string to an integer. * @param s the string to convert diff --git a/initrd/graph b/initrd/graph index a218474..8f9916a 100644 --- a/initrd/graph +++ b/initrd/graph @@ -40,7 +40,7 @@ func(makegrid) { # BIG LOOP - ask for equ, graph it # -makegrid +makegrid() clearcmd = "clear" while (1) { rect(0, 0, 480, 40, 0) @@ -48,7 +48,7 @@ while (1) { Fx = gets() if (Fx == clearcmd) { - makegrid + makegrid() } else { # do function x = xmin diff --git a/src/display_draw.c b/src/display_draw.c index c57f63b..7241f83 100644 --- a/src/display_draw.c +++ b/src/display_draw.c @@ -20,6 +20,8 @@ #include #include +#include +#include #include #include @@ -32,7 +34,8 @@ static unsigned int cury = 0; static unsigned int curxo = 0; static unsigned int curyo = 0; -extern const unsigned char inconsolata24[192 * 156 * 2 + 1]; +//extern const unsigned char inconsolata24[192 * 156 * 2 + 1]; +static unsigned char *inconsolata24; void task_cursor(void) { @@ -48,6 +51,9 @@ void task_cursor(void) void dsp_cursoron(void) { + inconsolata24 = malloc(192 * 156 * 2); + flash_read((char *)inconsolata24, 0, 192 * 156 * 2); + task_start(task_cursor, 512); } diff --git a/src/flash.c b/src/flash.c index 83816c7..396daaf 100644 --- a/src/flash.c +++ b/src/flash.c @@ -38,6 +38,10 @@ void flash_init(void) gpio_mode(SI, OUTPUT); gpio_mode(CS, OUTPUT); gpio_mode(SO, OUTPUT); + gpio_speed(SCK, VERYHIGH); + gpio_speed(SI, VERYHIGH); + gpio_speed(SO, VERYHIGH); + gpio_speed(CS, VERYHIGH); gpio_dout(SO, 0); gpio_mode(SO, INPUT); gpio_dout(CS, 1); @@ -50,14 +54,28 @@ void flash_read(char *buf, uint32_t addr, unsigned int count) if (buf == 0) return; - char *spibuf = malloc(count + 4); + char *spibuf = malloc(1028); + unsigned int c = count / 1024; + unsigned int i = 0; + uint32_t paddr = addr; + for (i = 0; i < c; i++, paddr += 1024) { + spibuf[0] = READ; + spibuf[1] = (paddr >> 16) & 0xFF; + spibuf[2] = (paddr >> 8) & 0xFF; + spibuf[3] = paddr & 0xFF; + //memcpy(spibuf + 4, buf, count); + flash_spi_xchg(spibuf, 1028); + memcpy(buf + i * 1024, spibuf + 4, 1024); + } + + c = count - i * 1024; spibuf[0] = READ; - spibuf[1] = (addr >> 16) & 0xFF; - spibuf[2] = (addr >> 8) & 0xFF; - spibuf[3] = addr & 0xFF; - memcpy(spibuf + 4, buf, count); - flash_spi_xchg(spibuf, count + 4); - memcpy(buf, spibuf + 4, count); + spibuf[1] = (paddr >> 16) & 0xFF; + spibuf[2] = (paddr >> 8) & 0xFF; + spibuf[3] = paddr & 0xFF; + flash_spi_xchg(spibuf, 4 + c); + memcpy(buf + i * 1024, spibuf + 4, c); + free(spibuf); } diff --git a/src/inconsolata24.c b/src/inconsolata24.c.bak similarity index 100% rename from src/inconsolata24.c rename to src/inconsolata24.c.bak diff --git a/src/main.c b/src/main.c index 8a28ce8..d3580c9 100644 --- a/src/main.c +++ b/src/main.c @@ -76,15 +76,14 @@ void kmain(void) dsp_rect(0, 0, LCD_WIDTH, LCD_HEIGHT, dsp_color(0, 0, 0)); dsp_cursoron(); - const char *test = "Hey there!"; - flash_write(test, 0x1FD, 10); - - char *buf = calloc(11, 1); - flash_read(buf, 0x1FD, 10); - dsp_puts(buf); - - //keypad_start(); - //task_start(task_interpreter, 4096); + /*extern const unsigned char inconsolata24[192 * 156 * 2 + 1]; + for (uint32_t i = 0; i <= 192 * 156 * 2; i += 624) { + flash_write((char *)(inconsolata24 + i), i, 624); + dsp_puts("."); + }*/ + + keypad_start(); + task_start(task_interpreter, 4096); while (1) { gpio_dout(GPIOA, 5, 1); diff --git a/src/script.c b/src/script.c index 95d62c0..3f0209f 100644 --- a/src/script.c +++ b/src/script.c @@ -53,7 +53,7 @@ int math_sin(instance *it); void script_loadlib(instance *it) { - inew_number(it, "pi", 3.1415926f); + inew_number(it, "pi", 3.1415926535f); inew_cfunc(it, "print", script_puts); inew_cfunc(it, "putchar", script_putchar); @@ -122,7 +122,8 @@ int script_puts(instance *it) variable *v = igetarg(it, 0); if (v->type == NUMBER) { char buf[33]; - snprintf(buf, 33, "%d", (int)v->value.f); // TODO + //snprintf(buf, 33, "%d", (int)v->value.f); // TODO + ftostr(buf, v->value.f); dsp_puts(buf); } else if (v->type == STRING) { dsp_puts((const char *)v->value.p); diff --git a/src/stdlib.c b/src/stdlib.c index b7b11dc..4714e93 100644 --- a/src/stdlib.c +++ b/src/stdlib.c @@ -23,7 +23,6 @@ #include #include -#include #include #include @@ -134,3 +133,42 @@ int atoi(const char *s) return (neg == 0) ? n : -n; } + +char *ftostr(char *buf, float f) +{ + if (buf == 0) + return 0; + + unsigned int i = 0; // offset + + // strip decimals, convert in reverse + for (int d = f; d != 0; d /= 10) + buf[i++] = d % 10 + '0'; + + // reverse + for (unsigned int j = 0; j < i / 2; j++) { + char c = buf[i - j - 1]; + buf[i - j - 1] = buf[j]; + buf[j] = c; + } + + if ((float)((int)f) == f) + goto end; + + buf[i++] = '.'; + + // decimals + float d = f; + // precision of 5 is safe, more gets questionable + for (unsigned int p = 0; p < 5; p++) { + d *= 10; + buf[i++] = (int)d % 10 + '0'; + } + + // trim 0's + for (unsigned int j = i - 1; buf[j] == '0'; j--) + buf[j] = '\0'; +end: + buf[i] = '\0'; + return buf; +}