Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add micropython for running python programs #471

Merged
merged 9 commits into from
Aug 27, 2022

Conversation

krakowski
Copy link
Contributor

Hi,

this PR adds micropython to the sources, enabling the execution of python scripts on the yi camera.

After #461 is merged, a small python program could be used to receive messages (via the ffi module) in a loop, instead of having ipc_notify spawn a new process for each message received.

@krakowski krakowski changed the title Add micropython for running python programs [WIP] Add micropython for running python programs Aug 6, 2022
@krakowski krakowski marked this pull request as draft August 6, 2022 20:38
@krakowski krakowski changed the title [WIP] Add micropython for running python programs Add micropython for running python programs Aug 6, 2022
@krakowski
Copy link
Contributor Author

I just wrote a simple python program which receives a message from the ipc_dispatch queue, which works great!

#!/tmp/sd/yi-hack/bin/python3

import ffi

# Constants
O_RDWR     = 0x0002
O_CREAT    = 0x0200
O_NONBLOCK = 0x0004
BUF_SIZE   = 512
QUEUE_NAME = "/ipc_dispatch"

# Open standard library
libc = ffi.open("libc.so.6")

# Lookup mq_open and mq_receive functions
mq_open    = libc.func("p", "mq_open",    "si")
mq_receive = libc.func("i", "mq_receive", "isip")

# Call mq_open on /ipc_dispatch queue
queue = mq_open(QUEUE_NAME, O_RDWR)

# Receive one message
buf = bytearray(BUF_SIZE)
mq_receive(queue, buf, BUF_SIZE, None)
print(''.join('{:02x}'.format(byte) for byte in buf))

I think this opens many new possibilities regarding plugins. Looking forward to feedback and ideas 🙂

@roleoroleo
Copy link
Owner

I think you have forgotten a redirection in the file system.sh

About micropython compilation I have a couple of question:

  1. The build process cannot find ffi.h. Probably you have this include in the host and it works but in my case I have to add CFLAGS in compile.micropython:
    export CFLAGS_MOD=-I(PATH_TO_ffi.h)
  2. I also have a linker problem. I need to chagne LD_FLAGS_MOD to
    export LDFLAGS_MOD="-L${SCRIPT_DIR}/libffi/_install/lib -lffi"
  3. The last section of compile.micropython:
# Copy upip package manager for micropython
#mkdir -p "${SCRIPT_DIR}/_install/.micropython/lib" || exit 1
cp ./build-standard/frozen_mpy/upip.mpy "${SCRIPT_DIR}/_install/.micropython/lib" || exit 1
cp ./build-standard/frozen_mpy/upip_utarfile.mpy "${SCRIPT_DIR}/_install/.micropython/lib" || exit 1

doesn't work for me, the path doesn't exist.

Could you please check these problems?

@krakowski
Copy link
Contributor Author

Hi @roleoroleo,

I think you have forgotten a redirection in the file system.sh

Do you get errors on startup?

About micropython compilation I have a couple of question:

  1. The build process cannot find ffi.h. Probably you have this include in the host and it works but in my case I have to add CFLAGS in compile.micropython:
    export CFLAGS_MOD=-I(PATH_TO_ffi.h)
  2. I also have a linker problem. I need to chagne LD_FLAGS_MOD to
    export LDFLAGS_MOD="-L${SCRIPT_DIR}/libffi/_install/lib -lffi"

You are right, I accidentaly installed libffi inside the dev container and didn't notice that the compiler used the host's header file. This should be fixed now 🙂

  1. The last section of compile.micropython:
# Copy upip package manager for micropython
#mkdir -p "${SCRIPT_DIR}/_install/.micropython/lib" || exit 1
cp ./build-standard/frozen_mpy/upip.mpy "${SCRIPT_DIR}/_install/.micropython/lib" || exit 1
cp ./build-standard/frozen_mpy/upip_utarfile.mpy "${SCRIPT_DIR}/_install/.micropython/lib" || exit 1

doesn't work for me, the path doesn't exist.

Just to be sure: Did you comment out the mkdir command by accident? Which path exactly does not exist?
I just rebuild the dev container (dropping any state it had before) and ran init.micrypthon && compile.micropython which works with the latest commit.

@roleoroleo
Copy link
Owner

roleoroleo commented Aug 25, 2022

Do you get errors on startup?

No, simply /etc/profile is not filled:
echo "PATH=/tmp/sd/yi-hack/bin:/tmp/sd/yi-hack/sbin:/tmp/sd/yi-hack/usr/bin:\$PATH"

Just to be sure: Did you comment out the mkdir command by accident?

Yes, sorry.

Which path exactly does not exist?
I just rebuild the dev container (dropping any state it had before) and ran init.micrypthon && compile.micropython which works with the latest commit.

./build-standard doesnt' exists
Inside micropython-1.19/ports/unix I have variants and x86_64-pc-linux-gnu folders.
Probably it depends on the host architecture.
I'm using a 64 bit debian.

@krakowski
Copy link
Contributor Author

No, simply /etc/profile is not filled: echo "PATH=/tmp/sd/yi-hack/bin:/tmp/sd/yi-hack/sbin:/tmp/sd/yi-hack/usr/bin:\$PATH"

Yes you are right. Seems I missed this line 😅 Fixed with 8a5ce39

./build-standard doesnt' exists Inside micropython-1.19/ports/unix I have variants and x86_64-pc-linux-gnu folders. Probably it depends on the host architecture. I'm using a 64 bit debian.

It seems that the host's toolchain is picked up. I don't really know why this is happening. Did you

export STAGING_DIR="/opt/yi/toolchain-sunxi-musl"
export PATH="/opt/yi/toolchain-sunxi-musl/toolchain/bin:~/.local/bin:$PATH"

before running init.micropython and compile.micropython? The latest commit 3b62f4b explicitly sets the variant name and the build output folder and uses absolute paths for copying the build files. Maybe this will work.

@roleoroleo
Copy link
Owner

roleoroleo commented Aug 26, 2022

The latest commit 3b62f4b fixes the problem of the folder.
The last problem I encountered is install.libffi, you need to add a "../":

mkdir -p ../../../build/yi-hack/bin/ || exit 1
rsync -av ./_install/* ../../../build/yi-hack/ || exit 1

@roleoroleo
Copy link
Owner

Ah no...
Another question.
The SD card is FAT32 formatted, so doesn't support symbolic links.
You should copy libffi.so.8.1.0 to libffi.so.8 and libffi.so

@krakowski
Copy link
Contributor Author

c0a5902 added the missing ../ in install.libffi and fixed the cp command in compile.libffi to use dereference mode, so that symbolic links are followed and the actual files are copied. I hope it works now 😁

@krakowski krakowski marked this pull request as ready for review August 26, 2022 16:59
@roleoroleo
Copy link
Owner

Yes, it works properly.
Thank you for your contribution.

@roleoroleo roleoroleo merged commit f3c53ea into roleoroleo:master Aug 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants