Commit 43188626 authored by tibut's avatar tibut
Browse files

rust polt example

parents cee35266 2b764950
Pipeline #3410 failed with stage
in 12 minutes and 5 seconds
......@@ -45,6 +45,13 @@ dependencies = [
"libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "bhi160-loop"
version = "0.1.0"
dependencies = [
"card10-l0dable 0.1.1",
]
[[package]]
name = "bindgen"
version = "0.51.0"
......@@ -279,6 +286,14 @@ name = "regex-syntax"
version = "0.6.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "rust-plot-example"
version = "0.0.0"
dependencies = [
"card10-l0dable 0.1.1",
"cc 1.0.37 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "rustc-demangle"
version = "0.1.16"
......
......@@ -2,6 +2,7 @@
members = [
"l0dable",
"example",
"rust-plot-example",
"rkanoid",
]
default-members = [
......
......@@ -160,9 +160,9 @@ impl<'a, S: SensorType> Iterator for SensorDataIter<'a, S> {
#[derive(Debug, Clone)]
pub struct SensorDataItem {
x: f32,
y: f32,
z: f32,
pub x: f32,
pub y: f32,
pub z: f32,
status: u8,
}
......
[target.thumbv7em-none-eabi]
rustflags = [
"-C", "linker=arm-none-eabi-gcc",
"-C", "link-args=-Tl0dable.ld -n -pie -fPIC",
"-C", "relocation-model=pic",
]
[build]
target = "thumbv7em-none-eabi"
[package]
edition = "2018"
name = "rust-plot-example"
version = "0.0.0"
authors = ["mwall <atzebadekappe@d00m.org>"]
[dependencies]
l0dable = { path = "../l0dable" }
[build-dependencies]
cc = "1.0"
use core::fmt::Write;
use l0dable::*;
pub struct LinePlot<'a> {
min: isize,
max: isize,
color: Color,
values: [u16; Display::W as usize],
disp: &'a Display,
}
impl<'a> LinePlot<'a> {
pub fn plot(&self){
for i in 0..self.values.len(){
if i > 0{
self.disp.line((i-1) as u16, self.values[i-1],i as u16,self.values[i],self.color,LineStyle::Full,1);
}
}
}
fn roll_array(&mut self, new_value: u16) {
for i in 0..self.values.len(){
if i < self.values.len() - 1{
self.values[i] = self.values[i+1];
}else{
self.values[i] = new_value;
}
};
}
fn scale(&self,v: isize) -> u16 {
if v > self.max || v < self.min{
writeln!(UART, "Warrning: Value out of range {:?}, unable to plot ..: ", v);
Display::H * 2 as u16
}else {
let k = v - self.min;
let f = k as f32 / (self.max-self.min) as f32;
Display::H - (f * Display::H as f32) as u16
}
}
pub fn update(&mut self, new_value: isize){
let scaled_value = self.scale(new_value);
self.roll_array(scaled_value);
}
pub fn new(min: isize, max: isize, color: Color, disp: &'a Display) -> Self {
let values: [u16; Display::W as usize] = [Display::H as u16; Display::W as usize];
Self { min, max, color, values, disp }
}
}
#![no_std]
#![no_main]
use core::fmt::Write;
use l0dable::*;
mod line_plot;
main!(main);
enum View{
WELCOMEVIEW,
PLOTVIEW,
INFOVIEW,
}
enum SensorType{
BME680_GAS,
BHI160_ACC,
}
const SENSOR_ORDER: [SensorType;2] = [SensorType::BME680_GAS, SensorType::BHI160_ACC];
fn plot_welcome_screen(display: &Display){
// first row
display.print(0, 0, b"BME-sensor\0", Color::white(), Color::black());
// second row
display.print(0, 20, b"visuals\0", Color::white(), Color::black());
// third row
display.print(0, 40, b"<\0", Color::red(), Color::black());
display.print(10, 40, b"Info\0", Color::white(), Color::black());
display.print(Display::W-70,40, b"exit\0", Color::white(), Color::black());
display.print(Display::W-14,40, b">\0", Color::red(), Color::black());
// fourth row
display.print(Display::W-14,60, b">\0", Color::red(), Color::black());
display.print(10, 60, b"chg sensor\0", Color::white(), Color::black());
}
fn main() {
let display = Display::open();
let bme_sensor = BME680::start();
let bhi_acc = BHI160::<Accelerometer>::start();
let mut gas_plot = line_plot::LinePlot::new(0,500000,Color::blue(),&display);
let mut acc_plot_x = line_plot::LinePlot::new(-100,100,Color::red(),&display);
let mut acc_plot_y = line_plot::LinePlot::new(-100,100,Color::black(),&display);
let mut acc_plot_z = line_plot::LinePlot::new(-100,100,Color::green(),&display,);
let mut view = View::WELCOMEVIEW;
let mut curr_sensor = &SENSOR_ORDER[0];
loop{
display.clear(Color::white());
gas_plot.update(bme_sensor.read().unwrap().gas_resistance as isize);
for d in &bhi_acc.read() {
writeln!(UART, "BHI {:?}\r", d).unwrap();
acc_plot_x.update((d.x * 100.0) as isize);
acc_plot_y.update((d.y * 100.0) as isize);
acc_plot_z.update((d.z * 100.0) as isize);
}
match view{
View::WELCOMEVIEW => {
plot_welcome_screen(&display);
}
View::PLOTVIEW => {
match curr_sensor{
SensorType::BME680_GAS => {
gas_plot.plot();
}
SensorType::BHI160_ACC => {
acc_plot_x.plot();
acc_plot_y.plot();
acc_plot_z.plot();
}
}
}
View::INFOVIEW => {
match curr_sensor{
SensorType::BME680_GAS => {
display.print(0, 0, b"BME-Sensor:\0", Color::red(), Color::black());
display.print(0, 20, b"gas pressure\0", Color::red(), Color::black());
}
SensorType::BHI160_ACC => {
display.print(0, 0, b"BHI-Sensor:\0", Color::red(), Color::black());
display.print(0, 20, b"accelerometer\0", Color::red(), Color::black());
}
}
}
}
let b = Buttons::read();
if b.left_bottom() {
view = match view{
View::WELCOMEVIEW => View::PLOTVIEW,
View::PLOTVIEW => View::INFOVIEW,
View::INFOVIEW => View::PLOTVIEW,
}
}
if b.right_top() {
exit(0);
}
if b.right_bottom() {
curr_sensor = match curr_sensor{
SensorType::BHI160_ACC => &SensorType::BME680_GAS,
SensorType::BME680_GAS => &SensorType::BHI160_ACC,
}
}
display.update();
};
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment