- Main
- Tutorials
- Library
- Samples

- File List
- Globals

counter.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 * Counter Sample Application
6 *
7 * Date Author Comment
8 * 2018-12-04 Wanhan Hsieh 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 di_slot = 1;
56 uint8_t di_modes[16];
57 uint8_t counter_channel_start = 0;
58 uint8_t counter_channel_count = 2;
59 uint32_t counter_starts = 0;
60 uint8_t counter_triggers[4];
61 uint32_t counter_values[4];
62 int32_t i;
63 char ch;
64
65 while (-1 != (rc = getopt(argc, argv, "c:hn:s:")))
66 {
67 switch (rc)
68 {
69 case 'c':
70 counter_channel_start = atoi(optarg);
71 break;
72 case 'n':
73 counter_channel_count = atoi(optarg);
74 break;
75 case 's':
76 di_slot = atoi(optarg);
77 break;
78 case 'h':
79 default:
80 printf("Counter sample program.\n\n");
81 printf("Usage: ./counter [OPTIONS]\n\n");
82 printf("Options:\n");
83 printf("\t%-8s Slot of DI module. Default slot = %d\n", "-s", di_slot);
84 printf("\t%-8s Counter start channel. Default channel = %d\n", "-c", counter_channel_start);
85 printf("\t%-8s Counter channel count. Default count = %d\n", "-n", counter_channel_count);
86 printf("\n");
87 return 0;
88 }
89 }
90
91 printf("DI slot = %lu\n", di_slot);
92 printf("Counter start channel = %u\n", counter_channel_start);
93 printf("Counter channel count = %u\n", counter_channel_count);
94
95 // initialize ioThinx I/O
96 rc = ioThinx_IO_Client_Init();
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 < counter_channel_count; i++)
105 {
106 di_modes[i] = DI_MODE_COUNTER;
107 }
108 rc = ioThinx_DI_Config_SetModes(di_slot, counter_channel_start, counter_channel_count, di_modes);
109 if (rc != IOTHINX_ERR_OK)
110 {
111 printf("ioThinx_DI_Config_SetModes() = %d\n", rc);
112 }
113
114 for (i = 0; i < counter_channel_count; i++)
115 {
116 counter_triggers[i] = CNT_TRIGGER_BOTH;
117 }
118 rc = ioThinx_DI_Config_SetCntTriggers(di_slot, counter_channel_start, counter_channel_count, counter_triggers);
119 if (rc != IOTHINX_ERR_OK)
120 {
121 printf("ioThinx_DI_Config_SetCntTriggers() = %d\n", rc);
122 }
123
124 for (i = 0; i < counter_channel_count; i++)
125 {
126 counter_values[i] = 0;
127 }
128 rc = ioThinx_DI_Config_SetCntValues(di_slot, counter_channel_start, counter_channel_count, counter_values);
129 if (rc != IOTHINX_ERR_OK)
130 {
131 printf("ioThinx_DI_Config_SetCntValues() = %d\n", rc);
132 }
133
134 // reload config
135 rc = ioThinx_IO_Config_Reload();
136 if (rc != IOTHINX_ERR_OK)
137 {
138 printf("ioThinx_IO_Config_Reload() = %d\n", rc);
139 }
140
141 // start counter
142 counter_starts = 0;
143 for (i = 0; i < counter_channel_count; i++)
144 {
145 counter_starts |= 0x1 << (counter_channel_start + i);
146 }
147 rc = ioThinx_DI_SetCntStarts(di_slot, counter_starts);
148 if (rc != IOTHINX_ERR_OK)
149 {
150 printf("ioThinx_DI_SetCntStarts() = %d\n", rc);
151 }
152
153 // get value
154 do
155 {
156 rc = ioThinx_DI_GetCntValues(di_slot, counter_channel_start, counter_channel_count, counter_values);
157 if (rc != IOTHINX_ERR_OK)
158 {
159 printf("ioThinx_DI_GetCntValues() = %d\n", rc);
160 break;
161 }
162 for (i = 0; i < counter_channel_count; i++)
163 {
164 printf("[%lu:%u] counter = %d\n", di_slot, counter_channel_start+i, counter_values[i]);
165 }
166 ch = getchar();
167 }
168 while (ch != 'q');
169
170 return 0;
171 }
ioThinx_DI_SetCntStarts
IOTHINX_ERR ioThinx_DI_SetCntStarts(uint32_t slot, uint32_t starts)
ioThinx_DI_Config_SetModes
IOTHINX_ERR ioThinx_DI_Config_SetModes(uint32_t slot, uint8_t start, uint8_t count, uint8_t buf[])
ioThinx_DI_GetCntValues
IOTHINX_ERR ioThinx_DI_GetCntValues(uint32_t slot, uint8_t start, uint8_t count, uint32_t buf[])
IOTHINX_ERR_OK
#define IOTHINX_ERR_OK
Definition: iothinxio.h:35
CNT_TRIGGER_BOTH
#define CNT_TRIGGER_BOTH
Definition: iothinxio.h:97
DI_MODE_COUNTER
#define DI_MODE_COUNTER
Definition: iothinxio.h:87
ioThinx_IO_Config_Reload
IOTHINX_ERR ioThinx_IO_Config_Reload(void)
ioThinx_DI_Config_SetCntTriggers
IOTHINX_ERR ioThinx_DI_Config_SetCntTriggers(uint32_t slot, uint8_t start, uint8_t count, uint8_t buf[])
ioThinx_DI_Config_SetCntValues
IOTHINX_ERR ioThinx_DI_Config_SetCntValues(uint32_t slot, uint8_t start, uint8_t count, uint32_t buf[])
main
int main(int argc, char **const argv)
Definition: counter.c:52
ioThinx_IO_Client_Init
IOTHINX_ERR ioThinx_IO_Client_Init(void)