Skip to content

Commit 4aa1c45

Browse files
author
sasilva1998
committed
modificaciones en metodos para uso desde codigo
1 parent bda0776 commit 4aa1c45

File tree

2 files changed

+49
-13
lines changed

2 files changed

+49
-13
lines changed

cli.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ def mkdir(directory, exists_okay):
170170
is_flag=True,
171171
help="recursively list all files and (empty) directories.",
172172
)
173-
def ls(directory, long_format, recursive):
173+
def ls(board,directory, long_format, recursive):
174174
"""List contents of a directory on the board.
175175
176176
Can pass an optional argument which is the path to the directory. The
@@ -190,7 +190,7 @@ def ls(directory, long_format, recursive):
190190
ampy --port /board/serial/port ls -l /foo/bar
191191
"""
192192
# List each file/directory on a separate line.
193-
board_files = files.Files(_board)
193+
board_files = files.Files(board)
194194
for f in board_files.ls(directory, long_format=long_format, recursive=recursive):
195195
print(f)
196196

@@ -258,10 +258,7 @@ def put(local, remote=None, board=None):
258258
board_files = files.Files(board)
259259
board_files.put(remote, infile.read())
260260

261-
262-
@cli.command()
263-
@click.argument("remote_file")
264-
def rm(remote_file):
261+
def rm(remote_file,board):
265262
"""Remove a file from the board.
266263
267264
Remove the specified file from the board's filesystem. Must specify one
@@ -274,7 +271,7 @@ def rm(remote_file):
274271
ampy --port /board/serial/port rm main.py
275272
"""
276273
# Delete the provided file/directory on the board.
277-
board_files = files.Files(_board)
274+
board_files = files.Files(board)
278275
board_files.rm(remote_file)
279276

280277

pyboard.py

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ def __init__(self, device, baudrate=115200, user='micro', password='python', wai
148148
if delayed:
149149
print('')
150150

151+
self.uioutput=""
152+
151153
def close(self):
152154
self.serial.close()
153155

@@ -160,8 +162,41 @@ def read_until(self, min_num_bytes, ending, timeout=10, data_consumer=None):
160162
if data.endswith(ending):
161163
break
162164
elif self.serial.inWaiting() > 0:
163-
new_data = self.serial.read(1)
165+
new_data = self.serial.read(1)
166+
data = data + new_data
167+
if data_consumer:
168+
data_consumer(new_data)
169+
timeout_count = 0
170+
else:
171+
timeout_count += 1
172+
if timeout is not None and timeout_count >= 100 * timeout:
173+
break
174+
time.sleep(0.01)
175+
return data
176+
177+
def read_until_output(self, min_num_bytes, ending, timeout=10, data_consumer=None,ter=None):
178+
data = self.serial.read(min_num_bytes)
179+
if data_consumer:
180+
data_consumer(data)
181+
timeout_count = 0
182+
while True:
183+
if data.endswith(ending):
184+
break
185+
elif self.serial.inWaiting() > 0:
186+
new_data = self.serial.read(1)
164187
data = data + new_data
188+
try:
189+
ded=str(data).split("\\")
190+
if ded[-1]=="n'" and ded[-2]=="r":
191+
ded=str(data)[2:-5]
192+
self.uioutput=self.uioutput+ded+"\n"
193+
print(self.uioutput)
194+
ter.value=self.uioutput
195+
ded=None
196+
data=b''
197+
except Exception as e:
198+
print(e)
199+
165200
if data_consumer:
166201
data_consumer(new_data)
167202
timeout_count = 0
@@ -215,21 +250,23 @@ def exit_raw_repl(self):
215250
self.serial.write(b'\r\x02') # ctrl-B: enter friendly REPL
216251

217252
def follow(self, timeout, data_consumer=None):
253+
218254
# wait for normal output
219255
data = self.read_until(1, b'\x04', timeout=timeout, data_consumer=data_consumer)
220256
if not data.endswith(b'\x04'):
221257
raise PyboardError('timeout waiting for first EOF reception')
222258
data = data[:-1]
223-
224259
# wait for error output
225260
data_err = self.read_until(1, b'\x04', timeout=timeout)
226261
if not data_err.endswith(b'\x04'):
227262
raise PyboardError('timeout waiting for second EOF reception')
228263
data_err = data_err[:-1]
229-
230264
# return normal and error output
231265
return data, data_err
232266

267+
def follow_output(self, timeout, data_consumer=None,term=None):
268+
data = self.read_until_output(1, b'\x04', timeout=timeout, data_consumer=data_consumer,ter=term)
269+
233270
def exec_raw_no_follow(self, command):
234271
if isinstance(command, bytes):
235272
command_bytes = command
@@ -280,11 +317,13 @@ def get_time(self):
280317
# but for Python3 we want to provide the nicer version "exec"
281318
setattr(Pyboard, "exec", Pyboard.exec_)
282319

283-
def execfile(filename, device='/dev/ttyACM0', baudrate=115200, user='micro', password='python'):
320+
def execfile(filename, device='/dev/ttyACM0', baudrate=115200, user='micro', password='python',terminal=None):
284321
pyb = Pyboard(device, baudrate, user, password)
285322
pyb.enter_raw_repl()
286-
output = pyb.execfile(filename)
287-
stdout_write_bytes(output)
323+
with open(filename, 'rb') as f:
324+
pyfile = f.read()
325+
pyb.exec_raw_no_follow(pyfile)
326+
pyb.follow_output(10, None, term=terminal)
288327
pyb.exit_raw_repl()
289328
pyb.close()
290329

0 commit comments

Comments
 (0)