- Main
- Tutorials
- Library
- Samples

- File List
- Globals

Functions
misc.c File Reference

Miscellaneous Sample More...

`#include <stdio.h>`
`#include <stdlib.h>`
`#include <unistd.h>`
`#include <iothinx/iothinxio.h>`

Go to the source code of this file.

## Functions
int | main (int argc, char **const argv)

## Detailed Description

Miscellaneous Sample

Copyright
  2019 Moxa Inc. All rights reserved.

Date
  2019-01-22

Author
  Wanhan Hsieh

Version
  V1.0

    Introduction:
    This is misc sample code.
    Example:
    1. Using default: ./misc
    2. Setting slot: ./misc -s5
    Default:
    Slot of module = 1
    Help:

    moxa@Moxa:~$ sudo ./misc -h
    Miscellaneous sample program.

    Usage: ./misc [OPTIONS]

    Options:
            -s       Slot of the module. Default slot = 1

Library:
Misc APIs

Definition in file misc.c.

## Function Documentation

int main | ( | int | argc,
|  | char **const | argv
| ) |  |

/*******************************************************************************
* Copyright (C) 2019 Moxa Inc. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*
* Miscellaneous Sample Application
*
* Date Author Comment
* 2019-01-22 Wanhan Hsieh Created it.
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <iothinx/iothinxio.h>
int main(int argc, char **const argv)
{
int32_t rc;
uint32_t misc_slot = 0;
uint32_t module_slot = 1;
uint32_t module_count = 0;
struct MODULE_INFO module_info;
uint8_t rs_state = 0;
uint8_t pbtn_state = 0;
uint8_t led_state = 0;
char ch;
while (-1 != (rc = getopt(argc, argv, "hs:")))
{
switch (rc)
{
case 's':
module_slot = atoi(optarg);
break;
case 'h':
default:
printf("Miscellaneous sample program.\n\n");
printf("Usage: ./misc [OPTIONS]\n\n");
printf("Options:\n");
printf("\t%-8s Slot of the module. Default slot = %d\n", "-s", module_slot);
printf("\n");
return 0;
}
}
// initialize ioThinx I/O
rc = ioThinx_IO_Client_Init();
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_IO_Client_Init() = %d\n", rc);
return -1;
}
// module infomation
rc = ioThinx_Misc_GetModuleCount(&module_count);
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_Misc_GetModuleCount() = %d\n", rc);
}
printf("Module count = %lu\n", module_count);
printf("Module slot = %lu\n", module_slot);
rc = ioThinx_Misc_GetModuleInfo(module_slot, &module_info);
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_Misc_GetModuleInfo() = %d\n", rc);
}
else
{
printf("Slot %d Module Information:\n", module_slot);
printf("Model Name: %s, ", module_info.model_name);
printf("Serial Number: %s\n", module_info.serial_number);
}
// locating
rc = ioThinx_Misc_SetLocateState(module_slot, 1);
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_Misc_SetLocateState() = %d\n", rc);
}
else
{
printf("Slot %d: Locating...\n", module_slot);
}
printf("Press enter to stop locate.");
ch = getchar();
rc = ioThinx_Misc_SetLocateState(module_slot, 0);
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_Misc_SetLocateState() = %d\n", rc);
}
// rotary switch
ioThinx_Misc_GetRotarySwitchState(misc_slot, &rs_state);
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_Misc_GetRotarySwitchState() = %d\n", rc);
}
else
{
printf("Rotary switch state = %d\n", rs_state);
}
// push button
ioThinx_Misc_GetPushButtonState(misc_slot, &pbtn_state);
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_Misc_GetPushButtonState() = %d\n", rc);
}
else
{
printf("Push button state = %d\n", pbtn_state);
}
// user led
ioThinx_Misc_SetUserLedState(misc_slot, LED_CHANNEL_U1, LED_STATE_GREEN);
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_Misc_SetUserLedState() = %d\n", rc);
}
else
{
printf("Set LED U1 to GREEN\n");
}
ioThinx_Misc_SetUserLedState(misc_slot, LED_CHANNEL_U2, LED_STATE_RED);
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_Misc_SetUserLedState() = %d\n", rc);
}
else
{
printf("Set LED U2 to RED\n");
}
printf("Press enter to clear.");
ch = getchar();
ioThinx_Misc_SetUserLedState(misc_slot, LED_CHANNEL_U1, LED_STATE_DARK);
ioThinx_Misc_SetUserLedState(misc_slot, LED_CHANNEL_U2, LED_STATE_DARK);
printf("Press enter to continue.");
ch = getchar();
return 0;
}

Definition at line 48 of file misc.c.

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:1174
LED_CHANNEL_U1
#define LED_CHANNEL_U1
Definition: iothinxio.h:1173
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:1167
IOTHINX_ERR_OK
#define IOTHINX_ERR_OK
Definition: iothinxio.h:35
LED_STATE_GREEN
#define LED_STATE_GREEN
Definition: iothinxio.h:1166
LED_STATE_DARK
#define LED_STATE_DARK
Definition: iothinxio.h:1165
ioThinx_Misc_GetModuleCount
IOTHINX_ERR ioThinx_Misc_GetModuleCount(uint32_t *p_module_count)
MODULE_INFO
Definition: iothinxio.h:1151
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)
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)