77#include <stdlib.h>
88#include "DataSampling.h"
99/*********************************************************************************/
10- #if (DATA_SAMPLING_REVISION_DATE != 20200416 )
10+ #if (DATA_SAMPLING_REVISION_DATE != 20200724 )
1111#error wrong include file. (DataSampling.h)
1212#endif
13+ /*********************************************************************************/
14+
1315/*********************************************************************************/
1416/** Global variable **/
1517
18+ static tS8 * pS8 ;
19+ static tS16 * pS16 ;
20+ static tS32 * pS32 ;
21+ static tS32 Ret ;
1622
23+ /*********************************************************************************/
24+ inline static void InToBuf (tag_DataSampling * Smp , tU8 Idx , tS32 Data )
25+ {
26+ switch (Smp -> DataSize )
27+ {
28+ case sizeof (tS8 ) : pS8 = (tS8 * ) Smp -> Buf ; pS8 [Idx ] = Data ; break ;
29+ case sizeof (tS16 ) : pS16 = (tS16 * ) Smp -> Buf ; pS16 [Idx ] = Data ; break ;
30+ case sizeof (tS32 ) : pS32 = (tS32 * ) Smp -> Buf ; pS32 [Idx ] = Data ; break ;
31+ }
32+ }
33+ /*********************************************************************************/
34+ inline static tS32 OutFromBuf (tag_DataSampling * Smp , tU8 Idx )
35+ {
36+ switch (Smp -> DataSize )
37+ {
38+ case sizeof (tS8 ) : pS8 = (tS8 * ) Smp -> Buf ; Ret = pS8 [Idx ]; break ;
39+ case sizeof (tS16 ) : pS16 = (tS16 * ) Smp -> Buf ; Ret = pS16 [Idx ]; break ;
40+ case sizeof (tS32 ) : pS32 = (tS32 * ) Smp -> Buf ; Ret = pS32 [Idx ]; break ;
41+ }
42+
43+ return Ret ;
44+ }
1745/*********************************************************************************/
1846/*
1947 1) 인수
2654 3) 설명
2755 - 인수로 전달받은 데이터로 버퍼를 채움.
2856*/
29- static void FillBuffer (tag_DataSampling * Smp , tS16 Data )
57+ static void FillBuffer (tag_DataSampling * Smp , tS32 Data )
3058{
3159 tS16 i ;
3260
3361 Smp -> Sum = Smp -> Index = 0 ;
3462 for (i = 0 ; i < Smp -> Level ; i ++ )
3563 {
36- Smp -> Buf [ i ] = Data ;
64+ InToBuf ( Smp , i , Data ) ;
3765 Smp -> Sum += Data ;
3866 }
3967}
@@ -51,14 +79,21 @@ static void FillBuffer(tag_DataSampling *Smp, tS16 Data)
5179 - 'tag_DataSampling' 인스턴스의 필수 초기화 실행.
5280 - DataSampling 모듈을 사용하기 위해 선행적 실행 필요.
5381*/
54- tU8 DataSamplingInitGeneral (tag_DataSampling * Smp , tS16 BufSize )
82+ tU8 DataSamplingInitGeneral (tag_DataSampling * Smp , tS16 BufSize , tS8 DataSize )
5583{
5684 if (Smp -> Bit .InitGeneral == true)
5785 {
5886 return true;
5987 }
88+
89+ if ((DataSize != sizeof (tS8 )) && (DataSize != sizeof (tS16 )) && (DataSize != sizeof (tS32 )))
90+ {
91+ return false;
92+ }
6093
61- Smp -> Buf = (tS16 * ) malloc (sizeof (tS16 ) * BufSize );
94+ Smp -> DataSize = DataSize ;
95+
96+ Smp -> Buf = malloc (Smp -> DataSize * BufSize );
6297
6398 if (Smp -> Buf != null )
6499 {
@@ -91,7 +126,7 @@ void DataSamplingChangeLevel(tag_DataSampling *Smp, tS16 Level)
91126 if ((Smp -> Level != Level ) && (1 <= Level ) && (Level <= Smp -> Level ))
92127 {
93128 Smp -> Level = Level ;
94- FillBuffer (Smp , Smp -> Buf [ 0 ] );
129+ FillBuffer (Smp , OutFromBuf ( Smp , 0 ) );
95130 }
96131}
97132/*********************************************************************************/
@@ -106,9 +141,9 @@ void DataSamplingChangeLevel(tag_DataSampling *Smp, tS16 Level)
106141 3) 설명
107142 - ring buffer 형식으로 데이터를 입력 받아 평균을 내어 샘플링.
108143*/
109- tS16 DataSamplingGetData (tag_DataSampling * Smp , tS16 Data )
144+ tS32 DataSamplingGetData (tag_DataSampling * Smp , tS32 Data )
110145{
111- tS16 Result ;
146+ tS32 Result ;
112147
113148 if (Smp -> Bit .InitGeneral == false)
114149 {
@@ -121,9 +156,9 @@ tS16 DataSamplingGetData(tag_DataSampling *Smp, tS16 Data)
121156 FillBuffer (Smp , Data );
122157 }
123158
124- Smp -> Sum -= Smp -> Buf [ Smp -> Index ] ;
125- Smp -> Buf [ Smp -> Index ] = Data ;
126- Smp -> Sum += Smp -> Buf [ Smp -> Index ] ;
159+ Smp -> Sum -= OutFromBuf ( Smp , Smp -> Index ) ;
160+ InToBuf ( Smp , Smp -> Index , Data ) ;
161+ Smp -> Sum += OutFromBuf ( Smp , Smp -> Index ) ;
127162 Result = Smp -> Sum / Smp -> Level ;
128163
129164 if (++ (Smp -> Index ) >= Smp -> Level ) Smp -> Index = 0 ;
0 commit comments