Bbb board Lesson 5: shell script programming Experiment
In Class 1, we test and control the high/low output of the gpio port through simple echo commands to enable and disable the control of an LED indicator. This section uses shell script programming, to complete the experiment of switching between two LED lights.
Experimental Equipment: Prepare two LEDs, crumb, etc.
Use gpio ports p8.12 and p8.11 pins
At present, without using other tools and software, we can remotely log on to the graph desktop of the BBB board and use the built-in ledfpad editor to program shell scripts.
Remember to remotely log on to the BBB board graphic desktop. If you do not remember to refer to the previous course, we will not explain it here. Click the menu in the lower left corner and click to enter leafpad.
After opening the editor, enter the following shell command code:
Script implementation: enables two LEDs to shine 10 times in turn every second
Follow the script in the figure and enter it one by one. If you have no idea about the basic knowledge of shell, let's go over a hundred times. There is a lot of related knowledge on it. Here I will briefly describe the script led1 (I save the name without the suffix, but it is better to add the suffix SH, it is easier to identify, led1.sh will know it is a shell script program) as follows:
The first line of code :#! It is an agreed tag that tells the system what interpreter is required to execute this script, that is, which shell is used. Here we use the bash interpreter to write the full path, you can find bash in the BBB/bin directory.
#! /Bin/bash
The following two lines of code are used to open gpio44 and gpio45. A judgment condition is added. If there is no such condition, run the echo command to open the corresponding port:
If [! -D/sys/class/gpio/gpio44]; Then ECHO 44>/sys/class/gpio/export; FI
If [! -D/sys/class/gpio/gpio45]; Then ECHO 45>/sys/class/gpio/export; FI
The following two lines of code are used to set the ports to the output mode "out ":
Echo out>/sys/class/gpio/gpio44/Direction
Echo out>/sys/class/gpio/gpio44/Direction
The following for loop statements enable two LEDs to output level 1 or 0 alternately for 10 times. Sleep is a latency command:
For (I = 0; I <10; ++ I ))
Do
Echo 1>/sys/class/gpio/gpio44/value
Echo 0>/sys/class/gpio/gpio45/value
Sleep 1
Echo 0>/sys/class/gpio/gpio44/value
Echo 1>/sys/class/gpio/gpio45/value
Sleep 1
Done
The last two lines of code are to turn off the LED light, which is equivalent to restoring the original state:
Echo 0>/sys/class/gpio/gpio44/value
Echo 0>/sys/class/gpio/gpio45/value
You can also add the following two lines of code to disable gpio44 and gpio45:
Echo 44>/sys/class/gpio/unexport
Echo 45>/sys/class/gpio/unexport
After the program code is explained, you only need to learn more about the shell information by yourself. Here, we mainly learn it in a simple way and know the simplicity and strength of using shell script programming, of course, the functions of C ++ and other languages will be more powerful. In the future programming experiments, we will mainly use the C ++ language for explanation.
After the above script code is compiled, save it and you will be able to run it in terminal mode.
Directly go to the script storage directory and execute: # bash led1 (or # bash led1.sh). Then we can see that the two lights are shining alternately:
(End)