Added test, fixed typo

This commit is contained in:
Conr86
2019-06-22 17:15:44 +10:00
parent 75457cdad8
commit e7cb94e364
3 changed files with 63 additions and 49 deletions

View File

@@ -11,24 +11,26 @@ from .crc import Crc8
DEVICE_BUS = 1
BASELINE_FILENAME = os.path.expanduser("~/.sgp_config_data.txt")
class _cmds():
"""container class for mapping between human readable names and the command values used by the sgp"""
Sgp30Cmd = namedtuple("Sgp30Cmd",["commands","replylen","waittime"])
IAQ_INIT=Sgp30Cmd([0x20, 0x03],0,10)
IAQ_MEASURE=Sgp30Cmd([0x20, 0x08],6,12)
GET_BASELINE=Sgp30Cmd([0x20, 0x15],6,120)
SET_BASELINE=Sgp30Cmd([0x20, 0x1e],0,10)
SET_HUMIDITY=Sgp30Cmd([0x20, 0x61],0,10)
IAQ_SELFTEST=Sgp30Cmd([0x20, 0x32],3,520)
GET_FEATURES=Sgp30Cmd([0x20, 0x2f],3,3)
GET_SERIAL=Sgp30Cmd([0x36, 0x82],9,10)
SGP30Cmd = namedtuple("SGP30Cmd", ["commands", "replylen", "waittime"])
IAQ_INIT = SGP30Cmd([0x20, 0x03], 0, 10)
IAQ_MEASURE = SGP30Cmd([0x20, 0x08], 6, 12)
GET_BASELINE = SGP30Cmd([0x20, 0x15], 6, 120)
SET_BASELINE = SGP30Cmd([0x20, 0x1e], 0, 10)
SET_HUMIDITY = SGP30Cmd([0x20, 0x61], 0, 10)
IAQ_SELFTEST = SGP30Cmd([0x20, 0x32], 3, 520)
GET_FEATURES = SGP30Cmd([0x20, 0x2f], 3, 3)
GET_SERIAL = SGP30Cmd([0x36, 0x82], 9, 10)
@classmethod
def new_set_baseline(cls, baseline_data):
cmd = cls.SET_BASELINE
return cls.Sgp30Cmd(cmd.commands +baseline_data,cmd.replylen,cmd.waittime)
return cls.SGP30Cmd(cmd.commands + baseline_data, cmd.replylen, cmd.waittime)
class Sgp30():
class SGP30():
def __init__(self, bus, device_address=0x58, baseline_filename=BASELINE_FILENAME):
self._bus = bus
@@ -37,7 +39,7 @@ class Sgp30():
self._last_save_time = time()
self._baseline_filename = baseline_filename
Sgp30Answer = namedtuple("Sgp30Answer",["data","raw","crc_ok"])
SGP30Answer = namedtuple("SGP30Answer", ["data", "raw", "crc_ok"])
def _raw_validate_crc(s, r):
a = list(zip(r[0::3], r[1::3]))
@@ -56,7 +58,7 @@ class Sgp30():
r = list(read)
crc_ok, a = self._raw_validate_crc(r)
answer = [i << 8 | j for i, j in a]
return self.Sgp30Answer(answer,r,crc_ok)
return self.SGP30Answer(answer, r, crc_ok)
def store_baseline(self):
with open(self._baseline_filename, "w") as conf:
@@ -101,7 +103,7 @@ class Sgp30():
#print("Initializing SGP30")
self.read_write(_cmds.IAQ_INIT)
def i2c_geral_call(self):
def i2c_general_call(self):
"""This attempts to reset _ALL_ devices on the i2c buss
This command issues the i2c-general call RW command that should result
@@ -117,14 +119,15 @@ class Sgp30():
def main():
with SMBusWrapper(1) as bus:
sgp=Sgp30(bus,baseline_filename=BASELINE_FILENAME+".TESTING")
sgp = SGP30(bus, baseline_filename=BASELINE_FILENAME+".TESTING")
print("resetting all i2c devices")
sgp.i2c_geral_call()
sgp.i2c_general_call()
print(sgp.read_features())
print(sgp.read_serial())
sgp.init_sgp()
print(sgp.read_measurements())
bus.close()
if __name__ == "__main__":
main()

11
test.py Normal file
View File

@@ -0,0 +1,11 @@
from smbus2 import SMBusWrapper
from sgp30 import Sgp30
import time
with SMBusWrapper(1) as bus:
sgp=Sgp30(bus)
print("resetting all i2c devices")
sgp.i2c_geral_call()
print(sgp.read_features())
print(sgp.read_serial())
sgp.init_sgp()
print(sgp.read_measurements())