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 "OlaLED"
From wiki.openlighting.org
(→Install script) |
(→Install script) |
||
Line 19: | Line 19: | ||
nano /srv/dmx/server.py | nano /srv/dmx/server.py | ||
− | |||
− | |||
− | import SocketServer | + | #!/usr/bin/python |
− | import threading | + | |
− | import time | + | import SocketServer |
− | from ola_send_dmx import * | + | import threading |
+ | import time | ||
+ | from ola_send_dmx import * | ||
+ | |||
+ | my_thread = 0 | ||
+ | my_command = 'start' | ||
+ | |||
+ | actual_program = 'color' ## program wanted | ||
+ | param1 = 'red' ## first param | ||
+ | intensity = 100 ## current intensity | ||
+ | actual_fixed_color = 0 ## if fixed color choice, put args here to remember (if having to change intensity) | ||
+ | R_before = 0 # current color state | ||
+ | G_before = 0 | ||
+ | B_before = 0 | ||
− | + | pitch = 1 | |
− | |||
− | + | class MyTCPHandler(SocketServer.BaseRequestHandler): | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | class MyTCPHandler(SocketServer.BaseRequestHandler): | ||
""" | """ | ||
The RequestHandler class for our server. | The RequestHandler class for our server. | ||
Line 48: | Line 48: | ||
client. | client. | ||
""" | """ | ||
− | + | ||
def handle(self): | def handle(self): | ||
# self.request is the TCP socket connected to the client | # self.request is the TCP socket connected to the client | ||
Line 82: | Line 82: | ||
# just send back the same data to the client, but upper-cased | # just send back the same data to the client, but upper-cased | ||
self.request.send(self.data.upper()) | self.request.send(self.data.upper()) | ||
− | + | ||
− | class Thread_dmx(threading.Thread): | + | class Thread_dmx(threading.Thread): |
− | + | ||
def __init__(self, nom = ''): | def __init__(self, nom = ''): | ||
threading.Thread.__init__(self) | threading.Thread.__init__(self) | ||
Line 98: | Line 98: | ||
print('Error: empty request\n') | print('Error: empty request\n') | ||
return | return | ||
− | + | ||
command = words[0] | command = words[0] | ||
args = words[1:] | args = words[1:] | ||
− | + | ||
− | + | ||
methodname = 'do_' + command | methodname = 'do_' + command | ||
Line 118: | Line 118: | ||
actual_program = command | actual_program = command | ||
− | + | ||
− | + | ||
## take care of inexistent function | ## take care of inexistent function | ||
if not hasattr(self, methodname): | if not hasattr(self, methodname): | ||
print('Error: ',command,' is not a valid command') | print('Error: ',command,' is not a valid command') | ||
return | return | ||
− | + | ||
## save color if fixed color | ## save color if fixed color | ||
if command=='fade': | if command=='fade': | ||
fixed_color = args | fixed_color = args | ||
− | + | ||
## launch corresponding function | ## launch corresponding function | ||
method = getattr(self, methodname) | method = getattr(self, methodname) | ||
method(*args) | method(*args) | ||
− | + | ||
## Stop the thread if called | ## Stop the thread if called | ||
def stop(self): | def stop(self): | ||
self.Terminated = True | self.Terminated = True | ||
− | + | ||
− | + | ||
## Go to specified DMX adresses from previous adresses in fade | ## Go to specified DMX adresses from previous adresses in fade | ||
def do_fade(self,R_after,G_after,B_after,speed): | def do_fade(self,R_after,G_after,B_after,speed): | ||
Line 145: | Line 145: | ||
global B_before | global B_before | ||
global intensity | global intensity | ||
− | + | ||
R_after=int(round((int(R_after)*intensity)/100)) | R_after=int(round((int(R_after)*intensity)/100)) | ||
G_after=int(round((int(G_after)*intensity)/100)) | G_after=int(round((int(G_after)*intensity)/100)) | ||
B_after=int(round((int(B_after)*intensity)/100)) | B_after=int(round((int(B_after)*intensity)/100)) | ||
− | + | ||
if(R_after==R_before and G_after==G_before and B_after==B_before): | if(R_after==R_before and G_after==G_before and B_after==B_before): | ||
print('Etat deja atteint') | print('Etat deja atteint') | ||
return | return | ||
− | + | ||
− | + | ||
− | + | ||
speed=float(speed) | speed=float(speed) | ||
actual_intensity = intensity | actual_intensity = intensity | ||
time_to_sleep=(speed/max(abs(R_before-R_after),abs(G_before-G_after),abs(B_before-B_after))) | time_to_sleep=(speed/max(abs(R_before-R_after),abs(G_before-G_after),abs(B_before-B_after))) | ||
− | + | ||
− | + | ||
# count to know wich pitch to apply (processor free) | # count to know wich pitch to apply (processor free) | ||
− | + | ||
speed_reference=float(0.01) | speed_reference=float(0.01) | ||
print(time_to_sleep) | print(time_to_sleep) | ||
Line 171: | Line 171: | ||
pitch = int(1) | pitch = int(1) | ||
print(pitch) | print(pitch) | ||
− | + | ||
− | + | ||
## while ending color are not reached or no order for stopping, increment data | ## while ending color are not reached or no order for stopping, increment data | ||
while (R_before-R_after!=0 or G_before-G_after!=0 or B_before-B_after!=0) and (not self.Terminated) : | while (R_before-R_after!=0 or G_before-G_after!=0 or B_before-B_after!=0) and (not self.Terminated) : | ||
Line 181: | Line 181: | ||
B_after=int(round((int(B_after)*intensity)/100)) | B_after=int(round((int(B_after)*intensity)/100)) | ||
actual_intensity = intensity | actual_intensity = intensity | ||
− | + | ||
R_diff=R_before-R_after | R_diff=R_before-R_after | ||
G_diff=G_before-G_after | G_diff=G_before-G_after | ||
B_diff=B_before-B_after | B_diff=B_before-B_after | ||
− | + | ||
if R_diff <= -pitch : | if R_diff <= -pitch : | ||
R_before += pitch | R_before += pitch | ||
Line 204: | Line 204: | ||
else : | else : | ||
B_before=B_after | B_before=B_after | ||
− | + | ||
− | + | ||
#print('AFTER : ',R_after,G_after,B_after) | #print('AFTER : ',R_after,G_after,B_after) | ||
print('values =',R_before,G_before,B_before) | print('values =',R_before,G_before,B_before) | ||
Line 212: | Line 212: | ||
dmx_send(R_before,G_before,B_before) | dmx_send(R_before,G_before,B_before) | ||
time.sleep(time_to_sleep*pitch) | time.sleep(time_to_sleep*pitch) | ||
− | + | ||
## make a chase from red to blue, indefinitely | ## make a chase from red to blue, indefinitely | ||
def do_chase(self,speed): | def do_chase(self,speed): | ||
Line 233: | Line 233: | ||
#self.do_fade(intensity,0,intensity,speed) | #self.do_fade(intensity,0,intensity,speed) | ||
#print("7") | #print("7") | ||
− | + | ||
− | if __name__ == "__main__": | + | if __name__ == "__main__": |
HOST, PORT = "localhost", 9999 | HOST, PORT = "localhost", 9999 | ||
− | + | ||
# Create the server, binding to localhost on port 9999 | # Create the server, binding to localhost on port 9999 | ||
server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler) | server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler) | ||
− | + | ||
# Activate the server; this will keep running until you | # Activate the server; this will keep running until you | ||
# interrupt the program with Ctrl-C | # interrupt the program with Ctrl-C | ||
server.serve_forever() | server.serve_forever() | ||
− | |||
− | |||
Revision as of 15:54, 24 July 2010
Introduction
In this topic, you will learn how to install a python script to permit to control a LED / RGB system via webcontrol (or other python script)
prerequisites
You need to have a fully functional ola system
Install script
create a directory to store all these new functionnality
mkdir -p /srv/dmx/
write a this new python script in this folder :
nano /srv/dmx/server.py
#!/usr/bin/python import SocketServer import threading import time from ola_send_dmx import * my_thread = 0 my_command = 'start' actual_program = 'color' ## program wanted param1 = 'red' ## first param intensity = 100 ## current intensity actual_fixed_color = 0 ## if fixed color choice, put args here to remember (if having to change intensity) R_before = 0 # current color state G_before = 0 B_before = 0
pitch = 1
class MyTCPHandler(SocketServer.BaseRequestHandler): """ The RequestHandler class for our server.
It is instantiated once per connection to the server, and must override the handle() method to implement communication to the client. """ def handle(self): # self.request is the TCP socket connected to the client
global my_thread
self.data = self.request.recv(1024).strip()
# was a test #if self.data == 'reset' : # global my_command # my_command = 'reset' #else :
command = self.data.split() command = command[0]
if command == 'intensity': # We separe intensity because should lauch in parallel another_thread = Thread_dmx(self.data) # thread named different for able controlling mythread another_thread.start() print "thread intensite lance" # just send back the same data, but upper-cased self.request.send(self.data.upper()) else: if my_thread != 0 : #if thread is already launched and is not about intensity try: my_thread.stop() #stop thread my_thread.join() #stop program to be sure thread is stopped except NameError: print "An error occured when trying to stop previous thread." print "launching thread " + self.data my_thread = Thread_dmx(self.data) my_thread.start() # just send back the same data to the client, but upper-cased self.request.send(self.data.upper())
class Thread_dmx(threading.Thread):
def __init__(self, nom = ): threading.Thread.__init__(self) self.nom = nom self.Terminated = False def run(self): global actual_program global intensity global fixed_color ## separate command and arguments ## words = self.nom.split() if len(words) == 0: print('Error: empty request\n') return
command = words[0] args = words[1:]
methodname = 'do_' + command
## relauch sending DMX for fixed color, if intensity changed if command=='intensity':
intensity = abs(int(args[0])) if intensity > 100: return if actual_program == 'fade': method = getattr(self, 'do_fade') method(*fixed_color)
else:
actual_program = command
## take care of inexistent function
if not hasattr(self, methodname):
print('Error: ',command,' is not a valid command')
return
## save color if fixed color if command=='fade': fixed_color = args
## launch corresponding function method = getattr(self, methodname) method(*args)
## Stop the thread if called def stop(self): self.Terminated = True
## Go to specified DMX adresses from previous adresses in fade
def do_fade(self,R_after,G_after,B_after,speed):
global R_before global G_before global B_before global intensity
R_after=int(round((int(R_after)*intensity)/100)) G_after=int(round((int(G_after)*intensity)/100)) B_after=int(round((int(B_after)*intensity)/100))
if(R_after==R_before and G_after==G_before and B_after==B_before): print('Etat deja atteint') return
speed=float(speed)
actual_intensity = intensity time_to_sleep=(speed/max(abs(R_before-R_after),abs(G_before-G_after),abs(B_before-B_after)))
# count to know wich pitch to apply (processor free) speed_reference=float(0.01) print(time_to_sleep) pitch=int(speed_reference/time_to_sleep) print(pitch) if pitch == 0: pitch = int(1) print(pitch)
## while ending color are not reached or no order for stopping, increment data while (R_before-R_after!=0 or G_before-G_after!=0 or B_before-B_after!=0) and (not self.Terminated) : ## If intensity was changed, take care of this if actual_intensity != intensity: R_after=int(round((int(R_after)*intensity)/100)) G_after=int(round((int(G_after)*intensity)/100)) B_after=int(round((int(B_after)*intensity)/100)) actual_intensity = intensity
R_diff=R_before-R_after G_diff=G_before-G_after B_diff=B_before-B_after
if R_diff <= -pitch : R_before += pitch elif R_diff >= pitch : R_before-= pitch else : R_before = R_after if G_diff <= -pitch : G_before += pitch elif G_diff >= pitch : G_before -= pitch else : G_before=G_after if B_diff <= -pitch : B_before += pitch elif B_diff >=pitch : B_before -= pitch else : B_before=B_after
#print('AFTER : ',R_after,G_after,B_after)
print('values =',R_before,G_before,B_before)
print('sleep =',time_to_sleep*pitch)
print('pitch =',pitch)
dmx_send(R_before,G_before,B_before)
time.sleep(time_to_sleep*pitch)
## make a chase from red to blue, indefinitely def do_chase(self,speed): intensity = 255 speed = float(speed) while not self.Terminated :
self.do_fade(intensity,0,intensity,speed) print("7")
print("1") self.do_fade(intensity,0,0,speed) print("2") self.do_fade(intensity,intensity,0,speed) print("3") self.do_fade(0,intensity,0,speed) print("4") self.do_fade(0,intensity,intensity,speed) print("5") self.do_fade(0,0,intensity,speed) print("6") #self.do_fade(intensity,0,intensity,speed) #print("7")
if __name__ == "__main__": HOST, PORT = "localhost", 9999 # Create the server, binding to localhost on port 9999 server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler) # Activate the server; this will keep running until you # interrupt the program with Ctrl-C server.serve_forever()
copy client_wrapper.py in this folder :
cp /usr/src/ola-0.7.4/python/examples/client_wrapper.py /srv/dmx/
create a new python file function : ola_send_dmx.py
nano /srv/dmx/ola_send_dmx.py