// USB-DMX adapters for the DMX bridge (bridge_dmx.c), linux only: // Enttec DMX USB Pro: the widget's "Output Only Send DMX" message (label 6) with start code 0 and 512 channels. // Open DMX (an FTDI cable with no processor): the DMX line itself, 250000 baud 8N2, a break and mark-after-break, // then start code 0 and 512 channels. // The kernel's termios2 sets 250000 baud exactly; its header clashes with glibc's , so this file has // only the kernel's. #include #include #include #include #include #include #include #include #include #include "dmx_wire.h" int ioctl(int fd, unsigned long request, ...); int dmx_serial_open(const char *path, bool open_dmx, char *err, size_t elen) { int fd = open(path, O_RDWR | O_NOCTTY | O_CLOEXEC); if (fd < 0) { snprintf(err, elen, "cannot open %s: %s", path, strerror(errno)); return -1; } struct termios2 t; if (ioctl(fd, TCGETS2, &t) != 0) { snprintf(err, elen, "%s is not a serial port: %s", path, strerror(errno)); close(fd); return -1; } // Raw: no echo, no line discipline, no flow control t.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON | IXOFF | IXANY); t.c_oflag &= ~OPOST; t.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN); t.c_cflag &= ~(CSIZE | PARENB | CBAUD | CRTSCTS); t.c_cflag |= CS8 | CLOCAL | CREAD | BOTHER; if (open_dmx) { t.c_cflag |= CSTOPB; // 8N2 } else { t.c_cflag &= ~CSTOPB; } unsigned baud = open_dmx ? 250000 : 57600; // the Pro is USB: its rate does not matter t.c_ispeed = t.c_ospeed = baud; t.c_cc[VMIN] = 0; t.c_cc[VTIME] = 0; if (ioctl(fd, TCSETS2, &t) != 0) { snprintf(err, elen, "cannot set up %s: %s", path, strerror(errno)); close(fd); return -1; } return fd; } static bool write_all(int fd, const uint8_t *p, size_t n) { while (n) { ssize_t w = write(fd, p, n); if (w < 0) { if (errno == EINTR) { continue; } return false; } p += w; n -= (size_t)w; } return true; } bool dmx_serial_write(int fd, bool open_dmx, const uint8_t data[DMX_CHANNELS]) { uint8_t frame[5 + 1 + DMX_CHANNELS + 1]; if (!open_dmx) { size_t len = 1 + DMX_CHANNELS; frame[0] = 0x7E; frame[1] = 6; frame[2] = (uint8_t)len; frame[3] = (uint8_t)(len >> 8); frame[4] = 0; // start code memcpy(frame + 5, data, DMX_CHANNELS); frame[5 + DMX_CHANNELS] = 0xE7; return write_all(fd, frame, 6 + DMX_CHANNELS); } ioctl(fd, TCSBRK, 1); // wait for the last frame to leave before the break if (ioctl(fd, TIOCSBRK) != 0) { return false; } usleep(110); // break: at least 88 us ioctl(fd, TIOCCBRK); usleep(16); // mark after break: at least 8 us frame[0] = 0; memcpy(frame + 1, data, DMX_CHANNELS); return write_all(fd, frame, 1 + DMX_CHANNELS); }