Add trigger functionality
Some checks failed
Build eismuliplexer for windows / Build (push) Has been cancelled
Build eismuliplexer for linux / Build (push) Has been cancelled
Some checks failed
Build eismuliplexer for windows / Build (push) Has been cancelled
Build eismuliplexer for linux / Build (push) Has been cancelled
This commit is contained in:
parent
5c42094b06
commit
ba13899644
3 changed files with 197 additions and 2 deletions
107
eismultiplexer.c
107
eismultiplexer.c
|
|
@ -33,6 +33,39 @@
|
|||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#ifdef WIND32
|
||||
#include <synchapi.h>
|
||||
#endif
|
||||
|
||||
typedef enum
|
||||
{
|
||||
COMMAND_LED_OFF = 0,
|
||||
COMMAND_LED_ON,
|
||||
COMMAND_SET_OUTPUTS,
|
||||
COMMAND_GET_OUTPUTS,
|
||||
COMMAND_EEPROM_READ,
|
||||
COMMAND_EEPROM_WRITE,
|
||||
COMMAND_GET_GITREV,
|
||||
COMMAND_GET_CHANNEL_COUNT,
|
||||
COMMAND_GET_PROTO_VERSION,
|
||||
COMMAND_SET_TRIG,
|
||||
COMMAND_GET_TRIG,
|
||||
COMMAND_GET_TRIG_COUNT
|
||||
} command_t;
|
||||
|
||||
static void msleep(uint64_t milliseconds)
|
||||
{
|
||||
#ifdef WIN32
|
||||
Sleep(milliseconds);
|
||||
#else
|
||||
struct timespec ts;
|
||||
ts.tv_sec = milliseconds / 1000;
|
||||
ts.tv_nsec = (milliseconds % 1000) * 1000000;
|
||||
nanosleep(&ts, NULL);
|
||||
#endif
|
||||
}
|
||||
|
||||
uint16_t* eismultiplexer_list_available_devices(size_t* count)
|
||||
{
|
||||
|
|
@ -130,6 +163,80 @@ uint16_t eismultiplexer_read_eeprom(struct eismultiplexer* muliplexer, uint16_t
|
|||
return *((uint16_t*)buffer);
|
||||
}
|
||||
|
||||
int eismultiplexer_set_trigger_state(struct eismultiplexer* muliplexer, uint16_t index, trigger_state_t state)
|
||||
{
|
||||
int ret;
|
||||
ret = usbshm_write_control_transfer(muliplexer->priv, COMMAND_SET_TRIG, NULL, 0, state, index);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int eismultiplexer_get_trigger_state(struct eismultiplexer* muliplexer, uint16_t index, trigger_state_t* state, bool* level)
|
||||
{
|
||||
uint8_t buffer[2] = {};
|
||||
int ret = usbshm_read_control_transfer(muliplexer->priv, COMMAND_GET_TRIG, 0, index, buffer, 2);
|
||||
if(ret == 2) {
|
||||
if(state)
|
||||
*state = buffer[1];
|
||||
if(level)
|
||||
*level = buffer[0];
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int eismultiplexer_wait_trigger(struct eismultiplexer* muliplexer, uint16_t index, trigger_wait_t wait, uint64_t timeout)
|
||||
{
|
||||
bool level;
|
||||
bool prevlevel;
|
||||
int ret = eismultiplexer_get_trigger_state(muliplexer, index, NULL, &prevlevel);
|
||||
if(ret != 2)
|
||||
return ret;
|
||||
|
||||
struct timeval start;
|
||||
struct timeval current;
|
||||
gettimeofday(&start, NULL);
|
||||
gettimeofday(¤t, NULL);
|
||||
while((current.tv_sec - start.tv_sec)*1000 + (current.tv_usec - start.tv_usec)/1000 < timeout) {
|
||||
int ret = eismultiplexer_get_trigger_state(muliplexer, index, NULL, &level);
|
||||
if(ret != 2)
|
||||
return ret;
|
||||
switch(wait) {
|
||||
case TRIGGER_WAIT_HIGH_LEVEL:
|
||||
if(level)
|
||||
return 0;
|
||||
break;
|
||||
case TRIGGER_WAIT_LOW_LEVEL:
|
||||
if(!level)
|
||||
return 0;
|
||||
break;
|
||||
case TRIGGER_WAIT_EDGE:
|
||||
if(prevlevel != level)
|
||||
return 0;
|
||||
break;
|
||||
case TRIGGER_WAIT_HIGH:
|
||||
if(!prevlevel && level)
|
||||
return 0;
|
||||
break;
|
||||
case TRIGGER_WAIT_LOW:
|
||||
if(prevlevel && !level)
|
||||
return 0;
|
||||
break;
|
||||
}
|
||||
msleep(100);
|
||||
gettimeofday(¤t, NULL);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int eismultiplexer_get_trigger_count(struct eismultiplexer* muliplexer)
|
||||
{
|
||||
uint8_t buffer[1] = {};
|
||||
int ret = usbshm_read_control_transfer(muliplexer->priv, COMMAND_GET_TRIG, 0, 0, buffer, 1);
|
||||
if(ret >= 0)
|
||||
return buffer[0];
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
void eismultiplexer_disconnect(struct eismultiplexer* muliplexer)
|
||||
{
|
||||
usbshm_close(muliplexer->priv);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue