-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataSampling.c
More file actions
211 lines (180 loc) · 5.17 KB
/
Copy pathDataSampling.c
File metadata and controls
211 lines (180 loc) · 5.17 KB
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
/*********************************************************************************/
/*
* Author : Jeong Hyun Gu
* File name : DataSampling.c
*/
/*********************************************************************************/
#include <stdlib.h>
#include "DataSampling.h"
/*********************************************************************************/
#if(DATA_SAMPLING_REVISION_DATE != 20230125)
#error wrong include file. (DataSampling.h)
#endif
/*********************************************************************************/
/*********************************************************************************/
/** Global variable **/
static tS8 *pS8;
static tS16 *pS16;
static tS32 *pS32;
static tS32 Ret;
/*********************************************************************************/
/*
@brief
- 버퍼에 데이터 삽입.
@param
- Smp : tag_DataSampling 인스턴스의 주소.
- Idx : 데이터 삽입 위치를 지시하는 index.
- Data : 삽입할 데이터.
@retval
- 없음.
*/
inline static void InToBuf(tag_DataSampling *Smp, tU8 Idx, tS32 Data)
{
switch(Smp->DataSize)
{
case sizeof(tS8) : pS8 = (tS8 *) Smp->Buf; pS8[Idx] = Data; break;
case sizeof(tS16) : pS16 = (tS16 *) Smp->Buf; pS16[Idx] = Data; break;
case sizeof(tS32) : pS32 = (tS32 *) Smp->Buf; pS32[Idx] = Data; break;
}
}
/*********************************************************************************/
/*
@brief
- 버퍼에서 특정 index의 데이터를 반환한다.
@param
- Smp : tag_DataSampling 인스턴스의 주소.
- Idx : 데이터 삽입 위치를 지시하는 index.
@retval
- 읽은 데이터.
*/
inline static tS32 OutFromBuf(tag_DataSampling *Smp, tU8 Idx)
{
switch(Smp->DataSize)
{
case sizeof(tS8) : pS8 = (tS8 *) Smp->Buf; Ret = pS8[Idx]; break;
case sizeof(tS16) : pS16 = (tS16 *) Smp->Buf; Ret = pS16[Idx]; break;
case sizeof(tS32) : pS32 = (tS32 *) Smp->Buf; Ret = pS32[Idx]; break;
}
return Ret;
}
/*********************************************************************************/
/*
@brief
- 인수로 전달받은 데이터로 버퍼를 채움.
@param
- Smp : tag_DataSampling 인스턴스의 주소.
- Data : 버퍼를 채울 데이터.
@retval
- 없음.
*/
static void FillBuffer(tag_DataSampling *Smp, tS32 Data)
{
tS16 i;
Smp->Sum = Smp->Index = 0;
for(i = 0; i < Smp->Level; i++)
{
InToBuf(Smp, i, Data);
Smp->Sum += Data;
}
}
/*********************************************************************************/
/*
@brief
- 'tag_DataSampling' 인스턴스의 필수 초기화 실행.
- DataSampling 모듈을 사용하기 위해 선행적 실행 필요.
@param
- Smp : tag_DataSampling 인스턴스의 주소.
- BufSize : 동적할당 받을 버퍼의 크기.
@retval
- 0 : 초기화 실패.
- 1 : 초기화 성공.
*/
tU8 DataSamplingInitGeneral(tag_DataSampling *Smp, tS16 BufSize, tS8 DataSize)
{
if(Smp->Bit.InitGeneral == true)
{
return true;
}
if((DataSize != sizeof(tS8)) && (DataSize != sizeof(tS16)) && (DataSize != sizeof(tS32)))
{
return false;
}
Smp->DataSize = DataSize;
Smp->Buf = malloc(Smp->DataSize * BufSize);
if(Smp->Buf != null)
{
Smp->Level = Smp->BufSize = BufSize;
Smp->Sum = Smp->Index = 0;
Smp->Bit.InitGeneral = true;
}
return Smp->Bit.InitGeneral;
}
/*********************************************************************************/
/*
@brief
- 샘플링 수준(Level) 변경.
@param
- Smp : tag_DataSampling 인스턴스의 주소.
- Level : 변경할 샘플링 수준.
@retval
- 없음.
*/
void DataSamplingChangeLevel(tag_DataSampling *Smp, tS16 Level)
{
if((Smp->Bit.InitGeneral == false) || (Smp->Bit.InitFillBuffer == false))
{
return;
}
if((Smp->Level != Level) && (1 <= Level) && (Level <= Smp->Level))
{
Smp->Level = Level;
FillBuffer(Smp, OutFromBuf(Smp, 0));
}
}
/*********************************************************************************/
/*
@brief
- ring buffer 형식으로 데이터를 입력 받아 평균을 내어 샘플링.
@param
- Smp : tag_DataSampling 인스턴스의 주소.
- Data : 데이터.
@retval
- Result : 샘플링된 데이터.
*/
tS32 DataSamplingGetData(tag_DataSampling *Smp, tS32 Data)
{
tS32 Result;
if(Smp->Bit.InitGeneral == false)
{
return 0;
}
if(Smp->Bit.InitFillBuffer == false)
{
Smp->Bit.InitFillBuffer = true;
FillBuffer(Smp, Data);
}
Smp->Sum -= OutFromBuf(Smp, Smp->Index);
InToBuf(Smp, Smp->Index, Data);
Smp->Sum += OutFromBuf(Smp, Smp->Index);
Result = Smp->Sum / Smp->Level;
if(++(Smp->Index) >= Smp->Level) Smp->Index = 0;
return Result;
}
/*********************************************************************************/
/*
@brief
- 버퍼 초기화 실행.
@param
- Smp : tag_DataSampling 인스턴스의 주소.
@retval
- 없음.
*/
void DataSamplingResetData(tag_DataSampling *Smp)
{
if((Smp->Bit.InitGeneral == false) || (Smp->Bit.InitFillBuffer == false))
{
return;
}
Smp->Bit.InitFillBuffer = false;
}
/*********************************************************************************/