Skip to content

Commit 3b4fe0f

Browse files
Merge pull request #5 from TimerOverflow/20200724
20200724
2 parents ccd87b3 + 76e52d4 commit 3b4fe0f

File tree

2 files changed

+54
-15
lines changed

2 files changed

+54
-15
lines changed

DataSampling.c

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,41 @@
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) 인수
@@ -26,14 +54,14 @@
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;

DataSampling.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@
99
/*********************************************************************************/
1010
#include "SysTypedef.h"
1111
/*********************************************************************************/
12-
#define DATA_SAMPLING_REVISION_DATE 20200416
12+
#define DATA_SAMPLING_REVISION_DATE 20200724
1313
/*********************************************************************************/
1414
/** REVISION HISTORY **/
1515
/*
16+
2020. 07. 24. - 이제 샘플링 데이터의 자료형을 tS8, tS16, tS32 중 선택할 수 있음.
17+
Jeong Hyun Gu
18+
1619
2020. 04. 16. - DataSamplingResetData()에서 FillBuffer()호출 부분 삭제.
1720
Jeong Hyun Gu
1821
@@ -47,17 +50,18 @@ typedef struct
4750
tU8 InitFillBuffer : 1; //버퍼초기화
4851
}Bit;
4952

50-
tS16 *Buf;
53+
void *Buf;
5154
tS16 Index, BufSize, Level;
5255
tS32 Sum;
56+
tS8 DataSize;
5357
}tag_DataSampling;
5458

5559
/*********************************************************************************/
5660
/**Function**/
5761

58-
tU8 DataSamplingInitGeneral(tag_DataSampling *Smp, tS16 BufSize);
62+
tU8 DataSamplingInitGeneral(tag_DataSampling *Smp, tS16 BufSize, tS8 DataSzie);
5963
void DataSamplingChangeLevel(tag_DataSampling *Smp, tS16 Level);
60-
tS16 DataSamplingGetData(tag_DataSampling *Smp, tS16 Data);
64+
tS32 DataSamplingGetData(tag_DataSampling *Smp, tS32 Data);
6165
void DataSamplingResetData(tag_DataSampling *Smp);
6266

6367
/*********************************************************************************/

0 commit comments

Comments
 (0)