You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+69-14Lines changed: 69 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,16 +1,23 @@
1
-
# Port of Lox language to Arduino GIGA R1 Wifi
1
+
# Port of Lox language to Arduino GIGA R1 WiFi (and other boards)
2
2
3
3
## Description
4
4
5
-
An experimental project to determine whether a full-featured scripting language can be used on larger-memory Arduino boards to control hardware. At the moment only the Giga board with Arduino Display Shield is supported, but future support for Portenta H7 (with USB-C display) may be possible.
5
+
An experimental project to determine whether a full-featured scripting language can be used on larger-memory Arduino boards to control hardware. At the moment only the Giga board with Arduino Display Shield is tested, but support for Portenta H7 (with USB-C video display) may be easy to add.
6
6
7
7
The [Lox language](https://www.craftinginterpreters.com/appendix-i.html) was chosen because of the availability of a ready-made implementation in C ([clox](https://github.com/munificent/craftinginterpreters/tree/master/c)) which is a compact and very quick JIT-compiled interpreter. It is also easy to extend with additional Lox functions, which are mapped to native C/C++ ones defined in the sketch. The construction of the interpreter is described in detail in the book "Crafting Interpreters", however reading the book is not necessarily a prerequisite for using the language or even extending it with new functions.
8
8
9
9
## Getting Started
10
10
11
-
The number of supported graphics functions (and other functions from [this page](https://www.arduino.cc/reference/en/)) is already quite large, leading to a near 400-line sketch required to support the library functionality. Take a look at the example "clox_gfx_demo" and copy it into your sketches folder; the functions prefixed with `gfx_` such as `gfx_millis` are called from within the Lox interpreter without this prefix, for example as `print millis();`.
11
+
The functionality of the sketch has been designed to be switchable, see the file `clox_gfx_config.h` in the "examples/clox_gfx_demo" directory.
12
12
13
-
Flashing and booting the Giga results in a REPL in the Serial Monitor, enter line(s) of Lox code at `> ` (start) and `. ` (continuation) prompts, and press Enter on a blank line to execute. Error messages are reported in the REPL, and the blue LED turns on for the duration of executing the code fragment just entered. To pulse the blue LED for ten seconds use the following Lox code in the interpreter:
13
+
* To enable `CLOX_GRAPHICS` a suitable board with support for Arduino_H7_Video is required: Portenta H7 with USBCVideo (untested) or GIGA R1 WiFi with GigaDisplayShield.
14
+
* To enable `CLOX_USB_HOST` a GIGA R1 WiFi is required, this allows reading sketches from a memory stick plugged into the host USB port on the board.
15
+
* To enable `CLOX_WEB_CONSOLE` support for the `WebSockets2_Generic` library is required, for GIGA R1 WiFi board this means a [patched version 1.14+](https://github.com/cpp-tutor/WebSockets2_Generic)
16
+
* To enable `CLOX_USE_SDRAM` a suitable board with support for Portenta_SDRAM is required.
17
+
18
+
The number of supported graphics functions (and other functions from [this page](https://www.arduino.cc/reference/en/)) is already quite large, leading to a 600+ line sketch required to support the library functionality. Take a look at the example "clox_gfx_demo" and copy it into your sketches folder; the functions prefixed with `gfx_` such as `gfx_millis` are called from within the Lox interpreter without this prefix, for example as `print millis();`.
19
+
20
+
Flashing and booting the Giga results in a REPL in the Serial Monitor, and also in the Web Console accessed from the address printed when booting, enter line(s) of Lox code at `> ` (start) and `. ` (continuation) prompts, and press Enter on a blank line to execute. Error messages are reported in the REPL, and the blue LED turns on for the duration of executing the code fragment just entered. To pulse the blue LED for ten seconds use the following Lox code in the interpreter:
14
21
15
22
```javascript
16
23
var led =88;
@@ -26,15 +33,63 @@ for (var i = 1; i <= 10; i = i + 1) {
26
33
Graphics on the Giga Display are also supported via the ArduinoGraphics library (note: **not** Arduino_GigaDisplay_GFX). To draw graphics, wrap the commands inside `beginDraw();` and `endDraw();`, for example:
27
34
28
35
```javascript
29
-
beginDraw();
30
-
fill(127, 127, 255);
31
-
ellipse(400, 240, 200, 100);
32
-
endDraw();
36
+
fun logo(center_x, center_y) {
37
+
beginDraw();
38
+
background(255, 255, 255);
39
+
clear();
40
+
fill(0, 129, 132);
41
+
circle(center_x, center_y, 300);
42
+
stroke(255, 255, 255);
43
+
noFill();
44
+
for (var i =0; i <30; i = i +1) {
45
+
circle(center_x-55+5, center_y, 110-i);
46
+
circle(center_x+55-5, center_y, 110-i);
47
+
}
48
+
fill(255, 255, 255);
49
+
rect(center_x-55-16+5, center_y-5, 32, 10);
50
+
fill(255, 255, 255);
51
+
rect(center_x+55-16-5, center_y-5, 32, 10);
52
+
fill(255, 255, 255);
53
+
rect(center_x+55-5-5, center_y-16, 10, 32);
54
+
endDraw();
55
+
}
56
+
57
+
fun animate(seconds) {
58
+
var x_speed =4;
59
+
var y_speed =4;
60
+
var time_limit =millis() + seconds *1000;
61
+
var x =width() /2;
62
+
var y =height() /2;
63
+
while (millis() < time_limit) {
64
+
logo(x, y);
65
+
delay(10);
66
+
x = x + x_speed;
67
+
y = y + y_speed;
68
+
if ((x +150) >width()) {
69
+
x =width() -150;
70
+
x_speed =-x_speed;
71
+
}
72
+
if ((y +150) >height()) {
73
+
y =height() -150;
74
+
y_speed =-y_speed;
75
+
}
76
+
if (x <150) {
77
+
x =150;
78
+
x_speed =-x_speed;
79
+
}
80
+
if (y <150) {
81
+
y =150;
82
+
y_speed =-y_speed;
83
+
}
84
+
}
85
+
}
86
+
87
+
animate(20);
33
88
```
34
89
35
90
Note: The Arduino_H7_Video library uses some shared SDRAM for the framebuffer, and `SDRAM.begin();` should **not** be called after initializing the display.
36
91
37
-
Scripts stored on USB flash devices plugged into the USB port on the Giga can be loaded with `load "script.lox"` at the prompt.
92
+
Scripts stored on USB flash devices plugged into the USB port on the Giga can be loaded with `load "script.lox"` at the prompt (some issues with this, currently).
38
93
39
94
## Adding Functions
40
95
@@ -54,10 +109,10 @@ Doing these steps correctly and in order ensures that the project should remain
54
109
55
110
There are a number of ideas for the future direction of this library:
56
111
57
-
* Stability: likely to be many bugs in non-core library code, and faulty Lox input can crash the board
112
+
* Stability: likely to be many bugs in non-core library code, and faulty Lox input can crash the board (fixed)
58
113
* Complete support for more "Arduino.h" functions
59
114
* Support for Arduino_GigaDisplayTouch
60
-
* Support for running scripts via a web interface (console-in-a-web-page)
115
+
* Support for running scripts via a web interface (console-in-a-web-page) (#define CLOX_WEB_CONSOLE 1)
61
116
* Use of the M4 co-processor as a graphics accelerator
62
117
* Changing the Lox interpreter language (not the JIT backend) to something less like JavaScript (GFX-Basic?)
63
118
* Adding to the ArduinoGraphics library (filled triangles, more fonts etc.)
@@ -70,11 +125,11 @@ In the style of the book "Crafting Interpreters" by Bob Nystrom, which describes
2. Make a folder in your "libraries" directory and paste all of the files apart from `main.c` into it.
128
+
2. Make a folder in your "libraries" directory and copy all of these checked-out files into it, apart from `main.c`.
74
129
75
-
3. Change all occurences of `#include <stdio.h>` to `#include "clox_stdio.h"`
130
+
3. Change all occurences of `#include <stdio.h>` to `#include "clox_stdio.h"` in files: `compiler.c`, `debug.c`, `memory.c`, `object.c`, `scanner.c`, `value.c` and `vm.c`
76
131
77
-
4. Add this code as `clox_stdio.h` to the folder:
132
+
4. Add this code as a new file `clox_stdio.h` to the same folder:
0 commit comments