forked from fdalvi/micro-bit-game-pad
-
Notifications
You must be signed in to change notification settings - Fork 1
/
JoystickService.cpp
79 lines (62 loc) · 1.92 KB
/
JoystickService.cpp
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
#include "JoystickService.h"
#include "mbed.h"
uint8_t joystick_report[] = { 0, 0, 0, 0, 0 };
JoystickService::JoystickService(BLE &_ble) :
HIDServiceBase(_ble,
JOYSTICK_REPORT_MAP, sizeof(JOYSTICK_REPORT_MAP),
inputReport = joystick_report,
outputReport = NULL,
featureReport = NULL,
inputReportLength = sizeof(joystick_report),
outputReportLength = 0,
featureReportLength = 0,
reportTickerDelay = 20),
buttonsState (0),
failedReports (0)
{
speed[0] = 0;
speed[1] = 0;
speed[2] = 0;
speed[3] = 0;
//startReportTicker();
}
int JoystickService::setSpeed(int8_t x, int8_t y, int8_t z)
{
// if (!connected)
// return ;
speed[0] = x;
speed[1] = y;
speed[2] = z;
joystick_report[0] = 0x00; // release button
joystick_report[1] = speed[0];
joystick_report[2] = speed[1];
joystick_report[3] = speed[2];
joystick_report[4] = speed[3];
if (send(joystick_report))
failedReports++;
return 0;
}
void JoystickService::pressButton(JoystickButton button) {
if (!connected)
return ;
joystick_report[0] = button; // press button
joystick_report[1] = speed[0];
joystick_report[2] = speed[1];
joystick_report[3] = speed[2];
joystick_report[4] = speed[3];
if (send(joystick_report))
failedReports++;
// wait for a little time to send button release report
// This fix: OpenEmu in game button not work
wait(0.050);
joystick_report[0] = 0x00; // release button
joystick_report[1] = speed[0];
joystick_report[2] = speed[1];
joystick_report[3] = speed[2];
joystick_report[4] = speed[3];
if (send(joystick_report))
failedReports++;
}
void JoystickService::sendCallback(void) {
// Empty not code
}