fixes most basic init

This commit is contained in:
Simon Albinsson
2018-04-12 19:52:03 +00:00
parent 9ec514ff92
commit afc8f18456

View File

@@ -11,7 +11,7 @@ import os.path
DEVICE_BUS = 1 DEVICE_BUS = 1
BASELINE_FILENAME = os.path.expanduser("~/sgp_config_data.txt") BASELINE_FILENAME = os.path.expanduser("~/sgp_config_data.txt")
class _commands(): 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"""
Sgp30Cmd = namedtuple("Sgp30Cmd",["commands","replylen","waittime"]) Sgp30Cmd = namedtuple("Sgp30Cmd",["commands","replylen","waittime"])
GET_SERIAL=Sgp30Cmd([0x36, 0x82],6,10) GET_SERIAL=Sgp30Cmd([0x36, 0x82],6,10)
@@ -30,11 +30,12 @@ class _commands():
class Sgp30(): class Sgp30():
def __init__(self,bus,device_address = 0x58): def __init__(self,bus,device_address = 0x58, baseline_filename=BASELINE_FILENAME):
self._bus = bus self._bus = bus
self._device_addr = device_address self._device_addr = device_address
self.rw=partial(read_write,bus=bus) self.rw=partial(self.read_write)
self._start_time = time() self._start_time = time()
self._baseline_filename=baseline_filename
Sgp30Answer = namedtuple("Sgp30Answer",["data","raw"]) Sgp30Answer = namedtuple("Sgp30Answer",["data","raw"])
@@ -52,10 +53,10 @@ class Sgp30():
answer = [i<<8 | j for i,j in zip(r[0::3],r[1::3])] answer = [i<<8 | j for i,j in zip(r[0::3],r[1::3])]
return Sgp30Answer(answer,r) return Sgp30Answer(answer,r)
def try_set_baseline(): def try_set_baseline(self):
try: try:
with open(BASELINE_FILENAME,"w") as conf: with open(self._baseline_filename,"w") as conf:
baseline_cmd = _commands.new_set_baseline(json.load(conf)) baseline_cmd = _cmds.new_set_baseline(json.load(conf))
except IOError: except IOError:
pass pass
except ValueError: except ValueError:
@@ -67,7 +68,7 @@ class Sgp30():
def init_sgp(self): def init_sgp(self):
print("Initializing SGP30") print("Initializing SGP30")
self.rw(IAQ_INIT) self.rw(_cmds.IAQ_INIT)
self.try_set_baseline() self.try_set_baseline()
#print(rw(SET_BASELINE)) #print(rw(SET_BASELINE))
print("Waiting for sensor warmup") print("Waiting for sensor warmup")
@@ -93,14 +94,14 @@ def store_baseline(n):
def main(): def main():
with SMBusWrapper(1) as bus: with SMBusWrapper(1) as bus:
rw=partial(read_write,bus=bus) rw=partial(read_write)
i2c_geral_call(bus) i2c_geral_call(bus)
print("Features: %s"%repr(rw(GET_FEATURES))) print("Features: %s"%repr(rw(GET_FEATURES)))
print("Serial: %s"%repr(rw(GET_SERIAL))) print("Serial: %s"%repr(rw(GET_SERIAL)))
init_sgp(bus) init_sgp(bus)
#print(rw(IAQ_SELFTEST)) #print(rw(_cmds.IAQ_SELFTEST))
print("Testing meassure") print("Testing meassure")
print(rw(IAQ_MEASURE)) print(rw(_cmds.IAQ_MEASURE))
print("Testing meassure again") print("Testing meassure again")
sleep(1) sleep(1)
print("Running") print("Running")
@@ -108,7 +109,7 @@ def main():
while(True): while(True):
start = time() start = time()
n+=1 n+=1
co2eq, tvoc = rw(IAQ_MEASURE).data co2eq, tvoc = rw(_cmds.IAQ_MEASURE).data
res = ( "%s CO_2eq: %d ppm, TVOC: %d"%( asctime(), co2eq, tvoc )) res = ( "%s CO_2eq: %d ppm, TVOC: %d"%( asctime(), co2eq, tvoc ))
print(res) print(res)
f.write("%i %i %i \n"%(time(),co2eq,tvoc)) f.write("%i %i %i \n"%(time(),co2eq,tvoc))