Linux External LED control for actions or states (playing etc)
#1
So as part of a media centre I'm building I'd like to have a few LEDs which I can have controlled via the current state of what XBMC is doing.

As an example I'd like to have a Blue LED light on when I'm playing back / off when playback stopped, a Red LED when its not doing anything, a Green LED when the library is updating in the background etc.

Is there anything out there which could help me with this? I'm not familiar with any which specifically do this but Ive seen there is some dealing with ambient light so not sure if this could be tweaked.

I've basic skills in coding and electronics but don't know much about how the XBMC system works to provide usable things in the background.
Reply
#2
You could probably do something with the JSON-RPC API (wiki) or even the older Web Server HTTP API (wiki) to get the status of XBMC.
Reply
#3
Mhh sounds like the job for an python addon.
AppleTV4/iPhone/iPod/iPad: HowTo find debug logs and everything else which the devs like so much: click here
HowTo setup NFS for Kodi: NFS (wiki)
HowTo configure avahi (zeroconf): Avahi_Zeroconf (wiki)
READ THE IOS FAQ!: iOS FAQ (wiki)
Reply
#4
I've had a quick look over the JSON-RPC API details and there a few notification things which may work.

Afraid I've no python experience so might be a steep learning curve for me so I'm going to have to get some more examples.

Any suggestions on options how to link in a python addon to external LEDs? Still not sure what functionality the arduino systems may give.

I thought something along the lines of Addon talked to XBMC to get status --> this tells the arduno the status --> this uses the status to enable / disable the LEDs
Reply
#5
Basically thats how it will work yes. Python is easy. You could have a look into that addon here:

https://github.com/Memphiz/script.xbmc.l...t.xbmc.lcd

or in any other addon. Beside that the wiki has alot info about python addon development.
AppleTV4/iPhone/iPod/iPad: HowTo find debug logs and everything else which the devs like so much: click here
HowTo setup NFS for Kodi: NFS (wiki)
HowTo configure avahi (zeroconf): Avahi_Zeroconf (wiki)
READ THE IOS FAQ!: iOS FAQ (wiki)
Reply
#6
You can write a Python script that receives notifications when playback starts, stops, etc. I've got a template lying around somewhere that you could easily hack. This seems to be the easiest way to get started. You just need some way to interface with your LEDs from Python.
Reply
#7
If your HTPC has a printer port, you can use it to drive LEDs. Here's how: http://eagerfish.eu/lpt-programming-exam...ntu-linux/

Here's a schematic of something that is close to what you want: http://www.logix4u.net/paralleltest.gif

From my memory, PC printer ports are not made to deliver much current (5 ma comes to mind). Yup, I just noticed that the schematic shows 1K resistors which would drive the LEDs at 5 ma. This might not be bright enough for what you want. Since you only want a couple of LEDs, you could double up on data pins to your LEDs for twice the drive current.

You'll need to write a program in C that controls the printer port (the link gives you a good start on that). The program will also need to be run as root - you can use suid for that.



Reply
#8
jhsrennie - did you managed to lay your hands on the template you thought you may have?

Thanks GomezAddams - nice idea but this is only part of the solutions I'm after. Without giving too much away I'm also going to be controlling other actions if I use a arduino and the main downside is hte itx board I have, no LTP port :p

I've done a little digging work and should be able to use an arduino controlled via serial using python to update the arduino with the current media state and control what the external LED's are doing for any given state in XBMC.

I'm a little confused on addon vs scripts. By the looks of it, I can create a addon and have it run as a xbmc service so its 'always on' in the background checking with xbmc for its current status. This correct? Having a addon constantly looping with a 'if mediaplayback = true then updateLED' (<-- just to show example - not code), would it have any possible side effects to performance?

Afraid you'll have to excuse any questions of obvious nature, only coded VB/SBScript and pascal in my college days so not to familiar with python.

Would be really useful if there was something build into xbmc which allowed a 'run script on action' type situation so I could say on Media Playback, run LEDupdate.py etc.
Reply
#9
(2012-05-07, 13:05)dynamis_dk Wrote: jhsrennie - did you managed to lay your hands on the template you thought you may have?

