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);
char *ftostr(char *buf, float f);
/**
* Attempts to convert the given string to an integer.
* @param s the string to convert

@ -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

@ -20,6 +20,8 @@
#include <display_draw.h>
#include <display.h>
#include <flash.h>
#include <heap.h>
#include <task.h>
#include <clock.h>
@ -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);
}

@ -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] = (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;
//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] = (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);
}

@ -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);

@ -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);

@ -23,7 +23,6 @@
#include <stdlib.h>
#include <ctype.h>
#include <heap.h>
#include <stdarg.h>
#include <string.h>
@ -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;
}

Loading…
Cancel
Save