#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
void SetLed(int hfile, char * value);
int main()
{
char ch;
int hBrightness=0;
int hTrigger=0;
char *ON = "1";
char *OFF = "0";
char *NONE = "none";
char *HEARTBEAT = "heartbeat";
enum BOOL {false=0,true} bLoop = true;
if ((hBrightness = open("/sys/class/leds/led3/brightness", O_WRONLY ) ) == -1){
printf("Error opening Brightness file: %s\n",strerror(errno));
}
else {
printf("Brightness file successfully opened.\n");
}
if ((hTrigger = open("/sys/class/leds/led3/trigger", O_WRONLY ) ) == -1){
printf("Error opening Trigger file: %s\n",strerror(errno));
}
else {
printf("Trigger file successfully opened.\n");
}
//As long as 'q' is not pressed
while ( bLoop ) {
printf("Actions on LED3:\n");
printf("\t 0 - Turn off. \n");
printf("\t 1 - Turn on. \n");
printf("\t h - Heartbeat. \n");
printf("\t n - No heartbeat. \n");
printf("\t q - Quit. \n");
printf("Your choice: ");
ch=getchar();
switch (ch) {
case '0' : SetLed(hBrightness,ON); break;
case '1' : SetLed(hBrightness,OFF); break;
case 'h' : SetLed(hTrigger,HEARTBEAT); break;
case 'n' : SetLed(hTrigger,NONE); break;
case 'q' : bLoop = false; break;
default : break;
}
//empty input buffer
while((ch=getchar()) != '\n'){}
}
if ( close(hBrightness) != 0 ) {
printf("Error closing Brightness file. Error: %s\n",strerror(errno));
}
else {
printf("Brightness file successfully closed.\n");
}
if ( close(hTrigger) != 0 ) {
printf("Error closing Trigger file. Error: %s\n",strerror(errno));
}
else {
printf("Trigger file successfully closed.\n");
}
exit(0);
}
void SetLed(int hFile, char * value){
int length = strlen(value);
int nwrite;
if ( (nwrite = write(hFile,value,length) ) != length )
{
printf("Error writing to file. Error: %s.\n",nwrite,strerror(errno));
}
}
Øvelse 4: Linux Kernemoduler
Abonner på:
Kommentarer til indlægget (Atom)
Ingen kommentarer:
Send en kommentar