-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_application.c
781 lines (669 loc) · 22.5 KB
/
data_application.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
/**
******************************************************************************
* @file DataLog/Src/datalog_application.c
* @author Central Labs
* @version V1.1.0
* @date 27-Sept-2016
* @brief This file provides a set of functions to handle the datalog
* application.
******************************************************************************
* @attention
*
* <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
*
* Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.st.com/software_license_agreement_liberty_v2
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "datalog_application.h"
#include "main.h"
#include "string.h"
#include "SensorTile.h"
#include <math.h>
#include <stdio.h>
/* FatFs includes component */
#include "ff_gen_drv.h"
#include "sd_diskio.h"
FRESULT res; /* FatFs function common result code */
uint32_t byteswritten, bytesread; /* File write/read counts */
FATFS SDFatFs; /* File system object for SD card logical drive */
FIL MyFile; /* File object */
char SDPath[4]; /* SD card logical drive path */
volatile uint8_t SD_Log_Enabled = 0;
static uint8_t verbose = 0; /* Verbose output to UART terminal ON/OFF. */
static char dataOut[256];
char newLine[] = "\r\n";
/**
* @brief Start SD-Card demo
* @param None
* @retval None
*/
void DATALOG_SD_Init(void)
{
char SDPath[4];
if(FATFS_LinkDriver(&SD_Driver, SDPath) == 0)
{
/* Register the file system object to the FatFs module */
if(f_mount(&SDFatFs, (TCHAR const*)SDPath, 0) != FR_OK)
{
/* FatFs Initialization Error */
while(1)
{
BSP_LED_On(LED1);
HAL_Delay(500);
BSP_LED_Off(LED1);
HAL_Delay(100);
}
}
}
}
/**
* @brief Start SD-Card demo
* @param None
* @retval None
*/
uint8_t DATALOG_SD_Log_Enable(void)
{
static uint16_t sdcard_file_counter = 0;
char header[] = "Timestamp\tAccX [mg]\tAccY [mg]\tAccZ [mg]\tGyroX [mdps]\tGyroY [mdps]\tGyroZ [mdps]\tMagX [mgauss]\tMagY [mgauss]\tMagZ [mgauss]\tP [mB]\tT [°C]\tH [%]\tVOL [mV]\tBAT [%]\r\n";
uint32_t byteswritten; /* written byte count */
char file_name[30] = {0};
/* SD SPI CS Config */
SD_IO_CS_Init();
sprintf(file_name, "%s%.3d%s", "SensorTile_Log_N", sdcard_file_counter, ".tsv");
sdcard_file_counter++;
HAL_Delay(100);
if(f_open(&MyFile, (char const*)file_name, FA_CREATE_ALWAYS | FA_WRITE) != FR_OK)
{
return 0;
}
if(f_write(&MyFile, (const void*)&header, sizeof(header)-1, (void *)&byteswritten) != FR_OK)
{
return 0;
}
return 1;
}
/**
* @brief Disable SDCard Log
* @param None
* @retval None
*/
void DATALOG_SD_Log_Disable(void)
{
f_close(&MyFile);
/* SD SPI Config */
SD_IO_CS_DeInit();
}
/**
* @brief Write New Line to file
* @param None
* @retval None
*/
void DATALOG_SD_NewLine(void)
{
uint32_t byteswritten; /* written byte count */
f_write(&MyFile, (const void*)&newLine, 2, (void *)&byteswritten);
}
/**
* @brief Handles the time+date getting/sending
* @param None
* @retval None
*/
void RTC_Handler( RTC_HandleTypeDef *RtcHandle )
{
uint8_t subSec = 0;
RTC_DateTypeDef sdatestructureget;
RTC_TimeTypeDef stimestructure;
HAL_RTC_GetTime( RtcHandle, &stimestructure, FORMAT_BIN );
HAL_RTC_GetDate( RtcHandle, &sdatestructureget, FORMAT_BIN );
subSec = (((((( int )RTC_SYNCH_PREDIV) - (( int )stimestructure.SubSeconds)) * 100) / ( RTC_SYNCH_PREDIV + 1 )) & 0xff );
if(SendOverUSB) /* Write data on the USB */
{
//sprintf( dataOut, "\nTimeStamp: %02d:%02d:%02d.%02d", stimestructure.Hours, stimestructure.Minutes, stimestructure.Seconds, subSec );
CDC_Fill_Buffer(( uint8_t * )dataOut, strlen( dataOut ));
}
else if(SD_Log_Enabled) /* Write data to the file on the SDCard */
{
uint8_t size;
size = sprintf( dataOut, "%02d:%02d:%02d.%02d\t", stimestructure.Hours, stimestructure.Minutes, stimestructure.Seconds, subSec);
res = f_write(&MyFile, dataOut, size, (void *)&byteswritten);
}
}
/**
* @brief Handles the accelerometer axes data getting/sending
* @param handle the device handle
* @retval None
*/
void Accelero_Sensor_Handler( void *handle , int *state, int *acc, double *maxvel,
RTC_HandleTypeDef *RtcHandle, int *initialminutes, int *initialseconds, int *initialsubsec,
int *ishook, void *ghandle)
{
uint8_t who_am_i;
float odr;
float fullScale;
uint8_t id;
SensorAxes_t acceleration;
SensorAxes_t angular_velocity;
uint8_t status;
int32_t d1, d2;
float thresh=50000.0f;
int jabthresh=10;
BSP_ACCELERO_Get_Instance( handle, &id );
BSP_ACCELERO_IsInitialized( handle, &status );
if ( status == 1 )
{
if ( BSP_ACCELERO_Get_Axes( handle, &acceleration ) == COMPONENT_ERROR )
{
acceleration.AXIS_X = 0;
acceleration.AXIS_Y = 0;
acceleration.AXIS_Z = 0;
}
//velocity
int32_t abs_acc;
abs_acc=((int)acceleration.AXIS_X*(int)acceleration.AXIS_X);
abs_acc+=((int)acceleration.AXIS_Y*(int)acceleration.AXIS_Y);
abs_acc+=((int)acceleration.AXIS_Z*(int)acceleration.AXIS_Z);
abs_acc=sqrt((float)abs_acc);
sprintf(dataOut,"");
CDC_Fill_Buffer(( uint8_t * )dataOut, strlen( dataOut ));
*acc=abs_acc;
if(acc>maxvel)
maxvel=acc;
//quickness
uint8_t subSec = 0;
RTC_DateTypeDef sdatestructureget;
RTC_TimeTypeDef stimestructure;
HAL_RTC_GetTime( RtcHandle, &stimestructure, FORMAT_BIN );
HAL_RTC_GetDate( RtcHandle, &sdatestructureget, FORMAT_BIN );
subSec = (((((( int )RTC_SYNCH_PREDIV) - (( int )stimestructure.SubSeconds)) * 100) / ( RTC_SYNCH_PREDIV + 1 )) & 0xff );
//type
if ( BSP_GYRO_Get_Axes( ghandle, &angular_velocity ) == COMPONENT_ERROR ){
angular_velocity.AXIS_X = 0;
angular_velocity.AXIS_Y = 0;
angular_velocity.AXIS_Z = 0;
}
//sprintf(dataOut," Y Ang Vel: %d", angular_velocity.AXIS_Y);
//CDC_Fill_Buffer(( uint8_t * )dataOut, strlen( dataOut ));
if(angular_velocity.AXIS_Y>thresh && *state==1){
*ishook=0;
}
if(SendOverUSB) /* Write data on the USB */
{
if(*acc>3550 && *state==0){
*state=1;
//sprintf( dataOut, "\nTimeStamp: %02d:%02d:%02d.%02d", stimestructure.Hours, stimestructure.Minutes, stimestructure.Seconds, subSec );
*initialminutes=stimestructure.Minutes;
*initialseconds=stimestructure.Seconds;
*initialsubsec=subSec;
/*
CDC_Fill_Buffer(( uint8_t * )dataOut, strlen( dataOut ));
sprintf(dataOut," Punch Started! ");
CDC_Fill_Buffer(( uint8_t * )dataOut, strlen( dataOut ));
sprintf(dataOut,"");
CDC_Fill_Buffer(( uint8_t * )dataOut, strlen( dataOut ));*/
}
else if(*acc<3550 && *state==1){
*state=0;
//sprintf( dataOut, "initialseconds: %d, initialsubsec: %d, stimestructure.Seconds: %d, subsec: %d", *initialseconds, *initialsubsec, stimestructure.Seconds, subSec);
//CDC_Fill_Buffer(( uint8_t * )dataOut, strlen( dataOut ));
int quick=abs(subSec-*initialsubsec);
//sprintf( dataOut, "Quickness: %i", quick);
//CDC_Fill_Buffer(( uint8_t * )dataOut, strlen( dataOut ));
if(quick<=jabthresh)
sprintf(dataOut,"Jab with force of %d m/ds detected ", *maxvel/20);
if(quick>jabthresh && *ishook==0)
sprintf(dataOut,"Cross with force of %d m/ds detected", *maxvel/20);
if(quick>jabthresh && *ishook==1)
sprintf(dataOut,"Hook with force of %d m/ds detected ", *maxvel/20);
CDC_Fill_Buffer(( uint8_t * )dataOut, strlen( dataOut ));
sprintf(dataOut,"");
CDC_Fill_Buffer(( uint8_t * )dataOut, strlen( dataOut ));
*ishook=1;
*maxvel=0;
}
if ( verbose == 1 )
{
if ( BSP_ACCELERO_Get_WhoAmI( handle, &who_am_i ) == COMPONENT_ERROR )
{
sprintf( dataOut, "WHO AM I address[%d]: ERROR\n", id );
}
else
{
sprintf( dataOut, "WHO AM I address[%d]: 0x%02X\n", id, who_am_i );
}
CDC_Fill_Buffer(( uint8_t * )dataOut, strlen( dataOut ));
if ( BSP_ACCELERO_Get_ODR( handle, &odr ) == COMPONENT_ERROR )
{
sprintf( dataOut, "ODR[%d]: ERROR\n", id );
}
else
{
floatToInt( odr, &d1, &d2, 3 );
sprintf( dataOut, "ODR[%d]: %d.%03d Hz\n", (int)id, (int)d1, (int)d2 );
}
CDC_Fill_Buffer(( uint8_t * )dataOut, strlen( dataOut ));
if ( BSP_ACCELERO_Get_FS( handle, &fullScale ) == COMPONENT_ERROR )
{
sprintf( dataOut, "FS[%d]: ERROR\n", id );
}
else
{
floatToInt( fullScale, &d1, &d2, 3 );
sprintf( dataOut, "FS[%d]: %d.%03d g\n", (int)id, (int)d1, (int)d2 );
}
CDC_Fill_Buffer(( uint8_t * )dataOut, strlen( dataOut ));
}
}
else if(SD_Log_Enabled) /* Write data to the file on the SDCard */
{
uint8_t size;
size = sprintf(dataOut, "%d\t%d\t%d\t", (int)acceleration.AXIS_X, (int)acceleration.AXIS_Y, (int)acceleration.AXIS_Z);
res = f_write(&MyFile, dataOut, size, (void *)&byteswritten);
}
}
}
/**
* @brief Handles the gyroscope axes data getting/sending
* @param handle the device handle
* @retval None
*/
void Gyro_Sensor_Handler( void *handle )
{
uint8_t who_am_i;
float odr;
float fullScale;
uint8_t id;
SensorAxes_t angular_velocity;
uint8_t status;
int32_t d1, d2;
float thresh=1000000.0f;
BSP_GYRO_Get_Instance( handle, &id );
BSP_GYRO_IsInitialized( handle, &status );
if ( status == 1 )
{
if ( BSP_GYRO_Get_Axes( handle, &angular_velocity ) == COMPONENT_ERROR )
{
angular_velocity.AXIS_X = 0;
angular_velocity.AXIS_Y = 0;
angular_velocity.AXIS_Z = 0;
}
if(SendOverUSB) /* Write data on the USB */
{
//sprintf( dataOut, "\n\rGYR_X: %d, GYR_Y: %d, GYR_Z: %d", (int)angular_velocity.AXIS_X, (int)angular_velocity.AXIS_Y, (int)angular_velocity.AXIS_Z );
/*
if(angular_velocity.AXIS_X>thresh){
sprintf(dataOut,"X Rotation!");
CDC_Fill_Buffer(( uint8_t * )dataOut, strlen( dataOut ));
}
if(angular_velocity.AXIS_Y>thresh){
sprintf(dataOut,"Y Rotation!");
CDC_Fill_Buffer(( uint8_t * )dataOut, strlen( dataOut ));
}
if(angular_velocity.AXIS_Z>thresh){
sprintf(dataOut,"Z Rotation!");
CDC_Fill_Buffer(( uint8_t * )dataOut, strlen( dataOut ));
}
*/
sprintf(dataOut,"");
CDC_Fill_Buffer(( uint8_t * )dataOut, strlen( dataOut ));
if ( verbose == 1 )
{
if ( BSP_GYRO_Get_WhoAmI( handle, &who_am_i ) == COMPONENT_ERROR )
{
sprintf( dataOut, "WHO AM I address[%d]: ERROR\n", id );
}
else
{
sprintf( dataOut, "WHO AM I address[%d]: 0x%02X\n", id, who_am_i );
}
CDC_Fill_Buffer(( uint8_t * )dataOut, strlen( dataOut ));
if ( BSP_GYRO_Get_ODR( handle, &odr ) == COMPONENT_ERROR )
{
sprintf( dataOut, "ODR[%d]: ERROR\n", id );
}
else
{
floatToInt( odr, &d1, &d2, 3 );
sprintf( dataOut, "ODR[%d]: %d.%03d Hz\n", (int)id, (int)d1, (int)d2 );
}
CDC_Fill_Buffer(( uint8_t * )dataOut, strlen( dataOut ));
if ( BSP_GYRO_Get_FS( handle, &fullScale ) == COMPONENT_ERROR )
{
sprintf( dataOut, "FS[%d]: ERROR\n", id );
}
else
{
floatToInt( fullScale, &d1, &d2, 3 );
sprintf( dataOut, "FS[%d]: %d.%03d dps\n", (int)id, (int)d1, (int)d2 );
}
CDC_Fill_Buffer(( uint8_t * )dataOut, strlen( dataOut ));
}
}
else if(SD_Log_Enabled) /* Write data to the file on the SDCard */
{
uint8_t size;
size = sprintf(dataOut, "%d\t%d\t%d\t", (int)angular_velocity.AXIS_X, (int)angular_velocity.AXIS_Y, (int)angular_velocity.AXIS_Z);
res = f_write(&MyFile, dataOut, size, (void *)&byteswritten);
}
}
}
/**
* @brief Handles the magneto axes data getting/sending
* @param handle the device handle
* @retval None
*/
void Magneto_Sensor_Handler( void *handle )
{
uint8_t who_am_i;
float odr;
float fullScale;
uint8_t id;
SensorAxes_t magnetic_field;
uint8_t status;
int32_t d1, d2;
BSP_MAGNETO_Get_Instance( handle, &id );
BSP_MAGNETO_IsInitialized( handle, &status );
if ( status == 1 )
{
if ( BSP_MAGNETO_Get_Axes( handle, &magnetic_field ) == COMPONENT_ERROR )
{
magnetic_field.AXIS_X = 0;
magnetic_field.AXIS_Y = 0;
magnetic_field.AXIS_Z = 0;
}
if(SendOverUSB) /* Write data on the USB */
{
//sprintf( dataOut, "\n\rMAG_X: %d, MAG_Y: %d, MAG_Z: %d", (int)magnetic_field.AXIS_X, (int)magnetic_field.AXIS_Y, (int)magnetic_field.AXIS_Z );
CDC_Fill_Buffer(( uint8_t * )dataOut, strlen( dataOut ));
if ( verbose == 1 )
{
if ( BSP_MAGNETO_Get_WhoAmI( handle, &who_am_i ) == COMPONENT_ERROR )
{
sprintf( dataOut, "WHO AM I address[%d]: ERROR\n", id );
}
else
{
sprintf( dataOut, "WHO AM I address[%d]: 0x%02X\n", id, who_am_i );
}
CDC_Fill_Buffer(( uint8_t * )dataOut, strlen( dataOut ));
if ( BSP_MAGNETO_Get_ODR( handle, &odr ) == COMPONENT_ERROR )
{
sprintf( dataOut, "ODR[%d]: ERROR\n", id );
}
else
{
floatToInt( odr, &d1, &d2, 3 );
sprintf( dataOut, "ODR[%d]: %d.%03d Hz\n", (int)id, (int)d1, (int)d2 );
}
CDC_Fill_Buffer(( uint8_t * )dataOut, strlen( dataOut ));
if ( BSP_MAGNETO_Get_FS( handle, &fullScale ) == COMPONENT_ERROR )
{
sprintf( dataOut, "FS[%d]: ERROR\n", id );
}
else
{
floatToInt( fullScale, &d1, &d2, 3 );
sprintf( dataOut, "FS[%d]: %d.%03d Gauss\n", (int)id, (int)d1, (int)d2 );
}
CDC_Fill_Buffer(( uint8_t * )dataOut, strlen( dataOut ));
}
}
else if(SD_Log_Enabled) /* Write data to the file on the SDCard */
{
uint8_t size;
size = sprintf(dataOut, "%d\t%d\t%d\t", (int)magnetic_field.AXIS_X, (int)magnetic_field.AXIS_Y, (int)magnetic_field.AXIS_Z);
res = f_write(&MyFile, dataOut, size, (void *)&byteswritten);
}
}
}
/**
* @brief Handles the humidity data getting/sending
* @param handle the device handle
* @retval None
*/
void Humidity_Sensor_Handler( void *handle )
{
int32_t d1, d2;
uint8_t who_am_i;
float odr;
uint8_t id;
float humidity;
uint8_t status;
BSP_HUMIDITY_Get_Instance( handle, &id );
BSP_HUMIDITY_IsInitialized( handle, &status );
if ( status == 1 )
{
if ( BSP_HUMIDITY_Get_Hum( handle, &humidity ) == COMPONENT_ERROR )
{
humidity = 0.0f;
}
floatToInt( humidity, &d1, &d2, 2 );
if(SendOverUSB) /* Write data on the USB */
{
//sprintf( dataOut, "\n\rHUM: %d.%02d", (int)d1, (int)d2 );
CDC_Fill_Buffer(( uint8_t * )dataOut, strlen( dataOut ));
if ( verbose == 1 )
{
if ( BSP_HUMIDITY_Get_WhoAmI( handle, &who_am_i ) == COMPONENT_ERROR )
{
sprintf( dataOut, "WHO AM I address[%d]: ERROR\n", id );
}
else
{
sprintf( dataOut, "WHO AM I address[%d]: 0x%02X\n", id, who_am_i );
}
CDC_Fill_Buffer(( uint8_t * )dataOut, strlen( dataOut ));
if ( BSP_HUMIDITY_Get_ODR( handle, &odr ) == COMPONENT_ERROR )
{
sprintf( dataOut, "ODR[%d]: ERROR\n", id );
}
else
{
floatToInt( odr, &d1, &d2, 3 );
sprintf( dataOut, "ODR[%d]: %d.%03d Hz\n", (int)id, (int)d1, (int)d2 );
}
CDC_Fill_Buffer(( uint8_t * )dataOut, strlen( dataOut ));
}
}
else if(SD_Log_Enabled) /* Write data to the file on the SDCard */
{
uint8_t size;
size = sprintf( dataOut, "%5.2f\t", humidity);
res = f_write(&MyFile, dataOut, size, (void *)&byteswritten);
}
}
}
/**
* @brief Handles the temperature data getting/sending
* @param handle the device handle
* @retval None
*/
void Temperature_Sensor_Handler( void *handle )
{
int32_t d1, d2;
uint8_t who_am_i;
float odr;
uint8_t id;
float temperature;
uint8_t status;
BSP_TEMPERATURE_Get_Instance( handle, &id );
BSP_TEMPERATURE_IsInitialized( handle, &status );
if ( status == 1 )
{
if ( BSP_TEMPERATURE_Get_Temp( handle, &temperature ) == COMPONENT_ERROR )
{
temperature = 0.0f;
}
floatToInt( temperature, &d1, &d2, 2 );
if(SendOverUSB) /* Write data on the USB */
{
//sprintf( dataOut, "\n\rTEMP: %d.%02d", (int)d1, (int)d2 );
CDC_Fill_Buffer(( uint8_t * )dataOut, strlen( dataOut ));
if ( verbose == 1 )
{
if ( BSP_TEMPERATURE_Get_WhoAmI( handle, &who_am_i ) == COMPONENT_ERROR )
{
sprintf( dataOut, "WHO AM I address[%d]: ERROR\n", id );
}
else
{
sprintf( dataOut, "WHO AM I address[%d]: 0x%02X\n", id, who_am_i );
}
CDC_Fill_Buffer(( uint8_t * )dataOut, strlen( dataOut ));
if ( BSP_TEMPERATURE_Get_ODR( handle, &odr ) == COMPONENT_ERROR )
{
sprintf( dataOut, "ODR[%d]: ERROR\n", id );
}
else
{
floatToInt( odr, &d1, &d2, 3 );
sprintf( dataOut, "ODR[%d]: %d.%03d Hz\n", (int)id, (int)d1, (int)d2 );
}
CDC_Fill_Buffer(( uint8_t * )dataOut, strlen( dataOut ));
}
}
else if(SD_Log_Enabled) /* Write data to the file on the SDCard */
{
uint8_t size;
size = sprintf( dataOut, "%3.1f\t", temperature);
res = f_write(&MyFile, dataOut, size, (void *)&byteswritten);
}
}
}
/**
* @brief Handles the pressure sensor data getting/sending
* @param handle the device handle
* @retval None
*/
void Pressure_Sensor_Handler( void *handle )
{
int32_t d1, d2;
uint8_t who_am_i;
float odr;
uint8_t id;
float pressure;
uint8_t status;
BSP_PRESSURE_Get_Instance( handle, &id );
BSP_PRESSURE_IsInitialized( handle, &status );
if( status == 1 )
{
if ( BSP_PRESSURE_Get_Press( handle, &pressure ) == COMPONENT_ERROR )
{
pressure = 0.0f;
}
floatToInt( pressure, &d1, &d2, 2 );
if(SendOverUSB)
{
//sprintf(dataOut, "\n\rPRESS: %d.%02d", (int)d1, (int)d2);
CDC_Fill_Buffer(( uint8_t * )dataOut, strlen( dataOut ));
if ( verbose == 1 )
{
if ( BSP_PRESSURE_Get_WhoAmI( handle, &who_am_i ) == COMPONENT_ERROR )
{
sprintf( dataOut, "WHO AM I address[%d]: ERROR\n", id );
}
else
{
sprintf( dataOut, "WHO AM I address[%d]: 0x%02X\n", id, who_am_i );
}
CDC_Fill_Buffer(( uint8_t * )dataOut, strlen( dataOut ));
if ( BSP_PRESSURE_Get_ODR( handle, &odr ) == COMPONENT_ERROR )
{
sprintf( dataOut, "ODR[%d]: ERROR\n", id );
}
else
{
floatToInt( odr, &d1, &d2, 3 );
sprintf( dataOut, "ODR[%d]: %d.%03d Hz\n", (int)id, (int)d1, (int)d2 );
}
CDC_Fill_Buffer(( uint8_t * )dataOut, strlen( dataOut ));
}
}
else if(SD_Log_Enabled) /* Write data to the file on the SDCard */
{
uint8_t size;
size = sprintf( dataOut, "%5.2f\t", pressure);
res = f_write(&MyFile, dataOut, size, (void *)&byteswritten);
}
}
}
void Gas_Gauge_Handler( void *handle )
{
uint32_t voltage, soc;
uint8_t vmode, status;
BSP_GG_IsInitialized(handle, &status );
if ( status == 1 )
{
/* Update Gas Gauge Status */
if (BSP_GG_Task(handle, &vmode)== COMPONENT_ERROR )
{
voltage= 0.0f;
soc= 0.0f;
}
/* Read the Gas Gauge Status */
if ( BSP_GG_GetVoltage(handle, &voltage) == COMPONENT_ERROR )
{
voltage = 0.0f;
}
if ( BSP_GG_GetSOC(handle, &soc) == COMPONENT_ERROR )
{
soc= 0.0f;
}
if(SendOverUSB) /* Write data on the USB */
{
//sprintf( dataOut, "\n\rV: %dmV Chrg: %d%%", (uint32_t)voltage, (uint32_t)soc);
CDC_Fill_Buffer(( uint8_t * )dataOut, strlen( dataOut ));
}
else if(SD_Log_Enabled) /* Write data to the file on the SDCard */
{
uint8_t size;
size = sprintf( dataOut, "%d\t%d\t", (uint32_t)voltage, (uint32_t)soc);
res = f_write(&MyFile, dataOut, size, (void *)&byteswritten);
}
}
}
/**
* @brief Splits a float into two integer values.
* @param in the float value as input
* @param out_int the pointer to the integer part as output
* @param out_dec the pointer to the decimal part as output
* @param dec_prec the decimal precision to be used
* @retval None
*/
void floatToInt( float in, int32_t *out_int, int32_t *out_dec, int32_t dec_prec )
{
*out_int = (int32_t)in;
if(in >= 0.0f)
{
in = in - (float)(*out_int);
}
else
{
in = (float)(*out_int) - in;
}
*out_dec = (int32_t)trunc(in * pow(10, dec_prec));
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/