8. Command line lighting and key detection¶
The command operations in this chapter are executed on the terminal of the board
So much content has been talked about before, and finally ushered in the main event - using various hardware peripherals of the board. Such as lighting, buttons, buzzer, serial port, etc. necessary for getting started. This chapter first guides you to use the command line to control LED lights and detect buttons.
8.1. Preliminary exploration of the /sys directory¶
Similar to the /proc directory, the files/folders in the /sys directory provide users with information about devices, kernel modules, file systems, and other kernel components, such as subdirectory block stores all block devices. Subdirectory bus stores all bus types in the system, including i2c, usb, sdio, pci, etc.. The subdirectory class classifies devices by type, such as leds, lcd, mtd, pwm, etc.
Try to execute the following command on the terminal of the board to view the directory content of each level of sys:
1 2 3 4 5 | #Execute the following command on the terminal on the board to view
ls /sys
ls /sys/class
ls /sys/class/leds
ls /sys/class/leds/sys_status_led
|

It can be seen that the /sys/class/leds directory of this board includes: sys_status_led. It represents the green light beating like a heart on the LubanCat-RK series board, and the silkscreen says USER.
Take the /sys/class/leds/sys_status_led directory as an example, it contains brightness, device, max_brightness, power, subsystem, trigger, uevent and other files. Among them, brightness indicates the brightness of the LED light, and trigger indicates the trigger mode of the LED light. We can modify or view these files through commands such as echo and cat, so as to achieve the purpose of controlling the LED light. The following examples are used to explain.
8.2. Control Heartbeat Light¶
Before controlling the heartbeat light, you need to switch to root user operation.
Before controlling the heartbeat light, we can first look at how the heartbeat light is triggered.
1 | cat /sys/class/leds/sys_status_led/trigger
|
As you can see, the selected one is the state of heartbeat.

The brightness file under the LED light device represents its brightness value. In the kernel driver provided by this board,the heartbeat light is directly controlled by IO, and its brightness range is 1 and 0, which means it is on or off.
1 2 3 4 | #Turn off the heartbeat light, the heart is destroyed
echo 0 > /sys/class/leds/sys_status_led/brightness
#Turn on the heartbeat light, the heart is eternal
echo 1 > /sys/class/leds/sys_status_led/brightness
|
When we finish this operation, we can see how the heartbeat light is triggered now, and it is now in an uncontrolled state.

If you want to switch the light to heartbeat mode, after execution, the heartbeat light will re-enter the heartbeat state.
1 2 | #Perform the following operations with root privileges
echo heartbeat > /sys/class/leds/sys_status_led/trigger
|
8.3. Preliminary exploration of the /dev directory¶
In addition to the /proc and /sys directories, the /dev directory also contains very rich device information. This directory contains all the external devices used in the Linux system. For example, /dev/tty is a serial port device, and /dev/ram is memory. Through these device files, we can also access the corresponding hardware devices.
Try viewing the contents of the dev directory with the following command:
1 2 | ls /dev
ls /dev/input
|

The content in the /dev/input directory in the above figure is an example. The above event0 is the event file interface of the input device. Through them, the input events reported by the device can be obtained. The number after the event is not bound to the device. See the file /proc/bus/input/devices for what they represent.
Execute the following command on the terminal of the board:
1 | cat /proc/bus/input/devices
|

As shown in the figure above, it can be seen that the current board contains multiple input devices, which may be different on specific boards:
event0: The name is gpio-keys/remotectl, it corresponds to the infrared receiver, input0, some boards may not have this function.
event1: the name is rk805 pwrkey”, it corresponds to the power button on/off, input1, some boards may not have this function.
event2: the name is adc-keys, it corresponds to the Recovery key, input2, some boards may not have this function.
event3: the name is rk-headset, it corresponds to headphone insertion detection, input3, some boards may not have this function.
注解
The flashing function of the Recovery key is only used when the kernel is started. After entering the system, this key will lose its function. We can operate this key. Let’s take this key as an example.
8.4. Detect key¶
In the board, we can use the evtest tool to more conveniently view the input device currently connected to the hardware and detect it in real time.
apt install evtest tool
1 | sudo apt install evtest
|
Notice:Before using the apt install command for the first time, you need to perform the following execution to refresh the mirror source.
1 | sudo apt update
|
Use the evtest tool:
1 | sudo evtest
|
After executing the command, it will scan the event device input event files in the /dev/input directory and list them to the terminal.

It reminds us that we can select the corresponding device for testing by numbers, please select according to the output on your own board.
We select “adc-keys”, that is, press ‘2’ and press Enter to confirm, and execute “Ctrl” + “c” to exit.

The adc-keys correspond to the Recovery button (not supported by the LubanCat-Zero series). When I press and release it once, it will look like the picture below.
