]> sigrok.org Git - libsigrokdecode.git/blob - decoders/mlx90614/pd.py
mlx90614: "Kelvin", not "degrees Kelvin".
[libsigrokdecode.git] / decoders / mlx90614 / pd.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 = 'Infrared Thermometer protocol.'
31     license = 'gplv2+'
32     inputs = ['i2c']
33     outputs = ['mlx90614']
34     probes = []
35     optional_probes = []
36     options = {}
37     annotations = [
38         ['Celsius', 'Temperature in degrees Celsius'],
39         ['Kelvin', 'Temperature in Kelvin'],
40     ]
41
42     def __init__(self, **kwargs):
43         self.state = 'IGNORE START REPEAT'
44         self.data = []
45
46     def start(self, metadata):
47         # self.out_proto = self.add(srd.OUTPUT_PROTO, 'mlx90614')
48         self.out_ann = self.add(srd.OUTPUT_ANN, 'mlx90614')
49
50     def report(self):
51         pass
52
53     def putx(self, data):
54         self.put(self.ss, self.es, self.out_ann, data)
55
56     # Quick hack implementation! This needs to be improved a lot!
57     def decode(self, ss, es, data):
58         cmd, databyte = data
59
60         # State machine.
61         if self.state == 'IGNORE START REPEAT':
62             if cmd != 'START REPEAT':
63                 return
64             self.state = 'IGNORE ADDRESS WRITE'
65         elif self.state == 'IGNORE ADDRESS WRITE':
66             if cmd != 'ADDRESS WRITE':
67                 return
68             self.state = 'GET TEMPERATURE'
69         elif self.state == 'GET TEMPERATURE':
70             if cmd != 'DATA WRITE':
71                 return
72             if len(self.data) == 0:
73                 self.data.append(databyte)
74                 self.ss = ss
75             elif len(self.data) == 1:
76                 self.data.append(databyte)
77                 self.es = es
78             else:
79                 kelvin = (self.data[0] | (self.data[1] << 8)) * 0.02
80                 celsius = kelvin - 273.15
81                 self.putx([0, ['Temperature: %3.2f °C' % celsius]])
82                 self.putx([1, ['Temperature: %3.2f K' % kelvin]])
83                 self.state = 'IGNORE START REPEAT'
84                 self.data = []
85         else:
86             raise Exception('Invalid state: %s' % self.state)
87