tests:mock-bus more python3 comptaible

This commit is contained in:
Simon Albinsson
2018-04-23 20:26:16 +00:00
parent 6ddc1b6abc
commit 05e6a98001

View File

@@ -2,12 +2,18 @@ from collections import namedtuple
from .context import sgp30
from time import time
import sys
I2CAnswers= namedtuple("I2CAnswers",["answer","min_delay"])
I2A = I2CAnswers
def add_crc(l):
return l + [sgp30.Crc8().hash(l)]
if sys.version_info[0] < 3:
byte_from_int = chr
else:
byte_from_int = lambda x: bytes([x])
answers = {
(0x36, 0x82): I2A(add_crc([0,0]),.4), #GET_SERIAL
(0x20, 0x15): I2A(add_crc([0,0]), 1), #GET_FEATURES
@@ -47,9 +53,9 @@ class MockSMBus:
if s.status == None:
raise AssertionError("Tried to read before write")
for i in range(len(s.status)):
msg.buf[i]=chr(s.status[i])
msg.buf[i]=byte_from_int(s.status[i])
if s._break_crc and i%3 == 2:
msg.buf[i]=chr(s.status[i]^42)
msg.buf[i]=byte_from_int(s.status[i]^42)
s.status=None