updated pcsensor.c with new sensor

This commit is contained in:
Paul 2023-03-07 18:31:50 +01:00
parent d434a9441a
commit 5d15838757

View File

@ -22,6 +22,7 @@ typedef struct {
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)();
float divisor;
} temper_type_t; } temper_type_t;
typedef struct { typedef struct {
@ -29,15 +30,24 @@ typedef struct {
temper_type_t *type; temper_type_t *type;
} temper_device_t; } temper_device_t;
void decode_answer_fm75(); void decode_answer();
void decode_answer_sht1x();
#define TEMPER_TYPES 4
const char GetFirmware[8] = { 0x01, 0x86, 0xff, 0x01, 0x00, 0x00, 0x00, 0x00 };
const char GetTemperature[8] = { 0x01, 0x80, 0x33, 0x01, 0x00, 0x00, 0x00, 0x00 };
//#define Temperature { 0x01, 0x82, 0x77, 0x01, 0x00, 0x00, 0x00, 0x00 }
//const char uTemperature1[8] = { 0x01, 0x80, 0x33, 0x01, 0x00, 0x00, 0x00, 0x00 };
//const char uTemperature2[8] = { 0x01, 0x82, 0x77, 0x01, 0x00, 0x00, 0x00, 0x00 };
//const char uTemperature3[8] = { 0x01, 0x86, 0xff, 0x01, 0x00, 0x00, 0x00, 0x00 };
#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 {0x1a86, 0xe025, "TEMPerGold", 1, 1, 0, decode_answer, 100.0},
{0x0c45, 0x7401, "TEMPer1", 0, 1, 0, decode_answer_fm75}, // other 0c45:7401 eg. TEMPerV1.4 {0x0c45, 0x7401, "TEMPer2", 1, 2, 0, decode_answer, 256.0},
{0x0c45, 0x7402, "TEMPerHUM", 0, 1, 1, decode_answer_sht1x}, {0x0c45, 0x7401, "TEMPer1", 0, 1, 0, decode_answer, 256.0},
//{0x0c45, 0x7402, "TEMPerHUM", 0, 1, 1, decode_answer_sht1x, 100.0},
}; };
/* memo: TEMPer2 cannot be distinguished with VID:PID, /* memo: TEMPer2 cannot be distinguished with VID:PID,
@ -51,12 +61,9 @@ temper_type_t tempers[TEMPER_TYPES] = {
#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_out = 0x02;
const int timeout = 5000; /* timeout in ms */ const int endpoint_Int_in = 0x82;
const int timeout = 300; /* 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 bsalir = 1;
static int debug = 0; static int debug = 0;
@ -156,7 +163,6 @@ int find_lvr_winusb(temper_device_t *devices) {
} }
int setup_libusb_access(temper_device_t *devices) { int setup_libusb_access(temper_device_t *devices) {
int i;
int log_level = 0; int log_level = 0;
int numdev; int numdev;
@ -176,7 +182,7 @@ int setup_libusb_access(temper_device_t *devices) {
return -1; return -1;
} }
for (i = 0; i < numdev; i++) { for (int 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);
@ -201,25 +207,6 @@ int setup_libusb_access(temper_device_t *devices) {
return numdev; return numdev;
} }
void ini_control_transfer(libusb_device_handle *dev) {
int r, i;
char question[reqIntLen];
question[0] = 0x01;
question[1] = 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) { void control_transfer(libusb_device_handle *dev, const char *pquestion) {
int r, i; int r, i;
@ -230,7 +217,7 @@ void control_transfer(libusb_device_handle *dev, const char *pquestion) {
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"); return;
} }
if (debug) { if (debug) {
@ -240,15 +227,24 @@ void control_transfer(libusb_device_handle *dev, const char *pquestion) {
} }
} }
void interrupt_read(libusb_device_handle *dev, unsigned char *answer) { void interrupt_read(libusb_device_handle *dev, unsigned char *answer, const char *pquestion, const char *product_name) {
int r, s, i; int r, s, i;
char question[reqIntLen];
memset(answer, 0, reqIntLen); memset(answer, 0, reqIntLen);
memcpy(question, pquestion, sizeof question);
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) {
fprintf(stderr, "USB read failed: %d\n", s); if (strcmp(product_name, "TEMPerGold")==0) {
libusb_interrupt_transfer(dev, endpoint_Int_out, (unsigned char *)question, reqIntLen, &r, timeout);
s = libusb_interrupt_transfer(dev, endpoint_Int_in, answer, reqIntLen, &r, timeout);
}
if (s != 0) {
fprintf(stderr, "USB read failed: %s\n", libusb_error_name(s));
perror("USB interrupt read"); perror("USB interrupt read");
bad("USB read failed"); return;
} }
if (debug) { if (debug) {
@ -260,13 +256,13 @@ void interrupt_read(libusb_device_handle *dev, unsigned char *answer) {
} }
void cleanup_usb_devices(temper_device_t *devices, int numdev) { void cleanup_usb_devices(temper_device_t *devices, int numdev) {
int i; for (int 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_attach_kernel_driver(devices[i].handle, INTERFACE1);
libusb_attach_kernel_driver(devices[i].handle, INTERFACE2);
libusb_close(devices[i].handle); libusb_close(devices[i].handle);
} }
@ -279,24 +275,22 @@ void ex_program() {
(void)signal(SIGINT, SIG_DFL); (void)signal(SIGINT, SIG_DFL);
} }
/* decode funcs */ void decode_answer(unsigned char *answer, float *divisor, float *tempd, float *calibration) {
/* Thanks to https://github.com/edorfaus/TEMPered */
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 / *divisor;
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 / *divisor;
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 *divisor, float *tempd, float *calibration) {
int buf; int buf;
// temp C // temp C
@ -313,10 +307,12 @@ void decode_answer_sht1x(unsigned char *answer, float *tempd, float *calibration
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;
int i = 0;
unsigned char *answer; unsigned char *answer;
float tempd[2]; float tempd[2];
float calibration[2] = {1, 0}; float calibration[2] = {1, 0};
@ -330,10 +326,9 @@ float get_temp_c() {
answer = calloc(reqIntLen, sizeof(unsigned char)); answer = calloc(reqIntLen, sizeof(unsigned char));
i = 0; control_transfer(devices[i].handle, GetTemperature);
control_transfer(devices[i].handle, uTemperature); interrupt_read(devices[i].handle, answer, GetTemperature, devices[i].type->product_name);
interrupt_read(devices[i].handle, answer); devices[i].type->decode_func(answer, &devices[i].type->divisor, tempd, calibration);
devices[i].type->decode_func(answer, tempd, calibration);
cleanup_usb_devices(devices, numdev); cleanup_usb_devices(devices, numdev);