Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/Microchip/PIC32MX320F128H/PmodTMP2/Command.c

raw symbols SCIP
1 /***************************************************************************//**
2 * @file Command.c
3 * @brief Implementation of the commands given by user through UART for PmodTMP2.
4 * @author Mihai Bancisor ([email protected])
5 ********************************************************************************
6 * Copyright 2013(c) Analog Devices, Inc.
7 *
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
12 * - Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 * - Neither the name of Analog Devices, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 * - The use of this software may or may not infringe the patent rights
22 * of one or more patent holders. This license does not release you
23 * from the requirement that you obtain separate licenses from these
24 * patent holders to use this software.
25 * - Use of the software either in source or binary form, must be run
26 * on or directly connected to an Analog Devices Inc. component.
27 *
28 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
30 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
34 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
35 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
36 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 *
39 *******************************************************************************/
40
41 /******************************************************************************/
42 /***************************** Include Files **********************************/
43 /******************************************************************************/
44 #include "Command.h"
45 #include "ADT7420.h"
46 #include "Console.h"
47
48 /******************************************************************************/
49 /************************ Constants Definitions *******************************/
50 /******************************************************************************/
51
52 /*!< List of available commands */
53 const char* cmdList[] = {
54 "help?",
55 "reset!",
56 "mode=",
57 "mode?",
58 "resolution=",
59 "resolution?",
60 "temperature?"};
61
62 const char* cmdDescription[] = {
63 " - Displays all available commands.",
64 " - Resets the ADT7420.",
65 " - Sets the operational mode for ADT7420. Accepted values: \r\n\
66 \t0 - Continuos conversions. \r\n\
67 \t1 - One shot. \r\n\
68 \t2 - 1 SPS mode. \r\n\
69 \t3 - Shutdown.",
70 " - Displays the selected operational mode.",
71 " - Sets the resolution for ADT7420. Accepted values: \r\n\
72 \t0 - 13 bits resolution. \r\n\
73 \t1 - 16 bits resolution.",
74 " - Displays the selected resolution.",
75 " - Displays the temperature."};
76
77 const char* cmdExample[] = {"",
78 "",
79 "To set the mode to 1 SPS, type: mode=2.",
80 "",
81 "To set the resolution to 16 bits, type: resolution=1.",
82 "",
83 ""};
84
85 const char cmdNo = (sizeof(cmdList) / sizeof(const char*));
86
87 /******************************************************************************/
88 /************************ Variables Definitions *******************************/
89 /******************************************************************************/
90 cmdFunction cmdFunctions[7] = {GetHelp, DoReset, SetMode, GetMode,\
91 SetResolution, GetResolution, GetTemperature};
92 /*!< Variables holding information about the device */
93 unsigned char currentMode = 0;
94 extern unsigned char resolutionSetting;
95 double temperature = 0;
96
97 /***************************************************************************//**
98 * @brief Displays all available commands.
99 *
100 * @return None.
101 *******************************************************************************/
102 void GetHelp(double* param, char paramNo) /*!< "help?" command */
103 {
104 char displayCmd;
105
106 CONSOLE_Print("Available commands:\r\n");
107 for(displayCmd = 0; displayCmd < cmdNo; displayCmd++)
108 {
109 CONSOLE_Print("%s%s\r\n", (char*)cmdList[displayCmd],
110 (char*)cmdDescription[displayCmd]);
111 }
112 }
113
114 /***************************************************************************//**
115 * @brief Initializes the device.
116 *
117 * @return - The result of the initialization.
118 * Example: ERROR - the device was not initialized or the device
119 * is not present.
120 * SUCCES - the device was initialized and the device
121 * is present.
122 *******************************************************************************/
123 char DoDeviceInit(void)
124 {
125 if(ADT7420_Init() == 0)
126 {
127 CONSOLE_Print("ADT7420 OK\r\n");
128 GetHelp(NULL, 0);
129
130 return SUCCESS;
131 }
132 else
133 {
134 CONSOLE_Print("ADT7420 Error\r\n");
135
136 return ERROR;
137 }
138 }
139
140 /***************************************************************************//**
141 * @brief Resets the ADT7420.
142 *
143 * @return None.
144 *******************************************************************************/
145 void DoReset(double* param, char paramNo) /*!< "reset!" command */
146 {
147 ADT7420_Reset();
148 currentMode = 0;
149 resolutionSetting = 0;
150 /*!< Send feedback to user. */
151 CONSOLE_Print("The device was reset.\r\n");
152 }
153 /***************************************************************************//**
154 * @brief Sets the operational mode for ADT7420.
155 *
156 * @param param[0] - selected power mode;
157 * paramNo - number of parameters (must be >=1); only the first parameter is taken into account;
158 *
159 * @return None.
160 *******************************************************************************/
161 void SetMode(double* param, char paramNo) /*!< "mode=" command */
162 {
163 if(paramNo >= 1)
164 {
165 if(param[0] < 0)
166 {
167 param[0] = 0;
168 }
169 if(param[0] > 3)
170 {
171 param[0] = 3;
172 }
173 currentMode = (char)param[0];
174 ADT7420_SetOperationMode(currentMode);
175 /*!< Send feedback to user. */
176 CONSOLE_Print("%s%d\r\n",(char*)cmdList[2],currentMode);
177 }
178 else
179 {
180 /*!< Display error messages. */
181 CONSOLE_Print("Invalid parameter!\r\n");
182 CONSOLE_Print("%s%s\r\n", (char*)cmdList[2], (char*)cmdDescription[2]);
183 CONSOLE_Print("Example: %s\r\n", (char*)cmdExample[2]);
184 }
185 }
186 /***************************************************************************//**
187 * @brief Displays the selected operational mode.
188 *
189 * @return None.
190 *******************************************************************************/
191 void GetMode(double* param, char paramNo) /*!< "mode?" command */
192 {
193 /* Send the requested value to user */
194 CONSOLE_Print("%s%d\r\n",(char*)cmdList[2],currentMode);
195 }
196 /***************************************************************************//**
197 * @brief Sets the resolution for ADT7420.
198 *
199 * @param param[0] - selected resolution;
200 * paramNo - number of parameters (must be >=1); only the first parameter is taken into account;
201 *
202 * @return None.
203 *******************************************************************************/
204 void SetResolution(double* param, char paramNo) /*!< "resolution=" command */
205 {
206 if(paramNo >= 1)
207 {
208 if(param[0] < 0)
209 {
210 param[0] = 0;
211 }
212 if(param[0] > 1)
213 {
214 param[0] = 1;
215 }
216 resolutionSetting = param[0];
217 ADT7420_SetResolution(resolutionSetting);
218 /*!< Send feedback to user */
219 CONSOLE_Print("%s%d\r\n",cmdList[4],resolutionSetting);
220 }
221 else
222 {
223 /*!< Display error messages. */
224 CONSOLE_Print("Invalid parameter!\r\n");
225 CONSOLE_Print("%s%s\r\n", (char*)cmdList[4], (char*)cmdDescription[4]);
226 CONSOLE_Print("Example: %s\r\n", (char*)cmdExample[4]);
227 }
228 }
229
230 /***************************************************************************//**
231 * @brief Displays the selected resolution.
232 *
233 * @return None.
234 *******************************************************************************/
235 void GetResolution(double* param, char paramNo) /*!< "resolution?" command */
236 {
237 /* Send the requested value to user */
238 CONSOLE_Print("%s%d\r\n",(char*)cmdList[4],resolutionSetting);
239 }
240 /***************************************************************************//**
241 * @brief Displays the temperature.
242 *
243 * @return None.
244 *******************************************************************************/
245 void GetTemperature(double* param, char paramNo) /*!< "temperature?" command */
246 {
247 if(currentMode != 3)
248 {
249 /*!< Read the temperature value from the device */
250 temperature = ADT7420_GetTemperature();
251 /*!< Send the requested value to user */
252 CONSOLE_Print("temperature=%.3f [C]\r\n",temperature);
253 }
254 else
255 {
256 CONSOLE_Print("Device in shutdown mode. Please change the \
257 mode in order to display the temperature. \r\n");
258 }
259 }