FreeHIL
Loading...
Searching...
No Matches
FH_DI.c
Go to the documentation of this file.
1/**
2 * @file
3 * @brief
4 * <b>File Map:</b>\n
5 * <b>FH_Root</b>, Section <b>FH_Perif</b>, <b>FH_DI</b> (Digital Input)\n
6 * Functions in this file, handle RFCommunication (<b>RobotFramework</b> <b>C</b>ommunication) for all commands related to <b>DI</b> (Digital Input)
7 *
8 * @attention <b>FH</b> user could left this file intact
9 *
10 *******************************************************************************
11 * SPDX-License-Identifier: Apache-2.0
12 *
13 * Copyright 2026 Vahid Hasirchi
14 *
15 * Licensed under the Apache License, Version 2.0 (the "License");
16 * you may not use this file except in compliance with the License.
17 * You may obtain a copy of the License at
18 *
19 * http://www.apache.org/licenses/LICENSE-2.0
20 *
21 * Unless required by applicable law or agreed to in writing, software
22 * distributed under the License is distributed on an "AS IS" BASIS,
23 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24 * See the License for the specific language governing permissions and
25 * limitations under the License.
26 ********************************************************************************
27
28 *
29 * <b>For more information refer to FreeHIL.com</b>
30 *
31*/
32/* Primary Includes ------------------------------------------------------------------*/
34
35#include "stdio.h"
36
37#if FH_DI_MaxPeripheralNumber > 0 // It is configurable in FH_DevicePeripherals.h
38
39/* Secondary Includes ------------------------------------------------------------------*/
49
50/**
51 * @brief This function is the basic function of handling RFCommunication (<b>RobotFramework</b> <b>C</b>ommunication) for all the <b>Commands</b> related to the <b>Function</b> <b>DI</b> (Digital Input)\n
52 * This function is called from <b>FH_RFCommunication()</b>
53 *
54 * @param fh_RFCommunication_Message
55 * The message frame set by <b>RobotFramework</b>
56 *
57 * @return FH_ErrorInfo is returned
58 *
59
60*/
62{
63 FH_ErrorInfo fh_ErrorInfo; // Error Information
64 FH_ResetErrorInfo(&fh_ErrorInfo); // Reset Error Information to default
65
66 uint8_t FH_DI_Data; // It temporarily stores the status of a digital input
67 uint8_t* FH_DI_Data_Ptr; // Pointer with &FH_DI_Data address
68 uint8_t FH_DI_MULTIData [8]; // It temporarily stores the status of a 64 digital inputs
69
70 switch (fh_RFCommunication_Message -> Command) // Check different commands
71 {
72 case FH_DI_Commands_Init: // If the command is FH_DI_Commands_Init
73 fh_ErrorInfo = FH_DI_ProcessCommand_Init(fh_RFCommunication_Message); // Handle FH_DI_Commands_Init command
74 sprintf(FH_RFCommunication_SendBuf,"%02x|DI|Init|%02x|\n",FH_RFCommunication_DeviceAddress,fh_ErrorInfo.error_code); // set FH_RFCommunication_SendBuf buffer
75 FH_RFCommunication_Send(FH_RFCommunication_SendBuf); // Send the result back to the Robot Framework
76 break;
77 case FH_DI_Commands_GetStatus: // If the command is FH_DI_Commands_SetStatus
78 FH_DI_Data_Ptr = &FH_DI_Data; // Pointer with &FH_DI_Data address
79 fh_ErrorInfo = FH_DI_ProcessCommand_GetStatus(fh_RFCommunication_Message, FH_DI_Data_Ptr); // Handle FH_DI_Commands_GetStatus command
80 sprintf(FH_RFCommunication_SendBuf,"%02x|DI|GetStatus|%02x|%d|\n",FH_RFCommunication_DeviceAddress,fh_ErrorInfo.error_code,FH_DI_Data); // set FH_RFCommunication_SendBuf buffer
81 FH_RFCommunication_Send(FH_RFCommunication_SendBuf); // Send the result back to the Robot Framework
82 break;
83 case FH_DI_Commands_InitMULTI: // If the command is FH_DI_Commands_InitMULTI
84 fh_ErrorInfo = FH_DI_ProcessCommand_InitMULTI(fh_RFCommunication_Message); // Handle FH_DI_Commands_InitMULTI command
85 sprintf(FH_RFCommunication_SendBuf,"%02x|DI|InitMULTI|%02x|\n",FH_RFCommunication_DeviceAddress,fh_ErrorInfo.error_code); // set FH_RFCommunication_SendBuf buffer
86 FH_RFCommunication_Send(FH_RFCommunication_SendBuf); // Send the result back to the Robot Framework
87 break;
88 case FH_DI_Commands_GetStatusMULTI: // If the command is FH_DI_Commands_GetStatusMULTI
89 FH_DI_Data_Ptr = &FH_DI_MULTIData[0];
90 fh_ErrorInfo = FH_DI_ProcessCommand_GetStatusMULTI(fh_RFCommunication_Message, FH_DI_Data_Ptr); // Handle FH_DI_Commands_GetStatusMULTI command
91
92 sprintf(FH_RFCommunication_SendBuf,"%02x|DI|GetStatusMULTI|%02x|",FH_RFCommunication_DeviceAddress,fh_ErrorInfo.error_code); // set FH_RFCommunication_SendBuf buffer
93 FH_RFCommunication_Send(FH_RFCommunication_SendBuf); // Send the result back to the Robot Framework
94
95 for (int i = 0 ; i < 8; i++)
96 {
97 sprintf(FH_RFCommunication_SendBuf,"%02x",FH_DI_MULTIData[i]); // Format the data and set FH_RFCommunication_SendBuf buffer
98 FH_RFCommunication_Send(FH_RFCommunication_SendBuf); // Send the result back to the Robot Framework
99 }
100
101 sprintf(FH_RFCommunication_SendBuf,"|\n"); // set FH_RFCommunication_SendBuf buffer
102 FH_RFCommunication_Send(FH_RFCommunication_SendBuf); // Send the result back to the Robot Framework
103 break;
104 default:
105 break;
106 }
107 return fh_ErrorInfo;
108}
109
110/**
111 * @brief This function handles <b>FH_DI_Commands_Init</b> command\n
112 * There are 3 main items handled in this function as following:\n
113 * 1 - Verifying the health of the received message\n
114 * 2 - Setting the addresses of the functions in <b>FH_Functions</b>, Section <b>FH_DI</b> (Digital Input) to an array of function pointer\n
115 * 3 - Calling the related function in <b>FH_Functions</b>, Section <b>FH_DI</b> (Digital Input) according to the first parameter of the Data in the message frame
116 *
117 * @param fh_RFCommunication_Message
118 * The message frame set by <b>RobotFramework</b>
119 *
120 * @verbatim
121 ==============================================================================
122 ##### RobotFramework Example #####
123 ==============================================================================
124 @{MessageData} = Create List ${DI01}
125 &{Message} = Create Dictionary DeviceAddress=${DeviceAddress_01} Function=${Function_DI} Command=${DI_Commands_Init} Data=@{MessageData}
126 ${Result} SendMessage &{Message}
127
128 Comments:
129
130 DI01 => It indicates the Digital Input 01
131 DeviceAddress_01 => It shall be equal to the address of the device: FH_RFCommunication_DeviceAddress
132 Function_DI => It is the function in the message frame
133 DI_Commands_Init => It is the command of the function in the message frame
134 @endverbatim
135 *
136 * @return FH_ErrorInfo is returned
137 *
138
139*/
141{
142 FH_ErrorInfo fh_ErrorInfo; // Error Information
143 FH_ResetErrorInfo(&fh_ErrorInfo); // Reset Error Information to default
144
145 // Item 1 - Verifying the health of the received message
147 if (fh_ErrorInfo.error_code != FH_ERROR_OK) // Check if the message is healthy or not
148 {
149 return fh_ErrorInfo; // Return the error
150 }
151
152 // 2 - Setting the addresses of the functions in <b>FH_Functions</b>, Section <b>FH_DI</b> (Digital Input) to an array of function pointer\n
153 FH_DI_Init_FuncPtrModel FH_DI_Init_FuncPtr[FH_DI_MaxPeripheralNumber]; // An array of function pointer FH_DI_Init_FuncPtrModel
154 FH_DI_Init_S (FH_DI_Init_FuncPtr); // Setting the address of the functions
155
156 // 3 - Calling the related function in <b>FH_Functions</b>, Section <b>FH_DI</b> (Digital Input) according to the first parameter of the Data in the message frame
157 FH_ResetErrorInfo(&fh_ErrorInfo); // Reset Error Information to default
158 fh_ErrorInfo = FH_DI_Init_C (fh_RFCommunication_Message, FH_DI_Init_FuncPtr); // Calling the related function according to the first parameter of the Data in the message frame
159 return fh_ErrorInfo;
160}
161
162/**
163 * @brief This function handles <b>FH_DI_Commands_GetStatus</b> command\n
164 * There are 3 main items handled in this function as following:\n
165 * 1 - Verifying the health of the received message\n
166 * 2 - Setting the addresses of the functions in <b>FH_Functions</b>, Section <b>FH_DI</b> (Digital Input) to an array of function pointer\n
167 * 3 - Calling the related function in <b>FH_Functions</b>, Section <b>FH_DI</b> (Digital Input) according to the first parameter of the Data in the message frame
168 *
169 * @param fh_RFCommunication_Message
170 * The message frame set by <b>RobotFramework</b>
171 *
172 * @verbatim
173 ==============================================================================
174 ##### RobotFramework Example #####
175 ==============================================================================
176 @{MessageData} = Create List ${DI01}
177 &{Message} = Create Dictionary DeviceAddress=${DeviceAddress_01} Function=${Function_DI} Command=${DI_Commands_GetStatus} Data=@{MessageData}
178 ${Result} SendMessage &{Message}
179
180 Comments:
181
182 DI01 => It indicates the Digital Input 01
183 DeviceAddress_01 => It shall be equal to the address of the device: FH_RFCommunication_DeviceAddress
184 Function_DI => It is the function in the message frame
185 DI_Commands_GetStatus => It is the command of the function in the message frame
186 @endverbatim
187 *
188 *
189 * @return FH_ErrorInfo is returned
190 *
191
192*/
194{
195 FH_ErrorInfo fh_ErrorInfo; // Error Information
196 FH_ResetErrorInfo(&fh_ErrorInfo); // Reset Error Information to default
197
198 // 1 - Verifying the health of the received message
199 fh_ErrorInfo = FH_DI_GetStatus_H(fh_RFCommunication_Message);
200 if (fh_ErrorInfo.error_code != FH_ERROR_OK) // Check if the message is healthy or not
201 {
202 return fh_ErrorInfo; // Return the error
203 }
204
205 // 2 - Setting the addresses of the functions in <b>FH_Functions</b>, Section <b>FH_DI</b> (Digital Input) to an array of function pointer
206 FH_DI_GetStatus_FuncPtrModel FH_DI_GetStatus_FuncPtr[FH_DI_MaxPeripheralNumber]; // An array of function pointer FH_DI_GetStatus_FuncPtrModel
207 FH_DI_GetStatus_S (FH_DI_GetStatus_FuncPtr); // Setting the address of the functions
208
209 // 3 - Calling the related function in <b>FH_Functions</b>, Section <b>FH_DI</b> (Digital Input) according to the first parameter of the Data in the message frame
210 FH_ResetErrorInfo(&fh_ErrorInfo); // Reset Error Information to default
211 fh_ErrorInfo = FH_DI_GetStatus_C (fh_RFCommunication_Message, FH_DI_Data, FH_DI_GetStatus_FuncPtr); // Calling the related function according to the first parameter of the Data in the message frame
212 return fh_ErrorInfo;
213}
214
215/**
216 * @brief This function handles <b>FH_DI_ProcessCommand_InitMULTI</b> command\n
217 * With this function, mass initialization of up to 64 <b>DI</b> (Digital Input) is possible instead of just 1 <b>DI</b> (Digital Input)\n
218 * There are 3 main items handled in this function as following:\n
219 * 1 - Verifying the health of the received message\n
220 * 2 - Setting the addresses of the functions in <b>FH_Functions</b>, Section <b>FH_DI</b> (Digital Input) to an array of function pointer\n
221 * 3 - Calling the related function(s) in <b>FH_Functions</b>, Section <b>FH_DI</b> (Digital Input) according to the 8 first parameters of the Data in the message frame\n
222 * (The first 8 parameters resemble 64 bits and each bit resembles 1 <b>DI</b> (Digital Input) and if the bit is 1, the related <b>DI</b> (Digital Input) function will be called)
223 *
224 * @param fh_RFCommunication_Message
225 * The message frame set by <b>RobotFramework</b>
226 *
227 * @verbatim
228 ==============================================================================
229 ##### RobotFramework Example #####
230 ==============================================================================
231 @{MessageData} = Create List FF FF FF FF FF FF FF FF
232 &{Message} = Create Dictionary DeviceAddress=${DeviceAddress_01} Function=${Function_DI} Command=${DI_Commands_InitMULTI} Data=@{MessageData}
233 ${Result} SendMessage &{Message}
234
235 Comments:
236
237 FF FF FF FF FF FF FF FF => Example data as the first 8 parameters
238 DeviceAddress_01 => It shall be equal to the address of the device: FH_RFCommunication_DeviceAddress
239 Function_DI => It is the function in the message frame
240 DI_Commands_InitMULTI => It is the command of the function in the message frame
241 @endverbatim
242 *
243 * @return FH_ErrorInfo is returned
244 *
245
246*/
248{
249 FH_ErrorInfo fh_ErrorInfo; // Error Information
250 FH_ResetErrorInfo(&fh_ErrorInfo); // Reset Error Information to default
251
252 // 1 - Verifying the health of the received message
254 if (fh_ErrorInfo.error_code != FH_ERROR_OK) // Check if the message is healthy or not
255 {
256 return fh_ErrorInfo; // Return the error
257 }
258
259 // 2 - Setting the addresses of the functions in <b>FH_Functions</b>, Section <b>FH_DI</b> (Digital Input) to an array of function pointer
260 FH_DI_Init_FuncPtrModel FH_DI_Init_FuncPtr[FH_DI_MaxPeripheralNumber]; // An array of function pointer FH_DI_Init_FuncPtrModel
261 FH_DI_Init_S (FH_DI_Init_FuncPtr); // Setting the address of the functions
262
263 // 3 - Calling the related function(s) in <b>FH_Functions</b>, Section <b>FH_DI</b> (Digital Input) according to the 8 first parameters of the Data in the message frame
264 // (The first 8 parameters resemble 64 bits and each bit resembles 1 <b>DI</b> (Digital Input) and if the bit is 1, the related <b>DI</b> (Digital Input) function will be called)
265 FH_ResetErrorInfo(&fh_ErrorInfo); // Reset Error Information to default
266 fh_ErrorInfo = FH_DI_InitMULTI_C (fh_RFCommunication_Message, FH_DI_Init_FuncPtr); // Calling the related function according to the first 8 parameters of the Data in the message frame
267 return fh_ErrorInfo;
268}
269
270/**
271 * @brief This function handles <b>FH_DI_ProcessCommand_GetStatusMULTI</b> command\n
272 * With this function, mass status get of up to 64 <b>DI</b> (Digital Input) is possible instead of just 1 <b>DI</b> (Digital Input)\n
273 * There are 3 main items handled in this function as following:\n
274 * 1 - Verifying the health of the received message\n
275 * 2 - Setting the addresses of the functions in <b>FH_Functions</b>, Section <b>FH_DI</b> (Digital Input) to an array of function pointer\n
276 * 3 - Calling the related function in <b>FH_Functions</b>, Section <b>FH_DI</b> (Digital Input) according to the 8 first parameters of the Data in the message frame\n
277 * (The first 8 parameters as mask resemble 64 bits and each bit resembles 1 <b>DI</b> (Digital Input) and if the bit is 1, the related <b>DI</b> (Digital Input) function will be called)
278 *
279 * @param fh_RFCommunication_Message
280 * The message frame set by <b>RobotFramework</b>
281 * @param FH_DI_MULTIData
282 * 64 bits (8 bytes) resemble 64 bits result and each bit resembles the status of 1 <b>DI</b> (Digital Input) as 1 or 0 of the corresponding bit in the first 8 bytes of the Data
283 *
284 * @verbatim
285 ==============================================================================
286 ##### RobotFramework Example #####
287 ==============================================================================
288 @{MessageData} = Create List FF FF FF FF FF FF FF FF
289 &{Message} = Create Dictionary DeviceAddress=${DeviceAddress_01} Function=${Function_DI} Command=${DI_Commands_GetStatusMULTI} Data=@{MessageData}
290 ${Result} SendMessage &{Message}
291
292 Comments:
293
294 FF FF FF FF FF FF FF FF => Example data as the first 8 parameters
295 DeviceAddress_01 => It shall be equal to the address of the device: FH_RFCommunication_DeviceAddress
296 Function_DI => It is the function in the message frame
297 DI_Commands_GetStatusMULTI => It is the command of the function in the message frame
298 @endverbatim
299 *
300 * @return FH_ErrorInfo is returned
301 *
302
303*/
305{
306 FH_ErrorInfo fh_ErrorInfo; // Error Information
307 FH_ResetErrorInfo(&fh_ErrorInfo); // Reset Error Information to default
308
309 // 1 - Verifying the health of the received message
310 fh_ErrorInfo = FH_DI_GetStatusMULTI_H(fh_RFCommunication_Message);
311 if (fh_ErrorInfo.error_code != FH_ERROR_OK) // Check if the message is healthy or not
312 {
313 return fh_ErrorInfo; // Return the error
314 }
315
316 // 2 - Setting the addresses of the functions in <b>FH_Functions</b>, Section <b>FH_DI</b> (Digital Input) to an array of function pointer
317 FH_DI_GetStatus_FuncPtrModel FH_DI_GetStatus_FuncPtr[FH_DI_MaxPeripheralNumber]; // An array of function pointer FH_DI_GetStatus_FuncPtrModel
318 FH_DI_GetStatus_S (FH_DI_GetStatus_FuncPtr); // Setting the address of the functions
319
320 // 3 - Calling the related function in <b>FH_Functions</b>, Section <b>FH_DI</b> (Digital Input) according to the 8 first parameters of the Data in the message frame
321 // (The first 8 parameters as mask resemble 64 bits and each bit resembles 1 <b>DI</b> (Digital Input) and if the bit is 1, the related <b>DI</b> (Digital Input) function will be called)
322 FH_ResetErrorInfo(&fh_ErrorInfo); // Reset Error Information to default
323 fh_ErrorInfo = FH_DI_GetStatusMULTI_C (fh_RFCommunication_Message, FH_DI_MULTIData, FH_DI_GetStatus_FuncPtr); // Calling the related function according to the first 8 parameters of the Data in the message frame
324 return fh_ErrorInfo;
325}
326
327#endif