Skip to content

Commit

Permalink
* update
Browse files Browse the repository at this point in the history
  • Loading branch information
lxowalle committed Sep 5, 2024
1 parent 0416da8 commit 448a3c2
Show file tree
Hide file tree
Showing 10 changed files with 243 additions and 61 deletions.
21 changes: 19 additions & 2 deletions examples/tools/maixcam_switch_usb_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,28 @@

cmd_restart = "/etc/init.d/S03usbdev stop &&/etc/init.d/S03usbdev start"

def usb_devive():
device_list = [
["/boot/usb.ncm", True],
["/boot/usb.rndis", True],
["/boot/usb.keyboard", False],
["/boot/usb.mouse", False],
["/boot/usb.touchpad", False]
]

def usb_devive(device_list):
if os.path.exists("/boot/usb.host"):
os.remove("/boot/usb.host")
with open("/boot/usb.dev", "w") as f:
pass

for device in device_list:
if device[1]:
with open(device[0], "w") as f:
pass
else:
if os.path.exists(device[0]):
os.remove(device[0])

ret = os.system(cmd_restart)
if ret != 0:
raise Exception("set device mode failed")
Expand Down Expand Up @@ -35,7 +52,7 @@ def list_usb_devices():
# mode = "host" # MaixCAM as host, you can plugin devices to MaixCAM's USB like USB camera.

if mode == "device":
usb_devive()
usb_devive(device_list)
else:
usb_host()
print("USB devices:")
Expand Down
16 changes: 12 additions & 4 deletions examples/vision/video/decode.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
from maix import video, display, app
from maix import video, display, app, time

disp = display.Display()
d = video.Decoder('/root/output.mp4')
print(f'resolution: {d.width()}x{d.height()} bitrate: {d.bitrate()} fps: {d.fps()}')
d.seek(0)

last_us = time.ticks_us()
while not app.need_exit():
img = d.decode_video()
if not img:
ctx = d.decode_video()
if not ctx:
d.seek(0)
continue
print(d.last_pts())

img = ctx.image()

while time.ticks_us() - last_us < ctx.duration_us():
time.sleep_ms(1)
last_us = time.ticks_us()

disp.show(img)
16 changes: 5 additions & 11 deletions examples/vision/video/encode.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
from maix import video, image, camera, app, time
import os
from maix import video, time, image, camera, display, app

cam = camera.Camera(640, 480, image.Format.FMT_YVU420SP)
e = video.Encoder()
f = open('/root/output.h265', 'wb')
disp = display.Display()
e = video.Encoder('/root/output.mp4')

record_ms = 2000
start_ms = time.ticks_ms()
while not app.need_exit():
img = cam.read()
frame = e.encode(img)

print(frame.size())
f.write(frame.to_bytes(False))
e.encode(img)
disp.show(img)

if time.ticks_ms() - start_ms > record_ms:
app.set_exit_flag(True)

# Pack h265 to mp4
os.system('ffmpeg -loglevel quiet -i /root/output.h265 -c:v copy -c:a copy /root/output.mp4 -y')
25 changes: 0 additions & 25 deletions examples/vision/video/encode_fast.py

This file was deleted.

19 changes: 0 additions & 19 deletions examples/vision/video/video_encode.py

This file was deleted.

5 changes: 5 additions & 0 deletions projects/app_mouse_simulator/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

build
dist
/CMakeLists.txt

Binary file added projects/app_mouse_simulator/app.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions projects/app_mouse_simulator/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
id: mouse
name: Mouse Simulator
version: 1.0.0
author: Sipeed Ltd
icon: app.png
desc: Simulate using a mouse to control the mouse
files:
- app.yaml
- icon.png
- main.py
Binary file added projects/app_mouse_simulator/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
192 changes: 192 additions & 0 deletions projects/app_mouse_simulator/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
from maix import image, display, touchscreen, time, hid, app

