updated sensor
This commit is contained in:
parent
2c8a6d2bbf
commit
6cb0559b43
353
src/pcsensor.c
353
src/pcsensor.c
@ -7,13 +7,15 @@
|
|||||||
#include <strings.h>
|
#include <strings.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
|
#include <getopt.h>
|
||||||
#include <libusb-1.0/libusb.h>
|
#include <libusb-1.0/libusb.h>
|
||||||
|
|
||||||
#define VERSION "1.2.0"
|
#define VERSION "1.2.0"
|
||||||
|
|
||||||
/* TEMPer type definition */
|
/* TEMPer type definition */
|
||||||
|
|
||||||
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];
|
||||||
@ -23,7 +25,8 @@ typedef struct {
|
|||||||
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;
|
||||||
@ -34,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,
|
||||||
@ -49,78 +52,94 @@ temper_type_t tempers[TEMPER_TYPES] = {
|
|||||||
#define INTERFACE1 0x00
|
#define INTERFACE1 0x00
|
||||||
#define INTERFACE2 0x01
|
#define INTERFACE2 0x01
|
||||||
|
|
||||||
const int reqIntLen=8;
|
const int reqIntLen = 8;
|
||||||
const int endpoint_Int_in=0x82; /* endpoint 0x81 address for IN */
|
const int endpoint_Int_in = 0x82; /* endpoint 0x81 address for IN */
|
||||||
const int timeout=5000; /* timeout in ms */
|
const int timeout = 5000; /* timeout in ms */
|
||||||
|
|
||||||
const char uTemperature[] = { 0x01, 0x80, 0x33, 0x01, 0x00, 0x00, 0x00, 0x00 };
|
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 uIni1[] = { 0x01, 0x82, 0x77, 0x01, 0x00, 0x00, 0x00, 0x00 };
|
||||||
//const static char uIni2[] = { 0x01, 0x86, 0xff, 0x01, 0x00, 0x00, 0x00, 0x00 };
|
//const static char uIni2[] = { 0x01, 0x86, 0xff, 0x01, 0x00, 0x00, 0x00, 0x00 };
|
||||||
|
|
||||||
static int bsalir=1;
|
static int bsalir = 1;
|
||||||
static int debug=0;
|
static int debug = 0;
|
||||||
static int seconds=5;
|
|
||||||
static int formato=1; //Celsius
|
|
||||||
|
|
||||||
static libusb_context *ctx = NULL;
|
static libusb_context *ctx = NULL;
|
||||||
|
|
||||||
/* functions */
|
/* functions */
|
||||||
|
|
||||||
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(debug) {
|
if (errno == ENODATA)
|
||||||
|
{
|
||||||
|
if (debug)
|
||||||
|
{
|
||||||
fprintf(stderr, "Device already detached\n");
|
fprintf(stderr, "Device already detached\n");
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
if(debug) {
|
else
|
||||||
|
{
|
||||||
|
if (debug)
|
||||||
|
{
|
||||||
fprintf(stderr, "Detach failed: %s[%d]\n", strerror(errno), errno);
|
fprintf(stderr, "Detach failed: %s[%d]\n", strerror(errno), errno);
|
||||||
fprintf(stderr, "Continuing anyway\n");
|
fprintf(stderr, "Continuing anyway\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
if(debug) {
|
else
|
||||||
|
{
|
||||||
|
if (debug)
|
||||||
|
{
|
||||||
fprintf(stderr, "detach successful\n");
|
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);
|
fprintf(stderr, "Could not find USB device: %d\n", cnt);
|
||||||
}
|
}
|
||||||
|
|
||||||
numdev = 0;
|
numdev = 0;
|
||||||
for (i = 0; i < cnt && numdev < MAX_DEV; i++) {
|
for (i = 0; i < cnt && numdev < MAX_DEV; i++)
|
||||||
|
{
|
||||||
struct libusb_device_descriptor desc;
|
struct libusb_device_descriptor desc;
|
||||||
|
|
||||||
if ((s = libusb_get_device_descriptor(devs[i], &desc)) < 0) {
|
if ((s = libusb_get_device_descriptor(devs[i], &desc)) < 0)
|
||||||
|
{
|
||||||
fprintf(stderr, "Could not get USB device descriptor: %d\n", s);
|
fprintf(stderr, "Could not get USB device descriptor: %d\n", s);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (j = 0; j < TEMPER_TYPES; j++) {
|
for (j = 0; j < TEMPER_TYPES; j++)
|
||||||
if (desc.idVendor == tempers[j].vendor_id && desc.idProduct == tempers[j].product_id) {
|
{
|
||||||
|
if (desc.idVendor == tempers[j].vendor_id && desc.idProduct == tempers[j].product_id)
|
||||||
|
{
|
||||||
unsigned char bus, addr, descmanu[256], descprod[256], descseri[256];
|
unsigned char bus, addr, descmanu[256], descprod[256], descseri[256];
|
||||||
|
|
||||||
bus = libusb_get_bus_number(devs[i]);
|
bus = libusb_get_bus_number(devs[i]);
|
||||||
addr = libusb_get_device_address(devs[i]);
|
addr = libusb_get_device_address(devs[i]);
|
||||||
|
|
||||||
if ((s = libusb_open(devs[i], &devices[numdev].handle)) < 0) {
|
if ((s = libusb_open(devs[i], &devices[numdev].handle)) < 0)
|
||||||
|
{
|
||||||
fprintf(stderr, "Could not open USB device: %d\n", s);
|
fprintf(stderr, "Could not open USB device: %d\n", s);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -129,21 +148,26 @@ int find_lvr_winusb(temper_device_t *devices) {
|
|||||||
libusb_get_string_descriptor_ascii(devices[numdev].handle, desc.iProduct, descprod, 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);
|
libusb_get_string_descriptor_ascii(devices[numdev].handle, desc.iSerialNumber, descseri, 256);
|
||||||
|
|
||||||
if (tempers[j].check_product_name) {
|
if (tempers[j].check_product_name)
|
||||||
if (strncmp((const char*)descprod, tempers[j].product_name, strlen(tempers[j].product_name)) == 0) {
|
{
|
||||||
|
if (strncmp((const char *)descprod, tempers[j].product_name, strlen(tempers[j].product_name)) == 0)
|
||||||
|
{
|
||||||
devices[numdev].type = &tempers[j];
|
devices[numdev].type = &tempers[j];
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
|
{
|
||||||
// vid and pid match, but product name unmatch
|
// vid and pid match, but product name unmatch
|
||||||
libusb_close(devices[numdev].handle);
|
libusb_close(devices[numdev].handle);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
|
{
|
||||||
devices[numdev].type = &tempers[j];
|
devices[numdev].type = &tempers[j];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (debug) {
|
if (debug)
|
||||||
|
{
|
||||||
fprintf(stderr, "lvr_winusb with Bus:%03d Addr:%03d VendorID:%04x ProductID:%04x Manufacturer:%s Product:%s Serial:%s found.\n",
|
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);
|
bus, addr, desc.idVendor, desc.idProduct, descmanu, descprod, descseri);
|
||||||
}
|
}
|
||||||
@ -158,14 +182,16 @@ int find_lvr_winusb(temper_device_t *devices) {
|
|||||||
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
|
||||||
@ -174,28 +200,33 @@ int setup_libusb_access(temper_device_t *devices) {
|
|||||||
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);
|
fprintf(stderr, "Couldn't find the USB device, Exiting: %d\n", numdev);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < numdev; i++) {
|
for (i = 0; i < numdev; i++)
|
||||||
|
{
|
||||||
usb_detach(devices[i].handle, INTERFACE1);
|
usb_detach(devices[i].handle, INTERFACE1);
|
||||||
usb_detach(devices[i].handle, INTERFACE2);
|
usb_detach(devices[i].handle, INTERFACE2);
|
||||||
libusb_reset_device(devices[i].handle);
|
libusb_reset_device(devices[i].handle);
|
||||||
|
|
||||||
if (libusb_set_configuration(devices[i].handle, 0x01) < 0) {
|
if (libusb_set_configuration(devices[i].handle, 0x01) < 0)
|
||||||
|
{
|
||||||
fprintf(stderr, "Could not set configuration 1\n");
|
fprintf(stderr, "Could not set configuration 1\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int s;
|
int s;
|
||||||
if ((s = libusb_claim_interface(devices[i].handle, INTERFACE1)) < 0) {
|
if ((s = libusb_claim_interface(devices[i].handle, INTERFACE1)) < 0)
|
||||||
|
{
|
||||||
fprintf(stderr, "Could not claim interface. Error:%d\n", s);
|
fprintf(stderr, "Could not claim interface. Error:%d\n", s);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((s = libusb_claim_interface(devices[i].handle, INTERFACE2)) < 0) {
|
if ((s = libusb_claim_interface(devices[i].handle, INTERFACE2)) < 0)
|
||||||
|
{
|
||||||
fprintf(stderr, "Could not claim interface. Error:%d\n", s);
|
fprintf(stderr, "Could not claim interface. Error:%d\n", s);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -204,61 +235,78 @@ int setup_libusb_access(temper_device_t *devices) {
|
|||||||
return numdev;
|
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"); bad("USB write failed");
|
{
|
||||||
|
perror("USB control write");
|
||||||
|
bad("USB write failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
if(debug) {
|
if (debug)
|
||||||
for (i=0;i<reqIntLen; i++) fprintf(stderr, "%02x ",question[i] & 0xFF);
|
{
|
||||||
|
for (i = 0; i < reqIntLen; i++)
|
||||||
|
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"); bad("USB write failed");
|
{
|
||||||
|
perror("USB control write");
|
||||||
|
bad("USB write failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
if(debug) {
|
if (debug)
|
||||||
for (i=0;i<reqIntLen; i++) fprintf(stderr, "%02x ",question[i] & 0xFF);
|
{
|
||||||
|
for (i = 0; i < reqIntLen; i++)
|
||||||
|
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"); bad("USB read failed");
|
perror("USB interrupt read");
|
||||||
|
bad("USB read failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
if(debug) {
|
if (debug)
|
||||||
for (i=0;i<reqIntLen; i++) fprintf(stderr, "%02x ",answer[i] & 0xFF);
|
{
|
||||||
|
for (i = 0; i < reqIntLen; i++)
|
||||||
|
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);
|
||||||
|
|
||||||
@ -268,16 +316,18 @@ void cleanup_usb_devices(temper_device_t *devices, int numdev) {
|
|||||||
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 */
|
||||||
/* Thanks to https://github.com/edorfaus/TEMPered */
|
/* Thanks to https://github.com/edorfaus/TEMPered */
|
||||||
|
|
||||||
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
|
||||||
@ -289,9 +339,10 @@ void decode_answer_fm75(unsigned char *answer, float *tempd, float *calibration)
|
|||||||
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
|
||||||
@ -302,164 +353,28 @@ void decode_answer_sht1x(unsigned char *answer, float *tempd, float *calibration
|
|||||||
// 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) tempd[1] = 0;
|
if (tempd[1] < 0)
|
||||||
if (tempd[1] > 99) tempd[1] = 100;
|
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() {
|
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));
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user