various fixes; new interpreter library

master
Clyne Sullivan 7 years ago
parent c27af361fe
commit 8275c9cac1

@ -10,7 +10,6 @@
/** /**
* Sets HCLK (system clock) to 80MHz, the maximum. * Sets HCLK (system clock) to 80MHz, the maximum.
* @param none
*/ */
extern void clock_init(void); extern void clock_init(void);

@ -1,18 +1,55 @@
/*
* @file display.h
* Display library for ILI9481 display.
*/
#ifndef DISPLAY_H_ #ifndef DISPLAY_H_
#define DISPLAY_H_ #define DISPLAY_H_
#include <stdint.h> #include <stdint.h>
/**
* The screen's width, in pixels.
*/
#define LCD_WIDTH 480 #define LCD_WIDTH 480
#define LCD_HEIGHT 320
#define COLOR_MAX 31 /**
* The screen's height, in pixels.
*/
#define LCD_HEIGHT 320
/**
* Returns the color integer for the given RGB values.
* Converts 8RGB to 5-6-5.
* @param r red value, 0-255
* @param g green value, 0-255
* @param b blue value, 0-255
* @return the 5-6-5 color value
*/
uint16_t dsp_color(uint8_t r, uint8_t g, uint8_t b); uint16_t dsp_color(uint8_t r, uint8_t g, uint8_t b);
/**
* Sets the direction of IO, for reading or writing to the screen.
* @param mode INPUT or OUPUT (defined in gpio.h)
*/
void dsp_dmode(int mode); void dsp_dmode(int mode);
/**
* Writes the command byte to the display.
* @param data the command to write
*/
void dsp_write_cmd(uint8_t data); void dsp_write_cmd(uint8_t data);
/**
* Writes the data byte to the display.
* @param data the data to write
*/
void dsp_write_data(uint8_t data); void dsp_write_data(uint8_t data);
/**
* Reads a byte of data from the display.
* @return the data byte
*/
uint8_t dsp_read_data(void); uint8_t dsp_read_data(void);
void dsp_set_addr(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2); void dsp_set_addr(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2);

@ -1,16 +1,65 @@
/**
* @file display_draw.h
* Provides functions for drawing to the display
*/
#ifndef DISPLAY_DRAW_H_ #ifndef DISPLAY_DRAW_H_
#define DISPLAY_DRAW_H_ #define DISPLAY_DRAW_H_
#include <stdint.h> #include <stdint.h>
/**
* Starts the task for a blinking text cursor.
*/
void dsp_cursoron(void); void dsp_cursoron(void);
/**
* Sets the chosen pixel to the given color.
* @param x x-coord of the pixel
* @param y y-coord of the pixel
* @param color the color to use (5-6-5)
*/
void dsp_pixel(int x, int y, uint16_t color); void dsp_pixel(int x, int y, uint16_t color);
/**
* Draws a line between the two coordinates.
* @param x start x-coord
* @param y start y-coord
* @param i end x-coord
* @param j end y-coord
* @param color the color to use (5-6-5)
*/
void dsp_line(int x, int y, int i, int j, uint16_t color); void dsp_line(int x, int y, int i, int j, uint16_t color);
/**
* Draws a rectangle with the given dimensions.
* @param x top left x-coord
* @param y top left y-coord
* @param w width of the rectangle
* @param h height of the rectangle
* @param color the color to use (5-6-5)
*/
void dsp_rect(int x, int y, int w, int h, uint16_t color); void dsp_rect(int x, int y, int w, int h, uint16_t color);
/**
* Sets the text cursor's position, in characters NOT pixels.
* @param x text column to move to
* @param y text row to move to
*/
void dsp_cpos(int x, int y); void dsp_cpos(int x, int y);
/**
* Sets the pixel offset of the text cursor.
* @param x x-pixel offset from (0, 0)
* @param y y-pixel offset from (0, 0)
*/
void dsp_coff(int x, int y); void dsp_coff(int x, int y);
/**
* Puts a string to the screen. Text position if kept track of internally, so
* multiple calls will print the strings in one line.
* @param s the string to print
*/
void dsp_puts(const char *s); void dsp_puts(const char *s);
#endif // DISPLAY_DRAW_H_ #endif // DISPLAY_DRAW_H_

@ -1 +1 @@
../../interpreter ../../interpreter3

@ -12,6 +12,6 @@
* Loads the library for the given interpreter. * Loads the library for the given interpreter.
* @param it the interpreter to use * @param it the interpreter to use
*/ */
void script_loadlib(interpreter *it); void script_loadlib(instance *it);
#endif // SCRIPT_H_ #endif // SCRIPT_H_

