Personal tools
The Open Lighting Project has moved!

We've launched our new site at www.openlighting.org. This wiki will remain and be updated with more technical information.

Difference between revisions of "OLA Python API"

From wiki.openlighting.org

Jump to: navigation, search
(Building the Python Bindings)
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
== Building the Python Bindings ==
 
== Building the Python Bindings ==
  
See [[Building LLA]]  but run ''./configure --enable-python-libs''
+
You should install readline if you haven't already:
  
Note that you have to install Swig in order to build the python bindings (sudo apt-get install swig)
+
<pre>
 +
sudo easy_install readline
 +
</pre>
 +
 
 +
You need the python version of protocol buffers installed.
 +
 
 +
See [[Building OLA]]  but run
 +
 
 +
<pre>
 +
$ ./configure --enable-python-libs
 +
</pre>
 +
 
 +
Check where the modules are going to be installed:
 +
 
 +
<pre>
 +
$ grep ^pythondir config.log
 +
pythondir='/Library/Python/2.5/site-packages'
 +
</pre>
 +
 
 +
Run the usual: make, make check, make install.
 +
 
 +
Check that the modules have been installed correctly in the $pythondir above.
 +
 
 +
Check that $pythondir appears in sys.path:
  
There is also a forgotten data in lla/python/Makefile : you have to add -I../include to the value of DEFAULT_INCLUDES at the line 70.
+
<pre>
 +
$ python -c "import sys; print sys.path"
 +
</pre>
  
At the end of the installation, you should run ldconfig as root in order to use the python bindings.
+
If it doesn't add it to $PYTHONPATH:
 +
 
 +
<pre>
 +
$ export PYTHONPATH=$PYTHONPATH:/path/to/your/install/dir
 +
</pre>
 +
 
 +
You probably want to put this line in your shell .rc file.
  
 
== Interesting Classes ==
 
== Interesting Classes ==
  
; LlaClient
+
; ClientWrapper
: the main connection class
+
: A helper class that connects to the server and creates a new OlaClient
; dmxBuffer
+
; OlaClient
: represents a list of DMX channel values
+
: The main API
; LlaClientObserver
+
 
: base class to handle events
+
 
; LlaPlugin
+
The best documentation is the set of example programs in the python/examples directory.
: represents a Plugin
 
; LlaDevice
 
: represents a Device
 
  
 
== Sending DMX ==
 
== Sending DMX ==
A simple example to send DMX:
+
A simple example to send one frame of DMX data:
  
 
<pre>
 
<pre>
from lla import *
+
import array
import sys
+
from ola.ClientWrapper import ClientWrapper
  
con = LlaClient()
+
def DmxSent(state):
if con.start():
+
   wrapper.Stop()
   sys.exit()
 
  
universe = 0
+
universe = 1
 +
data = array.array('B')
 +
data.append(10)
 +
data.append(50)
 +
data.append(255)
 +
wrapper = ClientWrapper()
 +
client = wrapper.Client()
 +
client.SendDmx(universe, data, DmxSent)
 +
wrapper.Run()
 +
</pre>
  
# create a dmxBuffer for the channel values
+
== Sending Multiple Frames ==
DMX_LEN = 512
 
dmx = dmxBuffer(DMX_LEN)
 
for i in range(0, DMX_LEN):
 
  dmx[i] = i
 
  
con.send_dmx(universe, dmx, DMX_LEN)
+
<pre>
</pre>
+
import array
 +
from ola.ClientWrapper import ClientWrapper
  
== Receiving DMX ==
+
wrapper = None
 +
loop_count = 0
 +
TICK_INTERVAL = 100  # in ms
  
Receiving is slightly harder, we need to setup an Observer object to handle the events:
+
def DmxSent(state):
 +
  if not state.Succeeded():
 +
    wrapper.Stop()
  
''' THIS ISN'T WORKING YET ''' [simonn]
+
def SendDMXFrame():
 +
  # schdule a function call in 100ms
 +
  # we do this first in case the frame computation takes a long time.
 +
  wrapper.AddEvent(TICK_INTERVAL, SendDMXFrame)
  
