| |
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/select.h>
#include "qlib.h"
int kbhit(void)
{
struct timeval tv;
fd_set read_fd;
/* Do not wait at all, not even a microsecond */
tv.tv_sec=0;
tv.tv_usec=0;
/* Must be done first to initialize read_fd */
FD_ZERO(&read_fd);
/* Makes select() ask if input is ready:
* 0 is the file descriptor for stdin */
FD_SET(0,&read_fd);
/* The first parameter is the number of the
* largest file descriptor to check + 1. */
if(select(1, &read_fd, NULL, NULL, &tv) == -1)
return 0; /* An error occured */
/* read_fd now holds a bit map of files that are
* readable. We test the entry for the standard
* input (file 0). */
if(FD_ISSET(0,&read_fd))
/* Character pending on stdin */
return 1;
/* no characters were pending */
return 0;
}
int main(int argc, char **argv)
{
ULONG handle;
char ch;
int j;
//
// This sample works for USBOPTOREL32 and USBOPTOIO32 Modules
//
//
// Open the USB Module
//
handle = QAPIExtOpenCard(USBOPTOREL32,0);
if ( handle == 0 )
{
handle = QAPIExtOpenCard(USBOPTOIO32,0);
}
//
// If there are no modules terminate application
//
if ( handle == 0 )
{
printf("No USB Modules found!\n");
return FALSE;
}
// Ok, we found a QUANCOM USB Module
// ---------------------------------------------------------------------------
// PART 1: Setting the outputs
//
// The following constants can be used to program the outputs:
// ---------------------------------------------------------------------------
#define OUT1 0x1
#define OUT2 0x2
#define OUT3 0x4
#define OUT4 0x8
#define OUT5 0x10
#define OUT6 0x20
#define OUT7 0x40
#define OUT8 0x80
#define OUT9 0x100
#define OUT10 0x200
#define OUT11 0x400
#define OUT12 0x800
#define OUT13 0x1000
#define OUT14 0x2000
#define OUT15 0x4000
#define OUT16 0x8000
#define OUT17 0x10000
#define OUT18 0x20000
#define OUT19 0x40000
#define OUT20 0x80000
#define OUT21 0x100000
#define OUT22 0x200000
#define OUT23 0x400000
#define OUT24 0x800000
#define OUT25 0x1000000
#define OUT26 0x2000000
#define OUT27 0x4000000
#define OUT28 0x8000000
#define OUT29 0x10000000
#define OUT30 0x20000000
#define OUT31 0x40000000
#define OUT32 0x80000000
ULONG lines = 0;
//
// Reset all lines to "Low"
//
printf("Reset all lines to 'Low' ( Press return to continue ):\n");
QAPIExtWriteDO32(handle, 0, lines, 0);
ch = getchar();
//
// Set the outputs OUT4,OUT10 and OUT15 to "High" ( 16-Bit )
//
printf("Set OUT4,OUT10,OUT15 and OUT30 to 'High' ( Press return to continue ):\n");
lines = OUT4 + OUT10 + OUT15 + OUT30;
QAPIExtWriteDO32(handle, 0, lines, 0);
ch = getchar();
//
// Set the output OUT1, OUT4,OUT10 and OUT15 to "High" ( 16-Bit )
//
printf("Set OUT1, OUT4,OUT10 and OUT15 to 'High' ( Press return to continue ):\n");
lines = OUT1 + OUT4 + OUT10 + OUT15;
QAPIExtWriteDO32(handle, 0, lines, 0);
ch = getchar();
//
// Reset line OUT10 to "Low"
//
printf("Reset line OUT10 to 'Low' ( Press return to continue ):\n");
QAPIExtWriteDO1(handle, 10 - 1, FALSE, 0);
ch = getchar();
//
// Set line OUT5 to "High"
//
printf("Set line OUT5 to 'High' ( Press return to continue ):\n");
QAPIExtWriteDO1(handle, 5 - 1, TRUE, 0);
ch = getchar();
//
// Reset all lines to "Low"
//
printf("Reset all to 'Low' ( Press return to continue ):\n");
lines = 0;
QAPIExtWriteDO32(handle, 0, lines, 0);
ch = getchar();
// ---------------------------------------------------------------------------
// PART 2: Reading the inputs ( and detect changed inputs )
//
// This part reads the state of the input lines.
//
// The following constants can be used to program the inputs:
// ---------------------------------------------------------------------------
int i;
while (!kbhit())
{
// read the current state from the inputs
lines = QAPIExtReadDI32(handle, 0, 0);
printf("\n--------------------------------------------------------------------------------\n");
printf("Current input states\n");
printf("IN1 IN2 IN3 IN4 IN5 IN6 IN7 IN8 IN9 IN10 IN11 IN12 IN13 IN14 IN15 IN16\n");
for (i=0;i<16;i++)
{
if ( lines & 1<<i)
{
printf(" 1 ");
}
else
{
printf(" 0 ");
}
}
printf("\nIN17 IN18 IN19 IN20 IN21 IN22 IN23 IN24 IN25 IN26 IN27 IN28 IN29 IN30 IN31 IN32\n");
for (i=16;i<32;i++)
{
if ( lines & 1<<i)
{
printf(" 1 ");
}
else
{
printf(" 0 ");
}
}
// read the state change ff from the input lines
lines = QAPIExtSpecial(handle, JOB_READ_IN_FFS, 0, 0);
printf("\n--------------------------------------------------------------------------------\n");
printf("Input change detection 1 = Input has changed its state since last reading\n");
printf("IN1 IN2 IN3 IN4 IN5 IN6 IN7 IN8 IN9 IN10 IN11 IN12 IN13 IN14 IN15 IN16\n");
for (i=0;i<16;i++)
{
if ( lines & 1<<i)
{
printf(" 1 ");
}
else
{
printf(" 0 ");
}
}
printf("\nIN17 IN18 IN19 IN20 IN21 IN22 IN23 IN24 IN25 IN26 IN27 IN28 IN29 IN30 IN31 IN32\n");
for (i=16;i<32;i++)
{
if ( lines & 1<<i)
{
printf(" 1 ");
}
else
{
printf(" 0 ");
}
}
printf("\n--------------------------------------------------------------------------------\n");
printf("Press return to stop reading the inputs every 5 seconds ...\n");
for (j=0;(j<5) && !kbhit();j++)
{
sleep(1);
}
}
ch = getchar();
// ---------------------------------------------------------------------------
// PART 3: Enabling and disabling the timeout control
//
// The timeout control is a security feature. If the application
// crashes or fails to set the outputs in a predefined time
// the module resets the outputs to 'low'. This is to prevent that
// connected devices ( i.e. motors, heaters, lamps, ... )
// continue to run when the software has lost control.
// ---------------------------------------------------------------------------
printf("Testing timeout function ...\n");
printf("Set OUT1,OUT2,OUT5,OUT10,OUT15 and OUT31 to 'high'\n");
// writing to OUTx pins
QAPIExtWriteDO32(handle, 0, OUT1+OUT2+OUT5+OUT10+OUT15+OUT31, 0);
printf("Setting timout time to 22.4s\n");
// first disable timeout
QAPIExtSpecial(handle, JOB_DISABLE_TIMEOUT, 0, 0);
// now, set timeout time
lines = QAPIExtSpecial(handle, JOB_SET_WATCHDOG_TIME, 6, 0);
printf("Enabling the timout control.\n");
// reset timeout status bit, this may be set by previous testing
QAPIExtSpecial(handle, JOB_RESET_TIMEOUT_STATUS, 0, 0);
// enable timeout control
QAPIExtSpecial(handle, JOB_ENABLE_TIMEOUT, 0, 0);
// this part waits for 40s without any write to the usb module
// after 22.4 seconds the hardware watchdog on the board
// sets all outputs to 'low'
printf("Now we wait for 40 seconds. This simulates a crash !\n");
for (j=0;j<40;j++)
{
sleep(1);
printf(".");
}
printf("\nAll OUT Pins should be reset now ! Press a key to continue !");
ch = getchar();
ULONG status = QAPIExtSpecial(handle, JOB_READ_TIMEOUT_STATUS, 0, 0);
if ( status )
printf("\nTimeout flag was set\n");
else
printf("\nNo timeout flag set\n");
// first disable timeout
QAPIExtSpecial(handle, JOB_DISABLE_TIMEOUT, 0, 0);
// reset timeout status bit
QAPIExtSpecial(handle, JOB_RESET_TIMEOUT_STATUS, 0, 0);
printf("\nReady ! Press a key to continue !");
ch = getchar();
QAPIExtCloseCard(handle);
return 0;
}
|
|