@@ -50,8 +50,8 @@ def __init__(
5050 serial = usb_cdc .console ,
5151 encoding = "utf-8" ,
5252 line_end_custom = None ,
53- universal_line_end_basic = True ,
54- universal_line_end_advanced = False ,
53+ use_universal_line_end_basic = True ,
54+ use_universal_line_end_advanced = False ,
5555 ):
5656 super (NonBlockingSerialInput , self ).__init__ ()
5757 self .parse_input_fn = parse_input_fn
@@ -61,13 +61,14 @@ def __init__(
6161 self .line_end_list = []
6262 if line_end_custom :
6363 self .line_end_list .extend (line_end_custom )
64- if universal_line_end_basic :
65- self .line_end_list .extend (self . universal_line_end_basic )
66- if universal_line_end_advanced :
67- self .line_end_list .extend (self . universal_line_end_advanced )
64+ if use_universal_line_end_basic :
65+ self .line_end_list .extend (universal_line_end_basic )
66+ if use_universal_line_end_advanced :
67+ self .line_end_list .extend (universal_line_end_advanced )
6868
6969 # no block:
7070 self .serial .timeout = 0
71+ self .input_buffer = ""
7172 self .input_list = []
7273
7374 def _parse_input_fallback (self , input_string ):
@@ -116,9 +117,12 @@ def _buffer_check_and_handle_line_ends(self):
116117 # # we have to leave the last part in place..
117118 # pass
118119 # self.input_list.extend(self.input_buffer.splitlines(self.line_end_list))
119- lines , rest = self . splitlines_advanced (self .input_buffer )
120+ lines , rest = splitlines_advanced (self .input_buffer )
120121 self .input_list .extend (lines )
121- self .input_buffer = rest
122+ if rest :
123+ self .input_buffer = rest
124+ else :
125+ self .input_buffer = ""
122126
123127 def input (self ):
124128 """get oldest input string if there is any available. Otherwise None."""
@@ -134,7 +138,9 @@ def update(self):
134138 available = self .serial .in_waiting
135139 while available :
136140 self .input_buffer += self .serial .read (available ).decode (
137- encoding = self .encoding , errors = "strict"
141+ self .encoding ,
142+ # encoding=self.encoding,
143+ # errors="strict",
138144 )
139145 self ._buffer_check_and_handle_line_ends ()
140146 available = self .serial .in_waiting
@@ -184,44 +190,71 @@ def update(self):
184190]
185191
186192
193+ # def find_first_line_end(input_string, line_end_list=universal_line_end_basic, start=0):
194+ # result = -1
195+ # # print("input_string: {}".format(repr(input_string)))
196+ # i = iter(line_end_list)
197+ # while result is -1:
198+ # try:
199+ # line_end = next(i)
200+ # except StopIteration:
201+ # result = False
202+ # # print("StopIteration")
203+ # else:
204+ # # print("line_end: {}".format(repr(line_end)))
205+ # result = input_string.find(line_end, start)
206+ # # print("result: {}".format(repr(result)))
207+ # if result is False:
208+ # result = -1
209+ # return result
210+
211+
187212def find_first_line_end (input_string , line_end_list = universal_line_end_basic , start = 0 ):
188- result = - 1
189- # print("input_string: {}".format(repr(input_string)))
190- i = iter (line_end_list )
191- while result is - 1 :
192- try :
193- line_end = next (i )
194- except StopIteration :
195- result = False
196- # print("StopIteration")
197- else :
198- # print("line_end: {}".format(repr(line_end)))
199- result = input_string .find (line_end , start )
200- # print("result: {}".format(repr(result)))
201- if result is False :
213+ result = None
214+ for line_end in line_end_list :
215+ index = input_string .find (line_end , start )
216+ # print("line_end: {: >10}; index: {}".format(repr(line_end), index))
217+ # just remember the first / smallest index
218+ if index > - 1 :
219+ if result is None :
220+ result = index
221+ else :
222+ result = min (index , result )
223+ if result is None :
202224 result = - 1
203225 return result
204226
205227
206- def splitlines_advanced (input_string , line_end_list ):
228+ def splitlines_advanced (input_string , line_end_list = universal_line_end_basic ):
207229 result = []
208230 rest = None
209231 # we have to do the splitting manually as we have a list of available seperators..
210232 pos_last = 0
211233 while (
212234 pos := find_first_line_end (input_string , line_end_list , start = pos_last )
213235 ) > - 1 :
214- result .append (input_string [:pos ])
215- print ("input_string[:pos]: {}" .format (repr (input_string [:pos ])))
216- pos_last = pos
217- if pos_last <= len (input_string ):
218- print ("ping - rest" )
236+ # print("pos: {}".format(repr(pos)))
237+ # print("input_string[pos_last:pos]: {}".format(repr(input_string[pos_last:pos])))
238+ result .append (input_string [pos_last :pos ])
239+ pos_last = pos + 1
240+ # print("pos_last: {}".format(repr(pos_last)))
241+ # print("pos_last: {}".format(repr(pos_last)))
242+ # print("len(input_string): {}".format(repr(len(input_string))))
243+ if pos_last < len (input_string ):
244+ # print(" rest handling:")
245+ # print("input_string[pos_last:]: {}".format(repr(input_string[pos_last:])))
246+ rest = input_string [pos_last :]
219247 return (result , rest )
220248
221249
222250"""
223251debugging:
224- nbs.splitlines_advanced("Hallo\n Welt\b Ein Wünder Schön€r Tag!", nbs.universal_line_end_basic)
252+ import nonblocking_serialinput as nbs
253+ nbs.splitlines_advanced("Hallo\n Welt\r ")
254+
255+ nbs.splitlines_advanced("Hallo\n Welt\r Tag!")
256+ nbs.splitlines_advanced("Hallo\n Welt\b Ein Wünder Schön€r Tag!")
257+
225258import nonblocking_serialinput as nbs
226259nbs.find_first_line_end("Hallo\n Welt\r Test", start=0)
227260nbs.find_first_line_end("Hallo\n Welt\r Test", start=5)
0 commit comments