Skip to content

Commit

Permalink
shell/democommands: Add example Rust module
Browse files Browse the repository at this point in the history
  • Loading branch information
chrysn committed Jul 9, 2022
1 parent 345f25f commit 730e8df
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 0 deletions.
3 changes: 3 additions & 0 deletions makefiles/cargo-targets.inc.mk
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ CARGO_COMPILE_COMMANDS_FLAGS = --clang
ifneq (,$(filter lsm303agr,$(USEMODULE)))
CARGO_OPTIONS += --features rust_riotmodules/riot-module-lsm303agr
endif
ifneq (,$(filter shell_democommands,$(USEMODULE)))
CARGO_OPTIONS += --features rust_riotmodules/riot-module-shell-democommands
endif

# This is duplicating the compile-commands rule because unlike in the use case
# when a $(RIOTBASE)/compile_commands.json is built, we *want* this to be
Expand Down
5 changes: 5 additions & 0 deletions sys/Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,11 @@ ifneq (,$(filter shell_commands,$(USEMODULE)))
endif
endif

ifneq (,$(filter shell_democommands,$(USEMODULE)))
USEMODULE += rust_riotmodules
USEMODULE += shell
endif

ifneq (,$(filter md5sum sha1sum sha256sum,$(USEMODULE)))
USEMODULE += vfs_util
USEMODULE += hashes
Expand Down
2 changes: 2 additions & 0 deletions sys/Makefile.include
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ ifneq (,$(filter shell_lock,$(USEMODULE)))
include $(RIOTBASE)/sys/shell_lock/Makefile.include
endif

PSEUDOMODULES += shell_democommands

ifneq (,$(filter rust_riotmodules,$(USEMODULE)))
include $(RIOTBASE)/sys/rust_riotmodules/Makefile.include
endif
1 change: 1 addition & 0 deletions sys/rust_riotmodules/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ publish = false
# autogenerated (or at least automatically checked for consistency).

riot-module-lsm303agr = { path = "../../drivers/lsm303agr", optional = true }
riot-module-shell-democommands = { path = "../../sys/shell/democommands", optional = true }
3 changes: 3 additions & 0 deletions sys/rust_riotmodules/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@

#[cfg(feature = "riot-module-lsm303agr")]
extern crate riot_module_lsm303agr;

#[cfg(feature = "riot-module-shell-democommands")]
extern crate riot_module_shell_democommands;
14 changes: 14 additions & 0 deletions sys/shell/democommands/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "riot-module-shell-democommands"
version = "0.1.0"
edition = "2021"

authors = ["Christian Amsüss <[email protected]>"]
license = "LGPL-2.1-only"

# Shipped with RIOT-OS; this has no external API that would make
# sense to consume in any context than from within RIOT
publish = false

[dependencies]
riot-wrappers = "^0.7.17"
17 changes: 17 additions & 0 deletions sys/shell/democommands/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#![no_std]

use riot_wrappers::println;
use core::fmt::Write;

riot_wrappers::static_command!(static_hello_world, "hello_world", "Print a greeting", hello_world);

pub fn hello_world<'a>(_w: &mut impl Write, args: impl IntoIterator<Item=&'a str>) {
let mut args = args.into_iter();
let commandname = args.next().expect("How was this started without an argv[0]?");

match args.next() {
Some("--help") => println!("Usage: {commandname}"),
None => println!("Hello RIOT!"),
_ => println!("Invalid argument."),
};
}

0 comments on commit 730e8df

Please sign in to comment.