do.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  * DO Sample Application
6  *
7  * Date Author Comment
8  * 2018-12-25 Eddy Chiu Created it.
9  ******************************************************************************/
10 
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <unistd.h>
47 #include <iothinx/iothinxio.h>
48 
50 int main(int argc, char **const argv)
51 {
52  int32_t rc;
53  uint32_t do_slot = 2;
54  uint8_t do_channel = 0;
55  uint8_t do_mode = DO_MODE_DO;
56  uint32_t do_values = 0;
57  char ch;
58 
59  while (-1 != (rc = getopt(argc, argv, "c:hs:")))
60  {
61  switch (rc)
62  {
63  case 's':
64  do_slot = atoi(optarg);
65  break;
66  case 'c':
67  do_channel = atoi(optarg);
68  break;
69  case 'h':
70  default:
71  printf("DO sample program.\n\n");
72  printf("Usage: ./do [OPTIONS]\n\n");
73  printf("Options:\n");
74  printf("\t%-8s Slot of DO module. Default slot = %d\n", "-s", do_slot);
75  printf("\t%-8s DO channel. Default channel = %d\n", "-c", do_channel);
76  printf("\n");
77  return 0;
78  }
79  }
80 
81  printf("DO slot = %lu\n", do_slot);
82  printf("DO channel = %u\n", do_channel);
83 
84  // initialize ioThinx I/O
86  if (rc != IOTHINX_ERR_OK)
87  {
88  printf("ioThinx_IO_Client_Init() = %d\n", rc);
89  return -1;
90  }
91 
92  // temporarily set config
93  rc = ioThinx_DO_Config_SetModes(do_slot, do_channel, 1, &do_mode);
94  if (rc != IOTHINX_ERR_OK)
95  {
96  printf("ioThinx_DO_Config_SetModes() = %d\n", rc);
97  }
98 
99  // reload config
101  if (rc != IOTHINX_ERR_OK)
102  {
103  printf("ioThinx_IO_Config_Reload() = %d\n", rc);
104  }
105 
106  rc = ioThinx_DO_GetValues(do_slot, &do_values);
107  if (rc != IOTHINX_ERR_OK)
108  {
109  printf("ioThinx_DO_GetValues() = %d\n", rc);
110  }
111 
112  // set value
113  do
114  {
115  // invert do value
116  if (do_values & (0x1 << do_channel))
117  {
118  do_values &= ~(0x1 << do_channel);
119  }
120  else
121  {
122  do_values |= 0x1 << do_channel;
123  }
124 
125  rc = ioThinx_DO_SetValues(do_slot, do_values);
126  if (rc != IOTHINX_ERR_OK)
127  {
128  printf("ioThinx_DO_SetValues() = %d\n", rc);
129  break;
130  }
131  printf("[%lu:%u] DO value = %x\n", do_slot, do_channel, (do_values >> do_channel) & 0x1);
132  ch = getchar();
133  }
134  while (ch != 'q');
135 
136  return 0;
137 }
#define DO_MODE_DO
Definition: iothinxio.h:291
int main(int argc, char **const argv)
Definition: do.c:50
IOTHINX_ERR ioThinx_DO_SetValues(uint32_t slot, uint32_t values)
#define IOTHINX_ERR_OK
Definition: iothinxio.h:35
IOTHINX_ERR ioThinx_DO_Config_SetModes(uint32_t slot, uint8_t start, uint8_t count, uint8_t buf[])
IOTHINX_ERR ioThinx_IO_Config_Reload(void)
IOTHINX_ERR ioThinx_IO_Client_Init(void)
IOTHINX_ERR ioThinx_DO_GetValues(uint32_t slot, uint32_t *p_values)