- Main
- Tutorials
- Library
- Samples

- File List
- Globals

misc.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 * Miscellaneous Sample Application
6 *
7 * Date Author Comment
8 * 2019-01-22 Wanhan Hsieh Created it.
9 ******************************************************************************/
10
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <unistd.h>
45 #include <iothinx/iothinxio.h>
46
48 int main(int argc, char **const argv)
49 {
50 int32_t rc;
51 uint32_t misc_slot = 0;
52 uint32_t module_slot = 1;
53 uint32_t module_count = 0;
54 struct MODULE_INFO module_info;
55 uint8_t rs_state = 0;
56 uint8_t pbtn_state = 0;
57 uint8_t led_state = 0;
58 char ch;
59
60 while (-1 != (rc = getopt(argc, argv, "hs:")))
61 {
62 switch (rc)
63 {
64 case 's':
65 module_slot = atoi(optarg);
66 break;
67 case 'h':
68 default:
69 printf("Miscellaneous sample program.\n\n");
70 printf("Usage: ./misc [OPTIONS]\n\n");
71 printf("Options:\n");
72 printf("\t%-8s Slot of the module. Default slot = %d\n", "-s", module_slot);
73 printf("\n");
74 return 0;
75 }
76 }
77
78 // initialize ioThinx I/O
79 rc = ioThinx_IO_Client_Init();
80 if (rc != IOTHINX_ERR_OK)
81 {
82 printf("ioThinx_IO_Client_Init() = %d\n", rc);
83 return -1;
84 }
85
86 // module infomation
87 rc = ioThinx_Misc_GetModuleCount(&module_count);
88 if (rc != IOTHINX_ERR_OK)
89 {
90 printf("ioThinx_Misc_GetModuleCount() = %d\n", rc);
91 }
92 printf("Module count = %lu\n", module_count);
93 printf("Module slot = %lu\n", module_slot);
94 rc = ioThinx_Misc_GetModuleInfo(module_slot, &module_info);
95 if (rc != IOTHINX_ERR_OK)
96 {
97 printf("ioThinx_Misc_GetModuleInfo() = %d\n", rc);
98 }
99 else
100 {
101 printf("Slot %d Module Information:\n", module_slot);
102 printf("Model Name: %s, ", module_info.model_name);
103 printf("Serial Number: %s\n", module_info.serial_number);
104 }
105
106 // locating
107 rc = ioThinx_Misc_SetLocateState(module_slot, 1);
108 if (rc != IOTHINX_ERR_OK)
109 {
110 printf("ioThinx_Misc_SetLocateState() = %d\n", rc);
111 }
112 else
113 {
114 printf("Slot %d: Locating...\n", module_slot);
115 }
116 printf("Press enter to stop locate.");
117 ch = getchar();
118 rc = ioThinx_Misc_SetLocateState(module_slot, 0);
119 if (rc != IOTHINX_ERR_OK)
120 {
121 printf("ioThinx_Misc_SetLocateState() = %d\n", rc);
122 }
123
124 // rotary switch
125 ioThinx_Misc_GetRotarySwitchState(misc_slot, &rs_state);
126 if (rc != IOTHINX_ERR_OK)
127 {
128 printf("ioThinx_Misc_GetRotarySwitchState() = %d\n", rc);
129 }
130 else
131 {
132 printf("Rotary switch state = %d\n", rs_state);
133 }
134
135 // push button
136 ioThinx_Misc_GetPushButtonState(misc_slot, &pbtn_state);
137 if (rc != IOTHINX_ERR_OK)
138 {
139 printf("ioThinx_Misc_GetPushButtonState() = %d\n", rc);
140 }
141 else
142 {
143 printf("Push button state = %d\n", pbtn_state);
144 }
145
146 // user led
147 ioThinx_Misc_SetUserLedState(misc_slot, LED_CHANNEL_U1, LED_STATE_GREEN);
148 if (rc != IOTHINX_ERR_OK)
149 {
150 printf("ioThinx_Misc_SetUserLedState() = %d\n", rc);
151 }
152 else
153 {
154 printf("Set LED U1 to GREEN\n");
155 }
156 ioThinx_Misc_SetUserLedState(misc_slot, LED_CHANNEL_U2, LED_STATE_RED);
157 if (rc != IOTHINX_ERR_OK)
158 {
159 printf("ioThinx_Misc_SetUserLedState() = %d\n", rc);
160 }
161 else
162 {
163 printf("Set LED U2 to RED\n");
164 }
165 printf("Press enter to clear.");
166 ch = getchar();
167 ioThinx_Misc_SetUserLedState(misc_slot, LED_CHANNEL_U1, LED_STATE_DARK);
168 ioThinx_Misc_SetUserLedState(misc_slot, LED_CHANNEL_U2, LED_STATE_DARK);
169 printf("Press enter to continue.");
170 ch = getchar();
171
172 return 0;
173 }
ioThinx_Misc_GetRotarySwitchState
IOTHINX_ERR ioThinx_Misc_GetRotarySwitchState(uint32_t slot, uint8_t *p_state)
LED_CHANNEL_U2
#define LED_CHANNEL_U2
Definition: iothinxio.h:1075
MODULE_INFO::model_name
uint8_t model_name[20]
Definition: iothinxio.h:1054
LED_CHANNEL_U1
#define LED_CHANNEL_U1
Definition: iothinxio.h:1074
ioThinx_Misc_GetPushButtonState
IOTHINX_ERR ioThinx_Misc_GetPushButtonState(uint32_t slot, uint8_t *p_state)
LED_STATE_RED
#define LED_STATE_RED
Definition: iothinxio.h:1068
IOTHINX_ERR_OK
#define IOTHINX_ERR_OK
Definition: iothinxio.h:35
LED_STATE_GREEN
#define LED_STATE_GREEN
Definition: iothinxio.h:1067
LED_STATE_DARK
#define LED_STATE_DARK
Definition: iothinxio.h:1066
MODULE_INFO::serial_number
uint8_t serial_number[13]
Definition: iothinxio.h:1058
ioThinx_Misc_GetModuleCount
IOTHINX_ERR ioThinx_Misc_GetModuleCount(uint32_t *p_module_count)
MODULE_INFO
Definition: iothinxio.h:1052
ioThinx_Misc_SetUserLedState
IOTHINX_ERR ioThinx_Misc_SetUserLedState(uint32_t slot, uint8_t channel, uint8_t state)
ioThinx_Misc_GetModuleInfo
IOTHINX_ERR ioThinx_Misc_GetModuleInfo(uint8_t slot, struct MODULE_INFO *p_module_info)
main
int main(int argc, char **const argv)
Definition: misc.c:48
ioThinx_IO_Client_Init
IOTHINX_ERR ioThinx_IO_Client_Init(void)
ioThinx_Misc_SetLocateState
IOTHINX_ERR ioThinx_Misc_SetLocateState(uint32_t slot, uint8_t state)