NAME

ibuf - digital input buffer user functions

SYNOPSIS

#include <ibuf_u.h>

ibread (input)
unsigned *input;

ibufx (b)
int b;

ibufl (wflag, b1, b2, ..., z)
int wflag;
int b1, b2, ..., z;

ibufv (wflag, bvec)
int wflag;
int *bvec;

ibset (nimput, pindex, b, store, tstore, flags)
unsigned *store, *istore, flags);

ibsetv(ninput, pindex, bv, store, tstore, flags)
int ninput *pindex, *bv;
unsigned *store, *istore, flags;

ibbeg

ibend

DESCRIPTION

The digital-input buffer routines montior and control a 16-bit digital-input buffer that registers external signals such as subject button presses. One set of routines works under program control (interrupts disabled). The other set of routines is for operationg the digital input buffer under interrupt control.

Program control
ibread, ibufx, ibufl, and ibufv read and monitor the digital input buffer under program control.

ibread reads the entire 16-bit digital-input buffer into the address passed as input.

ibufx inspects a single bit b where b can equal 1 - 16. If b is set, ibuf returns 1; if clear, 0 is returned.

ibufl and ibufv monitor the status of multiple bits in the digital-input buffer. ibufl accepts a variable length list of arguments that specify which bits to monitor and what status to check for. The last argument in the list must be 0. Bit specifications can range from -1 to -16 and from 1 to 16. If a bit specification is negative, ibufl checks for a clear (0) status for that bit; if a bit specification is positive, ibufl checks for a set (1) status for that bit. ibufl will begin checking each bit in the order the bits appear in list. When it finds a bit with the requested status it returns the index to the position of the bit in the argument list. The index ranges from 0 to n-1 where n is the number of bits specified in the argument list excluding the final 0. If wflag equals NOWAIT, then if after checking each bit once, none is found with the requested status, ibufl will return the value n. If wflag is WAIT then ibufl will continue checking bits until it fings a bit with the requested status.

ibufv is exactly like ibufl except that it takes a single argument that is a pointer to an array of bits to monitor instead of a multiple length argument list. The final value in the array must be 0. The value returned by ibufv is an index into this array just as the value returned by ibufl specifies a position in the argument list.

Interrupt Control
The set of routines for using the digital input buffer under interrupt control includes ibset, ibsetv, ibbeg, and ibend.

ibset and ibsetv initialize the digital input buffer for collecting and storing multiple input values under interrupt control.

b (in ibset ) specifies a value ranging from 1-16 that corresponds to one of the bits in the digital input buffer. That bit will cause an interrupt each time it changes state. bv (in ibsetv ) is the address of an array of values corresponding to digital input bits. The last element in the array must be 0. Each bit specified in bv will cause an interrupt every time it changes state.

ninput is the number of values to collect.

store points to an array for storing the number corresponding to the bit that caused the current interrupt. The sign of the number indicates whether the bit was on (positive) or off (negative). For example, if the interrupt was generated by bit 5 turning off, then -5 is stored. The array should be able to hold at least ninput elements.

pindex is the address of an index for keeping track of the last position filled in store. ibset initializes it to -1; however, the user may reset it anytime aftwerwards. To begin storing samples at the nth element in store (where n ranges from 0 to ninput -1), the index must be set to n -1.

tstore points to an array for storing the time at which an interrupt occurs. It assumes that the clock is running at the time of the interrupt. If tstore is NULL then times are not collected.

flags contains two optional pieces of information. If set to TLONG then the clock values are stored in a long rather than unsigned array. If set to BUF then the value stored is the 16-bit digital input buffer showing the current status of enabled bits (bits that are not enabled are set to 0). For example, suppose bits 14, 15, and 16 were specified in bv. If the interrupt is caused by bit 16 turning on, and bit 14 is already on while bit 15 is off value 000005 is stored in the next position in store. The flags field may be set to both TLONG and BUF by or'ing them together( TLONG | BUF ). If neither option is desired, flags should be set to 0.

ibset and ibsetv arrange for the software signal SIGIB to be ignored. The user may choose not to ignore the signal by calling signal(2F) after calling ibset or ibsetv and specifying the address of a function for catching this signal.

While ibset and ibsetv initialize interrupt controlled reading of the input buffer, interrupts are not enable until ibbeg is invoked. Then each time one of the bits specified by ibset or ibsetv changes state, the input value is stored in the user array and (if times are to be collected) the clock is read and the time is stored in the user time array. If signals are not being ignored, the user-specified signal catching function is called with the input value as the third argument. Digital inputs will continue to be processed until the value at pindex reaches ninput or until the user invokes ibend. To process indefinitely, the user may keep resettling the index to -1 before it reaches ninput.

EXAMPLES

Program Control
Given the following call to ibufl:
int pos;
pos = ibufl(NOWAIT,3,2,-1,-4,0);
ibufl first checks that bit 3 = 1, bit 1 = 0 or bit 4 = 0 in that order. Because wflag equals NOWAIT, ibufl will check each bit in the list only once. Given that all four bits are clear, ibufl will return 2 to indicate that the third bit in the argument list (-1) was the first encountered with the requested status. If bits 3 and 2 are clear and bits 1 and 4 are set, in other words, no bit has a requested status, then the value 4 is returned.

Interrupt Control

#include <stdio_p.h>
#include <ibuf_u.h>
/*
* collect 50 values and times from bits 1, 3, and 5
*/
#define NN 50;
int bv[] = {1,3,5,0}, index;
unsigned store[NN];
long store[NN];

main()
{
  if (ibsetv(NN, &index, bv, store, tstore, TLONG) == -1)
    errexit(1, "ibsetv error0");
  ibbeg;
  while(index < NN){
	.
	.
	.
  }
  ibend;
}

FILES

/dev/ib0

SEE ALSO

ibuf(4F) , signal(2F).

DIAGNOSTICS

If passed an illegal bit number, ibufx, ibufl, ibufv, ibset, and ibsetv will return -1. If arguments pindex or store to ibsetv are NULL then -1 is returned.