#data #arduino #avr #no-std #column-name

no-std udatatable

一个用于在无分配器的嵌入式no_std环境中创建数据表的库

2个版本

0.1.1 2023年11月15日
0.1.0 2023年11月15日

#491嵌入式开发

MIT/Apache

21KB
197

µDataTable

适用于Arduino等嵌入式系统的数据收集Rust库。

概述

此库允许创建具有最大行数容量的泛型类型数组。此crate旨在与在no_std环境中使用的ufmt crate一起使用。它是专门为小型微控制器(如Arduino)上的传感器数据收集而创建的。

所有数据都保存在栈上,因此不需要堆分配。在创建数据表时定义行类型的列名。数据表可以追加到编译时定义的最大行数。可以擦除数据表内容以将长度重置为零。

可以使用ufmt使用uDisplayuDebug trait显示uDataTable结构。目的是使用uDisplay trait以csv格式打印数据,使用uDebug trait打印表头和表长度。如果您的行类型不是原始类型,则必须为行类型定义uDisplayuDebug trait。

uDataTable结构还可以使用可选的plot功能进行绘图。需要提供一个函数来获取行类型的引用并返回一个i32。该方法将为数据表中的每一行绘制函数返回的值。

用法

将以下内容添加到您的Cargo.toml文件中,以使用udatatable crate。

[dependencies]
udatatable = "0.1"

功能

  • plot - 启用plot方法。将其作为一个可选功能,以便您可以在不需要plot方法时保持代码大小较小。

示例

创建一个数据表,添加行并显示内容。请注意,行类型必须实现CopyDefaultuDebuguDisplay特征。

use ufmt::{uDebug, uDisplay, uWrite, uwrite, uwriteln, Formatter};
use udatatable::uDataTable;

// Define the row type
#[derive(Copy, Clone, Default)]
struct Row {
    a: u32,
    b: u32,
    c: u32,
}

// Define the uDisplay and uDebug traits for the row type
impl uDebug for Row {
    fn fmt<W>(&self, f: &mut Formatter<'_, W>) -> Result<(), W::Error>
    where
        W: uWrite + ?Sized,
    {
        uwrite!(f, "Row {{ a: {}, b: {}, c: {} }}", self.a, self.b, self.c)
    }
}

impl uDisplay for Row {
    fn fmt<W>(&self, f: &mut Formatter<'_, W>) -> Result<(), W::Error>
    where
        W: uWrite + ?Sized,
    {
        // The uDisplay trait is meant to print the data in a csv format
        uwrite!(f, "{}, {}, {}", self.a, self.b, self.c)
    }
}

// Create the data table
const N: usize = 10;
const M: usize = 3;
let mut table = uDataTable::<Row, N, M>::new(["a", "b", "c"]);

// Append rows to the data table
for i in 0..5 {
    let row = Row {
        a: i as u32,
        b: i as u32 * 2,
        c: i as u32 * 3,
    };
    if let Err(error) = table.append(row) {
        // handle the error
    }
}

assert_eq!(table.length(), 5);
assert_eq!(*table.headers(), ["a", "b", "c"]);
assert_eq!(table.get(0).unwrap().a, 0);
assert_eq!(table.get(0).unwrap().b, 0);
assert_eq!(table.get(0).unwrap().c, 0);
assert_eq!(table.get(1).unwrap().a, 1);
assert_eq!(table.get(1).unwrap().b, 2);
assert_eq!(table.get(1).unwrap().c, 3);
assert_eq!(table.get(2).unwrap().a, 2);
assert_eq!(table.get(2).unwrap().b, 4);
assert_eq!(table.get(2).unwrap().c, 6);

// Display the data table
let mut s = String::new();
ufmt::uwrite!(&mut s, "{}", table).ok();
assert_eq!(s, "\"a\",\"b\",\"c\"\n0, 0, 0\n1, 2, 3\n2, 4, 6\n3, 6, 9\n4, 8, 12\n");

// Display the data table with uDebug
let mut s = String::new();
ufmt::uwrite!(&mut s, "{:?}", table).ok();
assert_eq!(s, "uDataTable<[\"a\", \"b\", \"c\"], length: 5>");

#[cfg(feature = "plot")]
{
    // graph the data table for value `a`
    let mut s = String::new();
    table.plot(&mut s, |row| row.a as i32);
    assert_eq!(s, "4 |    *\n  |   *.\n  |  *..\n  | *...\n0 |*....\n");
}

许可证

根据您的选择,许可协议为Apache License, Version 2.0MIT

贡献

除非您明确表示,否则根据Apache-2.0许可证定义的,您有意提交的任何贡献,都将按照上述方式双许可,不附加任何额外条款或条件。

欢迎提交pull请求。创建pull请求时,请确保包含并通过测试。要运行测试,请在存储库的根目录下运行cargo test。此外,请关注此crate的预期嵌入式用法,并保持代码体积小。

依赖

~1.5MB
~36K SLoC