[Music]
I love that little sting thing it's
great hi hi everyone and welcome to
Saturday afternoon um this afternoon
we've got a fellow who needs no
introduction to a lot of you Matt
trentini um Matt's been hosting the
micropython Meetup in Melbourne for uh
several years now and is a prolific
contributor to the micropython ecosystem
and culture um and he's here to talk to
us about micropython the best bits um
please make him very
welcome thanks
ni thanks Nick thanks every R um first
housekeeping if anyone's stickers we've
got a bunch of them uh they're really
cool cute so I highly highly recommend
grabbing a couple um Andrew's waving
them about over his head so go see him
uh and if you don't interrupt come grab
them afterwards no problem uh slides are
available on Discord and the eure
channel so if you want to play along at
home uh they're there uh and really have
changed the title without telling the
Pyon folks um I just couldn't get
through everything so today is just some
of the best bits of micropython um don't
tell anyone so I'm I'm Matt uh hi uh
I've got about 25 years experience which
I'm kind of proud to say that but I'm
also God I feels old um and I've been
lucky enough to play with most parts of
the software stack uh whether it's
desktop software or embedded or um Cloud
stuff uh but a lot of my time in this
domain has been working closer on the
hardware uh and about 10 or 15 years ago
I was getting a bit jaded with a lot of
that that um embedded development
because it was sort of in stuck in its
own little time capsule of building in
SE and quite slow developments when you
look around the rest the software space
and people are um trying to progress
with more agile developments can use
integration better tooling all this kind
of stuff and embedded is kind of
plotting along the way it had been for
many years and so I've been looking for
an improvement or a better way to do
things than embeded when micropython
came along I kind of jumped at it and so
uh I'm a
contributor I've created added some code
and and documentation I as Nick said
I've hosted the M meet up for for a long
time which Nick started just for anyone
that didn't get that inside note there
um I've given a couple of talks that Pon
a used in the past although they're a
little different to today in the past
have kind of gone deep today I'm sort of
going Broad and I want to give you a bit
of a a glimpse into some of the tooling
and the features which is part of the
reason why I think mic Python's a good
environment and worth your
time um I also use micropython
professionally we build medical devices
with micropython so for those that think
it's sort of a hobbyist system I'm happy
to have that argument with you after
this
talk um most of us have sort of already
talked about um I was planning on doing
some demos I'm not I just didn't have
time so uh it's mostly your talk and I
want to get you sort of interested in
some of these topics and I'll given you
link so you can go look look at things
if uh if they Peak your curiosity and
hands up does everyone know what
micropython is at least vaguely that it
runs on microcontrollers and it's python
right that's if not just hang on for the
ride you'll be
fine so starting from the the core guts
of micropython um the repel is the first
thing I wanted to talk about that's a
best bit um micropython gives you a live
reppel and and for those that have used
python you might be thinking well okay
we have a repple that's not that
exciting but having a repple that runs
on your embedded device is a complete
GameChanger because it allows you to
inspect the device you're you're able to
uh manipulate the pins or communicate to
peripherals in a live sort of fashion
and back in the days before um with c
and and C++ this is typically like a
compile deploy test it didn't work so
you try that again and again and again
and so having that live communication to
your device where we can um introspect
what's going on provides just
exploration and investigation you can
really see what's what's happening it's
a it's a really freeing way to develop
um it also gives you tab completion
which is nice although I really want to
get 33s prompt into micro time for
another
day um next one file system so again
everyone's got a file system on the
regular python environment but having a
file system on an embedded device is
actually really cool it lets you store
your code on the device and let you
store logs and sensor information and
configuration files so if you want to
change the way your system behaves just
store a file in your device seems simple
but it's a really useful
thing uh serial connection so we're
talking about the repple there's there's
a number of different ways you can get
to the reppel on the device the most
common way that is provided out of the
box is over a Serial uh connection uh
the other thing I just want to mention
is you also have file transfers over
that serial connection too micropython
has a way of of getting files on and off
the device over that same connection um
and bit of an age test I wonder how many
people actually realize what that is
[Music]
um we are going to skip over this Rabbit
Hole um but if anyone's got uh an
interest in serial factoids ask me about
this later if we have time it's kind of
interesting um Moving Straight on to MP
remote it's the official tool to use
with micropython command line tool uh
it's supported and used by the
micropython core team um half of which
are here today thanks folks um what MP
remote allows you to do is to make that
repple connection uh lets you transfer
files in a very easy sort of natural
kind of way but it also lets you run
files from your PC allows you to exe
execute Snippets of python uh let you
reset all these things here set your set
your clock on your device put your
device to sleep all sorts of things that
you want to do but today I want to
mention the mounting feature which MP
remote Mount is kind of magic and it's a
really interesting way to develop on an
embedded device because what it does is
it lets you mount a folder from your PC
on your device it's just like as I said
here it's just like having the PC as
your hard drive for your device and the
nice thing about this is that you can
develop in your regular use vs code
right we use our git repositories and so
you just develop as you would normally
you would commit as you would um and
when you're ready you just Mount that
that folder on your device and you test
and you edit your files and you test you
edit your files and you test and it's a
really quick way to do turnarounds and
uh to develop your embedded system uh
it's a relatively new feature um had it
for probably a year or so but um I want
to spread that word because we've gotten
a lot of use out of using this feature
it's it's
great uh MIP MIP is to micropython as
pip is to cpython so uh it's our package
manager for for
micropython and the important thing here
is that I like to say that micropython
has small batteries included it doesn't
have the vast um python standard library
that you would be used to uh has a much
smaller con constrained one because the
devices we're operating on have small
amounts of Flash and resources so we
need to be careful about what we deploy
so a bunch of packages are also hosted
on micropython lib which is a separate
repository and you can install those um
after you've got your device up and
running and mlet you do that easily uh
you can also install third party
libraries from GitHub or gitlab and it
makes it easy to do that as well um so
how do you do it you do it with MP
remote uh just MIP install package name
if you're used to Pip this should feel
kind of
familiar um and you can also do it
straight from GitHub if your device has
micropython like Anthony's rby Pi PW you
can also use MIP on a connected device
so you establish a connection to your
Wi-Fi um and yeah MIP install the
package which kind of feels really um
futuristic being able to install
packages on a device with like 30k of
ram um yeah it's pretty
cool H quick example should youtil is
not provided uh by default so I try to
use it doesn't exist uh MIP it's just
you know it's pseudo make me a sandwich
um and there it will work
yay thanks for the little appreciate uh
the Unix port's an important tool that
we have um and I know that Unix and
Linux are different but that's okay Unix
is like an umbrella term for the
micropython that runs on
x86 um super useful thing because it's
the same interpreter it's the same code
that you're running on Unix um so if you
get it running on the Unix Port you're
usually pretty confident it's going to
run in your device um so it's especially
useful for doing unit tests so you can
run your unit tests on x86 on the Unix
Port uh and you know it's going to
behave very similarly to the device it's
very handy to be able to configure the
memory because normally you've got
Oodles of memory on your PC but um the
Unix Port allows you to specify the
maximum amount of memory so you um Can
simulate more closely your your embedded
device and although it's easy to build
the Unix Port we also provide containers
so uh you can just Docker um grab grab a
Docker container of the Unix Port it's
also super useful because the tag uh can
specify the version so you can check for
aggressions between different
micropython versions um so yeah I would
encourage everyone to get familiar with
Unix
Port MP build I look I don't think it's
overstating it that it might be the best
build tool ever created for any platform
in any Universe um I did write it
recently um so I might be a little bit
biased um it does have a cool logo there
right
um so micr can be a bit challenging to
build because uh it uses it works on
number of different microcontrollers and
they all require their own tool chains
and their own um dependencies and so mpu
kind of simplifies a lot of that by
building in containers and knowing which
containers to use for the various um
devices that you're trying to build for
uh it also has completion which is
really nice because it it knows the name
of all of the um all the boards that you
might want to build so how do you do it
this is the shortest most boring um
animated demo ever because it's just MP
build and then the name of your board uh
if you don't have the container it'll go
grab it and it will just build and spit
you out a firmware binary and uh that's
pretty
nice um for those yeah I also use an
alias to make it even shorter because I
don't like
typing uh we're also just stying to add
everyone's used text rle um or if you
haven't go check out textle Library by
will mugan it lets you do um sort of
goys in uh the CLI and this is an
example of what we're doing here with
the um ports and the boards there on the
left and that big stripey section in the
middle or on the right is going to be
where you can choose to build or it'll
give you information about your board
and so on and so forth so um stay tuned
I'll hopefully talk about that another
time uh the memory profile uh just like
big python and yes we call it big python
um micropython uses a GC to to manage
memory but because these devices have
quite limited amounts of memory you
really need to be on top of this and
make sure that you're not using too much
memory and not fragmenting it in
particular and so micropython gives you
a tool M info that will specify how
memory is being used at any given time
it will spit out a whole uh asy uh line
that will show you for each character
how that Block's been
allocated and so we've built a tool
inhouse and thanks to my employer um Pi
for helping release this um we we
collect mem info data and we also
collect log information and we produce
an animation so you can see the logs and
the memory information over time uh and
it makes it easy to identify from the
logs what's allocating memory and you
can go back and Target that and use
techniques to reduce that memory use um
we need this for when you're trying to
run uh uh devices for long periods of
time in particular
so let's just have a quick look at what
the animation looks like
um where's the pause button so just to
explain what's going on on the left
we've got the logs coming out on the
right that's the dump that you get from
me info I won't go into detail but each
of these characters here you can
ascertain how that memory is being
allocated I've just got an async uh task
here that's allocating memory quite
frequently and then periodically we're
I'm deallocating half of the memory so
we expect to see the memory grow um but
you'll see the lines sort of increase
increase increase and then get returned
back to the back to the Heap and so this
allows us to see when we've got memory
problems because what you don't want is
to see that continuously grow when
you're running your application for days
at a time um this helps build reliable
applications so that tool is available
um we'd love to extend it a little
further at the moment like the me info
you know it looks asy and nerdy so we
could actually have like a pictorial
version like have pixels for represent
in the blocks there's a lot lot of room
for improvement there I think but it's
still right now it was a really useful
tool for
us uh other useful tools everyone wants
to play in the browser these days um so
you can do that if you want to with
micropython Viper ID is a is an IDE that
uh runs totally in the browser in any
Chromebase browser because it relies on
web USB web serial to U make make
connections to your device it allows
serial B and Wi-Fi connections has the
file editor file manager you can see
there on the side uh it has uh MIP
Integrations so you can install packages
it has a terminal built in so it has all
the things you'd sort of want and need
really good way to get started with
micropython um I personally prefer vs
code and MP remote because it gives you
a little bit of extra power but
particularly if you're in the
educational sector If you're sort of
dabling with micropython it's a really
great tool to use um yeah and speaking
of browser tools uh for those that
haven't used W it's a um it's a browser
based simulator so it actually simulates
a bunch of Hardware uh again free to use
uh it has a large range of peripherals
so you can see here we've got an esp32
connected to an LED ring but you could
easily put temperature sensors in there
um or whatever sort of stuff you've got
they've got you know quite a large list
of things that you can connect up and
wire in the way you would on your actual
Hardware uh and then you can run your
run your uh software and it will
actually run and uh see if it you can
see if it works which is quite nice it
also uh is available as a vs code
extension as well which is quite
neat I just I justess want to put make a
note there that it's quite a rich
simulation like it even has uh the
ability to reach out of the browser so
like this sp32 has Wi-Fi and this
particular example is actually looking
at a mqtt server that's in the cloud uh
which you can send an mqtt message to
and set the color of those LEDs and this
all happens in the browser it's pretty
awesome uh I'm trying to get the two
developers to talk to integrate Viper
IDE and uh and walk we because it'd be
cool to have that extra those extra
features in the browser but um stay
tuned mellane asy I had to mention this
because it's just a fundamental part of
python but also micropython uh it's so
useful in the embedded space because a
lot of the time we're dealing with
micros that just have a single core and
also a lot of the um operations that we
do are sort of asynchronous in nature we
do something we waiting for a result
we're doing something else and waiting
for a result so ennco is a really good
fit for the embedded space um and it's a
foundational Library a lot of other
libraries are built on it uh like AI
repple which is a library that you can
use to um provide uh async features to
the reppel um and this has been a real
win for us um if you've got an async
capable application you can run it from
your reppel so it's running live but
instead of being blocked until the
application uh finishes you can still uh
interact with that running application
so we can do things like have control
loops and they might be uh generating a
p Loop uh to keep a heater at a certain
temperature right um but while that's
running we can actually inquire as to
how it's performing and even change the
values to to modify it on the fly so
it's a really again we've got a really
tight sort of feedback loop and a really
quick way to iterate with our
applications um we also used it for like
a we had a state machine and the state
machine was getting quite complex and
and we want to test what happens when it
gets into unusual States and you can
just inject the States from from the
reppel it's um it's really useful thing
to have Anthony this would also allow
you to download your uh files while your
application's running
too uh a b just a quick mention but uh
if anyone's used B and they've tried to
get it working on an embedded device I'm
sure they would have shared my pain
doing that uh it's a really quite a
complex uh beast and it's it's often
callback heavy so you get a lot of
interrupts coming back a lot of
callbacks and you need to figure out
what's going on um as these callbacks
are being generated AO simplifies a lot
of that by introducing async functions
to a b Library so you can do things like
await a connection and you don't have to
wait for the Callback so you don't have
to inquire as to what message has been
generated the next line just means that
you've made that connection and so then
you can do the next step so uh you kind
of end up with like quite a linear easy
re chunk of code with a lot less code um
rather than having at Le callbacks being
generated so for me this has been the
best way to do B development on any
platform Library language it's been
great uh see we doing time um so C user
modules I've got like the warning down
the bottom there only if you only
indicate that we're we're actually
delving into the the C World here um
because one of the superpowers of
micropython is that it's written in C
and also makes it very easy to um create
C modules so why you'd want to do that
is for two obvious reasons you want to
integr great to C library because
there's lots of them out there but the
second one is if you want to optimize
your code so um you can if your profiler
shows you of course you should check
your profiler if your profiler shows you
You' got a bit of code that needs to run
faster you've always got the option to
drop into C and create like a small trck
of your code in C that's accessible for
micropython and that's actually
relatively easy to do
um and the disadvantage here is that you
do need to produce your own firmware so
we don't have that rapid iteration cycle
that we like with micropython but um you
do get SE level of performance right so
for me this is really
um nice in the sense that I never have
to be worried or scared that micropython
uh I'm going to run out of performance
right I know that I can always drop into
C and write small chunks of my system in
C and then execute them from micropython
um and so that way I'm sort of doing
surgical Cuts Like wherever there's a
little bit of code that needs to be
improved write that and C if I really
have to um and then just get the
benefits of developing a micropython for
the vast majority of my
code and related to that I have to talk
about Native modules this is another
best bit uh Native modules is similar to
the C user modules we can compile chunks
of native code and we can package them
much like you would a dll or or a shared
Library um uh and they got different
pros and cons here uh similar to
Performance to C so we're still talking
fast performance uh they can be loaded
at runtime which is awesome we don't
need to build our own firmware still um
but the little module has to be built
for the platform that you're Target
tting so if you want to support a
general Library you have to build it for
each of the platforms that are available
and there's a few other technical
constraints but we can leave those
around for this particular Doc it's also
worth mentioning there's been a
development uh of combining like wasm
and Empire and another holy sort of mix
um you can sort of package any library
that emits WM theoretically uh and these
ones have been tested um and so you can
compile these and emit uh rasm and then
take those and compile them down to C
and then automatically compile that c
into a native module and so you get near
native performance running in
micropython in a module that can be
loaded at runtime and I think that's
it's a really exciting development
that's um it's active right
now thank you uh lvgl uh is a really
popular embedded UI tool kit this is
kind of a bit of work that we're doing
uh at the moment in part of my day job
uh and the cor lvgl team they maintain
micropython bindings where they pause
their headed
um which generate all these widgets and
they give you micropython bindings for
them and performance is identical
because you're calling through to the C
code um but it's easier to write because
it's micropython and the UI can be
modified at runtime which you're not
going to do in see um and it's really
helpful uh the part that's been missing
till recently is is packaging this
system right the lvgl core team provides
those bindings but they don't provide uh
a firmware binary for the device you
need so we're working hard to try to fix
that uh and we're going to do things
like package lvgl as a c module and
package drivers as a native module
that's the goal we're trying to get to
uh we also use Asino and a AO reppel
here so this is a convergence of some of
these uh cool features asyo um allows us
to sort of tickle the event Loop in lvgl
and a AO reple means that we can
interact with the application while it's
running uh y so that's
right so I've got 20 seconds this is
Andrew who's uh put your hand up Andrew
it's great
this is a work uh you can see here that
he's introspecting uh what is going on
and moving that label around but then
something that's just a bit difficult to
do in see is just blobbing in say 20 12
lines of text and now you can move that
label around and that feature wasn't
there when we built it so uh you can
imagine being on a call with a customer
and they want to go oh what happens if
we move X wigit over here and we can
just do that on the Fly um really
helpful go to the next I thinkk not
you me a sec okay honorable
mentions said there was too much to
discuss so just one line is if these
things are interesting to you go look
him up we've got links py script I could
give give you a whole talk on py script
but it's basically micropython or
cpython in the browser um if that's
exciting to you and it should be uh go
check it out blay is a tool that allows
you to um uh put it allows you to put
your code on a micropython device from a
python application on your PC so so in
your P PC python application you can
decorate with a header with a decorator
uh and make that run on a device that's
connected kind of cool MP Bridge allows
you like MP remote to run code on your
device but it actually synchronizes
files it doesn't actually just Mount the
device it actually synchronizes the
files on both devices and the PC
micropython P test is an effort to get P
test running on micropython we've got
unit tests running but P test was just
too giant to uh to pull across easily So
based on a talk given that last year's
Pyon Au uh yeah micropython pest came
into
being we've got Viper and Native
emitters which are built-in features of
micropython where you can add decorators
to your code and it goes v uh I'll leave
it at that there's some technical
information there you'll need to go
learn uh hard in loot testing important
thing in the embedded space you want to
test your software running on your
Hardware devices that can be quite
complex and difficult to do because
we've got a seral Communications to our
device it actually makes it a lot easier
and uh Hans is here somewhere Hans is
trying to get this to work with the OCTA
probe project where he's built a generic
device that we can host uh a device
under test on and uh put new firmware on
it and do a lot of tests on it um pdb
support we're trying to get pdb running
on micropython it's uh not there yet but
it's getting pretty close uh USB device
uh we want to like if you want to get a
USB device as a client running on
micropython there's no better solution
than this allows you to specify your
descriptors at runtime and uh um yeah
instantiated USB
device uh I'll might skip over this but
Al well there's too much to talk about
there uh because I'm running it out of
time uh my conclusion is that uh
micropython is still a teenager right
it's 11 years old um but it's already
pretty great and we've got some years
now of experience using this
professionally and it's working out
pretty well um I think it's a great
environment right now but uh it's also
really exciting to see where it's going
and how quickly it's developing so
that's um really cool for me and just
one more best bit especially for the
people in in this conference is that
micropython is python right so we get
all the benefits of knowing Python and
using it on these little devices so uh
for me that's really one of the best
best bits um and I think that's all look
go for
[Applause]
thank you Matt we've got five minutes
for questions luxury yeah um put your
hand up if you've got a
question I'm G to
startop by the way we've got like a good
chunk of micropath and Easter here so if
if we can't get an answer there's
something really wrong yeah um you were
talking about uh being able to test on
proper x86 platforms yep what about
peripheral testing so if you've got
devices that are esoteric I squidy or
whatever um yeah how do you go about
testing those you've got you got two
options and the one we most use is to
mock them out yep and so we usually mock
them out sort of at the communications
layer whether it's i2c or spy um so we
know what i2c is doing we know what Spy
is doing um but the other option is that
you can actually get it running on Unix
as well and it's a bit harder and it's a
bit more Cutting Edge but you can
actually get these things running on
your on your x86 device
yeah so I was just wondering about the
for the memory management stuff because
it's like you have so little space and
with the fragmentation and everything
like
is like are there just patterns that
people are doing in micropython to avoid
things or is it kind of that like
usually you actually don't you know
you're kind of you're kind of not
worrying about memory that much in
practice because of what you're doing
well that's the thing so python gives
you lots of tools to do um programming
tasks really rapidly right um but that
usually means you don't care about
memory allocations you just care about
getting the job done um but particularly
with micropython but also with python
you can do things like preallocate your
bite arrays and reuse them for example
so really common money is we want to
send data to a device over over
Communications Channel and the obvious
way to do that is to generate a blob of
data and and send it across but that
allocates a temporary every time so
micropath provides ways of allocating a
b array once and reusing it and then
passing it in through the through the um
methods so um you don't have
those that's right yeah
y awesome more people up the front come
on people down the back you've got to I
know it's afternoon but you know I it
kind of quite a similar question and
maybe might have been over the uh over
the top of it but um what sort of
guarantees or or understandings that you
can have while you're using micropython
to say know that this is
using like statically using memory as
opposed to just underlying dynamically
access
creating all yeah it's kind of a
learning process and part of it is that
um python isn't that transparent about
how you manage memory and that's it's a
pro and a con right they can see you
have to be very aware of what you're
doing with the memory and that means
that you're very aware of what that
memory is doing so in Python you tend to
learn by using like tools like the
memory profile as to how uh and when
Temporaries get created um but it is a
learning process and that is kind of a
detail that you need to worry about but
uh I guess I'd encourage people just to
not care about that at first just just
get your application done and working
and then analyze it later and figure out
where you need to make the the
improvements um
yeah you're all so close
[Laughter]
together um at the risk of debugging my
own project
um what we offer consultancy service if
that's um what what ideas do you have
for look at like looking at debugging or
um working out why
something
fails after A Time are there sort of
good logging patterns and practices or
debuggers what what sort of tooling is
out there yeah so um we can hook into
the standard python logging system so
that uh for for the most part you've got
very similar features to regular logging
in Python that's the first Port of Call
uh other things kind of depends on the
on the on the the specifics like you
might want to um keep your repple open
so that you can get debug information
when problems occur that's actually
really useful to introspect what the
variables are doing um but yeah press
talk to me afterwards and we'll see if
we can fix your problem yeah all right
we got time for one more question I
think yeah come um how do the realtime
capability compared to like a bare metal
C or like an OS like application yeah
it's not a real time operating system is
the short answer um the best thing you
can do is drop into C and and use
whatever guarantees your Manu your
microcontroller manufacturer gives you
on their API that would be the if you're
looking for tight realtime operating
system stuff probably not the right tool
for the job um but I I would say that a
lot of the C real time operating systems
aren't that real time as well so
um yeah
yeah yeah so micro gives you access to
to regular isrs and to interrupts um so
that that part of the system actually
performs very well um as with anything
that's where timing is critical measure
measure measure like make sure you're
doing it within your timing constraints
yeah all right please U join with me in
thanking Matt TR again