How many layers of abstraction and indirection are between your Python code and machine instructions?
What if that number could be one and Python itself was the operating system?
That would be so amazing, right?
In fact, it is amazing, and it's called MicroPython.
This is show number 17.
It's MicroPython with guest Damian George.
You're listening to Talk Python to Me, and this was recorded on Thursday, June 25, 2015.
I'm a developer in many senses of the word, because I make these applications, but I also use these verbs to make this music.
I construct it line by line, just like when I'm coding another software design.
In both cases, it's about design patterns.
Anyone can get the job done.
It's the execution that matters.
I have many interests.
Sometimes it can flit.
My creativity can usually be...
Welcome to Talk Python to Me, a weekly podcast on Python, the language, the libraries, the ecosystem, and the personalities.
This is your host, Michael Kennedy.
Follow me on Twitter, where I'm @mkennedy.
Keep up with the show and listen to past episodes at talkpythontome.com.
And follow the show on Twitter, where we're at Talk Python.
This show will be talking to Damian George about rethinking the Python runtime for embedded microcontrollers and his implementation, MicroPython.
This episode is brought to you by CodeShip and Hired.
Thank them for supporting the show on Twitter via at CodeShip and at Hired underscore HQ.
Before we get to the interview with Damian, I'd like to give a shout out to Ganesh Kupan.
Ganesh suggested this topic for the show, and I think it's a great one.
Ganesh, thank you for sending that in and making this happen.
Now, let me introduce Damian.
Damian George is a theoretical physicist at the University of Cambridge and the creator of MicroPython.
Damian, welcome to the show.
Hi, Michael.
Thanks.
Yeah, this is really great.
We've talked a lot about software, and we've even talked a little bit about robots on the show before.
And now we're going to talk about sort of the most low-level stuff you can do with Python that I know of.
We're going to talk about running Python directly on microchips and microcontrollers, right?
Yes, that's right.
Yeah.
Your project, MicroPython, at micropython.org is really cool.
Thank you.
Yeah, you're welcome.
And some of the listeners said, hey, you really need to have Damian on your show and talk about this stuff because what you guys are doing, it's really amazing.
And so we're definitely going to do that.
We're going to talk all about that.
But before we do, maybe you could tell me a bit about your background, how you got into programming, how you got into Python, that kind of thing.
Okay.
Well, I mean, I've always liked computers and using computers and programming since I can remember.
I know that I've written many languages, toy languages, ever since I could start programming.
It's always been sort of a passion.
I think anyone who gets into programming at some stage tries writing their own language.
That's why there's so many of them.
But it's a really great way to understand how a computer works is by inventing your own language or at least implementing something that already exists.
That's sort of something that I did in my spare time.
But actually, formally, I also did a computer engineering degree at university, which was based around microcontrollers and designing computers and designing instruction sets and writing operating systems and those kinds of low-level things.
So that was really good to get an actual formal training so that you're forced to be exposed to all the things that maybe you didn't want to study.
Absolutely.
Was that mostly in C or C++?
And what languages and stuff did you use in that field?
We learned a lot of languages like Haskell and Java and C and C++.
So, yeah, but I mean, it was a bit about learning languages, but also you learn algorithms like hash tables and things like that.
And then, you know, you learn, I mean, you learn how to write a compiler.
And it's not so much about the language.
It's about the concepts and the algorithms.
Right, like the abstract syntax trees and things like this, yeah?
Yeah, that's right.
That's right.
So, I mean, this is what I studied.
I actually also studied theoretical physics and pure math.
So, I did a lot of extra stuff.
And all these things, I think, sort of tie in, in the end, to the same kind of way of thinking, is that you think very methodically and you try and solve problems.
And you use computers a lot in maths and physics to solve problems because they're so difficult nowadays that you can't just do it with a pen and paper.
Yeah, I think you're totally right.
My background is I was working on my PhD in math as well, and I got into computers to trying to do research with math.
And, like, you're going to have to learn to program this thing.
And it turned out I liked computers better, so I never did get a PhD.
But I totally know where you're coming from.
That's awesome.
In physics, if you're an experimental physicist, a lot of the time you end up doing just a lot of programming, like Monte Carlo simulations and data analysis and things like that.
And, I mean, yeah, if you like the kind of stuff, it's really good.
But if you don't, you know, maybe if you want to build an actual apparatus, then you probably need to be an engineer.
I think, yeah, my background is all these things combined.
And, yeah, I just really like the technical side of things.
That's cool.
Both programming and hardware.
Yeah, so how did you get into Python?
You started out in this sort of what to me feels more like, you know, the majority of the work is done in C, C++, Fortran, that kind of space with physics and operating systems and so on.
And then somehow you ended up in Python, right?
Yeah, I mean, I think it's good if you know more than one language or more than two or more than five languages.
And Python is such a popular language and for a good reason.
It's a very nice language.
And so, yeah, it's just one of the ones that you will, I think, eventually learn if you're into programming.
And, yeah, I mean, yeah, I just, I guess just stumbled across it like all the other languages that people learn, like C and C++.
Because, yeah, I mean, C is a great language and I love writing code in C.
I mean, I know it's a very old language, but it allows you to be really close to the hardware.
I mean, a line of C translates to a few lines of assembly and you sort of know what's going on.
And, yeah, that means you have to write more code to do the same sort of thing as you would in a higher level language.
But it gives you more power and when you need it, speed.
But then, of course, you want something like Python to say when you don't, you're not so worried about the efficiency and you're more worried about writing something quickly and expressing your idea in a very quick way and sort of a very clean and clear way.
So a Python program is, you know, you think it and then you sort of put your thoughts on it in code and it's sort of the same thing.
Yeah, that's a lovely thing about Python, isn't it?
Yeah, yeah, exactly.
You just, you know, you don't have to allocate lists and make links in the list.
You know, it just works having a list or it just works having a hash table, a dictionary.
And, you know, you have lambdas and you have list comprehension and you have generators.
And these things allow you to think in a certain way.
And if you're writing in C, you don't think about generators and you don't think about sort of closures because they're really hard.
But they're really good concepts.
And a language that allows you to express those concepts easily is a good language.
I agree.
And when I think of, you know, microcontrollers and embedded devices, I typically think of people using C because they want to squeeze that extra 5% of energy or performance or, you know, those are more or less the same thing in some sense.
Right.
You've taken the opposite perspective.
You've made Python suitable for microcontrollers rather than saying we're going to use a language that already sort of matched to the metal.
That's really awesome.
So you call this project MicroPython, right?
Yeah, that's right.
MicroPython.
I think the name says it all.
That's awesome.
If you want Python on a microcontroller, then that's what you need.
I did it as a sort of, to begin with, it was more of a can it be done question.
Because, you know, I just, it's technically interesting to try and put a language as big and as high level and as dynamic and as memory hungry as Python on a really small microcontroller, which has very, very, very little memory.
So that was kind of what started it for me was just the technical interest.
But at the same time, I also thought, you know, why not pick another language or why not just, I mean, C is already on microcontrollers and stuff, but why even choose a high level language to start with?
And my reason is that because microcontrollers now are a little, you know, are more powerful than they were a few years ago when they had almost no memory at all, you know, like 100 bytes or something.
Now they have 100 kilobytes.
And that's starting to be interesting.
They also have really sophisticated peripherals.
So you buy, you know, a $1 chip and it has all these UART and SPI and I2C and ADCs and DACs and all these peripherals that do great things and are very, very powerful.
It has DMA to do quick transfers and this kind of stuff.
When you start writing it in C to control all of these peripherals, it gets really, really hard really quickly because you've got to, you've got so many configuration options.
You've got so many things to think about, interrupts and interrupt levels and it becomes very quickly sort of, I mean, it can become a bit of a mess to get something to run quickly and easily.
So if you just want to flash an LED and read some sensor, you don't need to sort of write really efficient code that you would need to or C would allow you to.
So to do a simple thing, but a low level simple thing, like turn a light on, I mean, what you're doing is putting a voltage, five volts or three volts on a pin.
It's a pretty basic operation.
It's something that you can do in Python quite efficiently.
And then once you, you can do, you know, turn up an LED on with a Python command, you can think about having a list or a dictionary of pins and then you can iterate over them or you can pass them along as objects.
So you can start to really abstract the low level hardware.
And it's still relatively efficient because, well, the way MicroPython is implemented, you still have running relatively efficiently on the microcontroller.
You know, I've seen your API and it looks super clean.
I saw your Kickstarter video and the one that you have on micropython.org is sort of the opener video to introduce people.
That's really cool.
Maybe let's take a step back and just real quickly tell people what MicroPython is and tell them about your board and how it came into existence.
Sure.
Well, so MicroPython is a rewrite, a complete re-implementation from scratch of Python sort of version three, as it were, 3.4, actually.
So complete rewrite, no components whatsoever that are in common with CPython.
And the aim in rewriting it was to make sure that every operation was done as efficiently and used as little and, if possible, no RAM at all.
So that was sort of the guiding principle is use as little RAM as possible.
So MicroPython, the software, is this re-implementation of Python.
And it includes all of your favorites, lists and dictionaries and sets.
And, I mean, it's complete Python 3.4 in terms of the language specification.
Probably not the same standard library, though.
Probably things like dbapi2 is not there or something like that.
Right, yeah.
So there's the language in terms of the syntax and the semantics, you know, classes and function definitions.
And then there's the library.
So import numpy or whatever.
Import this, import that.
Python famously comes with batteries included with all of the standard library and everything you need, you know, statistics to compute the mean of something and things like that.
And these things, some of them are not sensible to run on a microcontroller and some of them won't fit on a microcontroller.
And so not all, well, not that many of the standard libraries are available.
But the ones that are useful are there, like JSON string parsing, for example, and regular expressions are there in a sort of cut down form.
And, I mean, we're always looking to add more.
It's just that the manpower to write all of these things in a micro way is not that easy.
Do you have to re-implement every standard library package or module that you're using?
Some of them we have, for example, JSON printing and parsing is rewritten because you want to make it efficient.
But other things actually import directly.
We can just copy over the CPython standard library and use that code directly.
So, yeah, it depends what you want to do.
I mean, the idea really is to provide micropython as the core language.
And then if people need some extra feature, they can copy it from normal CPython and just see if it works and it should work.
And, you know, if not, maybe it needs a few little tweaks here and there.
But, yeah, we concentrate mostly on the core language and providing a very robust and hopefully efficient core language.
So that's, but that's the software side of things.
Then there's the hardware side of things, which is, so the Pyboard, which is this little microcontroller that runs micropython.
And all the peripherals on this Pyboard are well supported, like the I2C bus or the UART, for example.
And the aim with this was, well, to be able to sort of kickstart this whole idea.
We went to Kickstarter.
Yeah.
Why don't you tell everybody a little bit about your Kickstarter?
It was super, super successful.
I saw you originally were going out for 15,000 pounds.
Yes.
And you raised 97,000 pounds or 153,000 US dollars.
That's, you must have been super thrilled.
Yeah.
Well, it was, it was a great reception.
We had a lot of people get on board.
And yeah, I mean, it was, it was really good.
I didn't expect it to be that big.
I mean, as I said, it started out as a sort of technical interest to be like, you know, it'd be really great to be able to see if this would fit.
And then, you know, getting it running, making a little board.
So the idea with the board was that I could give something to the Kickstarter backers.
If they back, you know, it's nice to actually have something physical as a backer instead of a piece of free software, which is going to be free for everyone.
So the hardware was a way to sort of, to give an incentive to say, look, there's actually something physical it can run on and you can do cool stuff with it.
That's cool.
The work of creating it, was it mostly actually writing the reimplementation of Python or was it, how much of it was hardware work and how much of it was software work?
I'd say it was about 50, 50 to begin with.
Up until when I launched the campaign, it was about 50, 50.
The software, I mean, yeah, I didn't write the entire thing before the Kickstarter campaign started.
I wrote enough to be able to convince myself and others that it would actually work.
Like you have a REPL and you can turn lights on and off and so on.
So there were bits of the language missing at the Kickstarter stage.
And probably 50% of the time was spent actually building the hardware.
So I had to learn about these microcontrollers and how they work.
The hardest thing is getting a tool chain up and running to cross compile stuff and download it and then, you know, debug it.
And why doesn't my USB thing work and so on?
There's a lot of really technical and difficult things to sort of get your head around when you first get into microcontrollers.
And I mean, these days with their 32-bit ARM chips, they're very powerful things.
And there's lots of little things that can go wrong.
So, yeah, I liked it.
It was a good balance of software and hardware.
Then after the Kickstarter, when the hardware was finalized, it shifted more into software.
And since then, it's been, you know, pretty much purely just software, improving the software both for the core language and also to control the Pyboard in a nice way and support all the features.
Yeah, it looks really rich.
Maybe you could tell everyone a little bit about the features of the board because when I learned about it, I was surprised at all the things it has.
You know, an accelerometer, for example, storage, all sorts of stuff.
It seems pretty much like I could take that, put it into something with a power source and use it.
Yeah, well, for sure.
I mean, the aim is you plug it into USB and it works straight away.
I just need to power source and it's got so that the accelerometer is not built into the microcontroller.
That's a separate little chip that's on there.
But the idea of putting that on was so that people could, you know, have sort of some input to play with straight away instead of buttons or switches or something.
An accelerometer is like a lot more fun to play with.
On your video, you have a little app you wrote where there's a robot that can lean forwards, backwards, left, right.
And as you tilt the board, the robot kind of leans in the way.
And you're doing that with an accelerometer and Python.
That's right.
That was a really cool example.
Yeah, well, I mean, that's only, you know, a handful of lines.
It's just an infinite loop while true.
And then you read the accelerometer, which is like one line in Python.
And then you set the servo angle based on that accelerometer reading.
So, you know, it really is just a few lines to do that stuff.
Okay, cool.
So maybe you could walk the listeners through what it's like from going, you know, file new project to having, what was it?
A main.py that runs when you turn on the board?
Yeah.
What's the lifecycle look like there?
So there's actually a few different ways of using it.
Okay.
So the simplest way is you just plug it in to USB and then it becomes a serial port, like a COM port, which you can log into.
It depends if you use, you know, Linux or Windows or Mac.
But you just use a terminal program and then you can connect to the serial port of the board and you have a Python prompt, a REPL right there.
And that runs directly on the board.
So you can type commands in straight away and they will be executed and do whatever you ask on the board.
So that's a good way to sort of just play around with things, debug things, test things.
The other way of programming it is to write your code, your script in a file and call it main.py.
And you copy that to the little mini file system that's on the board.
And then whenever it turns on, whenever it gets power, it runs that main.py script straight away and just does whatever you have in there.
So you could have an infinite loop, you know, reading a sensor and outputting some LEDs or servo angle or something like that.
And, of course, it's not just a single file.
You've got the full import machinery of Python.
So if you also copy some libraries to the little file system on the board, then you can import them as well.
So you can do lots and lots of different things.
That's cool.
And how much storage space do I have?
CodeShip is a hosted continuous delivery service focused on speed, security, and customizability.
You can set up continuous integration in a matter of seconds and automatically deploy when your tests have passed.
CodeShip supports your GitHub and Bitbucket projects.
You can get started with CodeShip's free plan today.
Should you decide to go with a premium plan, Talk Python listeners can save 20% off any plan for the next three months by using the code TALKPYTHON.
All caps, no spaces.
Check them out at CodeShip.com and tell them thanks for sponsoring the show on Twitter where they're at CodeShip.
It has about just under 200 or 100 kilobytes file system, which is not very big.
So you can put a few scripts and maybe log some data.
But it does have expansion with an SD card.
So you can put a little micro SD card in.
For a few dollars, you can buy something that has a gigabyte of storage and then you can run scripts from there.
So you can put everything there and log lots of data, do whatever you like.
So it's expandable in that sense.
Yeah, that's excellent.
So you should be able to put a lot of code on there for 200K.
Yeah, yeah.
And yeah, the other, there's actually, well, the third way to use it is you can actually sort of, you can write your script on your PC and then run it remotely by using a small, actually another Python script, which sort of downloads your script onto the board and runs it straight away.
So that's a quick way to sort of prototype your code.
Yeah, yeah, that's cool.
How do you get files on there other than this prototyping way?
Can I, does it like show up as a drive when I connect it or?
Yeah, it shows up as a USB drive and you just, it's just like a USB stick.
That's it.
Yeah, cool.
Can I set up networking if I want to do a little internet of thing type stuff?
Yes.
So the, the Pyboard itself has drivers for a wireless chip.
So you have to buy an extra wireless chip and connect it up and then it will work as a, it'll work as sort of a Wi-Fi extension.
So you have, you have your normal Python sockets.
So you can create a socket and you can, you bind it and connect it to a server.
You can either be a server or a client.
So you can do that kind of, the same Python stuff you can do on your PC on, on the board.
It's a bit restricted.
Yeah.
Could I run a web server on it?
Yes, you can.
It's like probably one of the micro, yeah, like one of the micro frameworks, like a Flask or Pyramid or something like that might be better.
Yeah.
Flask, even Flask might be a little bit big.
I mean, you'd be surprised.
Flask is, I mean, these, when you load up a full Python app,
there's a lot of stuff that actually gets imported that you don't think about.
So in micro Python, which we have to think about all that and, and really strip it back.
But the other thing, so there, there's been a few other Kickstarters based around micro Python since mine,
a year and a half, yeah, almost two years ago now started.
So just recently there was this Wi-Fi Kickstarter, which is actually a Wi-Fi module that runs micro Python.
So it's one ship and it has Wi-Fi built into it.
So it really is an Internet of Things toy.
Well, it's more than a toy.
It's a cool Internet of Things device that you, that is like the Pyboard in that you, it runs micro Python.
And you can tell net in to get a REPL prompt and you can FTP in to copy your main dot pyscript across.
So it's, it's really a proper wireless from the, from the ground micro Python device.
And on that you can make a server or a client and you, you can sort of attach a battery, put it behind your sofa.
And then there's this little Python wireless thing running behind your sofa.
That is really cool.
Yeah.
Just Google WIPY Kickstarter.
Yeah.
Wi-Fi and you'll find it.
That's awesome.
And it looks like it, it also really exceeded its, its Kickstarter goal.
You, they were going for 30, 30,000 pounds and they got 75,000 pounds.
That's great.
Yeah.
I think they're in euros there.
Yes.
Oh yeah, that's right.
It's euros.
So yeah, more like dollars.
Yeah.
Right now with the exchange rate.
Okay.
Very cool.
Several times I've had the thought as you're talking about this, you're like,
Oh, we've really got to squeeze the performance out of this framework or that framework is actually kind of heavy in this way.
Or I rewrote these things to be more efficient.
Is there a place for micro Python in the data center?
Well, maybe, maybe in the future.
Yeah.
So what I'm thinking is, you know, if you've got like these little Docker containers and if you're looking for server density, you know, maybe having something that was focused on a micro controller that uses so few things.
So few resources, you might be able to run really, really dense sort of cloud computing systems and do interesting things with it.
What are your thoughts?
Yeah.
No, definitely.
That's actually one of the directions of the project as well.
So it's so that the soft micro Python software runs on the Pyboard and the Wi-Fi and other pieces of hardware, but it also runs on your PC, Linux, Mac or Windows.
And it's basically Python, but unbloated.
So it tries to be a really efficient version of Python and as compatible as it can be.
So it's very, very quick to start up and it uses, you know, almost no memory.
And I've actually tried sort of spawning, you know, a thousand micro Pythons on my computer and it's fine.
It runs a thousand micro Pythons.
And I tried spawning a thousand Python, normal Pythons, and then my computer just grounds to a halt.
So, okay, that's just a sort of a fun game.
But it shows you that, yeah, as you were saying, if you wanted to, you know, have a container and put in some app and you needed it to be efficient because you wanted to run lots of them, then maybe Macro Python can do what you need.
So, I mean, there is a movement.
There are some people who are helping to, there's quite a lot of people on GitHub working on Macro Python, which is really great.
Yeah, that's cool.
And that's just github.com/micropython, all our case, right?
Yes, that's right.
Yep.
But one of the things they're doing is implementing as much of the Python standard library as can be done in order to run some real-life applications like a web server, for example.
And, yeah, I mean, there's some other interesting applications as well.
Running a web server is sort of, yeah, the number one thing that you think about when you're thinking about, you know, Python and running lots of instances.
So that's definitely something that we're working towards.
I mean, it already can run web server for sure.
But I don't know of anyone who's doing that in production just yet.
Yeah, actually doing it.
Well, maybe we're giving some people some ideas out there.
Yeah.
It seems like an interesting thing to be the sort of foundation of some lightweight, restful services that you want to just have lots of horizontal scale on.
Yeah, yeah.
Yeah, there's definitely lots of scope for this.
Lots and lots of scope.
Okay.
So the actual operating system on the microcontroller, that's written in Python?
Python is the operating system.
That's what – I think that's a good way to describe it.
That's really awesome.
On the microcontroller, there is no operating system.
It's just bare metal.
So the first thing that happens when it boots up is you have to set the stack pointer and you have to copy the data from ROM to RAM.
And then you've got sort of free reign.
You can do whatever you like.
So, you know, it jumps into some initialization, loads up the Python runtime, and then jumps to the REPL.
So Python essentially is the operating system, and it's giving you full control over the microcontroller.
There's nothing – there's no context switching.
I mean, every cycle is yours to spend on Python code.
That's fantastic.
Yeah.
So you can – I mean, you can do things like disable interrupts with a Python call, and then, you know, everything's nice and quiet.
You can do things in cycle count and then turn them back on again.
You can set interrupts to trigger, so you can have a timer which counts and say, you know, 10 times a second fires an interrupt.
And when that interrupt fires, you can call a Lambda function in Python to do something, and then it will return.
So you can call Python on an interrupt.
That's pretty awesome to hook a Lambda function straight to like a hardware interrupt.
That's super cool.
Yeah.
So you can do that.
And the other, I think, very nice thing – well, the thing that I think is quite cool is you can write inline assembler code within Python.
So just like in C, how you can say, you know, inline assembler, and you write your functions there.
In Python, you can write a function, and you use the inline assembler decorator for that function.
It's a special decorator.
And then the compiler knows that instead of compiling Python, it compiles the, you know, the arm assembly.
It assembles it.
And then it turns that into a normal function that you can call from Python.
You can even pass arguments to that function.
And then it can do whatever you want to do in assembler, and it can even return stuff back to Python land.
So this way, if you really need to do some low-level things, then it's at your fingertips.
That's very cool.
That's kind of your escape hatch from – if you haven't thought of it in Python, if you guys haven't pre-made this feature available, that's the way to do it, right?
That's right.
Yeah.
You can access the direct hardware however you like.
Now, I'm not sure that I would be super comfortable doing much in direct assembler, but I would be in C.
Is there a possibility of having, like, inline C in the future?
There already is such a thing.
It's not inline C, but it's a mode of a compiler, the Python compiler, that is.
So the Python compiler in MicroPython is a bytecode compiler.
It reads your script, and it turns it into a parse tree, and then it compiles the parse tree into some bytecode.
But there's a sort of an option to – instead of emitting bytecodes, it can emit direct machine code.
So this is kind of like just-in-time compiling, although it's ahead of time compiling.
But this is how the V8 JavaScript engine works.
Instead of targeting a virtual machine with bytecode, it just targets the direct hardware by emitting.
So if I wanted to load an integer onto the stack, then instead of calling a bytecode, I can just do the actual machine code.
And then if I wanted to call a function or call a method, then I just implement the machine code to actually do that.
So this makes your code run faster, but it also allows you to declare types to say, okay, using Python's type annotations, I'm going to say that this argument is an integer and this one's an integer.
And then MicroPython can optimize that when you do an addition.
And instead of calling lots and lots of calls to do an addition to these two numbers, it knows that it can use a single hardware add, and it just adds them.
And in this way, you essentially turn your Python code into sort of C-like code.
But it still looks Python, but it's compiled to something that would have happened if it was written in C.
So you've got pointers, and you've got integers, and you can do loops.
So you can write much faster code that way if you really need to.
Yeah, that sounds really interesting.
One thing I'd like to sort of think about with you is what's the tradeoff in terms of performance if I were to, say, work with embedded C on a regular, like, say, embedded Linux or something versus Python on MicroPython on this board?
So versus, say, Python on a regular, like, Adreno or something in terms of relative performance.
What's the tradeoffs here?
So, okay, yeah, if you want a number, it's something like a factor of 100 slower when you're running Python's bytecode.
So each, so, yeah, this is a very rough number.
So, you know, if you wanted to do a loop, which was flashing an LED on and off, then if you've written that loop in Python, you can get maybe 100 kilohertz out of that loop.
But if you're writing it in directly in C, you'll probably get 10 megahertz out of the loop.
Now, I mean, yeah, look, MicroPython is not meant to replace C in any form.
It's, I mean, it's supposed to be there for doing high-level control of all these sophisticated components.
And if you really need to run a 10 megahertz loop, then, well, you could use the inline assembler, maybe, or you could use this other mode I was talking about before.
Or you can write your own C function and then recompile MicroPython and use that C function as sort of an extension to MicroPython if you need to.
So you have lots of different options.
But there is, of course, going to be some kind of overhead in terms of the speed.
I mean, you can't escape that.
Yeah, no, that's totally reasonable.
I guess one of the things I was trying to get a sense of is how does MicroPython compare to CPython on the same hardware?
Ah, I see.
This episode is brought to you by Hired.
Hired is a two-sided, curated marketplace that connects the world's knowledge workers to the best opportunities.
Each offer you receive has salary and equity presented right up front, and you can view the offers to accept or reject them before you even talk to the company.
Typically, candidates receive five or more offers in just the first week, and there are no obligations ever.
Sounds pretty awesome, doesn't it?
Well, did I mention there's a signing bonus?
Everyone who accepts a job from Hired gets a $2,000 signing bonus.
And as Talk Python listeners, it gets way sweeter.
Use the link Hired.com slash Talk Python to me, and Hired will double the signing bonus to $4,000.
Opportunity's knocking.
Visit Hired.com slash Talk Python to me and answer the call.
On the same hardware, it's impossible to measure because C-Python won't run on a Pyboard.
That's really not working.
If you want to try and compare apples with apples, you can run MicroPython on your PC, and you can run C-Python on your PC, and you can compare the two using some benchmark that you like.
Actually, MicroPython is slightly faster than C-Python for a reasonable range of benchmarks that we have.
For example, the Pystone benchmark, MicroPython runs a little bit faster than C-Python.
And that's because it uses less resources, and the way it handles integers is a bit more efficient.
So, you know, not having to allocate RAM to do certain things makes it faster.
But, of course, there are other things that C-Python is quicker at.
But, actually, on par, they're about on par when you take your own overall sort of benchmark.
Okay, that's very interesting.
So, it's more like they're on par, but C-Python is kind of unsuitable for these microcontrollers because it's so big and memory hungry, yeah?
One thing this might be worth talking about is where is MicroPython used?
Are there some notable, I guess, I'm not sure if deployment's the right word, but products or devices with MicroPython?
Well, it's been used, I think, in quite a lot of places.
Some of them that I don't know about and some of them I do know about.
I think it's used a lot in robotics.
There's been a few other Kickstarters as well.
To do with MicroPython.
There was this Robocore Kickstarter where they made a little sort of robot brain that ran MicroPython so you could make robots.
And then the Wi-Fi, obviously, for Internet of Things.
It's been used in teaching.
There's some people that will be using it for teaching university classes because it's sort of a good mix of, you know,
in university course you don't have all this time to go into the details of getting cross-compilers working and all this stuff.
You just want to teach some basics about low-level interrupts and controlling an I2C bus.
And doing that in Python, yeah, it seems like a really great way because Python is so simple.
You totally understand what's going on and so it lets you focus on the concept, right?
Yeah, exactly.
And turn an LED on and off and sort of get some hands-on use of some hardware straight away.
So, I mean, in the UK here there's a big push for students at primary school even to start learning languages.
So, they have to learn two languages and mostly Python is one of the languages they learn.
And they also do sort of some hardware in their sort of design or science class.
So, if these two things go together, if MicroPython, because they already know Python and they need to do some sort of simple exercises,
it's a very easy path for them to take.
So, it's been used in education and I think it will grow a bit more.
One other very exciting thing is that the European Space Agency has actually expressed interest in using MicroPython for space-based applications.
That's awesome.
Because the hardware that runs in space is very limited in resources because it has to be proven that, you know,
it takes a long time to prove that a piece of hardware is actually run in space given its radiation hardening and so on.
So, yes, the European Space Agency has agreed to fund some further development of MicroPython to see if it can be made robust and deterministic
so that it can be put in a space-based application.
So, there's more than, yeah, more than just the Internet of Things.
Yeah, that's, yeah, space.
That is really cool.
I guess space agencies, like the European one and NASA and so on,
they're traditionally not really thought of as rapid, agile-type places.
And anything they can do to move away from these languages like Fortran and C that make it really hard to build reliable, tested stuff,
and something simpler like Python, it seems like a really big win for them.
So, that'd be amazing.
I mean, we look forward to the developments to see what will happen if it's happening.
It's very exciting.
Yeah.
Maybe some amazing picture of galaxies will be pulled down someday and it'll be, have taken, been taken by Python.
That'd be cool.
So, speaking of space and robots and things like that, another area where we've had people on the show,
and I know Python is used, is robot operating system, ROS and ROSPy.
Is there anything going on between those two projects?
With MicroPython, not that, no, we don't have direct communication with them yet.
But, I mean, ROS is more of a, ROS is sort of a very, it's sort of a large operating system in the sense that it needs sort of a PC to run on it.
It doesn't run directly on a microcontroller.
Yeah, it's more like an i7 or something like that, right?
So, yeah, so you'd need, so, MicroPython could fit in the picture there if they wanted to run an efficient Python implementation.
So, instead of running CPython, they could run MicroPython, but it would be sort of the PC version of MicroPython.
It wouldn't be the Pyboard version because the ROS has already taken over the system and it is controlling it.
Yeah.
Okay, that's very cool.
Good to know.
But there are other real-time operating systems that have MicroPython sort of already built in.
So, NutX is one of them.
And Riot is another real-time operating system that we're working to get MicroPython into.
So, there's, yeah, these things are good, interesting avenues.
Yeah, that's really nice.
There's a lot of stuff going out there and the more you guys can work together, the better, I guess.
Yeah, well, I mean, with an open-source project, it is an open-source project.
It's got an MIT license and anyone is free to contribute.
I mean, yeah, it's not something that's going to move super quickly unless it has help from external or from the community.
Of course.
You also said that the hardware was open hardware.
I don't do much with hardware, so that doesn't mean anything to me, but it sounds cool.
Can you talk about that?
It just means that the schematics are available for people to download and they can make their own board.
So, the idea with the PyBoard was that it's not supposed to be a sophisticated piece of hardware that's impossible for anyone to make themselves.
It was supposed to be simple enough that you could solder one by hand.
I mean, I made the first prototypes by hand just with a soldering iron.
So, it's not an overly complicated piece of hardware.
It's just there so that you can run MicroPython and connect it to some sensors and some motors and get things working straight away.
And then, if the PyBoard isn't exactly what you need because it doesn't have enough IO or you want to use a different microcontroller or you need more LEDs or something like that, you can make your own board very easily.
And MicroPython is sort of configurable enough that you can put it on your own chip.
That's really excellent.
And it runs on ARM chips, right?
Yes, it runs on ARM, but it also runs on other things as well.
It doesn't just have to be in ARM.
There are certain parts that are optimized for ARM, but there are other chips we also have it running on, like a microchip pick and this expressive Wi-Fi chip as well.
And these architectures are all different.
So, it's not just an ARM architecture that it's relying on.
Oh, that's really cool.
So, when I think of what I do during the day, I go and I sit down and I plug my MacBook into a larger monitor and have my keyboard and that's pretty much how I work.
But you're talking about things like hardware and soldering irons and all sorts of stuff.
You must have an interesting place where you work or an interesting setup.
Yeah, well, I think it's a mess like everyone else has set up, right?
There's just lots of stuff laying around.
But yeah, you're right.
There's soldering irons and there's oscilloscopes and, well, there's big screen and keyboards and everything.
And then lots and lots of pieboards just lying everywhere.
How many revisions did you go through the pieboards?
Is what you're shipping kind of what you started with or is it really different?
No, it's the same controller and pretty much the same.
It started out with the same features.
Accelerometer, SD card, a few LEDs and switches.
And it just sort of got a little bit smaller and all the components went on to the top side to make it cheaper to manufacture.
But otherwise, it's pretty much stayed the same.
I think this is the final version was the fifth revision.
Okay, excellent.
So if people want to get started working with MicroPython and the Pyboard, how do they do that?
Can they come and buy that from you?
Yes, they can.
They have different options.
They can buy it directly from us.
And it's, well, it's 23 pounds if you buy outside of the EU.
But if you're inside the EU, you've got to add 20% tax.
So it's 28.
We also have a distributor in America, Adafruit, who stocks them.
So you can buy it directly from them.
We have some also distributors in Australia and in Italy and Belgium.
And on the website, there are links to these places.
So you can buy from a few different places.
And, yeah, I mean, if you just want to try out the software, you can run it on your PC.
You can go to GitHub and download the code and compile it and see what it's like.
You can browse through the code.
It'll also run on some other boards that maybe some people already have and they just want to try it out, like a Teensy or a STMicroelectronics discovery kit.
So there's a few different options.
If you go to the GitHub page, there's quite a lot of explanation there.
There's also the forum, which has a lot of people you can talk to there and ask questions.
And these are all linked from micropython.org.
Okay, excellent.
And I recommend just to see what's going on that you go to micropython.org and just play the video.
It's like nine minutes or something.
And it'll give you – it's a good video and it really gives you a quick visual overview of what you guys are all about.
Yes, great.
Yeah, and I'll put, of course, all that stuff, links to all those things in the show notes.
So, Damien, I think this might be a good place to kind of wrap up the show.
So, a question I usually ask my guests on the way out the door is, you know, there's a ton of stuff on PyPI, the package index, that's really amazing.
And everybody kind of has their own favorite packages they work with and things they've discovered.
Do you have any favorites you want to give a shout-out to?
Favorite PyPI packages?
Well, there's a whole bunch of MicroPython PyPI packages.
Because MicroPython also uses PyPI and has a built-in package manager, MicroPip, so you can download lots of things from PyPI there.
But as for myself, in normal Python, wow, I mean, there's so many things that I use.
I mean, in my work in physics, I use a lot of scientific computing like NumPy, of course, and SciPy is very good.
Yeah, I don't know if I could narrow it down to one particular thing.
Yeah, it's kind of like a candy sword a little bit.
It's very nice.
A lot of stuff that looks good.
Awesome.
Okay, anything else you want to let the listeners know about that I haven't asked you about or we haven't had a chance to talk about?
Only that if they're interested, then please go to GitHub and take a look.
And there's lots of coding that can be done from, you know, beginner stuff to more advanced stuff.
You can write tests or you can just implement some module or some feature that's missing right up to, you know, trying to fix some nasty bugs or help improve, make it more efficient and reduce memory usage.
So there's lots of stuff that can be worked on.
And I think, I mean, for me, it's a lot of fun.
So maybe someone else would also find it fun to join in the coding experience.
Yeah, that's great.
And you guys have 63 contributors on GitHub.
So I'm guessing you're pretty open to having people jump in and help out and add to that.
Yeah, sure, sure.
We are very open.
Okay, great.
Damien, it's been a really good conversation.
I've really learned a lot.
Yeah, well, thanks for having me, Michael.
It's been great.
Yeah, you bet.
Talk to you later.
Okay, thank you.
Bye.
This has been another episode of Talk Python to Me.
Today's guest was Damien George.
And this episode has been sponsored by CodeChip and Hired.
Thank you guys for supporting the show.
Check out CodeChip at CodeChip.com and thank them on Twitter via at CodeChip.
Don't forget the discount code for listeners.
It's easy.
Talk Python.
All caps.
No spaces.
Hired wants to help you find your next big thing.
Visit Hired.com slash Talk Python To Me to get five or more offers with salary and equity
presented right up front and a special lister signing bonus of $4,000.
You'll find the links from today's show at Talk Python To Me.com slash episodes slash show slash 17.
Be sure to subscribe to the show.
Open your favorite podcatcher and search for Python.
We should be right at the top.
You can also find the iTunes and direct RSS feed links in the footer of the website.
Our theme music is the song Developers, Developers, Developers by Corey Smith, who goes by Smix.
You can hear the entire song on our website.
This is your host, Michael Kennedy.
Thanks for listening.
Smix, take us out of here.
Staying with my voice.
There's no norm that I can fill within.
Haven't been sleeping.
I've been using lots of rest.
I'll pass the mic back to who rocked it best.
I'm first developers.
Developers, developers, developers, developers.