asmuelle / node-temper1

Measure temperatures with nodejs and temper1 usb thermometers
MIT License
24 stars 7 forks source link

Below zero values don't make sense #5

Closed rjcorwin closed 8 years ago

rjcorwin commented 10 years ago

When below zero is reached, the temperature jumps to -127 with the current conversion.

@TimPietrusky Your code uses the same conversion.

Any idea on how to fix this? Reading the raw values I'm not sure what the trick is.

jwyse commented 8 years ago

Here's how I fixed it. I arrived at this after decompiling the Windows app that shipped with the device, hacking at it until I got it to work, and then trying to optimize and simplify it. Note that the manufacturer's Windows app even has a bug -- it returns an error when reading temps just under zero (between 0.0C and -1.0C, if I remember correctly -- whenever the high bit == 255). This is fixed in the following code:

exports.toDegreeCelcius = function(hiByte, loByte) { if ((hiByte & 128) == 128) { return -((256-hiByte) + (1 + ~(loByte >> 4)) / 16.0); } return hiByte + ((loByte >> 4) / 16.0); }

I've tested with the full range of possible high+low bytes (0-255), and the results appear to be correct. There's probably a more concise way of representing this, but I haven't had much experience with bitwise operations like this before.

asmuelle commented 8 years ago

Thanks, will incorporate your fix as soon as possible!

jwyse commented 8 years ago

I have submitted a pull request that contains this fix, as well as toDegreeFahrenheit() and a handful of other minor changes.