48
48
_AGS02MA_CRC8_POLYNOMIAL = const (0x31 )
49
49
50
50
51
+ def _generate_crc (data ):
52
+ """8-bit CRC algorithm for checking data"""
53
+ crc = _AGS02MA_CRC8_INIT
54
+ # calculates 8-Bit checksum with given polynomial
55
+ for byte in data :
56
+ crc ^= byte
57
+ for _ in range (8 ):
58
+ if crc & 0x80 :
59
+ crc = (crc << 1 ) ^ _AGS02MA_CRC8_POLYNOMIAL
60
+ else :
61
+ crc <<= 1
62
+ crc &= 0xFF
63
+ return crc & 0xFF
64
+
65
+
51
66
class AGS02MA :
52
67
"""Driver for the AGS02MA air quality sensor
53
68
:param ~busio.I2C i2c_bus: The I2C bus the AGS02MA is connected to.
@@ -95,7 +110,7 @@ def set_address(self, new_addr):
95
110
0 ,
96
111
]
97
112
)
98
- _buf [5 ] = self . _generate_crc (_buf [1 :5 ])
113
+ _buf [5 ] = _generate_crc (_buf [1 :5 ])
99
114
with self .i2c_device as i2c :
100
115
i2c .write (_buf )
101
116
@@ -106,22 +121,8 @@ def _read_reg(self, addr, delayms):
106
121
time .sleep (delayms / 1000 )
107
122
i2c .readinto (self ._buf )
108
123
# print([hex(x) for x in self._buf])
109
- if self . _generate_crc (self ._buf ) != 0 :
124
+ if _generate_crc (self ._buf ) != 0 :
110
125
raise RuntimeError ("CRC check failed" )
111
126
val , crc = struct .unpack (">IB" , self ._buf ) # pylint: disable=unused-variable
112
127
# print(hex(val), hex(crc))
113
128
return val
114
-
115
- def _generate_crc (self , data ):
116
- """8-bit CRC algorithm for checking data"""
117
- crc = _AGS02MA_CRC8_INIT
118
- # calculates 8-Bit checksum with given polynomial
119
- for byte in data :
120
- crc ^= byte
121
- for _ in range (8 ):
122
- if crc & 0x80 :
123
- crc = (crc << 1 ) ^ _AGS02MA_CRC8_POLYNOMIAL
124
- else :
125
- crc <<= 1
126
- crc &= 0xFF
127
- return crc & 0xFF
0 commit comments