foreign
there are stickers here and there's been
stickers passed around please feel free
to take them um the micros are mine
please don't take them
um by way of introduction my name is
Matt hi uh I'm an engineer of software
engineer for about 25 years which makes
me feel excessively old
um I I kind of describe myself as a full
stack engineer which uh we'll talk about
that in a minute but uh I've been
exposed to most areas of software
development all the way from low level
assembly stuff all the way to
cloud-based web stuff so but a lot of my
career has been spent
um developing on or close to hardware
um which should kind of make me a bit
jaded because in better development had
sort of been stagnant for a long time
and sort of watch all these wonderful
developments and the rest of the
software Fields uh but yeah sort of saw
the embedded stage just sort of plateau
for a long time until micro python came
along and we'll talk about it today
um I would describe myself as a
micropath and Enthusiast I've
contributed code and documentation uh I
host the Melbourne Michael python Meetup
and I've talked to the previous pycons
about Michael python so uh yeah just
knowing that full stack development I
looked up the definition of what a full
stack developer is it's like
the front end and back end of a website
and I'm like hey kids it's like I'm not
sure what you're talking about
anyway moving along uh can I have some
hands up and I'm gonna start with if
you're human
good audience participation question I
saw most people with their hands up
that's good I suppose uh who knows what
a microcontroller is
also good most people uh how about who's
written software for Market controller
it's quite a lot I guess about
huh I'm
um who knows what micropython is who
went to Damien's talk earlier Damien you
don't get to put your hand up good okay
that means I can sort of briefly go over
these things it's a python conference I
need to know how much of the background
we need to cover so who's seen Avengers
Infinity War
good that's helpful
um right
what's a microcontroller it's just a
small computer right so
microcontrollers is less about
processing power when you compare them
to a PC it's more about precise
dedicated sort of tasks they have a
small amount of resources they're
Limited in what they can do with ram and
Flash because they have very little of
it and they often interact with the real
world so they control Motors pumps
interactive sensors that kind of stuff
so
a key defining factor is that they're
low cost from cents to Dollars typically
uh and they've got very predictable
Behavior because they don't have an
operating system they tend to be just
doing that one task you tell them to do
uh many systems are often comprised of
lots of microcontrollers cars are a good
example you've got dozens or hundreds of
microcontrollers in a single car
and it kind of led me on to think about
the size of the market
um so it's like 30 billion dollar market
and it's expected to get to 50 billion
by 238 which was frankly a lot larger
than I expected and it seems like the
average microcontroller costs about a
dollar because they sell 30 billion of
these things a year which if you compare
it to PC sales is kind of scary
um so there's a lot of these things out
there if you look around you'll see
devices in the room with plenty of them
around
what's Market python if you look at
Reddit it's an Abomination
um quite a funny read actually you have
a look at that it's worth a worth a look
what what it is really is uh
sorry
tough tough
um
what is it really it's it's python three
re-implementation of Python 3 for
microcontrollers so a lot of effort's
been put into making it work on a
constrained device written in C the
Baseline was python34 but a lot of other
features been added since then we still
claim it's python 3-4 but there's
features from all over the shop we've
added as resources allow really
um
yeah so why should we keep updating it
to newer versions of python new features
especially so we can Port libraries more
easily from the C python world yeah
we've got excellent support for the
language it really just feels like using
big python and yes we call it big python
um
and we've got exceptional tests if you
entertainment thought you'll know we've
got exceptional test coverage across the
language above 98 so you can be pretty
confident that language itself is really
robust and stable
if you find a bug in Marketplace we will
give you a sticker yeah
standard library is a little hard harder
it's pretty vast as we all know we've
got a lot of things implemented but not
everything and a lot of the libraries
are written such that they're not so
conducive to work on a microcontroller
so we've got to be careful about what we
put in
it's a similar story for third-party
libraries
really tough to just pick up pandas and
have that run on micro python and some
libraries are built right in the
micropython and others are built in in a
separate repo or micropython lib between
install with a tool called MIP which is
like pip
yeah it's kind of fun
uh in addition to all this stuff we've
got the machine Library which gives you
access to the embedded side of things so
you can Twitter with pins you can
connect to peripherals it's very
pythonic so it gives you a nice familiar
sort of API if you if you're used to the
python world to interact with stuff
that's quite low level and typically
done in uh much more of a Bose and
low-level way
so quick example just setting an LED
turning it on using pwm which is a
technique called pulse width modulation
to flicker an LED on and off at
different rates this is much easier and
if you've done this if you've been in
the Sea World before you'll be aware
that's a lot easier than coding it and
see
why micropython and whenever I hear that
term I think of Avengers Infinity War
there's the throwback right where he
went you know why is Gamora that's what
I hear in my head when people ask me why
do you use micropath you know why
um
so similar reasons to C python it's it's
a readable easy language to learn and to
use it's easy to test
um it's very flexible it's a great
language right but the main better the
main benefit for me at least in the
embedded domain especially when you're
comparing it to CNC plus plus is 30
right 30 for me is an important number
because that's what I think the
development time is reduced by by using
micro python typically
um a lot of caveats around that you can
put around that but um uh I'd also think
we get happier customers but that's
harder to quantify
it does obviously depend on the project
but we've I work for a company uh
planned Innovation that uses micropython
for some um some projects and we've had
handful projects now and they've all
come in roughly through the estimates
that we said and we've often compared
them to the estimates in the CEC Plus
World and 20 to 30 is pretty typical I
also expect that number to actually
improve or at least stabilize towards
the higher end of that range because
micro python is still maturing and the
tooling is getting better and the um
libraries are getting more robust so
um so how do we get that
um it's high level than CNC plus plus
you're all aware of that if you're
familiar with C python but you don't
have to worry as much about memory
management and undefined behavior and
all that kind of stuff that you have to
even see
it's hard to overstate the live reple so
there's a way to get access to a running
device
um and you use the python Rebel on the
device so you can actually communicate
to your peripherals and do a lot of work
on the device itself and that's a huge
Boon to development we can really
explore
um uh the development environment that
way
we've got the Unix support for testing
so we can do a lot of things on x86
which is really helpful I've got really
fast workflow so I need to show you MP
remote which is a tool that we use to
connect to our devices
into our micro python devices you can
see here at this top here there's four
files that that's on my computer at the
top there and then MP remote Mount dot
actually mounts my PC file system at the
at the dot point on the device so it's
just like my device sees the files that
are on the PC
so you can see here if I do an OS Lister
that's running on my microcontroller and
it's just like I'm seeing the files that
um that are on my PC
it's fantastic it's hard to explain how
good this is because it feels a bit like
magic having worked in the embedded
space for some time it means that I can
develop on a PC I can use my regular
tools to do my my coding I can use git
and when I'm ready I can test in the
Unix port and then I can just mount it
and try it on the device it's really low
friction way to get moving
um
practice some of the other benefits
we've seen at least the company is that
the junior developers become superpowers
really quickly like and it's been really
really stark contrast to seeing people
um to seeing Juniors start in the CNC by
Foss world where it takes a long time
for that ramp up to occur
um it happens really quickly with
Michael python um so that's been a
surprise for me I didn't really expect
that it was wonderful
we've found that we're more responsive
to customer requests and we can we can
you know it's an overload a temple we're
certainly a lot more agile and customers
love it they can talk with us and work
with us more
and the other thing that for me at least
was that there's a mental change like I
tend to focus on the problem I'm trying
to solve rather than the Nitty Gritty
details of how to actually get it
happening and how to get it done so for
me that's a big win
yeah we're like 10 minutes in we've got
back around so right
people come with me all the time and say
you can't do that in micro python right
when performance matters you can't do it
market price and if you want to run from
a battery and you can't do a
microphizing if you're running if you
want to worry about manufacturing and
volume
it has a kernel of treason all of these
um
but I think it's worth digging into
right so
the most common concern is Marketplace
and slow and it's true to a degree right
you're really comparing native to
bytecode development so just a quick
refresher with Native coding but this is
you know if you're using C you can go
any language that compiles down to the
metal
you take a code you compile it you're
running code that the device can run
directly right by code interpreters work
a little differently they take code they
compile it to bytecode by code gets run
through the virtual machine and then the
virtual machine runs that on the device
directly
uh and slower it's slower if you're
spending time in the virtual machine
there's benefits to it you've heard it's
more portable there's a lot of things
you can do with that but that's the
difference the yellow bar there is just
something that I only occurred to me
later was worth pointing out is that all
of the things that are next to that
yellow bar actually occur on the device
in micropython by default so the
compiler actually works on the device
um yeah all the way down and we'll talk
about that a little further later
because there's a way you can optimize
some of that
uh so techniques for performance there's
a whole bunch of sophisticated methods
to improve performance and micropism
we're not going to cover them today
Damien's talked about them before I've
talked about them before and we'd need
you need a fair bit of time to cover
them uh but just for completeness uh
hopefully these are court on the video
you can go look these up later I'm going
to look at one performance Hammer which
is just using C um all the other
techniques are really sophisticated
elegant and have their place and they
should be used but just in terms of
um why you don't necessarily need to
exclude micro python for performance is
the fact that you can use c a chair
microphize and shares see Python's Dirty
Little Secret and that we offload our
performance to see when we need to not
so much Fortran on embedded devices but
you know so yeah think about like numpy
Hill sci-fi right they both got seen in
the guts of them that makes them work
faster
there's an interesting
um white paper recently that was talking
about comparing micro pythons of other
languages
it's kind of weird
um like this one for example talks about
crc32 they actually implemented a crc32
algorithm in Python and then tested that
we already have the CSC 32 algorithm
which works at the same performance of
the C but they're trying to compare the
language
overheads and what they conclusively
showed is that by code interpreted
languages are slower than native
languages which wasn't that surprising
the problem I have is at the end they
kind of make or they kind of that's to
the conclusion that micro python is not
appropriate for embedded use if
performance is a concerned and I don't
think that's correct it's kind of like
saying you know big python is not
appropriate for data science use because
you know you can't uh you know python
the language is too slow I don't believe
that and so
um I think you need to take this there's
a bit of grain of salt to be taken here
um so it is easy to extend marketplace
with C you'll find the documentation
talking about user modules
um if there's one thing here we can
improve it'll be around documentation
we've got some good documentation but
you can probably go a little further
because it's a fairly complex topic
about Bridging the the um the python to
C boundary but
the key point is that it's just C right
like it's fast so
um
relevant quotes that I have also heard I
might as well just write and see
I think that comes back to that 30
number before you're paying the cost of
having develop and see if you've got to
do that so my argument would be that you
should spend as much time on micro
python as you can and just optimize the
bits that you need and the projects
we've had at work
there's been any minimal amounts of code
that we've had to develop in C so it's a
small fraction
and the other one here is we're all
human well most of you put your hand up
as a human identify as a human so
um but we're always bad at guessing
where to optimize so
um my I guess my statement would be
Friday Michael python first important to
see only when profiling shows a need
and there's a small caveat there with it
almost in Brackets but there's almost no
performance ceiling like we can drop
into C and you do whatever compute you
need
um yeah
you can't use microphone when you're
running for a battery
so a little bit more background
um modern my controllers have low power
modes and this is kind of the way that
they get good battery life
um
the power mode ruins that they're sort
of uh trading off features for power
consumption so they'll turn off the core
and they'll just listen to some pins or
whatever and these can have dramatic
improvements in terms of power use
and so the key thing if you're running
from batteries is to drive your micro
into the lowest power mode you can and
keep it in there for as long as you can
that's the key thing for low power
applications
so if you think about like temperature
logging it's one of the nice easy
example to think about temperature
doesn't change frequently so we only
need to pull it every so often
so you might what does it read
temperature every 30 seconds and you
store it in a buffer
and every 10 minutes you might offload
it over Wi-Fi okay the key thing is to
where you're not doing stuff driving
into those low power modes and get the
benefit of running at lower power
so some quick charts hopefully it's not
too overwhelming but uh
that middle section there is showing
that we're in a deep sleep mode and
we're consuming one and a half milliamps
for this particular microcontroller
the other areas are just where the
process is running as normal it's not
normally responsive but yeah it's a 30
times difference in power
um
this is with Wi-Fi this particular micro
supports Wi-Fi as well and you can see
that the power use goes right up that
low bar there is actually the high bar
from the previous slide
and so we're up in 120 milliamps or
higher if we're actually connected to
Wi-Fi now it's also worth noting that's
with Wi-Fi just connected not doing
anything as soon as you start
transferring stuff it cranks up again
so
um a key thing to be aware of is that
these low power modes have identical
costs regardless of what language you
use the micro doesn't know that you're
running micropath and you'll see it
doesn't care
um so the key thing is to keep in those
low power modes as much as possible
there is an additional cost to using
micropython and that's because when the
micro starts up
we have to start an interpreter often we
have to start a file system we have to
start reading
python compiling it and doing something
now that takes about 40 or 15
milliseconds varies a bit port to port
but that's pretty typical
so what you need to think about is if we
go back to a deep sleep mode for example
we want to come out of our deep sleep
mode and stay in that higher power mode
for as little time as possible so
micropython adds 40 milliseconds is the
way to think about it and then
if you can live with that you can live
with using micro python with battery
powered applications
um
it's also possible to mitigate that cost
as well there's a hook at the start when
micro python starts to latch a little
bit of C code if you want to so if you
can do something like wake do a check to
see if you should be woken up or if you
need to and go back to sleep you
actually mitigate that cost even so
um
yeah so yes there is a small cost to
selling Marketplace and running in a VM
but the key thing you should be doing is
uh keeping down in those low power modes
as much as possible and so the
suggestion here is build a product in
market price list you'll see a theme
Here build a product in Marketplace and
first where you'll work faster and
you'll get to your end goal quicker
and if you can't achieve your power
budget put it to CNC plus plus then
you'll have already learned a lot you'll
have already been able to work with your
customer more you've already been able
to develop the thing that you want
faster so that porting effort should be
pretty small
drink water but you can't use
micropython if you're Manufacturing in
volume
again some truth here so Market python
places a higher Demand on the resource
uses of a of a microcontroller it needs
higher specs because it needs more Flash
and needs more RAM
they're the key ones
so if bomb costs if your bill of
materials cost is critical my price may
not be your best solution but it's worth
having a look at how critical
um so I put this in perspective
microcontroller costs tend to be in the
terms of like I said before like
high-end is dollars tens of dollars low
end is 20 30 cents
um and still get a whole range in
between now eight bits yeah just like
everyone I'm wouldn't be surprised if
you know we'll have stickers in a year
or two with eight bit microcontrollers
stuck to them
um the microphone is not going to run
the low end we just don't have the
resources to do so
um but how low can we go it's kind of
important to look at because typically
when you first come on a Marketplace and
you'll be told that there's you the
requirements are about about 200k a
Flash and about 32k Ram give or take
um it's not quite the whole story
um
it's kind of like when we package
micropython we it's kind of a kitchen
sink approach because micro python is a
dynamic language runtime you need all of
the features available to use them even
if you're not going to so you have to
package the whole thing
but if you know what your application
needs you can start carving it away it's
pretty easy to build your own custom
firmware of micro Python and just carve
out features
um so the claim here is I guess a
kitchen sink micro python is around 200k
a Flash and a minimal micro python is
about 40K a flash I'm looking at gym
because we're trying to get down there
today yeah thumbs up good okay
um
so what can we trade off like features
um f strings async SBI you can carve
these things out at a pretty granular
level you can drop the file system you
can drop the rebel
um you can drop the compiler so you
start trading off these quality of life
development features to aim at a smaller
micro okay so I mentioned before that
everything happens on the on the micro
that's next to that yellow bar but that
red bar is actually an alternative where
we can actually do compilation and
freeze our code into the into the
firmware so it's kind of like in the sea
world you would compile your application
and deploy it in micropy you would
compile your your source code and just
like in Python you'll get a pi C file
um you'll get a similar deal you get by
code that you can put into your firmware
which means that it's you lose that
ability to do it dynamically at runtime
you've got to build it into your
firmware
but you can Target a smaller firmware
blob
uh yeah
so
a possible workflow you can consider is
to start with like a big monstrous
microcontroller and just don't worry
about your problems so if your volume
manufacturing just get your application
running use something that's got a ton
of Slash a ton of RAM and develop as
quick as possible okay and shift to a
micro it's got smaller resources
um
you can start either customizing your
firmware or maybe just customizing your
application so it uses less less
resources
and then you can do that whole whole
another step where you start really
hacking into the firmware making it as
small as possible and developing it so
that it only supports the application
that you write
um and so the costs are important here
these are real examples of boards that
we've got
um where the board to the left is the
big beefy one we talked about it's an H5
cortex m33 I'll gloss those the details
but tons of flash tons of ram about 4.60
and these numbers are all in units of
10K which is St will give you these
prices but they are relatively accurate
I think a units and US dollars so 460
for the big beefy one with tons of
peripherals the middle one's interesting
that's the same uh core it's 250 Meg
cortex M3 but they've dropped a lot of
peripherals they dropped a lot of flash
dropped a lot of RAM and dropped a lot
of cost and so you're suddenly looking
at a dollar sixty for that
microcontroller
but it's a pretty easy Port because if
your code works on the original H5 it's
usually a pretty small jump to go from
the big one to the medium one
then you've got the small one there's a
g zero that I've selected which is a
dollar twenty
um so you save 40 whole cents but you're
going to a much smaller much lower spec
micro
now getting down to that size is more
difficult
um but it's not it's not out of the
realm of possibility
uh
right so
uh micro again there's a German truth
here like Microsoft does Place High
demands on
a microcontroller resources but there
are ways to offset some of that and so
yeah I'd suggest like I have in all the
other ones I've had micropython first
and make changes if you have to later
and that is my summary really uh
development important to say if you
really must right
um there's no real the barriers here
seem a lot more
um Impressions than actual reality in my
opinion and I guess I believe that
thought was that
I expected to find a lot more
limitations developing with micro python
I thought it was going to be useful for
certain class of microcontrollers for
certain applications
but now it's my default choice I would
need to be convinced to start a start an
application in CNC plus plus and it's
often the wrong choice it might be
okay so uh actually just quickly uh for
you to mentioned the stickers but we've
got we're going to try and get to the
python fair and uh Sprint day Monday
um so if you guys are interested please
stick around and thanks for listening
and thanks to the organizers and
Marketplace and community and especially
to the uh my support team who helped out
with making sure all these claims were
actually possible which they more or
less aren't
um
So yeah thank you
[Music]
all right we have time for a couple of
questions if anyone has any
sure that'll go right here
hi
um I was wondering if you tested and if
so what uh the results were around the
um
what kind of performance gain did you
see when you froze that code and and
pre-compiled it uh did you mention that
at all and see how that went we because
it's a no it's worth talking about
um so just for everyone's benefit like
frozen code we um as we mentioned
briefly we take python code we get by
code out of it that gets stored in the
flash
um normally that would happen uh on the
file system uh so what we do is we avoid
a lot of memory use because we don't
need to invoke the compiler and we don't
need to compile it so
um it varies wildly unfortunately is the
way to say it I don't know if anyone's
got any figures out there but um yeah
yeah
that thing is shaking his head Damien
doesn't know it doesn't it
yep I got a question so you mentioned
that one of the ways to shrink down the
footprint is to custom build that
firmware is there any tools where I can
just point it to my py file and it goes
okay you're using these things I'm just
going to build that firmware against
that not yet yeah we should create that
we should yeah let's do that that's a
great idea we've got a Sprint day Monday
right what are you up to okay
it'd be good to do that analysis yeah it
certainly came up during this discussion
of this talking
so it does sound like you're
volunteering there Adam so are you
showing up with the Sprints on Monday
and building that
if you Melvin come on tonight
do we have any other questions yeah um
I'm very I'm passingly familiar with uh
writing C extensions for C python the
the big python yep
um thank you for using the correct term
yeah sorry I realized as I said it
um I'm just wondering if you have uh if
you couldn't compare the experience of
writing a c extension for big python
versus microfizing uh it's completely
different it's a short answer
um I would say I found having an
embedded background I found it easier to
do the micro python side of things
because it's kind of a it's kind of an
obvious barrier to to jump
um with C python there was more things I
had to learn about how C python operated
to create those extensions
yeah so if you've gone in better
background I think you'll find it
relatively straightforward if you
haven't
um
and probably in general like uh once you
get beyond the sort of beginner range of
micropython the most the biggest ramp up
that you're going to have is becoming
familiar with the embedded domain if
you're not familiar with it so uh yeah I
guess you'll find extensions relatively
easy to do if you're sort of familiar
with that
anyone else
oh cool
um in that case
uh thank you so much
um for giving this talk it's been
amazing thank you so much
thank you
[Applause]