Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'serialport'
- class DMX
- START_OF_MESSAGE = "\x7E"
- SEND_DMX_LABEL = "\x06"
- START_OF_DATA = "\x00"
- END_OF_MESSAGE = "\xE7"
- SERIAL_PORT_PARAMS = {
- 'baud' => 115_200,
- 'data_bits' => 8,
- 'stop_bits' => 2,
- 'parity' => SerialPort::NONE
- }
- #
- # Wikipedia says "A maximum-sized packet, which has 512 channels, takes
- # approximately 23 ms to send, corresponding to a max refresh rate of ~44 Hz.
- # For higher refresh rates, packets having fewer than 512 channels can be sent.
- #
- def initialize(path, channel_count)
- raise ArgumentError.new("channel_count must be between 25 and 512") if (channel_count < 25 or channel_count > 512)
- @path, @channel_count = path, channel_count
- @port = SerialPort.new(@path, SERIAL_PORT_PARAMS)
- # Allocate a buffer once, so we don't continually produce garbage
- @packet = "\0" * (@channel_count + 6) # 6 bytes of START_OF_MESSAGE, etc.
- # Set the static values
- payload_size = (@channel_count + 1) # +1 for the start code
- @packet[0] = START_OF_MESSAGE
- @packet[1] = SEND_DMX_LABEL
- @packet[2] = (payload_size & 255).chr # LSB of size
- @packet[3] = ((payload_size >> 8) & 255).chr # MSB of size
- @packet[4] = START_OF_DATA
- # @channel_count bytes of DMX channels
- @packet[4 + @channel_count + 1] = END_OF_MESSAGE
- end
- def set(index, value)
- raise ArgumentError.new("index out of range") if (index < 0 or index >= @channel_count)
- @packet[5 + index] = value.chr
- end
- def send
- @port.write(@packet)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement