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