aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Chris Xiong <chirs241097@gmail.com> 2018-07-06 21:25:39 +0800
committerGravatar Chris Xiong <chirs241097@gmail.com> 2018-07-06 21:25:39 +0800
commitb5932d1d22d35cef95eb1cffbf489619b264442d (patch)
tree6040dc505e9b22875449bcc045fed9b0ac3f0692
parent61e0a974c07f38b8e42f226d68f22e2d1b26fa6d (diff)
downloadlightsd-b5932d1d22d35cef95eb1cffbf489619b264442d.tar.xz
Random code clean-ups to get ready for the next update.
Also tweaked the config a little bit.
-rw-r--r--README.md26
-rw-r--r--brightness_ctrl.cpp5
-rw-r--r--brightness_ctrl.hpp5
-rw-r--r--lightsd.conf4
-rw-r--r--main.cpp56
-rw-r--r--sensors.cpp6
-rw-r--r--utils.cpp12
-rw-r--r--utils.hpp2
8 files changed, 55 insertions, 61 deletions
diff --git a/README.md b/README.md
index 04d78ec..309e286 100644
--- a/README.md
+++ b/README.md
@@ -12,7 +12,8 @@ fifo so that you can adjust relative brightness of the lcd.
It only supports sensors using the industrial I/O bus. It has a generic
class for working with most types of iio devices though.
-The project also demostrates how damn stupid a C++ program could look like.
+The project also demostrates how damn stupid a C++ program could look like
+(not yet to its maximum extent).
Hopefully it does not yet hog the CPU.
@@ -46,11 +47,26 @@ Makes lcd x% darker.
Sets relative brightness of lcd.
- `r`
Resets relative brightness of lcd, equivalent to `s 0`.
-- `f`
+- `f`
Forces an adjustment to be made. You may want to call this when the lid
-is being opened.
+is being opened in order to turn on the keyboard backlight.
-The fifo is owned by `root:video` and has permission `0620` so that
+Not implemented:
+- `m`
+Disables automatic brightness. (\_M\_anual)
+Some of the commands (r/s/f) does nothing in manual brightness mode.
+- `a`
+Enables automatic brightness.
+- `i`
+Print current status to stdout. Example output:
+```
+Mode: <Automatic|Manual>
+ALS value: <%f|-->
+Display brightness: %d%% [(+%d%%)]
+Keyboard backlight brightness: %d%%
+```
+
+The fifo is owned by `root:video` and has permission `0220` so that
everyone in the video group could potentially mess with your brightness.
Surprise!
@@ -66,4 +82,4 @@ Surprise!
- use iio triggers instead?
- auto orientation using accelerometer?
- hogging cpu and battery?
- - ability to order pizza?
+ - ability to embed an email client and to order pizza?
diff --git a/brightness_ctrl.cpp b/brightness_ctrl.cpp
index 97f0149..f8e0407 100644
--- a/brightness_ctrl.cpp
+++ b/brightness_ctrl.cpp
@@ -5,13 +5,13 @@
#define log10_n(x) ((x)<1?0:log10(x))
void BrightnessControl::_brightness_slide(int p)
{
- //TODO: mutual exclusion
+ std::lock_guard<std::mutex> adjust_lck(adjust_m);
p+=offset;
if(p>100)p=100;
if(p<0)p=0;
int pbr=maxbr*p/100;
if(pbr<minabr)pbr=minabr;
- printf("brightness adjust: %d->%d/%d\n",br,pbr,maxbr);
+ LOG('I',"brightness adjust: %d->%d/%d\n",br,pbr,maxbr);
int d=1;if(pbr<br)d=-1;double dd=1;
while(d>0&&br+round(d*dd)<=pbr||d<0&&br+round(d*dd)>=pbr)
{
@@ -114,7 +114,6 @@ void BrightnessControl::worker()
--cur;lb=cur>0?thresh[cur-1]:0;
ub=thresh[cur];
}
- printf("%f lx\n",val);
brightness_slide(value[cur]);
}
}
diff --git a/brightness_ctrl.hpp b/brightness_ctrl.hpp
index 5beb0ec..c1e8a34 100644
--- a/brightness_ctrl.hpp
+++ b/brightness_ctrl.hpp
@@ -2,8 +2,9 @@
#define BRIGHTNESS_CTRL_HPP
#include <chrono>
#include <condition_variable>
-#include <experimental/filesystem>
+#include <filesystem>
#include <thread>
+#include <mutex>
#include <vector>
#include "sensor_als.hpp"
class BrightnessControl
@@ -14,7 +15,7 @@ private:
int delay,direction,br,maxbr,minabr,tr,offset;
size_t cur;
SensorALS *als;
- std::mutex interrupt_m,threshnotify_m;
+ std::mutex interrupt_m,threshnotify_m,adjust_m;
std::condition_variable interrupt,threshnotify;
void _brightness_slide(int p);
public:
diff --git a/lightsd.conf b/lightsd.conf
index 6b39c3f..7217a75 100644
--- a/lightsd.conf
+++ b/lightsd.conf
@@ -1,4 +1,4 @@
-#example configuration for lightsd
+#sample configuration for lightsd
#path to backlight control in sysfs
#expected files are max_brightness and brightness
@@ -30,7 +30,7 @@ lcd_backlight_trigger_range=2
lcd_backlight_min_value=1
#same as their lcd conterpart
-kbd_backlight_thresholds=100
+kbd_backlight_thresholds=40
kbd_backlight_values=50,0
kbd_backlight_control_delay=5
kbd_backlight_trigger_range=2
diff --git a/main.cpp b/main.cpp
index d46a73a..d1485b3 100644
--- a/main.cpp
+++ b/main.cpp
@@ -29,6 +29,14 @@ void als_callback(SensorBase* _s)
lcd.on_sensor_report(val);
kbd.on_sensor_report(val);
}
+std::vector<int> numlist2vec(std::string l)
+{
+ std::vector<std::string> v;
+ split(l,',',v);
+ std::vector<int> r;
+ for(auto&i:v)r.push_back(atoi(i.c_str()));
+ return r;
+}
void load_config()
{
fifo_path="/tmp/lightsd.cmd.fifo";
@@ -36,7 +44,7 @@ void load_config()
cfgf=fopen("/etc/lightsd.conf","r");
if(!cfgf)
cfgf=fopen("lightsd.conf","r");
- if(!cfgf){LOG('W',"Configuration file not found.",0);return;}
+ if(!cfgf)return (void)LOG('W',"Configuration file not found.",0);
char* buf=new char[1024];
while(!feof(cfgf))
{
@@ -48,41 +56,13 @@ void load_config()
if(sv.size()!=2)continue;
if(sv[0]=="lcd_backlight_control")lcd.set_path(sv[1]);
if(sv[0]=="kbd_backlight_control")kbd.set_path(sv[1]);
- if(sv[0]=="lcd_backlight_thresholds")
- {
- std::vector<std::string> vals;
- split(sv[1],',',vals);
- std::vector<int> t;
- for(auto&i:vals)t.push_back(atoi(i.c_str()));
- lcd.set_thresh(t);
- }
- if(sv[0]=="lcd_backlight_values")
- {
- std::vector<std::string> vals;
- split(sv[1],',',vals);
- std::vector<int> t;
- for(auto&i:vals)t.push_back(atoi(i.c_str()));
- lcd.set_value(t);
- }
+ if(sv[0]=="lcd_backlight_thresholds")lcd.set_thresh(numlist2vec(sv[1]));
+ if(sv[0]=="lcd_backlight_values")lcd.set_value(numlist2vec(sv[1]));
if(sv[0]=="lcd_backlight_control_delay")lcd.set_delay(atoi(sv[1].c_str()));
if(sv[0]=="lcd_backlight_trigger_range")lcd.set_trigrange(atoi(sv[1].c_str()));
if(sv[0]=="lcd_backlight_min_value")lcd.set_minabr(atoi(sv[1].c_str()));
- if(sv[0]=="kbd_backlight_thresholds")
- {
- std::vector<std::string> vals;
- split(sv[1],',',vals);
- std::vector<int> t;
- for(auto&i:vals)t.push_back(atoi(i.c_str()));
- kbd.set_thresh(t);
- }
- if(sv[0]=="kbd_backlight_values")
- {
- std::vector<std::string> vals;
- split(sv[1],',',vals);
- std::vector<int> t;
- for(auto&i:vals)t.push_back(atoi(i.c_str()));
- kbd.set_value(t);
- }
+ if(sv[0]=="kbd_backlight_thresholds")kbd.set_thresh(numlist2vec(sv[1]));
+ if(sv[0]=="kbd_backlight_values")kbd.set_value(numlist2vec(sv[1]));
if(sv[0]=="kbd_backlight_control_delay")kbd.set_delay(atoi(sv[1].c_str()));
if(sv[0]=="kbd_backlight_trigger_range")kbd.set_trigrange(atoi(sv[1].c_str()));
if(sv[0]=="kbd_backlight_min_value")kbd.set_minabr(atoi(sv[1].c_str()));
@@ -114,10 +94,10 @@ void setup_fifo()
{
if(fifo_path.empty())return;
int ret=0;
- ret|=unlink(fifo_path.c_str());
- ret|=mkfifo(fifo_path.c_str(),0620);
+ unlink(fifo_path.c_str());
+ ret|=mkfifo(fifo_path.c_str(),0220);
ret|=chown(fifo_path.c_str(),0,get_gid("video"));
- ret|=chmod(fifo_path.c_str(),0620);
+ ret|=chmod(fifo_path.c_str(),0220);
if(ret)LOG('W',"Failed to create fifo.",0);
}
void command_thread()
@@ -128,8 +108,7 @@ void command_thread()
while(1)
{
ignore_result(fgets(cmdbuf,256,fifo_f));
- printf("got command: ");
- puts(trim(cmdbuf).c_str());
+ LOG('I',"got command: %s",trim(cmdbuf).c_str());
std::vector<std::string> cav;
split(trim(cmdbuf),' ',cav);
if(cav.size()>=1)
@@ -169,7 +148,6 @@ int main()
if(als.init(als_id,"in_intensity"))return puts("Failed to initialize sensor."),1;
als.set_reader_callback(als_callback);
float init_val=als.get_value();
- //printf("initial value: %f lx\n",init_val);
load_config();
setup_fifo();
lcd.init(init_val,&als);
diff --git a/sensors.cpp b/sensors.cpp
index d2166be..615c784 100644
--- a/sensors.cpp
+++ b/sensors.cpp
@@ -124,16 +124,14 @@ bool SensorBase::init(int id,std::string _sensor_basename)
enable_buffer();
devfd=open(devbufpath.c_str(),O_RDONLY);
if(!~devfd)
- {
- LOG('E',"failed to open the iio buffer device: %s",devbufpath.c_str());
- return 1;
- }
+ return LOG('E',"failed to open the iio buffer device: %s",devbufpath.c_str()),1;
return 0;
}
void SensorBase::deinit()
{
if(~devfd)close(devfd);
devfd=-1;
+ //...also disable iio buffers?
}
void SensorBase::reset()
{
diff --git a/utils.cpp b/utils.cpp
index a661794..135dd0a 100644
--- a/utils.cpp
+++ b/utils.cpp
@@ -23,15 +23,17 @@ float readfloat(const char* path)
fclose(f);
return atof(buf);
}
-std::string readstr(const char* path)
+std::string readstr(const char* path,int max_length)
{
FILE* f=fopen(path,"r");
if(!f)return LOG('W',"failed to open %s for reading: %d",path,errno),"";
- char buf[256];
- ignore_result(fgets(buf,256,f));
- buf[255]=0;
+ char* buf=new char[max_length+1];
+ ignore_result(fgets(buf,max_length+1,f));
+ buf[max_length]=0;
fclose(f);
- return std::string(buf);
+ std::string ret(buf);
+ delete buf;
+ return ret;
}
void writeint(const char* path,int v)
{
diff --git a/utils.hpp b/utils.hpp
index eac0c8f..10a3d5f 100644
--- a/utils.hpp
+++ b/utils.hpp
@@ -5,7 +5,7 @@
#define LOG(type,format,...) fprintf(stderr,"%c: " format "\n",type,__VA_ARGS__)
int readint(const char* path);
float readfloat(const char* path);
-std::string readstr(const char* path);
+std::string readstr(const char* path,int max_length=256);
void writeint(const char* path,int v);
std::string trim(std::string s);
void split(std::string s,char c,std::vector<std::string>& v);