gkaindl / ambi-tv

a flexible ambilight clone for embedded linux
296 stars 85 forks source link

Infrared control instead of hardware button #36

Open mrwolf78 opened 10 years ago

mrwolf78 commented 10 years ago

Is it possible to control this setup with a infrared remote like the Logitech Harmony series? It would be perfect to switch it on and off from the couch!

I have no coding skills, but if someone has this working, please share it!

iLLiac4 commented 10 years ago

First RPi can not be powered off. If you want to turn off just deamon then it should be possible with lirc and ir receiver. For reference use something like this (just an idea): http://noods.wordpress.com/tag/lirc/ (to run .sh script with lirc) http://forum.stmlabs.com/showthread.php?tid=5549

Maybe try Hyperion and Raspbmc because gpio modules are already included in raspbmc release. And stop the xbmc from autoruning.

c6opuc commented 10 years ago

I just finished soldering TSOP4838 and configuring lirc. The step-by-step info how to create IR receiver is very well described here http://forum.stmlabs.com/showthread.php?tid=5549. The only what was missing there:

1) To start lircd on boot add something like this to your /etc/rc.local: /usr/sbin/lircd -d /dev/lirc0

2) In order to control ambilight modes I created /etc/lirc/lircrc file with the following content: begin remote = prog = irexec button = KEY_RED config = pkill ambi end begin remote = prog = irexec button = KEY_GREEN config = pkill ambi; /usr/bin/ambi-tv -p 2 & end begin remote = prog = irexec button = KEY_YELLOW config = pkill ambi; /usr/bin/ambi-tv -p 1 & end begin remote = prog = irexec button = KEY_BLUE config = pkill ambi; /usr/bin/ambi-tv & end 3) Start irexec during boot by adding this line to the /etc/rc.local: /usr/bin/irexec -d /etc/lirc/lircrc

As a result I can turn-off ambilight by pressing Red button on my remote and I can switch between various modes such as mood-light, avg_color and edge_color by pressing Blue, Green and Yellow buttons... Works like a charm! I also added one electrical relay to power-on my ambilight whenever I turn the monitor on. The relay is getting the control from the usb port of the monitor. When monitor is switched off, there is no power supplied to the usb port thus relay keeps the ambilight off completely. Now my ambi project is complete! :-)

Thanks to the author for this awesome idea!!!

mensler commented 10 years ago

I did a similar configuration which works great except for one thing: Killing the moodlight program with "pkill ambi" kills the process, but doesn't turn off the LEDs. I also tried "pkill -2 ambi" which should be equivalent to pressing Ctrl+C. Can you turn off the moodlight correctly with your remote?

c6opuc commented 10 years ago

Well it works for me.... The only difference i can think of is that I use screen utility to start ambi..... The LEDs go off when I kill it.... On 7 Dec 2013 13:46, "Mensler" notifications@github.com wrote:

I did a similar configuration which works great except for one thing: Killing the moodlight program with "pkill ambi" kills the process, but doesn't turn off the LEDs. I also tried "pkill -2 ambi" which should be equivalent to pressing Ctrl+C. Can you turn off the moodlight correctly with your remote?

— Reply to this email directly or view it on GitHubhttps://github.com/gkaindl/ambi-tv/issues/36#issuecomment-30054244 .

mensler commented 10 years ago

Interesting. What's the benefit of starting ambi-tv in screen?

c6opuc commented 10 years ago

I have suspicion that ambi-tv requires a terminal to work correctly. Without terminal I had couple of very strange side-effects. Besides that if I run via screen I can always attach to the terminal where ambi runs and see the output or control using keyboard. Here is how I run it: /usr/bin/screen -d -m /usr/bin/ambi-tv -p 1

On Sun, Dec 8, 2013 at 1:11 AM, Mensler notifications@github.com wrote:

Interesting. What's the benefit of starting ambi-tv in screen?

— Reply to this email directly or view it on GitHubhttps://github.com/gkaindl/ambi-tv/issues/36#issuecomment-30071236 .

mensler commented 10 years ago

Good idea, seems better than starting a background process with &. Works great for me though my moodlight problem persists, but as I'm not really using it I can ignore it for now.

