-
Notifications
You must be signed in to change notification settings - Fork 0
/
keymain.c
331 lines (302 loc) · 7.52 KB
/
keymain.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <pthread.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <math.h>
#include "types_def.h"
#include "commonFunc.h"
#define Steel_Key_MAX 10
SteelKey_C SteelKey_config[Steel_Key_MAX];
char steelOff[Steel_Key_MAX];
char SteelKey[50];
char steelAD[50];
char steelLine[10];
char steelOff[10];
int f=0; //SteelKey标志位
int nSK=1; //SteelKey不存在标志位
int ADf=0; //AD标志位
int nAD=1; //AD不存在标志位
int nLine=1;//Line不存在标志位
int nend=1; //结束程序标志位
int AD_proc_flag=1; //AD值处理
int steelLine_flag=1;//SteelLine值处理
int sub_flag=1,add_flag=1;
int num,numValue,numOff,numOther,numOff_Other,add_steelOff,sub_steelOff;
float new_numOff;
int i,j,k; //第几个按键
pthread_mutex_t AD_mutex;
pthread_cond_t user_cond;
BOOL bl= 0;
int fd1; //key_user_fifo
int fd2;
char buf_read[80];
void pipe_open() //创建打开管道
{
unlink("./key_key_fifo");
int ret2 = mkfifo("./key_key_fifo",0777);
if(ret2<0)
{
perror("mkfifo key_key_fifo error\n");
}
fd1 = open("./user/key_user_fifo",O_RDONLY);
fd2 = open("./key_key_fifo",O_WRONLY);
if( (fd1 <0) || (fd2<0) )
{
perror("open file failed\n");
}
}
BYTE* pipe_read()
{
int ret1 = read(fd1,buf_read,sizeof(buf_read)); //从key_user_fifo中读取内容
if(ret1 <= 0)
{
printf("read end or error\n");
}
return buf_read;
}
void pipe_write(char *buf_write)
{
int ret2 = write(fd2,buf_write,sizeof(buf_write)); //将内容写入到key_key_fifo中
if(ret2 <= 0)
{
printf("write end or error\n");
}
}
void find_key_config(BOOL bNode,char *id,char *value,int *flag) //解析.ini文件 打印出用户输入的方向盘的按键的配置
{
if(bNode==FALSE) //键和值
{
if(strcmp(id,"name")==0)
{
if(strcmp(value,SteelKey)==0)
{
printf("The SteelKey you select is : %s\n",SteelKey);
pipe_write("find");
f=1;
nSK=0;
AD_proc_flag=1;
steelLine_flag=1;
}
}
}
if(f && flag)
{
printf("\n\n[%s]\n",id);
if( strcmp(id,"SteelID_1")==0 ) //获取当前在第几个按键
i=1;
if( strcmp(id,"SteelID_2")==0 )
i=2;
if( strcmp(id,"SteelID_3")==0 )
i=3;
if( strcmp(id,"SteelID_4")==0 )
i=4;
if( strcmp(id,"SteelID_5")==0 )
i=5;
if( strcmp(id,"SteelID_6")==0 )
i=6;
if( strcmp(id,"SteelID_7")==0 )
i=7;
if( strcmp(id,"SteelID_8")==0 )
i=8;
if( strcmp(id,"SteelID_9")==0 )
i=9;
if( strcmp(id,"SteelID_10")==0 )
i=10;
}
if(f && flag==0)
{
printf("%s=%s\n",id,value); //将按键配置赋值到方向盘配置结构体中
if( strcmp(id,"steelLine")==0 )
strcpy(SteelKey_config[i].steelLine,value);
else if( strcmp(id,"steelAD")==0 )
strcpy(SteelKey_config[i].steelAD,value);
else if( strcmp(id,"steelOff")==0 )
strcpy(SteelKey_config[i].steelOff,value);
else if( strcmp(id,"steelBus")==0 )
strcpy(SteelKey_config[i].steelBus,value);
else if( strcmp(id,"short")==0 )
strcpy(SteelKey_config[i].S_short,value);
else if( strcmp(id,"long")==0 )
strcpy(SteelKey_config[i].S_long,value);
else if( strcmp(id,"downAtt")==0 )
strcpy(SteelKey_config[i].downAtt,value);
else if( strcmp(id,"shortAtt")==0 )
strcpy(SteelKey_config[i].shortAtt,value);
else if( strcmp(id,"longAtt")==0 )
strcpy(SteelKey_config[i].longAtt,value);
else if( strcmp(id,"continueAtt")==0 )
strcpy(SteelKey_config[i].continueAtt,value);
else if( strcmp(id,"upAtt")==0 )
strcpy(SteelKey_config[i].upAtt,value);
else if( strcmp(id,"longStart")==0 )
strcpy(SteelKey_config[i].longStart,value);
else if( strcmp(id,"longSpace")==0 )
strcpy(SteelKey_config[i].longSpace,value);
}
if(bNode==TRUE) //[]的内容
{
if(strcmp(id,"End")==0)
{
f=0;
}
}
}
void find_key(char *S_short) //打印出用户选择的方向盘、AD值和其AD值代表的按键
{
printf("\nThe SteelKey you select is : %s\n",SteelKey);
printf("\nThe SteelLine you select is :%s\n",steelLine);
printf("The steelAD you select is : %d\n",num);
printf("The key you push is %s\n\n",S_short);
pipe_write("find"); //找到AD
}
void *AD_proc(void *arg)
{
while(1)
{
WaitSignedTimeOut(&AD_mutex,&user_cond,&bl,0);
num = stringToDec(steelAD,4);
for(j=1;j<11;j++)
{
if( strcmp(SteelKey_config[j].steelLine , steelLine)==0)
{
sub_steelOff = stringToDec(SteelKey_config[j].steelOff,4); //左范围值,取设定值
add_steelOff= stringToDec(SteelKey_config[j].steelOff,4); //右范围值,取设定值
for(k=1;k<11;k++)
{
if(strcmp(SteelKey_config[k].steelLine , steelLine)==0)
{
numValue = stringToDec(SteelKey_config[j].steelAD,4); //获取第j个按键的AD值(被比较)
numOther = stringToDec(SteelKey_config[k].steelAD,4); //获取第k个按键的AD值(做比较)
numOff = stringToDec(SteelKey_config[j].steelOff,4); //获取第j个按键的AD范围值
numOff_Other = stringToDec(SteelKey_config[k].steelOff,4); //获取第k个按键的AD范围值
if( (abs)(numValue-numOther) <= numOff+numOff_Other ) //计算范围值是否重合
{
new_numOff = (fabs)(numValue-numOther)/2+0.5;
if(numValue>numOther)
{
sub_steelOff = new_numOff-1; //左边范围值
sub_flag = 0;
}
else if(numValue<numOther)
{
add_steelOff = new_numOff; //右边范围值
add_flag = 0;
}
}
}
}
if( numValue-sub_steelOff <= num && num <= numValue+add_steelOff ) //将每一个按键的AD值与用户输入的AD值做比较
{
find_key(SteelKey_config[j].S_short);
nAD = 0;
}
}
}
if(nAD)
{
if(num==1019)
{
printf("All keys not push\n\n");
}
else
{
printf("This AD is false\n\n");
}
pipe_write("AD");
continue;
}
nAD=1;
}
}
void *user(void *arg) //子线程 用以获取用户的命令 判断
{
while(1)
{
strcpy(SteelKey,pipe_read());
if(strcmp(SteelKey,"back")==0) //关闭程序
{
nend=0;
pipe_write("close");
break;
}
readINIFile("./SteelKeyV2.ini",find_key_config); //解析.ini文件
if(nSK)
{
printf("This SteelKey is false\n\n");
pipe_write("SteelKey");
continue;
}
steelLine_flag = 1;
while(steelLine_flag)
{
strcpy(steelLine,pipe_read());
if(strcmp(steelLine,"back")==0) //返回上一级
{
steelLine_flag = 0;
pipe_write("BTSK");
continue;
}
for(j=1;j<11;j++)
{
if(strcmp(steelLine,SteelKey_config[j].steelLine)==0)
{
nLine = 0;
break;
}
}
if(nLine) //Line值不存在
{
pipe_write("Line");
printf("Thie SteelLine is fail\n\n");
continue;
}
AD_proc_flag = 1;
nLine = 1;
pipe_write("find"); //找到line
while(AD_proc_flag)
{
strcpy(steelAD,pipe_read());
if(strcmp(steelAD,"a")==0) //返回上一级
{
AD_proc_flag = 0;
pipe_write("back");
continue;
}
else if(strcmp(steelAD,"b")==0) //返回方向盘
{
steelLine_flag = 0;
AD_proc_flag = 0;
pipe_write("BTSK");
continue;
}
PostSignal(&AD_mutex,&user_cond,&bl);
}
}
nSK=1;
}
}
void main()
{
pipe_open();
pthread_mutex_init(&AD_mutex,NULL);
pthread_cond_init(&user_cond,NULL);
pthread_t tid,tid2;
int etid = pthread_create(&tid,NULL,user,NULL);
if(etid<0)
{
printf("user pthread_create fail\n");
}
pthread_create(&tid2,NULL,AD_proc,NULL);
if(etid<0)
{
printf("AD_proc pthread_create fail\n");
}
while(nend) ;
pthread_mutex_destroy(&AD_mutex);
pthread_cond_destroy(&user_cond);
}