Adds timing to mock-bus

I dont know if it is a limitation of i2cwr or the sgp but it dosnt seem
to clock-strech but rahter just not present any data if the messure ment
is not done, so timing is important. The test should now fail if data is
read to fast
This commit is contained in:
Simon Albinsson
2018-04-16 12:39:39 +00:00
parent ac4446cb36
commit 9292f4e17e

View File

@@ -1,18 +1,22 @@
from smbus2 import i2c_msg from smbus2 import i2c_msg
from collections import namedtuple
from .context import sgp30 from .context import sgp30
from time import time
I2CAnswers= namedtuple("I2CAnswers",["answer","min_delay"])
I2A = I2CAnswers
def add_crc(l): def add_crc(l):
return l + [sgp30.Crc8().hash(l)] return l + [sgp30.Crc8().hash(l)]
answers = { answers = {
(0x36, 0x82): add_crc([0,0]) ,#GET_SERIAL (0x36, 0x82): I2A(add_crc([0,0]),.4), #GET_SERIAL
(0x20, 0x15): add_crc([0,0]) ,#GET_FEATURES (0x20, 0x15): I2A(add_crc([0,0]), 1), #GET_FEATURES
(0x20, 0x03): None ,#IAQ_INIT (0x20, 0x03): I2A(None, 2), #IAQ_INIT
(0x20, 0x08): [1, 144, 76, 0, 6, 39] ,#IAQ_MEASURE (0x20, 0x08): I2A([1, 144, 76, 0, 6, 39], 10), #IAQ_MEASURE
(0x20, 0x32): add_crc([0xD4,0x00]) ,#IAQ_SELFTEST (0x20, 0x32): I2A(add_crc([0xD4,0x00]), 200), #IAQ_SELFTEST
(0x20, 0x15): [133, 152, 85, 138, 32, 202] ,#GET_BASELINE (0x20, 0x15): I2A([133, 152, 85, 138, 32, 202], 10), #GET_BASELINE
(0x20, 0x1e): None,#SET_BASELINE (0x20, 0x1e): I2A(None, 10) #SET_BASELINE
} }
class MockSMBus: class MockSMBus:
@@ -21,9 +25,15 @@ class MockSMBus:
s.last=None s.last=None
s.addr=None s.addr=None
s._break_crc=break_crc s._break_crc=break_crc
s._deadline=time()
def _set_deadline(s, t):
s._deadline=time() + t/1000.
def i2c_rdwr(s,*msgs): def i2c_rdwr(s,*msgs):
for m in msgs: for m in msgs:
if time() < s._deadline:
raise IOError("To fast buss-access, device not ready")
if m.flags == 1: if m.flags == 1:
s._process_read(m) s._process_read(m)
else: else:
@@ -45,6 +55,8 @@ class MockSMBus:
def _process_write(s,msg): def _process_write(s,msg):
s.status = answers[tuple(msg)[0:2]] a = answers[tuple(msg)[0:2]]
s._set_deadline(a.min_delay)
s.status = a.answer
s.last=msg s.last=msg