-
Notifications
You must be signed in to change notification settings - Fork 51
rust: Add a Rust compatible API implementation. #141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: next
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| rust/Cargo.lock |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /* | ||
| * Copyright (c) 2025 TOKITA Hiroshi | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #include <Arduino.h> | ||
| #include "zephyrInternal.h" | ||
|
|
||
| extern "C" { | ||
| int32_t map_i32(int32_t x, int32_t in_min, int32_t in_max, int32_t out_min, int32_t out_max); | ||
| uint16_t makeWord_w(uint16_t w); | ||
| uint16_t makeWord_hl(byte h, byte l); | ||
| } | ||
|
|
||
| long map(long x, long in_min, long in_max, long out_min, long out_max) | ||
| { | ||
| return map_i32(x, in_min, in_max, out_min, out_max); | ||
| } | ||
|
|
||
| uint16_t makeWord(uint16_t w) { | ||
| return makeWord_w(w); | ||
| } | ||
| uint16_t makeWord(byte h, byte l) { | ||
| return makeWord_hl(h, l); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| [package] | ||
| name = "arduinocore_api_rust" | ||
| version = "0.1.0" | ||
| edition = "2024" | ||
| license = "Apache-2.0" | ||
|
|
||
| [lib] | ||
| crate-type = ["staticlib"] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| // Copyright (c) 2025 TOKITA Hiroshi | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| #[unsafe(no_mangle)] | ||
| pub extern "C" fn map_i32( | ||
| x: i32, in_min: i32, in_max: i32, out_min: i32, out_max: i32 | ||
| ) -> i32 { | ||
| let num = x.wrapping_sub(in_min).wrapping_mul(out_max.wrapping_sub(out_min)); | ||
| let den = in_max.wrapping_sub(in_min); | ||
| // Note: To keep compatibility, the panic when den=0 is left as is. | ||
| num / den.wrapping_add(out_min) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Due to operator precedence, this evaluates as: Original C++ code: return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;Can we just follow similar arithmetic syntax in rust too, the code using
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this is not a "new feature" but a reimplementation of the original implementation, we believe compatibility should be maintained. From another perspective, this implementation is an internal implementation of the C++ API, |
||
| } | ||
|
|
||
| #[unsafe(no_mangle)] | ||
| pub extern "C" fn makeWord_w(w: u16) -> u16 { | ||
| w | ||
| } | ||
|
|
||
| #[unsafe(no_mangle)] | ||
| pub extern "C" fn makeWord_hl(h: u8, l: u8) -> u16 { | ||
| ((h as u16) << 8) | (l as u16) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| // Copyright (c) 2025 TOKITA Hiroshi | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| #![no_std] | ||
|
|
||
| mod common; | ||
| pub use common::*; | ||
|
|
||
| use core::panic::PanicInfo; | ||
|
|
||
| #[panic_handler] | ||
| fn panic(_panic: &PanicInfo<'_>) -> ! { | ||
| loop {} | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The recommended way to install Rust is via the rustup.rs installer script, Ubuntu's rustup package from apt is often outdated and may not work correctly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also,
All initialization is lumped into one "Initialize" step mixing unrelated concerns (cloning API, copying files, Rust setup)...
Better structure:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed. Thank you for your comment.