Ansteuerung der Hardware/Beispiele > LED-Ansteuerung > Beispiel in C

Navigation:

 

Ansteuerung der Hardware/Beispiele > LED-Ansteuerung >



Beispiel in C

Previous pageReturn to chapter overviewNext page

#include <stdio.h>

#include <unistd.h>

#include <stdlib.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

#include <string.h>

 

#include <sys/select.h>

#include <sys/time.h>

#include <errno.h>

 

#include <linux/input.h>

 

#define LED_PATH "/sys/class/leds"

 

 

int led_init(void)

{

 FILE        *fd;

 char        *none_str = "none";

 char        led_file[40];

 unsigned long i;

 

 for(i=1;i<=7;++i)        // LED 1..7

 {

         sprintf(led_file, "%s/led%lu/trigger", LED_PATH, i);

         printf("LED-path=%s\n", led_file);

 

         // Configure leds

         fd = fopen(led_file, "w");

         if (fd == NULL) {

                 perror("led-test: unable to configure led1");

                 return -EACCES;

         }

 

         fprintf(fd, "%s\n", none_str);

         fclose(fd);

 }

 

 return 0;

}

 

int led_set(unsigned char nr, unsigned char data)

{

 FILE        *fd;

 char        led_file[40];

 sprintf(led_file, "%s/led%lu/brightness", LED_PATH, nr);

 

 // Set LED

 fd = fopen(led_file, "w");

 if (fd == NULL) {

         perror("led-test: unable to configure led");

         return -EACCES;

 }

 

 fprintf(fd, "%d\n", data);

 fclose(fd);

}

 

int main(void)

{

 unsigned long        i;

 

 int        ret;

 

 

 

 printf("Starting\n");

 

 ret = led_init();        // Init LED's

 if (ret < 0)

         exit(1);

 

 

 for(i=1;i<=7;++i)

 {

         led_set(i,1); // LED=on

 }

 

 sleep(1);

 

 for(i=1;i<=7;++i)

 {

         led_set(i,0); // LED=off

 }

 

 printf("End\n");

 exit(0);

}