From the shell, I can activate the LEDs in my system as follows:
#echo 1 > /sys/class/leds/NAME:COLOR:LOCATION/brightness
I want to do the same from a C program, but I could not find a simple example of how to do this?
Open sysfs node as a file, write '1' and close it again.
For instance:
#include <stdio.h> #include <fcntl.h> void enable_led() { int fd; char d = '1'; fd = open("sys/class/leds/NAME:COLOR:LOCATION/brightness", O_WRONLY); write (fd, &d, 1); close(fd); }
Something like that:
#include <stdio.h> int main(int argc, char **argv) { FILE* f = fopen("/sys/class/leds/NAME:COLOR:LOCATION/brightness", "w"); if (f == NULL) { fprintf(stderr, "Unable to open path for writing\n"); return 1; } fprintf(f, "1\n"); fclose(f); return 0; }
I do not boot into my Linux partition, but I suspect it looks something like this:
int f = open("/sys/class/leds/NAME:COLOR:LOCATION/brightness",O_WRONLY); if (f != -1) { write(f, "1", 1); close(f); }
If you are looking for a breakthrough and not looking for performance or anything, just use the system() function to invoke the command in the same way as in the shell. Here is a link.
system()
/* system example */ #include <stdio.h> #include <stdlib.h> int main () { system( "echo 1 > /sys/class/leds/NAME:COLOR:LOCATION/brightness"); return 0; }
Source: https://habr.com/ru/post/1410802/More articles:How to use Delphi to correctly execute a .bat file - delphiXcode 4.3 crashes when using LLDB - xcodeShould the events generated by the controls be in the user interface thread? - multithreadingCreateProcess returns immediately, but only if the running process is hidden - winapiGetting mySQL password / name for phpmyadmin using MAMP - mysqlPurpose of const boost :: shared_ptr & as function argument? - c ++https://translate.googleusercontent.com/translate_c?depth=1&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1410804/is-there-a-way-to-link-to-meteorcollections-together&usg=ALkJrhi9lQms7ePTkMBOtAySZog-hsZrdAerror C2275 using typedef structure - chttps://translate.googleusercontent.com/translate_c?depth=1&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1410806/js-to-monitor-the-css-property-change-like-displaynone-displayblock&usg=ALkJrhhfB18xn5ZbU93CHCGvCYzAqnoMOgHow to use Ant to include non-java resource files with compiled classes - javaAll Articles