diff --git a/ow-temp.c b/ow-temp.c index 37721dc..c8a51a9 100644 --- a/ow-temp.c +++ b/ow-temp.c @@ -24,7 +24,9 @@ short ow_temp_read(unsigned char* devid, unsigned char format) { unsigned char data[8]; - short rawtemp; + static int lasttemp=-32767; + int newtemp; + int rawtemp; unsigned char i; if (!ow_temp_idcheck(devid)) @@ -40,17 +42,27 @@ short ow_temp_read(unsigned char* devid, unsigned char format) for (i=0; i<8; i++) data[i]=ow_readbyte(); - rawtemp=(short)(data[0]+((int)data[1]<<8)); + rawtemp=((int)data[0]+((int)data[1]<<8)); errno=0; switch (format) { case OW_TEMP_FAHRENHEIT: - return (rawtemp*9+8)/80+32; + newtemp=(rawtemp*9+8)/80+32; + break; case OW_TEMP_CELSIUS: - return (rawtemp+8)>>4; + newtemp=(rawtemp+8)>>4; + break; case OW_TEMP_RAW: default: - return rawtemp; + return (short)rawtemp; + } + /* 17 Aug 08: filter out garbage readings */ + if ((newtemp-lasttemp>3 || newtemp-lasttemp<-3) && lasttemp!=-32767) + return (short)lasttemp; + else + { + lasttemp=newtemp; + return (short)newtemp; } }