Skip to content
Snippets Groups Projects
Commit 1c74d4e6 authored by Per Lindgren's avatar Per Lindgren
Browse files

heapless strings

parent 99e5cfef
Branches experimental
No related tags found
No related merge requests found
......@@ -16,7 +16,10 @@ cortex-m-rtfm = "0.2.2"
cortex-m = "0.3.1"
rtfm-core = "0.1.0"
cortex-m-semihosting = "0.2.0"
heapless = "0.2.0"
[dependencies.heapless]
path = "../heapless"
#version = "0.2.0"
[dependencies.cortex-m-debug]
version = "0.1.0"
......
//! Example of parsing an &str
#![deny(unsafe_code)]
//#![deny(warnings)]
#![feature(proc_macro)]
#![feature(unsize)]
#![no_std]
extern crate cortex_m_rtfm as rtfm;
extern crate f4;
extern crate heapless;
#[macro_use]
extern crate cortex_m_debug;
use rtfm::app;
use heapless::String;
use core::fmt::Write;
use core::marker::Unsize;
// RTFM FRAMEWORK
app! {
device: f4::stm32f40x,
}
#[derive(Debug)]
enum Command {
Start,
Stop,
Freq(u32),
}
fn f(s: &&str) -> bool {
!(s == &"")
}
const LEN: usize = 16;
fn parse<'a, A>(s: &str, err: &'a mut String<A>) -> Result<Command, &'a mut String<A>>
where
A: Unsize<[u8]>,
{
//let mut iter = s.split(' ').filter(|c| !(c == &""));
let mut iter = s.split(' ').filter(f);
match iter.next() {
Some("Stop") => Ok(Command::Stop),
Some("Start") => Ok(Command::Start),
Some("Freq") => match iter.next() {
Some(fs) => {
if let Ok(f) = fs.parse::<u32>() {
Ok(Command::Freq(f))
} else {
let _ = write!(err, "{}", fs);
Err(err)
}
}
let _ = write!(err, "No frequency");
None => Err(err),
},
Some(command) => {
let _ = write!(err, "{}", command);
Err(&mut err);
}
None => Err("No input"),
}
}
fn init(_p: init::Peripherals) {
let mut err: String<[u8; LEN]> = String::new();
ipln!("init");
ipln!("{:?}", parse("Start", &mut err)); // works
ipln!("err {}", err);
// ipln!("{:?}", parse(" Start ")); // works with white spaces
// ipln!("{:?}", parse(" Freq 122 ")); // works with white spaces
ipln!("{:?}", parse(" Freq a122 ", &mut err)); // Invalid frequency
ipln!("err {}", err);
// ipln!("{:?}", parse(" Freq ")); // No frequency
// ipln!("{:?}", parse("")); // No input
}
fn idle() -> ! {
// Sleep
loop {
rtfm::wfi();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment