Digital input

Digital input is accomplished by:

static int bits [16] = {
  0x0001, 0x0002, 0x0004, 0x0008,
  0x0010, 0x0020, 0x0040, 0x0080,
  0x0100, 0x0200, 0x0400, 0x0800,
  0x1000, 0x2000, 0x4000, 0x8000

pcl711_read_digital_bit (pcl711_t *pcl, int bitnum)
  bitnum &= 15;       // guarantee range

  if (bitnum < 8) {
    return (!!(in8 (pcl -> port + PCL711_DI_LOW) & bits [bitnum]));
  } else {
    return (!!(in8 (pcl -> port + PCL711_DI_HIGH) & bits [bitnum - 8]));

This function determines if the bit that's to be read is in the LOW or HIGH register, and then reads it. The read bit is then logically ANDed against the bits array to isolate the bit, and then the special Boolean typecast operator ("!!") that I invented a few years back is used to convert a zero or nonzero value to a zero or a one.

Note: The !! technique is something I discovered a few years back. It's 100% legal ANSI C and, more importantly, guaranteed to work. And it made my editor think there was a feature of C that he'd missed! :-)