In fact, this sentence is not completed. The full sentence is “Everything is a file descriptor or a process”. As a freshman in computer science, I think the first half is enough, so far.
There is an example from my Raspberry Pi.
I wanted to know how to control the GPIO of my Pi. Then I found an interesting way to operate it. Just use the “echo” command. Here is the code. In this example, we control GPIO18.
sudo echo 18 > /sys/class/gpio/export # 把GPIO18暴露到用户空间 cd /sys/class/gpio/gpio18 # 上面那条命令会创建/sys/class/gpio/gpio18这个目录 sudo echo out > direction # 把GPIO配置成输出模式 sudo echo 0 > value # 把GPIO拉低 sudo echo 1 > value # 把GPIO拉高
You can find “gpio” folder in my system. The premise is that OS has the driver of gpio control. That’s one of the reasons why I chose Raspberry Pi. I do not need to write the device driver by hand. And I do not need to follow the changing rules of the OEM. What’s more, they are always hard to follow. I’m fed up with weird syntax and version incompatibility. I even needed to write code and upload firmware in Windows and compile code in Ubuntu by ssh. What a stupid design! If I do all of this, why do you call your tool set IDE??? Hey, it’s you, H**w*i! Do you know your Hi3861 almost drove me crazy?
OK, just come back. I just follow the Linux way to operate the hardware. First, we write 18 to “export”. This tells the OS we need to call gpio device, and its code is 18. Then we write “out” to “file” which is called “direction” directly. As same as direction, we can also write the level of the pin.
If we just look at these commands. We just write some strings into files. You may ask “How to cancel it” or “What will happen after we cancel it, whether the new folder ‘gpio18’ will be ‘delete’?” The answer is yes. Here are the commands to cancel the operation.
cd ~ sudo echo 18 > /sys/class/gpio/unexport
All changes will not be saved. The “export” and “unexport” will restore the gpio number you chose. And the folder “gpio18” will be deleted.
Maybe there are many errors in this blog and this blog looks stupid or childish. Never mind. At least I learned “Everything is a file” in person. And this is a small but important step in my way to learning Linux.
Keen on it!
Views: 81