]> sigrok.org Git - libsigrokdecode.git/blob - decoders/mlx90614/pd.py
1f627e6adf755c63051a1bb853ba30ab583dd6fe
[libsigrokdecode.git] / decoders / mlx90614 / pd.py
1 ##
2 ## This file is part of the libsigrokdecode 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, see <http://www.gnu.org/licenses/>.
18 ##
19
20 import sigrokdecode as srd
21
22 class Decoder(srd.Decoder):
23     api_version = 3
24     id = 'mlx90614'
25     name = 'MLX90614'
26     longname = 'Melexis MLX90614'
27     desc = 'Infrared Thermometer protocol.'
28     license = 'gplv2+'
29     inputs = ['i2c']
30     outputs = ['mlx90614']
31     annotations = (
32         ('celsius', 'Temperature in degrees Celsius'),
33         ('kelvin', 'Temperature in Kelvin'),
34     )
35
36     def __init__(self):
37         self.state = 'IGNORE START REPEAT'
38         self.data = []
39
40     def start(self):
41         self.out_ann = self.register(srd.OUTPUT_ANN)
42
43     def putx(self, data):
44         self.put(self.ss, self.es, self.out_ann, data)
45
46     # Quick hack implementation! This needs to be improved a lot!
47     def decode(self, ss, es, data):
48         cmd, databyte = data
49
50         # State machine.
51         if self.state == 'IGNORE START REPEAT':
52             if cmd != 'START REPEAT':
53                 return
54             self.state = 'IGNORE ADDRESS WRITE'
55         elif self.state == 'IGNORE ADDRESS WRITE':
56             if cmd != 'ADDRESS WRITE':
57                 return
58             self.state = 'GET TEMPERATURE'
59         elif self.state == 'GET TEMPERATURE':
60             if cmd != 'DATA WRITE':
61                 return
62             if len(self.data) == 0:
63                 self.data.append(databyte)
64                 self.ss = ss
65             elif len(self.data) == 1:
66                 self.data.append(databyte)
67                 self.es = es
68             else:
69                 kelvin = (self.data[0] | (self.data[1] << 8)) * 0.02
70                 celsius = kelvin - 273.15
71                 self.putx([0, ['Temperature: %3.2f °C' % celsius]])
72                 self.putx([1, ['Temperature: %3.2f K' % kelvin]])
73                 self.state = 'IGNORE START REPEAT'
74                 self.data = []