updated sensor

This commit is contained in:
Paul 2022-05-08 10:46:27 +02:00
parent 6cb0559b43
commit be6393ec72
2 changed files with 252 additions and 260 deletions

63
Cargo.lock generated
View File

@ -62,9 +62,9 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]] [[package]]
name = "cc" name = "cc"
version = "1.0.72" version = "1.0.73"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22a9137b95ea06864e018375b72adfb7db6e6f68cfc8df5a04d00288050485ee" checksum = "2fff2a6927b3bb87f9595d67196a70493f627687a71d87a0d692242c33f58c11"
dependencies = [ dependencies = [
"jobserver", "jobserver",
] ]
@ -86,9 +86,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]] [[package]]
name = "clang-sys" name = "clang-sys"
version = "1.3.0" version = "1.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fa66045b9cb23c2e9c1520732030608b02ee07e5cfaa5a521ec15ded7fa24c90" checksum = "4cc00842eed744b858222c4c9faf7243aafc6d33f92f96935263ef4d8a41ce21"
dependencies = [ dependencies = [
"glob", "glob",
"libc", "libc",
@ -173,15 +173,15 @@ checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.112" version = "0.2.125"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b03d17f364a3a042d5e5d46b053bbbf82c92c9430c592dd4c064dc6ee997125" checksum = "5916d2ae698f6de9bfb891ad7a8d65c09d232dc58cc4ac433c7da3b2fd84bc2b"
[[package]] [[package]]
name = "libloading" name = "libloading"
version = "0.7.2" version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "afe203d669ec979b7128619bae5a63b7b42e9203c1b29146079ee05e2f604b52" checksum = "efbc0f03f9a775e9f6aed295c6a1ba2253c5757a9e03d55c6caa46a681abcddd"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"winapi", "winapi",
@ -189,18 +189,18 @@ dependencies = [
[[package]] [[package]]
name = "log" name = "log"
version = "0.4.14" version = "0.4.17"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710" checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
] ]
[[package]] [[package]]
name = "memchr" name = "memchr"
version = "2.4.1" version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
[[package]] [[package]]
name = "minimal-lexical" name = "minimal-lexical"
@ -210,13 +210,12 @@ checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
[[package]] [[package]]
name = "nom" name = "nom"
version = "7.1.0" version = "7.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b1d11e1ef389c76fe5b81bcaf2ea32cf88b62bc494e19f493d0b30e7a930109" checksum = "a8903e5a29a317527874d0402f867152a3d21c908bb0b933e416c65e301d4c36"
dependencies = [ dependencies = [
"memchr", "memchr",
"minimal-lexical", "minimal-lexical",
"version_check",
] ]
[[package]] [[package]]
@ -227,33 +226,33 @@ checksum = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099"
[[package]] [[package]]
name = "pkg-config" name = "pkg-config"
version = "0.3.24" version = "0.3.25"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "58893f751c9b0412871a09abd62ecd2a00298c6c83befa223ef98c52aef40cbe" checksum = "1df8c4ec4b0627e53bdf214615ad287367e482558cf84b109250b37464dc03ae"
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.36" version = "1.0.38"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c7342d5883fbccae1cc37a2353b09c87c9b0f3afd73f5fb9bba687a1f733b029" checksum = "9027b48e9d4c9175fa2218adf3557f91c1137021739951d4932f5f8268ac48aa"
dependencies = [ dependencies = [
"unicode-xid", "unicode-xid",
] ]
[[package]] [[package]]
name = "quote" name = "quote"
version = "1.0.14" version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "47aa80447ce4daf1717500037052af176af5d38cc3e571d9ec1c7353fc10c87d" checksum = "a1feb54ed693b93a84e14094943b84b7c4eae204c512b7ccb95ab0c66d278ad1"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
] ]
[[package]] [[package]]
name = "regex" name = "regex"
version = "1.5.4" version = "1.5.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461" checksum = "1a11647b6b25ff05a515cb92c365cec08801e83423a235b51e231e1808747286"
dependencies = [ dependencies = [
"aho-corasick", "aho-corasick",
"memchr", "memchr",
@ -296,9 +295,9 @@ checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
[[package]] [[package]]
name = "termcolor" name = "termcolor"
version = "1.1.2" version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4" checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755"
dependencies = [ dependencies = [
"winapi-util", "winapi-util",
] ]
@ -320,9 +319,9 @@ checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973"
[[package]] [[package]]
name = "unicode-xid" name = "unicode-xid"
version = "0.2.2" version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" checksum = "957e51f3646910546462e67d5f7599b9e4fb8acdd304b087a6494730f9eebf04"
[[package]] [[package]]
name = "vec_map" name = "vec_map"
@ -330,17 +329,11 @@ version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
[[package]]
name = "version_check"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]] [[package]]
name = "which" name = "which"
version = "4.2.2" version = "4.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ea187a8ef279bc014ec368c27a920da2024d2a711109bfbe3440585d5cf27ad9" checksum = "5c4fb54e6113b6a8772ee41c3404fb0301ac79604489467e0a9ce1f3e97c24ae"
dependencies = [ dependencies = [
"either", "either",
"lazy_static", "lazy_static",

View File

@ -16,19 +16,19 @@
typedef struct typedef struct
{ {
const int vendor_id; const int vendor_id;
const int product_id; const int product_id;
const char product_name[256]; const char product_name[256];
const int check_product_name; // if set, check product name in forward match const int check_product_name; // if set, check product name in forward match
const int has_sensor; // number of temperature sensor const int has_sensor; // number of temperature sensor
const int has_humid; // flag for humidity sensor const int has_humid; // flag for humidity sensor
void (*decode_func)(); void (*decode_func)();
} temper_type_t; } temper_type_t;
typedef struct typedef struct
{ {
libusb_device_handle *handle; libusb_device_handle *handle;
temper_type_t *type; temper_type_t *type;
} temper_device_t; } temper_device_t;
void decode_answer_fm75(); void decode_answer_fm75();
@ -37,9 +37,9 @@ void decode_answer_sht1x();
#define TEMPER_TYPES 3 #define TEMPER_TYPES 3
temper_type_t tempers[TEMPER_TYPES] = { temper_type_t tempers[TEMPER_TYPES] = {
{0x0c45, 0x7401, "TEMPer2", 1, 2, 0, decode_answer_fm75}, // TEMPer2* eg. TEMPer2V1.3 {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, 0x7401, "TEMPer1", 0, 1, 0, decode_answer_fm75}, // other 0c45:7401 eg. TEMPerV1.4
{0x0c45, 0x7402, "TEMPerHUM", 0, 1, 1, decode_answer_sht1x}, {0x0c45, 0x7402, "TEMPerHUM", 0, 1, 1, decode_answer_sht1x},
}; };
/* memo: TEMPer2 cannot be distinguished with VID:PID, /* memo: TEMPer2 cannot be distinguished with VID:PID,
@ -69,258 +69,257 @@ static libusb_context *ctx = NULL;
void bad(const char *why) void bad(const char *why)
{ {
fprintf(stderr, "Fatal error> %s\n", why); fprintf(stderr, "Fatal error> %s\n", why);
exit(17); exit(17);
} }
void usb_detach(libusb_device_handle *lvr_winusb, int iInterface) void usb_detach(libusb_device_handle *lvr_winusb, int iInterface)
{ {
int ret; int ret;
ret = libusb_detach_kernel_driver(lvr_winusb, iInterface); ret = libusb_detach_kernel_driver(lvr_winusb, iInterface);
if (ret) if (ret)
{
if (errno == ENODATA)
{ {
if (errno == ENODATA) if (debug)
{ {
if (debug) fprintf(stderr, "Device already detached\n");
{ }
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 else
{ {
if (debug) if (debug)
{ {
fprintf(stderr, "detach successful\n"); 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 find_lvr_winusb(temper_device_t *devices)
{ {
int i, j, s, cnt, numdev; int i, j, s, cnt, numdev;
libusb_device **devs; libusb_device **devs;
//handle = libusb_open_device_with_vid_pid(ctx, VENDOR_ID, PRODUCT_ID); //handle = libusb_open_device_with_vid_pid(ctx, VENDOR_ID, PRODUCT_ID);
cnt = libusb_get_device_list(ctx, &devs); cnt = libusb_get_device_list(ctx, &devs);
if (cnt < 1) 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 find USB device: %d\n", cnt); fprintf(stderr, "Could not get USB device descriptor: %d\n", s);
continue;
} }
numdev = 0; for (j = 0; j < TEMPER_TYPES; j++)
for (i = 0; i < cnt && numdev < MAX_DEV; i++)
{ {
struct libusb_device_descriptor desc; if (desc.idVendor == tempers[j].vendor_id && desc.idProduct == tempers[j].product_id)
{
unsigned char bus, addr, descmanu[256], descprod[256], descseri[256];
if ((s = libusb_get_device_descriptor(devs[i], &desc)) < 0) 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 get USB device descriptor: %d\n", s); 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; continue;
}
} }
else
for (j = 0; j < TEMPER_TYPES; j++)
{ {
if (desc.idVendor == tempers[j].vendor_id && desc.idProduct == tempers[j].product_id) devices[numdev].type = &tempers[j];
{
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++;
}
} }
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); libusb_free_device_list(devs, 1);
return numdev; return numdev;
} }
int setup_libusb_access(temper_device_t *devices) int setup_libusb_access(temper_device_t *devices)
{ {
int i; int i;
int log_level = 0; int log_level = 0;
int numdev; int numdev;
libusb_init(&ctx); libusb_init(&ctx);
if (debug) if (debug)
{ {
log_level = 4; log_level = 4;
} }
#if LIBUSBX_API_VERSION < 0x01000106 #if LIBUSBX_API_VERSION < 0x01000106
libusb_set_debug(ctx, log_level); libusb_set_debug(ctx, log_level);
#else #else
libusb_set_option(ctx, LIBUSB_OPTION_LOG_LEVEL, log_level); libusb_set_option(ctx, LIBUSB_OPTION_LOG_LEVEL, log_level);
#endif #endif
if ((numdev = find_lvr_winusb(devices)) < 1) 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, "Couldn't find the USB device, Exiting: %d\n", numdev); fprintf(stderr, "Could not set configuration 1\n");
return -1; return -1;
} }
for (i = 0; i < numdev; i++) int s;
if ((s = libusb_claim_interface(devices[i].handle, INTERFACE1)) < 0)
{ {
usb_detach(devices[i].handle, INTERFACE1); fprintf(stderr, "Could not claim interface. Error:%d\n", s);
usb_detach(devices[i].handle, INTERFACE2); return -1;
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; 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) void ini_control_transfer(libusb_device_handle *dev)
{ {
int r, i; int r, i;
char question[] = {0x01, 0x01}; char question[] = {0x01, 0x01};
r = libusb_control_transfer(dev, 0x21, 0x09, 0x0201, 0x00, (unsigned char *)question, 2, timeout); r = libusb_control_transfer(dev, 0x21, 0x09, 0x0201, 0x00, (unsigned char *)question, 2, timeout);
if (r < 0) if (r < 0)
{ {
perror("USB control write"); perror("USB control write");
bad("USB write failed"); bad("USB write failed");
} }
if (debug) if (debug)
{ {
for (i = 0; i < reqIntLen; i++) for (i = 0; i < reqIntLen; i++) fprintf(stderr, "%02x ", question[i] & 0xFF);
fprintf(stderr, "%02x ", question[i] & 0xFF); fprintf(stderr, "\n");
fprintf(stderr, "\n"); }
}
} }
void control_transfer(libusb_device_handle *dev, const char *pquestion) void control_transfer(libusb_device_handle *dev, const char *pquestion)
{ {
int r, i; int r, i;
char question[reqIntLen]; char question[reqIntLen];
memcpy(question, pquestion, sizeof question); memcpy(question, pquestion, sizeof question);
r = libusb_control_transfer(dev, 0x21, 0x09, 0x0200, 0x01, (unsigned char *)question, reqIntLen, timeout); r = libusb_control_transfer(dev, 0x21, 0x09, 0x0200, 0x01, (unsigned char *)question, reqIntLen, timeout);
if (r < 0) if (r < 0)
{ {
perror("USB control write"); perror("USB control write");
bad("USB write failed"); bad("USB write failed");
} }
if (debug) if (debug)
{ {
for (i = 0; i < reqIntLen; i++) for (i = 0; i < reqIntLen; i++)
fprintf(stderr, "%02x ", question[i] & 0xFF); fprintf(stderr, "%02x ", question[i] & 0xFF);
fprintf(stderr, "\n"); fprintf(stderr, "\n");
} }
} }
void interrupt_read(libusb_device_handle *dev, unsigned char *answer) void interrupt_read(libusb_device_handle *dev, unsigned char *answer)
{ {
int r, s, i; int r, s, i;
memset(answer, 0, reqIntLen); memset(answer, 0, reqIntLen);
s = libusb_interrupt_transfer(dev, endpoint_Int_in, answer, reqIntLen, &r, timeout); s = libusb_interrupt_transfer(dev, endpoint_Int_in, answer, reqIntLen, &r, timeout);
if (r != reqIntLen) if (r != reqIntLen)
{ {
fprintf(stderr, "USB read failed: %d\n", s); fprintf(stderr, "USB read failed: %d\n", s);
perror("USB interrupt read"); perror("USB interrupt read");
bad("USB read failed"); bad("USB read failed");
} }
if (debug) if (debug)
{ {
for (i = 0; i < reqIntLen; i++) for (i = 0; i < reqIntLen; i++)
fprintf(stderr, "%02x ", answer[i] & 0xFF); fprintf(stderr, "%02x ", answer[i] & 0xFF);
fprintf(stderr, "\n"); fprintf(stderr, "\n");
} }
} }
void cleanup_usb_devices(temper_device_t *devices, int numdev) void cleanup_usb_devices(temper_device_t *devices, int numdev)
{ {
int i; int i;
for (i = 0; i < numdev; i++) for (i = 0; i < numdev; i++)
{ {
libusb_release_interface(devices[i].handle, INTERFACE1); libusb_release_interface(devices[i].handle, INTERFACE1);
libusb_release_interface(devices[i].handle, INTERFACE2); libusb_release_interface(devices[i].handle, INTERFACE2);
libusb_close(devices[i].handle); libusb_close(devices[i].handle);
} }
libusb_exit(ctx); libusb_exit(ctx);
} }
void ex_program() void ex_program()
{ {
bsalir = 1; bsalir = 1;
(void)signal(SIGINT, SIG_DFL); (void)signal(SIGINT, SIG_DFL);
} }
/* decode funcs */ /* decode funcs */
@ -328,62 +327,62 @@ void ex_program()
void decode_answer_fm75(unsigned char *answer, float *tempd, float *calibration) void decode_answer_fm75(unsigned char *answer, float *tempd, float *calibration)
{ {
int buf; int buf;
// temp C internal // temp C internal
buf = ((signed char)answer[2] << 8) + (answer[3] & 0xFF); buf = ((signed char)answer[2] << 8) + (answer[3] & 0xFF);
tempd[0] = buf * (125.0 / 32000.0); tempd[0] = buf * (125.0 / 32000.0);
tempd[0] = tempd[0] * calibration[0] + calibration[1]; tempd[0] = tempd[0] * calibration[0] + calibration[1];
// temp C external // temp C external
buf = ((signed char)answer[4] << 8) + (answer[5] & 0xFF); buf = ((signed char)answer[4] << 8) + (answer[5] & 0xFF);
tempd[1] = buf * (125.0 / 32000.0); tempd[1] = buf * (125.0 / 32000.0);
tempd[1] = tempd[1] * calibration[0] + calibration[1]; tempd[1] = tempd[1] * calibration[0] + calibration[1];
} }
void decode_answer_sht1x(unsigned char *answer, float *tempd, float *calibration) void decode_answer_sht1x(unsigned char *answer, float *tempd, float *calibration)
{ {
int buf; int buf;
// temp C // temp C
buf = ((signed char)answer[2] << 8) + (answer[3] & 0xFF); buf = ((signed char)answer[2] << 8) + (answer[3] & 0xFF);
tempd[0] = -39.7 + 0.01 * buf; tempd[0] = -39.7 + 0.01 * buf;
tempd[0] = tempd[0] * calibration[0] + calibration[1]; tempd[0] = tempd[0] * calibration[0] + calibration[1];
// relative humidity // relative humidity
buf = ((signed char)answer[4] << 8) + (answer[5] & 0xFF); buf = ((signed char)answer[4] << 8) + (answer[5] & 0xFF);
tempd[1] = -2.0468 + 0.0367 * buf - 1.5955e-6 * buf * buf; tempd[1] = -2.0468 + 0.0367 * buf - 1.5955e-6 * buf * buf;
tempd[1] = (tempd[0] - 25) * (0.01 + 0.00008 * buf) + tempd[1]; tempd[1] = (tempd[0] - 25) * (0.01 + 0.00008 * buf) + tempd[1];
if (tempd[1] < 0) if (tempd[1] < 0)
tempd[1] = 0; tempd[1] = 0;
if (tempd[1] > 99) if (tempd[1] > 99)
tempd[1] = 100; tempd[1] = 100;
} }
float get_temp_c() float get_temp_c()
{ {
temper_device_t *devices; temper_device_t *devices;
int numdev, i; int numdev, i;
unsigned char *answer; unsigned char *answer;
float tempd[2]; float tempd[2];
float calibration[2] = {1, 0}; float calibration[2] = {1, 0};
devices = calloc(MAX_DEV, sizeof(temper_device_t)); devices = calloc(MAX_DEV, sizeof(temper_device_t));
if ((numdev = setup_libusb_access(devices)) < 1) if ((numdev = setup_libusb_access(devices)) < 1)
{ {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
(void)signal(SIGINT, ex_program); (void)signal(SIGINT, ex_program);
answer = calloc(reqIntLen, sizeof(unsigned char)); answer = calloc(reqIntLen, sizeof(unsigned char));
i = 0; i = 0;
control_transfer(devices[i].handle, uTemperature); control_transfer(devices[i].handle, uTemperature);
interrupt_read(devices[i].handle, answer); interrupt_read(devices[i].handle, answer);
devices[i].type->decode_func(answer, tempd, calibration); devices[i].type->decode_func(answer, tempd, calibration);
cleanup_usb_devices(devices, numdev); cleanup_usb_devices(devices, numdev);
return tempd[0]; return tempd[0];
} }