Kaleidoscope
Gamepad.h
Go to the documentation of this file.
1 /*
2 Copyright (c) 2014-2015 NicoHood
3 See the readme for credit to other people.
4 
5 Permission is hereby granted, free of charge, to any person obtaining a copy
6 of this software and associated documentation files (the "Software"), to deal
7 in the Software without restriction, including without limitation the rights
8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the Software is
10 furnished to do so, subject to the following conditions:
11 
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
14 
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 THE SOFTWARE.
22 */
23 
24 // Include guard
25 #pragma once
26 
27 #include <Arduino.h>
28 #include "PluggableUSB.h"
29 #include "HID.h"
30 #include "HID-Settings.h"
31 
32 // Dpad directions
33 #define GAMEPAD_DPAD_CENTERED 0
34 #define GAMEPAD_DPAD_UP 1
35 #define GAMEPAD_DPAD_UP_RIGHT 2
36 #define GAMEPAD_DPAD_RIGHT 3
37 #define GAMEPAD_DPAD_DOWN_RIGHT 4
38 #define GAMEPAD_DPAD_DOWN 5
39 #define GAMEPAD_DPAD_DOWN_LEFT 6
40 #define GAMEPAD_DPAD_LEFT 7
41 #define GAMEPAD_DPAD_UP_LEFT 8
42 
43 
44 typedef union {
45  // 32 Buttons, 6 Axis, 2 D-Pads
46  uint32_t buttons;
47 
48  struct {
49  uint8_t button1 : 1;
50  uint8_t button2 : 1;
51  uint8_t button3 : 1;
52  uint8_t button4 : 1;
53  uint8_t button5 : 1;
54  uint8_t button6 : 1;
55  uint8_t button7 : 1;
56  uint8_t button8 : 1;
57 
58  uint8_t button9 : 1;
59  uint8_t button10 : 1;
60  uint8_t button11 : 1;
61  uint8_t button12 : 1;
62  uint8_t button13 : 1;
63  uint8_t button14 : 1;
64  uint8_t button15 : 1;
65  uint8_t button16 : 1;
66 
67  uint8_t button17 : 1;
68  uint8_t button18 : 1;
69  uint8_t button19 : 1;
70  uint8_t button20 : 1;
71  uint8_t button21 : 1;
72  uint8_t button22 : 1;
73  uint8_t button23 : 1;
74  uint8_t button24 : 1;
75 
76  uint8_t button25 : 1;
77  uint8_t button26 : 1;
78  uint8_t button27 : 1;
79  uint8_t button28 : 1;
80  uint8_t button29 : 1;
81  uint8_t button30 : 1;
82  uint8_t button31 : 1;
83  uint8_t button32 : 1;
84 
85  int16_t xAxis;
86  int16_t yAxis;
87 
88  int16_t rxAxis;
89  int16_t ryAxis;
90 
91  int8_t zAxis;
92  int8_t rzAxis;
93 
94  uint8_t dPad1 : 4;
95  uint8_t dPad2 : 4;
96  };
98 
99 class Gamepad_ {
100  public:
101  Gamepad_(void);
102 
103  void begin(void);
104  void end(void);
105  void write(void);
106  void press(uint8_t b);
107  void release(uint8_t b);
108  void releaseAll(void);
109 
110  void buttons(uint32_t b);
111  void xAxis(int16_t a);
112  void yAxis(int16_t a);
113  void zAxis(int8_t a);
114  void rxAxis(int16_t a);
115  void ryAxis(int16_t a);
116  void rzAxis(int8_t a);
117  void dPad1(int8_t d);
118  void dPad2(int8_t d);
119 
120  void sendReport(void* data, int length);
121  protected:
123 };
124 extern Gamepad_ Gamepad;
125 
uint8_t button2
Definition: Gamepad.h:50
int8_t zAxis
Definition: Gamepad.h:91
uint8_t button13
Definition: Gamepad.h:62
HID_GamepadReport_Data_t _report
Definition: Gamepad.h:122
uint32_t buttons
Definition: Gamepad.h:46
uint8_t button15
Definition: Gamepad.h:64
void dPad1(int8_t d)
Definition: Gamepad.cpp:146
void yAxis(int16_t a)
Definition: Gamepad.cpp:121
uint8_t button20
Definition: Gamepad.h:70
uint8_t dPad2
Definition: Gamepad.h:95
uint8_t button32
Definition: Gamepad.h:83
uint8_t button4
Definition: Gamepad.h:52
uint8_t button17
Definition: Gamepad.h:67
void buttons(uint32_t b)
Definition: Gamepad.cpp:111
uint8_t button7
Definition: Gamepad.h:55
uint8_t button10
Definition: Gamepad.h:59
uint8_t button25
Definition: Gamepad.h:76
uint8_t button30
Definition: Gamepad.h:81
uint8_t button9
Definition: Gamepad.h:58
uint8_t button5
Definition: Gamepad.h:53
uint8_t button16
Definition: Gamepad.h:65
void zAxis(int8_t a)
Definition: Gamepad.cpp:126
uint8_t button27
Definition: Gamepad.h:78
void rxAxis(int16_t a)
Definition: Gamepad.cpp:131
void release(uint8_t b)
Definition: Gamepad.cpp:102
int8_t rzAxis
Definition: Gamepad.h:92
Gamepad_ Gamepad
Definition: Gamepad.cpp:160
void press(uint8_t b)
Definition: Gamepad.cpp:97
uint8_t button19
Definition: Gamepad.h:69
uint8_t button1
Definition: Gamepad.h:49
uint8_t button3
Definition: Gamepad.h:51
uint8_t button23
Definition: Gamepad.h:73
void ryAxis(int16_t a)
Definition: Gamepad.cpp:136
uint8_t button24
Definition: Gamepad.h:74
void write(void)
Definition: Gamepad.cpp:92
int16_t ryAxis
Definition: Gamepad.h:89
void xAxis(int16_t a)
Definition: Gamepad.cpp:116
uint8_t button8
Definition: Gamepad.h:56
int16_t rxAxis
Definition: Gamepad.h:88
void sendReport(void *data, int length)
Definition: Gamepad.cpp:156
uint8_t button11
Definition: Gamepad.h:60
uint8_t button14
Definition: Gamepad.h:63
void begin(void)
Definition: Gamepad.cpp:82
int16_t xAxis
Definition: Gamepad.h:85
uint8_t button18
Definition: Gamepad.h:68
Definition: Gamepad.h:99
void end(void)
Definition: Gamepad.cpp:87
uint8_t button21
Definition: Gamepad.h:71
uint8_t button31
Definition: Gamepad.h:82
void releaseAll(void)
Definition: Gamepad.cpp:107
uint8_t button28
Definition: Gamepad.h:79
Definition: Gamepad.h:44
Gamepad_(void)
Definition: Gamepad.cpp:76
uint8_t button12
Definition: Gamepad.h:61
uint8_t button6
Definition: Gamepad.h:54
uint8_t button22
Definition: Gamepad.h:72
uint8_t button29
Definition: Gamepad.h:80
uint8_t button26
Definition: Gamepad.h:77
uint8_t dPad1
Definition: Gamepad.h:94
int16_t yAxis
Definition: Gamepad.h:86
void dPad2(int8_t d)
Definition: Gamepad.cpp:151
void rzAxis(int8_t a)
Definition: Gamepad.cpp:141