diff --git a/sgptest.py b/sgptest.py index b4a8beb..ba31cf5 100644 --- a/sgptest.py +++ b/sgptest.py @@ -11,6 +11,7 @@ import os.path DEVICE_BUS = 1 DEVICE_ADDR = 0x58 +BASELINE_FILENAME = os.path.expanduser("~/sgp_config_data.txt") #print bus.write_byte_datadata(DEVICE_ADDR, 0x00, 0x01) #self._i2c_read_words_from_cmd([0x36, 0x82], 0.01, 3) @@ -36,8 +37,42 @@ def read_write(cmd,bus,addr=DEVICE_ADDR): bus.i2c_rdwr(read) r = list(read) answer = [i<<8 | j for i,j in zip(r[0::3],r[1::3])] - return answer - + return Sgp30Answer(answer,r) + +def try_set_baseline(): + try: + with open(BASELINE_FILENAME,"w") as conf: + baseline=json.load(conf) + baseline_cmd = copy(SET_BASELINE) + baseline_cmd.commands += baseline + except IOError: + pass + except ValueError: + pass + else: + if len(baseline) == 6: + print("Loading baseline data into sensor") + rw(baseline_cmd) + +def init_sgp(bus): + print("Initializing SGP30") + rw(IAQ_INIT) + print("Waiting for sensor warmup") + try_set_baseline() + #print(rw(SET_BASELINE)) + sleep(15) + +def i2c_geral_call(bus): + print("resetting bus") + bus.write_byte(0,0x06) + sleep(.1) + +def store_baseline(n): + if (n > 3600 * 12) and (n % 3600 == 3599): + with open(BASELINE_FILENAME,"w") as conf: + baseline= rw(GET_BASELINE) + json.dump(baseline.raw,conf) + with SMBusWrapper(1) as bus: rw=partial(read_write,bus=bus) print(rw(GET_FEATURES))