Adds sleep-support to command struct
It is cleaner to store the needed wait-time with the struct than to passit to the read-write function. This change mostly acopmplieshs that
This commit is contained in:
34
sgptest.py
34
sgptest.py
@@ -14,21 +14,27 @@ DEVICE_ADDR = 0x58
|
||||
#print bus.write_byte_datadata(DEVICE_ADDR, 0x00, 0x01)
|
||||
#self._i2c_read_words_from_cmd([0x36, 0x82], 0.01, 3)
|
||||
|
||||
Sgp30Cmd = namedtuple("Sgp30Cmd",["commands","replylen"])
|
||||
GET_SERIAL=Sgp30Cmd([0x36, 0x82],6)
|
||||
GET_FEATURES=Sgp30Cmd([0x20, 0x2f],2)
|
||||
IAQ_INIT=Sgp30Cmd([0x20, 0x03],0)
|
||||
IAQ_MEASURE=Sgp30Cmd([0x20, 0x08],6)
|
||||
IAQ_SELFTEST=Sgp30Cmd([0x20, 0x32],3)
|
||||
Sgp30Cmd = namedtuple("Sgp30Cmd",["commands","replylen","waittime"])
|
||||
GET_SERIAL=Sgp30Cmd([0x36, 0x82],6,10)
|
||||
GET_FEATURES=Sgp30Cmd([0x20, 0x15],2,2)
|
||||
IAQ_INIT=Sgp30Cmd([0x20, 0x03],0,10)
|
||||
IAQ_MEASURE=Sgp30Cmd([0x20, 0x08],6,12)
|
||||
IAQ_SELFTEST=Sgp30Cmd([0x20, 0x32],3,520)
|
||||
GET_BASELINE=Sgp30Cmd([0x20, 0x15],6,120)
|
||||
SET_BASELINE=Sgp30Cmd([0x20, 0x1e],0,10)
|
||||
|
||||
def read_write(cmd,bus,addr=DEVICE_ADDR,sleep_time=0):
|
||||
bus.i2c_rdwr(i2c_msg.write(addr,cmd.commands))
|
||||
if cmd.replylen >0 :
|
||||
b=i2c_msg.read(addr,cmd.replylen)
|
||||
sleep(sleep_time)
|
||||
bus.i2c_rdwr(b)
|
||||
r=list(b)
|
||||
crc=r[2::3]
|
||||
Sgp30Answer = namedtuple("Sgp30Answer",["data","raw"])
|
||||
|
||||
def read_write(cmd,bus,addr=DEVICE_ADDR):
|
||||
write = i2c_msg.write(addr,cmd.commands)
|
||||
if cmd.replylen <= 0 :
|
||||
bus.i2c_rdwr(write)
|
||||
else:
|
||||
read = i2c_msg.read(addr,cmd.replylen)
|
||||
bus.i2c_rdwr(write)
|
||||
sleep(waittime/1000.0)
|
||||
bus.i2c_rdwr(read)
|
||||
r = list(read)
|
||||
answer = [i<<8 | j for i,j in zip(r[0::3],r[1::3])]
|
||||
return answer
|
||||
|
||||
|
Reference in New Issue
Block a user