- #include <sys/types.h>
 
- #include <sys/stat.h>
 
- #include <fcntl.h>
 
- #include <stdio.h>
 
- #include <errno.h>
 
- #include <string.h>
 
- #include <unistd.h>
 
- #include <stdlib.h>
 
 
- #define LED_PATH_BRI "/sys/class/leds/blue:heartbeat/brightness"
 
- #define LED_PATH_TRI "/sys/class/leds/blue:heartbeat/trigger"
 
- //ledtest 0
 
- //ledtest 1
 
- //ledtest 2 heartbeat
 
- int main(int argc,char **argv)
 
- {
 
-     int fd;
 
-     if(argc < 2)
 
-     {
 
-         printf("need led commmand 0:close 1:open 2:trigger \n");
 
-         return -1;
 
-     }
 
-     if(strlen(argv[1]) == 1)
 
-     {
 
-         if(argv[1][0] == '0' || argv[1][0] == '1')
 
-         {
 
-             fd = open(LED_PATH_BRI,O_RDWR);
 
-             write(fd,argv[1],1);
 
-         }
 
-         else if(argv[1][0] == '2')
 
-         {
 
-             if(argc < 3)
 
-             {
 
-                 printf("need trigger \n");
 
-                 return -1;
 
-             }
 
-             fd = open(LED_PATH_TRI,O_RDWR);
 
-             write(fd,argv[2],strlen(argv[2]));
 
-         }
 
-         else
 
-         {
 
-             printf("led commmand error 0:close 1:open 2:trigger \n");
 
-             return -1;
 
-         }
 
-     }
 
-     else
 
-     {
 
-         printf("led commmand error 0:close 1:open 2:trigger \n");
 
-         return -1;
 
-     }
 
-     close(fd);
 
-     return 0;
 
- }
- ./ledtest 1 //打开LED
 
- ./ledtest 0 //关闭LED
 
- ./ledtest 2 heartbeat //修改LED触发方式为系统心跳
 
接下来做个有UI的,通过按钮切换LED状态
- #include <gtk/gtk.h>
 
- #include <sys/types.h>
 
- #include <sys/stat.h>
 
- #include <fcntl.h>
 
- #include <stdio.h>
 
- #include <errno.h>
 
- #include <string.h>
 
- #include <unistd.h>
 
- #include <stdlib.h>
 
 
- #define LED_PATH_BRI "/sys/class/leds/blue:heartbeat/brightness"
 
- #define LED_PATH_TRI "/sys/class/leds/blue:heartbeat/trigger"
 
 
- static void led_ctrl(uint8_t cmd,char *trigger)
 
- {
 
-     int fd;
 
-     if(cmd < 3)
 
-     {
 
-         if(cmd < 2)
 
-         {
 
-             fd = open(LED_PATH_BRI,O_RDWR);
 
-             write(fd,cmd==0?"0":"1",1);
 
-         }
 
-         else
 
-         {
 
-             if(strlen(trigger) < 1)
 
-             {
 
-                 g_print("need trigger \n");
 
-                 return ;
 
-             }
 
-             fd = open(LED_PATH_TRI,O_RDWR);
 
-             write(fd,trigger,strlen(trigger));
 
-         }
 
-     }
 
-     else
 
-     {
 
-         g_print("led commmand error 0:close 1:open 2:trigger \n");
 
-         return -1;
 
-     }
 
-     close(fd);
 
- }
 
 
- static void gw_led_off(GtkWidget *widget,gpointer data)
 
- {
 
-     led_ctrl(0,NULL);
 
- }
 
 
- static void gw_led_on(GtkWidget *widget,gpointer data)
 
- {
 
-     led_ctrl(1,NULL);
 
- }
 
 
- static void gw_led_trigger_heartbeat(GtkWidget *widget,gpointer data)
 
- {
 
-     led_ctrl(2,"heartbeat");
 
- }
 
 
- static void gw_led_trigger_none(GtkWidget *widget,gpointer data)
 
- {
 
-     led_ctrl(2,"none");
 
- }
 
 
- static void  activate (GtkApplication *app,gpointer user_data)
 
- {
 
-     GtkWidget *window;
 
-     GtkWidget *button0;
 
-     GtkWidget *button1;
 
-     GtkWidget *button2;
 
-     GtkWidget *button3;
 
-     GtkWidget *button_box;
 
 
-     window = gtk_application_window_new (app);
 
-     gtk_window_set_title (GTK_WINDOW (window), "LED Test");
 
-     gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);
 
 
-     button_box = gtk_button_box_new (GTK_ORIENTATION_HORIZONTAL);
 
-     gtk_container_add (GTK_CONTAINER (window), button_box);
 
 
-     button0 = gtk_button_new_with_label ("LED Off");
 
-     g_signal_connect (button0, "clicked", G_CALLBACK (gw_led_off), NULL);
 
-     gtk_container_add (GTK_CONTAINER (button_box), button0);
 
 
-     button1 = gtk_button_new_with_label ("LED On");
 
-     g_signal_connect (button1, "clicked", G_CALLBACK (gw_led_on), NULL);
 
-     gtk_container_add (GTK_CONTAINER (button_box), button1);
 
 
-     button2 = gtk_button_new_with_label ("heartbeat");
 
-     g_signal_connect (button2, "clicked", G_CALLBACK (gw_led_trigger_heartbeat), NULL);
 
-     gtk_container_add (GTK_CONTAINER (button_box), button2);
 
 
-     button3 = gtk_button_new_with_label ("trigger none");
 
-     g_signal_connect (button3, "clicked", G_CALLBACK (gw_led_trigger_none), NULL);
 
-     gtk_container_add (GTK_CONTAINER (button_box), button3);
 
 
-     gtk_widget_show_all (window);
 
- }
 
 
- int main (int argc, char **argv)
 
- {
 
-     GtkApplication *app;
 
-     int status;
 
 
-     app = gtk_application_new ("org.gtk.ledtest", G_APPLICATION_DEFAULT_FLAGS);
 
-     g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
 
-     status = g_application_run (G_APPLICATION (app), argc, argv);
 
-     g_object_unref (app);
 
 
-     return status;
 
- }
