diff --git a/examples/tools/maixcam_switch_usb_mode.py b/examples/tools/maixcam_switch_usb_mode.py index b8536bf6..511ca0e3 100644 --- a/examples/tools/maixcam_switch_usb_mode.py +++ b/examples/tools/maixcam_switch_usb_mode.py @@ -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") @@ -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:") diff --git a/examples/vision/video/decode.py b/examples/vision/video/decode.py index 0145a7ec..5ab0689a 100644 --- a/examples/vision/video/decode.py +++ b/examples/vision/video/decode.py @@ -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) \ No newline at end of file diff --git a/examples/vision/video/encode.py b/examples/vision/video/encode.py index 82fb3bb5..1a2b93ca 100644 --- a/examples/vision/video/encode.py +++ b/examples/vision/video/encode.py @@ -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') diff --git a/examples/vision/video/encode_fast.py b/examples/vision/video/encode_fast.py deleted file mode 100644 index 7ecd6779..00000000 --- a/examples/vision/video/encode_fast.py +++ /dev/null @@ -1,25 +0,0 @@ -from maix import video, time, image, camera, display, app -import os - -cam = camera.Camera(640, 480, image.Format.FMT_YVU420SP) -disp = display.Display() -e = video.Encoder(capture = True) -e.bind_camera(cam) - -f = open('/root/output.h265', 'wb') - -record_ms = 2000 -start_ms = time.ticks_ms() -while not app.need_exit(): - frame = e.encode() - img = e.capture() - disp.show(img) - - print(frame.size()) - f.write(frame.to_bytes(True)) - - 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') diff --git a/examples/vision/video/video_encode.py b/examples/vision/video/video_encode.py deleted file mode 100644 index 7495503f..00000000 --- a/examples/vision/video/video_encode.py +++ /dev/null @@ -1,19 +0,0 @@ -from maix import camera, display, time, image, app, video - -cam = camera.Camera(640, 480, image.Format.FMT_YVU420SP) # Manually set resolution, default is too large -disp = display.Display() - -v = video.Video("/root/output.h265", cam.width(), cam.height(), capture = True) -v.bind_camera(cam) - -record_ms = 5000 -start_ms = time.ticks_ms() -while not app.need_exit(): - v.encode() - img = v.capture() - - disp.show(img) - - if time.ticks_ms() - start_ms > record_ms: - v.finish() - app.set_exit_flag(True) diff --git a/projects/app_mouse_simulator/.gitignore b/projects/app_mouse_simulator/.gitignore new file mode 100644 index 00000000..babf76a7 --- /dev/null +++ b/projects/app_mouse_simulator/.gitignore @@ -0,0 +1,5 @@ + +build +dist +/CMakeLists.txt + diff --git a/projects/app_mouse_simulator/app.png b/projects/app_mouse_simulator/app.png new file mode 100644 index 00000000..10597579 Binary files /dev/null and b/projects/app_mouse_simulator/app.png differ diff --git a/projects/app_mouse_simulator/app.yaml b/projects/app_mouse_simulator/app.yaml new file mode 100644 index 00000000..fefd200f --- /dev/null +++ b/projects/app_mouse_simulator/app.yaml @@ -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 diff --git a/projects/app_mouse_simulator/icon.png b/projects/app_mouse_simulator/icon.png new file mode 100644 index 00000000..63e01648 Binary files /dev/null and b/projects/app_mouse_simulator/icon.png differ diff --git a/projects/app_mouse_simulator/main.py b/projects/app_mouse_simulator/main.py new file mode 100644 index 00000000..f6873707 --- /dev/null +++ b/projects/app_mouse_simulator/main.py @@ -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)