disp = display.Display()
ts = touchscreen.TouchScreen()
img = image.Image(disp.width(), disp.height())
mouse = None
try:
mouse = hid.Hid(hid.DeviceType.DEVICE_MOUSE)
except Exception as e:
print(e)
image.load_font("sourcehansans", "/maixapp/share/font/SourceHanSansCN-Regular.otf", size = 24)
image.set_default_font("sourcehansans")
img.draw_string(0, 0, "Maybe the HID device is not enabled.", image.COLOR_WHITE, 1)
img.draw_string(0, 50, "Try: Settings -> USB Settings -> HID Mouse, then click Confirm and reboot", image.COLOR_WHITE, 1)
img.draw_string(0, 150, "Click anywhere to exit.", image.COLOR_WHITE, 1)
while not app.need_exit():
t = ts.read()
if t[2]:
exit(0)
disp.show(img)

main_x = 0
main_y = 0
main_w = int(disp.width() * 0.8)
main_h = int(disp.height() * 0.8)
key_w = int(main_w / 2)
key_h = int(disp.height() - main_h)
left_key_x = 0
left_key_y = disp.height() - key_h
right_key_x = left_key_x + key_w
right_key_y = left_key_y
wheel_w = disp.width() - main_w
wheel_h = main_h
wheel_x = main_w
wheel_y = 0
exit_w = disp.width() - main_w
exit_h = disp.height() - main_h
exit_x = main_w
exit_y = main_h

main_box = [main_x, main_y, main_w, main_h]
left_key_box = [left_key_x, left_key_y, key_w, key_h]
right_key_box = [right_key_x, right_key_y, key_w, key_h]
wheel_box = [wheel_x, wheel_y, wheel_w, wheel_h]
exit_box = [exit_x, exit_y, exit_w, exit_h]

delay_ms = 100
# left key param
touch_left_key_last_ms = time.ticks_ms()
keep_left_key_last_ms = time.ticks_ms()
left_key_touched = 0
# right key param
touch_right_key_last_ms = time.ticks_ms()
keep_right_key_last_ms = time.ticks_ms()
right_key_touched = 0
# wheel movement param
wheel_first_touch = 0
wheel_first_y = 0
# main param
main_first_touch = 0
main_first_x = 0
main_first_y = 0
main_first_press = 0
main_first_press_x = 0
main_first_press_y = 0
def touch_box(t, box, oft = 0):
if t[2] and t[0] + oft > box[0] and t[0] < box[0] + box[2] + oft and t[1] + oft > box[1] and t[1] < box[1] + box[3] + oft:
return True
else:
return False

def mouse_set(button, x, y, wheel_move):
data = [button, x, y, wheel_move]
mouse.write(data)

