#17: Python on bare metal with MicroPython Transcript
00:00 How many layers of abstraction and indirection are between your Python code and machine instructions?
00:05 What if that number could be one and Python itself was the operating system?
00:08 That would be so amazing, right?
00:10 In fact, it is amazing, and it's called MicroPython.
00:13 This is show number 17.
00:15 It's MicroPython with guest Damian George.
00:18 You're listening to Talk Python to Me, and this was recorded on Thursday, June 25, 2015.
00:24 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.
00:35 I construct it line by line, just like when I'm coding another software design.
00:39 In both cases, it's about design patterns.
00:42 Anyone can get the job done.
00:44 It's the execution that matters.
00:45 I have many interests.
00:47 Sometimes it can flit.
00:48 My creativity can usually be...
00:50 Welcome to Talk Python to Me, a weekly podcast on Python, the language, the libraries, the ecosystem, and the personalities.
00:57 This is your host, Michael Kennedy.
00:59 Follow me on Twitter, where I'm @mkennedy.
01:01 Keep up with the show and listen to past episodes at talkpythontome.com.
01:06 And follow the show on Twitter, where we're at Talk Python.
01:09 This show will be talking to Damian George about rethinking the Python runtime for embedded microcontrollers and his implementation, MicroPython.
01:18 This episode is brought to you by CodeShip and Hired.
01:21 Thank them for supporting the show on Twitter via at CodeShip and at Hired underscore HQ.
01:27 Before we get to the interview with Damian, I'd like to give a shout out to Ganesh Kupan.
01:32 Ganesh suggested this topic for the show, and I think it's a great one.
01:37 Ganesh, thank you for sending that in and making this happen.
01:40 Now, let me introduce Damian.
01:43 Damian George is a theoretical physicist at the University of Cambridge and the creator of MicroPython.
01:48 Damian, welcome to the show.
01:50 Hi, Michael.
01:52 Thanks.
01:52 Yeah, this is really great.
01:53 We've talked a lot about software, and we've even talked a little bit about robots on the show before.
01:58 And now we're going to talk about sort of the most low-level stuff you can do with Python that I know of.
02:05 We're going to talk about running Python directly on microchips and microcontrollers, right?
02:10 Yes, that's right.
02:12 Yeah.
02:12 Your project, MicroPython, at micropython.org is really cool.
02:16 Thank you.
02:17 Yeah, you're welcome.
02:18 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.
02:24 And so we're definitely going to do that.
02:26 We're going to talk all about that.
02:28 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.
02:33 Okay.
02:33 Well, I mean, I've always liked computers and using computers and programming since I can remember.
02:41 I know that I've written many languages, toy languages, ever since I could start programming.
02:47 It's always been sort of a passion.
02:50 I think anyone who gets into programming at some stage tries writing their own language.
02:55 That's why there's so many of them.
02:57 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.
03:07 That's sort of something that I did in my spare time.
03:11 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.
03:30 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.
03:40 Absolutely.
03:41 Was that mostly in C or C++?
03:43 And what languages and stuff did you use in that field?
03:47 We learned a lot of languages like Haskell and Java and C and C++.
03:53 So, yeah, but I mean, it was a bit about learning languages, but also you learn algorithms like hash tables and things like that.
04:02 And then, you know, you learn, I mean, you learn how to write a compiler.
04:07 And it's not so much about the language.
04:09 It's about the concepts and the algorithms.
04:12 Right, like the abstract syntax trees and things like this, yeah?
04:15 Yeah, that's right.
04:17 That's right.
04:17 So, I mean, this is what I studied.
04:20 I actually also studied theoretical physics and pure math.
04:25 So, I did a lot of extra stuff.
04:29 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.
04:40 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.
04:49 Yeah, I think you're totally right.
04:50 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.
04:57 And, like, you're going to have to learn to program this thing.
04:59 And it turned out I liked computers better, so I never did get a PhD.
05:03 But I totally know where you're coming from.
05:05 That's awesome.
05:05 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.
05:16 And, I mean, yeah, if you like the kind of stuff, it's really good.
05:20 But if you don't, you know, maybe if you want to build an actual apparatus, then you probably need to be an engineer.
05:27 I think, yeah, my background is all these things combined.
05:31 And, yeah, I just really like the technical side of things.
05:35 That's cool.
05:37 Both programming and hardware.
05:38 Yeah, so how did you get into Python?
05:40 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.
05:51 And then somehow you ended up in Python, right?
05:53 Yeah, I mean, I think it's good if you know more than one language or more than two or more than five languages.
06:01 And Python is such a popular language and for a good reason.
06:04 It's a very nice language.
06:07 And so, yeah, it's just one of the ones that you will, I think, eventually learn if you're into programming.
06:13 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++.
06:25 Because, yeah, I mean, C is a great language and I love writing code in C.
06:31 I mean, I know it's a very old language, but it allows you to be really close to the hardware.
06:36 I mean, a line of C translates to a few lines of assembly and you sort of know what's going on.
06:42 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.
06:47 But it gives you more power and when you need it, speed.
06:51 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.
07:09 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.
07:18 Yeah, that's a lovely thing about Python, isn't it?
07:21 Yeah, yeah, exactly.
07:22 You just, you know, you don't have to allocate lists and make links in the list.
07:27 You know, it just works having a list or it just works having a hash table, a dictionary.
07:31 And, you know, you have lambdas and you have list comprehension and you have generators.
07:36 And these things allow you to think in a certain way.
07:40 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.
07:48 But they're really good concepts.
07:49 And a language that allows you to express those concepts easily is a good language.
07:55 I agree.
07:56 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.
08:10 Right.
08:11 You've taken the opposite perspective.
08:13 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.
08:21 That's really awesome.
08:21 So you call this project MicroPython, right?
08:24 Yeah, that's right.
08:25 MicroPython.
08:26 I think the name says it all.
08:29 That's awesome.
08:31 If you want Python on a microcontroller, then that's what you need.
08:36 I did it as a sort of, to begin with, it was more of a can it be done question.
08:41 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.
08:58 So that was kind of what started it for me was just the technical interest.
09:03 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?
09:16 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.
09:28 Now they have 100 kilobytes.
09:31 And that's starting to be interesting.
09:35 They also have really sophisticated peripherals.
09:38 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.
09:49 It has DMA to do quick transfers and this kind of stuff.
09:54 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.
10:04 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.
10:18 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.
10:31 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.
10:44 It's a pretty basic operation.
10:46 It's something that you can do in Python quite efficiently.
10:50 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.
11:03 So you can start to really abstract the low level hardware.
11:09 And it's still relatively efficient because, well, the way MicroPython is implemented, you still have running relatively efficiently on the microcontroller.
11:18 You know, I've seen your API and it looks super clean.
11:22 I saw your Kickstarter video and the one that you have on micropython.org is sort of the opener video to introduce people.
11:28 That's really cool.
11:29 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.
11:36 Sure.
11:37 Well, so MicroPython is a rewrite, a complete re-implementation from scratch of Python sort of version three, as it were, 3.4, actually.
11:51 So complete rewrite, no components whatsoever that are in common with CPython.
11:59 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.
12:10 So that was sort of the guiding principle is use as little RAM as possible.
12:15 So MicroPython, the software, is this re-implementation of Python.
12:21 And it includes all of your favorites, lists and dictionaries and sets.
12:26 And, I mean, it's complete Python 3.4 in terms of the language specification.
12:31 Probably not the same standard library, though.
12:34 Probably things like dbapi2 is not there or something like that.
12:37 Right, yeah.
12:38 So there's the language in terms of the syntax and the semantics, you know, classes and function definitions.
12:44 And then there's the library.
12:46 So import numpy or whatever.
12:48 Import this, import that.
12:51 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.
13:02 And these things, some of them are not sensible to run on a microcontroller and some of them won't fit on a microcontroller.
13:10 And so not all, well, not that many of the standard libraries are available.
13:16 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.
13:25 And, I mean, we're always looking to add more.
13:27 It's just that the manpower to write all of these things in a micro way is not that easy.
13:33 Do you have to re-implement every standard library package or module that you're using?
13:39 Some of them we have, for example, JSON printing and parsing is rewritten because you want to make it efficient.
13:48 But other things actually import directly.
13:52 We can just copy over the CPython standard library and use that code directly.
13:58 So, yeah, it depends what you want to do.
14:01 I mean, the idea really is to provide micropython as the core language.
14:06 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.
14:15 And, you know, if not, maybe it needs a few little tweaks here and there.
14:21 But, yeah, we concentrate mostly on the core language and providing a very robust and hopefully efficient core language.
14:33 So that's, but that's the software side of things.
14:36 Then there's the hardware side of things, which is, so the Pyboard, which is this little microcontroller that runs micropython.
14:45 And all the peripherals on this Pyboard are well supported, like the I2C bus or the UART, for example.
14:53 And the aim with this was, well, to be able to sort of kickstart this whole idea.
15:00 We went to Kickstarter.
15:01 Yeah.
15:03 Why don't you tell everybody a little bit about your Kickstarter?
15:05 It was super, super successful.
15:07 I saw you originally were going out for 15,000 pounds.
15:10 Yes.
15:11 And you raised 97,000 pounds or 153,000 US dollars.
15:16 That's, you must have been super thrilled.
15:18 Yeah.
15:19 Well, it was, it was a great reception.
15:21 We had a lot of people get on board.
15:24 And yeah, I mean, it was, it was really good.
15:27 I didn't expect it to be that big.
15:29 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.
15:36 And then, you know, getting it running, making a little board.
15:40 So the idea with the board was that I could give something to the Kickstarter backers.
15:45 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.
15:53 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.
16:01 That's cool.
16:02 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?
16:11 I'd say it was about 50, 50 to begin with.
16:15 Up until when I launched the campaign, it was about 50, 50.
16:19 The software, I mean, yeah, I didn't write the entire thing before the Kickstarter campaign started.
16:25 I wrote enough to be able to convince myself and others that it would actually work.
16:29 Like you have a REPL and you can turn lights on and off and so on.
16:33 So there were bits of the language missing at the Kickstarter stage.
16:37 And probably 50% of the time was spent actually building the hardware.
16:42 So I had to learn about these microcontrollers and how they work.
16:47 The hardest thing is getting a tool chain up and running to cross compile stuff and download it and then, you know, debug it.
16:52 And why doesn't my USB thing work and so on?
16:55 There's a lot of really technical and difficult things to sort of get your head around when you first get into microcontrollers.
17:04 And I mean, these days with their 32-bit ARM chips, they're very powerful things.
17:09 And there's lots of little things that can go wrong.
17:14 So, yeah, I liked it.
17:16 It was a good balance of software and hardware.
17:18 Then after the Kickstarter, when the hardware was finalized, it shifted more into software.
17:23 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.
17:37 Yeah, it looks really rich.
17:39 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.
17:45 You know, an accelerometer, for example, storage, all sorts of stuff.
17:50 It seems pretty much like I could take that, put it into something with a power source and use it.
17:57 Yeah, well, for sure.
17:58 I mean, the aim is you plug it into USB and it works straight away.
18:01 I just need to power source and it's got so that the accelerometer is not built into the microcontroller.
18:07 That's a separate little chip that's on there.
18:09 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.
18:19 An accelerometer is like a lot more fun to play with.
18:23 On your video, you have a little app you wrote where there's a robot that can lean forwards, backwards, left, right.
18:30 And as you tilt the board, the robot kind of leans in the way.
18:33 And you're doing that with an accelerometer and Python.
18:37 That's right.
18:37 That was a really cool example.
18:38 Yeah, well, I mean, that's only, you know, a handful of lines.
18:43 It's just an infinite loop while true.
18:45 And then you read the accelerometer, which is like one line in Python.
18:50 And then you set the servo angle based on that accelerometer reading.
18:54 So, you know, it really is just a few lines to do that stuff.
18:58 Okay, cool.
18:59 So maybe you could walk the listeners through what it's like from going, you know, file new project to having, what was it?
19:06 A main.py that runs when you turn on the board?
19:09 Yeah.
19:10 What's the lifecycle look like there?
19:12 So there's actually a few different ways of using it.
19:16 Okay.
19:18 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.
19:29 It depends if you use, you know, Linux or Windows or Mac.
19:33 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.
19:43 And that runs directly on the board.
19:45 So you can type commands in straight away and they will be executed and do whatever you ask on the board.
19:52 So that's a good way to sort of just play around with things, debug things, test things.
19:57 The other way of programming it is to write your code, your script in a file and call it main.py.
20:07 And you copy that to the little mini file system that's on the board.
20:11 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.
20:20 So you could have an infinite loop, you know, reading a sensor and outputting some LEDs or servo angle or something like that.
20:26 And, of course, it's not just a single file.
20:29 You've got the full import machinery of Python.
20:32 So if you also copy some libraries to the little file system on the board, then you can import them as well.
20:39 So you can do lots and lots of different things.
20:42 That's cool.
20:42 And how much storage space do I have?
20:44 CodeShip is a hosted continuous delivery service focused on speed, security, and customizability.
21:03 You can set up continuous integration in a matter of seconds and automatically deploy when your tests have passed.
21:09 CodeShip supports your GitHub and Bitbucket projects.
21:12 You can get started with CodeShip's free plan today.
21:15 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.
21:23 All caps, no spaces.
21:25 Check them out at CodeShip.com and tell them thanks for sponsoring the show on Twitter where they're at CodeShip.
21:38 It has about just under 200 or 100 kilobytes file system, which is not very big.
21:46 So you can put a few scripts and maybe log some data.
21:50 But it does have expansion with an SD card.
21:54 So you can put a little micro SD card in.
21:56 For a few dollars, you can buy something that has a gigabyte of storage and then you can run scripts from there.
22:01 So you can put everything there and log lots of data, do whatever you like.
22:06 So it's expandable in that sense.
22:10 Yeah, that's excellent.
22:11 So you should be able to put a lot of code on there for 200K.
22:17 Yeah, yeah.
22:17 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.
22:37 So that's a quick way to sort of prototype your code.
22:41 Yeah, yeah, that's cool.
22:42 How do you get files on there other than this prototyping way?
22:46 Can I, does it like show up as a drive when I connect it or?
22:50 Yeah, it shows up as a USB drive and you just, it's just like a USB stick.
22:54 That's it.
22:57 Yeah, cool.
22:58 Can I set up networking if I want to do a little internet of thing type stuff?
23:01 Yes.
23:03 So the, the Pyboard itself has drivers for a wireless chip.
23:09 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.
23:18 So you have, you have your normal Python sockets.
23:21 So you can create a socket and you can, you bind it and connect it to a server.
23:26 You can either be a server or a client.
23:28 So you can do that kind of, the same Python stuff you can do on your PC on, on the board.
23:34 It's a bit restricted.
23:36 Yeah.
23:36 Could I run a web server on it?
23:37 Yes, you can.
23:39 Yes, you can.
23:39 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.
23:45 Yeah.
23:46 Flask, even Flask might be a little bit big.
23:48 I mean, you'd be surprised.
23:50 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.
23:58 So in micro Python, which we have to think about all that and, and really strip it back.
24:04 But the other thing, so there, there's been a few other Kickstarters based around micro Python since mine,
24:12 a year and a half, yeah, almost two years ago now started.
24:18 So just recently there was this Wi-Fi Kickstarter, which is actually a Wi-Fi module that runs micro Python.
24:29 So it's one ship and it has Wi-Fi built into it.
24:32 So it really is an Internet of Things toy.
24:34 Well, it's more than a toy.
24:35 It's a cool Internet of Things device that you, that is like the Pyboard in that you, it runs micro Python.
24:44 And you can tell net in to get a REPL prompt and you can FTP in to copy your main dot pyscript across.
24:52 So it's, it's really a proper wireless from the, from the ground micro Python device.
24:59 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.
25:05 And then there's this little Python wireless thing running behind your sofa.
25:10 That is really cool.
25:11 Yeah.
25:11 Just Google WIPY Kickstarter.
25:15 Yeah.
25:16 Wi-Fi and you'll find it.
25:17 That's awesome.
25:17 And it looks like it, it also really exceeded its, its Kickstarter goal.
25:23 You, they were going for 30, 30,000 pounds and they got 75,000 pounds.
25:27 That's great.
25:27 Yeah.
25:28 I think they're in euros there.
25:29 Yes.
25:30 Oh yeah, that's right.
25:31 It's euros.
25:31 So yeah, more like dollars.
25:32 Yeah.
25:33 Right now with the exchange rate.
25:34 Okay.
25:34 Very cool.
25:35 Several times I've had the thought as you're talking about this, you're like,
25:40 Oh, we've really got to squeeze the performance out of this framework or that framework is actually kind of heavy in this way.
25:45 Or I rewrote these things to be more efficient.
25:48 Is there a place for micro Python in the data center?
25:53 Well, maybe, maybe in the future.
25:56 Yeah.
25:57 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.
26:09 So few resources, you might be able to run really, really dense sort of cloud computing systems and do interesting things with it.
26:16 What are your thoughts?
26:17 Yeah.
26:17 No, definitely.
26:19 That's actually one of the directions of the project as well.
26:23 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.
26:35 And it's basically Python, but unbloated.
26:39 So it tries to be a really efficient version of Python and as compatible as it can be.
26:45 So it's very, very quick to start up and it uses, you know, almost no memory.
26:49 And I've actually tried sort of spawning, you know, a thousand micro Pythons on my computer and it's fine.
26:56 It runs a thousand micro Pythons.
26:58 And I tried spawning a thousand Python, normal Pythons, and then my computer just grounds to a halt.
27:04 So, okay, that's just a sort of a fun game.
27:09 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.
27:23 So, I mean, there is a movement.
27:26 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.
27:34 Yeah, that's cool.
27:35 And that's just github.com/micropython, all our case, right?
27:39 Yes, that's right.
27:39 Yep.
27:40 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.
27:56 And, yeah, I mean, there's some other interesting applications as well.
28:03 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.
28:11 So that's definitely something that we're working towards.
28:15 I mean, it already can run web server for sure.
28:20 But I don't know of anyone who's doing that in production just yet.
28:25 Yeah, actually doing it.
28:26 Well, maybe we're giving some people some ideas out there.
28:28 Yeah.
28:29 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.
28:39 Yeah, yeah.
28:40 Yeah, there's definitely lots of scope for this.
28:43 Lots and lots of scope.
28:44 Okay.
28:44 So the actual operating system on the microcontroller, that's written in Python?
28:49 Python is the operating system.
28:51 That's what – I think that's a good way to describe it.
28:56 That's really awesome.
28:56 On the microcontroller, there is no operating system.
29:01 It's just bare metal.
29:02 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.
29:10 And then you've got sort of free reign.
29:14 You can do whatever you like.
29:15 So, you know, it jumps into some initialization, loads up the Python runtime, and then jumps to the REPL.
29:21 So Python essentially is the operating system, and it's giving you full control over the microcontroller.
29:28 There's nothing – there's no context switching.
29:31 I mean, every cycle is yours to spend on Python code.
29:35 That's fantastic.
29:36 Yeah.
29:38 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.
29:45 You can do things in cycle count and then turn them back on again.
29:49 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.
29:57 And when that interrupt fires, you can call a Lambda function in Python to do something, and then it will return.
30:03 So you can call Python on an interrupt.
30:06 That's pretty awesome to hook a Lambda function straight to like a hardware interrupt.
30:11 That's super cool.
30:11 Yeah.
30:12 So you can do that.
30:14 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.
30:22 So just like in C, how you can say, you know, inline assembler, and you write your functions there.
30:27 In Python, you can write a function, and you use the inline assembler decorator for that function.
30:34 It's a special decorator.
30:36 And then the compiler knows that instead of compiling Python, it compiles the, you know, the arm assembly.
30:43 It assembles it.
30:45 And then it turns that into a normal function that you can call from Python.
30:49 You can even pass arguments to that function.
30:53 And then it can do whatever you want to do in assembler, and it can even return stuff back to Python land.
30:57 So this way, if you really need to do some low-level things, then it's at your fingertips.
31:04 That's very cool.
31:05 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?
31:15 That's right.
31:16 Yeah.
31:17 You can access the direct hardware however you like.
31:21 Now, I'm not sure that I would be super comfortable doing much in direct assembler, but I would be in C.
31:26 Is there a possibility of having, like, inline C in the future?
31:30 There already is such a thing.
31:33 It's not inline C, but it's a mode of a compiler, the Python compiler, that is.
31:42 So the Python compiler in MicroPython is a bytecode compiler.
31:46 It reads your script, and it turns it into a parse tree, and then it compiles the parse tree into some bytecode.
31:54 But there's a sort of an option to – instead of emitting bytecodes, it can emit direct machine code.
32:01 So this is kind of like just-in-time compiling, although it's ahead of time compiling.
32:06 But this is how the V8 JavaScript engine works.
32:09 Instead of targeting a virtual machine with bytecode, it just targets the direct hardware by emitting.
32:16 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.
32:24 And then if I wanted to call a function or call a method, then I just implement the machine code to actually do that.
32:32 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.
32:43 And then MicroPython can optimize that when you do an addition.
32:46 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.
32:57 And in this way, you essentially turn your Python code into sort of C-like code.
33:06 But it still looks Python, but it's compiled to something that would have happened if it was written in C.
33:13 So you've got pointers, and you've got integers, and you can do loops.
33:17 So you can write much faster code that way if you really need to.
33:23 Yeah, that sounds really interesting.
33:26 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?
33:44 So versus, say, Python on a regular, like, Adreno or something in terms of relative performance.
33:52 What's the tradeoffs here?
33:55 So, okay, yeah, if you want a number, it's something like a factor of 100 slower when you're running Python's bytecode.
34:02 So each, so, yeah, this is a very rough number.
34:06 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.
34:19 But if you're writing it in directly in C, you'll probably get 10 megahertz out of the loop.
34:24 Now, I mean, yeah, look, MicroPython is not meant to replace C in any form.
34:33 It's, I mean, it's supposed to be there for doing high-level control of all these sophisticated components.
34:40 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.
34:50 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.
34:59 So you have lots of different options.
35:03 But there is, of course, going to be some kind of overhead in terms of the speed.
35:07 I mean, you can't escape that.
35:08 Yeah, no, that's totally reasonable.
35:10 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?
35:18 Ah, I see.
35:30 This episode is brought to you by Hired.
35:32 Hired is a two-sided, curated marketplace that connects the world's knowledge workers to the best opportunities.
35:39 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.
35:48 Typically, candidates receive five or more offers in just the first week, and there are no obligations ever.
35:54 Sounds pretty awesome, doesn't it?
35:56 Well, did I mention there's a signing bonus?
35:59 Everyone who accepts a job from Hired gets a $2,000 signing bonus.
36:02 And as Talk Python listeners, it gets way sweeter.
36:07 Use the link Hired.com slash Talk Python to me, and Hired will double the signing bonus to $4,000.
36:14 Opportunity's knocking.
36:16 Visit Hired.com slash Talk Python to me and answer the call.
36:20 On the same hardware, it's impossible to measure because C-Python won't run on a Pyboard.
36:36 That's really not working.
36:38 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.
36:51 Actually, MicroPython is slightly faster than C-Python for a reasonable range of benchmarks that we have.
37:01 For example, the Pystone benchmark, MicroPython runs a little bit faster than C-Python.
37:06 And that's because it uses less resources, and the way it handles integers is a bit more efficient.
37:11 So, you know, not having to allocate RAM to do certain things makes it faster.
37:16 But, of course, there are other things that C-Python is quicker at.
37:19 But, actually, on par, they're about on par when you take your own overall sort of benchmark.
37:27 Okay, that's very interesting.
37:29 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?
37:38 One thing this might be worth talking about is where is MicroPython used?
37:43 Are there some notable, I guess, I'm not sure if deployment's the right word, but products or devices with MicroPython?
37:50 Well, it's been used, I think, in quite a lot of places.
37:55 Some of them that I don't know about and some of them I do know about.
37:59 I think it's used a lot in robotics.
38:04 There's been a few other Kickstarters as well.
38:07 To do with MicroPython.
38:10 There was this Robocore Kickstarter where they made a little sort of robot brain that ran MicroPython so you could make robots.
38:17 And then the Wi-Fi, obviously, for Internet of Things.
38:20 It's been used in teaching.
38:23 There's some people that will be using it for teaching university classes because it's sort of a good mix of, you know,
38:31 in university course you don't have all this time to go into the details of getting cross-compilers working and all this stuff.
38:37 You just want to teach some basics about low-level interrupts and controlling an I2C bus.
38:43 And doing that in Python, yeah, it seems like a really great way because Python is so simple.
38:47 You totally understand what's going on and so it lets you focus on the concept, right?
38:52 Yeah, exactly.
38:52 And turn an LED on and off and sort of get some hands-on use of some hardware straight away.
39:00 So, I mean, in the UK here there's a big push for students at primary school even to start learning languages.
39:09 So, they have to learn two languages and mostly Python is one of the languages they learn.
39:15 And they also do sort of some hardware in their sort of design or science class.
39:21 So, if these two things go together, if MicroPython, because they already know Python and they need to do some sort of simple exercises,
39:30 it's a very easy path for them to take.
39:35 So, it's been used in education and I think it will grow a bit more.
39:40 One other very exciting thing is that the European Space Agency has actually expressed interest in using MicroPython for space-based applications.
39:50 That's awesome.
39:51 Because the hardware that runs in space is very limited in resources because it has to be proven that, you know,
40:00 it takes a long time to prove that a piece of hardware is actually run in space given its radiation hardening and so on.
40:09 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
40:21 so that it can be put in a space-based application.
40:26 So, there's more than, yeah, more than just the Internet of Things.
40:30 Yeah, that's, yeah, space.
40:32 That is really cool.
40:34 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.
40:45 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,
40:54 and something simpler like Python, it seems like a really big win for them.
40:58 So, that'd be amazing.
40:59 I mean, we look forward to the developments to see what will happen if it's happening.
41:03 It's very exciting.
41:04 Yeah.
41:05 Maybe some amazing picture of galaxies will be pulled down someday and it'll be, have taken, been taken by Python.
41:12 That'd be cool.
41:13 So, speaking of space and robots and things like that, another area where we've had people on the show,
41:23 and I know Python is used, is robot operating system, ROS and ROSPy.
41:27 Is there anything going on between those two projects?
41:32 With MicroPython, not that, no, we don't have direct communication with them yet.
41:40 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.
41:51 It doesn't run directly on a microcontroller.
41:53 Yeah, it's more like an i7 or something like that, right?
41:56 So, yeah, so you'd need, so, MicroPython could fit in the picture there if they wanted to run an efficient Python implementation.
42:03 So, instead of running CPython, they could run MicroPython, but it would be sort of the PC version of MicroPython.
42:11 It wouldn't be the Pyboard version because the ROS has already taken over the system and it is controlling it.
42:18 Yeah.
42:20 Okay, that's very cool.
42:21 Good to know.
42:22 But there are other real-time operating systems that have MicroPython sort of already built in.
42:33 So, NutX is one of them.
42:34 And Riot is another real-time operating system that we're working to get MicroPython into.
42:40 So, there's, yeah, these things are good, interesting avenues.
42:46 Yeah, that's really nice.
42:48 There's a lot of stuff going out there and the more you guys can work together, the better, I guess.
42:52 Yeah, well, I mean, with an open-source project, it is an open-source project.
42:57 It's got an MIT license and anyone is free to contribute.
43:01 I mean, yeah, it's not something that's going to move super quickly unless it has help from external or from the community.
43:11 Of course.
43:12 You also said that the hardware was open hardware.
43:16 I don't do much with hardware, so that doesn't mean anything to me, but it sounds cool.
43:19 Can you talk about that?
43:20 It just means that the schematics are available for people to download and they can make their own board.
43:26 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.
43:36 It was supposed to be simple enough that you could solder one by hand.
43:40 I mean, I made the first prototypes by hand just with a soldering iron.
43:45 So, it's not an overly complicated piece of hardware.
43:49 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.
43:57 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.
44:09 And MicroPython is sort of configurable enough that you can put it on your own chip.
44:14 That's really excellent.
44:15 And it runs on ARM chips, right?
44:18 Yes, it runs on ARM, but it also runs on other things as well.
44:22 It doesn't just have to be in ARM.
44:25 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.
44:41 And these architectures are all different.
44:43 So, it's not just an ARM architecture that it's relying on.
44:47 Oh, that's really cool.
44:48 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.
44:58 But you're talking about things like hardware and soldering irons and all sorts of stuff.
45:04 You must have an interesting place where you work or an interesting setup.
45:09 Yeah, well, I think it's a mess like everyone else has set up, right?
45:14 There's just lots of stuff laying around.
45:16 But yeah, you're right.
45:17 There's soldering irons and there's oscilloscopes and, well, there's big screen and keyboards and everything.
45:23 And then lots and lots of pieboards just lying everywhere.
45:26 How many revisions did you go through the pieboards?
45:29 Is what you're shipping kind of what you started with or is it really different?
45:33 No, it's the same controller and pretty much the same.
45:37 It started out with the same features.
45:40 Accelerometer, SD card, a few LEDs and switches.
45:44 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.
45:50 But otherwise, it's pretty much stayed the same.
45:53 I think this is the final version was the fifth revision.
45:57 Okay, excellent.
45:58 So if people want to get started working with MicroPython and the Pyboard, how do they do that?
46:05 Can they come and buy that from you?
46:07 Yes, they can.
46:08 They have different options.
46:10 They can buy it directly from us.
46:12 And it's, well, it's 23 pounds if you buy outside of the EU.
46:18 But if you're inside the EU, you've got to add 20% tax.
46:21 So it's 28.
46:22 We also have a distributor in America, Adafruit, who stocks them.
46:26 So you can buy it directly from them.
46:28 We have some also distributors in Australia and in Italy and Belgium.
46:33 And on the website, there are links to these places.
46:38 So you can buy from a few different places.
46:39 And, yeah, I mean, if you just want to try out the software, you can run it on your PC.
46:43 You can go to GitHub and download the code and compile it and see what it's like.
46:47 You can browse through the code.
46:49 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.
47:02 So there's a few different options.
47:04 If you go to the GitHub page, there's quite a lot of explanation there.
47:10 There's also the forum, which has a lot of people you can talk to there and ask questions.
47:16 And these are all linked from micropython.org.
47:18 Okay, excellent.
47:19 And I recommend just to see what's going on that you go to micropython.org and just play the video.
47:25 It's like nine minutes or something.
47:26 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.
47:32 Yes, great.
47:33 Yeah, and I'll put, of course, all that stuff, links to all those things in the show notes.
47:37 So, Damien, I think this might be a good place to kind of wrap up the show.
47:40 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.
47:50 And everybody kind of has their own favorite packages they work with and things they've discovered.
47:54 Do you have any favorites you want to give a shout-out to?
47:57 Favorite PyPI packages?
47:59 Well, there's a whole bunch of MicroPython PyPI packages.
48:04 Because MicroPython also uses PyPI and has a built-in package manager, MicroPip, so you can download lots of things from PyPI there.
48:16 But as for myself, in normal Python, wow, I mean, there's so many things that I use.
48:23 I mean, in my work in physics, I use a lot of scientific computing like NumPy, of course, and SciPy is very good.
48:31 Yeah, I don't know if I could narrow it down to one particular thing.
48:35 Yeah, it's kind of like a candy sword a little bit.
48:36 It's very nice.
48:37 A lot of stuff that looks good.
48:40 Awesome.
48:41 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?
48:47 Only that if they're interested, then please go to GitHub and take a look.
48:53 And there's lots of coding that can be done from, you know, beginner stuff to more advanced stuff.
49:00 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.
49:15 So there's lots of stuff that can be worked on.
49:19 And I think, I mean, for me, it's a lot of fun.
49:22 So maybe someone else would also find it fun to join in the coding experience.
49:28 Yeah, that's great.
49:28 And you guys have 63 contributors on GitHub.
49:32 So I'm guessing you're pretty open to having people jump in and help out and add to that.
49:36 Yeah, sure, sure.
49:37 We are very open.
49:39 Okay, great.
49:39 Damien, it's been a really good conversation.
49:41 I've really learned a lot.
49:42 Yeah, well, thanks for having me, Michael.
49:44 It's been great.
49:45 Yeah, you bet.
49:46 Talk to you later.
49:47 Okay, thank you.
49:48 Bye.
49:49 Bye.
49:49 This has been another episode of Talk Python to Me.
49:53 Today's guest was Damien George.
49:55 And this episode has been sponsored by CodeChip and Hired.
49:58 Thank you guys for supporting the show.
49:59 Check out CodeChip at CodeChip.com and thank them on Twitter via at CodeChip.
50:04 Don't forget the discount code for listeners.
50:06 It's easy.
50:07 Talk Python.
50:08 All caps.
50:09 No spaces.
50:10 Hired wants to help you find your next big thing.
50:13 Visit Hired.com slash Talk Python To Me to get five or more offers with salary and equity
50:18 presented right up front and a special lister signing bonus of $4,000.
50:22 You'll find the links from today's show at Talk Python To Me.com slash episodes slash show slash 17.
50:29 Be sure to subscribe to the show.
50:32 Open your favorite podcatcher and search for Python.
50:35 We should be right at the top.
50:36 You can also find the iTunes and direct RSS feed links in the footer of the website.
50:42 Our theme music is the song Developers, Developers, Developers by Corey Smith, who goes by Smix.
50:47 You can hear the entire song on our website.
50:49 This is your host, Michael Kennedy.
50:52 Thanks for listening.
50:53 Smix, take us out of here.
50:56 Staying with my voice.
50:57 There's no norm that I can fill within.
50:59 Haven't been sleeping.
51:00 I've been using lots of rest.
51:02 I'll pass the mic back to who rocked it best.
51:04 I'm first developers.
51:06 I'm first developers.
51:13 Developers, developers, developers, developers.