Embedding in a Windows GUI application

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
abainbridge
Posts: 5
Joined: Sun Oct 04, 2020 10:19 am

Embedding in a Windows GUI application

Post by abainbridge » Sun Oct 04, 2020 10:59 am

Hi. This is my first time using Micropython, please excuse my ignorance...

I cut and pasted the code from examples/embedding/hello-embed.c into my application.

The application threw an uncaught exception due to a setup problem with the stack. I fixed this by stepping through the full micropython executable (ie not the embedding example) and seeing that it did some extra stack setup that was missing from hello-embed.c. Adding calls to mp_stack_ctrl_init() and mp_pystack_init() fixed this problem. I only mention this in case it helps others in the future.

Now I need a way to capture the stdout and stderr streams so that I can render the output in my GUI. What's the best way to do that? I see that ultimately the print in the example ends up being written to stdout in fdfile_write() from ports/unix/file.c. I can hack in something at that level to call a callback in my application to transfer the buffer contents. But I'm wondering if Micropython already has an interface to allow me to capture stdout/stderr without modifying the Micropython code.

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: Embedding in a Windows GUI application

Post by stijn » Mon Oct 05, 2020 7:13 am

See mpconfigport.h and mpprint.h: most printing happens via the objects mp_plat_print, mp_sys_stdout_print, mp_stderr_print. If you change their definition you change where the output ends up. Those are meant to be used to do what you want and it's a better solution than your callback approach because fdfile_write gets used for all writing, including writing to arbitrary files.

abainbridge
Posts: 5
Joined: Sun Oct 04, 2020 10:19 am

Re: Embedding in a Windows GUI application

Post by abainbridge » Mon Oct 05, 2020 6:59 pm

OK, thanks for the pointers.

Post Reply