-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathledController.cpp
More file actions
61 lines (56 loc) · 1.66 KB
/
Copy pathledController.cpp
File metadata and controls
61 lines (56 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <stdio.h>
#include <unistd.h>
#include <string.h>
const char * const RED_PATH = "/sys/class/leds/led1/brightness";
const char * const GREEN_PATH = "/sys/class/leds/led0/brightness";
bool controllLED(const char * path,int status){
FILE * fpt = fopen(path,"w");
if(fpt==0){
return false;
}
fprintf(fpt,"%d\n",status);
fclose(fpt);
return true;
}
bool controllLEDByHeart(const char * path,int time){
int lightStatus = 0;
for(int i=0;i<time*2;i++){
if(!controllLED(path,lightStatus)){return false;}
lightStatus = 1 - lightStatus;
sleep(1);
}
return true;
}
bool blinkByAscii(const char * outputPath,const char * timePath,const char * input){
int ascii;
printf("Starting...\n");
if(!controllLEDByHeart(timePath,0)){return false;}
if(!controllLEDByHeart(outputPath,0)){return false;}
for(int k=0;k<strlen(input);k++){
ascii = int(input[k])-int('A');
if(ascii<0){return false;}
printf("Show %c by blinking %d times\n",input[k],ascii);
if(!controllLED(timePath,1)){return false;}
if(!controllLEDByHeart(outputPath,ascii)){return false;}
sleep(1);
if(!controllLED(outputPath,0)){return false;}
if(!controllLED(timePath,0)){return false;}
sleep(1);
}
return true;
}
int main()
{
int lightStatus = 0;
char input[1000];
printf("Press any word (at most 999 characters)\n");
scanf("%s",input);
printf("Get %s, start encoding...\n",input);
if(blinkByAscii(GREEN_PATH,RED_PATH,input)){
printf("Finish!\n");
return 0;
}else{
printf("Error!\n");
return 1;
}
}