FreeHIL
Loading...
Searching...
No Matches
FH_AI.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_AI</b> (Analog Input)\n
6 * Functions in this file, handle RFCommunication (<b>RobotFramework</b> <b>C</b>ommunication) for all commands related to <b>AI</b> (Analog 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_AI_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>AI</b> (Analog 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 uint32_t FH_AI_Data; // It temporarily stores the status of a analog input
67 uint32_t* FH_AI_Data_Ptr = &FH_AI_Data; // Pointer with &FH_AI_Data address
68
69 FH_Uint8Uint32 fh_Uint8Uint32; // To convert 32 bits data (uint32_t) to four 8 bits data (uint8_t) in FH_PWMI_Frequency
70
71 switch (fh_RFCommunication_Message -> Command)
72 {
73 case FH_AI_Commands_Init: // If the command is FH_AI_Commands_Init
74 fh_ErrorInfo = FH_AI_ProcessCommand_Init(fh_RFCommunication_Message); // Handle FH_AI_Commands_Init command
75 sprintf(FH_RFCommunication_SendBuf,"%02x|AI|Init|%02x|\n",FH_RFCommunication_DeviceAddress,fh_ErrorInfo.error_code); // set FH_RFCommunication_SendBuf buffer
76 FH_RFCommunication_Send(FH_RFCommunication_SendBuf); // Send the result back to the Robot Framework
77 break;
78 case FH_AI_Commands_GetStatus: // If the command is FH_AI_Commands_GetStatus
79 fh_ErrorInfo = FH_AI_ProcessCommand_GetStatus(fh_RFCommunication_Message, FH_AI_Data_Ptr); // Handle FH_AI_Commands_GetStatus command
80
81 sprintf(FH_RFCommunication_SendBuf,"%02x|AI|GetStatus|%02x|",FH_RFCommunication_DeviceAddress,fh_ErrorInfo.error_code); // set FH_RFCommunication_SendBuf buffer
82 FH_RFCommunication_Send(FH_RFCommunication_SendBuf); // Send the result back to the Robot Framework
83
84 fh_Uint8Uint32.value = FH_AI_Data; // set the 32 bit value
85 for (int i = 3 ; i >= 0; i--)
86 {
87 sprintf(FH_RFCommunication_SendBuf,"%02x",fh_Uint8Uint32.bytes[i]); // Format the data and set FH_RFCommunication_SendBuf buffer
88 FH_RFCommunication_Send(FH_RFCommunication_SendBuf); // Send the result back to the Robot Framework
89 }
90
91 sprintf(FH_RFCommunication_SendBuf,"|\n"); // set FH_RFCommunication_SendBuf buffer
92 FH_RFCommunication_Send(FH_RFCommunication_SendBuf); // Send the result back to the Robot Framework
93 break;
94 default:
95 break;
96 }
97 return fh_ErrorInfo;
98}
99
100/**
101 * @brief This function handles <b>FH_AI_Commands_Init</b> command\n
102 * There are 3 main items handled in this function as following:\n
103 * 1 - Verifying the health of the received message\n
104 * 2 - Setting the addresses of the functions in <b>FH_Functions</b>, Section <b>FH_AI</b> (Analog Input) to an array of function pointer\n
105 * 3 - Calling the related function in <b>FH_Functions</b>, Section <b>FH_AI</b> (Analog Input) according to the first parameter of the Data in the message frame
106 *
107 * @param fh_RFCommunication_Message
108 * The message frame set by <b>RobotFramework</b>
109 *
110 * @verbatim
111 ==============================================================================
112 ##### RobotFramework Example #####
113 ==============================================================================
114 @{MessageData} = Create List ${AI01}
115 &{Message} = Create Dictionary DeviceAddress=${DeviceAddress_01} Function=${Function_AI} Command=${AI_Commands_Init} Data=@{MessageData}
116 ${Result} SendMessage &{Message}
117
118 Comments:
119
120 AI01 => It indicates the Analog Input 01
121 DeviceAddress_01 => It shall be equal to the address of the device: FH_RFCommunication_DeviceAddress
122 Function_AI => It is the function in the message frame
123 AI_Commands_Init => It is the command of the function in the message frame
124 @endverbatim
125 *
126 * @return FH_ErrorInfo is returned
127 *
128
129*/
131{
132 FH_ErrorInfo fh_ErrorInfo; // Error Information
133 FH_ResetErrorInfo(&fh_ErrorInfo); // Reset Error Information to default
134
135 // Item 1 - Verifying the health of the received message
137 if (fh_ErrorInfo.error_code != FH_ERROR_OK) // Check if the message is healthy or not
138 {
139 return fh_ErrorInfo; // Return the error
140 }
141
142 // 2 - Setting the addresses of the functions in <b>FH_Functions</b>, Section <b>FH_AI</b> (Analog Input) to an array of function pointer
143 FH_AI_Init_FuncPtrModel FH_AI_Init_FuncPtr[FH_AI_MaxPeripheralNumber]; // An array of function pointer FH_AI_Init_FuncPtrModel
144 FH_AI_Init_S (FH_AI_Init_FuncPtr); // Setting the address of the functions
145
146 // 3 - Calling the related function in <b>FH_Functions</b>, Section <b>FH_AI</b> (Analog Input) according to the first parameter of the Data in the message frame
147 FH_ResetErrorInfo(&fh_ErrorInfo); // Reset Error Information to default
148 fh_ErrorInfo = FH_AI_Init_C (fh_RFCommunication_Message, FH_AI_Init_FuncPtr); // Calling the related function according to the first parameter of the Data in the message frame
149 return fh_ErrorInfo;
150}
151
152/**
153 * @brief This function handles <b>FH_AI_Commands_GetStatus</b> command\n
154 * There are 3 main items handled in this function as following:\n
155 * 1 - Verifying the health of the received message\n
156 * 2 - Setting the addresses of the functions in <b>FH_Functions</b>, Section <b>FH_AI</b> (Analog Input) to an array of function pointer\n
157 * 3 - Calling the related function in <b>FH_Functions</b>, Section <b>FH_AI</b> (Analog Input) according to the first parameter of the Data in the message frame
158 *
159 * @param fh_RFCommunication_Message
160 * The message frame set by <b>RobotFramework</b>
161 *
162 * @verbatim
163 ==============================================================================
164 ##### RobotFramework Example #####
165 ==============================================================================
166 @{MessageData} = Create List ${AI01}
167 &{Message} = Create Dictionary DeviceAddress=${DeviceAddress_01} Function=${Function_AI} Command=${AI_Commands_GetStatus} Data=@{MessageData}
168 ${Result} SendMessage &{Message}
169
170 Comments:
171
172 AI01 => It indicates the Analog Input 01
173 DeviceAddress_01 => It shall be equal to the address of the device: FH_RFCommunication_DeviceAddress
174 Function_AI => It is the function in the message frame
175 AI_Commands_GetStatus => It is the command of the function in the message frame
176 @endverbatim
177 *
178 *
179 * @return FH_ErrorInfo is returned
180 *
181
182*/
184{
185 FH_ErrorInfo fh_ErrorInfo; // Error Information
186 FH_ResetErrorInfo(&fh_ErrorInfo); // Reset Error Information to default
187
188 // Item 1 - Verifying the health of the received message
189 fh_ErrorInfo = FH_AI_GetStatus_H(fh_RFCommunication_Message);
190 if (fh_ErrorInfo.error_code != FH_ERROR_OK) // Check if the message is healthy or not
191 {
192 return fh_ErrorInfo; // Return the error
193 }
194
195 // 2 - Setting the addresses of the functions in <b>FH_Functions</b>, Section <b>FH_AI</b> (Analog Input) to an array of function pointer
196 FH_AI_GetStatus_FuncPtrModel FH_AI_GetStatus_FuncPtr[FH_AI_MaxPeripheralNumber]; // An array of function pointer FH_AI_GetStatus_FuncPtrModel
197 FH_AI_GetStatus_S (FH_AI_GetStatus_FuncPtr); // Setting the address of the functions
198
199 // 3 - Calling the related function in <b>FH_Functions</b>, Section <b>FH_AI</b> (Analog Input) according to the first parameter of the Data in the message frame
200 FH_ResetErrorInfo(&fh_ErrorInfo); // Reset Error Information to default
201 fh_ErrorInfo = FH_AI_GetStatus_C (fh_RFCommunication_Message, FH_AI_Data, FH_AI_GetStatus_FuncPtr); // Calling the related function according to the first parameter of the Data in the message frame
202 return fh_ErrorInfo;
203}
204
205#endif