PSYQo
simplepad.hh
1 /*
2 
3 MIT License
4 
5 Copyright (c) 2022 PCSX-Redux authors
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 SOFTWARE.
24 
25 */
26 
27 #pragma once
28 
29 #include <EASTL/functional.h>
30 #include <stdint.h>
31 
32 namespace psyqo {
33 
45 class SimplePad {
46  public:
47  enum Pad { Pad1, Pad2 };
48 
49  enum Button {
50  Select = 0,
51  L3 = 1,
52  R3 = 2,
53  Start = 3,
54  Up = 4,
55  Right = 5,
56  Down = 6,
57  Left = 7,
58  L2 = 8,
59  R2 = 9,
60  L1 = 10,
61  R1 = 11,
62  Triangle = 12,
63  Circle = 13,
64  Cross = 14,
65  Square = 15,
66  };
67 
68  struct Event {
69  enum { PadConnected, PadDisconnected, ButtonPressed, ButtonReleased } type;
70  Pad pad;
71  Button button;
72  };
73 
82  void initialize();
83 
95  void setOnEvent(eastl::function<void(Event)>&& callback) { m_callback = eastl::move(callback); }
96 
106  bool isPadConnected(Pad pad) const { return (m_padData[pad][0] & 0xff) == 0; }
107 
118  bool isButtonPressed(Pad pad, Button button) const { return (m_padData[pad][1] & (1 << button)) == 0; }
119 
120  private:
121  void processChanges(Pad pad);
122 
123  uint16_t m_padData[2][0x11];
124  eastl::function<void(Event)> m_callback;
125  bool m_connected[2] = {false, false};
126  uint16_t m_buttons[2] = {0xffff, 0xffff};
127 };
128 
129 } // namespace psyqo
A simple class to access the pads.
Definition: simplepad.hh:45
void setOnEvent(eastl::function< void(Event)> &&callback)
Sets the event callback function.
Definition: simplepad.hh:95
bool isPadConnected(Pad pad) const
Returns the state of a pad.
Definition: simplepad.hh:106
bool isButtonPressed(Pad pad, Button button) const
Returns the state of a button.
Definition: simplepad.hh:118
void initialize()
Initializes the pads.
Definition: simplepad.hh:68