-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtoggleflash.cpp
65 lines (52 loc) · 1.42 KB
/
toggleflash.cpp
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
62
63
64
65
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Author: Zachary Schroeder
* Created: July 31 2020
*/
#include <iostream>
#include <stdio.h>
#include <string>
#include <unistd.h>
#include <cstring>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
//reads file and converts to int
int ftoi(char* path)
{
FILE *f = fopen(path, "r");
char buffer[10];
if(f == NULL)
{
return NULL;
}
if(fgets(buffer, 10, f) == NULL)
{
return NULL;
}
fclose(f);
return std::stoi(buffer);
}
int main()
{
bool state = ftoi("/sys/devices/platform/led-controller/leds/white:flash/brightness"); //current flashlight state
char brightness[2];
sprintf(brightness, "%d", !state);
int fd = open("/sys/devices/platform/led-controller/leds/white:flash/brightness", O_RDWR);
if(fd != NULL)
{
write(fd, brightness, strlen(brightness));
}
close(fd);
}