-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathscd30.py
More file actions
32 lines (32 loc) · 1.24 KB
/
scd30.py
File metadata and controls
32 lines (32 loc) · 1.24 KB
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
from microbit import i2c
import ustruct
class SCD30:
def __init__(self,addr=0x61):
self.addr,self.co2,self.t,self.h=addr,0,0,0
def sendCommand(self,command,argument=None):
if argument is None:buffer=bytearray([command>>8,command&0xff])
else:
buffer=bytearray(5)
buffer[0]=command>>8
buffer[1]=command&0xff
buffer[2]=argument>>8
buffer[3]=argument&0xff
buffer[4]=self.calculateCrc(buffer,2)
return i2c.write(self.addr,buffer)
def read_n_bytes(self,n_bytes):return i2c.read(self.addr,n_bytes)
def parseData(self,data):return ustruct.unpack('>f',ustruct.pack('>BBBB',data[0],data[1],data[2],data[3]))[0]
def readMeasurement(self):
self.sendCommand(0x0300)
data=i2c.read(self.addr,18)
self.co2,self.t,self.h=self.parseData(data[:6]),self.parseData(data[6:12]),self.parseData(data[12:18])
def setForcedRecalibration(self,co2ppm):
if co2ppm!=None:self.sendCommand(0x5204,co2ppm)
else:print('Please enter a valid calibration reference')
def calculateCrc(self,data,len):
crc=0xff
for byteCtr in range(len):
crc^=data[byteCtr]
for bit in range(8,0,-1):
if crc and 0x80:crc=(crc<<1)^0x31
else:crc=(crc<<1)
return crc