Combining Native Code with Micropython

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
cefn
Posts: 230
Joined: Tue Aug 09, 2016 10:58 am

Combining Native Code with Micropython

Post by cefn » Fri Jan 06, 2017 3:03 pm

I would like to combine a capability for which native code already exists (with Espressif toolchain)

For example this...
https://github.com/wdim0/esp8266_fast_lcd_driver_hspi
...would be amazing to have access to from Python. It's demonstrated in this video...
https://www.youtube.com/watch?v=X0mwfAqiqkc

1) What are the keywords describing the tools, steps and skills I would need to achieve this, so I can start learning how?
2) Are there worked examples where a new native capability has been added to the Micropython libraries?
3) Is there a way to do this from a pre-existing Arduino example?

I have built my own Micropython img from source, so I have access to the tools at least, but need orientation to begin gaining the knowhow.

Cefn

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Combining Native Code with Micropython

Post by pythoncoder » Sat Jan 07, 2017 8:30 am

I take it you want to write a C module to build into the firmware which provides a MicroPython interface. If so, a forum search will provide plenty of guidance. Dave Hylands (@dhylands) in particular has posted a number of guides.

There are also examples of Python display drivers but these are mainly targeted at the Pyboard. Whether the ESP8266 has sufficient RAM to support larger displays is moot, but the device has a built-in frame buffer so it may well be possible, especially with frozen bytecode.
Peter Hinch
Index to my micropython libraries.

cefn
Posts: 230
Joined: Tue Aug 09, 2016 10:58 am

Re: Combining Native Code with Micropython

Post by cefn » Sat Apr 15, 2017 1:45 am

Thanks for your reply! I'll take a look for the posts you mention. Having that extra info of specific users to track who have provided 'tutorials' is handy as I'm a bit lost in all the detail of discussions between experts otherwise.

For the pixel display project I ended up putting together a simple driver for the st7920 monochrome display, which you can see here...
https://github.com/ShrimpingIt/micropython-st7920

...and some supporting bitfont rendering logic in a generic python-3-compatible bitfont library...
https://github.com/ShrimpingIt/bitfont

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Combining Native Code with Micropython

Post by dhylands » Sat Apr 15, 2017 6:01 pm

Here's a thread on writing modules in C:
viewtopic.php?f=2&t=1411
The latest code for that sample can be found here:
https://github.com/dhylands/micropython/tree/c-sample2
in this commit:
https://github.com/dhylands/micropython ... 57f5d7281b

@deshpu put together a "Hacking MicroPython" guide, which you can find here:
viewtopic.php?f=3&t=2370&p=13615&hilit=book#p13615

Edit: April 21, 2017 - I rebased the c_sample2 branch to the latest master, and I added a reference to that commit above.

For my own reference, the other conversation which I updated was this one:
viewtopic.php?f=2&t=1411

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Combining Native Code with Micropython

Post by pythoncoder » Mon Apr 17, 2017 6:29 am

@cefn Your font rendering logic appears to be dependent on PIL (pillow fork). Did you manage to run this on the ESP8266? If so, how did you do it? From a quick look it seems like a large library.
Peter Hinch
Index to my micropython libraries.

cefn
Posts: 230
Joined: Tue Aug 09, 2016 10:58 am

Re: Combining Native Code with Micropython

Post by cefn » Mon Apr 24, 2017 6:00 pm

The desktop python rendering test case relies on Pillow, but bitfont () simply takes a callback

Code: Select all

plot(x,y)
function, which can be provided by anything.

In the case of my current project using my st7920 library (https://github.com/ShrimpingIt/micropython-st7920) on ESP8266, a test case is as follows...

Code: Select all

from st7920 import Screen
from machine import Pin
screen = Screen(slaveSelectPin=Pin(15))
def plot(x, y):
    screen.plot(x, y)
screen.clear()
font.draw_line("Hello World", plot)
screen.redraw()
Recent commits provide an untested draw_generator function to render paragraphs which are constructed algorithmically (for example using https://github.com/pfalcon/utemplate ) without any string concatenation.

However, it's looking like I may have to abandon the NodeMCU for my project because of Memory issues - I'm just a few hundred bytes off running everything reliably including mfrc522 and st7920, but heading towards a hard deadline on Thursday. Any suggestions for a reference list of tricks to use to reclaim heap would be invaluable at this stage, (I've used all the tricks I can find so far) to the extent I might need to put a bounty on it, with my fallback being to switch to using the Pi Zero.

I'll post the heap issue in a separate thread.

Post Reply