From 3b9112a4a3a59a42a6bcce3cc2eee3336d771994 Mon Sep 17 00:00:00 2001 From: Axel von Bertoldi Date: Mon, 19 Jan 2015 21:01:20 -0700 Subject: [PATCH 1/7] retab and remove trailing whitespace --- pcsensor.c | 566 ++++++++++++++++++++++++++--------------------------- pcsensor.h | 4 +- temper.c | 82 ++++---- 3 files changed, 326 insertions(+), 326 deletions(-) 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/temper.c b/temper.c index 5670bb5..8e6eb80 100644 --- a/temper.c +++ b/temper.c @@ -13,46 +13,46 @@ static float scale = 1.0287; static float offset = -0.85; 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; - } + 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; + } } From b51c92fa3444bd1a78799cae78f2ecb0b3c1ad30 Mon Sep 17 00:00:00 2001 From: Axel von Bertoldi Date: Mon, 19 Jan 2015 21:06:30 -0700 Subject: [PATCH 2/7] Method to take temperature reading This is the first step in separating the various things happening in the main loop. Like the original, this method reads up to three times looking for a non-zero value and returns immediately if one is read. Unlike the original, this one uses errno to indicate no successful reads. --- temper.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/temper.c b/temper.c index 8e6eb80..b9eb0a5 100644 --- a/temper.c +++ b/temper.c @@ -4,6 +4,7 @@ */ #include +#include #include #include "pcsensor.h" @@ -12,6 +13,28 @@ static float scale = 1.0287; static float offset = -0.85; +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; +} + int main(){ int passes = 0; float tempc = 0.0000; From 5f39362691163a82c01923dfc57488318b631974 Mon Sep 17 00:00:00 2001 From: Axel von Bertoldi Date: Mon, 19 Jan 2015 21:14:01 -0700 Subject: [PATCH 3/7] Method to correct reading ...based on the scale and offset. --- temper.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/temper.c b/temper.c index b9eb0a5..9425610 100644 --- a/temper.c +++ b/temper.c @@ -35,6 +35,10 @@ float read_temp() { return tempc; } +float correct(float tempc) { + return (tempc * scale) + offset; +} + int main(){ int passes = 0; float tempc = 0.0000; From 1f04de73132cd7ab6470cc0d58c9dcb62ca5289a Mon Sep 17 00:00:00 2001 From: Axel von Bertoldi Date: Mon, 19 Jan 2015 21:14:59 -0700 Subject: [PATCH 4/7] Method to print the temperature to stdout Also prints the time. Unlike the original, this method uses localtime and ISO 8601 format. Also unlike the original, this one limits the significant decimals to two; I doubt this sensor is accurate to 4 or 5 decimal places. --- temper.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/temper.c b/temper.c index 9425610..a9c7fe6 100644 --- a/temper.c +++ b/temper.c @@ -39,6 +39,19 @@ 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; From 9586079e832112ca1f8d3aae141c7719c35e14a1 Mon Sep 17 00:00:00 2001 From: Axel von Bertoldi Date: Mon, 19 Jan 2015 21:17:15 -0700 Subject: [PATCH 5/7] Use new methods Now the gist of the main loop is easier to follow. --- temper.c | 44 +++++--------------------------------------- 1 file changed, 5 insertions(+), 39 deletions(-) diff --git a/temper.c b/temper.c index a9c7fe6..f324a8d 100644 --- a/temper.c +++ b/temper.c @@ -53,46 +53,12 @@ void print_temp(float tempc) { } 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); + float tempc = read_temp(); - 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 { + if (errno == ENOENT) { return 1; + } else { + print_temp(correct(tempc)); + return 0; } - } From 021b9caa451f0f59d127480c801d29d9f7e59f18 Mon Sep 17 00:00:00 2001 From: Axel von Bertoldi Date: Sun, 8 Feb 2015 19:43:48 -0700 Subject: [PATCH 6/7] Remove dubious temperature scaling --- temper.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/temper.c b/temper.c index f324a8d..a3cfb04 100644 --- a/temper.c +++ b/temper.c @@ -10,8 +10,8 @@ /* 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; From 677d06b57fef99c0919c8c2b06a112a6867f6c89 Mon Sep 17 00:00:00 2001 From: Axel von Bertoldi Date: Sun, 8 Feb 2015 19:46:17 -0700 Subject: [PATCH 7/7] Script to take temp reading, strip out time stamp and add units --- read_temp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100755 read_temp 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