diff --git a/README.md b/README.md index ba349fe..5a79e06 100644 --- a/README.md +++ b/README.md @@ -98,16 +98,16 @@ ext_tree = flipper.storage.tree('/ext') file_info = flipper.storage.stat("/ext/foo/bar.txt") #Make directory -file_info = flipper.storage.mkdir("/ext/foo") +flipper.storage.mkdir("/ext/foo") ``` #### Files ```python #Read file -file_info = flipper.storage.read("/ext/foo/bar.txt") +file= flipper.storage.read("/ext/foo/bar.txt") #Read file chunks -file_info = flipper.storage.read("/ext/foo/bar.txt", 42) +file = flipper.storage.read("/ext/foo/bar.txt", 42) #Remove file flipper.storage.remove("/ext/foo/bar.txt") @@ -215,10 +215,20 @@ flipper.gpio.mode(PIN_NAME, 1) #Play song in RTTTL format rttl_song = "Littleroot Town - Pokemon:d=4,o=5,b=100:8c5,8f5,8g5,4a5,8p,8g5,8a5,8g5,8a5,8a#5,8p,4c6,8d6,8a5,8g5,8a5,8c#6,4d6,4e6,4d6,8a5,8g5,8f5,8e5,8f5,8a5,4d6,8d5,8e5,2f5,8c6,8a#5,8a#5,8a5,2f5,8d6,8a5,8a5,8g5,2f5,8p,8f5,8d5,8f5,8e5,4e5,8f5,8g5" +#Play in loop flipper.music_player.play(song) +#Stop loop +flipper.music_player.stop() + +#Play for 20 seconds +flipper.music_player.play(song, duration=20) + #Beep flipper.music_player.beep() + +#Beep for 5 seconds +flipper.music_player.beep(duration=5) ``` ### NFC diff --git a/lib/serial_wrapper.py b/lib/serial_wrapper.py index 048fb8b..8af2769 100644 --- a/lib/serial_wrapper.py +++ b/lib/serial_wrapper.py @@ -16,15 +16,14 @@ class SerialWrapper: def __init__(self, port) -> None: self._serial_port = serial.Serial(port=port, baudrate=9600, bytesize=8, timeout=None, stopbits=serial.STOPBITS_ONE) - self._serial_port.read_until(b'>') + self._serial_port.read_until(b'>:') @error_handler - def send(self, payload): - self._serial_port.read() + def send(self, payload: str) -> str: self._serial_port.write(f"{payload}\r".encode()) time.sleep(0.5) self._serial_port.readline() - return self._serial_port.read_until(b'>').decode().rstrip('>\r\n') + return self._serial_port.read_until(b'>:').decode().rstrip('\r\n') def ctrl_c(self): self._serial_port.write(b'\x03') diff --git a/lib/storage.py b/lib/storage.py index 53e6083..ab99632 100644 --- a/lib/storage.py +++ b/lib/storage.py @@ -63,11 +63,17 @@ def remove(self, file): return self._serial_wrapper.send(f"storage remove {file}") def read(self, file): - return self._serial_wrapper.send(f"storage read {file}").split('\r\n')[1] + try: + return self._serial_wrapper.send(f"storage read {file}").split('\r\n')[1] + except IndexError: + return "" def read_chunk(self, file, chunks): - return self._serial_wrapper.send(f"storage read_chunks {file} {chunks}").split('\r\n')[1] - + try: + return self._serial_wrapper.send(f"storage read_chunks {file} {chunks}").split('\r\n')[1] + except IndexError: + return "" + def copy(self, source, destination): return self._serial_wrapper.send(f"storage copy {source} {destination}") diff --git a/lib/threaded.py b/lib/threaded.py index 3202a2e..49c50bd 100644 --- a/lib/threaded.py +++ b/lib/threaded.py @@ -9,12 +9,11 @@ def __init__(self) -> None: def exec(self, func, callback, timeout): def _run(): data = func() - if data: + if data and callback: callback(func()) def _timer(): - if self.thread.is_alive(): - time.sleep(timeout) - self.stop() + time.sleep(timeout) + self.stop() if not self.thread: self.thread = Thread(target=_run) self.thread.start()