Example using high-level API

The following example demonstrates how a flink device can be opened and a subdevice selected. The subdevice is of type GPIO. One of it's channels is configured as output and set to true.

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <flinklib.h>
 
int main(void) {
  char*         DEFAULT_DEV = (char*)"/dev/flink0";
  flink_dev*    dev;
  flink_subdev* subdev;
  char*         dev_name = DEFAULT_DEV;
  uint32_t      subdevice_id = 0x12705002;
  uint32_t      channel = 0;
  int           error = 0;
 
  // Open flink device
  dev = flink_open(DEFAULT_DEV);
  if(dev == NULL) {
    fprintf(stderr, "Failed to open device %s!\n", dev_name);
    return -1;
  }
 
  // Get a pointer to the choosen subdevice
  subdev = flink_get_subdevice_by_unique_id(dev, subdevice_id );
  if(subdev == NULL) {
    fprintf(stderr, "Illegal subdevice id %d!\n", subdevice_id);
    return -1;
  }
 
  // Set I/O direction to output
  error = flink_dio_set_direction(subdev, channel, true);
  if(error != 0) {
    fprintf(stderr, "Configuring GPIO direction failed!\n");
    return -1;
  }
 
  // toggle an output for 20s
  uint8_t out = 1;
  for (int count = 0; count < 200; count++) {
    error = flink_dio_set_value(subdev, channel, out);
    if(error != 0) {
      fprintf(stderr, "Writing value failed!\n");
      return -1;
    }
    usleep(100000);
    out = !out;
  }
 
  // Close flink device
  flink_close(dev);
 
  return EXIT_SUCCESS;
}