 Okay Welcome everybody. I'm Radomilio Piralski, and I'm going to talk about making computer games with micropyton I Have to start with a disclaimer. So I'm not getting paid for this. This is not my job All my opinions are my own opinions not of my employer or any organization I might be a part of or anything like that I didn't do however. I didn't do all of this myself I did receive a lot of help from a lot of people mostly from the micropyton community from the adafruit community from You know Comparise like adafruit or Osh Park and so on. So that's not just my own work. I'm showing here This is not an advertisement of any of those companies and they don't pay me for advertising them Just just to make it clear. However, I do sell my stuff on Tindy This is like Etsy for electronics, and I'm not also I Well, obviously I'm showing my stuff here, but it's not with the intention to advertise it for selling I'm not earning money from those sites. So it's just to make it available for people out there I do receive free stuff from from various electronics companies to try because they like the things I do and they would like me to write reviews or whatever. I'm not advertising them here and Yes, and I'm I do realize that I'm extremely privileged to be able to do all this to be able to afford To be able to have free time for that and that note everyone who could do that can do that. So I'm I Do try to make it easier for others to start with this so That out of the way we can talk about the topic so the the main goal the main idea in here is to take one of those Development boards that you can buy now like maybe Arduino. Maybe something else connect one of those trip display modules you can get for them and a bunch of buttons and make a game console out of that Preferably handheld game console because I don't own a TV. So that complicates regular game consoles and Obviously the easiest way to do this is to just take a Raspberry Pi install like Retro pi on it and and you're at home However, there are a number of Downsides that I don't like about this approach. I obviously I have tried that I did a handheld game console with a Raspberry Pi on it and The problem the first problem is that battery life is not so great because Raspberry Pi is a full Linux computer basically it it takes quite a lot of of power The boot time is not that great either because it takes even several minutes To boot depending on how it is configured and so on You get a lot of games you can play because there are a lot of game emulators on that platform However, most of the games you play are actually pirated ROM files Sure, it's abandon where and so on but what I wanted to do is to my to write my own games And that's kind of difficult when you have such a huge pile of Much better games that you can play instead So I wanted a platform that doesn't yet have any games on it so I can make my own games for it Next thing is it's very easy to break it. There are a lot of files in there There is an SD card that doesn't like when you shut down the Raspberry Pi forcefully And so on it's very easy to corrupt the file system. So I didn't like that It's not that easy to program games in Python in the first place because the libraries are not that great There is pi game, which is basically like 90s style pixel-pushing library there is a Piglet or or there is a pi OpenGL for 3d, but it's not really very convenient to use to install and use and Finally, you know doing all this stuff Basically everything is already done for you You just have to install and configure things and that's not what's Interesting to me. I like to program and I like to build things So I really feel like you know this bearded Unix guy who just configures everything and and it feels more like system administrator work than than a programmer work and Finally, even though Raspberry Pi is extremely cheap Once you have to buy all this stuff for it the display that the SD card everything It comes out. It sums up to quite a bit of money to actually do it So it's too expensive to do it say as a workshop with 20 people Where everyone has their own So Micro Python what is micro Python micro Python is an implementation of language similar to Python Basically with with the same syntax as Python, but a little bit different internals That runs directly on microcontrollers So no operating system. It's like basic on ZX spectrum or on Commodore 664 Basically your program owns the the platform. There is no operating system. You can do anything and The first platform for which micro Python was released was a custom Board called pi board There is a table in the first building where the hard or the hardware is where they are showing the Pi board and and the new version of by board and so on and that was an SDM city to base Board with quite a lot of Computing power and memory However, it was quite expensive at least at the time when I was starting with this that was too expensive for me I I did buy one, but I still have it in my drawer because it was too precious to actually use in any project So Sometime later Came out this Kickstarter for for macro Python to actually port it to this very cheap Chinese ESP 8266 Platform that there are a lot of development boards with that and that really Encouraged me to actually try and and start making my dream game console using Python and Yeah, so what I need it. I I needed all this stuff that I I listed here I will come back to this slide as I explained it. So first SPI HSP I so hardware SPI What's SPI SPI is like a serial protocol that's commonly used in electronics to communicate between two different chips It's similar to I2C, but it's usually much faster and the main idea is that you have a data pin and you have a clock pin and You send your you set the value that you want to send on the data pin and then you wiggle like change The the clock pin to tell the other side to at this moment you can read the value of the data pin If you didn't have this clock pin, they wouldn't know how fast they should read Like when you had three zeros in a row They wouldn't know if it's three zeros or one zero or two zeros. So when you have a clock pin They know that every time the clock changes. They have to read one new value for from it and There's some additional pins in there to to tell them that now you should listen that this is data and not command or command And no data and so on All in all in all you need about four Data pins to communicate with the other device You would need five if you needed Communication in the other direction as well. However, we are only going to send data to the display We are not going to listen what what display is telling us so we only need four and of course we need power that is not Included in here. So the thing is many microcontrollers have already this protocol implemented in hardware They have a separate peripheral on them that lets you talk this protocol very fast like in case of the ESP 266 It's 80 megahertz The clock rate is 80 megahertz. That means you can send 80,000 no 80 million beats per second Which is Quite nice, especially if you are trying to play graphics on like display display High frame rate video the problem is ESP 8 to 6 6 that port that was just Being made back then didn't have support for But the chip has the peripheral but micropyton didn't have support for this And I really needed it So I said shut down and and looked at the node MCU which is a Lua based firmware for the same platform That had support for it and basically I store all the C codes from from there copied it to micropyton And and got it to work which is For me quite an achievement because it was the first C Project I did in like 20 years after the University. So It was an adventure But yeah, we have working communication with a display. That's great Next things you need buttons The the common modules you can buy with for the ESP 8 to 6 6 only have a few GPIO pins broken out So you can only connect very few things to it and as you saw you already need four pins for the display So you only are left with a very few pins for the buttons So I made this breakout board that only had buttons on one side and on the other side. It has a very small microcontroller that takes the buttons all the time and Communicates with the main microcontroller through I to C protocol With only needs two pins the clock and the data so you can read the status of the buttons quite easily This is not well. There are several other ways you could do buttons On a device like that. This is just one of the solution I made and I also included Sockets for the display module on it for convenience so you can just plug the display module in there There is a slight problem You can see it it's clashes with one of the of the buttons But you can just cut out that corner and it works. There is no there are no traces on that corner So you can just adjust it Okay, next thing Sorry Moving I decided I will not play with sound at that moment This is just a prototype sound is too complicated. We can we can skip that next thing is the frame buffer so The way those displays Represent pixels on them. They have several modes So you are probably familiar with that when a four-bit mode of color where you have three bytes for the color and Each byte is one of the components red green and blue So this is what you use when you are doing web development and you specify the color in hex basically so This display has that however we want to be able to send our image data in as little bits as possible So we we instead use the 16-bit color which is called 565 RGB Because you have five bits for red six bits for green and five bits for blue The reason why green gets one more bit is because we have it and because people are sensitive to green cores more So so we get better quality this way Okay, so the next Thing is the frame buff so most actually all Libraries that work on a micropyton right now for displays work in in this way that they keep in in the memory of the microcontroller they keep a copy of the image on the display and What you do is just do you just use SPI that right to send all of that at once to the display every time you Change something this has an advantage that if you if you modify several pixels and they overlap You don't send several modifications to the display on the other hand. This is a lot of RAM So the the microcontrollers don't really have a lot of RAM so you have you want to save as much RAM as as possible and Yeah, and you are sending the whole frame every time so that's 62 kilobytes Of RAM if your display is just like 128 by 100 128 That's too too too. That's too big for us. Basically So what we can do we can use a palette which basically means we have an index of two bytes per color But in our image we only use four bits for every pixel and then we can use 16 colors for every every pixels and just replace it With with the color from the palette when we are displaying it The problem is the the micropitons frame booth Module that we have here doesn't have support for that we are It also doesn't have support for transparency which will become Important for us in a moment. So what we want to do instead So one thing is that we want to use a palette for our images The second thing is we don't want to keep in the memory all the pixels of our display Instead, we only want to have a tile map. So basically we will keep all the actual graphics in an additional memory in the flash memory on the on the chip which is programmed when we flash our program to it and We will only keep like an array of Squares on the display and each square can display like 16 by 16 image on it Most computer game most older computer games actually do the maps in that way so that that's compatible with what we want to do and This lets us save a lot of memory because we only store one byte per or even four bits per a big square of the display and all the graphics are actually saved on the flash memory which we have a lot of and When we do when we actually send the data to the display We just make a small buffer the size of one of those tiles We fill it with already converted colors and so on and we only send that Small area we only update the area that actually changed Next thing is we also want to have sprites So we want to have small pieces of graphics that can move around the screen independently of the tiles and Unless we only want to make games like chess or something like that You know where you don't need that but when you actually want to like interactive games where you move around You need those sprites which are representing like mass missiles or enemies or the plier character and so on so the way you do it you you you only store information about their position in your memory and Once you make this buffer buffer to send to the Display you just draw on top of that the graphics From your flash to show the the sprite and here you need transparency because most of the sprites are not really square so you need They have different shapes So you need Transparency and once you have transparency you can also have several layers of the tile map So you can have like parallax effect or you can have like trees on top of ground and and things like that So this is a tech demo of of this actually working and You can the red balls Sprites that the checkered background are ties and the text are actually also tiles so That's basically the overview of what I wanted to do the problem. I had is the development of Micro Python was at the time focusing on other things and while I did get Some of the features that I needed in there. Those are the ones that have check marks There is a lot of things. I'm missing in there So I decided, okay, let's let's stop with that way. Let's get some You know Vacation from that and let's try something simpler So I started to Work with circuit Python circuit Python is a fork of micro Python that takes the base That is micro Python, but tries to make it more friendly for beginners Basically, it's made by other fruit who sells Electronic modules for beginners and they really want to have it beginner friendly So what one thing when it started at least and right now it's also true I think they are very friendly for for, you know Contributors so it was very easy to get all this stuff. I needed in there But I started with something simpler. It looks like this and I can actually show you a working version of that So it looks like this Where is the switch here is the switch, okay, it's too bright It's a shame you can't see it because it's too bright No matter and now I need to switch back to my presentation Okay, so as you can see I made a lot of prototypes The idea is that you have a device that you connect to your computer. It doesn't require any drivers It just shows as a USB drive You can see that all the files in there. You can just edit them And you have the interactive console, of course as well. I started making them as shields for the other fruit things and Later because that was too expensive. I made a standalone device and I optimized it for cost So this thing costs ten dollars to make a Single one so I'm going to make workshops with that in the near future Maybe in half a year or something like that Yeah, I also experimented with microbead and I also Came back to my my experiments with graphics and sprites and But the time is running low. So I will only show you the the photos I have of that and You can catch me outside this room after the talk if you want to see more and talk about this and if you are interested Oh, by the way, I ported it back to micro Python, but I didn't make a pull request There is a separate repo, but you can make your own game console with micro Python yourself it works on M5 stack as well and Yeah, you can just connect a bunch of wires and and a display and it just works and those are all the addresses You might find useful and are there any questions? Do we have time for questions? Three questions Yes, please I prefer programming games It shows right Sorry, the question was if I prefer playing games or programming. Yes That's a basically bigger version of what is the thing I'm wearing yes, sorry This is a bigger version of of this so you can actually use it as a conference batch or something like that You can program it to display text and also games or things like that. Yes Do I have any repositories or open source or examples of the games? Yes, there are the two URLs at the top I Main URLs to the documentation and they also contain links to the repositories Everything I'm doing here is open source. Everything is In the open also the hackaday.io link in the middle there is a link to basically all my projects on hackaday so you can It's like a blog basically I just put all the updates on my projects in there. Thank you Thank you very much I'm going to need to talk to you I want to make the workshop with that on your Python However, the problem is people have to have some way to of paying for them. Yes, they can bring cash, right? Yeah, that's one way Or we can set up something in the shop maybe. Yeah, well, we will see I Need to vacate the premises Mog jeden zestap kupić za moment porozmawiamy. Dobrze