Another thing I did today: I wrote a script for editing config options via remote. With the up and down buttons I can now increase or decrease the top/bottom crop values, same with the left and right buttons regarding the left and right crop values. With the volume up/down buttons the autocrop-luminance-threshold values can be increased/decreased. Now I can compensate bad black levels or small black bars of some TV channels. The config file is restored each time the ambilight is started normally.

Here are my lircrc and script files (located in /home/pi/ambi-tools). The original config file also lies there named ambi-tv.conf.bak. Hope you like it :-)

/etc/lirc/lircrc:

begin
  remote = *
  prog = irexec
  button = KEY_0
  config = /home/pi/ambi-tools/stop_ambi.sh
end

begin
  remote = *
  prog = irexec
  button = KEY_1
  config = /home/pi/ambi-tools/restore_config.sh; /home/pi/ambi-tools/restart_ambi.sh
end

begin
  remote = *
  prog = irexec
  button = KEY_VOLUMEUP
  config = /home/pi/ambi-tools/change_config.sh autocrop-luminance-threshold 1; /home/pi/ambi-tools/restart_ambi.sh
end

begin
  remote = *
  prog = irexec
  button = KEY_VOLUMEDOWN
  config = /home/pi/ambi-tools/change_config.sh autocrop-luminance-threshold -1; /home/pi/ambi-tools/restart_ambi.sh
end

begin
  remote = *
  prog = irexec
  button = KEY_UP
  config = /home/pi/ambi-tools/change_config.sh crop-top 5; /home/pi/ambi-tools/change_config.sh crop-bottom 5; /home/pi/ambi-tools/restart_ambi.sh
end

begin
  remote = *
  prog = irexec
  button = KEY_DOWN
  config = /home/pi/ambi-tools/change_config.sh crop-top -5; /home/pi/ambi-tools/change_config.sh crop-bottom -5; /home/pi/ambi-tools/restart_ambi.sh
end

begin
  remote = *
  prog = irexec
  button = KEY_RIGHT
  config = /home/pi/ambi-tools/change_config.sh crop-left 5; /home/pi/ambi-tools/change_config.sh crop-right 5; /home/pi/ambi-tools/restart_ambi.sh
end

begin
  remote = *
  prog = irexec
  button = KEY_LEFT
  config = /home/pi/ambi-tools/change_config.sh crop-left -5; /home/pi/ambi-tools/change_config.sh crop-right -5; /home/pi/ambi-tools/restart_ambi.sh
end

begin
  remote = *
  prog = irexec
  button = KEY_2
  config = /home/pi/ambi-tools/restart_ambi.sh -p 1
end

begin
  remote = *
  prog = irexec
  button = KEY_3
  config = /home/pi/ambi-tools/restart_ambi.sh -p 2
end

begin
  remote = *
  prog = irexec
  button = KEY_POWER
  config = reboot
end

begin
  remote = *
  prog = irexec
  button = KEY_POWER2
  config = poweroff
end

/home/pi/ambi-tools/change_config.sh:

#!/bin/bash
source "`dirname $0`/log.sh"

CONFIG_FILE="/etc/ambi-tv.conf"
TMP_FILE="/tmp/ambi-tv.conf.tmp"
CONFIG_KEY=$1
VALUE_CHANGE=$2

log_success "Trying to change key $CONFIG_KEY by $VALUE_CHANGE"

# create empty tmp file
> $TMP_FILE

# read config file per line keeping whitespace
IFS=""
while read LINE ; do
    if [[ "$LINE" =~ (\ *$CONFIG_KEY\ +)([0-9]+) ]] ; then
        # config key and numeric value found
        NEW_VALUE=$((BASH_REMATCH[2] + $VALUE_CHANGE))

        if [ $NEW_VALUE -ge 0 ] ; then
            echo "${BASH_REMATCH[1]}$NEW_VALUE" >> $TMP_FILE
            log_success "Changed $CONFIG_KEY to $NEW_VALUE"
        else
            log_error "Invalid value: $NEW_VALUE"
            exit 1
        fi
    else
        # other line - don't touch, just write to file
        echo "$LINE" >> $TMP_FILE
    fi
done < "$CONFIG_FILE"

# overwrite old config
if mv "$TMP_FILE" "$CONFIG_FILE" ; then
    log_success "$CONFIG_FILE written"
    exit 0
else
    log_error "File $CONFIG_FILE could not be written"
    exit 1
fi

/home/pi/ambi-tools/log.sh:

