-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_of_driver.c
289 lines (264 loc) · 6.7 KB
/
test_of_driver.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
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#define DATA_SIZE 65535
#define TRIGGER_LEVEL 13
#define ZERO_TRIGGER_LEVEL 6
#define BOOSTED_TRIGGER_LEVEL 20
#define JUDGE_LEVEL 30
int __judge(float ap1, float ap2, float ap3){
if ((ap1 < ap3)&&(ap3 < ap2)){
return 1;
}
if ((ap2 < ap3)&&(ap3 < ap1)){
return 2;
}
if ((ap3 >= ap1)&&(ap1 > ap2)){
return 2;
}
if ((ap3 >= ap2)&&(ap2 > ap1)){
return 1;
}
if ((ap3 <= ap1)&&(ap1 < ap2)){
return 1;
}
if ((ap3 <= ap2)&&(ap2 < ap1)){
return 2;
}
return (int)ap3%2+1;
}
int _judge(float ap1, float ap2, float ap3){
if ((ap1 < ap3)&&(ap3 < ap2)){
return 1;
}
if ((ap2 < ap3)&&(ap3 < ap1)){
return 2;
}
// if ((ap3 >= ap1)&&(ap1 > ap2)){
// return 2;
// }
// if ((ap3 >= ap2)&&(ap2 > ap1)){
// return 1;
// }
// if ((ap3 <= ap1)&&(ap1 < ap2)){
// return 1;
// }
// if ((ap3 <= ap2)&&(ap2 < ap1)){
// return 2;
// }
return 0;
}
void clear(int data[DATA_SIZE]){
for (int i = 0; i < 25; i++){
data[i] = -1;
}
}
int judge(int data[DATA_SIZE], int count, int opt){
float ap1 = 0, ap2 = 0, ap3 = 0;
int c0 = 0, c1 = 0, c2 = 0, c3 = 0;
if (data[0] == 1){
return 1;
}
if (data[0] == 2){
return 2;
}
for (int i = 0; i < count; i++){
switch (data[i]){
case 0:
c0++;
break;
case 1:
c1++;
ap1 += i;
break;
case 2:
c2++;
ap2 += i;
break;
case 3:
c3++;
ap3 += i;
break;
default:
break;
}
}
if (c0 > count / 2){
return 0;
}
ap1 /= (float)c1;
ap2 /= (float)c2;
ap3 /= (float)c3;
if (opt){
return _judge(ap1,ap2,ap3);
}else{
return __judge(ap1,ap2,ap3);
}
}
int main(void){
char buf[10];
int data[DATA_SIZE];
int before = 0;
int boost = 0;
int interval = 0;
int dominate = 0, dominate_flag = 0;
int wait = 0;
int count = 0;
int trigger, zero_trigger;
int fd;
if ((fd = open("/dev/test_device0", O_RDONLY))<0) perror("open");
printf("\n");
printf("Initializing in progress...\n");
printf("\n");
printf("It will take several seconds.\n");
printf("\n");
printf("Please make sure there are no moving objects in front of the sensor.\n");
printf("\n");
printf("If there are objects, the sensor may give a wrong feedback\n");
printf("\n");
//No init any more!
printf("\n");
printf("Initialization Complete!\n");
while (1){
if (count >= DATA_SIZE){
printf("data overflow\n");
//Overflow dealing
} else if (count >= JUDGE_LEVEL){
dominate = judge(data, count, 1);
if (dominate == 1){
printf("enter\n");
wait = 1;
boost = 0;
clear(data);
count = 0;
interval = 0;
}else if (dominate == 2){
printf("leave\n");
wait = 1;
boost = 0;
clear(data);
count = 0;
interval = 0;
}else{
}
}
//***SLEEP*** (Significant because if no sleep the system will hang up)
if (boost){
usleep(50000);
}else {
usleep(100000);
}
if (read(fd, buf,4)<0) perror("read");
printf("%s",buf);
if (buf[0]!='e') {
interval = 0;
if (wait) {
data[count] = buf[0] - '0';
before = data[count];
if (buf[0] == '0'){
zero_trigger = 1;
}
count++;
continue;
}
if (trigger){
boost = 1;
trigger = 0;
dominate_flag = 1;
clear(data);
count = 0;
}
switch (buf[0])
{
case '0':
if (dominate_flag){
dominate = -1;
dominate_flag = 0;
}
data[count] = 0;
before = data[count];
count++;
zero_trigger = 1;
break;
case '1':
if (dominate_flag){
dominate = 1;
dominate_flag = 0;
}
data[count] = 1;
before = data[count];
count++;
zero_trigger = 0;
break;
case '2':
if (dominate_flag){
dominate = 2;
dominate_flag = 0;
}
data[count] = 2;
before = data[count];
count++;
zero_trigger = 0;
break;
case '3':
if (dominate_flag){
dominate = -1;
dominate_flag = 0;
}
data[count] = 3;
before = data[count];
count++;
zero_trigger = 0;
break;
default:
break;
}
}else{
interval++;
printf("-");
if (zero_trigger){
if (interval > ZERO_TRIGGER_LEVEL){
trigger = 1;
wait = 0;
zero_trigger = 0;
}
}else{
if (interval > TRIGGER_LEVEL){
trigger = 1;
wait = 0;
}
}
if (boost){
if (interval > BOOSTED_TRIGGER_LEVEL){
if (!wait){
dominate = judge(data, count, 0);
if (dominate == 1){
printf("enter\n");
trigger = 1;
boost = 0;
clear(data);
count = 0;
interval = 0;
}else if (dominate == 2){
printf("leave\n");
trigger = 1;
boost = 0;
clear(data);
count = 0;
interval = 0;
}else{
data[count] = before;
count++;
}
}
}else{
data[count] = before;
count++;
}
}
}
}
if (close(fd) !=0) perror("close");
}