Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add send_file #10

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/frameutils/bluetooth.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,43 @@ async def send_break_signal(self, show_me=False):
If `show_me=True`, the exact bytes send to the device will be printed.
"""
await self._transmit(bytearray(b"\x03"), show_me=show_me)

async def send_file(self, filename, file_path=None, file_string=None):
"""
Sends a file to Frame. Pass either a file path or a string to be uploaded.
"""

data_string = ""
if (file_string != None):
data_string = file_string

elif (file_path != None):
with open(file_path, 'r') as f:
data_string = f.read()

data_string = data_string.replace("'", "\\'")
data_string = data_string.replace('"', '\\"')
data_string = data_string.replace("\n", "\\n")

await self.send_break_signal()
await self.send_lua(f"f=frame.file.open('{filename}', 'w');print(nil)", await_print=True)

# exclude size of f:write("");print(nil)
max_chunk_size = self.max_lua_payload() - 22
current_offset = 0
while True:
chunk = ""
if max_chunk_size < len(data_string[current_offset:]):
if data_string[current_offset+max_chunk_size] == '\\':
chunk = data_string[current_offset:current_offset+max_chunk_size-2]
current_offset += max_chunk_size - 2
else:
chunk = data_string[current_offset:current_offset+max_chunk_size]
current_offset += max_chunk_size
await self.send_lua(f'f:write("{chunk}");print(nil)', await_print=True)
else:
chunk = data_string[current_offset:]
await self.send_lua(f'f:write("{chunk}");print(nil)', await_print=True)
break

await self.send_lua('f:close()')
1 change: 1 addition & 0 deletions tests/test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
frame.bluetooth.receive_callback((function(d)frame.bluetooth.send(d)end))
34 changes: 34 additions & 0 deletions tests/test_bluetooth.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,40 @@ async def test_mtu(self):

await b.disconnect()

async def test_send_file_string(self):
b = Bluetooth()
await b.connect()

lua_string = """
frame.bluetooth.receive_callback((function(d)frame.bluetooth.send(d)end))
"""

await b.send_file(filename='main.lua', file_string=lua_string)
await asyncio.sleep(0.1)
await b.send_reset_signal()
await asyncio.sleep(1)

self.assertEqual(await b.send_data(b"test", await_data=True), bytearray(b'test'))

await b.send_lua("frame.file.remove('main.lua'); print(nil)", await_print=True)

await b.disconnect()

async def test_send_file_path(self):
b = Bluetooth()
await b.connect()

await b.send_file(filename='main.lua', file_path='test.lua')
await asyncio.sleep(0.1)
await b.send_reset_signal()
await asyncio.sleep(1)

self.assertEqual(await b.send_data(b"test", await_data=True), bytearray(b'test'))

await b.send_lua("frame.file.remove('main.lua'); print(nil)", await_print=True)

await b.disconnect()


if __name__ == "__main__":
unittest.main()