di.c
Go to the documentation of this file.
1 /*******************************************************************************
2  * Copyright (C) 2019 Moxa Inc. All rights reserved.
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * DI Sample Application
6  *
7  * Date Author Comment
8  * 2018-12-04 Wanhan Hsieh Created it.
9  ******************************************************************************/
10 
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <unistd.h>
50 #include <iothinx/iothinxio.h>
51 
53 int main(int argc, char **const argv)
54 {
55  int32_t rc;
56  uint32_t di_slot = 1;
57  uint8_t di_channel = 0;
58  uint16_t di_filter = 2;
59  uint32_t di_values = 0;
60  char ch;
61 
62  while (-1 != (rc = getopt(argc, argv, "c:f:hs:")))
63  {
64  switch (rc)
65  {
66  case 'c':
67  di_channel = atoi(optarg);
68  break;
69  case 'f':
70  di_filter = atoi(optarg);
71  break;
72  case 's':
73  di_slot = atoi(optarg);
74  break;
75  case 'h':
76  default:
77  printf("DI sample program.\n\n");
78  printf("Usage: ./di [OPTIONS]\n\n");
79  printf("Options:\n");
80  printf("\t%-8s Slot of DI module. Default slot = %d\n", "-s", di_slot);
81  printf("\t%-8s DI channel. Default channel = %d\n", "-c", di_channel);
82  printf("\t%-8s Filter of DI module. Default filter = %d\n", "-f", di_filter);
83  printf("\t%-8s (One unit = 500us)\n", "");
84  printf("\n");
85  return 0;
86  }
87  }
88 
89  printf("DI slot = %lu\n", di_slot);
90  printf("DI channel = %u\n", di_channel);
91  printf("DI filter = %u\n", di_filter);
92 
93  // initialize ioThinx I/O
95  if (rc != IOTHINX_ERR_OK)
96  {
97  printf("ioThinx_IO_Client_Init() = %d\n", rc);
98  return -1;
99  }
100 
101  // temporarily set config
102  rc = ioThinx_DI_Config_SetFilters(di_slot, di_channel, 1, &di_filter);
103  if (rc != IOTHINX_ERR_OK)
104  {
105  printf("ioThinx_DI_Config_SetFilters() = %d\n", rc);
106  }
107 
108  // reload config
110  if (rc != IOTHINX_ERR_OK)
111  {
112  printf("ioThinx_IO_Config_Reload() = %d\n", rc);
113  }
114 
115  do
116  {
117  rc = ioThinx_DI_GetValues(di_slot, &di_values);
118  if (rc != IOTHINX_ERR_OK)
119  {
120  printf("ioThinx_DI_GetValues() = %d\n", rc);
121  break;
122  }
123  printf("[%lu] di_values = 0x%x\n", di_slot, di_values);
124  printf("[%lu:%u] di_val = %x\n", di_slot, di_channel, (di_values >> di_channel) & 0x1);
125  ch = getchar();
126  }
127  while (ch != 'q');
128 
129  return 0;
130 }
#define IOTHINX_ERR_OK
Definition: iothinxio.h:35
int main(int argc, char **const argv)
Definition: di.c:53
IOTHINX_ERR ioThinx_DI_Config_SetFilters(uint32_t slot, uint8_t start, uint8_t count, uint16_t buf[])
IOTHINX_ERR ioThinx_IO_Config_Reload(void)
IOTHINX_ERR ioThinx_DI_GetValues(uint32_t slot, uint32_t *p_values)
IOTHINX_ERR ioThinx_IO_Client_Init(void)