From a7d900ed7b04debe907c868a07d2d4f4bb229ae1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Szczodrzy=C5=84ski?= Date: Thu, 8 Feb 2024 22:44:39 +0100 Subject: [PATCH] Support brightness control on Linux fbdev --- src/dev/posix/hasp_posix.cpp | 27 ++++++++++++++++++++++++++- src/dev/posix/hasp_posix.h | 4 ++++ src/main_pc.cpp | 23 +++++++++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/src/dev/posix/hasp_posix.cpp b/src/dev/posix/hasp_posix.cpp index ef4fc10b..3b0c7566 100644 --- a/src/dev/posix/hasp_posix.cpp +++ b/src/dev/posix/hasp_posix.cpp @@ -25,6 +25,7 @@ #include "display/fbdev.h" #endif +#include #include // extern monitor_t monitor; @@ -153,7 +154,31 @@ void PosixDevice::update_backlight() #if USE_MONITOR monitor_backlight(level); #elif USE_FBDEV - // set display backlight, if possible + // set display backlight, if possible + if(backlight_device != "") { + if(backlight_max == 0) { + std::ifstream f; + f.open("/sys/class/backlight/" + backlight_device + "/max_brightness"); + if(!f.fail()) { + f >> backlight_max; + f.close(); + } else { + perror("Max brightness read failed"); + } + } + + int brightness = map(level, 0, 255, 0, backlight_max); + LOG_VERBOSE(0, "Setting brightness to %d/255 (%d)", level, brightness); + + std::ofstream f; + f.open("/sys/class/backlight/" + backlight_device + "/brightness"); + if(!f.fail()) { + f << brightness; + f.close(); + } else { + perror("Brightness write failed"); + } + } #endif } diff --git a/src/dev/posix/hasp_posix.h b/src/dev/posix/hasp_posix.h index 73a8a49c..4ec64458 100644 --- a/src/dev/posix/hasp_posix.h +++ b/src/dev/posix/hasp_posix.h @@ -56,6 +56,10 @@ class PosixDevice : public BaseDevice { bool is_system_pin(uint8_t pin) override; + public: + std::string backlight_device; + int backlight_max = 0; + private: std::string _hostname; std::string _core_version; diff --git a/src/main_pc.cpp b/src/main_pc.cpp index c6190c7a..ad851d1b 100644 --- a/src/main_pc.cpp +++ b/src/main_pc.cpp @@ -119,6 +119,10 @@ void usage(const char* progName, const char* version) << " (default: 'AppData\\hasp\\hasp')" << std::endl #elif defined(POSIX) << " (default: '~/.local/share/hasp/hasp')" << std::endl +#endif +#if USE_FBDEV && defined(POSIX) + << " -b | --backlight Backlight device name (in /sys/class/backlight/)" << std::endl + << " -B | --bl-max Backlight brightness limit (default: max_brightness)" << std::endl #endif << std::endl; fflush(stdout); @@ -138,6 +142,25 @@ int main(int argc, char* argv[]) for(int arg = 1; arg < argc; arg++) { if(strncmp(argv[arg], "--help", 6) == 0 || strncmp(argv[arg], "-h", 2) == 0) { showhelp = true; +#if USE_FBDEV && defined(POSIX) + } else if(strncmp(argv[arg], "--backlight", 11) == 0 || strncmp(argv[arg], "-b", 2) == 0) { + if(arg + 1 < argc) { + haspDevice.backlight_device = argv[arg + 1]; + arg++; + } else { + std::cout << "Missing device name" << std::endl; + showhelp = true; + } + } else if(strncmp(argv[arg], "--bl-max", 9) == 0 || strncmp(argv[arg], "-B", 2) == 0) { + if(arg + 1 < argc) { + int bl_max = atoi(argv[arg + 1]); + if(bl_max > 0) haspDevice.backlight_max = bl_max; + arg++; + } else { + std::cout << "Missing backlight level" << std::endl; + showhelp = true; + } +#endif } else if(strncmp(argv[arg], "--width", 7) == 0 || strncmp(argv[arg], "-W", 2) == 0) { if(arg + 1 < argc) { int w = atoi(argv[arg + 1]);