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.
# Copyright (c) 2013/2014 Ibrahim Abdelkader <i.abdalkader@gmail.com>
# 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):
__serial.write(struct.pack("<BBI", __USBDBG_CMD, __USBDBG_SCRIPT_EXEC, len(buf)))
__serial.write(buf)
__serial.write(buf.encode())
def stop_script():
__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]
if __name__ == '__main__':
if len(sys.argv)!= 2:
print ('usage: pyopenmv.py <script>')
if len(sys.argv)!= 3:
print ('usage: pyopenmv.py <port> <script>')
sys.exit(1)
with open(sys.argv[1], 'r') as fin:
with open(sys.argv[2], 'r') as fin:
buf = fin.read()
s = serial.Serial("/dev/openmvcam", 921600, timeout=0.3)
init(s)
disconnect()
init(sys.argv[1])
stop_script()
exec_script(buf)
tx_len = tx_buf_len()
time.sleep(0.250)
if (tx_len):
print(tx_buf(tx_len))
s.close()
print(tx_buf(tx_len).decode())
disconnect()

View File

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