Debian Codegraph @

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

raw symbols SCIP
1 /***************************************************************************//**
2 * @file Command.c
3 * @brief Implementation of the commands given by user through UART for PmodACL.
4 * @author DNechita ([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 "Communication.h"
45 #include "Command.h"
46 #include "ADXL362.h"
47 #include "Console.h"
48
49 /******************************************************************************/
50 /************************ Constants Definitions *******************************/
51 /******************************************************************************/
52
53 /*!< List of available commands */
54 const char* cmdList[] = { "help?",
55 "id?",
56 "measure=",
57 "temperature?",
58 "reset!",
59 "acceleration?",
60 "accelerationX?",
61 "accelerationY?",
62 "accelerationZ?",
63 "activity?"};
64 const char* cmdDescription[] = {
65 " - Displays all available commands.",
66 " - Display device details.",
67 " - Start/stop the measurement process of the device. Accepted values:\r\n\
68 \t0 - Stop measurement. \r\n\
69 \t1 - Start measurement.",
70 " - Displays the temperature.",
71 " - Resets the device.",
72 " - Displays the accelerations on XYZ axes.",
73 " - Displays the acceleration on X axis.",
74 " - Displays the acceleration on Y axis.",
75 " - Displays the acceleration on Z axis.",
76 " - Displays the activity status of the device. It runs for 5 motion detections."};
77 const char* cmdExample[] = {
78 "",
79 "",
80 "To turn on the measurement process, type: measure=1.",
81 "",
82 "",
83 "",
84 "",
85 "",
86 "",
87 ""};
88
89 const char cmdNo = (sizeof(cmdList) / sizeof(const char*));
90
91 /***************************************************************************/
92 /************************ Variables Definitions ****************************/
93 /***************************************************************************/
94 cmdFunction cmdFunctions[10] = {GetHelp, GetId, SetMeasurement, GetTemperature,
95 DoReset, GetAcceleration, GetAccelerationX,
96 GetAccelerationY, GetAccelerationZ, GetActivity};
97 char measureSetting = 0;
98
99 /******************************************************************************/
100 /************************ Functions Description *******************************/
101 /******************************************************************************/
102
103 /***************************************************************************//**
104 * @brief Displays all available commands.
105 *
106 * @return None.
107 *******************************************************************************/
108 void GetHelp(double* param, char paramNo) /*!< "help?" command */
109 {
110 char displayCmd;
111
112 CONSOLE_Print("Available commands:\r\n");
113 for(displayCmd = 0; displayCmd < cmdNo; displayCmd++)
114 {
115 CONSOLE_Print("%s%s\r\n", (char*)cmdList[displayCmd],
116 (char*)cmdDescription[displayCmd]);
117 }
118 }
119
120 /***************************************************************************//**
121 * @brief Initializes the device.
122 *
123 * @return - The result of the initialization.
124 * Example: ERROR - the device was not initialized or the device
125 * is not present.
126 * SUCCES - the device was initialized and the device
127 * is present.
128 *******************************************************************************/
129 char DoDeviceInit(void)
130 {
131
132 if(ADXL362_Init() == 0)
133 {
134 CONSOLE_Print("ADXL362 OK\r\n");
135 GetHelp(NULL, 0);
136 return SUCCESS;
137 }
138 else
139 {
140 CONSOLE_Print("ADXL362 Error\r\n");
141
142 return ERROR;
143 }
144 }
145
146 /***************************************************************************//**
147 * @brief Displays device details.
148 *
149 * @return None.
150 *******************************************************************************/
151 void GetId(double* param, char paramNo) /*!< "id?" command */
152 {
153 unsigned char regVal = 0;
154
155 /*!< Send the requested value to user. */
156 CONSOLE_Print("Analog Devices device ID: ");
157 ADXL362_GetRegisterValue(&regVal, ADXL362_REG_DEVID_AD, 1);
158 CONSOLE_Print("%d\r\n",regVal);
159
160 CONSOLE_Print("Analog Devices MEMS ID: ");
161 ADXL362_GetRegisterValue(&regVal, ADXL362_REG_DEVID_MST, 1);
162 CONSOLE_Print("%d\r\n",regVal);
163
164 CONSOLE_Print("Analog Devices PART ID: ");
165 ADXL362_GetRegisterValue(&regVal, ADXL362_REG_PARTID, 1);
166 CONSOLE_Print("%d\r\n",regVal);
167
168 CONSOLE_Print("Analog Devices REVISION ID: ");
169 ADXL362_GetRegisterValue(&regVal, ADXL362_REG_REVID, 1);
170 CONSOLE_Print("%d\r\n",regVal);
171 }
172
173 /***************************************************************************//**
174 * @brief Turn on/off the measurement process of the device.
175 *
176 * @return None.
177 *******************************************************************************/
178 void SetMeasurement(double* param, char paramNo) /*!< "measure=" command */
179 {
180 if(paramNo >= 1)
181 {
182 if(param[0] < 0)
183 {
184 param[0] = 0;
185 }
186 if(param[0] > 1)
187 {
188 param[0] = 1;
189 }
190
191 measureSetting = param[0];
192 /*!< Turn on/off the measurement process of the device. */
193 ADXL362_SetPowerMode(param[0]);
194 /*!< Send feedback to user. */
195 CONSOLE_Print("%s%d\r\n",cmdList[2],(short)param[0]);
196 }
197 else
198 {
199 /*!< Display error messages. */
200 CONSOLE_Print("Invalid parameter!\r\n");
201 CONSOLE_Print("%s%s\r\n", (char*)cmdList[2], (char*)cmdDescription[2]);
202 CONSOLE_Print("Example: %s\r\n", (char*)cmdExample[2]);
203 }
204 }
205
206 /***************************************************************************//**
207 * @brief Displays the temperature.
208 *
209 * @return None.
210 *******************************************************************************/
211 void GetTemperature(double* param, char paramNo) /*!< "temperature?" command */
212 {
213 float temperature = 0; /*!< Last read value. */
214
215 if(measureSetting)
216 {
217 /*!< Read temperature from the device */
218 temperature = ADXL362_ReadTemperature();
219 /*!< Send the requested value to user */
220 CONSOLE_Print("temperature=%.2f [C]\r\n",temperature);
221 }
222 else
223 {
224 CONSOLE_Print("Device is in Standby mode. Use the 'measure=1' command \
225 to enter Measurement mode.\r\n");
226 }
227 }
228
229 /***************************************************************************//**
230 * @brief Resets the device.
231 *
232 * @return None.
233 *******************************************************************************/
234 void DoReset(double* param, char paramNo) /*!< "reset!" command */
235 {
236 /*!< Resets the device(software reset). */
237 measureSetting = 0;
238 ADXL362_SoftwareReset();
239
240 /*!< Send feedback to user */
241 CONSOLE_Print("Device was reset.\r\n");
242 }
243
244 /***************************************************************************//**
245 * @brief Displays the accelerations on XYZ axes.
246 *
247 * @return None.
248 *******************************************************************************/
249 void GetAcceleration(double* param, char paramNo) /*!< "acceleration?" command */
250 {
251 unsigned char samplesNumber = 0;
252 float xAxisAcc = 0;
253 float yAxisAcc = 0;
254 float zAxisAcc = 0;
255
256 if(measureSetting)
257 {
258 /*!< Read and displays 30 samples from the accelerometer. */
259 samplesNumber = 30;
260 while(samplesNumber)
261 {
262 /*!< Read the requested value from the device. */
263 ADXL362_GetGxyz(&xAxisAcc, &yAxisAcc, &zAxisAcc);
264 /*!< Send the requested value to user. */
265 CONSOLE_Print("x axis:%.3f\r\n",xAxisAcc);
266 CONSOLE_Print("y axis:%.3f\r\n",yAxisAcc);
267 CONSOLE_Print("z axis:%.3f\r\n",zAxisAcc);
268
269 TIME_DelayMs(200);
270 samplesNumber--;
271 }
272 }
273 else
274 {
275 CONSOLE_Print("Device is in Standby mode. Use the 'measure=1' command \
276 to enter Measurement mode.\r\n");
277 }
278 }
279
280 /***************************************************************************//**
281 * @brief Displays the acceleration on X axis.
282 *
283 * @return None.
284 *******************************************************************************/
285 void GetAccelerationX(double* param, char paramNo) /*!< "accelerationX?" command */
286 {
287 unsigned char samplesNumber = 0;
288 float xAxisAcc = 0;
289 float yAxisAcc = 0;
290 float zAxisAcc = 0;
291
292 if(measureSetting)
293 {
294 /*!< Read and displays 30 samples from the accelerometer. */
295 samplesNumber = 30;
296 while(samplesNumber)
297 {
298 /*!< Read the requested value from the device. */
299 ADXL362_GetGxyz(&xAxisAcc, &yAxisAcc, &zAxisAcc);
300 /*!< Send the requested value to user. */
301 CONSOLE_Print("x axis:%.3f\r\n",xAxisAcc);
302
303 TIME_DelayMs(200);
304 samplesNumber--;
305 }
306 }
307 else
308 {
309 CONSOLE_Print("Device is in Standby mode. Use the 'measure=1' command \
310 to enter Measurement mode.\r\n");
311 }
312 }
313
314 /***************************************************************************//**
315 * @brief Displays the acceleration on Y axis.
316 *
317 * @return None.
318 *******************************************************************************/
319 void GetAccelerationY(double* param, char paramNo) /*!< "accelerationY?" command */
320 {
321 unsigned char samplesNumber = 0;
322 float xAxisAcc = 0;
323 float yAxisAcc = 0;
324 float zAxisAcc = 0;
325
326 if(measureSetting)
327 {
328 /*!< Read and displays 30 samples from the accelerometer. */
329 samplesNumber = 30;
330 while(samplesNumber)
331 {
332 /*!< Read the requested value from the device. */
333 ADXL362_GetGxyz(&xAxisAcc, &yAxisAcc, &zAxisAcc);
334 /*!< Send the requested value to user. */
335 CONSOLE_Print("y axis:%.3f\r\n",yAxisAcc);
336
337 TIME_DelayMs(200);
338 samplesNumber--;
339 }
340 }
341 else
342 {
343 CONSOLE_Print("Device is in Standby mode. Use the 'measure=1' command \
344 to enter Measurement mode.\r\n");
345 }
346 }
347
348 /***************************************************************************//**
349 * @brief Displays the acceleration on Z axis.
350 *
351 * @return None.
352 *******************************************************************************/
353 void GetAccelerationZ(double* param, char paramNo) /*!< "accelerationZ?" command */
354 {
355 unsigned char samplesNumber = 0;
356 float xAxisAcc = 0;
357 float yAxisAcc = 0;
358 float zAxisAcc = 0;
359
360 if(measureSetting)
361 {
362 /*!< Read and displays 30 samples from the accelerometer. */
363 samplesNumber = 30;
364 while(samplesNumber)
365 {
366 /*!< Read the requested value from the device. */
367 ADXL362_GetGxyz(&xAxisAcc, &yAxisAcc, &zAxisAcc);
368 /*!< Send the requested value to user. */
369 CONSOLE_Print("z axis:%.3f\r\n",zAxisAcc);
370
371 TIME_DelayMs(200);
372 samplesNumber--;
373 }
374 }
375 else
376 {
377 CONSOLE_Print("Device is in Standby mode. Use the 'measure=1' command \
378 to enter Measurement mode.\r\n");
379 }
380 }
381
382 /***************************************************************************//**
383 * @brief Displays the activity status of the device.
384 *
385 * @return None.
386 *******************************************************************************/
387 void GetActivity(double* param, char paramNo) /*!< "activity?" command */
388 {
389 unsigned char detections = 0;
390 unsigned char regVal = 0;
391
392 if(measureSetting)
393 {
394 /*!< Setup the activity and inactivity detection. */
395 CONSOLE_Print("Activity polling enabled.\r\n");
396
397 ADXL362_SetPowerMode(0);
398 ADXL362_SetOutputRate(ADXL362_ODR_100_HZ);
399 ADXL362_SetRegisterValue(
400 ADXL362_ACT_INACT_CTL_LINKLOOP(ADXL362_MODE_LINK),
401 ADXL362_REG_ACT_INACT_CTL,
402 1);
403 ADXL362_SetupActivityDetection(1, 60, 4);
404 ADXL362_SetupInactivityDetection(1, 700, 250);
405 ADXL362_SetPowerMode(1);
406 /*!< Clear ACT and INACT bits by reading the Status Register. */
407 ADXL362_GetRegisterValue(&regVal,
408 ADXL362_REG_STATUS,
409 1);
410 /*!< Exit polling after 5 detections. */
411 detections = 5;
412 while(detections)
413 {
414 do /*!< Wait for the detection of an activity or inactivity. */
415 {
416 ADXL362_GetRegisterValue(&regVal,
417 ADXL362_REG_STATUS,
418 1);
419 }while(!(regVal & ADXL362_STATUS_ACT) &&
420 !(regVal & ADXL362_STATUS_INACT));
421 detections--;
422 if(regVal & ADXL362_STATUS_ACT)
423 {
424 CONSOLE_Print("Activity\r\n");
425 }
426 if(regVal & ADXL362_STATUS_INACT)
427 {
428 CONSOLE_Print("Inactivity\r\n");
429 }
430 }
431 CONSOLE_Print("Finished activity polling.\r\n");
432 }
433 else
434 {
435 CONSOLE_Print("Device is in Standby mode. Use the 'measure=1' command \
436 to enter Measurement mode.\r\n");
437 }
438 }