-
Notifications
You must be signed in to change notification settings - Fork 0
/
mlx90614.py
76 lines (65 loc) · 2.16 KB
/
mlx90614.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from time import sleep
class MLX90614():
# RAM offsets with 16-bit data, MSB first
# Raw data IR channel 1
MLX90614_RAWIR1 = 0x04
# Raw data IR channel 2
MLX90614_RAWIR2 = 0x05
# Ambient temperature
MLX90614_TA = 0x06
# Object 1 temperature
MLX90614_TOBJ1 = 0x07
# Object 2 temperature
MLX90614_TOBJ2 = 0x08
# EEPROM offsets with 16-bit data, MSB first
# Object temperature max register
MLX90614_TOMAX = 0x20
# Object temperature min register
MLX90614_TOMIN = 0x21
# PWM configuration register
MLX90614_PWMCTRL = 0x22
# Ambient temperature register
MLX90614_TARANGE = 0x23
# Emissivity correction register
MLX90614_EMISS = 0x24
# Configuration register
MLX90614_CONFIG = 0x25
# Slave address register
MLX90614_ADDR = 0x2E
# 1 ID register (read-only)
MLX90614_ID1 = 0x3C
# 2 ID register (read-only)
MLX90614_ID2 = 0x3D
# 3 ID register (read-only)
MLX90614_ID3 = 0x3E
# 4 ID register (read-only)
MLX90614_ID4 = 0x3F
comm_retries = 5
comm_sleep_amount = 0.1
def __init__(self, bus, address=0x5A):
self.bus = bus
self.address = address
def read_reg(self, reg_addr):
err = None
for i in range(self.comm_retries):
try:
return self.bus.read_word_data(self.address, reg_addr)
except IOError as e:
err = e
# "Rate limiting" - sleeping to prevent problems with sensor
# when requesting data too quickly
sleep(self.comm_sleep_amount)
# By this time, we made a couple requests and the sensor didn't respond
# (judging by the fact we haven't returned from this function yet)
# So let's just re-raise the last IOError we got
raise err
def read_temp(self, reg):
data = self.read_reg(reg)
temp = (data * 0.02) - 273.15
return temp
def get_ambient(self):
return self.read_temp(self.MLX90614_TA)
def get_object_1(self):
return self.read_temp(self.MLX90614_TOBJ1)
def get_object_2(self):
return self.read_temp(self.MLX90614_TOBJ2)