]> code.bitgloo.com Git - clyne/calculator.git/commitdiff
functional graphing script
authorClyne Sullivan <tullivan99@gmail.com>
Thu, 8 Mar 2018 03:43:54 +0000 (22:43 -0500)
committerClyne Sullivan <tullivan99@gmail.com>
Thu, 8 Mar 2018 03:43:54 +0000 (22:43 -0500)
initrd/init
libinterp.a
src/flash.c
src/script.c

index 56e5212ae1897dcb1e566493e10a0f53b628f6e3..a2508224c1b9451a64983e480b914a03358e842a 100644 (file)
@@ -1,12 +1,10 @@
-func Fx
-       ret (arg0 * arg0)
-end
-
 # graph area
-set plotx 0
-set ploty 0
-set plotw 479
-set ploth 319
+set plotx 50
+set ploty 50
+set plotw 380
+set ploth 220
+set cx (plotx + (plotw / 2))
+set cy (ploty + (ploth / 2))
 
 # graph range
 set xmin (0 - 10)
@@ -17,21 +15,55 @@ set ymax 10
 set xinc (plotw / (xmax - xmin))
 set yinc (ploth / (ymax - ymin))
 
+color 3 3 3 > mlines
+
 # print axis
-line 240 0 240 319 32767
-line 0 160 479 160 32767
 
-# do function
-set x xmin
-set cx (plotx + (plotw / 2))
-set cy (ploty + (ploth / 2))
+func makegrid
+       rect plotx ploty plotw ploth 0
+
+       set x plotx
+       do
+               line x ploty x (ploty + ploth) mlines
+               set x (x + xinc)
+       while (x <= plotx + plotw)
+
+       set y ploty
+       do
+               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
+#
+
+makegrid
+set clearcmd "clear"
 do
-       Fx x > y
-       set y (0 - y)
-       if ((y > ymin) & (y < ymax))
-               pixel (cx + (x * xinc)) (cy + (y * yinc)) 511
+       rect 0 0 480 40
+
+       print "f(x) = "
+       gets > Fx
+
+       if (Fx == clearcmd)
+               makegrid
+       else
+               # do function
+               set x xmin
+               do
+                       solve Fx > y
+                       set y (0 - y)
+                       if ((y > ymin) & (y < ymax))
+                               pixel (cx + x * xinc) (cy + y * yinc) 511
+                       end
+                       set x (x + 1 / xinc)
+               while (x < xmax)
        end
-       set x (x + (1 / xinc))
-while (x < xmax)
 
-print "Done."
+       ppos 0 0
+while (1)
index a11013cf3c14896093240f36a613d8cb9782b74c..2d0400d5b353ae437de29b69131a42fadc24320f 100644 (file)
Binary files a/libinterp.a and b/libinterp.a differ
index 3027ec0da0c7be99819919701525fd8c08470b8f..6b3ee0043a638f637900be85ae523513f9bb761b 100644 (file)
@@ -35,7 +35,7 @@ void flash_init(void)
        SPI2->CR2 |= SPI_CR2_FRXTH;
        SPI2->CR1 |= SPI_CR1_SPE;
 
-       char buf[3];
+       char buf[4];
        buf[0] = READ;
        buf[1] = 0;
        buf[2] = 0;
index c7b361427f85922bd60b73e147ec6346e370978d..495e409f4af60b6ec9b7fb4d52f1e8232406a725 100644 (file)
@@ -10,6 +10,8 @@
 #include <stack.h>
 #include <keypad.h>
 
+#include <string.h>
+
 int script_puts(interpreter *it);
 int script_gets(interpreter *it);
 int script_delay(interpreter *it);
@@ -20,6 +22,7 @@ int script_color(interpreter *it);
 int script_rand(interpreter *it);
 int script_getkey(interpreter *it);
 int script_pixel(interpreter *it);
+int script_solve(interpreter *it);
 
 void script_loadlib(interpreter *it)
 {
@@ -33,14 +36,13 @@ void script_loadlib(interpreter *it)
        inew_cfunc(it, "rand", script_rand);
        inew_cfunc(it, "getkey", script_getkey);
        inew_cfunc(it, "pixel", script_pixel);
+       inew_cfunc(it, "solve", script_solve);
 }
 
 int script_puts(interpreter *it)
 {
        const char *s = igetarg_string(it, 0);
        dsp_puts(s);
-       //dsp_puts("\n");
-       //asm("mov r0, %0; svc 2" :: "r" (s));
        return 0;
 }
 
@@ -58,9 +60,10 @@ int script_gets(interpreter *it)
        } while (s[index] != '\r' && index++ < 23);
        s[index] = '\0';
 
-       variable *v = igetarg(it, 0);
-       v->valtype = STRING;
-       v->value.p = (uint32_t)s;
+       variable *r = make_vars(0, s);
+       iret(it, r);
+       free(s);
+       free(r);
        return 0;
 }
 
@@ -131,3 +134,19 @@ int script_pixel(interpreter *it)
                igetarg_integer(it, 2));
        return 0;
 }
+
+int script_solve(interpreter *it)
+{
+       const char *expr = igetarg_string(it, 0);
+       int len = strlen(expr);
+       char *buf = (char *)malloc(len + 2);
+       strcpy(buf, expr);
+       buf[len] = ')';
+       buf[len + 1] = '\0';
+       variable *r = idoexpr(it, buf);
+       if (r == 0)
+               r = make_varn(0, 0.0f);
+       iret(it, r);
+       free(r);
+       return 0;
+}