From 662b4a81c10dae1d6c2b1fe7f0b16b81e040ca34 Mon Sep 17 00:00:00 2001 From: kellyho67 Date: Tue, 29 Jan 2013 13:38:02 -0700 Subject: [PATCH 1/3] Updated pcsensor.c to accommodate neg temperatures MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is a simple fork of the original code because I was not able to contriubte and simply didn't want to loose the code.  This change has only been tested on my raspberry pi where I'm using the Temper1 probe currently. --- pcsensor.c | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/pcsensor.c b/pcsensor.c index 99687d2..727e4e2 100644 --- a/pcsensor.c +++ b/pcsensor.c @@ -1,4 +1,6 @@ /* + * based on pcsensor.c by Michitaka Ohno and updatede to deal with negative temperatures + * using changes presented by Torbjørn Hergum in the temper1 * pcsensor.c by Michitaka Ohno (c) 2011 (elpeo@mars.dti.ne.jp) * based oc pcsensor.c by Juan Carlos Perez (c) 2011 (cray@isp-sl.com) * based on Temper.c by Robert Kavaler (c) 2009 (relavak.com) @@ -254,7 +256,7 @@ static int interrupt_read(usb_dev_handle *dev) { static int interrupt_read_temperatura(usb_dev_handle *dev, float *tempC) { - int r,i, temperature; + int r,i,temperature; char answer[reqIntLen]; bzero(answer, reqIntLen); @@ -275,6 +277,18 @@ static int interrupt_read_temperatura(usb_dev_handle *dev, float *tempC) { } temperature = (answer[3] & 0xFF) + (answer[2] << 8); + + /* msb means the temperature is negative -- less than 0 Celsius -- and in 2'complement form. + * We can't be sure that the host uses 2's complement to store negative numbers + * so if the temperature is negative, we 'manually' get its magnitude + * by explicity getting it's 2's complement and then we return the negative of that. + */ + + if ((answer[2] & 0x80)!=0) { + /* return the negative of magnitude of the temperature */ + temperature = -((temperature ^ 0xffff)+1); + } + // end of the updates made for netgative temps *tempC = temperature * (125.0 / 32000.0); return 0; } @@ -285,8 +299,8 @@ static int get_data(usb_dev_handle *dev, char *buf, int len){ static int get_temperature(usb_dev_handle *dev, float *tempC){ char buf[256]; - int ret, temperature, i; - + int ret,i,temperature; + control_transfer(dev, uCmd1 ); control_transfer(dev, uCmd4 ); for(i = 0; i < 7; i++) { @@ -298,7 +312,22 @@ static int get_temperature(usb_dev_handle *dev, float *tempC){ return -1; } - temperature = (buf[1] & 0xFF) + (buf[0] << 8); + temperature = (buf[1] & 0xFF) + (buf[0] << 8); + + + /* msb means the temperature is negative -- less than 0 Celsius -- and in 2'complement form. + * We can't be sure that the host uses 2's complement to store negative numbers + * so if the temperature is negative, we 'manually' get its magnitude + * by explicity getting it's 2's complement and then we return the negative of that. + */ + + if ((buf[0] & 0x80)!=0) { + /* return the negative of magnitude of the temperature */ + temperature = -((temperature ^ 0xffff)+1); + } + + // end up the updates made. + *tempC = temperature * (125.0 / 32000.0); return 0; } From 92cfa032d57443fbbfd57beec5789d8f84ed7d48 Mon Sep 17 00:00:00 2001 From: kellyho67 Date: Tue, 29 Jan 2013 13:40:06 -0700 Subject: [PATCH 2/3] Update README --- README | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README b/README index 6d5dfd4..0da4332 100644 --- a/README +++ b/README @@ -1,3 +1,6 @@ +12/29/2013 - Updated made to the pcsensor.c code based off of information provided from +Torbjørn Hergum to accomidate negative temperatures in the reader. +Tested on a raspberry pi with a single temper1 unit only. USB温度計「TEMPer」のRubyライブラリです。 たぶんLinuxでのみ動きます。 From 838df18996840e07a4b9dcdacec3dcbf48086465 Mon Sep 17 00:00:00 2001 From: kellyho67 Date: Tue, 29 Jan 2013 19:56:53 -0700 Subject: [PATCH 3/3] Update README --- README | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README b/README index 0da4332..743f66e 100644 --- a/README +++ b/README @@ -1,7 +1,3 @@ -12/29/2013 - Updated made to the pcsensor.c code based off of information provided from -Torbjørn Hergum to accomidate negative temperatures in the reader. -Tested on a raspberry pi with a single temper1 unit only. - USB温度計「TEMPer」のRubyライブラリです。 たぶんLinuxでのみ動きます。 @@ -33,3 +29,7 @@ ID 0c45:7401 Microdia コンパイルにはlibusbが必要です。 使い方は test.rb をご参照ください。 + +12/29/2013 - Updated made to the pcsensor.c code based off of information provided from +Torbjørn Hergum to accomidate negative temperatures in the reader. +Tested on a raspberry pi with a single temper1 unit only.