 Perfect to welcome, welcome, welcome. It is day two of Euro Python. I hope everyone is keeping fine. I hope everyone is having loads of fun so far. I have here with me already Peter Chu, our first speaker for today. Hello, Peter. Hello, everyone. So Peter is doing it from Taiwan, right? Yeah, that's true. And it's two in the afternoon there. It's seven a.m. for me. But let's do this. Let's kick this off. Perfect. So Peter is going to talk to us today about Speak Python with devices, how Python can be used in IoT infrastructure automation. So it's your turn now. Take it away, Peter. Thank you very much. And hello, everyone. Hello, Europe. And I'm Peter. Thank you for joining me in this session. And let me first introduce myself. I am a software engineer in Taiwan and love Python like all you guys. Besides that, I am a conference scholar. I like to share myself while I learn and more importantly, the more for others after the conference. And in fact, I have course on open source conference in Taiwan. And in fact, we have a conference next week, two days, 14 tracks, and 1,000 people in person. And I would like to invite you to come that you know due to the pandemic. So next time maybe. And in law, I cannot join your person today. I'm glad we can still hang out in this way. So now it's your turn. Why are you interested in this session? Apparently, I cannot join with you one more at this time. But I guess most of you are working on data and lights, and web development, things like that. These are the most common ways people use Python. But you know that Python is very powerful and useful language, which can be used in a variety of tasks, not just those I mentioned. So if you wish to acquire something new into your physical set, you are in the right talk. We will see how to use the power of Python to build some amazing things for a tiny IoT device to a large-scale platform system. This is why we will talk about it today. Since this talk is for beginners, we will start with some fundamental concepts of the operating system. On the other hand, if you are already an expert in this area, I'm sorry, but I'm sorry, here is the point. Let's begin. In Instagram, the light green part stands for the OS, and the light green part means physical hardware devices. There are two layers in the OS, user space and aerospace respectively. Your Python program will be in user space. On the other hand, device drivers are living in general space. We know that Alex Kuro is firing in C language. So notice that if the device you concern does not have a corresponding driver, sorry, but as a Python user, we can do nothing at all. So in the firing slice, let's assume the driver is ready. Fair enough, the OS will represent our devices as sound space applies in the Python system. You can see that in the slice screen shot. Hopefully speaking, those files are located in the depth directory in Linux. So you may have been heard about in Unis. Everything is a file, plus also two for devices. I emphasize this here because it means that we can manipulate a device like a file. And the great thing is you are able to manipulate a file. Let's see an example. And if you run the snippet here in a REST verified, you can make an onboard LED blink. You can see the demonstration on my camera. Take it. You can see here is something blinking. And yeah, the blinking label is story, you might think. It's true, but the interesting thing is the code here just opened a device file, write something, then the behavior of the device is changed like magic. Do you think it's trivial? Yeah, it is. But last now, the full story, there are one more operation few people to know. And it's our living after today, IOCTL. IOCTL means input output control. As it's not suggested, the purpose of it is to control devices. We don't need it in the LED example, since it's the simplest one. We can manipulate it with merely rating and writing. But for many devices, that's not enough. And example is a model. And later things to do with the receive data by reading and send data by writing. But how can we send get a copy of it? The example, this is why and where IOCTL plays for. This is why exactly it looked like. It has three parameters, file descriptor, request, and arguments. Notice now your request is constructed with four more parameters. Here are some of the details I don't want to go through with them. Instead, an analogy may be more helpful. No matter what you work for, you should feel comfortable with REST4API write. I find that we can have a mapping between REST4API request and parameters of IOCTL. For example, as you have known, REST4API always has two paths, the header and the body. Then we can say IOCTL also has a header and a body. The second parameter of IOCTL, the request is the header. The third parameter of it, the arguments, is the body. From this mapping, I expect you can understand all parameters of IOCTL. These are, I say, any more. So you can read it in seconds. Yeah, got it? So indeed, many people feel struggle to understand IOCTL for the very first time. So as me, so as I. But after studying, I found out IOCTL is just like HTTP. They're coming in the phase to interact with resources, but the resources are hardware devices at least time. No, we know IOCTL, but hey, we are presently stuck. How can we use it in Python? And that's getting stuck for a mini example. In this example, we use IOCTL to get device names, two buttons, keyboard mouse, and some ultra-parameter devices. That's what I have on my computer. To achieve it in Python, there are three common criteria we have to make. The first is to create IOCTL request if you forgot what it is. The request let the driver know what do you want to do, like act like a response API header. The second is, since we communicate with drivers and drivers around C, we have to convert the data types between C and Python. And lastly, we have to manage to assess IOCTL in the Python environment. Then we can call in. There are many ways to meet this criteria, and we will see two approaches to that. The first approach, we write a C extension module. We have to write some C calls in this approach, and it's only work on C Python. On the other hand, the second is a pure Python approach. I will explain both of them step by step. And let's start on the first one. Firstly, we define the IOCTL function, the thing you intend to do. Here we define a function called getDeviceNet. It has three arguments. The first is the device file path. The second answer is an address of a digital memory, and besides, we can carry so far it after we call this function. I think the call is well-safe to explain. It's definitely a thing, but let's have a look at it. We create an IOCTL request at this line. We can see I use a micro-name, dbiocg-name here. In general, device drivers will provide open micros like this, so you can use it easier. And another thing is remember an IOCTL request is constructed with four parameters. So if you expand the micro here, you can see it exactly takes four parameters, as shown in the line 17s. After the IOCTL function is defined, last important, implement a wrap function out of it. This function consumes a device file path and return a corresponding device name. This function is what we actually access in our Python code. So you can see we have a Python style to function name, and arguments and written values are proved by type, type objects. Type objects is one of the most common address structure provided by SleepPython. And most labels to implement it are converting the data types between C and Python. You can see I use some type prefix micros to do it. These micros are also provided by SleepPython. And this is a trivial case. Since the arguments and return types are just streamed, it can be a speech struct or Python D dictionary in some more complex example. Okay, in the last step, the last step is to do some packaging work. In this approach, we are, what we actually get is a Python module. So to build this module, we have to provide some necessary information, such as module name, at least some functions available in this module, things like that. Just follow the SleepPython spec here. Okay. So now our module is ready. We can install it, then import it, and then call it in the usual Python way, as shown in the screenshot. We get a device then called MyOS first. Okay. Now let's turn to a second approach. In this approach, we do similar things like the first one. But at least we don't have to write any SQL. Again, let's start by creating the IOCTA request. We use a micro to do it in the last time. But how can we access macros in Python at this time? No, we can't. We have to port necessary macros to Python. And fortunately, common macros for creating IOCTA request has been provided by some good guys. So we just need to pull driver's basic macros. And it's usually not a big deal, just build lines. Again, we have to cover the data type. And it's still trivial here, since we just didn't go in detail by calling right here. Notice that we use a viewing module called FCNTL here. It's provided to use IOCTA function. So we don't have to write any SQL at this time. Finally, we can have the same resource as before. Now, you might be moving in your mind. Enough theories, please give me something in practical. Okay. So let me find a convincing example for you. A couple feet there might be sounds interesting. Just attach a sensor onto a Raspberry Pi, then get a sensor status by what we learned today. And then when our car is approaching, open again, most of the time. Yeah. But I want to, that's interesting, but I want to show you something more impressive. Like this one. Do you know what it is? It's a very unique massive storage system used in nuclear experiments. Unlike the interest-based media we use, this system is based on magnetic caps, dozens of caps, they use the system to store data of how our universe works. You can see what's inside of it in the video in the right button. There are many kinds of devices, mobile arms, powerful readers, type drives, things like that. All these devices need to be controlled and coordinated by some systems. So this gives us a great idea to show what we can do from what we learned today. So in the following slides, last image, we build such kinds of systems with Python. That's beginning. We will use the tape simulator today so you can make it at home. You can see my virtual drives and their device file path in the screenshot. We will use that later. And we also need a driver. Things already have one, so no worry about it. Now, let's see how to use the tape storage. To write data on tape cardage, we need to find it and load it, and then rewind our provided to a correct position, and then we can start to write. For proof of concept, we will see how to implement rewind and get status today. First, let's see how to get the status. We have a reducing how to manipulate a device with IOCTL and Python, but it's a little bit more complex this time. In the previous case, we just asked string and the device name, and this time we get the status in a C-strap. Notice how we convert C-strap into a Python dictionary in the snippet. Let's go to the next one. In the previous one, we used C extension and now let's try a pure Python approach. You can see how to use the struct module here to encode arguments and decode return values in this course maybe. And here is the last course need. And besides the module we covered today, the C-type module is also very useful. C-types is a pointer function library for Python. It provides the compatible data type and allowing core C functions like IOCTL. You can see how we use it to implement the rewind function in this course maybe. And you can run course maybe and get a result like this. You can see how we fetch and change the type position. It's the first step to build a massive type storage we just saw. Okay, let's see. Summary. The purpose of this storage is to let you know we can use Python to build some major things for our card to be there to a massive storage system. You just need to know how the computer works and how to manipulate a device in Python. Then you can thank me. Thank you. Yay, thank you. Thank you very much. Wonderful. We took a little bit less time so let's see who has questions. So far we didn't have any questions in this course and I don't think we have any questions on Zoom, not yet. Okay, that's also a great thing. I think it's a bit early for everyone. No, we're having someone is typing a question, I believe. So any case saying, yeah, it's applauding as well. Perfect. So our first question. So do you know any robotic kits that would be great to experiment with? You said the storage system, I just saw. Okay, and do you have any other suggestions as well or like that's your favorite? Sorry, can you say that again? Is that the one you just showed? Is that your favorite or do you have other suggestions as well? Oh, no, I just, in the past few years I've tried to work on a sample drug just like the system I showed you. So I think it's simple. Okay, so while we're waiting for questions, yeah, tell me more about your participation in PyCon Taiwan. How long have you been part of the community? Yeah, I love Python. In fact, I like all PyCons in the past few years. I have attended PyCon Hong Kong and PyCon Japan. And yeah, I think the PyCon community have a big connections, international connections. So I enjoy all PyCons, not just Taiwan. Okay, the next PyCon Japan is coming up soon, right? The CFP just closed. We're quite excited. Did you just submit anything to it? Yeah, but you know, a lot of pandemics, so maybe we can just virtually traveling around the world is new. Yes, I think that's going to be the case. But well, it's okay. Any more questions? Anyone has any other questions for Peter here? I see people typing, but I don't see, I don't see their actual outpush. Well, it's maybe just caste typing. Yeah, let's see. No, I don't think we have anything else right now. If you have any more questions for Peter, please just type your questions on and speak Python devices. There is a room for that under the knee breakouts. So well, Peter, thank you very, very much. It was an absolute pleasure. Thank you for joining us. Yeah, thank you. And I will be there waiting for your more questions. You can have a copy and think about us. Thank you. Thank you. Thank you. It was an absolute pleasure.