-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathali.cpp
More file actions
113 lines (104 loc) · 3.11 KB
/
ali.cpp
File metadata and controls
113 lines (104 loc) · 3.11 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <iostream>
#include <fstream>
#include <chrono>
#include <thread>
#include <boost/circular_buffer.hpp>
#include <cmath>
#include <regex>
using namespace std;
using namespace boost;
const static char * const ENABLE_ALI = "/sys/devices/LNXSYSTM:00/LNXSYBUS:00/ACPI0008:00/enable";
const static char * const BRIGHT = "/sys/devices/pci0000:00/0000:00:02.0/drm/card0/card0-eDP-1/intel_backlight/brightness";
const static char * const ALI = "/sys/devices/LNXSYSTM:00/LNXSYBUS:00/ACPI0008:00/ali";
const static char * const AC = "/sys/bus/acpi/drivers/ac/ACPI0003:00/power_supply/AC0/online";
const static char * const LID = "/proc/acpi/button/lid/LID/state";
const static regex LIDOPEN {"open"};
const static size_t samples = 10;
const static auto interval = std::chrono::seconds(4);
const static int MAX_ILLU = 6407;
const static int MIN_BRIGHT = 80;
const static int MIN_AC_BRIGHT = 250;
const static int MAX_BRIGHT = 937;
const static double cooldown = .95;
const static double exponent = 0.3;
static bool getLidStatus() {
ifstream lid(LID);
string lidStatus;
getline(lid, lidStatus);
return regex_search(begin(lidStatus), end(lidStatus), LIDOPEN);
}
static int getAli() {
ifstream ali(ALI);
int res;
ali >> res;
return res;
}
static int getBrightness() {
ifstream brightness(BRIGHT);
int res;
brightness >> res;
return res;
}
static void setBrightness(int value) {
ofstream brightness(BRIGHT);
brightness << value;
}
static int min_bright() {
ifstream ac(AC);
bool connected;
ac >> connected;
if(connected)
return MIN_AC_BRIGHT;
else
return MIN_BRIGHT;
}
static double compute(double illumination) {
double min = min_bright();
return min + (MAX_BRIGHT-min)*pow(illumination/MAX_ILLU, exponent);
}
int main() {
ios::sync_with_stdio(false);
{
ofstream enable(ENABLE_ALI);
enable << 1 << endl;
}
circular_buffer<double> buf(samples);
double average = 0;
int expected = -1;
double over = 0, inserted = 0;
while(1) {
over *= cooldown;
if(getLidStatus()) {
double value = compute(getAli());
int old = getBrightness();
if(expected != -1 && old != expected) {
over = 1;
inserted = old;
buf.clear();
}
value = over*inserted + (1-over)*value;
if(buf.size() == samples) {
average -= buf.back()/samples;
buf.pop_back();
} else {
average *= buf.size();
average /= buf.size() + 1;
}
buf.push_front(value);
average += value / buf.size();
} else if(buf.size()) {
double out = *buf.rbegin();
if(buf.size() != 1) {
average -= out / buf.size();
average *= buf.size();
average /= buf.size()-1;
buf.pop_back();
}
}
if(buf.size()) {
expected = round(average);
setBrightness(expected);
}
this_thread::sleep_for( interval );
}
}