Reworked module

This commit is contained in:
Conr86
2019-06-22 17:35:11 +10:00
parent e7cb94e364
commit 4589b9b8cb

View File

@@ -9,8 +9,6 @@ import os.path
from .crc import Crc8 from .crc import Crc8
DEVICE_BUS = 1 DEVICE_BUS = 1
BASELINE_FILENAME = os.path.expanduser("~/.sgp_config_data.txt")
class _cmds(): class _cmds():
"""container class for mapping between human readable names and the command values used by the sgp""" """container class for mapping between human readable names and the command values used by the sgp"""
@@ -32,12 +30,12 @@ class _cmds():
class SGP30(): class SGP30():
def __init__(self, bus, device_address=0x58, baseline_filename=BASELINE_FILENAME): def __init__(self, bus, device_address=0x58, baseline=[]):
self._bus = bus self._bus = bus
self._device_addr = device_address self._device_addr = device_address
self._start_time = time() self._start_time = time()
self._last_save_time = time() self._last_save_time = time()
self._baseline_filename = baseline_filename self._baseline = baseline
SGP30Answer = namedtuple("SGP30Answer", ["data", "raw", "crc_ok"]) SGP30Answer = namedtuple("SGP30Answer", ["data", "raw", "crc_ok"])
@@ -60,28 +58,17 @@ class SGP30():
answer = [i << 8 | j for i, j in a] 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): def dump_baseline(self):
with open(self._baseline_filename, "w") as conf:
baseline = self.read_write(_cmds.GET_BASELINE) baseline = self.read_write(_cmds.GET_BASELINE)
if baseline.crc_ok == True: if baseline.crc_ok == True:
json.dump(baseline.raw, conf) print(baseline)
return True
else: else:
#print("Ignoring baseline due to invalid CRC") print("Ignoring baseline due to invalid CRC")
return False
def try_set_baseline(self): def set_baseline(self):
try: crc, _ = self._raw_validate_crc(self._baseline)
with open(self._baseline_filename, "r") as conf: if len(self._baseline) == 6 and crc == True:
conf = json.load(conf) self.read_write(_cmds.new_set_baseline(self._baseline))
except IOError:
pass
except ValueError:
pass
else:
crc, _ = self._raw_validate_crc(conf)
if len(conf) == 6 and crc == True:
self.read_write(_cmds.new_set_baseline(conf))
return True return True
else: else:
#print("Failed to load baseline, invalid data") #print("Failed to load baseline, invalid data")
@@ -100,7 +87,6 @@ class SGP30():
return self.read_write(_cmds.GET_FEATURES) return self.read_write(_cmds.GET_FEATURES)
def init_sgp(self): def init_sgp(self):
#print("Initializing SGP30")
self.read_write(_cmds.IAQ_INIT) self.read_write(_cmds.IAQ_INIT)
def i2c_general_call(self): def i2c_general_call(self):
@@ -114,7 +100,7 @@ class SGP30():
affect any device on the bus. affect any device on the bus.
""" """
self._bus.write_byte(0, 0x06) self._bus.write_byte(0, 0x06)
sleep(.1) sleep(0.1)
def main(): def main():