16
16
17
17
**Hardware:**
18
18
19
- * `Adafruit AS7341 Breakout <https:// www.adafruit.com/products/45XX>`_
19
+ * `Adafruit AS7341 Breakout <https:# www.adafruit.com/products/45XX>`_
20
20
21
21
**Software and Dependencies:**
22
22
23
23
* Adafruit CircuitPython firmware for the supported boards:
24
- https:// github.com/adafruit/circuitpython/releases
24
+ https:# github.com/adafruit/circuitpython/releases
25
25
26
- * Adafruit's Bus Device library: https:// github.com/adafruit/Adafruit_CircuitPython_BusDevice
27
- * Adafruit's Register library: https:// github.com/adafruit/Adafruit_CircuitPython_Register
26
+ * Adafruit's Bus Device library: https:# github.com/adafruit/Adafruit_CircuitPython_BusDevice
27
+ * Adafruit's Register library: https:# github.com/adafruit/Adafruit_CircuitPython_Register
28
28
29
29
"""
30
30
31
31
__version__ = "0.0.0-auto.0"
32
- __repo__ = "https:// github.com/adafruit/Adafruit_CircuitPython_MS8607.git"
32
+ __repo__ = "https:# github.com/adafruit/Adafruit_CircuitPython_MS8607.git"
33
33
34
34
35
35
from struct import unpack_from
36
36
from time import sleep
37
37
from micropython import const
38
38
import adafruit_bus_device .i2c_device as i2c_device
39
39
40
- # // HSENSOR device commands
40
+ # # HSENSOR device commands
41
41
_MS8607_RESET_COMMAND = const (0xFE ) #
42
42
_MS8607_READ_HUMIDITY_W_HOLD_COMMAND = const (0xE5 ) #
43
43
_MS8607_READ_HUMIDITY_WO_HOLD_COMMAND = const (0xF5 ) #
47
47
_MS8607_READ_USER_REG_COMMAND = const (0xE7 ) #
48
48
49
49
_MS8607_HSENSOR_ADDR = const (0x40 ) #
50
+ _MS8607_COEFF_MUL = const (125 ) #
51
+ _MS8607_COEFF_ADD = const (- 6 ) #
50
52
51
53
# enum MS8607_humidity_resolution
52
54
# {
53
55
# MS8607_humidity_resolution_12b = 0,
54
56
# MS8607_humidity_resolution_8b,
55
57
# MS8607_humidity_resolution_10b,
56
58
# MS8607_humidity_resolution_11b
57
- # }; ]
59
+ # }]
58
60
59
61
60
62
class MS8607Humidity :
@@ -81,50 +83,67 @@ def relative_humidity(self):
81
83
self ._buffer [0 ] = _MS8607_READ_HUMIDITY_WO_HOLD_COMMAND
82
84
with self .i2c_device as i2c :
83
85
i2c .write (self ._buffer , end = 1 )
84
- sleep (0.1 ) # _i2cPort->requestFrom((uint8_t)MS8607_HSENSOR_ADDR, 3U);
86
+ sleep (0.1 ) # _i2cPort->requestFrom((uint8_t)MS8607_HSENSOR_ADDR, 3U)
85
87
86
88
with self .i2c_device as i2c :
87
89
i2c .readinto (self ._buffer , end = 3 )
88
90
89
- # _adc = (buffer[0] << 8) | buffer[1];
90
- # crc = buffer[2];
91
- raw_humidity = unpack_from (">H" , self ._buffer )
91
+ # _adc = (buffer[0] << 8) | buffer[1]
92
+ # crc = buffer[2]
93
+ raw_humidity = unpack_from (">H" , self ._buffer )[0 ]
94
+ crc_value = unpack_from (">B" , self ._buffer , offset = 2 )[0 ]
95
+ humidity = (raw_humidity * (_MS8607_COEFF_MUL / (1 << 16 ))) + _MS8607_COEFF_ADD
96
+ if not self ._check_humidity_crc (raw_humidity , crc_value ):
97
+ raise RuntimeError ("CRC Error reading humidity data" )
98
+ return humidity
92
99
93
- # *humidity =
94
- # adc * HUMIDITY_COEFF_MUL / (1UL << 16) + HUMIDITY_COEFF_ADD;
95
- return raw_humidity
96
- # // compute CRC
97
- # uint8_t crc;
98
- # status = hsensor_crc_check(_adc, crc);
99
- # if (status != MS8607_status_ok)
100
- # return status;
100
+ # *adc = _adc
101
101
102
- # *adc = _adc;
102
+ # return status
103
103
104
- # return status;
104
+ @staticmethod
105
+ def _check_humidity_crc (value , crc ):
106
+ polynom = 0x988000 # x^8 + x^5 + x^4 + 1
107
+ msb = 0x800000
108
+ mask = 0xFF8000
109
+ result = value << 8 # Pad with zeros as specified in spec
110
+
111
+ while msb != 0x80 :
112
+ # Check if msb of current value is 1 and apply XOR mask
113
+ if result & msb :
114
+ result = ((result ^ polynom ) & mask ) | (result & ~ mask )
115
+
116
+ # Shift by one
117
+ msb >>= 1
118
+ mask >>= 1
119
+ polynom >>= 1
120
+
121
+ if result == crc :
122
+ return True
123
+ return False
105
124
106
125
107
126
# read_temperature_pressure_humidity(float *t, float *p,
108
127
109
- # set_humidity_resolution(enum MS8607_humidity_resolution res);
110
- # void set_humidity_i2c_master_mode(enum MS8607_humidity_i2c_master_mode mode);
128
+ # set_humidity_resolution(enum MS8607_humidity_resolution res)
129
+ # void set_humidity_i2c_master_mode(enum MS8607_humidity_i2c_master_mode mode)
111
130
112
- # get_compensated_humidity(float temperature, float relative_humidity, float *compensated_humidity);
131
+ # get_compensated_humidity(float temperature, float relative_humidity, float *compensated_humidity)
113
132
114
133
# /*
115
134
# private:
116
- # bool hsensor_is_connected(void);
117
- # hsensor_reset(void);
118
- # hsensor_crc_check(uint16_t value, uint8_t crc);
119
- # hsensor_read_user_register(uint8_t *value);
120
- # hsensor_write_user_register(uint8_t value);
121
- # enum MS8607_humidity_i2c_master_mode hsensor_i2c_master_mode;
122
- # hsensor_humidity_conversion_and_read_adc(uint16_t *adc);
123
- # hsensor_read_relative_humidity(float *humidity);
124
- # int err = set_humidity_resolution(MS8607_humidity_resolution_12b); // 12 bits
125
- # hsensor_conversion_time = HSENSOR_CONVERSION_TIME_12b;
126
- # hsensor_i2c_master_mode = MS8607_i2c_no_hold;
127
- # float humidity = getHumidity();
128
- # float temperature = getTemperature();
129
- # float compensated_RH= get_compensated_humidity(temperature, humidity, &compensated_RH);
130
- # float dew_point = get_dew_point(temperature, humidity, &dew_point);
135
+ # bool hsensor_is_connected(void)
136
+ # hsensor_reset(void)
137
+ # hsensor_crc_check(uint16_t value, uint8_t crc)
138
+ # hsensor_read_user_register(uint8_t *value)
139
+ # hsensor_write_user_register(uint8_t value)
140
+ # enum MS8607_humidity_i2c_master_mode hsensor_i2c_master_mode
141
+ # hsensor_humidity_conversion_and_read_adc(uint16_t *adc)
142
+ # hsensor_read_relative_humidity(float *humidity)
143
+ # int err = set_humidity_resolution(MS8607_humidity_resolution_12b) # 12 bits
144
+ # hsensor_conversion_time = HSENSOR_CONVERSION_TIME_12b
145
+ # hsensor_i2c_master_mode = MS8607_i2c_no_hold
146
+ # float humidity = getHumidity()
147
+ # float temperature = getTemperature()
148
+ # float compensated_RH= get_compensated_humidity(temperature, humidity, &compensated_RH)
149
+ # float dew_point = get_dew_point(temperature, humidity, &dew_point)
0 commit comments