-
Notifications
You must be signed in to change notification settings - Fork 1
/
app_ini.h
336 lines (263 loc) · 14.7 KB
/
app_ini.h
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
332
333
334
335
336
#ifndef _APPINI_H
#define _APPINI_H
#ifdef EnableSwSerial
#define receivePin 14
#define transmitPin 12
#include <SoftwareSerial.h>
SoftwareSerial swSerial(receivePin, transmitPin, false, 64);
#endif
#include <Wire.h>
#define SlaveDeviceId 9
#include "ffb.h"
#define SDA_pin 13 //A4 white
#define SCL_pin 4 //A5 RED A2B91E
#define dataRecieveLength 22
#define I2Cinterval 70
uint32_t lastI2Crecieved =0,CalibrationTime=0;
String Arduino_get_Data,Last_get_Data;
byte Command2Arduino[]={'D','A'}; //(x engine, y engine)
//CG,CB,CW, DA(Raw data),RR( report calibrate result)
byte WheelButtonLeft,WheelButtonRight,GasAxis,GasButton,BreakAxis,BreakBtton,EnableAxisBotton;
byte JostickAxis[]={0,0,0,0};
void SerialEvent(void);
void ServerINI(void);
void Get_Data_from_Arduino(void);
/*************************************************************************************************/
#define BLresetPin 2
String RN42="",ArduniDataFor="Joy";
bool SENDCODE=false;
bool HareReset=false;
bool RecieveMode=true,InitializedCalibration=false,SendCalibrationData=false,SaveCalibrationDO=false;
char CalibrationCode;
/*
rows ][ columns
currentData LastData max mean min //5
gas
break
wheel
rotate x
*/
uint16_t ArduinoData[4][5];
static byte NumberOfselectingData=0, button;
uint16_t MeanWheal,MeanRotationX,SMeanWheal=0,SMeanRotationX=0;
/*************************************************************************************************/
void Joystick(int8_t BP_X1,int8_t BP_Y1,int8_t BP_X2,int8_t BP_Y2,byte BP_ST_BTN, byte BP_ND_BTN){
Serial.write((byte)0xFD); //Start HID Report
Serial.write((byte)0x6); //Length byte
Serial.write(BP_X1 & 0xFF); //First X coordinate
Serial.write(BP_Y1 & 0xFF); //First Y coordinate
Serial.write(BP_X2 & 0xFF); //Second X coordinate
Serial.write(BP_Y2 & 0xFF); //Second Y coordinate
Serial.write((byte)BP_ST_BTN); //First Button byte
Serial.write((byte)BP_ND_BTN);
}
void gameJoyMove(int8_t BP_X1,int8_t BP_Y1,int8_t BP_X2,int8_t BP_Y2){
Serial.write((byte)0xFD); //Start HID Report
Serial.write((byte)0x6); //Length byte
Serial.write(BP_X1 & 0xFF); //First X coordinate
Serial.write(BP_Y1 & 0xFF); //First Y coordinate
Serial.write(BP_X2 & 0xFF); //Second X coordinate
Serial.write(BP_Y2 & 0xFF); //Second Y coordinate
Serial.write((byte)0x00); //First Button byte
Serial.write((byte)0x00); //Second Button byte
}
void gameJoyPress(byte BP_ST_BTN, byte BP_ND_BTN){
Serial.write((byte)0xFD); //Start HID Report
Serial.write((byte)0x6); //Length byte
Serial.write((byte)0x00);
Serial.write((byte)0x00);
Serial.write((byte)0x00);
Serial.write((byte)0x00);
Serial.write((byte)BP_ST_BTN); //First Button byte
Serial.write((byte)BP_ND_BTN); //Serial1.write((byte)BP_ND_BTN);//Second Button byte
}
void keyboardPress(byte BP_KEY,byte BP_MOD){
Serial.write((byte)0xFD); //Start HID Report
Serial.write((byte)0x9); //Length byte
Serial.write((byte)0x1); //Descriptor byte
Serial.write(BP_MOD); //Modifier byte
Serial.write((byte)0x00); //-
Serial.write(BP_KEY); //Send KEY
for(byte i = 0;i<5;i++){ //Send five zero bytes
Serial.write((byte)0x00);
}
}
void keyboardReleaseAll() {keyboardPress((byte)0x00,(byte)0x00); }
byte HEX2BYTE(char x0,char x1){
byte a=0;
if(x0 < '9') a=16*(x0 - '0');
else a=16*(x0 - '7');
if(x1 < '9') a+=(x1 - '0');
else a+=(x1 - '7');
// byte a= (16 * ((x0 < '9') ? x0 - '0' : x0 - '7')) + (x1 < '9') ? x1 - '0' : x1 - '7';
return a;
}
void setup_ini(void);
void app_loop(void);
String RestorCalibration(void);
void SaveCalibration(char code);
#include "main.h"
void SaveCalibration(char code){
byte row;
switch (code){
case 'G': row=0; break;
case 'B': row=1; break;
case 'W': row=2; break;
case 'X': row=3; break;}
EEPROM.write(Calibration_p+row*6 ,(byte)(ArduinoData[row][2]/255)); EEPROM.write(Calibration_p+row*6+1,(byte)(ArduinoData[row][2]%255)); //max
EEPROM.write(Calibration_p+row*6+2,(byte)(ArduinoData[row][3]/255)); EEPROM.write(Calibration_p+row*6+3,(byte)(ArduinoData[row][3]%255)); //mean
EEPROM.write(Calibration_p+row*6+4,(byte)(ArduinoData[row][4]/255)); EEPROM.write(Calibration_p+row*6+5,(byte)(ArduinoData[row][4]%255)); //max
PRINT("SaveCalibration "); PRINT(row); PRINT(EEPROM.commit()?" Done\n":" Not\n");
/*
PRINT(EEPROM.read(Calibration_p+row*6 )*255+EEPROM.read(Calibration_p+row*6+1)); PRINT(",");PRINT(ArduinoData[row][2]);PRINT(" MAX:"); PRINT(ArduinoData[row][2]/255);PRINTLN(ArduinoData[row][2]%255);
PRINT(EEPROM.read(Calibration_p+row*6 +2 )*255+EEPROM.read(Calibration_p+row*6+3)); PRINT(",");PRINT(ArduinoData[row][3]);PRINT(" MEAN:");PRINT(ArduinoData[row][3]/255);PRINTLN(ArduinoData[row][3]%255);
PRINT(EEPROM.read(Calibration_p+row*6 +4 )*255+EEPROM.read(Calibration_p+row*6+5)); PRINT(",");PRINT(ArduinoData[row][4]);PRINT(" MIN:"); PRINT(ArduinoData[row][4]/255);PRINTLN(ArduinoData[row][4]%255);
*/}
String RestorCalibration(void){
byte row;
String s="";
for(row=0;row<4;row++) {
ArduinoData[row][2]=EEPROM.read(Calibration_p+row*6 )*255+EEPROM.read(Calibration_p+row*6+1); s+="M"+String(ArduinoData[row][2]);//max
ArduinoData[row][3]=EEPROM.read(Calibration_p+row*6 + 2)*255+EEPROM.read(Calibration_p+row*6+3); s+="D"+String(ArduinoData[row][3]);//mean
ArduinoData[row][4]=EEPROM.read(Calibration_p+row*6 + 4)*255+EEPROM.read(Calibration_p+row*6+5); s+="m"+String(ArduinoData[row][4]);//min
}
WheelButtonLeft =EEPROM.read(WheelButtonLeft_p);//143
WheelButtonRight=EEPROM.read(WheelButtonRight_p);//47
EnableAxisBotton=EEPROM.read(EnableAxisBotton_p);
GasButton=EEPROM.read(GasButton_p);
BreakBtton=EEPROM.read(BreakBtton_p);
GasAxis=EEPROM.read(GasAxis_p);//47
BreakAxis=EEPROM.read(BreakAxis_p);//47
PRINTLN("Restor Calibration"); PRINTLN(s);
return s;
}
void sendCommand2RN42(String _Data){
bool change=false;
int8_t gas,Break,Lgas,LBreak;
byte row;
if(_Data[4]=='[' && _Data[21]==']')//S011[G424B064W390R410];if it valid
{
ArduinoData[0][0] =(_Data[6]-'0' )*100+(_Data[7]-'0' )*10+(_Data[8]-'0' );//Gas
ArduinoData[1][0] =(_Data[10]-'0')*100+(_Data[11]-'0')*10+(_Data[12]-'0');//Break
ArduinoData[2][0] =(_Data[14]-'0')*100+(_Data[15]-'0')*10+(_Data[16]-'0');//Wheel
ArduinoData[3][0] =(_Data[18]-'0')*100+(_Data[19]-'0')*10+(_Data[20]-'0');//RotateX
if(ArduniDataFor[0]=='C'){//calibration for find min and max value CG
switch (ArduniDataFor[1]){
case 'G': row=0; break;
case 'B': row=1; break;
case 'W': row=2; break;
case 'X': row=3; break;}
if(InitializedCalibration)
{
if(ArduinoData[row][0]>ArduinoData[row][2]) ArduinoData[row][2]=ArduinoData[row][0];
if(ArduinoData[row][0]<ArduinoData[row][4]) ArduinoData[row][4]=ArduinoData[row][0];
PRINT("M");PRINT(ArduinoData[row][2]);PRINT("m");PRINTLN(ArduinoData[row][4]);
}
else
{
PRINT("InitializedCalibration ");PRINTLN(row);
ArduinoData[row][4]=65000,ArduinoData[row][2]=0;
InitializedCalibration=true;
}
}
else if(ArduniDataFor[0]=='M' ){// set mean value for wheel and rotation X MX,MW
switch (ArduniDataFor[1]){
case 'W': row=2; break;
case 'X': row=3; break;}
ArduinoData[row][3]=ArduinoData[row][0];
PRINTLN("set mean value");
}
else if(ArduniDataFor[0]=='J' ){
if(_Data[1]!=Last_get_Data[1] || _Data[2]!=Last_get_Data[2] ||_Data[3]!=Last_get_Data[3] ){ change=true;
if(_Data[1]=='0') button=0;
else if(_Data[1]=='1') button=WheelButtonLeft;/// LEFTROTATE 1
else if(_Data[1]=='2') button=WheelButtonRight; /// LEFTROTATE 1
}
// current value Max Min
gas=map(ArduinoData[0][0], ArduinoData[0][2], ArduinoData[0][4], -127, 127);
Lgas=map(ArduinoData[0][1], ArduinoData[0][2], ArduinoData[0][4], -127, 127);
Break=map(ArduinoData[1][0], ArduinoData[1][2], ArduinoData[1][4], 127, -127);
LBreak=map(ArduinoData[1][1], ArduinoData[1][2], ArduinoData[1][4], 127, -127);
if( (gas-Lgas)>10 || (gas-Lgas)<-10 || (Break-LBreak)>10 || (Break-LBreak)<-10) change=true;
if(change){
// S011[G420B058W360R432];Direction=0 Gas=110 Break=-47
// char databuffer[18];
// sprintf(databuffer,"S%c%02X%02X%c%c",_Data[1],gas,Break,_Data[6],_Data[7]);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// LHJ,LVJ,RHJ,RVJ,
// JostickAxis[]={0,0,0,0};
if(EnableAxisBotton) {
if(gas<-70) button+=GasButton;
else if(Break<-70) button+=BreakBtton;
Joystick(0,0,0,0,button,0);
}
else {
memset(JostickAxis, 0, sizeof(JostickAxis));
JostickAxis[GasAxis]=gas;
JostickAxis[BreakAxis]=Break;
Joystick(JostickAxis[0],JostickAxis[1],JostickAxis[2],JostickAxis[3],button,0);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Serial1.print(_Data); Serial1.print(F(";Direction="));Serial1.print(_Data[1]);
Serial1.print(F(" Gas="));Serial1.print(gas); Serial1.print(F("("));Serial1.print(ArduinoData[0][2]); Serial1.print(F(","));Serial1.print(ArduinoData[0][4]);Serial1.print(F(")"));
Serial1.print(F(" Break="));Serial1.print(Break);Serial1.print(F("("));Serial1.print(ArduinoData[1][2]); Serial1.print(F(","));Serial1.print(ArduinoData[1][4]);Serial1.print(F(")"));
Serial1.println();
WS.text(MainWSclinet,"ARD@"+_Data);
}
}
Last_get_Data=_Data;
ArduinoData[0][1] =ArduinoData[0][0];//Gas
ArduinoData[1][1] =ArduinoData[1][0];//Break
ArduinoData[2][1] =ArduinoData[2][0];//Wheel
ArduinoData[3][1] =ArduinoData[3][0];//RotateX
}
}
void Get_Data_from_Game_USB(){
#ifdef EnableSwSerial
inputSwString
byte data[inputSwString.length()];
inputSwString.getBytes(data, inputSwString.length());
FfbOnUsbData( data, inputSwString.length())
#endif
}
void Get_Data_from_Arduino()
{
Wire.beginTransmission(SlaveDeviceId);
Wire.write(Command2Arduino,sizeof(Command2Arduino));
Wire.endTransmission(); // Moved up here by frippe75
delay(5);
Wire.requestFrom(SlaveDeviceId, dataRecieveLength); //address, quantity , sendStop=true
Arduino_get_Data="";
while (Wire.available())
{
delay(1);
char c = (char)Wire.read(); // Serial1.write(c);
Arduino_get_Data += c;
}
Arduino_get_Data.trim();
sendCommand2RN42(Arduino_get_Data);//S011[G420B058W360R432];Direction=0 Gas=110 Break=-47
/*
if (Arduino_get_Data.length()>4) {
byte eqI1 = Arduino_get_Data.indexOf(']');
if(eqI1<dataRecieveLength){ Arduino_get_Data = Arduino_get_Data.substring(0, eqI1); sendCommand2RN42(Arduino_get_Data);}
}*/
// PRINTLN(Arduino_get_Data);
}
void setup_ini(void){
#ifdef EnableSwSerial
swSerial.begin(115200);
#endif
Wire.begin(SDA_pin,SCL_pin);
}
void app_loop(void){
SerialEvent();
#ifdef EnableSwSerial
swSerialEvent()
#endif
if(SENDCODE) {Serial.print(RN42);SENDCODE=false;}
if(HareReset){digitalWrite(0,LOW); delay(400); digitalWrite(0,HIGH); HareReset=false;}
if((millis()-lastI2Crecieved)>(I2Cinterval) && ArduniDataFor[0]!='I' ){ Get_Data_from_Arduino(); lastI2Crecieved=millis(); }
if(SendCalibrationData) { WS.text(MainWSclinet,"RCA@"+RestorCalibration()); SendCalibrationData=false;}
if(SaveCalibrationDO) { SaveCalibration(CalibrationCode); WS.text(MainWSclinet,"CAS@"+String(CalibrationCode)); SaveCalibrationDO=false;}
}
#endif