Skip to content

Commit 97fcd6d

Browse files
committed
Initial test with string passing and Rust
0 parents  commit 97fcd6d

16 files changed

+625
-0
lines changed

.gitignore

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.pio
2+
.vscode/.browse.c_cpp.db*
3+
.vscode/c_cpp_properties.json
4+
.vscode/launch.json
5+
.vscode/ipch
6+
node_modules
7+
*.wasm
8+
app.wasm.h
9+
.vscode/

.travis.yml

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Continuous Integration (CI) is the practice, in software
2+
# engineering, of merging all developer working copies with a shared mainline
3+
# several times a day < https://docs.platformio.org/page/ci/index.html >
4+
#
5+
# Documentation:
6+
#
7+
# * Travis CI Embedded Builds with PlatformIO
8+
# < https://docs.travis-ci.com/user/integration/platformio/ >
9+
#
10+
# * PlatformIO integration with Travis CI
11+
# < https://docs.platformio.org/page/ci/travis.html >
12+
#
13+
# * User Guide for `platformio ci` command
14+
# < https://docs.platformio.org/page/userguide/cmd_ci.html >
15+
#
16+
#
17+
# Please choose one of the following templates (proposed below) and uncomment
18+
# it (remove "# " before each line) or use own configuration according to the
19+
# Travis CI documentation (see above).
20+
#
21+
22+
23+
#
24+
# Template #1: General project. Test it using existing `platformio.ini`.
25+
#
26+
27+
# language: python
28+
# python:
29+
# - "2.7"
30+
#
31+
# sudo: false
32+
# cache:
33+
# directories:
34+
# - "~/.platformio"
35+
#
36+
# install:
37+
# - pip install -U platformio
38+
# - platformio update
39+
#
40+
# script:
41+
# - platformio run
42+
43+
44+
#
45+
# Template #2: The project is intended to be used as a library with examples.
46+
#
47+
48+
# language: python
49+
# python:
50+
# - "2.7"
51+
#
52+
# sudo: false
53+
# cache:
54+
# directories:
55+
# - "~/.platformio"
56+
#
57+
# env:
58+
# - PLATFORMIO_CI_SRC=path/to/test/file.c
59+
# - PLATFORMIO_CI_SRC=examples/file.ino
60+
# - PLATFORMIO_CI_SRC=path/to/test/directory
61+
#
62+
# install:
63+
# - pip install -U platformio
64+
# - platformio update
65+
#
66+
# script:
67+
# - platformio ci --lib="." --board=ID_1 --board=ID_2 --board=ID_N

assemblyscript/app.ts

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
@external("arduino", "millis")
2+
declare function millis(): u32;
3+
4+
@external("arduino", "delay")
5+
declare function delay(ms: u32): void;
6+
7+
@external("arduino", "pinMode")
8+
declare function pinMode(pin: u32, mode: u32): void;
9+
10+
@external("arduino", "digitalWrite")
11+
declare function digitalWrite(pin: u32, value: u32): void;
12+
13+
@external("arduino", "getPinLED")
14+
declare function getPinLED(): u32;
15+
16+
@external("arduino", "serialLog")
17+
declare function println(output: string): void;
18+
//declare function println(output: usize, len: i32): void;
19+
//declare function println(output: string, len: i32): void;
20+
21+
function log(out: string): void {
22+
//println(out, out.length) // <- Only first letter arrives, but lenght is correct
23+
println(out);
24+
//println(out); // <- Only first letter arrives
25+
//String.UTF16.encode(out) // <- Just referencing this crashes
26+
//println(changetype<usize>(String.UTF8.encode(out, true)), String.UTF8.byteLength(out, true)); // <- Crash
27+
//println(String.UTF8.encode(out, true), String.UTF8.byteLength(out, true)); // <- Crash
28+
}
29+
30+
const LOW: u32 = 0;
31+
const HIGH: u32 = 1;
32+
33+
const INPUT: u32 = 0x0;
34+
const OUTPUT: u32 = 0x1;
35+
const INPUT_PULLUP: u32 = 0x2;
36+
37+
let LED: u32 = -1;
38+
39+
function setup(): void {
40+
LED = getPinLED();
41+
pinMode(LED, OUTPUT);
42+
}
43+
44+
function run(): void {
45+
log('From Web Assembly')
46+
digitalWrite(LED, HIGH);
47+
delay(1000);
48+
digitalWrite(LED, LOW);
49+
delay(1000);
50+
}
51+
52+
/*
53+
* Entry point
54+
*/
55+
export function _start(): void {
56+
setup();
57+
while (1) run();
58+
}

assemblyscript/package-lock.json

+36
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assemblyscript/package.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "wasm-arduino-wifi-app",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1",
8+
"build": "npm run asbuild:optimized && xxd -i app.wasm > app.wasm.h",
9+
"asbuild:optimized": "npx asc app.ts -b app.wasm -O3z --runtime full --noAssert --use abort="
10+
},
11+
"author": "Alvaro Viebrantz",
12+
"license": "MIT",
13+
"devDependencies": {
14+
"as-bind": "^0.1.3",
15+
"assemblyscript": "^0.8.1"
16+
}
17+
}

assemblyscript/tsconfig.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": "./node_modules/assemblyscript/std/assembly.json",
3+
"include": [
4+
"./**/*.ts"
5+
]
6+
}