<pre>
+
  # compute frame here
from lla import *
+
  data = array.array('B')
import sys
+
  global loop_count
 +
  data.append(loop_count % 255)
 +
  loop_count += 1
  
class Observer(LlaClientObserver):
+
   # send
   """ Handle the events """
+
   wrapper.Client().SendDmx(1, data, DmxSent)
   def new_dmx(self, uni, length, data):
+
                                                                                                                       
    """ Called with new dmx buffer """
 
    return 0
 
  
 +
wrapper = ClientWrapper()
 +
wrapper.AddEvent(TICK_INTERVAL, SendDMXFrame)
 +
wrapper.Run()
 +
</pre>
 +
 +
== Receiving DMX ==
  
# create a new LlaClient
+
Here is some code to receive DMX on universe 1.
con = LlaClient()
 
  
# create an observer object and register
+
<pre>
ob = Observer()
+
from ola.ClientWrapper import ClientWrapper
con.set_observer(ob)
 
 
 
if con.start():
 
  sys.exit()
 
  
con.register_uni(1, LlaClient.REGISTER)
+
def NewData(data):
 +
  print data
  
while True:
+
universe = 1
  con.fd_action(1)
 
  
 +
wrapper = ClientWrapper()
 +
client = wrapper.Client()
 +
client.RegisterUniverse(universe, client.REGISTER, NewData)
 +
wrapper.Run()
 
</pre>
 
</pre>

Latest revision as of 17:48, 7 June 2011

Building the Python Bindings

You should install readline if you haven't already:

sudo easy_install readline

You need the python version of protocol buffers installed.

See Building OLA but run

$ ./configure --enable-python-libs

Check where the modules are going to be installed:

$ grep ^pythondir config.log
pythondir='/Library/Python/2.5/site-packages'

Run the usual: make, make check, make install.

Check that the modules have been installed correctly in the $pythondir above.

Check that $pythondir appears in sys.path:

$ python -c "import sys; print sys.path"

If it doesn't add it to $PYTHONPATH:

$ export PYTHONPATH=$PYTHONPATH:/path/to/your/install/dir

You probably want to put this line in your shell .rc file.

Interesting Classes

ClientWrapper
A helper class that connects to the server and creates a new OlaClient
OlaClient
The main API


The best documentation is the set of example programs in the python/examples directory.

Sending DMX

A simple example to send one frame of DMX data:

import array
from ola.ClientWrapper import ClientWrapper

def DmxSent(state):
  wrapper.Stop()

universe = 1
data = array.array('B')
data.append(10)
data.append(50)
data.append(255)
wrapper = ClientWrapper()
client = wrapper.Client()
client.SendDmx(universe, data, DmxSent)
wrapper.Run()

Sending Multiple Frames

import array
from ola.ClientWrapper import ClientWrapper

wrapper = None
loop_count = 0
TICK_INTERVAL = 100  # in ms

def DmxSent(state):
  if not state.Succeeded():
    wrapper.Stop()

def SendDMXFrame():
  # schdule a function call in 100ms
  # we do this first in case the frame computation takes a long time.
  wrapper.AddEvent(TICK_INTERVAL, SendDMXFrame)

  # compute frame here
  data = array.array('B')
  global loop_count
  data.append(loop_count % 255)
  loop_count += 1

  # send
  wrapper.Client().SendDmx(1, data, DmxSent)
                                                                                                                        

wrapper = ClientWrapper()
wrapper.AddEvent(TICK_INTERVAL, SendDMXFrame)
wrapper.Run()

Receiving DMX

Here is some code to receive DMX on universe 1.

from ola.ClientWrapper import ClientWrapper

def NewData(data):
  print data

universe = 1

wrapper = ClientWrapper()
client = wrapper.Client()
client.RegisterUniverse(universe, client.REGISTER, NewData)
wrapper.Run()