Skip to content

Commit ab50fc5

Browse files
committed
Make irrx use timeout
1 parent de0eab4 commit ab50fc5

File tree

2 files changed

+55
-37
lines changed

2 files changed

+55
-37
lines changed

servercom/irrx.py

Lines changed: 54 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import board
77
import digitalio
88
import adafruit_irremote
9+
from .timeout import Timeout
10+
911
#irPin = digitalio.DigitalInOut(board.A0)
1012
#irPin.direction = digitalio.Direction.INPUT
1113
DEFAULT_PULSEIN = pulseio.PulseIn(board.A0, maxlen=30000, idle_state=True)
@@ -45,44 +47,60 @@ def division(self):
4547
def text(self):
4648
return self.body.decode()
4749

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
7355
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
8379
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()
86104

87105
#dude = receive()
88106
#print()

servercom/timeout

Submodule timeout updated from 431f451 to ade55fb

0 commit comments

Comments
 (0)