From 98ecd3714b6f7b42c7f20ff46ddff80a7f9358bc Mon Sep 17 00:00:00 2001 From: Gino D Date: Sat, 31 Dec 2022 04:10:28 +0100 Subject: [PATCH] Save work in progress... --- picowatch_d/picowatch.py | 146 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 135 insertions(+), 11 deletions(-) diff --git a/picowatch_d/picowatch.py b/picowatch_d/picowatch.py index dc9c8d4..5b2cc85 100644 --- a/picowatch_d/picowatch.py +++ b/picowatch_d/picowatch.py @@ -143,11 +143,10 @@ class Pyboard(object): # ctrl-B: exit raw REPL 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 - for _ in range(0, max_times): + for _ in range(0, 2): self.serial.write(b'\x03') - time.sleep(0.1) return b'raw REPL; CTRL-B to exit\r\n' @@ -156,11 +155,6 @@ class Pyboard(object): self.serial.write(b'\x04') 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]: data = self.serial.read(1) @@ -512,7 +506,13 @@ class Picowatch(object): def __init__(self, pyboard: 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 = '/'): status, output, exception = self._fs.ls(remote) @@ -600,5 +600,129 @@ class Picowatch(object): def launch(self, filepath: str): self._fs.launch(filepath) - def watch(self, filepath: str): - pass + def watch(self, filename: str): + 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() \ No newline at end of file