 Welcome to from hardware simulation to real devices with web assembly using tiny go an entirely too long of a title I'd like to take one minute of my brief time to thank you organizers you Participants the sponsors and all the people involved with this for bringing me back into the real world. It's amazing I did not was still here So I am dead program Some humans call me dad others call me Ron But the important thing is I am dead program in the internets that matter Technologists for hire. There's my title. I was told to be very subtle. So I'm trying that out. I Have a consultancy called the hybrid group where we are all Technologists for hire and we write the software for hardware companies some of which you may have heard of But today I'm here to talk about tiny go on the amazing project that we've been involved with since its inception and Tiny go is a go compiler for small places small like really tiny places for example microcontrollers like Arduino's and Web assembly, which is of course why we're all here So a good size comparison is always good. So let's start with is anybody here never programmed in go Okay, well, we're going to start with a hello world which is package main Which says this is our main package our main unit of code our function main Which is our main function and then we're going to print line hello world and that's it so if we go over here to my terminal and We go to Make hello Let's go make hello run. So this is going to run that same program both in Regular standard go and in tiny go and it's got the same output hello world Okay, that's good So now let's say hello build which is going to actually build them both into binary executables not just run them and then make hello size So the regular go that somewhat inconsequential program is one point one megabytes Whereas the exact same tiny go program is 67k Thank you. Good night. No Wait, how do you do that? Is that even possible? Yes. Yes, it is possible So, but what about in the real world? I mean who's using this? That's neat. You just showed me a toy program dead program. How this what what it can it really do? Well first of all Fermion or fermion as I've discovered they're actually called I live in Spain so Great blog post that just came out about shrinking your tiny go web assembly modules that shrink and shrink and keep shrinking until it Just disappears to some sort of quantum superposition So cool blog post just came out They're doing a lot of really cool stuff and actually they're going to be giving a talk at the end of the day Which I'm really looking forward to So Suborbital very cool company one of the sponsors They've been doing all kinds of interesting things a recent blog post that just came out from Connor Hicks talking about a simple Serverless system that's written and of course it's got built-in tiny go support into their command line tools Then you've been able to use tiny go in envoy and Istio for quite some time This proxy web assembly for proxies project, you'll know this this very small type seems appropriate This SDK is powered by tiny go and does not support the official go compiler So it's smaller and yet somehow better in this context Some people are actually running web assembly on physical hardware devices, which I bet you thought is what this talk was about But it's not So they can actually compile the web assembly into a binary that could be run right on the hardware itself There's some books about tiny go Tobias teal Contributor to tiny go and awesome cool human being has written an entire book about creative DIY microcontroller projects with tiny go and Web assembly, I think I see a pattern forming and then a recent book that I just acquired myself wasm cooking with Golang from Philippe who I think might even be here although we've never met very cool book. Thank you for that All right, but this is not an ad for tiny go I mean if you haven't been convinced yet you probably hopefully are but this is actually a talk about simulating hardware using web assembly So the way that we do that currently is with the tiny go playground So if you haven't seen it the tiny go playground is an actual website on the internets at play dot tiny go dot org and In the grand tradition of playgrounds. It lets you play with go code or tiny go code They're on the internets and so we can see our exact same program I'll just recompile it by getting rid of the exclamation and it's compiling it and It will take a little minute because it's actually doing it all in the cloud and so there's my there's my terminal output All right Very cool. So but what about that hardware? So a lot of people have the Arduino Uno hanging around in the desk drawer Or maybe quite a few of them and so one of the things that we can do is we can actually simulate the hardware itself So if I go to this Arduino blink program, are we doing on time? Okay, we got to keep going So you'll see here. It's got a package main It's importing the machine package, which is a special package used by tiny go for the hardware abstraction layer So we can actually talk directly to the physical device itself We've defined our LED, which is the built-in LED on most boards and then our function main We configure the LED for output and then in our forever loop We say LED dot low that turn it off wait for a second then LED high that turn it on and Sleep for a second and you could see on this little rendering here in SVG an Arduino Uno board with the thing flashing on it So that's very interesting. We but but tell me about other boards So what are we even seeing? What is this? What is this madness? So when we're running tiny go on hardware We're actually running a native binary and so when we compile it using tiny go We compile all of the packages that we would normally be using and we use the machine package As I was showing you to talk directly to the hardware registers were running bare metal on the physical hardware And so when we would compile it We would use the normal tiny go compiler and say build with our target of Arduino and our output being a hex file Which is a standard binary format for flashing firmware But if we're running using web assembly in our playground, it's very similar It's still the same compiler But instead we're using a JavaScript interface to a web assembly module that we just compiled and we're executing That simulator as a web worker in our browser And we use the same tiny go build command the difference here is that our output is now a wasm binary But it's otherwise. It's exactly the same code So let's take a quick look at an Arduino or sorry an a the fruit circuit playground express board Which is actually the same board that I have on Go for bot here, and we can take a quick look at how it actually looks It's a board just like this kind of circular And if we go back to our code here, we can see that it's blinking in exactly the same way It's exactly the same code. I did not change any of the code We're using the same hardware abstraction layer that tiny go provides for this low-level hardware And it's able to blink the built-in LED on any of you know, 70 some odd different boards All right, so but what if we want to actually put that on to some physical hardware? Well, by the way, just a quick how this is working your program is being compiled Using Google Cloud Run in the form of a serverless function through a tiny go running in a container And then that can be either compiling it to web assembly as we saw or it can actually Program and compile the native binary. So if I click on this flash button It'll actually compile it in the cloud and then it'll download it So if we go over to Hopefully I will find it Let's see if it made it It's waiting. There's my firmware dot UF to which is a universal format for firmware develop cooperatively with Microsoft Which lets you just flash different boards and if we go back to I'm going to Double tap my board here so I can put it in the bootloader mode And then it'll pop up just as a normal mass storage device and I'll drag my firmware onto it and it will flash it and Then you can see That it's actually blinking the LED on the board Amazing we actually just flashed the board from the internet using web assembly built compiled all the way down to the physical board itself It is real So let's do something a little bit fancier We have support for many different hardware devices that are not just simple LEDs for example RGB LEDs Yeah, we need those so if I look at the circuit playground Express It actually has a bunch of built-in LEDs. You probably saw on gopher bots back And so this program is very very similar It uses the standard image color package the machine package We saw before the time package and then the WS 28 12 or neopixel driver package Which is some external package that we have for talking to all of the different 70 some odd displays Sensors and other devices that physical actual devices and so in this case We are configuring this WS 28 12 device and we're changing the LEDs between the red and blue All right, we put those into the LED display and then we every hundred milliseconds We rotate and so you can actually see it here. Well, let's do the same thing now, but let's actually program the board So I'm going to download the firmware I'm going to jump over here We'll put it back in the bootloader mode and then we'll drag the newest firmware onto it And you can see that Yes, now we're actually running that new firmware with so we can do a lot of really interesting things and it's all there Allowing you to simulate It works amazing. You can applaud now All right, so if you want to know more about how this is built check out the recent blog post from my colleague I cave on lotum the original creator of tiny go tiny go preview. How does it work? That's on this URL Check us out at tiny go dot or g. You don't need hardware anymore the hack hardware. You could just do it with the playground Check us out on Twitter tiny go lang and I thank you very much a wet one minute over Sorry, no questions. See me at the cocktail reception. I'll give you stickers and Tell you everything you want to know and more Thank you