def caculate_main_oft(t, last_x, last_y):
main_oft_x = t[0] - last_x
main_oft_y = t[1] - last_y
max_level = 100
main_oft_x2 = int(main_oft_x / (main_w // 2) * max_level)
main_oft_y2 = int(main_oft_y / (main_h // 2) * max_level)
print(f"curr:{t[0]} first:{main_first_press_x} x offset:{main_oft_x} x offset2:{main_oft_x2}")
print(f"curr:{t[1]} first:{main_first_press_y} y offset:{main_oft_y} y offset2:{main_oft_y2}")
return main_oft_x2, main_oft_y2

while not app.need_exit():
t = ts.read()
img.clear()
img.draw_rect(main_x, main_y, main_w, main_h, image.COLOR_WHITE, 2)
img.draw_string(main_x + 10, main_y + 10, "TOUCHPAD", image.COLOR_WHITE, 2)

img.draw_rect(wheel_x, wheel_y, wheel_w, wheel_h, image.COLOR_WHITE, 2)
img.draw_string(wheel_x + 10, wheel_y + 10, "WHEEL", image.COLOR_WHITE, 2)

img.draw_rect(exit_x, exit_y, exit_w, exit_h, image.COLOR_WHITE, 2)
img.draw_string(exit_x + 10, exit_y + 10, "EXIT", image.COLOR_WHITE, 2)

# check exit
if touch_box(t, exit_box, 0):
print('exit')
break

# check the left key is touch
if touch_box(t, left_key_box, 0):
keep_left_key_last_ms = time.ticks_ms()
if time.ticks_ms() - touch_left_key_last_ms > delay_ms:
touch_left_key_last_ms = time.ticks_ms()
left_key_touched = 1
print("press left key")
mouse_set(0x01, 0, 0, 0)

# check the right key is touch
if touch_box(t, right_key_box, 0):
keep_right_key_last_ms = time.ticks_ms()
if time.ticks_ms() - touch_right_key_last_ms > delay_ms:
touch_right_key_last_ms = time.ticks_ms()
right_key_touched = 1
print("press right key")
mouse_set(0x02, 0, 0, 0)

# draw left key
if left_key_touched:
img.draw_rect(left_key_x, left_key_y, key_w, key_h, image.COLOR_WHITE, -1)
img.draw_string(left_key_x + 10, left_key_y + 10, "LEFT KEY", image.COLOR_WHITE, 2)
if time.ticks_ms() - keep_left_key_last_ms > 30:
left_key_touched = 0
print("release left key")
mouse_set(0, 0, 0, 0)
else:
img.draw_rect(left_key_x, left_key_y, key_w, key_h, image.COLOR_WHITE, 2)
img.draw_string(left_key_x + 10, left_key_y + 10, "LEFT KEY", image.COLOR_WHITE, 2)

# draw right key
if right_key_touched:
img.draw_rect(right_key_x, right_key_y, key_w, key_h, image.COLOR_WHITE, -1)
img.draw_string(right_key_x + 10, right_key_y + 10, "RIGHT KEY", image.COLOR_WHITE, 2)
if time.ticks_ms() - keep_right_key_last_ms > 30:
right_key_touched = 0
print("release right key")
mouse_set(0, 0, 0, 0)
else:
img.draw_rect(right_key_x, right_key_y, key_w, key_h, image.COLOR_WHITE, 2)
img.draw_string(right_key_x + 10, right_key_y + 10, "RIGHT KEY", image.COLOR_WHITE, 2)

# check wheel movement
if touch_box(t, wheel_box, 0):
if wheel_first_touch:
wheel_oft = t[1] - wheel_first_y
max_level = 6
wheel_oft2 = int(wheel_oft / (wheel_h // 2) * max_level)
mouse_set(0, 0, 0, -wheel_oft2)
time.sleep_ms(30)
print(f"curr:{t[1]} first:{wheel_first_y} y offset:{wheel_oft} y offset2:{wheel_oft2}")
wheel_first_y = t[1]
wheel_first_touch = 1
else:
wheel_first_touch = 0
wheel_first_y = 0

# check main
if touch_box(t, main_box, 0):
if not main_first_press:
main_first_press_x = t[0]
main_first_press_y = t[1]
main_first_press = 1
if main_first_touch:
main_oft_x2,main_oft_y2 = caculate_main_oft(t, main_first_x, main_first_y)
mouse_set(0, main_oft_x2, main_oft_y2, 0)

main_first_x = t[0]
main_first_y = t[1]
main_first_touch = 1
else:
if main_first_press:
main_oft_x2,main_oft_y2 = caculate_main_oft(t, main_first_press_x, main_first_press_y)
if main_oft_x2 == 0 and main_oft_y2 == 0:
mouse_set(0x01, 0, 0, 0)
time.sleep_ms(30)
mouse_set(0, 0, 0, 0)

main_first_touch = 0
main_first_x = 0
main_first_y = 0
main_first_press = 0
main_first_press_x = 0
main_first_press_y = 0

# draw red point
if t[2]:
img.draw_circle(t[0], t[1], 5, image.COLOR_RED, -1)

disp.show(img)

0 comments on commit 448a3c2

Please sign in to comment.