 It is my pleasure to introduce you to the very first talk of EMF 2016, which is Nicholas Tolawy talking about MicroPyphen. With the talk, some of theEMF schedules do overlap so when we get to the point where the main talk is finished, if you do need to run to the next stage to catch whatever's going on there He is not going to cry. Felly weithio cael yw soedd, rydw i'n rwy'n ein bod ni'n ffordd ni'n cwysig yng Nghymru, yr ychydigion Wyllfa Fawr Fawr Fawr, rydw i'n ymddangos, rydw i'n ymddangos. Rhyw ddim ond, rydyn ni'n gweithio. A rydych chi'n credu allan i'r rheimhau, rydw i'n credu allan i'r rhwng bod rydw i'n credu llunio'r rhwng. Rydw i'n credu allan i'r rhwng, beth sydd wedi'i'n credu allan i'r rhwng, ddim yn ei gynllenio i'r ddechrau. Gwnaeth eich cyfnod yw'r gwybodaeth gwybodaeth, a'r ddweud y ddweud o'r ddwyngol. Mae'n gwybodaeth nrhyw gyda'n ddweud. Efallai eich tynnu ar gyfer python ac efallai efallai efallai efallai efallai micro python fel yw'r holl? Mae yna yn wahanol ond? Felly mae'n gweithio'r holl byddwch chi'n byw. Efallai efallai? Mae micro python efallai efallai implementaeth python 3, for microcontrollers, small systems on a chip. What does it do? Well, it brings the power and beauty of the Python programming language to highly constrained computing environments. So things like the BBC micro bit, Arduino shaped devices and the EMF badge this year. And talking of that, why is it suddenly everywhere? Well, it's become popular in the last year or so because it facilitates simple yet rapid development on embedded devices. It has all the powerful features of Python that you would expect. Sometimes, depending on your device, it might have more or less of the standard library that comes with Python. Importantly, it has a transferable skill set. So I work in Python during the day working, building large scalable websites but I can use the skills that I know as a Python developer on microcontrollers and embedded devices which makes it rather a fun thing to do. And also, this being Python and MicroPython, it has an outstanding community. So, that's it, really. Actually, as I mentioned, MicroPython, it's all over EMF because all the badges that you are going to get run a build of MicroPython. MicroPython is actually all over the UK as well. Now, I can't see an awful lot because I've got lights in my eyes. But could you put your hands up if you've heard of the BBC Microbit device? Okay, that's pretty much everybody, which is what I was expecting from EMF. So, you would be unsurprised then if I told you that a million of these devices have been delivered to the UK's year sevens, so that's the year when you're turning from 11 to 12. And this was a partnership created by the BBC. One of the partners was the Python Software Foundation. I'm a PSF fellow, and so I was the person who helped organise for the PSF the collaboration with the BBC. And all the work that I'm going to show you on the Microbits today for MicroPython has been done by volunteers here in the UK and around the world. So, nobody's paid people to do this. So, you could also use software from one of our other partners. So, Microsoft, if you go to their tent, they have an alternative programming environment that you could use on the Microbit as well. MicroPython is all over the world. So, Damien George, the creator of MicroPython, is Australian, and he ran a successful Kickstarter, which I'll talk about later on. But the Pie Board, he tells me that he gets fan mail from children in Saudi Arabia who are using the Pie Board and MicroPython in their lessons in the US. It's kind of all over the place. And this is the thing that I really like, because I'm a bit of a space nut, is that MicroPython is going into space. I'll talk a little bit about that as well soon. But I think that's really cool, is that I could be writing in language that I could use to programme satellites in a few years time if this project comes to fruition. So, this dapper looking chap is Damien George. He's a physicist who was, until two weeks ago, based in Cambridge, here in the UK. And for a bit of fun, he created MicroPython. Why wouldn't you want to re-implement Python 3 in your spare time? But that's what he did, and it's become this hugely successful project. So, how does MicroPython work? So, I'm not going to give a very technical explanation of this because you can find out the technical details online. But this diagram essentially shows you the flow of what MicroPython is doing. If you start at the red boxes on the left-hand side, I'm not sure if you can see it that well, but it says repel prompt, user script, and compile strings and things. That essentially represents Python code, as we would see it in our editor. That goes through several steps to get to the compiler. So, the lexer will tokenise the Python script. The tokens are then sent through the parser, so you get the parse tree, which is what the compiler uses to turn into something that can be executed on the device. Now, for the microbit, we have a virtual machine. So, it's compiled to bytecode. The bytecode is loaded into the virtual machine and it is executed on the device. I believe that's the same for the EMF badge. So, the virtual machine executes bytecode and stuff happens. You have native code as well, so MicroPython can compile to whatever executable you might need for the particular device that you are programming. And there's also something called ViperCode, which I have absolutely no idea what it is, because I've never been involved in that part of the MicroPython project. But it sounds cool, called Viper. Anyway, that's how... Actually, I know a little bit about what it is, but ask me later. That's essentially how MicroPython works. It's a very, very simple system. The important thing that you need to know is if you're thinking, ah, right, OK, well, Nicholas hasn't really told me that much about it. I know what I'll do. I'll go to the MicroPython.org website, find where that GitHub repository and see what I can find out about this code. Some things that you need to know. Because MicroPython is designed to work in highly constrained computing environments. It does not follow traditional software engineering practices that I might use when I'm writing great big websites, for example, in Python. So basically Damian optimises first. He uses lots of creative solutions and tricks. If he can, everything's about size, so everything has to be as small as it can. So Clarity, which is something that developers, especially Python people, like to trump as a benefit of the language. In the C code, the implementation of MicroPython, we're looking for smaller code. Smaller code, more smaller code. Goto is not discouraged in the C code. Programmers will understand that's rather a contentious issue, but in the case of MicroPython, it gives us some efficiency here. Because we don't have a lot of RAM, everything's optimised for not a lot of RAM. Minimize the stack usage. All the decisions that Damian makes about these optimisations that I've flung through with a wavy type of description are based on analysis. So he's using analysis to work out where he can make the best optimisations given the particular platform for which MicroPython is being built for. So, here's Damian again, and he's always asked the same sorts of questions, which is, why do you do it this way rather than that way? Why do you do it this way rather than that way? Why are you doing GoTo rather than blah blah blah blah? Why blah blah blah blah blah? Essentially, it all boils down to one essential factor that Damian is concerned about, and that is to minimise RAM usage because we are working in a constrained environment. If you are interested, and I encourage you to have a look, because it's a very friendly community, it's MicroPython.org. MicroPython supports lots of different devices, not just the micro-bits, or the pie board, or the EMF badge. There's lots of people doing crazy stuff with lots of interesting embedded devices. MicroPython.org is the place to go. So, why would you want to re-implement Python 3 for microcontrollers? So, I asked Damian this a couple of days ago when I was exchanging emails with him, and he said, well, you know, because it was there, really, it was a fun thing to do, and he wanted to see whether he could do it. So, he's done it. Congratulations to Damian. Also, Damian loves writing in Python, and he wanted to make it easy for people to programme using Python in such constrained environments. So, this goes back to my point about having a skill set that's easily transferable. So, how did MicroPython come into being? Well, Damian sat down and he figured out, well, could this work after a spike of code? Yes, it could. He put his proposal on Kickstarter, and almost 2,000 backers pledged almost 100,000 pounds for him to complete the work, and they got a little pie board, which I don't have on me right now, a very small device which they could then use to play around with MicroPython. About nine months ago, Damian was a second Kickstarter for converting for a conversion of MicroPython to run on the ESP8266, which is a system on a chip that has built-in Wi-Fi. And it works, which is wonderful. He had almost 1,400 backers who pledged almost 30,000 pounds for this to happen. So, Damian is working full-time on MicroPython on this. And, of course, there's the BBC Microbit, so this is how I know Damian, because my involvement in MicroPython has been through the Microbit. One day we were sat in London having a cup of coffee, wondering how we would describe to the BBC what our vision of MicroPython on the Microbit would be, and we literally sketched it out on the back of the table mat and took an arty-farty photo of it, really, and that's what we showed them when we went into the meeting. The international team of volunteers have been involved in the Microbit. The million children obviously get these devices and they can learn to programme with them. And the international team of volunteers, many Pythonistas, many people who, for the first time, have started to work in an embedded environment like myself. It's been an interesting sort of an adventure. And they've been doing some crazy stuff. So this is a robot built by a friend of mine called Radamiin who is a Polish chap based in Switzerland. And he essentially well, by the looks of it, got some motors and nailed them together with a Microbit. And this is what the result was. Kind of cute and all humans must die sort of a way really, isn't it? I think he does it again. Here we go. Very, very silly. The important thing is about the Microbit is that we want to inspire creativity. And with silly robots like this, we hope that kids, things that kids could make in their CDT class, remember this is just some motors nailed to get, well, glued perhaps, together with a Microbit and some very, very simple Python. I think it's only about 15 or 16 lines of Python code that gets you to here, okay? So this is the bit that I've been working towards, which is the live demo. So I believe tomorrow you get your badges. So here's a sneak peek. Yes, it works. Here we go. So here's the badge. It's running MicroPython. And what I'm going to do is give you, whoops, a very simple outline of what you need to do to get in touch with Python on this device so you can start hacking with it. So you, first of all, plug it in, which is easier said than done when you're live on stage at EMF camp. There we go. And if I go to my REPL here, I should be able to... That's interesting. I obviously didn't sacrifice enough chickens to the demo God this morning. Let's see what I can see. Okay, so we have the board. I can do it this way. So when you plug in your badge, it will be mounted as a flash storage device. And there's nothing on there. There we go. And what the device will do is it will attempt to look for a file called main.py in the root directory here of the file system and it will attempt to run whatever Python it finds in there. Now, the badge team here have told me and another caveat here is that I only got this badge about two hours ago and have spent about 15 minutes playing with it. So there's live demos for you. What it does, it actually runs a boot.py file if it also finds it and the main file that it runs is going to be in home main.py, which is this beautiful piece of Python code. But what I can do, and this is without the aid of a safety net, is if I rename this to main.py2 and I rename this to main.py and then I unmount the device and then I restart it. It works. So I have created a very simple spirit level in MicroPython. That's all well and good but there is a REPL that you should be able to connect to. So we have a REPL. So plug your device in you screen or something to connect to it I have no idea what you have to do if you're on Windows but anyway, here we are this is Python so I can see what's in my scope there's even help built in just like regular Python I can print hello world there we go it's just regular Python so please have a poke around you can live code the device as well so there are several libraries for the screen that lets you draw all sorts of interesting things on them so I would use the REPL to experiment and work out how it works put it into a script and then see what happens back to the presentation so that's the batch that you're going to get the thing that I've been involved with is the micro bit this is the micro bit how many of you have actually seen one and held it and caressed it there's about a third of you so let me tell you what a micro bit does it runs micro Python obviously on the front there are two buttons button A and B and there's a 5x5 LED matrix that scrolls bright rays should you make it do so across the bottom are GPIO pins some of which are big enough for you to attach crocodile clips so it's very makey makey as it were on the back the parts are labeled so we have over here is a power socket we have a reset button here a micro USB connector here here is an antenna for BLE related things we have the arm chip is here and an accelerometer and a compass as well so there's rather an awful lot on the device so as part of the project to work with the BBC they asked us to create a web based editor which fits in with the micro bit.co.uk website one of the things that we did in the autumn after we created this editor is we went around the country and spoke at various teach meets and Python user groups and demo this to get feedback from teachers because it's the teachers who are going to be providing the support for the device and the feedback we got was that actually they preferred using a real editor rather than using a browser based editor so over the Christmas holidays basically we wrote a native app which we call Mu one of the features that teachers said was you know what when you're giving presentations the people at the back can never see what it is so we would like zoom in and zoom out but I can demonstrate this feature for you now here it's awesome look and unlike the other sorts of platforms where you have to do it through a browser you have to download a hex file you have to drag the hex file over to the mounted file system it flashes blah blah blah blah because this is a native app we can do all sorts of interesting things like if I want to flash this very very simple piece of Python that all it does let me zoom all it does is zoom scroll hello world across the screen I just click the flash button and it should scroll hello world let's see what happens flashing the device still here we go hello world woo it works just fancy that ok that's not all it can do for example we've built in animation too so let me flash this what we have endeavoured to do with the APIs that we have created there we go it's a little clock or a radar or something like that is make it as simple and as easy as possible for kids who are using this device to get to something usable so this is in essence apart from the import a single line of Python and it's rather easy to read show on the display all the clocks in the image library delay for 100 milliseconds between each of the files and I want you to keep looping that for ever and ever and ever and so we get our little radar something a bit more complicated here we've built in a whole bunch of images kids can create their own images it's very simple to do with Python but I put the device in an infinite loop and I'm going to make this thing sparkle and react to button presses and things so the sparkling happens here if button A was pressed show a random image show a random image if button B was pressed scroll hello world and if the accelerometer had a shake gesture just show an angry micro bit let's see if this works live coding demos aren't they great so it's sparkling and if I press button A sorry button B we get hello world and if I press button A there it is going back sparkling we get a picture of a rabbit that disappears into the sparkles an umbrella or sun shade depending on what the weather's day a heart all done at random a skull and if I shake it you get an angry micro bit there it is pissed off micro bit there we go if you remember I said that you could get to the REPL on the on the EMF badge well you can do the same thing zoom in a bit let me not do that excuse me REPL zoom in there we go so what you can see here what you will see here live demos here we go this is micro python running on a micro bit and I used to be a music teacher so one of the things that we've implemented is a music module and I have a speaker here and if I plug it in it should just work turn the volume up there we go right then so I can do something like this music.play this is rather obvious we want kids to be able to discover this sort of stuff and understand how the API works so we want to play music what you use you use music.play music.tab completion it's got some built-in tunes which are used for demonstration purposes so somebody shout out a tune from the list there birthday what's that going to be it's going to be like being in WH Smith with all those cards that you weren't going to close let's see look at that and I can also make it do other interesting things music.pitch so I used to be a musician 440 is the standard for concert A that the oboe player will give when it walks to tunes I wanted to play for one second and now what I can do is some interesting stuff for example putting it into a loop and I can say music.pitch pitch takes an integer where can I get these integers from how about accelerometer.getX and then wait for 20 milliseconds and let's see what happens strangled cat or musical instrument so imagine imagine a room full of 11 year old children on a Friday afternoon and it's raining and their teacher brings this out it's going to be chaos and mayhem but they're going to have a lovely time so I'm going to reset things a little bit now and hopefully I can make this work properly like I said not enough chickens for the demo gods oh well I've lost the ripple anyway what we've done is we've built in a speech synthesizer I found some a Commodore 64 based speech synthesizer and it was written in C and so we've made it work and maybe the ripple there yes it is so I can do something like this I can so you can see what I'm doing I import speech everyone see that speech whoops why is it you can never spell or type when you're doing a demo speech.say again very very obvious hello F I have no idea if this will work can you imagine the fun the kids are going to have with this with micro python so let me flash this on to the device and essentially it's a recreation of the sound of music we're going to get it's singing I hope singing micro bits it's sort of like the Daleks greatest hits really so the other thing that I mentioned is that it had an antenna so you can have into device communication going on I just happen in blue Peter fashion to have one I prepared earlier second micro bit with a battery pack and it's been programmed with the script that you see in front of you now it's very simple I import the radio module I switch the radio on I clear the display I put it into an infinite loop and if it receives a message and the message is hello it displays a happy face that code that I've just described to you almost reads I guess like English this device however I'm going to flash with radio hello again I import the radio module I switch the radio on while true if button A was pressed send the hello signal and if everything lines up this little fella should show a happy face it's still flashing for me on to the device okay let's see three two one hey it works okay so you can send arbitrary messages over the radio the important thing is that kids again we try to create a very simple API kids are going to be inspired by this so back to my talk that was the live demo who I made it so I only just got my EMF badge about two hours ago and so all you saw was what I managed to figure out in about 15 minutes I hope you explore what the device can do because it's a lot more powerful than the micro bit if you're interested in the hardware that went into the badge there's a link there for the software there's a github repo as well again on the slides there micro python documentation is docs.micropython.org forum.micropython.org is where you should be asking questions as well please experiment and play okay what about the future for micro python well Damien tells me he wants to improve the existing offering so there's an awful lot of work going on for tutorials and new websites and things like that so that micro python actually starts to look more professional and so on not that it's not professional at the moment you can actually use micro python on a real operating system so micro python runs on the bare metal at the moment but there's no reason why you can't use it in other situations where you have a constrained computing environment like perhaps in a game or something like that I don't know you could program a block in Minecraft with micro python maybe it's fully buzzword compliant in that a lot of internet of things people have been using micro python recently and Damien recently got an email explained that somebody who would have spent weeks doing such and such a thing managed to do this thing in just an afternoon thanks to micro python so hopefully this is a bit of a game changer in terms of rapid application development for internet of things appliances so like I said you should be able to embed micro python within other software as well like games and places where you need scripting and like I mentioned at the beginning Damien has been working with the European Space Agency on a satellite control layer that's written in micro python so literally you should be able to program satellites in python how cool is that and perhaps one of the most important aspects of micro python is the legacy that it'll have so this was me having a discussion with some friends of mine on Facebook last year and they wanted to know why on earth is all this effort being put into programming education and why is micro python involved in this and I answered like this and I'll read it out doing a bit of a powerpoint karaoke for you asking what sort of education and learning our community support is how we decide what sort of community we become for it is through education and learning that we engage with our future colleagues friends and supporters if we want there to be an EMF camp in ten years time we need to help the young programmers of today to build and get into the making movement and all those other good things we have about five minutes left of this slot at this point I'm going to take questions I'd like you to put your hand up and the gentleman over there with a microphone will run over and ask a question and you can ask a question into the mic we have about five minutes and there's somebody at the back there always at the back why are they always at the back hi thank you for the talk very interesting the radio send is that a serialisable python object or is it limiting what it can send and distribute okay so you can send arbitrary bytes so you could do Repra for example to serialise the object you could do what you want the point is that it's just bytes that's being sent over the air fantastic I'm saying thumbs up any more questions if you want to go to see the next talk don't feel free don't feel that you have to wait here for me to finish I think we'll call it a day thank you very much