|
1 | | -import struct |
2 | | - |
3 | | - |
4 | | -class MockI2CDev(): |
5 | | - def __init__(self): |
6 | | - self._open = True |
7 | | - self._last_write = None |
8 | | - |
9 | | - def i2c_rdwr(self, a, b=None): |
10 | | - if isinstance(a, MockI2CMsgW): |
11 | | - self._last_write = a.process() |
12 | | - if isinstance(a, MockI2CMsgR): |
13 | | - a.process(self._last_write) |
14 | | - if b is not None: |
15 | | - b.process(self._last_write) |
16 | | - |
17 | | - def close(self): |
18 | | - self._open = False |
19 | | - |
20 | | - |
21 | | -class MockI2CMsg(): |
22 | | - def write(self, i2c_addr, data): |
23 | | - return MockI2CMsgW(i2c_addr, data) |
24 | | - |
25 | | - def read(self, i2c_addr, response_len): |
26 | | - return MockI2CMsgR(i2c_addr, response_len) |
27 | | - |
28 | | - |
29 | | -class MockI2CMsgR(): |
30 | | - def __init__(self, i2c_addr, response_len): |
31 | | - self._num_words = response_len // 3 |
32 | | - self.buf = None |
33 | | - |
34 | | - def process(self, last_command): |
35 | | - result = [0, 0, 0] |
36 | | - if last_command[0] == 0x3682: # get_serial_id |
37 | | - result = [0xffff, 0xffff, 0xffff] |
38 | | - if last_command[0] == 0x202f: # get_feature_set_version |
39 | | - result = [0xCAFE, 0x0000, 0x0000] |
40 | | - |
41 | | - buf = [] |
42 | | - |
43 | | - for i in range(self._num_words): |
44 | | - word = result[i] |
45 | | - packed = bytearray(struct.pack('>H', word)) |
46 | | - buf.append(packed[0]) |
47 | | - buf.append(packed[1]) |
48 | | - buf.append(self.calculate_crc(word)) |
49 | | - |
50 | | - self.buf = bytearray(buf) |
51 | | - |
52 | | - def calculate_crc(self, data): |
53 | | - """Calculate an 8-bit CRC from a 16-bit word |
54 | | -
|
55 | | - Defined in section 6.6 of the SGP30 datasheet. |
56 | | -
|
57 | | - Polynominal: 0x31 (x8 + x5 + x4 + x1) |
58 | | - Initialization: 0xFF |
59 | | - Reflect input/output: False |
60 | | - Final XOR: 0x00 |
61 | | -
|
62 | | - """ |
63 | | - crc = 0xff # Initialization value |
64 | | - # calculates 8-Bit checksum with given polynomial |
65 | | - for byte in [(data & 0xff00) >> 8, data & 0x00ff]: |
66 | | - crc ^= byte |
67 | | - for _ in range(8): |
68 | | - if crc & 0x80: |
69 | | - crc = (crc << 1) ^ 0x31 # XOR with polynominal |
70 | | - else: |
71 | | - crc <<= 1 |
72 | | - return crc & 0xff |
73 | | - |
74 | | - |
75 | | -class MockI2CMsgW(): |
76 | | - def __init__(self, i2c_addr, data): |
77 | | - self._data = data |
78 | | - |
79 | | - def process(self): |
80 | | - command_len = (len(self._data) - 2) // 3 |
81 | | - return struct.unpack('>H' + ('Hx' * command_len), self._data) |
| 1 | +import struct |
| 2 | + |
| 3 | + |
| 4 | +class MockI2CDev(): |
| 5 | + def __init__(self): |
| 6 | + self._open = True |
| 7 | + self._last_write = None |
| 8 | + |
| 9 | + def i2c_rdwr(self, a, b=None): |
| 10 | + if isinstance(a, MockI2CMsgW): |
| 11 | + self._last_write = a.process() |
| 12 | + if isinstance(a, MockI2CMsgR): |
| 13 | + a.process(self._last_write) |
| 14 | + if b is not None: |
| 15 | + b.process(self._last_write) |
| 16 | + |
| 17 | + def close(self): |
| 18 | + self._open = False |
| 19 | + |
| 20 | + |
| 21 | +class MockI2CMsg(): |
| 22 | + def write(self, i2c_addr, data): |
| 23 | + return MockI2CMsgW(i2c_addr, data) |
| 24 | + |
| 25 | + def read(self, i2c_addr, response_len): |
| 26 | + return MockI2CMsgR(i2c_addr, response_len) |
| 27 | + |
| 28 | + |
| 29 | +class MockI2CMsgR(): |
| 30 | + def __init__(self, i2c_addr, response_len): |
| 31 | + self._num_words = response_len // 3 |
| 32 | + self.buf = None |
| 33 | + |
| 34 | + def process(self, last_command): |
| 35 | + result = [0, 0, 0] |
| 36 | + if last_command[0] == 0x3682: # get_serial_id |
| 37 | + result = [0xffff, 0xffff, 0xffff] |
| 38 | + if last_command[0] == 0x202f: # get_feature_set_version |
| 39 | + result = [0xCAFE, 0x0000, 0x0000] |
| 40 | + |
| 41 | + buf = [] |
| 42 | + |
| 43 | + for i in range(self._num_words): |
| 44 | + word = result[i] |
| 45 | + packed = bytearray(struct.pack('>H', word)) |
| 46 | + buf.append(packed[0]) |
| 47 | + buf.append(packed[1]) |
| 48 | + buf.append(self.calculate_crc(word)) |
| 49 | + |
| 50 | + self.buf = bytearray(buf) |
| 51 | + |
| 52 | + def calculate_crc(self, data): |
| 53 | + """Calculate an 8-bit CRC from a 16-bit word |
| 54 | +
|
| 55 | + Defined in section 6.6 of the SGP30 datasheet. |
| 56 | +
|
| 57 | + Polynominal: 0x31 (x8 + x5 + x4 + x1) |
| 58 | + Initialization: 0xFF |
| 59 | + Reflect input/output: False |
| 60 | + Final XOR: 0x00 |
| 61 | +
|
| 62 | + """ |
| 63 | + crc = 0xff # Initialization value |
| 64 | + # calculates 8-Bit checksum with given polynomial |
| 65 | + for byte in [(data & 0xff00) >> 8, data & 0x00ff]: |
| 66 | + crc ^= byte |
| 67 | + for _ in range(8): |
| 68 | + if crc & 0x80: |
| 69 | + crc = (crc << 1) ^ 0x31 # XOR with polynominal |
| 70 | + else: |
| 71 | + crc <<= 1 |
| 72 | + return crc & 0xff |
| 73 | + |
| 74 | + |
| 75 | +class MockI2CMsgW(): |
| 76 | + def __init__(self, i2c_addr, data): |
| 77 | + self._data = data |
| 78 | + |
| 79 | + def process(self): |
| 80 | + command_len = (len(self._data) - 2) // 3 |
| 81 | + return struct.unpack('>H' + ('Hx' * command_len), self._data) |
0 commit comments