There is a generic LED driver under Linux, for the LEDs on the development board, a separate gpio is used for control and its corresponding driver is named gpio-leds. this pin is already occupied by the gpio-leds driver, providing a dedicated LED operation interface to the application layer.
There are two LEDs on the development board in this position
Among them, the LED corresponding to GPIO3_A4 is named "work", and the LED corresponding to GPIO2_B2 is named "work1"
The nodes provided to the application layer are stored in the following two folders
/sys/class/leds/work
/sys/class/leds/work1
The led device node requires root privileges to operate, and root privileges are required to operate the led using the command line or a program compiled in C language. If you are using ssh or LX terminal, first execute the following command to obtain root privileges.
sudo su
The following is an example of operating the LED corresponding to GPIO3_A4. This LED is located at the position shown in the red box in the figure below
Set the led flip condition to none, that is, only set to light or off
echo none > /sys/class/leds/work/trigger
Set led to light
echo 1 > /sys/class/leds/work/brightness
The following phenomena can be seen
Set led to off
echo 0 > /sys/class/leds/work/brightness
The following phenomena can be seen
Set the led flip condition to timer, then the kernel thread will realize the timer flip, the user needs to set the time of light and off
echo timer > /sys/class/leds/work/trigger
Write the delay_on node to configure the on time in ms and the delay_off node to configure the off time in ms. e.g. set the led to be on for 1000ms and off for 200ms cycle
echo 1000 > /sys/class/leds/work/delay_on
echo 200 > /sys/class/leds/work/delay_off
By linking to the libperipheral_api.a static library, the following interfaces can be called in C to manipulate LEDs
/**
* @name: user_led_set_none_mode
* @description: Set the LED to no-trigger mode, in which user_led_turn_on_off is called to configure on and off
* @param led_name:LED name,for instance “work“ “work1“
* @return 0 - success ; less than 0 - fail
*/
int user_led_set_none_mode(char *led_name);
/**
* @name: user_led_turn_on_off
* @description: Configure the LED to be on or off, only in no-trigger mode
* @param led_name:LED name,for instance “work“ “work1“
* @param level:1 - ON 0 - OFF
* @return 0 - success ; less than 0 - fail
*/
int user_led_turn_on_off(char *led_name, unsigned char level);
/**
* @name: user_led_set_timer_mode
* @description: Configure LED for timer flip mode
* @param led_name:LED name,for instance “work“ “work1“
* @return 0 - success ; less than 0 - fail
*/
int user_led_set_timer_mode(char *led_name);
/**
* @name: user_led_set_timer_on_off
* @description: When the LED is in timer flip mode, set the time to turn on and off
* @param led_name:LED name,for instance “work“ “work1“
* @param on_time:Time to ON, in ms
* @param on_time:Time to OFF, in ms
* @return 0 - success ; less than 0 - fail
*/
int user_led_set_timer_on_off(char *led_name, int on_time, int off_time);
The test demo is as follows, taking the operation of "work" led as an example
#include "peripheral_api.h"
void led_api_test(void)
{
int ret = 0;
ret = user_led_set_none_mode("work");
if (ret < 0) {
printf("user_led_set_none_mode fail \n");
return;
}
ret = user_led_turn_on_off("work",1);
if (ret < 0) {
printf("user_led_turn_on_off fail \n");
return;
}
sleep(1);
ret = user_led_turn_on_off("work",0);
if (ret < 0) {
printf("user_led_turn_on_off fail \n");
return;
}
sleep(1);
ret = user_led_turn_on_off("work",1);
if (ret < 0) {
printf("user_led_turn_on_off fail \n");
return;
}
ret = user_led_set_timer_mode("work");
if (ret < 0) {
printf("user_led_set_timer_mode fail \n");
return;
}
ret = user_led_set_timer_on_off("work", 1000, 200);
if (ret < 0) {
printf("user_led_set_timer_on_off fail \n");
return;
}
sleep(5);
ret = user_led_set_none_mode("work");
if (ret < 0) {
printf("user_led_set_none_mode fail \n");
return;
}
return;
}
int main()
{
led_api_test();
return 0;
}
This command also requires root privileges, and the result after running is as follows