@ -1,69 +1,67 @@
# graph area # graph area
set plotx 50 plotx = 50
set ploty 50 ploty = 50
set plotw 380 plotw = 380
set ploth 220 ploth = 220
set cx (plotx + (plotw / 2)) cx = plotx + plotw / 2
set cy (ploty + (ploth / 2)) cy = ploty + ploth / 2
# graph range # graph range
set xmin (0 - 10) xmin = 0 - 10
set xmax 10 xmax = 10
set ymin (0 - 10) ymin = 0 - 10
set ymax 10 ymax = 10
set xinc (plotw / (xmax - xmin)) xinc = plotw / (xmax - xmin)
set yinc (ploth / (ymax - ymin)) yinc = ploth / (ymax - ymin)
color 3 3 3 > mlines mlines = color(3, 3, 3)
# print axis func(makegrid) {
rect(plotx, ploty, plotw, ploth, 0)
func makegrid x = plotx
rect plotx ploty plotw ploth 0 while (x <= plotx + plotw) {
line(x, ploty, x, ploty + ploth, mlines)
x = x + xinc
}
set x plotx y = ploty
do while (y <= ploty + ploth) {
line x ploty x (ploty + ploth) mlines line(plotx, y, plotx + plotw, y, mlines)
set x (x + xinc) y = y + yinc
while (x <= plotx + plotw) }
set y ploty line(cx, ploty, cx, ploty + ploth, 32767)
do line(plotx, cy, plotx + plotw, cy, 32767)
line plotx y (plotx + plotw) y mlines }
set y (y + yinc)
while (y <= ploty + ploth)
line cx ploty cx (ploty + ploth) 32767
line plotx cy (plotx + plotw) cy 32767
end
# #
# BIG LOOP - ask for equ, graph it # BIG LOOP - ask for equ, graph it
# #
makegrid makegrid()
set clearcmd "clear" clearcmd = "clear"
do while (1) {
rect 0 0 480 40 rect(0, 0, 480, 40, 0)
print("f(x) = ")
print "f(x) = " Fx = gets()
gets > Fx
if (Fx == clearcmd) if (Fx == clearcmd) {
makegrid makegrid()
else } else {
# do function # do function
set x xmin x = xmin
do while (x < xmax) {
solve Fx > y y = solve(Fx)
set y (0 - y) y = 0 - y
if ((y >= ymin) & (y <= ymax)) if ((y >= ymin) & (y <= ymax)) {
pixel (cx + x * xinc) (cy + y * yinc) 511 pixel(cx + x * xinc, cy + y * yinc, 511)
end }
set x (x + 1 / xinc) x = x + 1 / xinc
while (x < xmax) }
end }
ppos(0, 0)
}
ppos 0 0
while (1)

Binary file not shown.

@ -76,6 +76,8 @@ void free(void *buf)
return; return;
alloc_t *alloc = (alloc_t *)((uint8_t *)buf - sizeof(alloc_t)); alloc_t *alloc = (alloc_t *)((uint8_t *)buf - sizeof(alloc_t));
if (alloc->next != 0)
return;
heap_used -= alloc->size; heap_used -= alloc->size;
alloc->next = free_blocks; alloc->next = free_blocks;
free_blocks = alloc; free_blocks = alloc;

