]> sigrok.org Git - libsigrokdecode.git/blob - decoders/mlx90614/mlx90614.py
srd: I2C: change format to have ACK/NACK bits as separate events
[libsigrokdecode.git] / decoders / mlx90614 / mlx90614.py
1 ##
2 ## This file is part of the sigrok project.
3 ##
4 ## Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
5 ##
6 ## This program is free software; you can redistribute it and/or modify
7 ## it under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 2 of the License, or
9 ## (at your option) any later version.
10 ##
11 ## This program is distributed in the hope that it will be useful,
12 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 ## GNU General Public License for more details.
15 ##
16 ## You should have received a copy of the GNU General Public License
17 ## along with this program; if not, write to the Free Software
18 ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19 ##
20
21 # Melexis MLX90614 Infrared Thermometer protocol decoder
22
23 import sigrokdecode as srd
24
25 class Decoder(srd.Decoder):
26     api_version = 1
27     id = 'mlx90614'
28     name = 'MLX90614'
29     longname = 'Melexis MLX90614'
30     desc = 'TODO.'
31     longdesc = 'TODO.'
32     license = 'gplv2+'
33     inputs = ['i2c']
34     outputs = ['mlx90614']
35     probes = []
36     optional_probes = []
37     options = {}
38     annotations = [
39         ['Celsius', 'Temperature in degrees Celsius'],
40         ['Kelvin', 'Temperature in degrees Kelvin'],
41     ]
42
43     def __init__(self, **kwargs):
44         self.state = 'IGNORE START REPEAT'
45         self.data = []
46
47     def start(self, metadata):
48         # self.out_proto = self.add(srd.OUTPUT_PROTO, 'mlx90614')
49         self.out_ann = self.add(srd.OUTPUT_ANN, 'mlx90614')
50
51     def report(self):
52         pass
53
54     def putx(self, data):
55         self.put(self.ss, self.es, self.out_ann, data)
56
57     # Quick hack implementation! This needs to be improved a lot!
58     def decode(self, ss, es, data):
59         cmd, databyte = data
60
61         # State machine.
62         if self.state == 'IGNORE START REPEAT':
63             if cmd != 'START REPEAT':
64                 return
65             self.state = 'IGNORE ADDRESS WRITE'
66         elif self.state == 'IGNORE ADDRESS WRITE':
67             if cmd != 'ADDRESS WRITE':
68                 return
69             self.state = 'GET TEMPERATURE'
70         elif self.state == 'GET TEMPERATURE':
71             if len(self.data) == 0:
72                 self.data.append(databyte)
73                 self.ss = ss
74             elif len(self.data) == 1:
75                 self.data.append(databyte)
76                 self.es = es
77             else:
78                 kelvin = (self.data[0] | (self.data[1] << 8)) * 0.02
79                 celsius = kelvin - 273.15
80                 self.putx([0, ['Temperature: %3.2f °C' % celsius]])
81                 self.putx([1, ['Temperature: %3.2f °K' % kelvin]])
82                 self.state = 'IGNORE START REPEAT'
83                 self.data = []
84         else:
85             raise Exception('Invalid state: %d' % self.state)
86