Backlight

My goal was twofold: 1) Stay awake on a conference call and 2) Adjust the backlight of my display to match the ambient light of my room.  Writing this script solved both problems, fortunately.  My system has two webcams attached to it.  The first shows up as /dev/video0 and is the one built in to the lid of my laptop display.  Since my laptop is docked when I use the LG display, all this device will register is a black picture -- not very helpful.  Fortunately I had a logitech webcam lying around and a spare USB port on my dock connector, so I positioned the webcam on the TV pedestal -- hoping it might come in handy with Google+ or Skype.

Using the tv.sh script (described on the main LG TV hacks page), and some other script-fu, I have been able to cobble together this script; It measures the ambient light (the mean brightness of the room averaged from 5 samples) and calculates an appropriate backlight setting for the TV.  When the room is very dark (or when I put my thumb over the cam), it drops down to 20.  When the room is very bright (especially if there is a window open behind me in the daytime), the backlight goes up to 100.  Under normal lighting conditions it usually hovers in the 70-80 range.  To avoid annoyance, I had the script only set the level by multiples of 5 since minute differences just cause an OSD to pop up on the TV with no measurable change in actual brightness.  Oh, and one more thing to mention -- to match my own perceptual demands (read: I was being kinda arbitrary), I calculate the level based on an exponential curve.  Once you get this script working to your liking, you can assign a hotkey to call it (Keyboard Shortcuts in the settings menu for Ubuntu users) or via Cron.  I run it every 5 minutes.

You'll probably need to adjust this depending on what your actual webcam device is -- mine is /dev/video1

adjustBacklight.sh:

#!/bin/bash

function gt {

$(echo $1 $2|awk '{exit ($1 > $2)}') || echo OK

}

# If TV is off, don't bother with this...

tvStatus=$(~/tv.sh ka ff 2>/dev/null)

if [ "$tvStatus" != "01" ]; then

exit 0;

fi

#Figure out what camera to use

#Note: You might want to modify this and hard-code your actual camera location

cam=/dev/video0

if [ -e /dev/video1 ]; then

cam=/dev/video1

fi

#The negative value for min helps raise the lower shelf for the brightness level

#(I really don't want it lower than 20 -- you can adjust to your own taste)

min=-0.25

# If you raise the max value, you're raising the value of mean brightness is considered "bright"

# This might vary in your environment or with your webcam.

# If your TV always seems too bright, raise this value.

max=0.45

samples=5

values=""

#Take an averate of five brightness readings (to prevent too much fluctuation)

for i in {1..5}; do

fswebcam -d $cam -q --no-banner --no-info --no-underlay --greyscale --save /tmp/sample.jpg

brightness=`echo "$(convert /tmp/sample.jpg -verbose info:)" |\

       sed -n '/^.*mean:.*[(]\([0-9.]*\).*$/{ s//\1/; p; q; }'`

rm /tmp/sample.jpg

if [ $(gt $brightness $max) ]; then

brightness=$max

elif [ $(gt $min $brightness) ]; then

brightness=$min

fi

values=$(echo $values $brightness)

sleep 2s

done

average=$(echo $values|awk '{print ($1+$2+$3+$4+$5)/5}')

#Turn the brightness to a percentage based on the range of min and max

normalized=$(echo $average $min $max|awk '{print ($1-$2)/($3-$2)}')

#This converts the final percentage back to the range of 0-100 

#(rounded to the nearest multiple of 5) and outputs in hex

value=$(echo $normalized|awk '{printf "%X",int(((exp($1)-1)*58.2)/5)*5}')

#echo Value is $value

ignore=$(~/tv.sh -s mg $value)

#Quickly hide OSD so it's not too annoying.

ignore=$(~/tv.sh mc 5b)