@@ -22,8 +22,18 @@ class SI7006A20:
22
22
23
23
SI7006A20_I2C_ADDR = const (0x40 )
24
24
25
+ # I2C commands
25
26
TEMP_NOHOLDMASTER = const (0xF3 )
26
27
HUMD_NOHOLDMASTER = const (0xF5 )
28
+ WRITE_USER_REG1 = const (0xE6 )
29
+ READ_USER_REG1 = const (0xE7 )
30
+ WRITE_HEATER_CTRL_REG = const (0x51 )
31
+ READ_HEATER_CTRL_REG = const (0x11 )
32
+
33
+ # Register masks and offsets
34
+ USER_REG1_HTR_ENABLE_MASK = const (0b00000100 )
35
+ USER_REG1_HTR_ENABLE_OFFSET = const (0x02 )
36
+ HTR_CTRL_REG_MASK = const (0b00001111 )
27
37
28
38
def __init__ (self , pysense = None , sda = 'P22' , scl = 'P21' ):
29
39
if pysense is not None :
@@ -55,18 +65,32 @@ def humidity(self):
55
65
56
66
def read_user_reg (self ):
57
67
""" reading the user configuration register """
58
- self .i2c .writeto (SI7006A20_I2C_ADDR , bytearray ([0xE7 ]))
68
+ self .i2c .writeto (SI7006A20_I2C_ADDR , bytearray ([READ_USER_REG1 ]))
59
69
time .sleep (0.5 )
60
70
data = self .i2c .readfrom (SI7006A20_I2C_ADDR , 1 )
61
71
return data [0 ]
62
72
63
73
def read_heater_reg (self ):
64
74
""" reading the heater configuration register """
65
- self .i2c .writeto (SI7006A20_I2C_ADDR , bytearray ([0x11 ]))
75
+ self .i2c .writeto (SI7006A20_I2C_ADDR , bytearray ([READ_HEATER_CTRL_REG ]))
66
76
time .sleep (0.5 )
67
77
data = self .i2c .readfrom (SI7006A20_I2C_ADDR , 1 )
68
78
return data [0 ]
69
79
80
+ def write_heater_reg (self , heater_value ):
81
+ """ writing the heater configuration register """
82
+ # We should only set the bottom four bits of this register
83
+ heater_setting = heater_value & HTR_CTRL_REG_MASK
84
+ self .write_reg (WRITE_HEATER_CTRL_REG , heater_setting )
85
+
86
+ def heater_control (self , on_off ):
87
+ """ turn the heater on or off """
88
+ # Get current settings for everything else
89
+ user_reg = self .read_user_reg ()
90
+ # Set the heater bit
91
+ user_reg = (user_reg & ~ USER_REG1_HTR_ENABLE_MASK ) | (on_off << USER_REG1_HTR_ENABLE_OFFSET )
92
+ self .write_reg (WRITE_USER_REG1 , user_reg )
93
+
70
94
def read_electronic_id (self ):
71
95
""" reading electronic identifier """
72
96
self .i2c .writeto (SI7006A20_I2C_ADDR , bytearray ([0xFA ]) + bytearray ([0x0F ]))
0 commit comments