Skip to content

Commit b261965

Browse files
committed
Refactoring of code.
1 parent 08cb257 commit b261965

File tree

3 files changed

+31
-26
lines changed

3 files changed

+31
-26
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Frame-based streaming protocol for embedded applications.
33

44
## Introduction
5-
This is a Python module to provide a bi-directional frame-based streaming protocol for low-speed embedded applications, such as serial connected devices. It allowes the receiver to 'jump into' a stream of data frames. Every frame starts with a preamble, so the receiver can synchronize. Any mismatch in checksum will the receiver.
5+
This is a general purpose Python module to provide a bi-directional frame-based streaming protocol for low-speed embedded applications, such as serial connected devices. It allowes the receiver to 'jump into' a stream of data frames. Every frame starts with a preamble, so the receiver can synchronize. Any mismatch in checksum will the receiver.
66

77
A payload is optional. The reserved `RESET` flag can be used to indicate that the link should reset, for instance when the receiver just started and the sender should restart.
88

tests/test_tinylink.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ def __init__(self):
1212
self.index = 0
1313
self.length = 0
1414

15-
def send(self, data):
15+
def write(self, data):
1616
self.buffer.extend(data)
1717
self.length += len(data)
1818

1919
# Return number of bytes written
2020
return len(data)
2121

22-
def recv(self, count):
23-
data = self.buffer[self.index:min(self.length, self.index+count)]
22+
def read(self, count):
23+
data = str(self.buffer[self.index:min(self.length, self.index+count)])
2424
self.index += len(data)
2525

2626
# Return data
@@ -33,7 +33,7 @@ class TinyLinkTest(unittest.TestCase):
3333

3434
def test_basic(self):
3535
"""
36-
Test send/receive by using a dummy handle.
36+
Test read/write by using a dummy handle.
3737
"""
3838

3939
handle = DummyHandle()
@@ -99,7 +99,7 @@ def test_sync(self):
9999
garbage = "Garbage here that doesn't synchronize."
100100
message = "Hi!"
101101

102-
size = handle.send(garbage) + link.write(message)
102+
size = handle.write(garbage) + link.write(message)
103103
frames = link.read(size)
104104

105105
self.assertEqual(len(frames), 1)
@@ -116,7 +116,7 @@ def test_sync_small(self):
116116
garbage = "Garbage here that doesn't synchronize."
117117
message = "Hi!"
118118

119-
size = handle.send(garbage) + link.write(message)
119+
size = handle.write(garbage) + link.write(message)
120120
frames = link.read(size)
121121

122122
self.assertEqual(len(frames), 1)

tinylink/__init__.py

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
import struct
44

5+
__version__ = "1.1"
6+
57
# This can be anything, and is used to synchronize a frame
68
PREAMBLE = 0xAA55AA55
79

810
# Endianness
911
LITTLE_ENDIAN = "<"
10-
BIG_ENDINAN = ">"
12+
BIG_ENDIAN = ">"
1113

1214
# Protocol states
1315
WAITING_FOR_PREAMBLE = 1
@@ -37,7 +39,7 @@ def __init__(self, data=None, flags=FLAG_NONE):
3739
self.flags = flags
3840

3941
def __repr__(self):
40-
return "%s(%s, flags=0x%02x)" % (self.__class__.__name__,
42+
return "%s(%s, flags=%d)" % (self.__class__.__name__,
4143
repr(self.data), self.flags)
4244

4345
class Frame(BaseFrame):
@@ -77,10 +79,10 @@ class TinyLink(object):
7779
"""
7880

7981
def __init__(self, handle, endianness=LITTLE_ENDIAN,
80-
max_length=2**(LEN_LENGTH * 8)):
82+
max_length=2**(LEN_LENGTH * 8), ignore_damaged=False):
8183
"""
8284
Construct a new TinyLink state machine. A state machine takes a handle,
83-
which provides a `recv' and `send' method.
85+
which provides a `read' and `write' method.
8486
8587
The endianness is either LITTLE_ENDIAN or BIG_ENDIAN. While big endian
8688
is common for networking, little endian is directly compatible with ARM
@@ -90,6 +92,9 @@ def __init__(self, handle, endianness=LITTLE_ENDIAN,
9092
Both microcontroller and this instance should agree upon the value of
9193
`max_length'. In case a message is received that exceeds this value, it
9294
will be silently ignored.
95+
96+
By default, if a fully received frame is damaged, it will be returned
97+
as a `DamagedFrame' instance, unless `ignored_damaged' is True.
9398
"""
9499

95100
self.handle = handle
@@ -105,9 +110,9 @@ def __init__(self, handle, endianness=LITTLE_ENDIAN,
105110
self.buffer = buffer(self.stream)
106111
self.index = 0
107112

108-
def send_frame(self, frame):
113+
def write_frame(self, frame):
109114
"""
110-
Send a frame via the handle.
115+
Write a frame via the handle.
111116
"""
112117

113118
result = bytearray()
@@ -130,38 +135,35 @@ def send_frame(self, frame):
130135
frame.data, checksum_frame)
131136

132137
# Write to file
133-
return self.handle.send(result)
138+
return self.handle.write(result)
134139

135140
def reset(self):
136141
"""
137-
Shorthand for `send_data(ResetFrame())'.
142+
Shorthand for `write_frame(ResetFrame())'.
138143
"""
139144

140-
return self.send_frame(ResetFrame())
145+
return self.write_frame(ResetFrame())
141146

142147
def write(self, data, flags=FLAG_NONE):
143148
"""
144-
Shorthand for `send_data(Frame(data, flags=flags))'.
149+
Shorthand for `write_frame(Frame(data, flags=flags))'.
145150
"""
146151

147-
return self.send_frame(Frame(data, flags=flags))
152+
return self.write_frame(Frame(data, flags=flags))
148153

149-
def read(self, limit=1, ignore_damaged=False):
154+
def read(self, limit=1):
150155
"""
151156
Read at `limit' bytes from the handle and process this byte. Returns a
152157
list of received frames, if any. A reset frame is indicated by a
153158
`ResetFrame' instance.
154-
155-
By default, if a fully received package is damaged, it will be returned
156-
as a `DamagedFrame' instance, unless `ignored_damaged' is True.
157159
"""
158160

159-
# Define list for all results
161+
# List of frames received
160162
frames = []
161163

162164
# Bytes are added one at a time
163-
for byte in self.handle.recv(limit):
164-
self.stream[self.index] = byte
165+
while limit:
166+
self.stream[self.index] = self.handle.read(1)
165167
self.index += 1
166168

167169
# Decide what to do
@@ -217,12 +219,15 @@ def read(self, limit=1, ignore_damaged=False):
217219
# Verify checksum
218220
if checksum_b == utils.checksum_frame(result, checksum_a):
219221
frames.append(Frame(result, flags=flags))
220-
elif not ignore_damaged:
222+
elif not self.ignore_damaged:
221223
frames.append(DamagedFrame(result, flags=flags))
222224

223225
# Reset to start state
224226
self.index = 0
225227
self.state = WAITING_FOR_PREAMBLE
226228

229+
# Decrement number of bytes to read
230+
limit -= 1
231+
227232
# Done
228233
return frames

0 commit comments

Comments
 (0)