@ -82,9 +82,8 @@ void kmain(void)
void task_interpreter(void) void task_interpreter(void)
{ {
interpreter it; instance *it = inewinstance();
iinit(&it); script_loadlib(it);
script_loadlib(&it);
char *s = initrd_getfile("init"); char *s = initrd_getfile("init");
if (s == 0) { if (s == 0) {
@ -105,7 +104,7 @@ void task_interpreter(void)
} }
strncpy(linebuf, s + prev, lc + 1); strncpy(linebuf, s + prev, lc + 1);
linebuf[lc] = '\0'; linebuf[lc] = '\0';
ret = idoline(&it, linebuf); ret = idoline(it, linebuf);
if (ret < 0) if (ret < 0)
break; break;
prev = ++i; prev = ++i;

@ -7,23 +7,25 @@
#include <heap.h> #include <heap.h>
#include <random.h> #include <random.h>
#include <serial.h> #include <serial.h>
#include <stack.h> #include <stdlib.h>
#include <keypad.h> #include <keypad.h>
#include <string.h> #include <string.h>
int script_puts(interpreter *it); #define igetarg_integer(it, n) ((int)igetarg(it, n)->value.f)
int script_gets(interpreter *it);
int script_delay(interpreter *it); int script_puts(instance *it);
int script_rect(interpreter *it); int script_gets(instance *it);
int script_ppos(interpreter *it); int script_delay(instance *it);
int script_line(interpreter *it); int script_rect(instance *it);
int script_color(interpreter *it); int script_ppos(instance *it);
int script_rand(interpreter *it); int script_line(instance *it);
int script_getkey(interpreter *it); int script_color(instance *it);
int script_pixel(interpreter *it); int script_rand(instance *it);
int script_getkey(instance *it);
void script_loadlib(interpreter *it) int script_pixel(instance *it);
void script_loadlib(instance *it)
{ {
inew_cfunc(it, "print", script_puts); inew_cfunc(it, "print", script_puts);
inew_cfunc(it, "gets", script_gets); inew_cfunc(it, "gets", script_gets);
@ -37,14 +39,20 @@ void script_loadlib(interpreter *it)
inew_cfunc(it, "pixel", script_pixel); inew_cfunc(it, "pixel", script_pixel);
} }
int script_puts(interpreter *it) int script_puts(instance *it)
{ {
const char *s = igetarg_string(it, 0); variable *v = igetarg(it, 0);
dsp_puts(s); if (v->type == NUMBER) {
char buf[33];
snprintf(buf, 33, "%f", v->value.f);
dsp_puts(buf);
} else if (v->type == STRING) {
dsp_puts((const char *)v->value.p);
}
return 0; return 0;
} }
int script_gets(interpreter *it) int script_gets(instance *it)
{ {
char *s = malloc(64); char *s = malloc(64);
char c[2] = {0, 0}; char c[2] = {0, 0};
@ -66,20 +74,19 @@ int script_gets(interpreter *it)
s[index] = '\0'; s[index] = '\0';
variable *r = make_vars(0, s); variable *r = make_vars(0, s);
iret(it, r); ipush(it, (uint32_t)r);
free(s); free(s);
free(r);
return 0; return 0;
} }
int script_delay(interpreter *it) int script_delay(instance *it)
{ {
int ms = igetarg_integer(it, 0); int ms = (int)igetarg(it, 0)->value.f;
delay(ms); delay(ms);
return 0; return 0;
} }
int script_rect(interpreter *it) int script_rect(instance *it)
{ {
dsp_rect(igetarg_integer(it, 0), igetarg_integer(it, 1), dsp_rect(igetarg_integer(it, 0), igetarg_integer(it, 1),
igetarg_integer(it, 2), igetarg_integer(it, 3), igetarg_integer(it, 2), igetarg_integer(it, 3),
@ -87,7 +94,7 @@ int script_rect(interpreter *it)
return 0; return 0;
} }
int script_line(interpreter *it) int script_line(instance *it)
{ {
int x = igetarg_integer(it, 0); int x = igetarg_integer(it, 0);
int y = igetarg_integer(it, 1); int y = igetarg_integer(it, 1);
@ -98,42 +105,42 @@ int script_line(interpreter *it)
return 0; return 0;
} }
int script_ppos(interpreter *it) int script_ppos(instance *it)
{ {
dsp_cpos(0, 0); dsp_cpos(0, 0);
dsp_coff(igetarg_integer(it, 0), igetarg_integer(it, 1)); dsp_coff(igetarg_integer(it, 0), igetarg_integer(it, 1));
return 0; return 0;
} }
int script_color(interpreter *it) int script_color(instance *it)
{ {
uint16_t c = dsp_color(igetarg_integer(it, 0), igetarg_integer(it, 1), uint16_t c = dsp_color(igetarg_integer(it, 0), igetarg_integer(it, 1),
igetarg_integer(it, 2)); igetarg_integer(it, 2));
variable *v = make_varn(0, (float)c); variable *v = make_varf(0, (float)c);
iret(it, v); ipush(it, (uint32_t)v);
free(v); free(v);
return 0; return 0;
} }
int script_rand(interpreter *it) int script_rand(instance *it)
{ {
unsigned int mod = igetarg_integer(it, 0); unsigned int mod = igetarg_integer(it, 0);
unsigned int val = random_get(); unsigned int val = random_get();
variable *v = make_varn(0, (float)(mod % val)); variable *v = make_varf(0, (float)(mod % val));
iret(it, v); ipush(it, (uint32_t)v);
free(v); free(v);
return 0; return 0;
} }
int script_getkey(interpreter *it) int script_getkey(instance *it)
{ {
variable *v = make_varn(0, (float)keypad_get()); variable *v = make_varf(0, (float)keypad_get());
iret(it, v); ipush(it, (uint32_t)v);
free(v); free(v);
return 0; return 0;
} }
int script_pixel(interpreter *it) int script_pixel(instance *it)
{ {
dsp_pixel(igetarg_integer(it, 0), igetarg_integer(it, 1), dsp_pixel(igetarg_integer(it, 0), igetarg_integer(it, 1),
igetarg_integer(it, 2)); igetarg_integer(it, 2));

Loading…
Cancel
Save