DMX with the Kunbus Revolution Pi Core 3+
Program
The Python script (Listing 3) first loads all the required libraries. The block that follows creates some variables and constants for the offset. As seen in the previous section, values on the RevPi Core can be accessed by their plain text names and by offsets.
Listing 3
dmx_demo.py
01 #!/usr/bin/python3 02 import time 03 import struct 04 05 DEBUG = False 06 07 DMX1_MASTER = 525 08 DMX1_RED = 526 09 DMX1_GREEN = 527 10 DMX1_BLUE = 528 11 12 DMX2_MASTER = 533 13 DMX2_RED = 534 14 DMX2_GREEN = 535 15 DMX2_BLUE = 536 16 17 class RevPi: 18 _rev_pi = open('/dev/piControl0', 'wb+', 0) 19 20 def __init__(self): 21 pass 22 23 class DMX(RevPi): 24 _green = 0 25 _red = 255 26 _blue = 0 27 28 def __init__(self, master, adr_green, adr_red, adr_blue): 29 self.adr_green = adr_green 30 self.adr_red = adr_red 31 self.adr_blue = adr_blue 32 self._rev_pi.seek(master) 33 self._rev_pi.write(struct.pack('<H', 255)) 34 35 def update_color(self, adr, val): 36 self._rev_pi.seek(adr) 37 self._rev_pi.write(struct.pack('<H', val)) 38 39 def set_step(self, mode_val): 40 if mode_val == 1: 41 self._green += 16 42 elif mode_val == 2: 43 self._red += -16 44 elif mode_val == 3: 45 self._blue += 16 46 elif mode_val == 4: 47 self._green += -16 48 elif mode_val == 5: 49 self._red += 16 50 elif mode_val == 6: 51 self._blue += -16 52 53 def do_green(self): 54 if DEBUG: 55 print('GREEN: %d' % self._green) 56 self.update_color(self.adr_green, self._green) 57 58 def do_red(self): 59 if DEBUG: 60 print('RED: %d' % self._red) 61 self.update_color(self.adr_red, self._red) 62 63 def do_blue(self): 64 if DEBUG: 65 print('BLUE: %d' % self._blue) 66 self.update_color(self.adr_blue, self._blue) 67 68 DMX_1 = DMX(DMX1_MASTER, DMX1_GREEN, DMX1_RED, DMX1_BLUE) 69 DMX_2 = DMX(DMX2_MASTER, DMX2_GREEN, DMX2_RED, DMX2_BLUE) 70 71 while 1: 72 for mode in range(1, 7): 73 if DEBUG: 74 print('mode=%d' % mode) 75 for i in range(0, 15): 76 if DEBUG: 77 print('i=%d' % i) 78 DMX_1.set_step(mode) 79 DMX_2.set_step(mode) 80 DMX_1.do_red() 81 DMX_2.do_red() 82 DMX_1.do_green() 83 DMX_2.do_green() 84 DMX_1.do_blue() 85 DMX_2.do_blue() 86 time.sleep(0.05)
With the help of the fcntl
library, the script could be designed in such a way that it determines the offset in the background and uses it instead of the plain text name. The script here avoids this complex procedure by using the offsets directly. Because these values always increase by 1 in the example, you do not have to determine all values by trial and error. A code example demonstrating the use of the plain text names can be found on the Kunbus homepage [5].
Two classes (lines 17 and 23) establish the connection to the RevPi (RevPi
) and control color changes of the lights (DMX
). The open()
command lets you open a connection to the hardware driver and store it in the _rev_Pi
variable. All access to the hardware will then be through this variable.
In the DMX
class, the do_<color>
methods use the update_color()
statement to ensure that the appropriate color is sent to the light. The color is calculated by the set_step()
method and controlled by the mode_val
variable. Because the lamps can only handle fairly coarse dimming, the values for each color are always increased or decreased by 16.
Two lights and three colors allow six possible color transitions, which is why the mode
variable is reset to 1 as soon as it reaches a value of 7 (line 72). The time.sleep(0.05)
command controls the speed of the color transitions. The __init__
method of the DMX
class sets the master dimmer to 100 percent, to make full use of the dimmers for each color.
The while 1
command tells the program to run in an infinite loop, which you can cancel with the Ctrl+C keyboard combination. Inside this loop, the code changes the variable mode
and passes it to the objects for the two lights, turning a particular color on or off. When a color reaches its new value, the mode
variable is incremented by 1. To cycle through a color transition fully, you need to call the set_step()
method 16 times, as in the second for
loop (line 75).
You can launch the demo program with the command
python3 dmx_demo.py
at the RevPi Core's command line. You can find a video on YouTube showing the demo program in action [6].
Conclusions
The DMX module for the Revolution Pi Core 3+ makes creating your own lighting show easy. A little browsing on the Internet reveals many interesting devices that can be controlled by DMX. Besides classic stage lighting, the products also cover lighting systems for home automation. One genuine highlight among the DMX devices is a moving, talking skull [7]. Have fun with your own DMX experiments.
Infos
- DMX lights: https://www.aliexpress.com/item/1005002449259723.html?gatewayAdapt=glo2deu
- RevPi Core 3+: https://revolutionpi.com/revpi-core/
- DMX module user manual: https://www.kunbus.com/files/media/bedienungsanleitungen/DO0262R00-MGW-UM-DMX-EN.pdf
- PuTTY: https://www.putty.org
- Python example: https://revolution.kunbus.com/download/1141/
- Video for the program: https://youtu.be/2i2Httbwc1E
- Talking skull: https://www.hauntedhousecreations.com/full-motion-talking-skull/
« Previous 1 2
Buy this article as PDF
(incl. VAT)