Skip to content

Commit a85475b

Browse files
Merge pull request #1 from TimerOverflow/modify
20171017
2 parents 5461fd3 + 0c8a9a5 commit a85475b

File tree

2 files changed

+185
-0
lines changed

2 files changed

+185
-0
lines changed

DataSampling.c

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*********************************************************************************/
2+
/*
3+
* Author : Jeong Hyun Gu
4+
* File name : DataSampling.c
5+
*/
6+
/*********************************************************************************/
7+
#include <stdlib.h>
8+
#include "DataSampling.h"
9+
/*********************************************************************************/
10+
#if(DATA_SAMPLING_REVISION_DATE != 20171017)
11+
#error wrong include file. (DataSampling.h)
12+
#endif
13+
/*********************************************************************************/
14+
/** Global variable **/
15+
16+
17+
/*********************************************************************************/
18+
/*
19+
1) 인수
20+
- Smp : tag_DataSampling 인스턴스의 주소.
21+
- Data : 버퍼를 채울 데이터.
22+
23+
2) 반환
24+
- 없음.
25+
26+
3) 설명
27+
- 인수로 전달받은 데이터로 버퍼를 채움.
28+
*/
29+
static void FillBuffer(tag_DataSampling *Smp, int Data)
30+
{
31+
int i;
32+
33+
Smp->Sum = Smp->Index = 0;
34+
for(i = 0; i < Smp->Level; i++)
35+
{
36+
Smp->Buf[i] = Data;
37+
Smp->Sum += Data;
38+
}
39+
}
40+
/*********************************************************************************/
41+
/*
42+
1) 인수
43+
- Smp : tag_DataSampling 인스턴스의 주소.
44+
- BufSize : 동적할당 받을 버퍼의 크기.
45+
46+
2) 반환
47+
- 0 : 초기화 실패.
48+
- 1 : 초기화 성공.
49+
50+
3) 설명
51+
- 'tag_DataSampling' 인스턴스의 필수 초기화 실행.
52+
- DataSampling 모듈을 사용하기 위해 선행적 실행 필요.
53+
*/
54+
char DataSamplingInitGeneral(tag_DataSampling *Smp, int BufSize)
55+
{
56+
if(Smp->Bit.InitGeneral == true)
57+
{
58+
return true;
59+
}
60+
61+
Smp->Buf = (int *) malloc(sizeof(int) * BufSize);
62+
63+
if(Smp->Buf != null)
64+
{
65+
Smp->Level = Smp->BufSize = BufSize;
66+
Smp->Sum = Smp->Index = 0;
67+
Smp->Bit.InitGeneral = true;
68+
}
69+
70+
return Smp->Bit.InitGeneral;
71+
}
72+
/*********************************************************************************/
73+
/*
74+
1) 인수
75+
- Smp : tag_DataSampling 인스턴스의 주소.
76+
- Level : 변경할 샘플링 수준.
77+
78+
2) 반환
79+
- 없음.
80+
81+
3) 설명
82+
- 샘플링 수준(Level) 변경.
83+
*/
84+
void DataSamplingChangeLevel(tag_DataSampling *Smp, int Level)
85+
{
86+
if((Smp->Bit.InitGeneral == false) || (Smp->Bit.InitFillBuffer == false))
87+
{
88+
return;
89+
}
90+
91+
if((Smp->Level != Level) && (1 <= Level) && (Level <= Smp->Level))
92+
{
93+
Smp->Level = Level;
94+
FillBuffer(Smp, Smp->Buf[0]);
95+
}
96+
}
97+
/*********************************************************************************/
98+
/*
99+
1) 인수
100+
- Smp : tag_DataSampling 인스턴스의 주소.
101+
- Data : 데이터.
102+
103+
2) 반환
104+
- Result : 샘플링된 데이터.
105+
106+
3) 설명
107+
- ring buffer 형식으로 데이터를 입력 받아 평균을 내어 샘플링.
108+
*/
109+
int DataSamplingGetData(tag_DataSampling *Smp, int Data)
110+
{
111+
int Result;
112+
113+
if(Smp->Bit.InitGeneral == false)
114+
{
115+
return 0;
116+
}
117+
118+
if(Smp->Bit.InitFillBuffer == false)
119+
{
120+
Smp->Bit.InitFillBuffer = true;
121+
FillBuffer(Smp, Data);
122+
}
123+
124+
Smp->Sum -= Smp->Buf[Smp->Index];
125+
Smp->Buf[Smp->Index] = Data;
126+
Smp->Sum += Smp->Buf[Smp->Index];
127+
Result = Smp->Sum / Smp->Level;
128+
129+
if(++(Smp->Index) >= Smp->Level) Smp->Index = 0;
130+
131+
return Result;
132+
}
133+
/*********************************************************************************/

DataSampling.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*********************************************************************************/
2+
/*
3+
* Author : Jeong Hyun Gu
4+
* File name : DataSampling.h
5+
*/
6+
/*********************************************************************************/
7+
#ifndef __DATA_SAMPLING_H__
8+
#define __DATA_SAMPLING_H__
9+
/*********************************************************************************/
10+
#define DATA_SAMPLING_REVISION_DATE 20171017
11+
/*********************************************************************************/
12+
/** REVISION HISTORY **/
13+
/*
14+
2017. 10. 17. - 초기버전.
15+
Jeong Hyun Gu
16+
*/
17+
/*********************************************************************************/
18+
/**Define**/
19+
20+
#define false 0
21+
#define true 1
22+
#define null 0
23+
24+
/*********************************************************************************/
25+
/**Enum**/
26+
27+
28+
/*********************************************************************************/
29+
/**Struct**/
30+
31+
typedef struct
32+
{
33+
struct
34+
{
35+
char InitGeneral : 1; //필수초기화
36+
char InitFillBuffer : 1; //버퍼초기화
37+
}Bit;
38+
39+
int *Buf;
40+
int Index, BufSize, Level;
41+
long Sum;
42+
}tag_DataSampling;
43+
44+
/*********************************************************************************/
45+
/**Function**/
46+
47+
char DataSamplingInitGeneral(tag_DataSampling *Smp, int BufSize);
48+
void DataSamplingChangeLevel(tag_DataSampling *Smp, int Level);
49+
int DataSamplingGetData(tag_DataSampling *Smp, int Data);
50+
51+
/*********************************************************************************/
52+
#endif //__DATA_SAMPLING_H__

0 commit comments

Comments
 (0)