Update openmv Python module.

* Make it work with Python2/3
* Fix command line args.
* Fix pygame key events handling.
This commit is contained in:
iabdalkader 2018-11-10 02:32:12 +02:00
parent edf5a9fbf3
commit ec7bca0a3d
2 changed files with 36 additions and 41 deletions

View File

@ -1,4 +1,4 @@
#!/usr/bin/env python2 #!/usr/bin/env python
# This file is part of the OpenMV project. # This file is part of the OpenMV project.
# Copyright (c) 2013/2014 Ibrahim Abdelkader <i.abdalkader@gmail.com> # Copyright (c) 2013/2014 Ibrahim Abdelkader <i.abdalkader@gmail.com>
# This work is licensed under the MIT license, see the file LICENSE for details. # This work is licensed under the MIT license, see the file LICENSE for details.
@ -105,7 +105,7 @@ def fb_dump():
def exec_script(buf): def exec_script(buf):
__serial.write(struct.pack("<BBI", __USBDBG_CMD, __USBDBG_SCRIPT_EXEC, len(buf))) __serial.write(struct.pack("<BBI", __USBDBG_CMD, __USBDBG_SCRIPT_EXEC, len(buf)))
__serial.write(buf) __serial.write(buf.encode())
def stop_script(): def stop_script():
__serial.write(struct.pack("<BBI", __USBDBG_CMD, __USBDBG_SCRIPT_STOP, 0)) __serial.write(struct.pack("<BBI", __USBDBG_CMD, __USBDBG_SCRIPT_STOP, 0))
@ -167,16 +167,19 @@ def arch_str():
return __serial.read(64).split('\0', 1)[0] return __serial.read(64).split('\0', 1)[0]
if __name__ == '__main__': if __name__ == '__main__':
if len(sys.argv)!= 2: if len(sys.argv)!= 3:
print ('usage: pyopenmv.py <script>') print ('usage: pyopenmv.py <port> <script>')
sys.exit(1) sys.exit(1)
with open(sys.argv[1], 'r') as fin:
with open(sys.argv[2], 'r') as fin:
buf = fin.read() buf = fin.read()
s = serial.Serial("/dev/openmvcam", 921600, timeout=0.3) disconnect()
init(s) init(sys.argv[1])
stop_script()
exec_script(buf) exec_script(buf)
tx_len = tx_buf_len() tx_len = tx_buf_len()
time.sleep(0.250)
if (tx_len): if (tx_len):
print(tx_buf(tx_len)) print(tx_buf(tx_len).decode())
s.close() disconnect()

View File

@ -1,4 +1,4 @@
#!/usr/bin/env python2 #!/usr/bin/env python
import sys import sys
import numpy as np import numpy as np
import pygame import pygame
@ -26,12 +26,11 @@ while(True):
# init pygame # init pygame
pygame.init() pygame.init()
# init openmv if len(sys.argv)!= 2:
if 'darwin' in sys.platform: print ('usage: pyopenmv_fb.py <serial port>')
portname = "/dev/cu.usbmodem14221" sys.exit(1)
else:
portname = "/dev/openmvcam"
portname = sys.argv[1]
connected = False connected = False
pyopenmv.disconnect() pyopenmv.disconnect()
for i in range(10): for i in range(10):
@ -68,19 +67,13 @@ while running:
# read framebuffer # read framebuffer
fb = pyopenmv.fb_dump() fb = pyopenmv.fb_dump()
if fb != None:
if fb == None:
continue
# create image from RGB888 # create image from RGB888
image = pygame.image.frombuffer(fb[2].flat[0:], (fb[0], fb[1]), 'RGB') image = pygame.image.frombuffer(fb[2].flat[0:], (fb[0], fb[1]), 'RGB')
# TODO check if res changed # TODO check if res changed
screen = pygame.display.set_mode((fb[0], fb[1]), pygame.DOUBLEBUF, 32) screen = pygame.display.set_mode((fb[0], fb[1]), pygame.DOUBLEBUF, 32)
fps = Clock.get_fps() fps = Clock.get_fps()
if fps < 50.0:
sys.stderr.write("WARNING: fps drop\n")
# blit stuff # blit stuff
screen.blit(image, (0, 0)) screen.blit(image, (0, 0))
screen.blit(font.render("FPS %.2f"%(fps), 1, (255, 0, 0)), (0, 0)) screen.blit(font.render("FPS %.2f"%(fps), 1, (255, 0, 0)), (0, 0))
@ -88,7 +81,7 @@ while running:
# update display # update display
pygame.display.flip() pygame.display.flip()
event = pygame.event.poll() for event in pygame.event.get():
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
running = False running = False
elif event.type == pygame.KEYDOWN: elif event.type == pygame.KEYDOWN:
@ -97,6 +90,5 @@ while running:
if event.key == pygame.K_c: if event.key == pygame.K_c:
pygame.image.save(image, "capture.png") pygame.image.save(image, "capture.png")
pygame.quit() pygame.quit()
pyopenmv.stop_script() pyopenmv.stop_script()