[docs]defrh_dwyer(raw_value):"""Returns Dwyer sensor relative humidity (RH) from a raw register value. Range is 0-100%. """# Humidity linear calibration = 100 / (2^15 - 1)RH0=0.0RHs=100.0/(2**15-1)return(RH0+RHs*float(raw_value),"percent")
[docs]deft_dwyer(raw_value):"""Returns Dwyer sensor temperature from a raw register value. Range is -30C to +70C. """# Temperature linear calibration = 100 / (2^15 - 1)T0=-30.0Ts=100.0/(2**15-1)return(T0+Ts*float(raw_value),"degC")
[docs]defrtd(raw_value):"""Converts platinum RTD (resistance thermometer) output to degrees C. The temperature resolution is 0.1C per ADU, and the temperature range is -273C to +850C. The 16-bit digital number wraps below 0C to 2^16-1 ADU. This handles that conversion. """tempRes=0.1# Module resolution is 0.1C per ADUtempMax=850.0# Maximum temperature for a Pt RTD in deg CwrapT=tempRes*(2**16-1)# ADU wrap at 0C to 2^16-1temp=tempRes*raw_valueiftemp>tempMax:temp-=wrapTreturn(temp,"degC")
[docs]defrtd10(raw_value):"""Convert platinum RTD output to degrees C. The conversion is simply ``0.1 * raw_value``. """return(float(raw_value)/10.0,"degC")
[docs]defvoltage(raw_value,v_min=0,v_max=10,res=32760,gain=1):"""Converts a raw value to a voltage measurement. ``V = raw_value / res * (v_max - v_min) * gain`` """return(float(raw_value)/res*(v_max-v_min)*gain,"V")
[docs]deflinear(raw_value,min,max,range_min,range_max,unit=None):"""A general adaptor for a linear sensor. ``M = min + raw_value / (range_max - range_min) * (max - min)`` """return(min+float(raw_value-range_min)/(range_max-range_min)*(max-min),unit,)
[docs]defproportional(raw_value,factor,unit=None):"""Applies a proportional factor."""return(float(raw_value)*factor,unit)
[docs]defpwd(raw_value,unit=None):"""Pulse Width Modulator (PWM) output. The register is a 16-bit word as usual, but the module does not use the 5 LSBs. So, 10-bit resolution on the PWM value (MSB is for sign and is fixed). 0-100% duty cycle maps to 0 to 32736 decimal value in the register. ``PWD = 100 * raw_value / (2**15 - 1)`` """return(100*float(raw_value)/(2**15-1),unit)