HPI example that uses GPIO.
#ifndef _MSC_VER
#include <unistd.h>
#endif
#include <getopt.h>
#ifdef __APPLE__
#include <hpi/hpidebug.h>
#else
#include <hpidebug.h>
#endif
#define verbose_printf if (verbose) printf
typedef struct {
uint16_t wControlType;
uint16_t wSrcNodeType;
uint16_t wSrcNodeIndex;
uint16_t wDstNodeType;
uint16_t wDstNodeIndex;
static int do_dump = 0;
static int input = -1;
static int set = -1;
static int clear = -1;
static int do_loop = -1;
static int wAdapterIndex = 0;
static int verbose = 1;
static struct option long_options[] = {
{"adapter", required_argument, 0, 'a'},
{"input", required_argument, 0, 'i'},
{"set", required_argument, 0, 's'},
{"clear", required_argument, 0, 'c'},
{"dump", no_argument, 0, 'd'},
{"loop", no_argument, 0, 'l' },
{"verbose", no_argument, 0, 'v' },
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0}
};
static const char *short_options = "a:i:s:c:dlvh?";
static const char *option_help[] = {
"<adapter number> to test.",
"<decimal value> input show GPIO input N (zero based)",
"<decimal value> set sets GPIO output N (zero based)",
"<decimal value> clear clears GPIO output N (zero based)",
"dump state of all GPIO inputs and outputs",
"loops, turning each GPIO output on and off continuously",
"turn on verbose output",
"Show this text."
};
static void help(void)
{
int i = 0;
printf("\nASIHPIGPIO - GPIO example code");
printf("\nUsage - asihpigpio [options]\n");
while (long_options[i].name != 0) {
printf("--%s -%c %s\n",
long_options[i].name,
(char)(long_options[i].val), option_help[i]);
i++;
}
exit(0);
}
static void parse_options(
int argc,
char *argv[]
)
{
int c;
while (1) {
int option_index = 0;
c = getopt_long(argc, argv, short_options,
long_options, &option_index);
if (c == -1)
break;
switch (c) {
case 0:
printf("option %s", long_options[option_index].name);
if (optarg)
printf(" with arg %s", optarg);
printf("\n");
break;
case 'a':
wAdapterIndex = atoi(optarg);
break;
case 'd':
do_dump = 1;
break;
case 's':
set = atoi(optarg);
break;
case 'c':
clear = atoi(optarg);
break;
case 'i':
input = atoi(optarg);
break;
case 'l':
do_loop = 1;
break;
case 'v':
verbose = 1;
break;
case '?':
case 'h':
help();
break;
default:
printf("?? getopt returned character code 0%o ??\n",
c);
}
}
if (optind < argc) {
}
}
{
size_t value_size = 0;
size_t value_items = 0;
uint8_t *value;
int i;
asihpi_control.wSrcNodeIndex = 0;
asihpi_control.wDstNodeType = 0;
asihpi_control.wDstNodeIndex = 0;
asihpi_control.wSrcNodeType, asihpi_control.wSrcNodeIndex,
asihpi_control.wDstNodeType, asihpi_control.wDstNodeIndex,
"GPIO",
&block );
if ( !err ) {
}
if ( !err ) {
NULL, &value_size, &value_items);
}
value = calloc(value_items, 1);
if (!value)
if ( !err ) {
value, value_size );
}
if (gpio_index >= 0) {
if (gpio_index >= (int)value_items) {
printf("Index %d out of range\n", gpio_index);
}
if ( !err ) {
if (val)
value[gpio_index] = 'T';
else
value[gpio_index] = 'F';
}
if ( !err ) {
value, value_size );
}
}
if ( !err ) {
if (gpio_index < 0) {
printf("GPIO Outputs: ");
for (i = 0; i < (int)value_items; i++)
printf("%c", value[i]);
printf("\n");
} else {
printf("GPIO Output [%d]: %c\n", gpio_index, value[gpio_index]);
}
}
if (err) {
printf("Error %d setting GPIO output.\n",err);
}
if (value)
free(value);
return err;
}
{
size_t value_size = 0;
size_t value_items = 0;
uint8_t *value;
int i;
asihpi_control.wSrcNodeIndex = 0;
asihpi_control.wDstNodeType = 0;
asihpi_control.wDstNodeIndex = 0;
asihpi_control.wSrcNodeType, asihpi_control.wSrcNodeIndex,
asihpi_control.wDstNodeType, asihpi_control.wDstNodeIndex,
"GPIO",
&block );
if ( !err ) {
}
if ( !err ) {
NULL, &value_size, &value_items);
}
value = calloc(value_items, 1);
if (!value)
if ( !err ) {
value, value_size );
}
if (gpio_index >= (int)value_items) {
printf("Index %d out of range\n", gpio_index);
}
if ( !err ) {
if (gpio_index < 0) {
printf("GPIO Inputs: ");
for (i = 0; i < (int)value_items; i++)
printf("%c", value[i]);
printf("\n");
} else {
printf("GPIO Input [%d]: %c\n",
gpio_index, value[gpio_index]);
}
}
if (err) {
printf("Error %d getting GPIO input.\n",err);
}
if (value)
free(value);
return err;
}
{
get_gpio_input(hMixer, -1);
set_gpio_output(hMixer, -1, 0);
}
int main(int argc, char *argv[])
{
hpi_hsubsys_t *hSubSys;
uint16_t wVersion;
uint32_t dwSerialNumber;
uint16_t wType;
uint16_t wNumOutStreams;
uint16_t wNumInStreams;
uint32_t i = 0;
long testcount = 0;
parse_options(argc, argv);
verbose_printf
("********************************************************************\n");
verbose_printf("\n** Test HPI using Functions **\n");
verbose_printf
("********************************************************************\n");
verbose_printf("HPI_SubSysCreate\n");
if (hSubSys == NULL) {
printf("hSubSys==NULL\n");
exit(1);
}
verbose_printf("HPI_AdapterClose \n");
HandleError(err);
verbose_printf("HPI_AdapterOpen \n");
HandleError(err);
wAdapterIndex,
&wNumOutStreams,
&wNumInStreams, &wVersion, &dwSerialNumber, &wType);
verbose_printf("HPI_AdapterGetInfo\n");
HandleError(err);
verbose_printf("Adapter ID=%4X Index=%d NumOutStreams=%d NumInStreams=%d S/N=%d\nHw Version %c%d DSP code version %03d\n", wType, wAdapterIndex, wNumOutStreams, wNumInStreams, dwSerialNumber, ((wVersion >> 3) & 0xf) + 'A',
wVersion & 0x7,
((wVersion >> 13) * 100) + ((wVersion >> 7) & 0x3f)
);
verbose_printf("HPI_MixerOpen: handle=%X\n", hMixer);
HandleError(err);
if ( do_dump ) {
dump_settings(hMixer);
}
if (input >= 0) {
get_gpio_input(hMixer, input);
}
if (set >= 0) {
set_gpio_output(hMixer, set, 1);
}
if (clear >= 0) {
set_gpio_output(hMixer, clear, 0);
}
if (do_loop >= 0) {
int i = 0;
while (1)
for (i = 0; i < 4; i++)
{
set_gpio_output(hMixer, i, 1);
dump_settings(hMixer);
Sleep(2000);
set_gpio_output(hMixer, i, 0);
printf("\n");
}
}
verbose_printf("\nHPI_MixerClose\n");
HandleError(err);
verbose_printf("HPI_AdapterClose\n");
HandleError(err);
return 0;
}
static int getch(void)
{
return getchar();
}
{
char szError[256];
char nK = 0;
if (err) {
HPI_GetErrorText(err, szError);
printf("ERROR %d %s\n", err, szError);
printf("press Enter to continue, (q,Enter) to exit...\n");
nK = getch();
if (nK == 'q')
exit(0);
}
}