include/README

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
This directory is intended for project header files.
3+
4+
A header file is a file containing C declarations and macro definitions
5+
to be shared between several project source files. You request the use of a
6+
header file in your project source file (C, C++, etc) located in `src` folder
7+
by including it, with the C preprocessing directive `#include'.
8+
9+
```src/main.c
10+
11+
#include "header.h"
12+
13+
int main (void)
14+
{
15+
...
16+
}
17+
```
18+
19+
Including a header file produces the same results as copying the header file
20+
into each source file that needs it. Such copying would be time-consuming
21+
and error-prone. With a header file, the related declarations appear
22+
in only one place. If they need to be changed, they can be changed in one
23+
place, and programs that include the header file will automatically use the
24+
new version when next recompiled. The header file eliminates the labor of
25+
finding and changing all the copies as well as the risk that a failure to
26+
find one copy will result in inconsistencies within a program.
27+
28+
In C, the usual convention is to give header files names that end with `.h'.
29+
It is most portable to use only letters, digits, dashes, and underscores in
30+
header file names, and at most one dot.
31+
32+
Read more about using header files in official GCC documentation:
33+
34+
* Include Syntax
35+
* Include Operation
36+
* Once-Only Headers
37+
* Computed Includes
38+
39+
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html

lib/README

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
This directory is intended for project specific (private) libraries.
3+
PlatformIO will compile them to static libraries and link into executable file.
4+
5+
The source code of each library should be placed in a an own separate directory
6+
("lib/your_library_name/[here are source files]").
7+
8+
For example, see a structure of the following two libraries `Foo` and `Bar`:
9+
10+
|--lib
11+
| |
12+
| |--Bar
13+
| | |--docs
14+
| | |--examples
15+
| | |--src
16+
| | |- Bar.c
17+
| | |- Bar.h
18+
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
19+
| |
20+
| |--Foo
21+
| | |- Foo.c
22+
| | |- Foo.h
23+
| |
24+
| |- README --> THIS FILE
25+
|
26+
|- platformio.ini
27+
|--src
28+
|- main.c
29+
30+
and a contents of `src/main.c`:
31+
```
32+
#include <Foo.h>
33+
#include <Bar.h>
34+
35+
int main (void)
36+
{
37+
...
38+
}
39+
40+
```
41+
42+
PlatformIO Library Dependency Finder will find automatically dependent
43+
libraries scanning project source files.
44+
45+
More information about PlatformIO Library Dependency Finder
46+
- https://docs.platformio.org/page/librarymanager/ldf.html

platformio.ini

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
;PlatformIO Project Configuration File
2+
;
3+
; Build options: build flags, source filter
4+
; Upload options: custom upload port, speed and extra flags
5+
; Library options: dependencies, extra library storages
6+
; Advanced options: extra scripting
7+
;
8+
; Please visit documentation for the other options and examples
9+
; https://docs.platformio.org/page/projectconf.html
10+
11+
[env:esp32]
12+
platform = espressif32
13+
board = esp32dev
14+
framework = arduino
15+
16+
monitor_speed = 115200
17+
18+
src_build_flags =
19+
-Dd_m3LogOutput=false
20+
-DLED_BUILTIN=5
21+
-DESP32
22+
-O3 -flto
23+
-Wno-unused-function
24+
-Wno-unused-variable
25+
-Wno-unused-parameter
26+
-Wno-missing-field-initializers
27+
28+
lib_deps=
29+
#wasm3/wasm3
30+
wasm3/wasm3-arduino
31+
32+
#lib_extra_dirs=
33+
#.pio/libdeps/esp32/wasm3

rust/app.rs

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#![no_std]
2+
3+
mod arduino_api;
4+
use arduino_api::*;
5+
6+
static LED: u32 = 5;
7+
8+
fn setup() {
9+
pin_mode(LED, OUTPUT);
10+
}
11+
12+
fn run() {
13+
serial_log("Hello from Rust");
14+
digital_write(LED, HIGH);
15+
delay(1000);
16+
digital_write(LED, LOW);
17+
delay(1000);
18+
}
19+
20+
/*
21+
* Entry point
22+
*/
23+
24+
#[no_mangle]
25+
pub extern fn _start() {
26+
setup();
27+
loop {
28+
run();
29+
}
30+
}
31+
32+
#[panic_handler]
33+
fn handle_panic(_: &core::panic::PanicInfo) -> ! {
34+
loop {}
35+
}

rust/arduino_api.rs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#![allow(dead_code)]
2+
3+
#[link(wasm_import_module = "arduino")]
4+
extern {
5+
#[link_name = "millis"] fn unsafe_millis() -> u32;
6+
#[link_name = "delay"] fn unsafe_delay(ms: u32);
7+
#[link_name = "pinMode"] fn unsafe_pinMode(pin:u32, mode:u32);
8+
#[link_name = "digitalWrite"] fn unsafe_digitalWrite(pin:u32, value:u32);
9+
#[link_name = "serialLog"] fn unsafe_println(out: *const u8);
10+
#[link_name = "getPinLED"] fn unsafe_getPinLED() -> u32;
11+
}
12+
13+
pub static LOW:u32 = 0;
14+
pub static HIGH:u32 = 1;
15+
16+
pub static INPUT:u32 = 0x0;
17+
pub static OUTPUT:u32 = 0x1;
18+
pub static INPUT_PULLUP:u32 = 0x2;
19+
20+
pub fn millis () -> u32 { unsafe { unsafe_millis() } }
21+
pub fn delay (ms: u32) { unsafe { unsafe_delay(ms); } }
22+
pub fn pin_mode (pin:u32, mode:u32) { unsafe { unsafe_pinMode(pin, mode) } }
23+
pub fn digital_write (pin:u32, value:u32) { unsafe { unsafe_digitalWrite(pin, value) } }
24+
pub fn serial_log (out: &str) {
25+
unsafe {
26+
unsafe_println(out.as_bytes().as_ptr() as *const u8)
27+
}
28+
}
29+
pub fn get_pin_led () -> u32 { unsafe { unsafe_getPinLED() } }

0 commit comments

Comments
 (0)