Skip to content

Commit e5caa99

Browse files
MaxGraeyvshymanskyy
authored andcommitted
[rust] Rewrite Rust example (#3)
1 parent 7701cb0 commit e5caa99

File tree

4 files changed

+49
-44
lines changed

4 files changed

+49
-44
lines changed

wasm_apps/rust/app.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,29 @@
33
mod arduino_api;
44
use arduino_api::*;
55

6-
static LED: u32 = 19;
7-
8-
fn setup() {
9-
pin_mode(LED, OUTPUT);
6+
struct App {
7+
led: u32,
108
}
119

12-
fn run() {
13-
digital_write(LED, HIGH);
14-
delay(100);
15-
digital_write(LED, LOW);
16-
delay(900);
17-
}
10+
impl App {
11+
fn new() -> Self {
12+
let led = get_pin_led();
13+
pin_mode(led, OUTPUT);
14+
Self { led }
15+
}
1816

19-
/*
20-
* Entry point
21-
*/
17+
fn update(&self) {
18+
digital_write(self.led, HIGH);
19+
delay(100);
20+
digital_write(self.led, LOW);
21+
delay(900);
22+
}
23+
}
2224

2325
#[no_mangle]
2426
pub extern fn _start() {
25-
setup();
26-
loop {
27-
run();
28-
}
27+
let app = App::new();
28+
loop { app.update() };
2929
}
3030

3131
#[panic_handler]

wasm_apps/rust/app.wasm

64 Bytes
Binary file not shown.

wasm_apps/rust/app.wasm.h

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
unsigned char app_wasm[] = {
2-
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x0d, 0x03, 0x60,
3-
0x02, 0x7f, 0x7f, 0x00, 0x60, 0x00, 0x00, 0x60, 0x01, 0x7f, 0x00, 0x02,
4-
0x3a, 0x03, 0x07, 0x61, 0x72, 0x64, 0x75, 0x69, 0x6e, 0x6f, 0x05, 0x64,
5-
0x65, 0x6c, 0x61, 0x79, 0x00, 0x02, 0x07, 0x61, 0x72, 0x64, 0x75, 0x69,
6-
0x6e, 0x6f, 0x07, 0x70, 0x69, 0x6e, 0x4d, 0x6f, 0x64, 0x65, 0x00, 0x00,
7-
0x07, 0x61, 0x72, 0x64, 0x75, 0x69, 0x6e, 0x6f, 0x0c, 0x64, 0x69, 0x67,
8-
0x69, 0x74, 0x61, 0x6c, 0x57, 0x72, 0x69, 0x74, 0x65, 0x00, 0x00, 0x03,
9-
0x02, 0x01, 0x01, 0x05, 0x03, 0x01, 0x00, 0x01, 0x06, 0x0d, 0x02, 0x7f,
10-
0x00, 0x41, 0x80, 0x20, 0x0b, 0x7f, 0x00, 0x41, 0x80, 0x20, 0x0b, 0x07,
11-
0x2e, 0x04, 0x06, 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x02, 0x00, 0x0a,
12-
0x5f, 0x5f, 0x64, 0x61, 0x74, 0x61, 0x5f, 0x65, 0x6e, 0x64, 0x03, 0x00,
13-
0x0b, 0x5f, 0x5f, 0x68, 0x65, 0x61, 0x70, 0x5f, 0x62, 0x61, 0x73, 0x65,
14-
0x03, 0x01, 0x06, 0x5f, 0x73, 0x74, 0x61, 0x72, 0x74, 0x00, 0x03, 0x0a,
15-
0x26, 0x01, 0x24, 0x00, 0x41, 0x13, 0x41, 0x01, 0x10, 0x01, 0x03, 0x40,
16-
0x41, 0x13, 0x41, 0x01, 0x10, 0x02, 0x41, 0xe4, 0x00, 0x10, 0x00, 0x41,
17-
0x13, 0x41, 0x00, 0x10, 0x02, 0x41, 0x84, 0x07, 0x10, 0x00, 0x0c, 0x00,
18-
0x0b, 0x00, 0x0b
2+
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x11, 0x04, 0x60,
3+
0x01, 0x7f, 0x00, 0x60, 0x02, 0x7f, 0x7f, 0x00, 0x60, 0x00, 0x01, 0x7f,
4+
0x60, 0x00, 0x00, 0x02, 0x4e, 0x04, 0x07, 0x61, 0x72, 0x64, 0x75, 0x69,
5+
0x6e, 0x6f, 0x05, 0x64, 0x65, 0x6c, 0x61, 0x79, 0x00, 0x00, 0x07, 0x61,
6+
0x72, 0x64, 0x75, 0x69, 0x6e, 0x6f, 0x07, 0x70, 0x69, 0x6e, 0x4d, 0x6f,
7+
0x64, 0x65, 0x00, 0x01, 0x07, 0x61, 0x72, 0x64, 0x75, 0x69, 0x6e, 0x6f,
8+
0x0c, 0x64, 0x69, 0x67, 0x69, 0x74, 0x61, 0x6c, 0x57, 0x72, 0x69, 0x74,
9+
0x65, 0x00, 0x01, 0x07, 0x61, 0x72, 0x64, 0x75, 0x69, 0x6e, 0x6f, 0x09,
10+
0x67, 0x65, 0x74, 0x50, 0x69, 0x6e, 0x4c, 0x45, 0x44, 0x00, 0x02, 0x03,
11+
0x02, 0x01, 0x03, 0x04, 0x05, 0x01, 0x70, 0x01, 0x01, 0x01, 0x05, 0x03,
12+
0x01, 0x00, 0x01, 0x06, 0x13, 0x03, 0x7f, 0x01, 0x41, 0x80, 0x20, 0x0b,
13+
0x7f, 0x00, 0x41, 0x80, 0x20, 0x0b, 0x7f, 0x00, 0x41, 0x80, 0x20, 0x0b,
14+
0x07, 0x2e, 0x04, 0x06, 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x02, 0x00,
15+
0x0a, 0x5f, 0x5f, 0x64, 0x61, 0x74, 0x61, 0x5f, 0x65, 0x6e, 0x64, 0x03,
16+
0x01, 0x0b, 0x5f, 0x5f, 0x68, 0x65, 0x61, 0x70, 0x5f, 0x62, 0x61, 0x73,
17+
0x65, 0x03, 0x02, 0x06, 0x5f, 0x73, 0x74, 0x61, 0x72, 0x74, 0x00, 0x04,
18+
0x0a, 0x41, 0x01, 0x3f, 0x01, 0x01, 0x7f, 0x10, 0x83, 0x80, 0x80, 0x80,
19+
0x00, 0x22, 0x00, 0x41, 0x01, 0x10, 0x81, 0x80, 0x80, 0x80, 0x00, 0x03,
20+
0x40, 0x20, 0x00, 0x41, 0x01, 0x10, 0x82, 0x80, 0x80, 0x80, 0x00, 0x41,
21+
0xe4, 0x00, 0x10, 0x80, 0x80, 0x80, 0x80, 0x00, 0x20, 0x00, 0x41, 0x00,
22+
0x10, 0x82, 0x80, 0x80, 0x80, 0x00, 0x41, 0x84, 0x07, 0x10, 0x80, 0x80,
23+
0x80, 0x80, 0x00, 0x0c, 0x00, 0x0b, 0x0b
1924
};
20-
unsigned int app_wasm_len = 195;
25+
unsigned int app_wasm_len = 259;

wasm_apps/rust/arduino_api.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,22 @@
44
extern {
55
#[link_name = "millis"] fn unsafe_millis() -> u32;
66
#[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);
7+
#[link_name = "pinMode"] fn unsafe_pinMode(pin: u32, mode: u32);
8+
#[link_name = "digitalWrite"] fn unsafe_digitalWrite(pin: u32, value: u32);
99

1010
#[link_name = "getPinLED"] fn unsafe_getPinLED() -> u32;
1111
}
1212

13-
pub static LOW:u32 = 0;
14-
pub static HIGH:u32 = 1;
13+
pub static LOW: u32 = 0;
14+
pub static HIGH: u32 = 1;
1515

16-
pub static INPUT:u32 = 0x0;
17-
pub static OUTPUT:u32 = 0x1;
18-
pub static INPUT_PULLUP:u32 = 0x2;
16+
pub static INPUT: u32 = 0x0;
17+
pub static OUTPUT: u32 = 0x1;
18+
pub static INPUT_PULLUP: u32 = 0x2;
1919

2020
pub fn millis () -> u32 { unsafe { unsafe_millis() } }
2121
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) } }
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) } }
2424
pub fn get_pin_led () -> u32 { unsafe { unsafe_getPinLED() } }
2525

0 commit comments

Comments
 (0)