根据日出和日落自动调整显示亮度


10

Ask Ubuntu中也有类似的问题和答案,它们指向这些解决方案:

但它们要么需要手动输入日出和日落时间,要么仅调整色彩平衡并在晚上添加更多红色。

我正在寻找一种可以每天自动获取日出和日落时间并实际调整显示屏亮度的解决方案。此外,我想要60到120分钟的过渡效果,因此我不会注意到显示的变化。

在阴天(外面较暗)或当太阳刚好落在两栋三层楼的建筑物之间而不是屋顶(在日落之前更亮)之间进行重新配置应该是一个快速,容易的过程。

Answers:


7

从2019年起更新的帖子-令人讨厌

Eyesome是一个bash脚本,它作为守护进程运行,并且大部分时间都是24/7休眠。它可以通过硬件接口和最多两个其他的显示器(使用xrandr的软件控制)为笔记本电脑显示器自动调整屏幕亮度(也可以选择伽玛)。

日出时(每天自动从互联网获取时间),您的屏幕亮度(以及可选的伽玛值)会逐渐调整。逐步调整由您定义,但120分钟对我有效。为使调整不明显,请在调整之间设置睡眠间隔。15到60秒之间的任何时间可能都是最好的,默认值为60。

令人讨厌的守护程序会休眠许多小时,直到日落过渡开始。与日出过渡相反,日落过渡会逐渐降低屏幕亮度(以及可选的gamma值),因此它并不明显。建议在日落前90分钟,但您可以设置自己喜欢的任何时间。

在日落过渡期间,可以将伽玛定义为增加。例如,红色伽玛可以定义为白天1.0和夜晚1.2,以减少眼睛疲劳。依次将蓝色伽玛在白天定义为1.0,在夜晚将定义为0.8,因此它将降低。

为了减少资源,眼睛组会在日落和日出之间的整个时间段内睡眠。从挂起或热插拔外部监视器恢复时有一次例外。这取决于您的居住地和一年中的季节,但是平均睡眠时间为12小时。

您可以从Github下载Eyesome


2017年的原始帖子

介绍

这是一个bash解决方案,无需安装其他程序。它需要Internet连接(如果您正在阅读,则有一个连接)才能每天自动获取日出和日落时间。它提供了一个用户友好的配置屏幕。它会自动启动,cron并且不需要用户交互。

这个答案分为多个部分

  • 每天自动获取日出和日落时间 cron
  • 配置亮度设置和过渡时间
  • 主脚本可根据一天中的时间调整亮度
  • 每次启动时都会自动启动display-auto-brightness cron
  • 从暂停状态恢复即时/透明亮度调整
  • 摘要

每天使用cron自动获取日出和日落的时间

