/* ================================================================
** easydaq.[ch]
**
** Support functions for easyDAQ SERDIO8R relay card.
**
** 31 DEC 2008  Ryan Eustice  Created and written.
** ================================================================
*/

#include <stdio.h>
#include <time.h>
#include <errno.h>

#include "libcommon/error.h"
#include "libcommon/serial.h"

#include "easydaq.h"


#define EASYDAQ_READ    (0x41) // ascii 'A'
#define EASYDAQ_SETDIR  (0x42) // ascii 'B'
#define EASYDAQ_WRITE   (0x43) // ascii 'C'
#define EASYDAQ_DELAY   (1e4)  // useconds

int
easydaq_open (const char *device)
{
    int fd = serial_open (device, B9600, PARITY_8N1, 1);
    if (fd < 0) {
        ERROR ("serial_open");
        return -1;
    }
    
    if (serial_set_noncanonical (fd, 0, 1) != 0) {
        ERROR ("serial_set_noncanonical");
        return -1;
    }

    return fd;
}

int
easydaq_close (int fd)
{
    return serial_close (fd);
}

int
easydaq_setdir (int fd, uint8_t byte)
{
    int ret;
    uint8_t cmd[2];

    usleep (EASYDAQ_DELAY);

    cmd[0] = EASYDAQ_SETDIR;
    cmd[1] = byte;
    ret = write (fd, cmd, 2);
    if (ret!=2)
        return -1;
    else
        tcdrain (fd);

    return 0;
}

int
easydaq_read (int fd, uint8_t *byte)
{
    int ret;
    uint8_t cmd[2];

    usleep  (EASYDAQ_DELAY);
    tcflush (fd, TCIFLUSH);

    cmd[0] = EASYDAQ_READ;
    cmd[1] = 0x00;
    ret = write (fd, cmd, 2);
    if (ret != 2)
        return -1;
    else
        tcdrain (fd);

    ret = read (fd, byte, 1);
    if (ret != 1)
        return -1;
    else
        return 0;
}

int
easydaq_write (int fd, uint8_t byte)
{
    int ret;
    uint8_t cmd[2];

    usleep (EASYDAQ_DELAY);

    cmd[0] = EASYDAQ_WRITE;
    cmd[1] = byte;
    ret = write (fd, cmd, 2);
    if (ret != 2)
        return -1;
    else
        tcdrain (fd);

    return 0;
}