Place this code ina file called autoexec.py in your userdata folder. When XBMC starts it looks for an autoexec.py, and if one exists it executes it. This code subclasses the xbmc.Player class to allow you to do whatever you want with notifications.

NB the other devs will probably shout at me for mis-using the autoexec.py feature. You're only supposed to use it for short scripts that finish quickly. For code that needs to start when XBMC starts then continue running you should use a service add-on. have a play with this code and if you decide to go this route post here and I'll show you how to convert it to a service add-on.

Code:
import xbmc

class MyPlayer(xbmc.Player) :

    def __init__ (self):
        xbmc.Player.__init__(self)

    def onPlayBackStarted(self):
        xbmc.log('*** CALLBACK: PLAY')

    def onPlayBackEnded(self):
        xbmc.log('*** CALLBACK: END')

    def onPlayBackStopped(self):
        xbmc.log('*** CALLBACK: STOP')

player=MyPlayer()

while(1):
    xbmc.sleep(500)
Reply
#10
Thanks jhsrennie - I apprecite the effort. I actually managed to most of it working as i require using the following code:

Code:
import xbmc
import serial

class XBMCPlayer( xbmc.Player ):

    def __init__( self, *args ):
        pass

    def onPlayBackStarted( self ):
        # Will be called when xbmc starts playing a file
        #xbmc.log( "LED Status: Playback Stopped, LED ON" )
        ser.write("LED_ON\r")
        
    def onPlayBackEnded( self ):
        # Will be called when xbmc stops playing a file
        #xbmc.log( "LED Status: Playback Stopped, LED OFF" )
        ser.write("LED_OFF\r")

    def onPlayBackStopped( self ):
        # Will be called when user stops xbmc playing a file
        #xbmc.log( "LED Status: Playback Stopped, LED OFF" )
        ser.write("LED_OFF\r")
        
    def onPlayBackPaused( self ):
        # Will be called when user Pauses xbmc playing a file
        #xbmc.log( "LED Status: Playback Paused, LED ON BUT STILL" )
        ser.write("LED_PAUSE\r")    

    def onPlayBackResumed( self ):
        # Will be called when user stops xbmc playing a file
        #xbmc.log( "LED Status: Playback Resumed, LED ON" )
        ser.write("LED_ON\r")        

player = XBMCPlayer()
xbmc.log( "LED Status: Script Started" )
ser = serial.Serial(port=2, baudrate=9600, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, timeout=4, xonxoff=True, rtscts=False, writeTimeout=1, dsrdtr=False, interCharTimeout=None)

while(not xbmc.abortRequested):
    xbmc.sleep(100)
    
ser.close()
xbmc.log( "LED Status: Script Stopped" )

This is sending the correct info to the serial port for the Arduino to watch for so everything seems fine so far. Its all packaged up as a service too so its pretty seamless as far as any user action is involved. I may at some point put some kind of user config page to allow for selection of the serial port but thats the last of my worries as I've got all the electronics to do first.

only issues I've had is I can't seem to find an upto date list of the XBMC functions etc as shown here in the python docs: http://xbmc.sourceforge.net/python-docs/xbmc.html

Do we have a more upto date version as some like onPlayBackResumed() isn't mentions from what I can see.
Reply
#11
(2012-05-09, 01:47)dynamis_dk Wrote: Do we have a more upto date version as some like onPlayBackResumed() isn't mentions from what I can see.

http://mirrors.xbmc.org/docs/python-docs/ has the latest versions. See http://mirrors.xbmc.org/docs/python-docs...tml#Player for all the onPlayBackXXX functions.
Reply
#12
Thanks jhsrennie - not sure If I was just looking at an old part of the Wiki or if the links of out of date. That ones going in my favorates Smile
Reply
#13
I do similar, but i dim the house lights when movie starts.

i send a http command to a self build small delphi server so i can receive commands from more then one device.
the delphi server sends the commands to the arduino.
Reply

Logout Mark Read Team Forum Stats Members Help
External LED control for actions or states (playing etc)0