Initial bindings

This commit is contained in:
marcelbuesing 2018-04-11 21:40:48 +02:00 committed by marcelbuesing
commit 6e85dee3e9
No known key found for this signature in database
GPG key ID: EF3934305E975944
11 changed files with 767 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/target
**/*.rs.bk

3
.gitmodules vendored Normal file
View file

@ -0,0 +1,3 @@
[submodule "BME680_driver"]
path = BME680_driver
url = https://github.com/BoschSensortec/BME680_driver.git

1
BME680_driver Submodule

@ -0,0 +1 @@
Subproject commit 313a58a9c57ad86c8df06c98521579c6cb695999

381
Cargo.lock generated Normal file
View file

@ -0,0 +1,381 @@
[[package]]
name = "aho-corasick"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"memchr 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "ansi_term"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "atty"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.40 (registry+https://github.com/rust-lang/crates.io-index)",
"termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "bindgen"
version = "0.35.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"cexpr 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
"cfg-if 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"clang-sys 0.22.0 (registry+https://github.com/rust-lang/crates.io-index)",
"clap 2.31.2 (registry+https://github.com/rust-lang/crates.io-index)",
"env_logger 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
"peeking_take_while 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)",
"which 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "bitflags"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "bme680-rs"
version = "0.1.0"
dependencies = [
"bindgen 0.35.0 (registry+https://github.com/rust-lang/crates.io-index)",
"gcc 0.3.54 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "cc"
version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "cexpr"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"nom 3.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "cfg-if"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "clang-sys"
version = "0.22.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.40 (registry+https://github.com/rust-lang/crates.io-index)",
"libloading 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "clap"
version = "2.31.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
"atty 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
"bitflags 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"strsim 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
"textwrap 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-width 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
"vec_map 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "env_logger"
version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"atty 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
"humantime 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)",
"termcolor 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "gcc"
version = "0.3.54"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "glob"
version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "humantime"
version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"quick-error 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "lazy_static"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "libc"
version = "0.2.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "libloading"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"cc 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "log"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"cfg-if 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "memchr"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.40 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "memchr"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.40 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "nom"
version = "3.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"memchr 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "peeking_take_while"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "quick-error"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "quote"
version = "0.3.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "redox_syscall"
version = "0.1.37"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "redox_termios"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"redox_syscall 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "regex"
version = "0.2.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"aho-corasick 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)",
"memchr 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"regex-syntax 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
"thread_local 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
"utf8-ranges 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "regex-syntax"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"ucd-util 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "strsim"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "termcolor"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"wincolor 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "termion"
version = "1.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.40 (registry+https://github.com/rust-lang/crates.io-index)",
"redox_syscall 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)",
"redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "textwrap"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"unicode-width 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "thread_local"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "ucd-util"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "unicode-width"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "unreachable"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "utf8-ranges"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "vec_map"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "void"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "which"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.40 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "winapi"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "wincolor"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
]
[metadata]
"checksum aho-corasick 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)" = "d6531d44de723825aa81398a6415283229725a00fa30713812ab9323faa82fc4"
"checksum ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"
"checksum atty 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "af80143d6f7608d746df1520709e5d141c96f240b0e62b0aa41bdfb53374d9d4"
"checksum bindgen 0.35.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b023955126e7909ab9fc1d1973965b8b004f1f388afb5c589640ab483b3b0ad2"
"checksum bitflags 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b3c30d3802dfb7281680d6285f2ccdaa8c2d8fee41f93805dba5c4cf50dc23cf"
"checksum cc 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)" = "2b4911e4bdcb4100c7680e7e854ff38e23f1b34d4d9e079efae3da2801341ffc"
"checksum cexpr 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "42aac45e9567d97474a834efdee3081b3c942b2205be932092f53354ce503d6c"
"checksum cfg-if 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d4c819a1287eb618df47cc647173c5c4c66ba19d888a6e50d605672aed3140de"
"checksum clang-sys 0.22.0 (registry+https://github.com/rust-lang/crates.io-index)" = "939a1a34310b120d26eba35c29475933128b0ec58e24b43327f8dbe6036fc538"
"checksum clap 2.31.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f0f16b89cbb9ee36d87483dc939fe9f1e13c05898d56d7b230a0d4dff033a536"
"checksum env_logger 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)" = "0561146661ae44c579e993456bc76d11ce1e0c7d745e57b2fa7146b6e49fa2ad"
"checksum gcc 0.3.54 (registry+https://github.com/rust-lang/crates.io-index)" = "5e33ec290da0d127825013597dbdfc28bee4964690c7ce1166cbc2a7bd08b1bb"
"checksum glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "8be18de09a56b60ed0edf84bc9df007e30040691af7acd1c41874faac5895bfb"
"checksum humantime 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0484fda3e7007f2a4a0d9c3a703ca38c71c54c55602ce4660c419fd32e188c9e"
"checksum lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c8f31047daa365f19be14b47c29df4f7c3b581832407daabe6ae77397619237d"
"checksum libc 0.2.40 (registry+https://github.com/rust-lang/crates.io-index)" = "6fd41f331ac7c5b8ac259b8bf82c75c0fb2e469bbf37d2becbba9a6a2221965b"
"checksum libloading 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9c3ad660d7cb8c5822cd83d10897b0f1f1526792737a179e73896152f85b88c2"
"checksum log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "89f010e843f2b1a31dbd316b3b8d443758bc634bed37aabade59c686d644e0a2"
"checksum memchr 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "148fab2e51b4f1cfc66da2a7c32981d1d3c083a803978268bb11fe4b86925e7a"
"checksum memchr 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "796fba70e76612589ed2ce7f45282f5af869e0fdd7cc6199fa1aa1f1d591ba9d"
"checksum nom 3.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "05aec50c70fd288702bcd93284a8444607f3292dbdf2a30de5ea5dcdbe72287b"
"checksum peeking_take_while 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099"
"checksum quick-error 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "eda5fe9b71976e62bc81b781206aaa076401769b2143379d3eb2118388babac4"
"checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a"
"checksum redox_syscall 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)" = "0d92eecebad22b767915e4d529f89f28ee96dbbf5a4810d2b844373f136417fd"
"checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76"
"checksum regex 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)" = "aec3f58d903a7d2a9dc2bf0e41a746f4530e0cab6b615494e058f67a3ef947fb"
"checksum regex-syntax 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "b2550876c31dc914696a6c2e01cbce8afba79a93c8ae979d2fe051c0230b3756"
"checksum strsim 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bb4f380125926a99e52bc279241539c018323fab05ad6368b56f93d9369ff550"
"checksum termcolor 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "adc4587ead41bf016f11af03e55a624c06568b5a19db4e90fde573d805074f83"
"checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096"
"checksum textwrap 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c0b59b6b4b44d867f1370ef1bd91bfb262bf07bf0ae65c202ea2fbc16153b693"
"checksum thread_local 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "279ef31c19ededf577bfd12dfae728040a21f635b06a24cd670ff510edd38963"
"checksum ucd-util 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "fd2be2d6639d0f8fe6cdda291ad456e23629558d466e2789d2c3e9892bda285d"
"checksum unicode-width 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "bf3a113775714a22dcb774d8ea3655c53a32debae63a063acc00a91cc586245f"
"checksum unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56"
"checksum utf8-ranges 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "662fab6525a98beff2921d7f61a39e7d59e0b425ebc7d0d9e66d316e55124122"
"checksum vec_map 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "887b5b631c2ad01628bbbaa7dd4c869f80d3186688f8d0b6f58774fbe324988c"
"checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d"
"checksum which 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "e84a603e7e0b1ce1aa1ee2b109c7be00155ce52df5081590d1ffb93f4f515cb2"
"checksum winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "04e3bd221fcbe8a271359c04f21a76db7d0c6028862d1bb5512d85e1e2eb5bb3"
"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
"checksum wincolor 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "eeb06499a3a4d44302791052df005d5232b927ed1a9658146d842165c4de7767"

11
Cargo.toml Normal file
View file

@ -0,0 +1,11 @@
[package]
name = "bme680-rs"
version = "0.1.0"
build = "build.rs"
authors = ["marcelbuesing <buesing.marcel@googlemail.com>"]
[dependencies]
[build-dependencies]
bindgen = "0.35.0"
gcc = "0.3.54"

1
README.md Normal file
View file

@ -0,0 +1 @@
WIP Rust bindings for bme680

34
build.rs Normal file
View file

@ -0,0 +1,34 @@
extern crate bindgen;
extern crate gcc;
use std::env;
use std::path::PathBuf;
fn main() {
// Tell cargo to tell rustc to link the system bzip2
// shared library.
//println!("cargo:rustc-link-lib=bz2");
gcc::Config::new()
.file("BME680_driver/bme680.c")
.include("BME680_driver")
.compile("bme680.a");
// The bindgen::Builder is the main entry point
// to bindgen, and lets you build up options for
// the resulting bindings.
let bindings = bindgen::Builder::default()
// The input header we would like to generate
// bindings for.
.header("wrapper.h")
// Finish the builder and generate the bindings.
.generate()
// Unwrap the Result and panic on failure.
.expect("Unable to generate bindings");
// Write the bindings to the $OUT_DIR/bindings.rs file.
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");
}

23
src/consts.rs Normal file
View file

@ -0,0 +1,23 @@
pub const BME680_OK: i8 = 0;
pub const BME680_E_NULL_PTR: i8 = -1;
pub const BME680_E_COM_FAIL: i8 = -2;
pub const BME680_E_DEV_NOT_FOUND: i8 = -3;
pub const BME680_E_INVALID_LENGTH: i8 = -4;
pub const BME680_I2C_ADDR_PRIMARY: u8 = 0x76;
pub const BME680_I2C_ADDR_SECONDARY: u8 = 0x77;
pub const BME680_CHIP_ID: u8 = 0x61;
pub const BME680_SLEEP_MODE: u8 = 0;
pub const BME680_FORCED_MODE: u8 = 1;
///
/// SPI memory page settings
///
pub const BME680_MEM_PAGE0: u8 = 0x10;
///
/// SPI memory page settings
///
pub const BME680_MEM_PAGE1: u8 = 0x00;

232
src/device_builder.rs Normal file
View file

@ -0,0 +1,232 @@
use consts;
use {to_result, Result, bme680_calib_data, bme680_dev, bme680_gas_sett, bme680_init, bme680_intf,
bme680_intf_BME680_I2C_INTF, bme680_tph_sett};
pub struct Bme680Device {
dev: bme680_dev,
}
impl Bme680Device {
/// @brief This API is the entry point.
/// It reads the chip-id and calibration data from the sensor.
///
/// @param[in,out] dev : Structure instance of bme680_dev
///
/// @return Result of API execution status
pub fn init(mut self) -> Result<()> {
let dev_ptr: *mut bme680_dev = &mut self.dev;
unsafe { to_result(bme680_init(dev_ptr)) }
}
}
impl Default for bme680_calib_data {
fn default() -> bme680_calib_data {
bme680_calib_data {
/// Variable to store calibrated humidity data
par_h1: Default::default(),
/// Variable to store calibrated humidity data
par_h2: Default::default(),
/// Variable to store calibrated humidity data
par_h3: Default::default(),
/// Variable to store calibrated humidity data
par_h4: Default::default(),
/// Variable to store calibrated humidity data
par_h5: Default::default(),
/// Variable to store calibrated humidity data
par_h6: Default::default(),
/// Variable to store calibrated humidity data
par_h7: Default::default(),
/// Variable to store calibrated gas data
par_gh1: Default::default(),
/// Variable to store calibrated gas data
par_gh2: Default::default(),
/// Variable to store calibrated gas data
par_gh3: Default::default(),
/// Variable to store calibrated temperature data
par_t1: Default::default(),
/// Variable to store calibrated temperature data
par_t2: Default::default(),
/// Variable to store calibrated temperature data
par_t3: Default::default(),
/// Variable to store calibrated pressure data
par_p1: Default::default(),
/// Variable to store calibrated pressure data
par_p2: Default::default(),
/// Variable to store calibrated pressure data
par_p3: Default::default(),
/// Variable to store calibrated pressure data
par_p4: Default::default(),
/// Variable to store calibrated pressure data
par_p5: Default::default(),
/// Variable to store calibrated pressure data
par_p6: Default::default(),
/// Variable to store calibrated pressure data
par_p7: Default::default(),
/// Variable to store calibrated pressure data
par_p8: Default::default(),
/// Variable to store calibrated pressure data
par_p9: Default::default(),
/// Variable to store calibrated pressure data
par_p10: Default::default(),
/// Variable to store t_fine size
t_fine: Default::default(),
/// Variable to store heater resistance range
res_heat_range: Default::default(),
/// Variable to store heater resistance value
res_heat_val: Default::default(),
/// Variable to store error range
range_sw_err: Default::default(),
}
}
}
impl Default for bme680_gas_sett {
fn default() -> bme680_gas_sett {
bme680_gas_sett {
/// Variable to store nb conversion
nb_conv: Default::default(),
/// Variable to store heater control
heatr_ctrl: Default::default(),
/// Run gas enable value
run_gas: Default::default(),
/// Heater temperature value
heatr_temp: Default::default(),
/// Duration profile value
heatr_dur: Default::default(),
}
}
}
impl Default for bme680_tph_sett {
fn default() -> bme680_tph_sett {
bme680_tph_sett {
/// Humidity oversampling
os_hum: Default::default(),
/// Temperature oversampling
os_temp: Default::default(),
/// Pressure oversampling
os_pres: Default::default(),
/// Filter coefficient
filter: Default::default(),
}
}
}
pub struct Bme680DeviceBuilder {
dev: bme680_dev,
}
/// Generic communication function pointer
/// @param[in] dev_id: Place holder to store the id of the device structure
/// Can be used to store the index of the Chip select or
/// I2C address of the device.
/// @param[in] reg_addr: Used to select the register the where data needs to
/// be read from or written to.
/// @param[in/out] reg_data: Data array to read/write
/// @param[in] len: Length of the data array
type ComFn = extern "C" fn(dev_id: u8, reg_addr: u8, data: *mut u8, len: u16) -> i8;
type DelayFn = extern "C" fn(period: u32);
impl Bme680DeviceBuilder {
pub fn new(read: ComFn, write: ComFn, delay_ms: DelayFn) -> Self {
let dev = bme680_dev {
/// Chip Id
chip_id: consts::BME680_CHIP_ID,
/// Device Id
dev_id: consts::BME680_I2C_ADDR_PRIMARY,
/// SPI/I2C interface
intf: bme680_intf_BME680_I2C_INTF,
/// Memory page used
mem_page: Default::default(),
/// Ambient temperature in Degree C
amb_temp: Default::default(),
/// Sensor calibration data
calib: Default::default(),
/// Sensor settings
tph_sett: Default::default(),
/// Gas Sensor settings
gas_sett: Default::default(),
/// Sensor power modes
power_mode: consts::BME680_SLEEP_MODE,
/// New sensor fields
new_fields: Default::default(),
/// Store the info messages
info_msg: Default::default(),
/// Bus read function pointer
read: Some(read),
/// Bus write function pointer
write: Some(write),
/// delay function pointer
delay_ms: Some(delay_ms),
/// Communication function result
com_rslt: Default::default(),
};
Self { dev: dev }
}
pub fn with_chip_id(mut self, chip_id: u8) -> Self {
self.dev.chip_id = chip_id;
self
}
pub fn with_dev_id(mut self, dev_id: u8) -> Self {
self.dev.dev_id = dev_id;
self
}
pub fn with_intf(mut self, intf: bme680_intf) -> Self {
self.dev.intf = intf;
self
}
pub fn with_mem_page(mut self, mem_page: u8) -> Self {
self.dev.mem_page = mem_page;
self
}
pub fn with_amb_temp(mut self, amb_temp: i8) -> Self {
self.dev.amb_temp = amb_temp;
self
}
pub fn with_calib(mut self, calib: bme680_calib_data) -> Self {
self.dev.calib = calib;
self
}
pub fn with_tph_sett(mut self, tph_sett: bme680_tph_sett) -> Self {
self.dev.tph_sett = tph_sett;
self
}
pub fn with_gas_sett(mut self, gas_sett: bme680_gas_sett) -> Self {
self.dev.gas_sett = gas_sett;
self
}
pub fn with_power_mode(mut self, power_mode: u8) -> Self {
self.dev.power_mode = power_mode;
self
}
pub fn with_new_fields(mut self, new_fields: u8) -> Self {
self.dev.new_fields = new_fields;
self
}
pub fn with_info_msg(mut self, info_msg: u8) -> Self {
self.dev.info_msg = info_msg;
self
}
pub fn with_com_rslt(mut self, com_rslt: i8) -> Self {
self.dev.com_rslt = com_rslt;
self
}
pub fn build(self) -> Bme680Device {
Bme680Device { dev: self.dev }
}
}

77
src/lib.rs Normal file
View file

@ -0,0 +1,77 @@
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
use std::default::Default;
use std::result;
#[link(name = "example", kind = "static")]
mod consts;
mod device_builder;
#[cfg(test)]
mod tests {
use device_builder::Bme680DeviceBuilder;
extern "C" fn com(dev_id: u8, reg_addr: u8, data: *mut u8, len: u16) -> i8 {
0
}
extern "C" fn delay(period: u32) {
println!("delay {}", period);
}
#[test]
fn init() {
let dev = Bme680DeviceBuilder::new(com, com, delay).build();
dev.init();
}
}
pub type Result<T> = result::Result<T, Bme680Error>;
pub enum Bme680Error {
///
/// aka BME680_E_NULL_PTR
///
NulltPtr,
///
/// aka BME680_E_COM_FAIL
///
CommunicationFailure,
///
/// aka BME680_E_DEV_NOT_FOUND
///
DeviceNotFound,
///
/// aka BME680_E_INVALID_LENGTH
///
InvalidLength,
}
fn to_result(res: i8) -> Result<()> {
match res {
consts::BME680_OK => Ok(()),
consts::BME680_E_NULL_PTR => Err(Bme680Error::NulltPtr),
consts::BME680_E_COM_FAIL => Err(Bme680Error::CommunicationFailure),
consts::BME680_E_DEV_NOT_FOUND => Err(Bme680Error::DeviceNotFound),
consts::BME680_E_INVALID_LENGTH => Err(Bme680Error::InvalidLength),
_ => panic!("Invalid bme680 result: {}", res),
}
}
pub enum I2CAddr {
Primary,
Secondary,
}
///
/// Power mode settings
///
pub enum PowerMode {
SleepMode,
ForcedMode,
}

2
wrapper.h Normal file
View file

@ -0,0 +1,2 @@
#include "BME680_driver/bme680_defs.h"
#include "BME680_driver/bme680.h"