timofurrer / w1thermsensor

A Python package and CLI tool to work with w1 temperature sensors like DS1822, DS18S20 & DS18B20 on the Raspberry Pi, Beagle Bone and other devices.
MIT License
493 stars 113 forks source link

Parse raw bytes to get max. resolution #51

Closed Jakeler closed 5 years ago

Jakeler commented 5 years ago

On full 12 bit resolution the steps are 1/16 °C = 0.0625 °C, that is a problem with the milli celsius output calculated from the kernel module. It will simply cut off the last digit and therefore distort the output. For example 19.8125 becomes just 19.812. This PR changes the data source to the raw bytes and calculates the temperature in the module itself, to preserve full precision on 12 bit.

The calculation works on the following types, all with the same 9-12bit memory layout: https://datasheets.maximintegrated.com/en/ds/DS18B20.pdf https://datasheets.maximintegrated.com/en/ds/DS1822.pdf https://datasheets.maximintegrated.com/en/ds/DS1825.pdf https://datasheets.maximintegrated.com/en/ds/DS28EA00.pdf These have all the same 85 °C reset value, that check is then also enabled on them.

The DS18S20 is different, provides only 9bit in the usual registers, but it is possible to get some more data from other registers: https://datasheets.maximintegrated.com/en/ds/DS18S20.pdf The MAX31850 has a bit different register ordering, otherwise similar to the first 4 types: https://cdn-shop.adafruit.com/datasheets/MAX31850-MAX31851.pdf So for the DS18S20 and MAX31850 it uses still the old millicelsius method.

Also i have added a get_precision method, which reads and parses the currently set resolution from the sensor memory. In addition it would be pretty easy now to add read functionality for the user data/alarm registers, but i think that is not to relevant for most users.

codecov-io commented 5 years ago

Codecov Report

Merging #51 into master will increase coverage by 0.23%. The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #51      +/-   ##
==========================================
+ Coverage   96.46%   96.69%   +0.23%     
==========================================
  Files           6        6              
  Lines         226      242      +16     
==========================================
+ Hits          218      234      +16     
  Misses          8        8
Impacted Files Coverage Δ
w1thermsensor/core.py 96.92% <100%> (+0.43%) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 094bea5...a6bb8ee. Read the comment docs.

timofurrer commented 5 years ago

Looks great, thanks! :tada: