Skip to content

Commit a629378

Browse files
committed
Ready for Release 0.0.2
1 parent a972997 commit a629378

File tree

10 files changed

+447
-59
lines changed

10 files changed

+447
-59
lines changed

README.md

Lines changed: 69 additions & 14 deletions
Original file line numberDiff line numberDiff 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)
22

33
## Description
44

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.
66

77
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.
88

99
## Getting Started
1010

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.
1212

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:
1421

1522
```javascript
1623
var led = 88;
@@ -26,15 +33,63 @@ for (var i = 1; i <= 10; i = i + 1) {
2633
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:
2734

2835
```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);
3388
```
3489

3590
Note: The Arduino_H7_Video library uses some shared SDRAM for the framebuffer, and `SDRAM.begin();` should **not** be called after initializing the display.
3691

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).
3893

3994
## Adding Functions
4095

@@ -54,10 +109,10 @@ Doing these steps correctly and in order ensures that the project should remain
54109

55110
There are a number of ideas for the future direction of this library:
56111

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)
58113
* Complete support for more "Arduino.h" functions
59114
* 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)
61116
* Use of the M4 co-processor as a graphics accelerator
62117
* Changing the Lox interpreter language (not the JIT backend) to something less like JavaScript (GFX-Basic?)
63118
* 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
70125

71126
1. Clone (part of) the GitHub repo munificent/craftinginterpreters: `svn export https://github.com/munificent/craftinginterpreters.git/trunk/c`
72127

73-
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`.
74129

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`
76131

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:
78133

79134
```c
80135
#ifndef clox_stdio_h
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#define SECRET_SSID "MyNetwork"
2+
#define SECRET_PASS "<passphrase>"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#define CLOX_GRAPHICS 0
2+
#define CLOX_USB_HOST 0
3+
#define CLOX_WEB_CONSOLE 0
4+
#define CLOX_USE_SDRAM 0

0 commit comments

Comments
 (0)