Add AssemblyScript for Arduino Blink (#62)

Related to #57
extensions
Max Graey 5 years ago committed by Volodymyr Shymanskyy
parent 14d6fa866b
commit 1580e4dc3b

@ -1,10 +1,10 @@
## Arduino blink example
This example was tested with ESP32.
This example was tested with ESP32.
ESP8266 is almost working (just needs a workaround for 64KiB wasm pages).
`PlatformIO` is used to build the host interpreter.
You can change the LED pin number in `platformio.ini` with `-DLED_BUILTIN` option.
`PlatformIO` is used to build the host interpreter.
You can change the LED pin number in `platformio.ini` with `-DLED_BUILTIN` option.
To run the example on ESP32:
@ -14,9 +14,9 @@ pio run -e esp32 -t upload && pio device monitor
## C++ app
`./wasm_cpp` directory contains an example Arduino app (sketch) that is compiled to WebAssembly.
Compilation is performed using `wasicc` here, but `clang --target=wasm32` can be used as well.
The resulting `wasm` binary is then converted to a C header using `xxd`.
`./wasm_cpp` directory contains an example Arduino app (sketch) that is compiled to WebAssembly.
Compilation is performed using `wasicc` here, but `clang --target=wasm32` can be used as well.
The resulting `wasm` binary is then converted to a C header using `xxd`.
See `wasm_cpp/build.sh` for details.
## Rust app
@ -28,9 +28,20 @@ rustup target add wasm32-unknown-unknown
```
To rebuild, use `wasm_rust/build.sh`.
## AssemblyScript app
For AssemblyScript the LED pin is currently hardcoded in the app.
Before building the app, please install npm dependencies:
```sh
yarn install
```
or
```sh
npm install
```
To rebuild, use `wasm_assemblyscript/build.sh` or `npm run build`.
## TinyGO app
For TinyGO, the LED pin is currently hardcoded in the app.
To rebuild, use `wasm_tinygo/build.sh`.
To rebuild, use `wasm_tinygo/build.sh`.

@ -15,6 +15,9 @@
// Rust app
//#include "../wasm_rust/app.wasm.h"
// AssemblyScript app
//#include "../wasm_assemblyscript/app.wasm.h"
// TinyGO app. For this, change _start to cwa_main below
//#include "../wasm_tinygo/app.wasm.h"

@ -0,0 +1,42 @@
@external("arduino", "millis")
declare function millis(): u32;
@external("arduino", "delay")
declare function delay(ms: u32): void;
@external("arduino", "pinMode")
declare function pinMode(pin: u32, mode: u32): void;
@external("arduino", "digitalWrite")
declare function digitalWrite(pin: u32, value: u32): void;
@external("arduino", "getPinLED")
declare function getPinLED(): u32;
const LOW: u32 = 0;
const HIGH: u32 = 1;
const INPUT: u32 = 0x0;
const OUTPUT: u32 = 0x1;
const INPUT_PULLUP: u32 = 0x2;
const LED: u32 = 19;
function setup(): void {
pinMode(LED, OUTPUT);
}
function run(): void {
digitalWrite(LED, HIGH);
delay(100);
digitalWrite(LED, LOW);
delay(900);
}
/*
* Entry point
*/
export function _start(): void {
setup();
while (1) run();
}

@ -0,0 +1,16 @@
unsigned char app_wasm[] = {
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x0d, 0x03, 0x60,
0x02, 0x7f, 0x7f, 0x00, 0x60, 0x00, 0x00, 0x60, 0x01, 0x7f, 0x00, 0x02,
0x3a, 0x03, 0x07, 0x61, 0x72, 0x64, 0x75, 0x69, 0x6e, 0x6f, 0x07, 0x70,
0x69, 0x6e, 0x4d, 0x6f, 0x64, 0x65, 0x00, 0x00, 0x07, 0x61, 0x72, 0x64,
0x75, 0x69, 0x6e, 0x6f, 0x0c, 0x64, 0x69, 0x67, 0x69, 0x74, 0x61, 0x6c,
0x57, 0x72, 0x69, 0x74, 0x65, 0x00, 0x00, 0x07, 0x61, 0x72, 0x64, 0x75,
0x69, 0x6e, 0x6f, 0x05, 0x64, 0x65, 0x6c, 0x61, 0x79, 0x00, 0x02, 0x03,
0x02, 0x01, 0x01, 0x05, 0x03, 0x01, 0x00, 0x00, 0x07, 0x13, 0x02, 0x06,
0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x02, 0x00, 0x06, 0x5f, 0x73, 0x74,
0x61, 0x72, 0x74, 0x00, 0x03, 0x0a, 0x27, 0x01, 0x25, 0x00, 0x41, 0x13,
0x41, 0x01, 0x10, 0x00, 0x03, 0x40, 0x41, 0x13, 0x41, 0x01, 0x10, 0x01,
0x41, 0xe4, 0x00, 0x10, 0x02, 0x41, 0x13, 0x41, 0x00, 0x10, 0x01, 0x41,
0x84, 0x07, 0x10, 0x02, 0x0c, 0x00, 0x00, 0x0b, 0x00, 0x0b
};
unsigned int app_wasm_len = 154;

@ -0,0 +1,12 @@
# Compile
npx asc app.ts -b app.wasm \
-O3z \
--runtime half \
--noAssert \
--use abort=
# Convert to WAT
#wasm2wat app.wasm -o app.wat
# Convert to C header
xxd -i app.wasm > app.wasm.h

@ -0,0 +1,9 @@
{
"scripts": {
"build": "npm run asbuild:optimized && xxd -i app.wasm > app.wasm.h",
"asbuild:optimized": "npx asc app.ts -b app.wasm -O3z --runtime half --noAssert --use abort="
},
"devDependencies": {
"assemblyscript": "^0.8.1-nightly.20200119"
}
}

@ -0,0 +1,6 @@
{
"extends": "./node_modules/assemblyscript/std/assembly.json",
"include": [
"./**/*.ts"
]
}

@ -0,0 +1,21 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
assemblyscript@^0.8.1-nightly.20200119:
version "0.8.1-nightly.20200119"
resolved "https://registry.yarnpkg.com/assemblyscript/-/assemblyscript-0.8.1-nightly.20200119.tgz#8b62ce969a3e00e2fe7108802b25d17425723a2f"
integrity sha512-KxIM7zmTz67Fo0Tjz0uUKcGXXZ9JS8upkvrChoe2bnVlYPWrGufKFCpGgGivClhOX2u49KvF99tAdQBKK2h3/A==
dependencies:
binaryen "90.0.0-nightly.20200101"
long "^4.0.0"
binaryen@90.0.0-nightly.20200101:
version "90.0.0-nightly.20200101"
resolved "https://registry.yarnpkg.com/binaryen/-/binaryen-90.0.0-nightly.20200101.tgz#75fb065e4a2dd88852c2872085e05eee0883106c"
integrity sha512-64lgruunUnkJD4YVD0DLCSRskqfo3xhoErM1H22GfkkWNT16/7tPTLG+MYqKLMALTiIyLL6L9UqmDaxOxDv0rw==
long@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/long/-/long-4.0.0.tgz#9a7b71cfb7d361a194ea555241c92f7468d5bf28"
integrity sha512-XsP+KhQif4bjX1kbuSiySJFNAehNxgLb6hPRGJ9QsUr8ajHkuXGdrHmFUTUUXhDwVX2R5bY4JNZEwbUiMhV+MA==
Loading…
Cancel
Save