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