diff --git a/pcsensor.c b/pcsensor.c index 7bc3d8a..2c4db16 100644 --- a/pcsensor.c +++ b/pcsensor.c @@ -8,7 +8,7 @@ * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. - * + * * THIS SOFTWARE IS PROVIDED BY Juan Carlos Perez ''AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE @@ -19,22 +19,22 @@ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * + * */ #include "pcsensor.h" - + #define INTERFACE1 (0x00) #define INTERFACE2 (0x01) #define SUPPORTED_DEVICES (2) -const static unsigned short vendor_id[] = { - 0x1130, - 0x0c45 +const static unsigned short vendor_id[] = { + 0x1130, + 0x0c45 }; -const static unsigned short product_id[] = { - 0x660c, - 0x7401 +const static unsigned short product_id[] = { + 0x660c, + 0x7401 }; const static char uTemperatura[] = { 0x01, 0x80, 0x33, 0x01, 0x00, 0x00, 0x00, 0x00 }; @@ -49,320 +49,320 @@ const static char uCmd4[] = { 0x54, 0, 0, 0, 0, 0, 0, 0 }; const static int reqIntLen=8; const static int reqBulkLen=8; const static int timeout=5000; /* timeout in ms */ - + static int debug=0; static int device_type(usb_dev_handle *lvr_winusb){ - struct usb_device *dev; - int i; - dev = usb_device(lvr_winusb); - for(i =0;i < SUPPORTED_DEVICES;i++){ - if (dev->descriptor.idVendor == vendor_id[i] && - dev->descriptor.idProduct == product_id[i] ) { - return i; - } - } - return -1; + struct usb_device *dev; + int i; + dev = usb_device(lvr_winusb); + for(i =0;i < SUPPORTED_DEVICES;i++){ + if (dev->descriptor.idVendor == vendor_id[i] && + dev->descriptor.idProduct == product_id[i] ) { + return i; + } + } + return -1; } static int usb_detach(usb_dev_handle *lvr_winusb, int iInterface) { - int ret; - - ret = usb_detach_kernel_driver_np(lvr_winusb, iInterface); - if(ret) { - if(errno == ENODATA) { - if(debug) { - printf("Device already detached\n"); - } - } else { - if(debug) { - printf("Detach failed: %s[%d]\n", - strerror(errno), errno); - printf("Continuing anyway\n"); - } - } - } else { - if(debug) { - printf("detach successful\n"); - } - } - return ret; -} + int ret; + + ret = usb_detach_kernel_driver_np(lvr_winusb, iInterface); + if(ret) { + if(errno == ENODATA) { + if(debug) { + printf("Device already detached\n"); + } + } else { + if(debug) { + printf("Detach failed: %s[%d]\n", + strerror(errno), errno); + printf("Continuing anyway\n"); + } + } + } else { + if(debug) { + printf("detach successful\n"); + } + } + return ret; +} static usb_dev_handle *find_lvr_winusb() { - - struct usb_bus *bus; - struct usb_device *dev; - int i; - - for (bus = usb_busses; bus; bus = bus->next) { - for (dev = bus->devices; dev; dev = dev->next) { - for(i =0;i < SUPPORTED_DEVICES;i++){ - if (dev->descriptor.idVendor == vendor_id[i] && - dev->descriptor.idProduct == product_id[i] ) { - usb_dev_handle *handle; - if(debug) { - printf("lvr_winusb with Vendor Id: %x and Product Id: %x found.\n", vendor_id[i], product_id[i]); - } - - if (!(handle = usb_open(dev))) { - if(debug){ - printf("Could not open USB device\n"); - } - return NULL; - } - return handle; - } - } - } - } - return NULL; + + struct usb_bus *bus; + struct usb_device *dev; + int i; + + for (bus = usb_busses; bus; bus = bus->next) { + for (dev = bus->devices; dev; dev = dev->next) { + for(i =0;i < SUPPORTED_DEVICES;i++){ + if (dev->descriptor.idVendor == vendor_id[i] && + dev->descriptor.idProduct == product_id[i] ) { + usb_dev_handle *handle; + if(debug) { + printf("lvr_winusb with Vendor Id: %x and Product Id: %x found.\n", vendor_id[i], product_id[i]); + } + + if (!(handle = usb_open(dev))) { + if(debug){ + printf("Could not open USB device\n"); + } + return NULL; + } + return handle; + } + } + } + } + return NULL; } static usb_dev_handle* setup_libusb_access() { - usb_dev_handle *lvr_winusb; - - if(debug) { - usb_set_debug(255); - } else { - usb_set_debug(0); - } - usb_init(); - usb_find_busses(); - usb_find_devices(); - - - if(!(lvr_winusb = find_lvr_winusb())) { - if(debug){ - printf("Couldn't find the USB device, Exiting\n"); - } - return NULL; - } - - - usb_detach(lvr_winusb, INTERFACE1); - - - usb_detach(lvr_winusb, INTERFACE2); - - - if (usb_set_configuration(lvr_winusb, 0x01) < 0) { - if(debug){ - printf("Could not set configuration 1\n"); - } - return NULL; - } - - - // Microdia tiene 2 interfaces - if (usb_claim_interface(lvr_winusb, INTERFACE1) < 0) { - if(debug){ - printf("Could not claim interface\n"); - } - return NULL; - } - - if (usb_claim_interface(lvr_winusb, INTERFACE2) < 0) { - if(debug){ - printf("Could not claim interface\n"); - } - return NULL; - } - - return lvr_winusb; + usb_dev_handle *lvr_winusb; + + if(debug) { + usb_set_debug(255); + } else { + usb_set_debug(0); + } + usb_init(); + usb_find_busses(); + usb_find_devices(); + + + if(!(lvr_winusb = find_lvr_winusb())) { + if(debug){ + printf("Couldn't find the USB device, Exiting\n"); + } + return NULL; + } + + + usb_detach(lvr_winusb, INTERFACE1); + + + usb_detach(lvr_winusb, INTERFACE2); + + + if (usb_set_configuration(lvr_winusb, 0x01) < 0) { + if(debug){ + printf("Could not set configuration 1\n"); + } + return NULL; + } + + + // Microdia tiene 2 interfaces + if (usb_claim_interface(lvr_winusb, INTERFACE1) < 0) { + if(debug){ + printf("Could not claim interface\n"); + } + return NULL; + } + + if (usb_claim_interface(lvr_winusb, INTERFACE2) < 0) { + if(debug){ + printf("Could not claim interface\n"); + } + return NULL; + } + + return lvr_winusb; } - + static int ini_control_transfer(usb_dev_handle *dev) { - int r,i; + int r,i; - char question[] = { 0x01,0x01 }; + char question[] = { 0x01,0x01 }; - r = usb_control_msg(dev, 0x21, 0x09, 0x0201, 0x00, (char *) question, 2, timeout); - if( r < 0 ) - { - if(debug){ - printf("USB control write"); - } - return -1; - } + r = usb_control_msg(dev, 0x21, 0x09, 0x0201, 0x00, (char *) question, 2, timeout); + if( r < 0 ) + { + if(debug){ + printf("USB control write"); + } + return -1; + } - if(debug) { - for (i=0;i diff --git a/read_temp b/read_temp new file mode 100755 index 0000000..a46a9b6 --- /dev/null +++ b/read_temp @@ -0,0 +1,14 @@ +#!/bin/dash + +BIN_PATH=$(dirname $(readlink -fn $0)) +cd $BIN_PATH + +die() { + >&2 echo "failed to read from temperature sensor." + exit $1 +} + +reading=$(./temper) || die $? +temp=$(echo $reading | sed 's/.*,//') +echo "$temp ÂșC" +exit 0 diff --git a/temper.c b/temper.c index 5670bb5..a3cfb04 100644 --- a/temper.c +++ b/temper.c @@ -4,55 +4,61 @@ */ #include +#include #include #include "pcsensor.h" /* Calibration adjustments */ /* See http://www.pitt-pladdy.com/blog/_20110824-191017_0100_TEMPer_under_Linux_perl_with_Cacti/ */ -static float scale = 1.0287; -static float offset = -0.85; +static float scale = 1; +static float offset = 0; + +float read_temp() { + int i; + float tempc = 0.0000; + + for (i = 0; i < 4; i++) { + usb_dev_handle* lvr_winusb = pcsensor_open(); + if (!lvr_winusb) { + sleep(3); + continue; + } + tempc = pcsensor_get_temperature(lvr_winusb); + pcsensor_close(lvr_winusb); + + /* Read can fail silently with a 0.0 return; repeat until we get a not zero + * value or until we have read a zero value 3 times (just in case the + * temperature really is zero */ + if (tempc < -0.0001 || tempc > 0.0001) { return tempc; } + } + errno = ENOENT; + return tempc; +} + +float correct(float tempc) { + return (tempc * scale) + offset; +} + +void print_temp(float tempc) { + struct tm *utc; + time_t t; + t = time(NULL); + utc = localtime(&t); + + char dt[80]; + strftime(dt, 80, "%F %T", utc); + + printf("%s,%2.2f\n", dt, tempc); + fflush(stdout); +} int main(){ - int passes = 0; - float tempc = 0.0000; - do { - usb_dev_handle* lvr_winusb = pcsensor_open(); - - if (!lvr_winusb) { - /* Open fails sometime, sleep and try again */ - sleep(3); - } - else { - - tempc = pcsensor_get_temperature(lvr_winusb); - pcsensor_close(lvr_winusb); - } - ++passes; - } - /* Read fails silently with a 0.0 return, so repeat until not zero - or until we have read the same zero value 3 times (just in case - temp is really dead on zero */ - while ((tempc > -0.0001 && tempc < 0.0001) || passes >= 4); - - if (!((tempc > -0.0001 && tempc < 0.0001) || passes >= 4)) { - /* Apply calibrations */ - tempc = (tempc * scale) + offset; - - struct tm *utc; - time_t t; - t = time(NULL); - utc = gmtime(&t); - - char dt[80]; - strftime(dt, 80, "%d-%b-%Y %H:%M", utc); - - printf("%s,%f\n", dt, tempc); - fflush(stdout); - - return 0; - } - else { - return 1; - } + float tempc = read_temp(); + if (errno == ENOENT) { + return 1; + } else { + print_temp(correct(tempc)); + return 0; + } }