X-Git-Url: https://sigrok.org/gitweb/?p=libsigrokdecode.git;a=blobdiff_plain;f=decoders%2Flm75%2Fpd.py;h=14df1b52e9a704bfc3a72981b9a11fc0ab38aa70;hp=8e447b734f040ccc31b51d484ae3e921036b0da5;hb=HEAD;hpb=8915b34659332288aab38780d8f10d75c4c83e7f diff --git a/decoders/lm75/pd.py b/decoders/lm75/pd.py index 8e447b7..d59fa5f 100644 --- a/decoders/lm75/pd.py +++ b/decoders/lm75/pd.py @@ -14,12 +14,9 @@ ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License -## along with this program; if not, write to the Free Software -## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +## along with this program; if not, see . ## -# National LM75 (and compatibles) temperature sensor protocol decoder - # TODO: Better support for various LM75 compatible devices. import sigrokdecode as srd @@ -42,66 +39,54 @@ ft = { } class Decoder(srd.Decoder): - api_version = 1 + api_version = 3 id = 'lm75' name = 'LM75' longname = 'National LM75' desc = 'National LM75 (and compatibles) temperature sensor.' license = 'gplv2+' inputs = ['i2c'] - outputs = ['lm75'] - probes = [] - optional_probes = [ - {'id': 'os', 'name': 'OS', 'desc': 'Overtemperature shutdown'}, - {'id': 'a0', 'name': 'A0', 'desc': 'I2C slave address input 0'}, - {'id': 'a1', 'name': 'A1', 'desc': 'I2C slave address input 1'}, - {'id': 'a2', 'name': 'A2', 'desc': 'I2C slave address input 2'}, - ] - options = { - 'sensor': ['Sensor type', 'lm75'], - 'resolution': ['Resolution', 9], # 9-12 bit, sensor/config dependent - } - annotations = [ - ['Celsius', 'Temperature in degrees Celsius'], - ['Kelvin', 'Temperature in Kelvin'], - ['Text (verbose)', 'Human-readable text (verbose)'], - ['Text', 'Human-readable text'], - ['Warnings', 'Human-readable warnings'], - ] - - def __init__(self, **kwargs): + outputs = [] + tags = ['Sensor'] + options = ( + {'id': 'sensor', 'desc': 'Sensor type', 'default': 'lm75', + 'values': ('lm75',)}, + {'id': 'resolution', 'desc': 'Resolution (bits)', 'default': 9, + 'values': (9, 10, 11, 12)}, + ) + annotations = ( + ('celsius', 'Temperature / °C'), + ('kelvin', 'Temperature / Kelvin'), + ('text-verbose', 'Text (verbose)'), + ('text', 'Text'), + ('warning', 'Warning'), + ) + + def __init__(self): + self.reset() + + def reset(self): self.state = 'IDLE' self.reg = 0x00 # Currently selected register self.databytes = [] - self.mintemp = 0 - self.maxtemp = 0 - self.avgvalues = [] def start(self): - # self.out_proto = self.add(srd.OUTPUT_PROTO, 'lm75') - self.out_ann = self.add(srd.OUTPUT_ANN, 'lm75') - - def report(self): - # TODO: print() or self.put() or return xyz, or... ? - avg = sum(self.avgvalues) / len(self.avgvalues) - temperatures = (self.mintemp, self.maxtemp, avg) - # TODO: Configurable report() output, e.g. for Kelvin. - return 'Min/max/avg temperature: %f/%f/%f °C' % temperatures + self.out_ann = self.register(srd.OUTPUT_ANN) def putx(self, data): - # Helper for annotations which span exactly one I2C packet. + # Helper for annotations which span exactly one I²C packet. self.put(self.ss, self.es, self.out_ann, data) def putb(self, data): - # Helper for annotations which span a block of I2C packets. - self.put(self.block_start, self.block_end, self.out_ann, data) + # Helper for annotations which span a block of I²C packets. + self.put(self.ss_block, self.es_block, self.out_ann, data) def warn_upon_invalid_slave(self, addr): - # LM75 and compatible devices have a 7-bit I2C slave address where + # LM75 and compatible devices have a 7-bit I²C slave address where # the 4 MSBs are fixed to 1001, and the 3 LSBs are configurable. # Thus, valid slave addresses (1001xxx) range from 0x48 to 0x4f. if addr not in range(0x48, 0x4f + 1): - s = 'Warning: I2C slave 0x%02x not an LM75 compatible sensor.' + s = 'Warning: I²C slave 0x%02x not an LM75 compatible sensor.' self.putx([4, [s % addr]]) def output_temperature(self, s, rw): @@ -117,21 +102,14 @@ class Decoder(srd.Decoder): s = 'Warning: The temperature register is read-only!' self.putb([4, [s]]) - # Keep some statistics. Can be output in report(), for example. - if celsius < self.mintemp: - self.mintemp = celsius - if celsius > self.maxtemp: - self.maxtemp = celsius - self.avgvalues.append(celsius) - def handle_temperature_reg(self, b, s, rw): # Common helper for the temperature/T_HYST/T_OS registers. if len(self.databytes) == 0: - self.block_start = self.ss + self.ss_block = self.ss self.databytes.append(b) return self.databytes.append(b) - self.block_end = self.es + self.es_block = self.es self.output_temperature(s, rw) self.databytes = [] @@ -182,12 +160,12 @@ class Decoder(srd.Decoder): def decode(self, ss, es, data): cmd, databyte = data - # Store the start/end samples of this I2C packet. + # Store the start/end samples of this I²C packet. self.ss, self.es = ss, es # State machine. if self.state == 'IDLE': - # Wait for an I2C START condition. + # Wait for an I²C START condition. if cmd != 'START': return self.state = 'GET SLAVE ADDR' @@ -206,6 +184,3 @@ class Decoder(srd.Decoder): else: # self.putx([0, ['Ignoring: %s (data=%s)' % (cmd, databyte)]]) pass - else: - raise Exception('Invalid state: %s' % self.state) -