3131LEN_HEADER = LEN_FLAGS + LEN_LENGTH + LEN_XOR
3232LEN_BODY = LEN_CRC
3333
34+
3435class BaseFrame (object ):
3536 """
3637 Base frame.
@@ -41,21 +42,24 @@ def __init__(self, data=None, flags=FLAG_NONE):
4142 self .flags = flags
4243
4344 def __repr__ (self ):
44- return "%s(%s, flags=%d)" % (self .__class__ .__name__ ,
45- repr (self .data ), self .flags )
45+ return "%s(%s, flags=%d)" % (
46+ self .__class__ .__name__ , repr (self .data ), self .flags )
47+
4648
4749class Frame (BaseFrame ):
4850 """
4951 Represent normal frame.
5052 """
5153 pass
5254
55+
5356class DamagedFrame (BaseFrame ):
5457 """
5558 Represent damaged frame.
5659 """
5760 pass
5861
62+
5963class ResetFrame (BaseFrame ):
6064 """
6165 Represent reset frame.
@@ -67,12 +71,14 @@ def __init__(self):
6771 def __repr__ (self ):
6872 return "ResetFrame()"
6973
74+
7075class TinyLink (object ):
7176 """
7277 TinyLink state machine for streaming communication with low-speed embedded
7378 applications that only use RX/TX. Every message is encapsulated in a frame.
74- A frame has a header checksum and a frame checksum, to detect errors as fast
75- as possible (this can happen when you jump right into a stream of packets).
79+ A frame has a header checksum and a frame checksum, to detect errors as
80+ fast as possible (this can happen when you jump right into a stream of
81+ packets).
7682
7783 A typical frame has 13 bytes overhead, and can have a data payload up to
7884 65536 bytes.
@@ -81,7 +87,7 @@ class TinyLink(object):
8187 """
8288
8389 def __init__ (self , handle , endianness = LITTLE_ENDIAN ,
84- max_length = 2 ** (LEN_LENGTH * 8 ), ignore_damaged = False ):
90+ max_length = 2 ** (LEN_LENGTH * 8 ), ignore_damaged = False ):
8591 """
8692 Construct a new TinyLink state machine. A state machine takes a handle,
8793 which provides a `read' and `write' method.
@@ -122,19 +128,22 @@ def write_frame(self, frame):
122128
123129 # Check length of message
124130 if length > self .max_length :
125- raise ValueError ("Message length %d exceeds max length %d" %
126- (length , self .max_length ))
131+ raise ValueError (
132+ "Message length %d exceeds max length %d" % (
133+ length , self .max_length ))
127134
128135 # Pack header
129136 checksum_header = utils .checksum_header (frame .flags , length )
130- result += struct .pack (self .endianness + "IHHB" , PREAMBLE , frame .flags ,
131- length , checksum_header )
137+ result += struct .pack (
138+ self .endianness + "IHHB" , PREAMBLE , frame .flags , length ,
139+ checksum_header )
132140
133141 # Pack data
134142 if frame .data is not None :
135143 checksum_frame = utils .checksum_frame (frame .data , checksum_header )
136- result += struct .pack (self .endianness + str (length ) + "sI" ,
137- frame .data , checksum_frame )
144+ result += struct .pack (
145+ self .endianness + str (length ) + "sI" , frame .data ,
146+ checksum_frame )
138147
139148 # Write to file
140149 return self .handle .write (result )
@@ -177,8 +186,8 @@ def read(self, limit=1):
177186 # Decide what to do
178187 if self .state == WAITING_FOR_PREAMBLE :
179188 if self .index >= LEN_PREAMBLE :
180- start , = struct .unpack_from (self . endianness + "I" ,
181- self .buffer , self .index - 4 )
189+ start , = struct .unpack_from (
190+ self .endianness + "I" , self . buffer , self .index - 4 )
182191
183192 if start == PREAMBLE :
184193 # Advance to next state
@@ -198,7 +207,7 @@ def read(self, limit=1):
198207
199208 # Verify checksum
200209 if checksum == utils .checksum_header (flags , length ) and \
201- length <= self .max_length :
210+ length <= self .max_length :
202211
203212 if length > 0 :
204213 self .state = WAITING_FOR_BODY
@@ -238,4 +247,4 @@ def read(self, limit=1):
238247 limit -= 1
239248
240249 # Done
241- return frames
250+ return frames
0 commit comments