Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@ _deps

core
venv

*.wasm
11 changes: 8 additions & 3 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
# Examples

This folder contains multiple different example programs for WARDuino.
All WebAssembly text files (.wat) from the examples can be run without additional hardware, by using the emulated version of WARDuino.
This folder contains the code for all examples from the [tutorials](https://topllab.github.io/WARDuino/guide/examples/) from the documentation website.

Additionally, the `wat` folder contains a simple example in textual WebAssembly, which relies on no other higher-level languages.

## Running the examples

All WebAssembly binaries (.wasm) from the examples can be run without additional hardware, by using the emulated version of WARDuino.
This version is packaged as a command line interface.

```bash
./wdcli --file example.wat
./wdcli --file example.wasm
```

Binary file added examples/assemblyscript/as-warduino-0.1.1.tgz
Binary file not shown.
31 changes: 26 additions & 5 deletions examples/assemblyscript/asconfig.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,32 @@
{
"entries": [
"src/main.ts"
],
"targets": {
"debug": {
"debug": true,
"outFile": "bin/main.debug.wasm",
"textFile": "bin/main.debug.wast"
},
"release": {
"binaryFile": "build/smartlamp.wasm",
"textFile": "build/smartlamp.wat",
"sourceMap": false,
"optimize": true
"converge": true,
"optimizeLevel": 3,
"outFile": "bin/main.wasm",
"shrinkLevel": 2
}
},
"options": {}
"options": {
"disable": [
"mutable-globals",
"sign-extension",
"nontrapping-f2i",
"bulk-memory"
],
"exportTable": true,
"exportRuntime": false,
"maximumMemory": 2,
"noAssert": false,
"runtime": "stub",
"sourceMap": true
}
}
111 changes: 0 additions & 111 deletions examples/assemblyscript/lib/warduino.ts

This file was deleted.

22 changes: 22 additions & 0 deletions examples/assemblyscript/main/analog-io.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {analogRead,
analogWrite,
delay,
print} from "as-warduino/assembly";

const IN: u32 = 13;
const OUT: u32 = 9;

function map(value: u32, x1: u32, y1: u32, x2: u32, y2: u32): u32 {
return <u32>((value - x1) * (y2 - x2) / (y1 - x1) + x2);
}

export function main(): void {
const value = analogRead(IN);

const output = map(value, 0, 1023, 0, 255);

analogWrite(OUT, output);

print(`sensor = ${value}\t output = ${output}`);
delay(1);
}
7 changes: 7 additions & 0 deletions examples/assemblyscript/main/analog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {analogRead, delay, print} from "as-warduino/assembly";

export function main(): void {
const value = analogRead(13);
print(value);
delay(1);
}
17 changes: 17 additions & 0 deletions examples/assemblyscript/main/blink-without-delay.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Blinking LED example
import {pinMode, PinMode, PinVoltage,
digitalWrite, delay} from "as-warduino/assembly";

export function main(): void {
const led = 26;
const pause: u32 = 1000;

pinMode(led, PinMode.OUTPUT);

while (true) {
digitalWrite(led, PinVoltage.HIGH);
delay(pause);
digitalWrite(led, PinVoltage.LOW);
delay(pause);
}
}
17 changes: 17 additions & 0 deletions examples/assemblyscript/main/blink.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Blinking LED example
import {pinMode, PinMode, PinVoltage,
digitalWrite, delay} from "as-warduino/assembly";

export function main(): void {
const led: u32 = 26;
const pause: u32 = 1000;

pinMode(led, PinMode.OUTPUT);

while (true) {
digitalWrite(led, PinVoltage.HIGH);
delay(pause);
digitalWrite(led, PinVoltage.LOW);
delay(pause);
}
}
41 changes: 41 additions & 0 deletions examples/assemblyscript/main/button.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {
delay,
digitalRead,
digitalWrite,
InterruptMode,
interruptOn,
pinMode,
PinMode,
PinVoltage
} from "as-warduino/assembly";

const button = 25;
const led = 26;

function invert(voltage: PinVoltage): PinVoltage {
switch (voltage) {
case PinVoltage.LOW:
return PinVoltage.HIGH;
case PinVoltage.HIGH:
default:
return PinVoltage.LOW;
}
}

function toggleLED(_topic: string, _payload: string): void {
// Get current status of LED
let status = digitalRead(led);
// Toggle LED
digitalWrite(led, invert(status));
}

export function main(): void {
pinMode(button, PinMode.INPUT);
pinMode(led, PinMode.OUTPUT);

interruptOn(button, InterruptMode.FALLING, toggleLED);

while (true) {
delay(1000);
}
}
5 changes: 5 additions & 0 deletions examples/assemblyscript/main/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const BUTTON = 25;
export const LED = 26;
export const SSID = "local-network";
export const PASSWORD = "network-password";
export const CLIENT_ID = "random-mqtt-client-id";
7 changes: 7 additions & 0 deletions examples/assemblyscript/main/configuration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {pinMode, PinMode} from "as-warduino/assembly";
import * as config from "./config";

export function main(): void {
let led = config.LED;
pinMode(led, PinMode.OUTPUT);
}
Loading