Skip to content

Commit eb943d4

Browse files
committed
feat(rust-wasm): add wokwi_chip_ll crate
1 parent fd019c4 commit eb943d4

File tree

4 files changed

+16
-46
lines changed

4 files changed

+16
-46
lines changed

rust-wasm/Dockerfile

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ RUN chown nobody /project/src/lib.rs && chmod o+w /project
77

88
WORKDIR /project
99
USER nobody
10+
ENV HOME=/tmp
1011
RUN cargo build --target wasm32-unknown-unknown --release
1112

1213
# Wokwi builder configuration:

rust-wasm/project/Cargo.lock

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

rust-wasm/project/Cargo.toml

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ name = "chip-rust"
33
version = "0.1.0"
44
edition = "2021"
55

6-
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7-
86
[dependencies]
7+
wokwi_chip_ll = "0.1.0"
98

109
[lib]
1110
crate-type = ["cdylib"]

rust-wasm/project/src/lib.rs

+5-44
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,8 @@
1-
use std::{ffi::{c_void, CString}, os::raw::c_char};
1+
use std::ffi::{c_void, CString};
22

3-
// --- API Declaration ---
4-
5-
type PinId = i32;
6-
7-
#[repr(C)]
8-
pub struct WatchConfig {
9-
user_data: *const c_void,
10-
edge: u32,
11-
pin_change: *const c_void,
12-
}
13-
14-
/* Just a stub to specify the Chip API version */
15-
#[no_mangle]
16-
pub unsafe extern "C" fn __wokwi_api_version_1() -> u32 { return 0; }
17-
18-
extern "C" {
19-
fn pinInit(name: *const c_char, mode: u32) -> PinId;
20-
fn pinRead(pin: PinId) -> u32;
21-
fn pinWrite(pin: PinId, value: u32);
22-
fn pinWatch(pin: PinId, watch_config: *const WatchConfig) -> bool;
23-
fn debugPrint(message: *const c_char);
24-
}
25-
26-
/* Pin values */
27-
const LOW: u32 = 0;
28-
const HIGH: u32 = 1;
29-
30-
/* Pin modes */
31-
const INPUT: u32 = 0;
32-
const OUTPUT: u32 = 1;
33-
const INPUT_PULLUP: u32 = 2;
34-
const INPUT_PULLDOWN: u32 = 3;
35-
const ANALOG: u32 = 4;
36-
const OUTPUT_LOW: u32 = 16;
37-
const OUTPUT_HIGH: u32 = 16;
38-
39-
/* Pin edges */
40-
const RISING: u32 = 1;
41-
const FALLING: u32 = 2;
42-
const BOTH: u32 = 3;
43-
44-
/// --- Implementation ---
3+
use wokwi_chip_ll::{
4+
debugPrint, pinInit, pinWatch, pinWrite, PinId, WatchConfig, BOTH, HIGH, INPUT, LOW, OUTPUT,
5+
};
456

467
struct Chip {
478
pin_in: PinId,
@@ -59,7 +20,7 @@ pub unsafe fn on_pin_change(user_data: *const c_void, _pin: PinId, value: u32) {
5920

6021
#[no_mangle]
6122
pub unsafe extern "C" fn chipInit() {
62-
debugPrint(CString::new("Hello rust!").unwrap().into_raw());
23+
debugPrint(CString::new("Hello rusty!").unwrap().into_raw());
6324

6425
let chip = Chip {
6526
pin_in: pinInit(CString::new("IN").unwrap().into_raw(), INPUT),

0 commit comments

Comments
 (0)