#!/bin/bash
ENABLED=0
LOGFILE=/var/log/ambi-tools.log
SCRIPTNAME=`basename $0`

function log_error {
        if [ $ENABLED == 1 ] ; then
                echo "$SCRIPTNAME: ERROR: $1" > $LOGFILE
        fi
}

function log_success {
        if [ $ENABLED == 1 ] ; then
                echo "$SCRIPTNAME: $1" > $LOGFILE
        fi
}

/home/pi/ambi-tools/restart_ambi.sh:

#!/bin/bash
PWD=`pwd`
cd `dirname $0`

source "log.sh"
./stop_ambi.sh

if screen -d -m /home/pi/ambi-tv-mnorthfield/bin/ambi-tv $@ ; then
    log_success "ambi-tv started"
else
    log_error "ambi-tv could not be started"
fi

cd $PWD

/home/pi/ambi-tools/restore_config.sh:

#!/bin/bash
source "`dirname $0`/log.sh"

if cp /home/pi/ambi-tools/ambi-tv.conf.bak /etc/ambi-tv.conf ; then
    log_success "Config restored"
else
    log_error "Config could not be restored"
fi

/home/pi/ambi-tools/stop_ambi.sh:

#!/bin/bash
source "`dirname $0`/log.sh"

if killall ambi-tv ; then
    log_success "Process killed"
else
    log_success "No process found"
fi
lutzdatenschutz commented 10 years ago

Many thanks for the suggestions. I also had the problem that killing the process did not turn of the LEDs. A quick workaround is to add a -l/--ledsoffswitch to main.c:


static void ambitv_usage(const char* name)
{
     //...
     "\t-p,--program [i]         run the [i]-th program from the configuration file on start-up.\n"
     "\t-l,--ledsoff             turn off the LEDs\n"
     //...
}

static int ambitv_main_configure(int argc, char** argv)
{
    int c, ret = 0;
    static struct option lopts[] = {
      { "button-gpio", required_argument, 0, 'b' },
      { "file", required_argument, 0, 'f' },
      { "help", no_argument, 0, 'h' },
      { "program", required_argument, 0, 'p' },
      { "ledsoff", no_argument, 0, 'l'},
      { NULL, 0, 0, 0 }
    };
    while (1) {
      c = getopt_long(argc, argv, "b:f:hp:l", lopts, NULL);
      if (c < 0)
         break;
      switch (c) {
         case 'l': {
           conf.running=0;
           break;
         }
         //...
      }
      //...
   }
}

Delete src/main.o and recompile the binary with make. kill_ambilight.sh kills any running ambi-tv processes and turns off the LEDs:


#!/bin/bash
#kill running ambi-tv processes
sudo killall ambi-tv 2>/dev/null
#switch off LEDs and exit
sudo /home/pi/ambi-tv-master/bin/ambi-tv --ledsoff 1>/dev/null

My lircrc:


begin irexec

    begin
            prog   = irexec
            remote = *
            button = KEY_0
            config = /home/pi/kill_ambilight.sh
    end

    begin
            prog   = irexec
            remote = *
            button = KEY_1
            config = /home/pi/kill_ambilight.sh; sudo screen -d -m /home/pi/ambi-tv-master/bin/ambi-tv --button-gpio 3
    end
    begin
            prog   = irexec
            remote = *
            button = KEY_2
            config = /home/pi/kill_ambilight.sh; sudo screen -d -m /home/pi/ambi-tv-master/bin/ambi-tv --program 1 --button-gpio 3
    end
    begin
            prog   = irexec
            remote = *
            button = KEY_3
            config = /home/pi/kill_ambilight.sh; sudo screen -d -m /home/pi/ambi-tv-master/bin/ambi-tv --program 2 --button-gpio 3
    end

end irexec 

/etc/rc.local contains:


/usr/sbin/lircd -d /dev/lirc0
/usr/bin/irexec -d /home/pi/lircrc
ghgoldberg commented 10 years ago

I just installed the RemotePi Board available from http://www.msldigital.com/ and now I can turn my ambilight on and off using my harmony remote control. The maker of the board, Matt at MSL Digital, is now working to integrate additional ambilight controls into the script. He will be posting here once he gets more done. Here is my processor with the board (with the IR receiver) installed. 2014-04-07 21 03 58