// 0xff 0x02 {x:00red, 01blue} {y:angle} 0xff
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/input.h> // struct input_event
#include <arpa/inet.h>
#include <stdlib.h>
#define PORT 8888
// #define IP "192.168.9.72"
#define IP "192.168.71.33"
#define PATH "/dev/input/event1"
#define SENSITIVITY 10
#define CONTINUANCE 0
#define ERR_MSG(msg) do{\
fprintf(stderr, "line:%d\t", __LINE__);\
perror(msg);\
}while(0)
// 限定红色机械臂的运动范围
char modify_red(char y);
// 限定蓝色机械臂的运动范围
char modify_blue(char y, int flag);
// 外挂
void cheet(int cfd, char operation[5]);
char msg1 = 0xff;
char msg2 = 0x02;
char x_red = 0x00;
char x_blue = 0x01;
char y_red = 0x00;
char y_blue = 0x00;
char msg5 = 0xff;
int main(int argc, char const *argv[])
{
// 1. socket
int cfd = socket(AF_INET, SOCK_STREAM, 0);
if(cfd < 0)
{
ERR_MSG("socket");
return -1;
}
printf("socket success with %d.\n", cfd);
// 2. connect
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(IP);
if(connect(cfd, (struct sockaddr *)&sin, sizeof(sin)) < 0)
{
ERR_MSG("connect");
return -1;
}
printf("connect success.\n");
// 3. open
int fd_op = open(PATH, O_RDONLY);
// init
lseek(fd_op, 0, SEEK_END);
struct input_event buf;// read w a s d >>> struct input_event
char operation[5] = {msg1, msg2, x_red, y_red, msg5};
write(cfd, &operation, sizeof(operation));
operation[2] = x_blue;
write(cfd, &operation, sizeof(operation));
// 4. op
ssize_t res = 0;
while(1)
{
// 4. read[block]
res = read(fd_op, &buf, sizeof(buf));
// printf("res >>> %ld\tbuf.code >>> %d\tbuf.value >>> %d\n", res, buf.value, buf.code);// test
if(sizeof(buf) == res)
{
if(EV_KEY == buf.type)
{
// printf("buf.code >>> %d\tbuf.value >>> %d\n", buf.value, buf.code);// test
if((KEY_A == buf.code || KEY_LEFT == buf.code) && CONTINUANCE >= buf.value)// a >> red -SENSITIVITY
{
y_red -= (char)SENSITIVITY;
y_red = (char)(modify_red(y_red)&0xff);
operation[2] = x_red;
operation[3] = y_red;
write(cfd, &operation, sizeof(operation));
printf("\rA >> red -%d >> %x\n", SENSITIVITY, (char)y_red&0xff);
}
else if((KEY_D == buf.code || KEY_RIGHT == buf.code) && CONTINUANCE >= buf.value)// d >> red +SENSITIVITY
{
y_red += (char)SENSITIVITY;
y_red = (char)(modify_red(y_red)&0xff);
operation[2] = x_red;
operation[3] = y_red;
write(cfd, &operation, sizeof(operation));
printf("\rD>> red +%d >> %x\n", SENSITIVITY, (char)y_red&0xff);
}
else if((KEY_W == buf.code || KEY_UP == buf.code) && CONTINUANCE >= buf.value)// w >> blue -SENSITIVITY
{
y_blue -= (char)SENSITIVITY;
y_blue = (char)(modify_blue(y_blue, 1)&0xff);
operation[2] = x_blue;
operation[3] = y_blue;
write(cfd, &operation, sizeof(operation));
printf("\rW >> blue -%d >> %x\n", SENSITIVITY, (char)y_blue&0xff);
}
else if((KEY_S == buf.code || KEY_DOWN == buf.code) && CONTINUANCE >= buf.value)// s >> blue +SENSITIVITY
{
y_blue += (char)SENSITIVITY;
y_blue = (char)(modify_blue(y_blue, 0)&0xff);
operation[2] = x_blue;
operation[3] = y_blue;
write(cfd, &operation, sizeof(operation));
printf("\rS >> blue +%d >> %x\n", SENSITIVITY, (char)y_blue&0xff);
}
else if(KEY_0 == buf.code && CONTINUANCE >= buf.value)
{
y_red = (char)0x00;
operation[2] = x_red;
operation[3] = y_red;
write(cfd, &operation, sizeof(operation));
y_blue = (char)0x00;
operation[2] = x_blue;
operation[3] = y_blue;
write(cfd, &operation, sizeof(operation));
printf("\rinit...\n");
}
else if(KEY_1 == buf.code && CONTINUANCE >= buf.value)
{
cheet(cfd, operation);
}
else if(KEY_ESC == buf.code && CONTINUANCE >= buf.value)
{
printf("\rclient exit...\n");
break;
}
}
}
}
// z. close
close(cfd);
// clean screen
getchar();
system("clear");
return 0;
}
// 限定红色机械臂的运动范围
char modify_red(char y)// -90 ~ 90
{
if((y&0xff) >= 0xa6 || (y&0xff) <= 0x5a)
{
return (char)(y&0xff);
}
else if((y&0xff) >= 0x5a && (y&0xff) <= 0x5a+SENSITIVITY)// 00[0]->5a[90]
{
return (char)0x5a;
}
else// a6[160]-ff[255]
{
return (char)0xa6;
}
}
// 限定蓝色机械臂的运动范围
char modify_blue(char y, int flag)
{
// printf("modifing... %x\n", y&0xff);// test
if((y&0xff) >= 0x00 && (y&0xff) <= 0xb4)
{
return (char)(y&0xff);
}
else if((y&0xff) <= 0x00 || ((y&0xff) >= 0x00-SENSITIVITY && flag))
{
return (char)0x00;
}
else
{
return (char)0xb4;
}
}
// 外挂
void cheet(int cfd, char operation[5])
{
printf("\rrunning...\n");
y_red = (char)0xa6;
operation[2] = x_red;
operation[3] = y_red;
write(cfd, operation, sizeof(char[5]));
y_blue = (char)0x00;
operation[2] = x_blue;
operation[3] = y_blue;
write(cfd, operation, sizeof(char[5]));
for(int i = 0; i < 180; i+=7, y_red = (char)(y_red+7)&0xff)
{
operation[2] = x_red;
operation[3] = y_red;
write(cfd, operation, sizeof(char[5]));
operation[2] = x_blue;
if(0 == y_red%2)
{
operation[3] = 0xff;
write(cfd, operation, sizeof(char[5]));
}
else
{
operation[3] = 0x00;
write(cfd, operation, sizeof(char[5]));
}
sleep(2);
}
operation[2] = x_red;
return ;
}
注意点:类型转换和按位与
待优化: 连续输入同一个值(.code变化)【略:sleep与线程】