-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameServer.ino
137 lines (119 loc) · 3.84 KB
/
GameServer.ino
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
#define GAME_HOST "manunited.ringggo.981park.net"
// #define GAME_HOST "192.168.41.254"
#define GAME_PORT 9998
#define STOP 0
#define START 1
#define SUSPEND 2
#define RESUME 3
WiFiClient gameClient;
void GameServerInit()
{
log_i("Init GameServer");
// gameClient.setNoDelay(true);
if (!gameClient.connect(GAME_HOST, GAME_PORT))
{
log_i("connection failed");
return;
}
log_i("connected GameServer");
gameClient.setNoDelay(true);
}
void GameServerTask(void* parameter)
{
BaseType_t xStatus;
uint8_t packetHeader[8] = { 0, };
uint8_t packetBody[8] = { 0, };
GameServerInit();
for (;;)
{
// Serial.println("GameServer Task");
while (gameClient.available())
{
gameClient.readBytes(packetHeader, 8);
log_i("%x %x %x %x %x %x %x %x ", packetHeader[0], packetHeader[1], packetHeader[2], packetHeader[3], packetHeader[4], packetHeader[5], packetHeader[6], packetHeader[7]);
if (packetHeader[0] == PK_WHO_ARE_YOU_ANS) {
log_i("whoyouare received");
Protocol_t protocol = {PK_IAM_ANS, CAR, 8, CAR_ID, 0, };
gameClient.write((const uint8_t *)&protocol, sizeof(protocol));
log_i("send answer");
} else if (packetHeader[0] == PK_CHECK_CONNECTION_REQ) {
log_i("check received");
Protocol_t protocol = {PK_CHECK_CONNECTION_ANS, CAR, 8, CAR_ID, 0, };
gameClient.write((const uint8_t *)&protocol, sizeof(protocol));
log_i("send answer");
} else if (packetHeader[0] == PK_CARLED_NOTI) {
gameClient.readBytes(packetBody, 7);
log_i("car led received: %d, %d, %d, %d, %d, %d, %d", packetBody[0], packetBody[1], packetBody[2], packetBody[3], packetBody[4], packetBody[5], packetBody[6]);
xStatus = xQueueSendToFront(xQueueLed, packetBody, 100);
if(xStatus == pdPASS)
{
log_i("led queue send");
}
} else if (packetHeader[0] == PK_CARSOUND_NOTI) {
gameClient.readBytes(packetBody, 2);
log_i("car sound received: %d, %d", packetBody[0], packetBody[1]);
xStatus = xQueueSendToFront(xQueueMp3, packetBody, 100);
if(xStatus == pdPASS)
{
log_i("mp3 queue send");
}
} else if (packetHeader[0] == PK_CARACTIVEMODE_NOTI) {
gameClient.readBytes(packetBody, 1);
log_i("car start/stop received: %d\n", packetBody[0]);
xStatus = xQueueSendToFront(xQueueSubBoard, packetBody, 100);
if(xStatus == pdPASS)
{
log_i("subboard queue send");
}
} else {
log_i("invalid req code");
}
}
vTaskDelay(1000);
}
vTaskDelete(NULL);
}
void SendBattery(uint8_t percentage)
{
Protocol_bt protocol = {PK_BATTERY_NOTI, CAR, 8, CAR_ID, 0, percentage};
gameClient.write((const uint8_t *)&protocol, sizeof(protocol));
log_i("send battery");
}
void SendNfc(char wrbId[], uint8_t uid[])
{
Protocol_nfct protocol = {PK_NFC_NOTI, CAR, 8, CAR_ID, 0,};
memcpy(protocol.wrbId, wrbId, 13);
protocol.sep = ',';
memcpy(protocol.uid, uid, 7);
gameClient.write((const uint8_t *)&protocol, sizeof(protocol));
log_i("send nfc");
}
void SendBumper(int bumper)
{
Protocol_bumpert protocol = {PK_BUMP_NOTI, CAR, 8, CAR_ID, 0, (uint8_t)bumper};
gameClient.write((const uint8_t *)&protocol, sizeof(protocol));
log_i("send bumper");
}
void GameServerSendTask(void* parameter)
{
BaseType_t xStatus;
uint8_t data = 0;
for (;;)
{
// Serial.println("GameServer Send Task");
xStatus = xQueueReceive(xQueueBumper, &data, 100);
if(xStatus == pdPASS)
{
SendBumper(data);
log_i("receive queue: 0x%x", data);
}
xStatus = xQueueReceive(xQueueBattery, &data, 100);
if(xStatus == pdPASS)
{
SendBattery(data);
log_i("receive queue: %d%", data);
}
vTaskDelay(100);
}
vTaskDelete(NULL);
}