有许多网站可以报告您所在位置的日出和日落时间。该脚本使用(https://www.timeanddate.com/),该网站已经有很长时间了。

使用sudo powers编辑文件/usr/local/bin/sun-hours并将其粘贴:

#!/bin/bash

# Called daily from /etc/cron.daily/sun-hours
while true; do

### "-q"= quiet, "-O-" pipe output
echo $(wget -q -O- https://www.timeanddate.com/sun/canada/edmonton | grep -oE 'Sunrise Today.{35}' | awk -F\> '{print $3}' | tr --delete "<") > /tmp/sunrise
echo $(wget -q -O- https://www.timeanddate.com/sun/canada/edmonton | grep -oE 'Sunset Today.{35}' | awk -F\> '{print $3}' | tr --delete "<") > /tmp/sunset

## If network is down files will have one byte size
size1=$(wc -c < /tmp/sunrise)
size2=$(wc -c < /tmp/sunset)

if [ $size1 -gt 1 ] && [ $size2 -gt 1 ] ; then
    cp /tmp/sunrise /usr/local/bin/sunrise
    cp /tmp/sunset  /usr/local/bin/sunset
    chmod 666 /usr/local/bin/sunrise
    chmod 666 /usr/local/bin/sunset
    rm /tmp/sunrise
    rm /tmp/sunset
    exit 0
else
    logger "/etc/cron.daily/sun-hours: Network is down. Waiting 5 minutes to try again."
    sleep 300
fi

done

保存脚本之前,请/canada/edmonton用您自己的国家和城市替换两次出现的。访问该网站www.timeanddate.com以获取确切的名称或号码。例如,“美国巴黎”有数十个名称,因此它们将包含一个数字,例如/worldclock/@5205082“美国宾夕法尼亚州巴黎”。

每天使用cron运行星期日脚本

使用sudo powers编辑文件/etc/cron.daily/sun-hours并将其粘贴:

#!/bin/sh
#
# Each day /etc/cron.daily/sun-hours will get sunrise and sunset times.

sleep 60 # give time for network to come up.
/usr/local/bin/sun-hours

将两个文件都标记为可执行文件,sudo chmod a+x ___/sun-hours其中“ ___”是每个文件的目录。

使用Conky监控每天的日出/日落时间变化

Conky是用于监视系统的流行工具。以下是用于显示日出,日落和亮度设置的命令:

${color orange}${voffset 2}${hr 1}
${color}${goto 5}Day: ${color green}${execpi 300 cat /usr/local/bin/sunrise}  ${color}Night: ${color green}${execpi 300 cat /usr/local/bin/sunset}  ${color}Level: ${color green}${execpi 10 cat cat /sys/class/backlight/intel_backlight/brightness}
${color orange}${voffset 2}${hr 1}

看起来是这样的:

显示自动亮度conky

注意:此Conky代码使用的intel_backlight内容需要根据您的显示适配器进行更改,如下所述。

配置亮度设置和过渡时间

您不希望显示器在日出时变为全亮度,而在日落时变为全暗。需要一个过渡期。另外,还需要一个配置文件来记录用于全亮度和全暗的变量。这是配置屏幕:

显示自动亮度配置

该屏幕是使用标准的zenity命令构建的,因此,现有值将显示在带有“(值)”标签的标签中,并在需要时键入新值。否则,当新值空白时,将保留现有文件。

intel_backlight如果不同,请替换为显示驱动程序。要查找您的姓名,请使用:ls /sys/class/backlight/*/brightness

使用sudo powers创建文件/usr/local/bin/auto-brightness-config并粘贴以下代码:

#!/bin/bash

# Read hidden configuration file with entries separated by " " into array
IFS=' ' read -ra CfgArr < /usr/local/bin/.auto-brightness-config

# Zenity form with current values in entry label
# because initializing multiple entry data fields not supported
output=$(zenity --forms --title="Display Auto Brightness Configuration" \
        --text="Enter new settings or leave entries blank to keep (existing) settings" \
   --add-entry="/sys/class/backlight/??????/brightness driver : (${CfgArr[0]})" \
   --add-entry="Day time maximum display brightness : (${CfgArr[1]})" \
   --add-entry="Transition minutes after sunrise to maximum : (${CfgArr[2]})" \
   --add-entry="Night time minimum display brightness : (${CfgArr[3]})" \
   --add-entry="Transition minutes before sunset to minimum : (${CfgArr[4]})")

IFS='|' read -a ZenArr <<<$output # Split zenity entries separated by "|" into array elements

# Update non-blank zenity array entries into configuration array
for i in ${!ZenArr[@]}; do
    if [[ ${ZenArr[i]} != "" ]]; then CfgArr[i]=${ZenArr[i]} ; fi
done

# write hidden configuration file using array (fields automatically separated by " ")
echo "${CfgArr[@]}" > /usr/local/bin/.auto-brightness-config

使用以下命令将文件标记为可执行文件:

chmod a+x /usr/local/bin/auto-brightness-config

主程序 display-auto-brightness

使用sudo powers创建文件/usr/local/bin/display-auto-brightness并粘贴以下代码:

#!/bin/bash

# NAME: display-auto-brightness
# PATH: /usr/local/bin
# DESC: Set display brightness based on min/max values, sun rise/set time
#       and transition minutes.
# CALL: Called from cron on system startup with @reboot option.
# DATE: Feb 17, 2017. Modified: Dec 2, 2017.

# NOTE: Only sleep for 1 minute to make display brightness changes gradual.
#       Configuration file can change any time so variables reread when waking.
#       The file is maintained by /usr/local/bin/auto-brightness-config script.

#       Sunrise and sunset time obtained from www.dateandtime.com using
#       /usr/local/bin/sun-hours script. The script is called from cron using
#       /etc/cron.daily/cron-daily-sun-hours. Variables stored in am/pm format
#       in /usr/local/bin/sunrise and /usr/local/bin/sunset.

#       When suspending at 6 am it might be dark with setting at 300. When 
#       resuming at 4:30pm it might be full sun and setting needs to be 2000.
#       It will take between 1 and 59 seconds to adjust screen brightness with
#       sudden jump in brightness. To correct this create kill sleep command
#       during suspend with /lib/systemd/system-sleep/display-auto-brightness
#       script.

# TODO: Add support for external monitors connected via HDMI / DisplayPort.
#       xrandr --output HDMI-0  --brightness .799
#       xrandr --output DP-1-1  --brightness 1.15

if [[ $(id -u) != 0 ]]; then # root powers needed to call this script
    echo display-auto-brightness must be called with sudo powers
    exit 1
fi

# global variable
LastSetting=""

function set-and-sleep {
    if [[ "$1" != "$LastSetting" ]]; then
        sudo sh -c "echo $1 | sudo tee $backlight"
        echo "$1" > "/tmp/display-current-brightness"
        LastSetting="$1"
    fi
    sleep 60
}  

re='^[0-9]+$'   # regex for valid numbers

function calc-level-and-sleep {
    # Parms $1 = number of minutes for total transition
    #       $2 = number of seconds into transition

    secTotal=$(( $1 * 60 )) # Convert total transition minutes to seconds
    Adjust=$( bc <<< "scale=6; $transition_spread * ( $2 / $secTotal )" )
    Adjust=$( echo $Adjust | cut -f1 -d"." ) # Truncate number to integer

    if ! [[ $Adjust =~ $re ]] ; then
        Adjust=0   # When we get to last minute $Adjust can be non-numeric
    fi

    calc_bright=$(( $min_bright + $Adjust ))
    set-and-sleep "$calc_bright"
}


while true ; do

    # Although variables change once a day it could be weeks between reboots.
    sunrise=$(cat /usr/local/bin/sunrise)
    sunset=$(cat /usr/local/bin/sunset)

    # Read hidden configuration file with entries separated by " " into array
    IFS=' ' read -ra CfgArr < /usr/local/bin/.auto-brightness-config
    backlight="/sys/class/backlight/${CfgArr[0]}/brightness"
    max_bright="${CfgArr[1]}"
    after_sunrise="${CfgArr[2]}"
    min_bright="${CfgArr[3]}"
    before_sunset="${CfgArr[4]}"

    # Current seconds
    secNow=$(date +"%s")
    secSunrise=$(date --date="$sunrise today" +%s)
    secSunset=$(date --date="$sunset today" +%s)

    # Is it night time?
    if [ "$secNow" -gt "$secSunset" ] || [ "$secNow" -lt "$secSunrise" ]; then
        # MINIMUN: after sunset or before sunrise nightime setting
        set-and-sleep "$min_bright"
        continue
    fi

    # We're somewhere between sunrise and sunset
    secMaxCutoff=$(( $secSunrise + ( $after_sunrise * 60 ) ))
    secMinStart=$((  $secSunset  - ( $before_sunset * 60 ) ))

    # Is it full bright day time?
    if [ "$secNow" -gt "$secMaxCutoff" ] && [ "$secNow" -lt "$secMinStart" ]; then
        # MAXIMUN: after sunrise transition AND before nightime transition
        set-and-sleep "$max_bright"
        continue
    fi

    # Daytime - nightime = transition brightness levels
    transition_spread=$(( $max_bright - $min_bright ))

    # Are we between sunrise and full brightness?
    if [ "$secNow" -gt "$secSunrise" ] && [ "$secNow" -lt "$secMaxCutoff" ]; then
        # Current time - Sunrise = progress through transition
        secPast=$(( $secNow - $secSunrise ))
        calc-level-and-sleep $after_sunrise $secPast
        continue
    fi

    # Are we between beginning to dim and sunset (full dim)?
    if [ "$secNow" -gt "$secMinStart" ] && [ "$secNow" -lt "$secSunset" ]; then
        # Sunset - Current time = progress through transition
        secBefore=$(( $secSunset - $secNow ))
        calc-level-and-sleep $before_sunset $secBefore
        continue
    fi

    # At this stage brightness was set with manual override outside this program
    # or exactly at a testpoint, then it will change next minute so no big deal.
    sleep 60 # reset brightness once / minute.

done # End of forever loop

使用以下命令将文件标记为可执行文件:

chmod a+x /usr/local/bin/display-auto-brightness

注意:未成功修订,于2017年8月26日更正了以下错误:在上一个亮度水平等于最小或最大亮度且当前分钟的调整值为空白(零)时,程序将停止的错误。已成功修复2017年12月2日,但直到2018年2月17日才发布。糟糕!

每次启动时都会自动启动display-auto-brightness cron

Cron用于在每次引导时启动主应用程序。创建/etc/cron.d/display-auto-brightness具有sudo权限的文件并在其中复制:

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
@reboot   root    /usr/local/bin/display-auto-brightness

从暂停状态恢复即时亮度调节

您可以在太阳升起且设置为300之前的上午6点使计算机暂停。您可以在太阳明亮且设置为2000的下午4点恢复计算机,但必须等待1到59秒才能display-auto-brightness恢复重置显示。当显示屏确实重置时,更改会非常明显。为了解决这个问题,systemd需要一个脚本。创建/lib/systemd/system-sleep/display-auto-brightness包含以下内容的文件:

#!/bin/sh

# NAME: display-auto-brightness
# PATH: /lib/systemd/system-sleep/
# DESC: Restart display brightness when resuming from suspend
# CALL: Automatically called when system goes to sleep and wakes up
# DATE: August 2017. Modified: June 10, 2018.

# NOTE: Gives instant display brightness adjustment instead of waiting 1 to 59 seconds.

logger -t "logger -t "test" "\$0=$0, \$1=$1, \$2=$2" test" "\$0=$0, \$1=$1, \$2=$2"

case $1/$2 in
  pre/*)
    echo "/lib/systemd/system-sleep/display-auto-brightness: Going to $2..."
    ;;
  post/*)
    # March 28, 2018 On AW17R3 this script runs too fast
    sleep 2

    echo "/lib/systemd/system-sleep/display-auto-brightness: Resuming from $2..."

    # Find running tree processes containing "display-auto" AND "sleep"
    ProgramTree=$(pstree -g -p | grep display-auto | grep sleep)
    # echo's below will print in /var/log/syslog. Comment out with # to suppress
    echo "pstree -g -p | grep display-auto | grep sleep"
    echo "============================================="
    echo "$ProgramTree"

    # extract sleep program ID within `pstree`. eg we would want "16621" below:
    # |-cron(1198,1198)---cron(1257,1198)---sh(1308,1308)---display-auto-br(1321,1308)---sleep(16621,1308)
    pID=$(echo "$ProgramTree" | cut -f 6 -d '(' )
    pID=$(echo "$pID" | cut -f1 -d",")

    kill $pID  # kill sleep command forcing screen brightness to adjust immediately
    rm /tmp/display-current-brightness
    echo "display-auto-brightness: sleep pID: '$pID' has been killed."
    ;;
esac

使用以下命令将文件标记为可执行文件:

chmod a+x /lib/systemd/system-sleep/display-auto-brightness

摘要

显示当前亮度等级为系统托盘

除了前面介绍的Conky示例之外,indicator-sysmonitor还可以通过bash在系统托盘中显示当前亮度:

显示自动亮度系统

在此系统托盘示例中,亮度设置为418,大约在日落前20分钟。日落时该值为250,白天全亮度为1200。在这台机器上可以使用intel_backlight = 4882,但是,如果您在室内打开窗帘的情况下,屏幕就像凝视着阳光。

使用以下代码设置系统托盘指示器:

#!/bin/bash

if [ -f ~/.lock-screen-timer-remaining ]; then
    text-spinner
    Spinner=$(cat ~/.last-text-spinner) # read last text spinner used
    Minutes=$(cat ~/.lock-screen-timer-remaining)
    systray=" $Spinner Lock screen in: $Minutes"
else
    systray=" Lock screen: OFF"
fi

if [ -f /tmp/display-current-brightness ]; then
    Brightness=$(cat /tmp/display-current-brightness)
    systray="$systray Brightness: $Brightness"
else
    systray="$systray Brightness: OFF"
fi

echo "$systray" # sysmon-indidicator will put echo string into systray for us.

exit 0

此问答(BASH能否在systray中显示为应用程序指示符?)描述了如何设置indicator-sysmonitor。

未来增强

  • 将bash脚本迁移到github的此答案
  • 使用以下方法添加对外部显示器的支持 xrandr
  • 添加对使用更多红色/更少蓝色的支持 xrandr

很棒的帖子,但是您能帮助我如何进入屏幕以配置转换等吗?
Marek Urbanowicz

@MU感谢您的友好评论。我正在使用几个月前编写的Suspend / Resume更改来修订文档。另外,我将很快编写WSL(Linux的Windows子系统)混合代码。我也想在新文档中解决您的问题。您在创建/usr/local/bin/auto-brightness-config脚本时遇到问题吗?您在确定/sys/class/backlight/??????/brightness驱动程序名称时遇到麻烦吗?我想使它成为一个有用的工具,您的问题很重要。
WinEunuuchs2Unix

我只是不知道基于您的帖子,如何获得显示自动亮度配置
Marek Urbanowicz

@MU将上述代码复制/usr/local/bin/auto-brightness-config/usr/local/bin名为的文件中的目录中auto-brightness-config。然后从终端将其标记为可执行chmod a+x /usr/local/bin/auto-brightness-config。然后从终端运行脚本/usr/local/bin/auto-brightness-config。您可以设置脚本的桌面快捷方式,以避免打开终端并键入脚本名称。
WinEunuuchs2Unix

1
@dessert我终于建立一个GitHub的页面eyesome的下一个化身display-auto-brightnessgithub.com/WinEunuuchs2Unix/eyesome将会在文档和改进对接口未来几周内频繁更新。
WinEunuuchs2Unix

3

我只是写了一个程序,几乎可以完成您想要的。它根据网络摄像头拍摄的图像更改屏幕的亮度,以获取周围的亮度。您可以自己选择间隔来检测亮度和调光速度。

https://github.com/Wandersalamander/Dimmer


1
对于大多数人来说,这听起来像是个好主意,除了我们这些人,他们在网络摄像
头上

1
@ WinEunuuchs2Unix即使对于“关注”的人来说,在凸轮上贴上胶带也可能很棒!您无需用黑色胶带遮盖网络摄像头即可使捕获的图像变得无用-一张白纸可以实现几乎相同的匿名效果,并且仍然保留了相机测量房间亮度的能力。实际上,纸片实际上可以改善它,因为它可以更均匀地测量实际入射到照相机的光量,如果照相机在它的正上方,则声音与入射到监视器屏幕的光量大致相同。=)
ViníciusM,


0

如果您无法访问Internet,则可以使用hdate:

hdate -s -l N50 -L E14 -z2

输出:

Wednesday, 26 June 2019, 23 Sivan 5779

sunrise: 04:55

sunset: 21:17

-s日落日出

-l -L布拉格的高度和纬度(50°05′N 14°25′E)

-z区域SELC = + 2

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.