|
| 1 | +from aioconsole import ainput |
| 2 | +from frameutils import Bluetooth |
| 3 | +import asyncio |
| 4 | + |
| 5 | +image_buffer = b"" |
| 6 | + |
| 7 | + |
| 8 | +def receive_data(data): |
| 9 | + global image_buffer |
| 10 | + |
| 11 | + if len(data) == 1: |
| 12 | + with open("temp_focus_image.jpg", "wb") as f: |
| 13 | + f.write(image_buffer) |
| 14 | + image_buffer = b"" |
| 15 | + return |
| 16 | + |
| 17 | + image_buffer += data[1:] |
| 18 | + print( |
| 19 | + f"Received {str(len(image_buffer)-1)} bytes. Press enter to finish ", |
| 20 | + end="\r", |
| 21 | + ) |
| 22 | + |
| 23 | + |
| 24 | +async def main(): |
| 25 | + |
| 26 | + lua_script = """ |
| 27 | + local last_autoexp_time = 0 |
| 28 | + local state = 'CAPTURE' |
| 29 | + local state_time = 0 |
| 30 | +
|
| 31 | + while true do |
| 32 | + if state == 'CAPTURE' then |
| 33 | + frame.camera.capture { quality_factor = 10 } |
| 34 | + state_time = frame.time.utc() |
| 35 | + state = 'WAIT' |
| 36 | + elseif state == 'WAIT' then |
| 37 | + if frame.time.utc() > state_time + 0.5 then |
| 38 | + state = 'SEND' |
| 39 | + end |
| 40 | + elseif state == 'SEND' then |
| 41 | + local i = frame.camera.read(frame.bluetooth.max_length() - 1) |
| 42 | + if (i == nil) then |
| 43 | + state = 'DONE' |
| 44 | + else |
| 45 | + while true do |
| 46 | + if pcall(frame.bluetooth.send, '0' .. i) then |
| 47 | + break |
| 48 | + end |
| 49 | + end |
| 50 | + end |
| 51 | + elseif state == 'DONE' then |
| 52 | + while true do |
| 53 | + if pcall(frame.bluetooth.send, '0') then |
| 54 | + break |
| 55 | + end |
| 56 | + end |
| 57 | + state = 'CAPTURE' |
| 58 | + end |
| 59 | +
|
| 60 | + if frame.time.utc() - last_autoexp_time > 0.1 then |
| 61 | + frame.camera.auto { metering = 'CENTER_WEIGHTED', exposure = -0.5, exposure_limit = 5500 } |
| 62 | + last_autoexp_time = frame.time.utc() |
| 63 | + end |
| 64 | + end |
| 65 | + """ |
| 66 | + |
| 67 | + # Connect to bluetooth and upload file |
| 68 | + b = Bluetooth() |
| 69 | + await b.connect( |
| 70 | + print_response_handler=lambda s: print(s), data_response_handler=receive_data |
| 71 | + ) |
| 72 | + |
| 73 | + print("Uploading script") |
| 74 | + |
| 75 | + await b.send_break_signal() |
| 76 | + |
| 77 | + await b.send_lua("f=frame.file.open('main.lua', 'w')") |
| 78 | + |
| 79 | + for line in lua_script.splitlines(): |
| 80 | + await b.send_lua(f'f:write("{line}\\n");print(nil)', await_print=True) |
| 81 | + |
| 82 | + await b.send_lua("f:close()") |
| 83 | + |
| 84 | + await asyncio.sleep(0.1) |
| 85 | + |
| 86 | + await b.send_reset_signal() |
| 87 | + |
| 88 | + # Wait until a keypress |
| 89 | + await ainput("") |
| 90 | + |
| 91 | + await b.send_break_signal() |
| 92 | + await b.disconnect() |
| 93 | + |
| 94 | + |
| 95 | +loop = asyncio.new_event_loop() |
| 96 | +loop.run_until_complete(main()) |
0 commit comments