initial commit
This commit is contained in:
commit
2c8a6d2bbf
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
/target
|
379
Cargo.lock
generated
Normal file
379
Cargo.lock
generated
Normal file
@ -0,0 +1,379 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "aho-corasick"
|
||||||
|
version = "0.7.18"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f"
|
||||||
|
dependencies = [
|
||||||
|
"memchr",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ansi_term"
|
||||||
|
version = "0.12.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2"
|
||||||
|
dependencies = [
|
||||||
|
"winapi",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "atty"
|
||||||
|
version = "0.2.14"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
|
||||||
|
dependencies = [
|
||||||
|
"hermit-abi",
|
||||||
|
"libc",
|
||||||
|
"winapi",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "bindgen"
|
||||||
|
version = "0.59.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "2bd2a9a458e8f4304c52c43ebb0cfbd520289f8379a52e329a38afda99bf8eb8"
|
||||||
|
dependencies = [
|
||||||
|
"bitflags",
|
||||||
|
"cexpr",
|
||||||
|
"clang-sys",
|
||||||
|
"clap",
|
||||||
|
"env_logger",
|
||||||
|
"lazy_static",
|
||||||
|
"lazycell",
|
||||||
|
"log",
|
||||||
|
"peeking_take_while",
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"regex",
|
||||||
|
"rustc-hash",
|
||||||
|
"shlex",
|
||||||
|
"which",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "bitflags"
|
||||||
|
version = "1.3.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "cc"
|
||||||
|
version = "1.0.72"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "22a9137b95ea06864e018375b72adfb7db6e6f68cfc8df5a04d00288050485ee"
|
||||||
|
dependencies = [
|
||||||
|
"jobserver",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "cexpr"
|
||||||
|
version = "0.6.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766"
|
||||||
|
dependencies = [
|
||||||
|
"nom",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "cfg-if"
|
||||||
|
version = "1.0.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "clang-sys"
|
||||||
|
version = "1.3.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "fa66045b9cb23c2e9c1520732030608b02ee07e5cfaa5a521ec15ded7fa24c90"
|
||||||
|
dependencies = [
|
||||||
|
"glob",
|
||||||
|
"libc",
|
||||||
|
"libloading",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "clap"
|
||||||
|
version = "2.34.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c"
|
||||||
|
dependencies = [
|
||||||
|
"ansi_term",
|
||||||
|
"atty",
|
||||||
|
"bitflags",
|
||||||
|
"strsim",
|
||||||
|
"textwrap",
|
||||||
|
"unicode-width",
|
||||||
|
"vec_map",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "either"
|
||||||
|
version = "1.6.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "env_logger"
|
||||||
|
version = "0.9.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0b2cf0344971ee6c64c31be0d530793fba457d322dfec2810c453d0ef228f9c3"
|
||||||
|
dependencies = [
|
||||||
|
"atty",
|
||||||
|
"humantime",
|
||||||
|
"log",
|
||||||
|
"regex",
|
||||||
|
"termcolor",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "glob"
|
||||||
|
version = "0.3.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "hermit-abi"
|
||||||
|
version = "0.1.19"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "humantime"
|
||||||
|
version = "2.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "jobserver"
|
||||||
|
version = "0.1.24"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "af25a77299a7f711a01975c35a6a424eb6862092cc2d6c72c4ed6cbc56dfc1fa"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "lazy_static"
|
||||||
|
version = "1.4.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "lazycell"
|
||||||
|
version = "1.3.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "libc"
|
||||||
|
version = "0.2.112"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1b03d17f364a3a042d5e5d46b053bbbf82c92c9430c592dd4c064dc6ee997125"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "libloading"
|
||||||
|
version = "0.7.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "afe203d669ec979b7128619bae5a63b7b42e9203c1b29146079ee05e2f604b52"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
"winapi",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "log"
|
||||||
|
version = "0.4.14"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "memchr"
|
||||||
|
version = "2.4.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "minimal-lexical"
|
||||||
|
version = "0.2.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "nom"
|
||||||
|
version = "7.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1b1d11e1ef389c76fe5b81bcaf2ea32cf88b62bc494e19f493d0b30e7a930109"
|
||||||
|
dependencies = [
|
||||||
|
"memchr",
|
||||||
|
"minimal-lexical",
|
||||||
|
"version_check",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "peeking_take_while"
|
||||||
|
version = "0.1.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "pkg-config"
|
||||||
|
version = "0.3.24"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "58893f751c9b0412871a09abd62ecd2a00298c6c83befa223ef98c52aef40cbe"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "proc-macro2"
|
||||||
|
version = "1.0.36"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "c7342d5883fbccae1cc37a2353b09c87c9b0f3afd73f5fb9bba687a1f733b029"
|
||||||
|
dependencies = [
|
||||||
|
"unicode-xid",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "quote"
|
||||||
|
version = "1.0.14"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "47aa80447ce4daf1717500037052af176af5d38cc3e571d9ec1c7353fc10c87d"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "regex"
|
||||||
|
version = "1.5.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461"
|
||||||
|
dependencies = [
|
||||||
|
"aho-corasick",
|
||||||
|
"memchr",
|
||||||
|
"regex-syntax",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "regex-syntax"
|
||||||
|
version = "0.6.25"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rustc-hash"
|
||||||
|
version = "1.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "sensor"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"bindgen",
|
||||||
|
"cc",
|
||||||
|
"libc",
|
||||||
|
"pkg-config",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "shlex"
|
||||||
|
version = "1.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "43b2853a4d09f215c24cc5489c992ce46052d359b5109343cbafbf26bc62f8a3"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "strsim"
|
||||||
|
version = "0.8.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "termcolor"
|
||||||
|
version = "1.1.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4"
|
||||||
|
dependencies = [
|
||||||
|
"winapi-util",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "textwrap"
|
||||||
|
version = "0.11.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
|
||||||
|
dependencies = [
|
||||||
|
"unicode-width",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "unicode-width"
|
||||||
|
version = "0.1.9"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "unicode-xid"
|
||||||
|
version = "0.2.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "vec_map"
|
||||||
|
version = "0.8.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "version_check"
|
||||||
|
version = "0.9.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "which"
|
||||||
|
version = "4.2.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ea187a8ef279bc014ec368c27a920da2024d2a711109bfbe3440585d5cf27ad9"
|
||||||
|
dependencies = [
|
||||||
|
"either",
|
||||||
|
"lazy_static",
|
||||||
|
"libc",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "winapi"
|
||||||
|
version = "0.3.9"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
|
||||||
|
dependencies = [
|
||||||
|
"winapi-i686-pc-windows-gnu",
|
||||||
|
"winapi-x86_64-pc-windows-gnu",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "winapi-i686-pc-windows-gnu"
|
||||||
|
version = "0.4.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "winapi-util"
|
||||||
|
version = "0.1.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
|
||||||
|
dependencies = [
|
||||||
|
"winapi",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "winapi-x86_64-pc-windows-gnu"
|
||||||
|
version = "0.4.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
13
Cargo.toml
Normal file
13
Cargo.toml
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
[package]
|
||||||
|
name = "sensor"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
build = "build.rs"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
libc = "0.2"
|
||||||
|
|
||||||
|
[build-dependencies]
|
||||||
|
cc = { version = "1.0", features = ["parallel"] }
|
||||||
|
bindgen = "*"
|
||||||
|
pkg-config = "0.3"
|
38
build.rs
Normal file
38
build.rs
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
extern crate bindgen;
|
||||||
|
use cc;
|
||||||
|
|
||||||
|
use std::path::PathBuf;
|
||||||
|
use std::env;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
println!("cargo:rerun-if-changed=src/pcsensor.c");
|
||||||
|
bind();
|
||||||
|
compile_pcsensor();
|
||||||
|
link();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn bind() {
|
||||||
|
let bindings = bindgen::Builder::default()
|
||||||
|
.header("/usr/include/libusb-1.0/libusb.h")
|
||||||
|
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
|
||||||
|
.generate()
|
||||||
|
.expect("Unable to generate bindings");
|
||||||
|
|
||||||
|
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
|
||||||
|
bindings
|
||||||
|
.write_to_file(out_path.join("bindings.rs"))
|
||||||
|
.expect("Couldn't write bindings!");
|
||||||
|
}
|
||||||
|
|
||||||
|
fn compile_pcsensor() {
|
||||||
|
cc::Build::new()
|
||||||
|
.file("src/pcsensor.c")
|
||||||
|
.shared_flag(true)
|
||||||
|
.static_flag(true)
|
||||||
|
.compile("pcsensor");
|
||||||
|
}
|
||||||
|
|
||||||
|
fn link() {
|
||||||
|
println!("cargo:rustc-link-lib=pcsensor");
|
||||||
|
println!("cargo:rustc-link-lib=usb");
|
||||||
|
}
|
5
src/lib.rs
Normal file
5
src/lib.rs
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
#![allow(non_upper_case_globals)]
|
||||||
|
#![allow(non_camel_case_types)]
|
||||||
|
#![allow(non_snake_case)]
|
||||||
|
|
||||||
|
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
|
13
src/main.rs
Normal file
13
src/main.rs
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
use libc;
|
||||||
|
|
||||||
|
#[link(name = "pcsensor")]
|
||||||
|
#[link(name = "usb-1.0")]
|
||||||
|
extern "C" {
|
||||||
|
fn get_temp_c() -> libc::c_float;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
unsafe {
|
||||||
|
println!("{}", get_temp_c());
|
||||||
|
}
|
||||||
|
}
|
474
src/pcsensor.c
Normal file
474
src/pcsensor.c
Normal file
@ -0,0 +1,474 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <strings.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <signal.h>
|
||||||
|
#include <libusb-1.0/libusb.h>
|
||||||
|
|
||||||
|
#define VERSION "1.2.0"
|
||||||
|
|
||||||
|
/* TEMPer type definition */
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
const int vendor_id;
|
||||||
|
const int product_id;
|
||||||
|
const char product_name[256];
|
||||||
|
const int check_product_name; // if set, check product name in forward match
|
||||||
|
const int has_sensor; // number of temperature sensor
|
||||||
|
const int has_humid; // flag for humidity sensor
|
||||||
|
void (*decode_func)();
|
||||||
|
} temper_type_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
libusb_device_handle *handle;
|
||||||
|
temper_type_t *type;
|
||||||
|
} temper_device_t;
|
||||||
|
|
||||||
|
void decode_answer_fm75();
|
||||||
|
void decode_answer_sht1x();
|
||||||
|
|
||||||
|
#define TEMPER_TYPES 3
|
||||||
|
|
||||||
|
temper_type_t tempers[TEMPER_TYPES] = {
|
||||||
|
{ 0x0c45, 0x7401, "TEMPer2", 1, 2, 0, decode_answer_fm75 }, // TEMPer2* eg. TEMPer2V1.3
|
||||||
|
{ 0x0c45, 0x7401, "TEMPer1", 0, 1, 0, decode_answer_fm75 }, // other 0c45:7401 eg. TEMPerV1.4
|
||||||
|
{ 0x0c45, 0x7402, "TEMPerHUM", 0, 1, 1, decode_answer_sht1x },
|
||||||
|
};
|
||||||
|
|
||||||
|
/* memo: TEMPer2 cannot be distinguished with VID:PID,
|
||||||
|
thus product name (like TEMPer2V1.3) should be checked. */
|
||||||
|
|
||||||
|
/* global variables */
|
||||||
|
|
||||||
|
#define MAX_DEV 8
|
||||||
|
|
||||||
|
#define INTERFACE1 0x00
|
||||||
|
#define INTERFACE2 0x01
|
||||||
|
|
||||||
|
const int reqIntLen=8;
|
||||||
|
const int endpoint_Int_in=0x82; /* endpoint 0x81 address for IN */
|
||||||
|
const int timeout=5000; /* timeout in ms */
|
||||||
|
|
||||||
|
const char uTemperature[] = { 0x01, 0x80, 0x33, 0x01, 0x00, 0x00, 0x00, 0x00 };
|
||||||
|
//const static char uIni1[] = { 0x01, 0x82, 0x77, 0x01, 0x00, 0x00, 0x00, 0x00 };
|
||||||
|
//const static char uIni2[] = { 0x01, 0x86, 0xff, 0x01, 0x00, 0x00, 0x00, 0x00 };
|
||||||
|
|
||||||
|
static int bsalir=1;
|
||||||
|
static int debug=0;
|
||||||
|
static int seconds=5;
|
||||||
|
static int formato=1; //Celsius
|
||||||
|
|
||||||
|
static libusb_context *ctx = NULL;
|
||||||
|
|
||||||
|
/* functions */
|
||||||
|
|
||||||
|
void bad(const char *why) {
|
||||||
|
fprintf(stderr,"Fatal error> %s\n",why);
|
||||||
|
exit(17);
|
||||||
|
}
|
||||||
|
|
||||||
|
void usb_detach(libusb_device_handle *lvr_winusb, int iInterface) {
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
ret = libusb_detach_kernel_driver(lvr_winusb, iInterface);
|
||||||
|
if(ret) {
|
||||||
|
if(errno == ENODATA) {
|
||||||
|
if(debug) {
|
||||||
|
fprintf(stderr, "Device already detached\n");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if(debug) {
|
||||||
|
fprintf(stderr, "Detach failed: %s[%d]\n", strerror(errno), errno);
|
||||||
|
fprintf(stderr, "Continuing anyway\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if(debug) {
|
||||||
|
fprintf(stderr, "detach successful\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int find_lvr_winusb(temper_device_t *devices) {
|
||||||
|
int i, j, s, cnt, numdev;
|
||||||
|
libusb_device **devs;
|
||||||
|
|
||||||
|
//handle = libusb_open_device_with_vid_pid(ctx, VENDOR_ID, PRODUCT_ID);
|
||||||
|
|
||||||
|
cnt = libusb_get_device_list(ctx, &devs);
|
||||||
|
if (cnt < 1) {
|
||||||
|
fprintf(stderr, "Could not find USB device: %d\n", cnt);
|
||||||
|
}
|
||||||
|
|
||||||
|
numdev = 0;
|
||||||
|
for (i = 0; i < cnt && numdev < MAX_DEV; i++) {
|
||||||
|
struct libusb_device_descriptor desc;
|
||||||
|
|
||||||
|
if ((s = libusb_get_device_descriptor(devs[i], &desc)) < 0) {
|
||||||
|
fprintf(stderr, "Could not get USB device descriptor: %d\n", s);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (j = 0; j < TEMPER_TYPES; j++) {
|
||||||
|
if (desc.idVendor == tempers[j].vendor_id && desc.idProduct == tempers[j].product_id) {
|
||||||
|
unsigned char bus, addr, descmanu[256], descprod[256], descseri[256];
|
||||||
|
|
||||||
|
bus = libusb_get_bus_number(devs[i]);
|
||||||
|
addr = libusb_get_device_address(devs[i]);
|
||||||
|
|
||||||
|
if ((s = libusb_open(devs[i], &devices[numdev].handle)) < 0) {
|
||||||
|
fprintf(stderr, "Could not open USB device: %d\n", s);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
libusb_get_string_descriptor_ascii(devices[numdev].handle, desc.iManufacturer, descmanu, 256);
|
||||||
|
libusb_get_string_descriptor_ascii(devices[numdev].handle, desc.iProduct, descprod, 256);
|
||||||
|
libusb_get_string_descriptor_ascii(devices[numdev].handle, desc.iSerialNumber, descseri, 256);
|
||||||
|
|
||||||
|
if (tempers[j].check_product_name) {
|
||||||
|
if (strncmp((const char*)descprod, tempers[j].product_name, strlen(tempers[j].product_name)) == 0) {
|
||||||
|
devices[numdev].type = &tempers[j];
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// vid and pid match, but product name unmatch
|
||||||
|
libusb_close(devices[numdev].handle);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
devices[numdev].type = &tempers[j];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (debug) {
|
||||||
|
fprintf(stderr, "lvr_winusb with Bus:%03d Addr:%03d VendorID:%04x ProductID:%04x Manufacturer:%s Product:%s Serial:%s found.\n",
|
||||||
|
bus, addr, desc.idVendor, desc.idProduct, descmanu, descprod, descseri);
|
||||||
|
}
|
||||||
|
|
||||||
|
numdev++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
libusb_free_device_list(devs, 1);
|
||||||
|
|
||||||
|
return numdev;
|
||||||
|
}
|
||||||
|
|
||||||
|
int setup_libusb_access(temper_device_t *devices) {
|
||||||
|
int i;
|
||||||
|
int log_level = 0;
|
||||||
|
int numdev;
|
||||||
|
|
||||||
|
libusb_init(&ctx);
|
||||||
|
|
||||||
|
if(debug) {
|
||||||
|
log_level = 4;
|
||||||
|
}
|
||||||
|
#if LIBUSBX_API_VERSION < 0x01000106
|
||||||
|
libusb_set_debug(ctx, log_level);
|
||||||
|
#else
|
||||||
|
libusb_set_option(ctx, LIBUSB_OPTION_LOG_LEVEL, log_level);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if((numdev = find_lvr_winusb(devices)) < 1) {
|
||||||
|
fprintf(stderr, "Couldn't find the USB device, Exiting: %d\n", numdev);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i = 0; i < numdev; i++) {
|
||||||
|
usb_detach(devices[i].handle, INTERFACE1);
|
||||||
|
usb_detach(devices[i].handle, INTERFACE2);
|
||||||
|
libusb_reset_device(devices[i].handle);
|
||||||
|
|
||||||
|
if (libusb_set_configuration(devices[i].handle, 0x01) < 0) {
|
||||||
|
fprintf(stderr, "Could not set configuration 1\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int s;
|
||||||
|
if ((s = libusb_claim_interface(devices[i].handle, INTERFACE1)) < 0) {
|
||||||
|
fprintf(stderr, "Could not claim interface. Error:%d\n", s);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((s = libusb_claim_interface(devices[i].handle, INTERFACE2)) < 0) {
|
||||||
|
fprintf(stderr, "Could not claim interface. Error:%d\n", s);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return numdev;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ini_control_transfer(libusb_device_handle *dev) {
|
||||||
|
int r,i;
|
||||||
|
|
||||||
|
char question[] = { 0x01,0x01 };
|
||||||
|
|
||||||
|
r = libusb_control_transfer(dev, 0x21, 0x09, 0x0201, 0x00, (unsigned char *) question, 2, timeout);
|
||||||
|
if(r < 0) {
|
||||||
|
perror("USB control write"); bad("USB write failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
if(debug) {
|
||||||
|
for (i=0;i<reqIntLen; i++) fprintf(stderr, "%02x ",question[i] & 0xFF);
|
||||||
|
fprintf(stderr, "\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void control_transfer(libusb_device_handle *dev, const char *pquestion) {
|
||||||
|
int r,i;
|
||||||
|
|
||||||
|
char question[reqIntLen];
|
||||||
|
|
||||||
|
memcpy(question, pquestion, sizeof question);
|
||||||
|
|
||||||
|
r = libusb_control_transfer(dev, 0x21, 0x09, 0x0200, 0x01, (unsigned char *) question, reqIntLen, timeout);
|
||||||
|
if(r < 0) {
|
||||||
|
perror("USB control write"); bad("USB write failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
if(debug) {
|
||||||
|
for (i=0;i<reqIntLen; i++) fprintf(stderr, "%02x ",question[i] & 0xFF);
|
||||||
|
fprintf(stderr, "\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void interrupt_read(libusb_device_handle *dev, unsigned char *answer) {
|
||||||
|
int r,s,i;
|
||||||
|
memset(answer, 0, reqIntLen);
|
||||||
|
|
||||||
|
s = libusb_interrupt_transfer(dev, endpoint_Int_in, answer, reqIntLen, &r, timeout);
|
||||||
|
if(r != reqIntLen) {
|
||||||
|
fprintf(stderr, "USB read failed: %d\n", s);
|
||||||
|
perror("USB interrupt read"); bad("USB read failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
if(debug) {
|
||||||
|
for (i=0;i<reqIntLen; i++) fprintf(stderr, "%02x ",answer[i] & 0xFF);
|
||||||
|
|
||||||
|
fprintf(stderr, "\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void cleanup_usb_devices(temper_device_t *devices, int numdev) {
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for (i = 0; i < numdev; i++) {
|
||||||
|
libusb_release_interface(devices[i].handle, INTERFACE1);
|
||||||
|
libusb_release_interface(devices[i].handle, INTERFACE2);
|
||||||
|
|
||||||
|
libusb_close(devices[i].handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
libusb_exit(ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ex_program() {
|
||||||
|
bsalir=1;
|
||||||
|
|
||||||
|
(void) signal(SIGINT, SIG_DFL);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* decode funcs */
|
||||||
|
/* Thanks to https://github.com/edorfaus/TEMPered */
|
||||||
|
|
||||||
|
void decode_answer_fm75(unsigned char *answer, float *tempd, float *calibration) {
|
||||||
|
int buf;
|
||||||
|
|
||||||
|
// temp C internal
|
||||||
|
buf = ((signed char)answer[2] << 8) + (answer[3] & 0xFF);
|
||||||
|
tempd[0] = buf * (125.0 / 32000.0);
|
||||||
|
tempd[0] = tempd[0] * calibration[0] + calibration[1];
|
||||||
|
|
||||||
|
// temp C external
|
||||||
|
buf = ((signed char)answer[4] << 8) + (answer[5] & 0xFF);
|
||||||
|
tempd[1] = buf * (125.0 / 32000.0);
|
||||||
|
tempd[1] = tempd[1] * calibration[0] + calibration[1];
|
||||||
|
};
|
||||||
|
|
||||||
|
void decode_answer_sht1x(unsigned char *answer, float *tempd, float *calibration){
|
||||||
|
int buf;
|
||||||
|
|
||||||
|
// temp C
|
||||||
|
buf = ((signed char)answer[2] << 8) + (answer[3] & 0xFF);
|
||||||
|
tempd[0] = -39.7 + 0.01 * buf;
|
||||||
|
tempd[0] = tempd[0] * calibration[0] + calibration[1];
|
||||||
|
|
||||||
|
// relative humidity
|
||||||
|
buf = ((signed char)answer[4] << 8) + (answer[5] & 0xFF);
|
||||||
|
tempd[1] = -2.0468 + 0.0367 * buf - 1.5955e-6 * buf * buf;
|
||||||
|
tempd[1] = ( tempd[0] - 25 ) * ( 0.01 + 0.00008 * buf ) + tempd[1];
|
||||||
|
if (tempd[1] < 0) tempd[1] = 0;
|
||||||
|
if (tempd[1] > 99) tempd[1] = 100;
|
||||||
|
};
|
||||||
|
|
||||||
|
int run(int argc, char **argv) {
|
||||||
|
temper_device_t *devices;
|
||||||
|
int numdev,i;
|
||||||
|
unsigned char *answer;
|
||||||
|
float tempd[2];
|
||||||
|
float calibration[2] = { 1, 0 }; //scale, offset
|
||||||
|
char strdate[20];
|
||||||
|
int c;
|
||||||
|
struct tm *local;
|
||||||
|
time_t t;
|
||||||
|
|
||||||
|
while ((c = getopt(argc, argv, "vcfl::a:h")) != -1)
|
||||||
|
switch (c)
|
||||||
|
{
|
||||||
|
case 'v':
|
||||||
|
debug = 1;
|
||||||
|
break;
|
||||||
|
case 'c':
|
||||||
|
formato=1; //Celsius
|
||||||
|
break;
|
||||||
|
case 'f':
|
||||||
|
formato=2; //Fahrenheit
|
||||||
|
break;
|
||||||
|
case 'l':
|
||||||
|
if (optarg!=NULL){
|
||||||
|
if (!(sscanf(optarg,"%i",&seconds) == 1)) {
|
||||||
|
fprintf (stderr, "Error: '%s' is not numeric.\n", optarg);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
} else {
|
||||||
|
bsalir = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
bsalir = 0;
|
||||||
|
seconds = 5;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'a':
|
||||||
|
if (!(sscanf(optarg,"%f:%f", &calibration[0], &calibration[1]) == 2)) {
|
||||||
|
fprintf (stderr, "Error: '%s' is not numeric.\n", optarg);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case '?':
|
||||||
|
case 'h':
|
||||||
|
printf("pcsensor version %s\n",VERSION);
|
||||||
|
printf(" Available options:\n");
|
||||||
|
printf(" -h help\n");
|
||||||
|
printf(" -v verbose\n");
|
||||||
|
printf(" -l[n] loop every 'n' seconds, default value is 5s\n");
|
||||||
|
printf(" -a scale:offset set values for calibration TempC*scale+offset eg. 1.02:-0.55 \n");
|
||||||
|
printf(" -c output in Celsius (default)\n");
|
||||||
|
printf(" -f output in Fahrenheit\n");
|
||||||
|
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
default:
|
||||||
|
if (isprint (optopt))
|
||||||
|
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
|
||||||
|
else
|
||||||
|
fprintf (stderr, "Unknown option character `\\x%x'.\n", optopt);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (optind < argc) {
|
||||||
|
fprintf(stderr, "Non-option ARGV-elements, try -h for help.\n");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
devices = calloc(MAX_DEV, sizeof(temper_device_t));
|
||||||
|
if ((numdev = setup_libusb_access(devices)) < 1) {
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
(void) signal(SIGINT, ex_program);
|
||||||
|
|
||||||
|
answer = calloc(reqIntLen, sizeof(unsigned char));
|
||||||
|
/* This looks unnecessary
|
||||||
|
for (i = 0; i < numdev; i++) {
|
||||||
|
ini_control_transfer(devices[i].handle);
|
||||||
|
|
||||||
|
control_transfer(devices[i].handle, uTemperature);
|
||||||
|
interrupt_read(devices[i].handle, answer);
|
||||||
|
|
||||||
|
control_transfer(devices[i].handle, uIni1);
|
||||||
|
interrupt_read(devices[i].handle, answer);
|
||||||
|
|
||||||
|
control_transfer(devices[i].handle, uIni2);
|
||||||
|
interrupt_read(devices[i].handle, answer);
|
||||||
|
interrupt_read(devices[i].handle, answer);
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
do {
|
||||||
|
for (i = 0; i < numdev; i++) {
|
||||||
|
// get localtime
|
||||||
|
t = time(NULL);
|
||||||
|
local = localtime(&t);
|
||||||
|
sprintf(strdate, "%04d-%02d-%02dT%02d:%02d:%02d",
|
||||||
|
local->tm_year +1900,
|
||||||
|
local->tm_mon + 1,
|
||||||
|
local->tm_mday,
|
||||||
|
local->tm_hour,
|
||||||
|
local->tm_min,
|
||||||
|
local->tm_sec);
|
||||||
|
|
||||||
|
// get temperature
|
||||||
|
control_transfer(devices[i].handle, uTemperature);
|
||||||
|
interrupt_read(devices[i].handle, answer);
|
||||||
|
devices[i].type->decode_func(answer, tempd, calibration);
|
||||||
|
|
||||||
|
// print temperature
|
||||||
|
printf("%s\t%d\t%s\t%.2f %s\n",
|
||||||
|
strdate,
|
||||||
|
i,
|
||||||
|
devices[i].type->has_sensor == 2 ? "internal" : "temperature",
|
||||||
|
formato == 2 ? 9.0 / 5.0 * tempd[0] + 32.0 : tempd[0],
|
||||||
|
formato == 2 ? "F" : "C");
|
||||||
|
|
||||||
|
if (devices[i].type->has_sensor == 2) {
|
||||||
|
printf("%s\t%d\texternal\t%.2f %s\n",
|
||||||
|
strdate,
|
||||||
|
i,
|
||||||
|
formato == 2 ? 9.0 / 5.0 * tempd[1] + 32.0 : tempd[1],
|
||||||
|
formato == 2 ? "F" : "C");
|
||||||
|
}
|
||||||
|
|
||||||
|
// print humidity
|
||||||
|
if (devices[i].type->has_humid == 1) {
|
||||||
|
printf("%s\t%d\thumidity\t%.2f %%\n", strdate, i, tempd[1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!bsalir)
|
||||||
|
sleep(seconds);
|
||||||
|
} while (!bsalir);
|
||||||
|
|
||||||
|
cleanup_usb_devices(devices, numdev);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
float get_temp_c() {
|
||||||
|
temper_device_t *devices;
|
||||||
|
int numdev,i;
|
||||||
|
unsigned char *answer;
|
||||||
|
float tempd[2];
|
||||||
|
float calibration[2] = { 1, 0 };
|
||||||
|
|
||||||
|
devices = calloc(MAX_DEV, sizeof(temper_device_t));
|
||||||
|
if ((numdev = setup_libusb_access(devices)) < 1) {
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
(void) signal(SIGINT, ex_program);
|
||||||
|
|
||||||
|
answer = calloc(reqIntLen, sizeof(unsigned char));
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
control_transfer(devices[i].handle, uTemperature);
|
||||||
|
interrupt_read(devices[i].handle, answer);
|
||||||
|
devices[i].type->decode_func(answer, tempd, calibration);
|
||||||
|
|
||||||
|
cleanup_usb_devices(devices, numdev);
|
||||||
|
|
||||||
|
return tempd[0];
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user