Skip to content

Commit 7d4aced

Browse files
committed
Add documentation.
1 parent ded0f5e commit 7d4aced

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

src/lib.rs

+88
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,91 @@
1+
//! # PX4 bindings for Rust
2+
//!
3+
//! This crate provides the framework to make dynamically loadable PX4 modules
4+
//! in Rust. Right now, it provides bindings for the two most important APIs:
5+
//! Logging and uORB. It also provides the entry point for your module, and
6+
//! handles panics on the main thread of the module.
7+
//!
8+
//! ## Compiling and running
9+
//!
10+
//! To build a PX4 module in Rust, create a crate as you would for any other
11+
//! application binary, and then add the following to your Cargo.toml:
12+
//!
13+
//! ```text
14+
//! [lib]
15+
//! crate-type = ["cdylib"]
16+
//! path = "src/main.rs"
17+
//! ```
18+
//!
19+
//! This will turn your program into a loadable module instead of a standalone
20+
//! application. The resulting file will be called `lib<name>.so`, which you
21+
//! can manually rename to `<name>.px4mod` if you want.
22+
//!
23+
//! To run your module, use the
24+
//! [`dyn`](https://dev.px4.io/en/middleware/modules_command.html#dyn)
25+
//! PX4 command. Give it the full path name, followed by any arguments to your
26+
//! module. Note that `dyn` will *not* reload your file if you run it again.
27+
//! If you want to run a changed version of your module, you'll either need to
28+
//! restart PX4, or move/rename the file.
29+
//!
30+
//! ## Entry point
31+
//!
32+
//! Mark your entry function with `#[px4_module_main]`. The boilerplate code
33+
//! needed to set up the environment and export the function under the right
34+
//! name is then inserted automatically.
35+
//!
36+
//! Your main function should take a `&[&str]` as argument, and return a `i32`
37+
//! status code. A panic from your main thread is caught and results in a
38+
//! status code of −1.
39+
//!
40+
//! ### Example
41+
//!
42+
//! ```
43+
//! extern crate px4;
44+
//!
45+
//! use px4::px4_module_main;
46+
//!
47+
//! #[px4_module_main]
48+
//! fn my_module(args: &[&str]) -> i32 {
49+
//! 0
50+
//! }
51+
//! ```
52+
//!
53+
//! ## Logging
54+
//!
55+
//! As soon as your main function is entered, logging is already set up using
56+
//! the standard [`log` crate](https://docs.rs/log/). You can use the standard
57+
//! logging macros such as `info!`, `warn!`, and `error!` to log messages,
58+
//! equivalent to `PX4_INFO` (etc.) in C and C++.
59+
//!
60+
//! Use the `info_raw!` macro to send raw output, equivalent to the
61+
//! `PX4_INFO_RAW` macro in C and C++.
62+
//! Do not use standard output or standard error for this, as the standard
63+
//! streams of the PX4 process are often not the ones connected to the terminal
64+
//! the user is looking at.
65+
//!
66+
//! ### Example
67+
//!
68+
//! ```
69+
//! extern crate log;
70+
//! extern crate px4;
71+
//!
72+
//! use log::{info, warn};
73+
//! use px4::px4_module_main;
74+
//!
75+
//! #[px4_module_main]
76+
//! fn my_module(args: &[&str]) -> i32 {
77+
//! info!("Hello World!");
78+
//! warn!("A warning!");
79+
//! panic!("Bye!");
80+
//! }
81+
//! ```
82+
//!
83+
//! ### uORB
84+
//!
85+
//! See the [`uorb` module](uorb/index.html) for documentation on how to use
86+
//! the uORB bindings.
87+
//!
88+
189
extern crate log;
290
extern crate px4_macros;
391

src/logging.rs

+15
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ extern "C" {
66
fn px4_log_raw(level: i32, fmt: *const u8, ...);
77
}
88

9+
#[doc(hidden)]
910
pub enum LogLevel {
1011
Debug = 0,
1112
Info = 1,
@@ -14,6 +15,7 @@ pub enum LogLevel {
1415
Panic = 4,
1516
}
1617

18+
#[doc(hidden)]
1719
pub fn log_raw(level: LogLevel, message: &str) {
1820
unsafe {
1921
px4_log_raw(
@@ -25,6 +27,19 @@ pub fn log_raw(level: LogLevel, message: &str) {
2527
}
2628
}
2729

30+
/// Print output without any decoration.
31+
///
32+
/// The equivalent of `PX4_INFO_RAW` in C and C++.
33+
///
34+
/// ## Example
35+
///
36+
/// ```
37+
/// # #[macro_use] extern crate px4;
38+
/// # #[no_mangle] fn px4_log_raw() {}
39+
/// # fn main() {
40+
/// info_raw!("Hello {}!\n", "World");
41+
/// # }
42+
/// ```
2843
#[macro_export]
2944
macro_rules! info_raw {
3045
($($arg:tt)+) => (

src/uorb/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
//! Bindings to the uORB messaging system.
2+
//!
3+
//! This part is not yet finished.
4+
15
mod c;
26
mod publish;
37
mod subscribe;

0 commit comments

Comments
 (0)