font on flash, ftostr

master
Clyne Sullivan 7 years ago
parent cbdea74b39
commit 7eb572702c

@ -41,6 +41,8 @@ char *snprintf(char *buf, unsigned int max, const char *format, ...);
*/ */
float strtof(const char *s, char **endptr); float strtof(const char *s, char **endptr);
char *ftostr(char *buf, float f);
/** /**
* Attempts to convert the given string to an integer. * Attempts to convert the given string to an integer.
* @param s the string to convert * @param s the string to convert

@ -40,7 +40,7 @@ func(makegrid) {
# BIG LOOP - ask for equ, graph it # BIG LOOP - ask for equ, graph it
# #
makegrid makegrid()
clearcmd = "clear" clearcmd = "clear"
while (1) { while (1) {
rect(0, 0, 480, 40, 0) rect(0, 0, 480, 40, 0)
@ -48,7 +48,7 @@ while (1) {
Fx = gets() Fx = gets()
if (Fx == clearcmd) { if (Fx == clearcmd) {
makegrid makegrid()
} else { } else {
# do function # do function
x = xmin x = xmin

@ -20,6 +20,8 @@
#include <display_draw.h> #include <display_draw.h>
#include <display.h> #include <display.h>
#include <flash.h>
#include <heap.h>
#include <task.h> #include <task.h>
#include <clock.h> #include <clock.h>
@ -32,7 +34,8 @@ static unsigned int cury = 0;
static unsigned int curxo = 0; static unsigned int curxo = 0;
static unsigned int curyo = 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) void task_cursor(void)
{ {
@ -48,6 +51,9 @@ void task_cursor(void)
void dsp_cursoron(void) void dsp_cursoron(void)
{ {
inconsolata24 = malloc(192 * 156 * 2);
flash_read((char *)inconsolata24, 0, 192 * 156 * 2);
task_start(task_cursor, 512); task_start(task_cursor, 512);
} }

@ -38,6 +38,10 @@ void flash_init(void)
gpio_mode(SI, OUTPUT); gpio_mode(SI, OUTPUT);
gpio_mode(CS, OUTPUT); gpio_mode(CS, OUTPUT);
gpio_mode(SO, 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_dout(SO, 0);
gpio_mode(SO, INPUT); gpio_mode(SO, INPUT);
gpio_dout(CS, 1); gpio_dout(CS, 1);
@ -50,14 +54,28 @@ void flash_read(char *buf, uint32_t addr, unsigned int count)
if (buf == 0) if (buf == 0)
return; 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[0] = READ;
spibuf[1] = (addr >> 16) & 0xFF; spibuf[1] = (paddr >> 16) & 0xFF;
spibuf[2] = (addr >> 8) & 0xFF; spibuf[2] = (paddr >> 8) & 0xFF;
spibuf[3] = addr & 0xFF; spibuf[3] = paddr & 0xFF;
memcpy(spibuf + 4, buf, count); flash_spi_xchg(spibuf, 4 + c);
flash_spi_xchg(spibuf, count + 4); memcpy(buf + i * 1024, spibuf + 4, c);
memcpy(buf, spibuf + 4, count);
free(spibuf); free(spibuf);
} }

@ -76,15 +76,14 @@ void kmain(void)
dsp_rect(0, 0, LCD_WIDTH, LCD_HEIGHT, dsp_color(0, 0, 0)); dsp_rect(0, 0, LCD_WIDTH, LCD_HEIGHT, dsp_color(0, 0, 0));
dsp_cursoron(); dsp_cursoron();
const char *test = "Hey there!"; /*extern const unsigned char inconsolata24[192 * 156 * 2 + 1];
flash_write(test, 0x1FD, 10); for (uint32_t i = 0; i <= 192 * 156 * 2; i += 624) {
flash_write((char *)(inconsolata24 + i), i, 624);
char *buf = calloc(11, 1); dsp_puts(".");
flash_read(buf, 0x1FD, 10); }*/
dsp_puts(buf);
keypad_start();
//keypad_start(); task_start(task_interpreter, 4096);
//task_start(task_interpreter, 4096);
while (1) { while (1) {
gpio_dout(GPIOA, 5, 1); gpio_dout(GPIOA, 5, 1);

@ -53,7 +53,7 @@ int math_sin(instance *it);
void script_loadlib(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, "print", script_puts);
inew_cfunc(it, "putchar", script_putchar); inew_cfunc(it, "putchar", script_putchar);
@ -122,7 +122,8 @@ int script_puts(instance *it)
variable *v = igetarg(it, 0); variable *v = igetarg(it, 0);
if (v->type == NUMBER) { if (v->type == NUMBER) {
char buf[33]; 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); dsp_puts(buf);
} else if (v->type == STRING) { } else if (v->type == STRING) {
dsp_puts((const char *)v->value.p); dsp_puts((const char *)v->value.p);

@ -23,7 +23,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <ctype.h> #include <ctype.h>
#include <heap.h>
#include <stdarg.h> #include <stdarg.h>
#include <string.h> #include <string.h>
@ -134,3 +133,42 @@ int atoi(const char *s)
return (neg == 0) ? n : -n; 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;
}

Loading…
Cancel
Save