ai.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  * AI Sample Application
6  *
7  * Date Author Comment
8  * 2018-12-27 Eddy Chiu Created it.
9  ******************************************************************************/
10 
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <unistd.h>
49 #include <iothinx/iothinxio.h>
50 
52 int main(int argc, char **const argv)
53 {
54  int32_t rc;
55  uint32_t ai_slot = 4;
56  uint8_t ai_channel_start = 0;
57  uint8_t ai_channel_count = 8;
58  uint8_t ai_ranges[8];
59  float ai_burnouts[8];
60  float ai_engs[8];
61  uint8_t ai_statuss[8];
62  int32_t i;
63  char ch;
64 
65  while (-1 != (rc = getopt(argc, argv, "c:n:hs:")))
66  {
67  switch (rc)
68  {
69  case 's':
70  ai_slot = atoi(optarg);
71  break;
72  case 'c':
73  ai_channel_start = atoi(optarg);
74  break;
75  case 'n':
76  ai_channel_count = atoi(optarg);
77  break;
78  case 'h':
79  default:
80  printf("AI sample program.\n\n");
81  printf("Usage: ./ai [OPTIONS]\n\n");
82  printf("Options:\n");
83  printf("\t%-8s Slot of AI module. Default slot = %d\n", "-s", ai_slot);
84  printf("\t%-8s AI start channel. Default channel = %d\n", "-c", ai_channel_start);
85  printf("\t%-8s AI channel count. Default count = %d\n", "-n", ai_channel_count);
86  printf("\n");
87  return 0;
88  }
89  }
90 
91  printf("AI slot = %lu\n", ai_slot);
92  printf("AI start channel = %u\n", ai_channel_start);
93  printf("AI channel count = %u\n", ai_channel_count);
94 
95  // initialize ioThinx I/O
97  if (rc != IOTHINX_ERR_OK)
98  {
99  printf("ioThinx_IO_Client_Init() = %d\n", rc);
100  return -1;
101  }
102 
103  // temporarily set config
104  for (i = 0; i < ai_channel_count; i++)
105  {
106  ai_ranges[i] = AI_RANGE_4_20mA_BURNOUT;
107  }
108  rc = ioThinx_AI_Config_SetRanges(ai_slot, ai_channel_start, ai_channel_count, ai_ranges);
109  if (rc != IOTHINX_ERR_OK)
110  {
111  printf("ioThinx_AI_Config_SetRanges() = %d\n", rc);
112  }
113  for (i = 0; i < ai_channel_count; i++)
114  {
115  ai_burnouts[i] = 2.0f;
116  }
117  rc = ioThinx_AI_Config_SetBurnoutValues(ai_slot, ai_channel_start, ai_channel_count, ai_burnouts);
118  if (rc != IOTHINX_ERR_OK)
119  {
120  printf("ioThinx_AI_Config_SetBurnoutValues() = %d\n", rc);
121  }
122 
123  // reload config
125  if (rc != IOTHINX_ERR_OK)
126  {
127  printf("ioThinx_IO_Config_Reload() = %d\n", rc);
128  }
129 
130  // get value
131  do
132  {
133  rc = ioThinx_AI_GetEngs(ai_slot, ai_channel_start, ai_channel_count, ai_engs);
134  if (rc != IOTHINX_ERR_OK)
135  {
136  printf("ioThinx_AI_GetEngs() = %d\n", rc);
137  break;
138  }
139  rc = ioThinx_AI_GetStatuss(ai_slot, ai_channel_start, ai_channel_count, ai_statuss);
140  if (rc != IOTHINX_ERR_OK)
141  {
142  printf("ioThinx_AI_GetStatuss() = %d\n", rc);
143  break;
144  }
145  for (i = 0; i < ai_channel_count; i++)
146  {
147  printf("[%lu:%u] eng = %.3f, status = %d\n", ai_slot, ai_channel_start+i, ai_engs[i], ai_statuss[i]);
148  }
149  ch = getchar();
150  }
151  while (ch != 'q');
152 
153  return 0;
154 }
#define AI_RANGE_4_20mA_BURNOUT
Definition: iothinxio.h:543
#define IOTHINX_ERR_OK
Definition: iothinxio.h:35
int main(int argc, char **const argv)
Definition: ai.c:52
IOTHINX_ERR ioThinx_AI_GetEngs(uint32_t slot, uint8_t start, uint8_t count, float buf[])
IOTHINX_ERR ioThinx_IO_Config_Reload(void)
IOTHINX_ERR ioThinx_AI_Config_SetBurnoutValues(uint32_t slot, uint8_t start, uint8_t count, float buf[])
IOTHINX_ERR ioThinx_AI_Config_SetRanges(uint32_t slot, uint8_t start, uint8_t count, uint8_t buf[])
IOTHINX_ERR ioThinx_IO_Client_Init(void)
IOTHINX_ERR ioThinx_AI_GetStatuss(uint32_t slot, uint8_t start, uint8_t count, uint8_t buf[])