Copy main.py from Android assets to camera sdcard

Hi,

I’m trying to copy a simple script

import time
from machine import Pin
led = Pin('LED_GREEN', Pin.OUT)
while (True):
	led.on()
	time.sleep_ms(250)
	led.off()
	time.sleep_ms(250)

on a camera connected through USB to an android device.

I believe I’m successfully copying the script cat /storage/D21A-D8D4/main.py returns the copied script. Then I’m sending a reset command through serial (/dev/ttyACMX) using

val wrap = ByteBuffer.wrap(listOf(__USBDBG_CMD, __USBDBG_SYS_RESET, 0).map { it.toByte() }.toByteArray())
wrap.order(ByteOrder.LITTLE_ENDIAN)
serial?.write(wrap.array())

emulating __serial.write(struct.pack("<BBI", __USBDBG_CMD, __USBDBG_SYS_RESET, 0))

I believe the camera is properly reset… But once it’s accessible again, either main.py is empty or is back to the old script.

What am I missing?

Thanks in advance.

Hi, the OS will buffer writes. Please safety remove the disk.

Right, see I’m trying to do this in order to update the running script remotely. Is there a better way to do that?

I guess you could “umount /storage/D21A-D8D4”

Ok so unmounting the drive and then resetting the camera should do the trick?

Assuming resetting the device would also remount the sdcard.

Yes, that should flush the write. Wait a bit before sending the reset after the unmount.

Yep this works. Unmounting /storage/D21-D8D4 did not work as it’s not the actual mount point. umount /mnt/media_rw/D21-D8D4 did the trick.

Thanks a bunch!

Will the mount point always be D21-D8D4?

No, Linux changes this per mount.

Right, is there a way to reliably find the mount point?

Google search? If it’s a general purpose Linux question then you don’t need to ask the forums here.

Ah yes of course. I meant through any sort of rpc or api.

Anyway, thank you for your help.

Nope.

What I ended up doing

  1. Find OpenMV storage device root by scanning every external media for the file ‘.openmv_disk’;
  2. If this fails, parse dumpsys mount output to find Volume with fsLabel=OPENMV
  3. Unmounting said storage;
  4. Waiting a whole 5 seconds;
  5. Reset OpenMV camera with the above command.

The other tricky part for me was to ensure I was sending the command to the right serial ttyACMX. To find it I used the following grep 1209/abd1 /sys/class/tty/*/device/uevent and mapping the matching tty device to /dev/$tty.

Thanks for your help @kwagyeman.