diff options
author | Oliwerix <oliwerix@gmail.com> | 2022-10-31 17:22:49 +0100 |
---|---|---|
committer | Oliwerix <oliwerix@gmail.com> | 2022-10-31 17:22:49 +0100 |
commit | 852b828e93fa1d85ea6fe7f5fd08b0360fe05cfb (patch) | |
tree | 3c40d8db619de48007fa71c25e835ec0418a05fa /sw/dmx2artnet/settty.c | |
parent | dnbk (diff) | |
parent | Merge branch 'master' of ssh://ni.sijanec.eu/var/lib/git/repo/luč (diff) | |
download | luč-852b828e93fa1d85ea6fe7f5fd08b0360fe05cfb.tar luč-852b828e93fa1d85ea6fe7f5fd08b0360fe05cfb.tar.gz luč-852b828e93fa1d85ea6fe7f5fd08b0360fe05cfb.tar.bz2 luč-852b828e93fa1d85ea6fe7f5fd08b0360fe05cfb.tar.lz luč-852b828e93fa1d85ea6fe7f5fd08b0360fe05cfb.tar.xz luč-852b828e93fa1d85ea6fe7f5fd08b0360fe05cfb.tar.zst luč-852b828e93fa1d85ea6fe7f5fd08b0360fe05cfb.zip |
Diffstat (limited to 'sw/dmx2artnet/settty.c')
-rw-r--r-- | sw/dmx2artnet/settty.c | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/sw/dmx2artnet/settty.c b/sw/dmx2artnet/settty.c new file mode 100644 index 0000000..65f54b1 --- /dev/null +++ b/sw/dmx2artnet/settty.c @@ -0,0 +1,91 @@ +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <signal.h> +#include <errno.h> +#include <string.h> +#include <sys/ioctl.h> +#include <termios.h> +#include <poll.h> +#include <sys/types.h> +#include <error.h> +#include <fcntl.h> +#include <sys/sendfile.h> +#define termios asmtermios +#define winsize asmwinsize +#define termio asmtermio +#include <asm/termios.h> +#undef termios +#undef winsize +#undef termio +#define S0(x) (x ? x : "") +// start bit: low, 2 stop bits high +int rate (int what_rate, int uart) { + int r = 0; + struct termios2 uartattr2; + if (ioctl(uart, TCGETS2, &uartattr2) == -1) { + error_at_line(0, errno, __FILE__, __LINE__, "ioctl TCGETS2, fd: %d", uart); + r = 1; + goto r; + } + uartattr2.c_cflag &= ~CBAUD; + uartattr2.c_cflag |= BOTHER; + uartattr2.c_ispeed = what_rate; + uartattr2.c_ospeed = what_rate; + if (ioctl(uart, TCSETS2, &uartattr2) == -1) { + error_at_line(0, errno, __FILE__, __LINE__, "ioctl TCSETS2"); + r = 2; + goto r; + } +r: + return r; +} +int main (int argc, char ** argv) { + int r = 0; + if (argc != 1+1) + error_at_line(1, 0, __FILE__, __LINE__, "usage: %s /dev/ttyUSB0", S0(argv[0])); + int uart = open(argv[1], O_WRONLY | O_NOCTTY | O_NDELAY | O_CLOEXEC); + if (uart == -1) + error_at_line(3, errno, __FILE__, __LINE__, "open"); + struct termios uartattr; + if (tcgetattr(uart, &uartattr) == -1) { + error_at_line(0, errno, __FILE__, __LINE__, "tcgetattr"); + r = 4; + goto r; + } + uartattr.c_iflag = 0; + uartattr.c_oflag = 0; + uartattr.c_cflag = CS8 | CSTOPB; + uartattr.c_lflag = 0; + if (tcflush(uart, TCIOFLUSH) == -1) { + error_at_line(0, errno, __FILE__, __LINE__, "tcflush"); + r = 5; + goto r; + } + if (tcsetattr(uart, TCSANOW, &uartattr) == -1) { + error_at_line(0, errno, __FILE__, __LINE__, "tcsetattr"); + r = 6; + goto r; + } + if (tcflush(uart, TCIOFLUSH) == -1) { + error_at_line(0, errno, __FILE__, __LINE__, "tcflush"); + r = 15; + goto r; + } + switch (rate(250000, uart)) { + case 1: + r = 20; + goto r; + break; + case 2: + r = 21; + goto r; + break; + default: + break; + } +r: + if (close(uart) == -1) + error_at_line(22, errno, __FILE__, __LINE__, "close(uart)"); + return r; +} |