|
6 | 6 | import board |
7 | 7 | import digitalio |
8 | 8 | import adafruit_irremote |
| 9 | +from .timeout import Timeout |
| 10 | + |
9 | 11 | #irPin = digitalio.DigitalInOut(board.A0) |
10 | 12 | #irPin.direction = digitalio.Direction.INPUT |
11 | 13 | DEFAULT_PULSEIN = pulseio.PulseIn(board.A0, maxlen=30000, idle_state=True) |
@@ -45,44 +47,60 @@ def division(self): |
45 | 47 | def text(self): |
46 | 48 | return self.body.decode() |
47 | 49 |
|
48 | | -def receive(decoder = DEFAULT_DECODER, pulsein = DEFAULT_PULSEIN): |
49 | | - # Read until BELL |
50 | | - print("*", end="") |
51 | | - while decode_chunk(decoder, pulsein) != b'\x07': |
52 | | - pass |
53 | | - buf = b'\x07' |
54 | | - # Read until no BELL |
55 | | - print("*", end="") |
56 | | - while buf == b'\x07': |
57 | | - buf = decode_chunk(decoder, pulsein) |
58 | | - # Figure out datagram length |
59 | | - print("*", end="") |
60 | | - total_length = int.from_bytes(buf, 'big') |
61 | | - read_length = 0 |
62 | | - if total_length < 8: |
63 | | - return b'', b'', b'' |
64 | | - # Start receiving the datagram |
65 | | - print("*", end="") |
66 | | - protocol = decode_line(decoder, pulsein) |
67 | | - read_length += len(protocol) |
68 | | - if protocol != b"CSMSG/1.0\r\n": |
69 | | - print("Potentially unsupported protocol version. Try getting the latest servercom library?") |
70 | | - headers = {} |
71 | | - buf = b': ' |
72 | | - while buf.find(b': ') >= 0 or buf == b'\r\n': |
| 50 | +def receive(timeout: int = 10, line_timeout: int = 100, decoder = DEFAULT_DECODER, pulsein = DEFAULT_PULSEIN) -> IRMsg: |
| 51 | + |
| 52 | + @Timeout(timeout) |
| 53 | + def wait_for_bell(): |
| 54 | + # Read until BELL |
73 | 55 | print("*", end="") |
74 | | - buf = decode_line(decoder, pulsein) |
75 | | - read_length += len(buf) |
76 | | - if buf == b'\r\n': |
77 | | - break |
78 | | - split_buf = buf.strip(b'\r\n').split(b': ') |
79 | | - headers.update({split_buf[0]: split_buf[1]}) |
80 | | - body = b'' |
81 | | - content_length = int(headers[b'Content-Length']) |
82 | | - while len(body) < content_length: |
| 56 | + while decode_chunk(decoder, pulsein) != b'\x07': |
| 57 | + t_wd.reset() |
| 58 | + return True |
| 59 | + |
| 60 | + if not wait_for_bell(): |
| 61 | + return None |
| 62 | + |
| 63 | + t_wd = Timeout(line_timeout) |
| 64 | + @t_wd |
| 65 | + def receive_the_thing(): |
| 66 | + buf = b'\x07' |
| 67 | + # Read until no BELL |
| 68 | + print("*", end="") |
| 69 | + while buf == b'\x07': |
| 70 | + buf = decode_chunk(decoder, pulsein) |
| 71 | + t_wd.reset() |
| 72 | + # Figure out datagram length |
| 73 | + print("*", end="") |
| 74 | + total_length = int.from_bytes(buf, 'big') |
| 75 | + read_length = 0 |
| 76 | + if total_length < 8: |
| 77 | + return None |
| 78 | + # Start receiving the datagram |
83 | 79 | print("*", end="") |
84 | | - body += decode_chunk(decoder, pulsein) |
85 | | - return IRMsg(headers, body) |
| 80 | + protocol = decode_line(decoder, pulsein) |
| 81 | + t_wd.reset() |
| 82 | + read_length += len(protocol) |
| 83 | + if protocol != b"CSMSG/1.0\r\n": |
| 84 | + print("Potentially unsupported protocol version. Try getting the latest servercom library?") |
| 85 | + headers = {} |
| 86 | + buf = b': ' |
| 87 | + while buf.find(b': ') >= 0 or buf == b'\r\n': |
| 88 | + print("*", end="") |
| 89 | + buf = decode_line(decoder, pulsein) |
| 90 | + t_wd.reset() |
| 91 | + read_length += len(buf) |
| 92 | + if buf == b'\r\n': |
| 93 | + break |
| 94 | + split_buf = buf.strip(b'\r\n').split(b': ') |
| 95 | + headers.update({split_buf[0]: split_buf[1]}) |
| 96 | + body = b'' |
| 97 | + content_length = int(headers[b'Content-Length']) |
| 98 | + while len(body) < content_length: |
| 99 | + print("*", end="") |
| 100 | + body += decode_chunk(decoder, pulsein) |
| 101 | + t_wd.reset() |
| 102 | + return IRMsg(headers, body) |
| 103 | + return receive_the_thing() |
86 | 104 |
|
87 | 105 | #dude = receive() |
88 | 106 | #print() |
|
0 commit comments