diff options
author | Matthias P. Braendli <matthias.braendli@mpb.li> | 2014-03-28 11:49:21 +0100 |
---|---|---|
committer | Matthias P. Braendli <matthias.braendli@mpb.li> | 2014-03-28 11:49:21 +0100 |
commit | 71004263abe2ea01b39798204d02262036ad6be7 (patch) | |
tree | 0b505c7553e40222f85cff9d740d9035cf3d4afc /xpad.c | |
parent | e354907d7e3c915821bb680787689bf2f7af224c (diff) | |
download | toolame-dab-71004263abe2ea01b39798204d02262036ad6be7.tar.gz toolame-dab-71004263abe2ea01b39798204d02262036ad6be7.tar.bz2 toolame-dab-71004263abe2ea01b39798204d02262036ad6be7.zip |
Add support for mot-encoder fifo
Diffstat (limited to 'xpad.c')
-rw-r--r-- | xpad.c | 70 |
1 files changed, 53 insertions, 17 deletions
@@ -6,6 +6,7 @@ #include <sys/stat.h> #include <fcntl.h> #include <assert.h> +#include <errno.h> #include "xpad.h" @@ -22,31 +23,66 @@ static int xpad_fd = 0; it generates */ -int xpad_len() { - if (xpad_fd == 0) { - xpad_fd = open("/home/bram/dab/mot-slideshow.file", O_RDONLY); - if (xpad_fd < 0) { - perror("Failed to open xpad file"); - exit(1); +/* Create and open the desired PAD input fifo + */ +int xpad_init(char* pad_fifo, int pad_len) +{ + if (mkfifo(pad_fifo, S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH) != 0) { + if (errno != EEXIST) { + fprintf(stderr, "Can't create pad file: %d!\n", errno); + return -1; } } - return 1; -} -uint8_t xpad_byte(void) { - uint8_t dat; + xpad_fd = open(pad_fifo, O_RDONLY | O_NONBLOCK); + if (xpad_fd == -1) { + fprintf(stderr, "Can't open pad file!\n"); + return -1; + } + + int flags = fcntl(xpad_fd, F_GETFL, 0); + if (fcntl(xpad_fd, F_SETFL, flags | O_NONBLOCK)) { + fprintf(stderr, "Can't set non-blocking mode in pad file!\n"); + return -1; + } + + return 0; +} +int xpad_read_len(uint8_t* buf, int len) +{ assert(xpad_fd != 0); - ssize_t num_read = read(xpad_fd, &dat, 1); - if (num_read == 0) { - fprintf(stderr, "xpad rewind\n"); - lseek(xpad_fd, 0, SEEK_SET); - num_read = read(xpad_fd, &dat, 1); + ssize_t num_read = 0; + + while (num_read < len) { + ssize_t r = read(xpad_fd, buf, len); + + if(r < 0) { + if (errno == EAGAIN) { + return 0; + } + else { + perror("PAD input read error"); + return -1; + } + } + else if (r == 0) { + // reached end of data + return 0; + } + + num_read += r; + } - assert(num_read == 1); +#if XPAD_DEBUG + int i; + for (i = 0; i < len; i++) { + fprintf(stderr, "%02x ", buf[i]); } + fprintf(stderr, "\n"); +#endif - return dat; + return num_read; } |