User | Revision | Line number | New contents of line |
xx316 |
1:032b96b05e51
|
1
|
/* mbed Microcontroller Library
|
xx316 |
1:032b96b05e51
|
2
|
* Copyright (c) 2015 ARM Limited
|
xx316 |
1:032b96b05e51
|
3
|
*
|
xx316 |
1:032b96b05e51
|
4
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
xx316 |
1:032b96b05e51
|
5
|
* you may not use this file except in compliance with the License.
|
xx316 |
1:032b96b05e51
|
6
|
* You may obtain a copy of the License at
|
xx316 |
1:032b96b05e51
|
7
|
*
|
xx316 |
1:032b96b05e51
|
8
|
* http://www.apache.org/licenses/LICENSE-2.0
|
xx316 |
1:032b96b05e51
|
9
|
*
|
xx316 |
1:032b96b05e51
|
10
|
* Unless required by applicable law or agreed to in writing, software
|
xx316 |
1:032b96b05e51
|
11
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
xx316 |
1:032b96b05e51
|
12
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
xx316 |
1:032b96b05e51
|
13
|
* See the License for the specific language governing permissions and
|
xx316 |
1:032b96b05e51
|
14
|
* limitations under the License.
|
xx316 |
1:032b96b05e51
|
15
|
*/
|
xx316 |
1:032b96b05e51
|
16
|
|
xx316 |
1:032b96b05e51
|
17
|
#include "mbed.h"
|
xx316 |
1:032b96b05e51
|
18
|
|
xx316 |
1:032b96b05e51
|
19
|
#include "HIDServiceBase.h"
|
xx316 |
1:032b96b05e51
|
20
|
|
xx316 |
1:032b96b05e51
|
21
|
enum ButtonState
|
xx316 |
1:032b96b05e51
|
22
|
{
|
xx316 |
1:032b96b05e51
|
23
|
BUTTON_UP,
|
xx316 |
1:032b96b05e51
|
24
|
BUTTON_DOWN
|
xx316 |
1:032b96b05e51
|
25
|
};
|
xx316 |
1:032b96b05e51
|
26
|
|
xx316 |
1:032b96b05e51
|
27
|
enum JoystickButton
|
xx316 |
1:032b96b05e51
|
28
|
{
|
xx316 |
1:032b96b05e51
|
29
|
JOYSTICK_BUTTON_1 = 0x1,
|
xx316 |
1:032b96b05e51
|
30
|
JOYSTICK_BUTTON_2 = 0x2,
|
xx316 |
1:032b96b05e51
|
31
|
};
|
xx316 |
1:032b96b05e51
|
32
|
|
xx316 |
1:032b96b05e51
|
33
|
report_map_t JOYSTICK_REPORT_MAP = {
|
xx316 |
1:032b96b05e51
|
34
|
USAGE_PAGE(1), 0x01, // Generic Desktop
|
xx316 |
1:032b96b05e51
|
35
|
USAGE(1), 0x04, // Joystick
|
xx316 |
1:032b96b05e51
|
36
|
COLLECTION(1), 0x01, // Application
|
xx316 |
1:032b96b05e51
|
37
|
COLLECTION(1), 0x00, // Physical
|
xx316 |
1:032b96b05e51
|
38
|
USAGE_PAGE(1), 0x09, // Buttons
|
xx316 |
1:032b96b05e51
|
39
|
USAGE_MINIMUM(1), 0x01,
|
xx316 |
1:032b96b05e51
|
40
|
USAGE_MAXIMUM(1), 0x03,
|
xx316 |
1:032b96b05e51
|
41
|
LOGICAL_MINIMUM(1), 0x00,
|
xx316 |
1:032b96b05e51
|
42
|
LOGICAL_MAXIMUM(1), 0x01,
|
xx316 |
1:032b96b05e51
|
43
|
REPORT_COUNT(1), 0x03, // 2 bits (Buttons)
|
xx316 |
1:032b96b05e51
|
44
|
REPORT_SIZE(1), 0x01,
|
xx316 |
1:032b96b05e51
|
45
|
INPUT(1), 0x02, // Data, Variable, Absolute
|
xx316 |
1:032b96b05e51
|
46
|
REPORT_COUNT(1), 0x01, // 6 bits (Padding)
|
xx316 |
1:032b96b05e51
|
47
|
REPORT_SIZE(1), 0x05,
|
xx316 |
1:032b96b05e51
|
48
|
INPUT(1), 0x01, // Constant
|
xx316 |
1:032b96b05e51
|
49
|
USAGE_PAGE(1), 0x01, // Generic Desktop
|
xx316 |
1:032b96b05e51
|
50
|
USAGE(1), 0x30, // X
|
xx316 |
1:032b96b05e51
|
51
|
USAGE(1), 0x31, // Y
|
xx316 |
1:032b96b05e51
|
52
|
USAGE(1), 0x32, // Z
|
xx316 |
1:032b96b05e51
|
53
|
USAGE(1), 0x33, // Rx
|
xx316 |
1:032b96b05e51
|
54
|
LOGICAL_MINIMUM(1), 0x81, // -127
|
xx316 |
1:032b96b05e51
|
55
|
LOGICAL_MAXIMUM(1), 0x7f, // 127
|
xx316 |
1:032b96b05e51
|
56
|
REPORT_SIZE(1), 0x08, // Three bytes
|
xx316 |
1:032b96b05e51
|
57
|
REPORT_COUNT(1), 0x04,
|
xx316 |
1:032b96b05e51
|
58
|
INPUT(1), 0x02, // Data, Variable, Absolute (unlike mouse)
|
xx316 |
1:032b96b05e51
|
59
|
END_COLLECTION(0),
|
xx316 |
1:032b96b05e51
|
60
|
END_COLLECTION(0),
|
xx316 |
1:032b96b05e51
|
61
|
};
|
xx316 |
1:032b96b05e51
|
62
|
|
xx316 |
1:032b96b05e51
|
63
|
uint8_t report[] = { 0, 0, 0, 0, 0 };
|
xx316 |
1:032b96b05e51
|
64
|
|
xx316 |
1:032b96b05e51
|
65
|
class JoystickService: public HIDServiceBase
|
xx316 |
1:032b96b05e51
|
66
|
{
|
xx316 |
1:032b96b05e51
|
67
|
public:
|
xx316 |
1:032b96b05e51
|
68
|
JoystickService(BLE &_ble) :
|
xx316 |
1:032b96b05e51
|
69
|
HIDServiceBase(_ble,
|
xx316 |
1:032b96b05e51
|
70
|
JOYSTICK_REPORT_MAP, sizeof(JOYSTICK_REPORT_MAP),
|
xx316 |
1:032b96b05e51
|
71
|
inputReport = report,
|
xx316 |
1:032b96b05e51
|
72
|
outputReport = NULL,
|
xx316 |
1:032b96b05e51
|
73
|
featureReport = NULL,
|
xx316 |
1:032b96b05e51
|
74
|
inputReportLength = sizeof(inputReport),
|
xx316 |
1:032b96b05e51
|
75
|
outputReportLength = 0,
|
xx316 |
1:032b96b05e51
|
76
|
featureReportLength = 0,
|
xx316 |
1:032b96b05e51
|
77
|
reportTickerDelay = 20),
|
xx316 |
1:032b96b05e51
|
78
|
buttonsState (0),
|
xx316 |
1:032b96b05e51
|
79
|
failedReports (0)
|
xx316 |
1:032b96b05e51
|
80
|
{
|
xx316 |
1:032b96b05e51
|
81
|
speed[0] = 0;
|
xx316 |
1:032b96b05e51
|
82
|
speed[1] = 0;
|
xx316 |
1:032b96b05e51
|
83
|
speed[2] = 0;
|
xx316 |
1:032b96b05e51
|
84
|
speed[3] = 0;
|
xx316 |
1:032b96b05e51
|
85
|
|
xx316 |
1:032b96b05e51
|
86
|
startReportTicker();
|
xx316 |
1:032b96b05e51
|
87
|
}
|
xx316 |
1:032b96b05e51
|
88
|
|
xx316 |
1:032b96b05e51
|
89
|
int setSpeed(int8_t x, int8_t y, int8_t z)
|
xx316 |
1:032b96b05e51
|
90
|
{
|
xx316 |
1:032b96b05e51
|
91
|
speed[0] = x;
|
xx316 |
1:032b96b05e51
|
92
|
speed[1] = y;
|
xx316 |
1:032b96b05e51
|
93
|
speed[2] = z;
|
xx316 |
1:032b96b05e51
|
94
|
|
xx316 |
1:032b96b05e51
|
95
|
return 0;
|
xx316 |
1:032b96b05e51
|
96
|
}
|
xx316 |
1:032b96b05e51
|
97
|
|
xx316 |
1:032b96b05e51
|
98
|
int setButton(JoystickButton button, ButtonState state)
|
xx316 |
1:032b96b05e51
|
99
|
{
|
xx316 |
1:032b96b05e51
|
100
|
if (state == BUTTON_UP)
|
xx316 |
1:032b96b05e51
|
101
|
buttonsState &= ~(button);
|
xx316 |
1:032b96b05e51
|
102
|
else
|
xx316 |
1:032b96b05e51
|
103
|
buttonsState |= button;
|
xx316 |
1:032b96b05e51
|
104
|
|
xx316 |
1:032b96b05e51
|
105
|
return 0;
|
xx316 |
1:032b96b05e51
|
106
|
}
|
xx316 |
1:032b96b05e51
|
107
|
|
xx316 |
1:032b96b05e51
|
108
|
virtual void sendCallback(void) {
|
xx316 |
1:032b96b05e51
|
109
|
if (!connected)
|
xx316 |
1:032b96b05e51
|
110
|
return;
|
xx316 |
1:032b96b05e51
|
111
|
|
xx316 |
1:032b96b05e51
|
112
|
report[0] = buttonsState & 0x7;
|
xx316 |
1:032b96b05e51
|
113
|
report[1] = speed[0];
|
xx316 |
1:032b96b05e51
|
114
|
report[2] = speed[1];
|
xx316 |
1:032b96b05e51
|
115
|
report[3] = speed[2];
|
xx316 |
1:032b96b05e51
|
116
|
report[4] = speed[3];
|
xx316 |
1:032b96b05e51
|
117
|
|
xx316 |
1:032b96b05e51
|
118
|
if (send(report))
|
xx316 |
1:032b96b05e51
|
119
|
failedReports++;
|
xx316 |
1:032b96b05e51
|
120
|
}
|
xx316 |
1:032b96b05e51
|
121
|
|
xx316 |
1:032b96b05e51
|
122
|
protected:
|
xx316 |
1:032b96b05e51
|
123
|
uint8_t buttonsState;
|
xx316 |
1:032b96b05e51
|
124
|
uint8_t speed[4];
|
xx316 |
1:032b96b05e51
|
125
|
|
xx316 |
1:032b96b05e51
|
126
|
public:
|
xx316 |
1:032b96b05e51
|
127
|
uint32_t failedReports;
|
xx316 |
1:032b96b05e51
|
128
|
};
|
xx316 |
1:032b96b05e51
|
129
|
|