forked from CamiAlfa/M365-BLE-PROTOCOL
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscooter_process.c
183 lines (160 loc) · 7.87 KB
/
scooter_process.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
// GNU, blablabla
// ub4raf
// HARDWARE DEPENDET!
// used w Tiva(Stellaris) ARM core and ROM_-based HAL.
void send_controls (uint8_t throttle_des, uint8_t brake_des, uint8_t question)
{
NinebotPack send_pack;
uint32_t length,counter;
uint8_t data[32] = { 0x06, 0x2A , 0x29 , 0x00 , 0x00 , 0x07 , 0x01 };
data[1]=throttle_des;
data[2]=brake_des;
if(question)
ninebot_create_pack(0x21,0x20,0x64,0,0x7,data,&send_pack);
else
ninebot_create_pack(0x21,0x20,0x65,0,0x5,data,&send_pack);
length=ninebot_serialyze(&send_pack,data);
counter=0;
while(counter<length)
{
ROM_UARTCharPut/*NonBlocking*/(UART5_BASE,data[counter]);
counter++;
}
}
inline void string_left(uint8_t *data, uint32_t *index, uint8_t shift)
{
uint32_t result_buf;
for(result_buf=0;
result_buf<*index-shift;
result_buf++)
data[result_buf]=data[result_buf+shift];
*index-=shift;
}
uint32_t NB_UART_Process (void)
{
uint32_t msg_counter=0;
uint32_t result_buf;
uint32_t counter;
#ifdef DEBUG_UART
result_buf=0;
UARTprintf("\nProcess buffer:");
while((result_buf)<dataUART_NB_index)
UARTprintf(" %0x",dataUART_NB_buffer[result_buf++]);
UARTprintf("\n");
#endif
while(dataUART_NB_index>8)
{
result_buf=ninebot_parse(dataUART_NB_buffer,dataUART_NB_index, &pack);
switch(result_buf)
{
case 0:
string_left(dataUART_NB_buffer,&dataUART_NB_index,(pack.len+9));
#ifdef DEBUG_UART
counter=0;
while((counter)<pack.len)
UARTprintf(" %0x",pack.data[counter++]);
UARTprintf("\n");
#endif
//5A A5 06 20 21 64 00 05 64 00 00 1B 00 D0 FE
// 1B=speed
if( (pack.direction==0x20)&&
(pack.RW==0x21)&&
//(pack.len==0x06)&&
(pack.command==0x64)&&
(pack.parameter==0x00)
//&&(pack.data[0]==0x05)
)
{
state= pack.data[0]; // charging, moove, stop etc
led_bat=pack.data[1]; // % of battery
lamp=pack.data[2];
beep=pack.data[3];
speed = pack.data[4];
new_2164=0xFF;
}
else
if( (pack.direction==0x21)&&
(pack.RW==0x20)&&
( ((pack.command==0x65)&&(pack.len==0x05)) || ((pack.command==0x64)&&(pack.len==0x07)) )&&
(pack.parameter==0x00)
//&&(pack.data[0]==0x04)
)
{
throttle = pack.data[1];
brakes = pack.data[2];
new_2065=0xFF;
}
else
if( (pack.direction==0x20)&&
(pack.RW==0x21)&&
(pack.command==0x01)//&&
//(pack.len==0x00)&&
//(pack.parameter==0x00)
)
{
// 5A A5 00 20 21 01 00 BD FF ++++++++++
// esc to head, read register 00
// 5A A5 0D 21 20 04 00 6A 6F 68 6E 73 63 6F 6F 74 65 72 00 00 FF FA
// head to esc, command 04!!! arg=00, "johnscooter", 00 00 hash
//answer here
UARTprintf(" johnscooter\n");
NinebotPack send_pack;
uint8_t length;
uint8_t data[32] = { 0x6A , 0x6F , 0x68 , 0x6E , 0x73 , 0x63 , 0x6F , 0x6F , 0x74 , 0x65 , 0x72 , 0x00 , 0x00 };
ninebot_create_pack(0x21,0x20,0x04,0x00,0x0D,data,&send_pack);
length=ninebot_serialyze(&send_pack,data);
counter=0;
while(counter<length)
{
ROM_UARTCharPut/*NonBlocking*/(UART5_BASE,data[counter]);
//TODO: NonBlocking tx
counter++;
}
}
else
//if( // (pack.direction==0x25)&&
// (pack.RW==0x20)//&&
// (pack.command==0x31)//&&
//( ((pack.command==0x65)&&(pack.len==0x05)) || ((pack.command==0x64)&&(pack.len==0x07)) )&&
// (pack.parameter==0x00)
//&&(pack.data[0]==0x04)
//)
{
//0x25 0x31
//#ifdef DEBUG_UART
UARTprintf("Unknown: %x from %x, %x:",pack.direction,pack.RW, pack.command);
result_buf=0;
while((result_buf)<pack.len)
UARTprintf(" %0x",pack.data[result_buf++]);
UARTprintf("\n");
//#endif
}
//else
{
};
msg_counter++;
//return 0;
break;
//dataUART_NB_index--;
//move 1 or 2 times
case 1://not enough data?
case 4://Incomplete message
//return ;
case 5:
goto endofloop;
case 2:
case 3:
case 6:
default:
string_left(dataUART_NB_buffer,&dataUART_NB_index,1);
}
#ifdef DEBUG_UART
result_buf=0;
while((result_buf)<dataUART_NB_index)
UARTprintf(" %0x",dataUART_NB_buffer[result_buf++]);
UARTprintf("\n");
#endif
}
endofloop:
return msg_counter;
}