|
4 | 4 | print('SKIP') |
5 | 5 | raise SystemExit |
6 | 6 |
|
| 7 | +from array import array |
| 8 | +import micropython |
7 | 9 | import pyb |
8 | 10 |
|
9 | 11 | # test we can correctly create by id or name |
|
19 | 21 | can = CAN(1) |
20 | 22 | print(can) |
21 | 23 |
|
| 24 | +# Test state when de-init'd |
| 25 | +print(can.state() == can.STOPPED) |
| 26 | + |
22 | 27 | can.init(CAN.LOOPBACK) |
23 | 28 | print(can) |
24 | 29 | print(can.any(0)) |
25 | 30 |
|
| 31 | +# Test state when freshly created |
| 32 | +print(can.state() == can.ERROR_ACTIVE) |
| 33 | + |
| 34 | +# Test that restart can be called |
| 35 | +can.restart() |
| 36 | + |
| 37 | +# Test info returns a sensible value |
| 38 | +print(can.info()) |
| 39 | + |
26 | 40 | # Catch all filter |
27 | 41 | can.setfilter(0, CAN.MASK16, 0, (0, 0, 0, 0)) |
28 | 42 |
|
29 | 43 | can.send('abcd', 123, timeout=5000) |
30 | | -print(can.any(0)) |
| 44 | +print(can.any(0), can.info()) |
31 | 45 | print(can.recv(0)) |
32 | 46 |
|
33 | 47 | can.send('abcd', -1, timeout=5000) |
|
44 | 58 | else: |
45 | 59 | print('failed') |
46 | 60 |
|
| 61 | +# Test that recv can work without allocating memory on the heap |
| 62 | + |
| 63 | +buf = bytearray(10) |
| 64 | +l = [0, 0, 0, memoryview(buf)] |
| 65 | +l2 = None |
| 66 | + |
| 67 | +micropython.heap_lock() |
| 68 | + |
| 69 | +can.send('', 42) |
| 70 | +l2 = can.recv(0, l) |
| 71 | +assert l is l2 |
| 72 | +print(l, len(l[3]), buf) |
| 73 | + |
| 74 | +can.send('1234', 42) |
| 75 | +l2 = can.recv(0, l) |
| 76 | +assert l is l2 |
| 77 | +print(l, len(l[3]), buf) |
| 78 | + |
| 79 | +can.send('01234567', 42) |
| 80 | +l2 = can.recv(0, l) |
| 81 | +assert l is l2 |
| 82 | +print(l, len(l[3]), buf) |
| 83 | + |
| 84 | +can.send('abc', 42) |
| 85 | +l2 = can.recv(0, l) |
| 86 | +assert l is l2 |
| 87 | +print(l, len(l[3]), buf) |
| 88 | + |
| 89 | +micropython.heap_unlock() |
| 90 | + |
| 91 | +# Test that recv can work with different arrays behind the memoryview |
| 92 | +can.send('abc', 1) |
| 93 | +print(bytes(can.recv(0, [0, 0, 0, memoryview(array('B', range(8)))])[3])) |
| 94 | +can.send('def', 1) |
| 95 | +print(bytes(can.recv(0, [0, 0, 0, memoryview(array('b', range(8)))])[3])) |
| 96 | + |
| 97 | +# Test for non-list passed as second arg to recv |
| 98 | +can.send('abc', 1) |
| 99 | +try: |
| 100 | + can.recv(0, 1) |
| 101 | +except TypeError: |
| 102 | + print('TypeError') |
| 103 | + |
| 104 | +# Test for too-short-list passed as second arg to recv |
| 105 | +can.send('abc', 1) |
| 106 | +try: |
| 107 | + can.recv(0, [0, 0, 0]) |
| 108 | +except ValueError: |
| 109 | + print('ValueError') |
| 110 | + |
| 111 | +# Test for non-memoryview passed as 4th element to recv |
| 112 | +can.send('abc', 1) |
| 113 | +try: |
| 114 | + can.recv(0, [0, 0, 0, 0]) |
| 115 | +except TypeError: |
| 116 | + print('TypeError') |
| 117 | + |
| 118 | +# Test for read-only-memoryview passed as 4th element to recv |
| 119 | +can.send('abc', 1) |
| 120 | +try: |
| 121 | + can.recv(0, [0, 0, 0, memoryview(bytes(8))]) |
| 122 | +except ValueError: |
| 123 | + print('ValueError') |
| 124 | + |
| 125 | +# Test for bad-typecode-memoryview passed as 4th element to recv |
| 126 | +can.send('abc', 1) |
| 127 | +try: |
| 128 | + can.recv(0, [0, 0, 0, memoryview(array('i', range(8)))]) |
| 129 | +except ValueError: |
| 130 | + print('ValueError') |
| 131 | + |
47 | 132 | del can |
48 | 133 |
|
49 | 134 | # Testing extended IDs |
|
0 commit comments