├── .gitignore ├── README.md └── dmx.py /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyb 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #pyb_DMX 2 | This module tries to make it slightly simpler to send DMX512 messages to lights 3 | 4 | Here is a very simple example of how it works, ideally ```write_frame()``` should be done on a timer interrupt as DMX lights expect a regular message 5 | 6 | ```python 7 | # create a dmx device on UART port 1 8 | dmx1 = dmx.universe(1) 9 | 10 | # Set the channel(s) to the value you want 11 | DMX1.set_channels({1:i}) 12 | 13 | # Send the message 14 | DMX1.write_frame() 15 | ``` 16 | 17 | thanks to this website for useful details 18 | [http://www.ubasics.com/DMX-512] 19 | -------------------------------------------------------------------------------- /dmx.py: -------------------------------------------------------------------------------- 1 | from pyb import UART, Pin, udelay 2 | from array import array 3 | 4 | tx_pins = [None, 'X9', 'X3', 'Y9', 'X1', None, 'Y1'] 5 | 6 | class universe(): 7 | def __init__(self, port): 8 | self.port = port 9 | 10 | # To check if port is valid 11 | dmx_uart = UART(port) 12 | del(dmx_uart) 13 | 14 | # First byte is always 0, 512 after that is the 512 channels 15 | self.dmx_message = array('B', [0] * 513) 16 | 17 | def set_channels(self, message): 18 | """ 19 | a dict and writes them to the array 20 | format {channel:value} 21 | """ 22 | 23 | for ch in message: 24 | self.dmx_message[ch] = message[ch] 25 | 26 | # for i, ch in enumerate(channels): 27 | # self.dmx_message[ch] = values[i] 28 | 29 | def write_frame(self): 30 | """ 31 | Send a DMX frame 32 | """ 33 | # DMX needs a 88us low to begin a frame, 34 | # 77uS us used because of time it takes to init pin 35 | dmx_uart = Pin(tx_pins[self.port], Pin.OUT_PP) 36 | dmx_uart.value(0) 37 | udelay(74) 38 | dmx_uart.value(1) 39 | 40 | # Now turn into a UART port and send DMX data 41 | dmx_uart = UART(self.port) 42 | dmx_uart.init(250000, bits=8, parity=None, stop=2) 43 | #send bytes 44 | dmx_uart.write(self.dmx_message) 45 | #Delete as its going to change anyway 46 | del(dmx_uart) 47 | --------------------------------------------------------------------------------