Save work in progress...

This commit is contained in:
Gino D
2022-12-31 04:10:28 +01:00
parent e7ecde0d0c
commit 98ecd3714b

View File

@@ -143,11 +143,10 @@ class Pyboard(object):
# ctrl-B: exit raw REPL # ctrl-B: exit raw REPL
self.serial.write(b'\x02') self.serial.write(b'\x02')
def send_ctrl_c(self, max_times: int = 2) -> bytes: def send_ctrl_c(self) -> bytes:
# ctrl-C twice: interrupt any running program # ctrl-C twice: interrupt any running program
for _ in range(0, max_times): for _ in range(0, 2):
self.serial.write(b'\x03') self.serial.write(b'\x03')
time.sleep(0.1)
return b'raw REPL; CTRL-B to exit\r\n' return b'raw REPL; CTRL-B to exit\r\n'
@@ -156,11 +155,6 @@ class Pyboard(object):
self.serial.write(b'\x04') self.serial.write(b'\x04')
return b'soft reboot\r\n' return b'soft reboot\r\n'
def interupt(self, max_times: int = 2):
for _ in range(0, max_times):
self.serial.write(b'\r\x03')
time.sleep(0.05)
def read_until(self, delimiter: bytes, stream_output: bool = False) -> Optional[bytes]: def read_until(self, delimiter: bytes, stream_output: bool = False) -> Optional[bytes]:
data = self.serial.read(1) data = self.serial.read(1)
@@ -512,7 +506,13 @@ class Picowatch(object):
def __init__(self, pyboard: Pyboard): def __init__(self, pyboard: Pyboard):
self._fs = FileSystem(pyboard) self._fs = FileSystem(pyboard)
signal.signal(signal.SIGINT, lambda a, b: pyboard.interupt()) signal.signal(signal.SIGINT, lambda a, b: self.interupt())
def interupt(self):
self._fs._pyboard.send_ctrl_c()
def terminal(self, command: str):
self._fs.terminal(command, stream_output=True)
def listing(self, remote: str = '/'): def listing(self, remote: str = '/'):
status, output, exception = self._fs.ls(remote) status, output, exception = self._fs.ls(remote)
@@ -600,5 +600,129 @@ class Picowatch(object):
def launch(self, filepath: str): def launch(self, filepath: str):
self._fs.launch(filepath) self._fs.launch(filepath)
def watch(self, filepath: str): def watch(self, filename: str):
pass if filename.startswith('/'):
filename = '.' + filename
if not filename.startswith('./'):
filename = './' + filename
with open(filename, 'r') as fh:
self._fs.terminal(fh.read(), stream_output=True)
print('Welcome to picowatch lib.')
# picowatch = False
# while not picowatch:
# print('-' * 30)
# device = input('Port: ').strip()
# baudrate = input('Baudrate (115200): ').strip() or 115200
# try:
# picowatch = Picowatch(Pyboard(device=device, baudrate=baudrate))
# print(f'Connected to device: {device} at a baudrate of: {baudrate}')
# print('-' * 30)
# except Exception as e:
# print(str(e))
picowatch = Picowatch(Pyboard('COM5'))
picowatch.interupt()
# sessions = {'deleted': set(), 'modified': set()}
# def on_modified_callback(event):
# if event.is_directory == True:
# return
# source = event.src_path.replace(WATCHING_DIRECTORY, '').replace('\\', '/')
# sessions['modified'].add(source)
# def on_deleted_callback(event):
# source = event.src_path.replace(WATCHING_DIRECTORY, '').replace('\\', '/')
# if event.is_directory == True and not source.endswith('/'):
# source += '/'
# elif len(source.split('.')) == 1:
# source += '/'
# sessions['deleted'].add(source)
# def watchdog_callback():
# for source in sessions['deleted']:
# delete(source, is_directory=source.endswith('/'))
# for source in sessions['modified']:
# upload(source)
# sessions['deleted'] = set()
# sessions['modified'] = set()
# watchdog_event = PatternMatchingEventHandler(
# patterns = ['*'],
# ignore_patterns = None,
# ignore_directories = False,
# case_sensitive = True
# )
# watchdog_event.on_modified = on_modified_callback
# watchdog_event.on_deleted = on_deleted_callback
# watchdog = Observer()
# watchdog.schedule(watchdog_event, path = './', recursive = True)
# watchdog.start()
# try:
while True:
try:
message = input('>>> ').strip()
match message.split(' '):
case ['0' | 'exit']:
sys.exit()
case ['reboot' | 'reset']:
picowatch.terminal('help()')
case ['ls' | 'stat', *source]:
picowatch.listing(source[0] if source else '/')
case ['cat' | 'open' | 'contents', source]:
picowatch.contents(source)
case ['del' | 'rm' | 'delete' | 'remove', source]:
picowatch.delete(source)
case ['format']:
picowatch.delete('/')
case ['upl' | 'upload' | 'update', source]:
picowatch.upload(source)
case ['restore']:
picowatch.upload('/')
case ['download' | 'transfer', source]:
picowatch.download(source)
case ['backup']:
picowatch.download('/')
# case ['' | 'save' | 'commit']:
# watchdog_callback()
# case ['status' | 'staged']:
# for filename in sessions['deleted']:
# print('-', filename)
# for filename in sessions['modified']:
# print('+', filename)
# case ['cancel' | 'unstaged']:
# sessions['deleted'] = set()
# sessions['modified'] = set()
case ['watch' | 'test', filename]:
picowatch.watch(filename)
case _:
if message.startswith('./'):
picowatch.launch(message[2:])
elif message:
print(f'"{message}" is not recognized.')
except Exception as e:
print(str(e))
# except KeyboardInterrupt:
# watchdog.stop()
# watchdog.join()