Skip to content

Commit 8ee0d22

Browse files
committed
HID cannot be alive with glfw
1 parent 59854de commit 8ee0d22

File tree

4 files changed

+89
-138
lines changed

4 files changed

+89
-138
lines changed

core/src/Input/HID.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,12 @@ std::vector<HID::DeviceProperty> HID::Enumerate() {
8585
hid_set_nonblocking(prop.Handler, 1);
8686

8787
// enable vibration for joycon
88-
uint8_t data[0x01];
88+
uint8_t data[1];
8989
data[0] = 0x01;
9090
SendSubcommand(prop.Handler, 0x48, data, 1);
9191

9292
// send player light
93-
data[0] = 0;
93+
data[0] = 0b1111;
9494
SendSubcommand(prop.Handler, 0x30, data, 1);
9595
}
9696
}
@@ -145,10 +145,13 @@ void HID::TerminateDevices() {
145145
if (prop.Handler != nullptr) {
146146
if (prop.VendorID == VendorNintendo) {
147147
if (prop.ProductID == ProductJoyconL || prop.ProductID == ProductJoyconR) {
148-
uint8_t data[0x01];
149-
data[0] = 0;
148+
uint8_t data[1];
149+
150+
// set light
151+
data[0] = 0b1111;
150152
this->SendSubcommand(prop.Handler, 0x30, data, 1);
151153

154+
// vibration
152155
data[0] = 0x01;
153156
this->SendSubcommand(prop.Handler, 0x48, data, 1);
154157
}
@@ -190,7 +193,7 @@ bool HID::SetLight(int32_t joystickIndex, int32_t number) {
190193

191194
if (prop.VendorID == VendorNintendo) {
192195
if (prop.ProductID == ProductJoyconL || prop.ProductID == ProductJoyconR) {
193-
uint8_t data[0x01];
196+
uint8_t data[1];
194197
// send player light
195198
data[0] = 0b1111 & number;
196199
SendSubcommand(prop.Handler, 0x30, data, 1);
@@ -226,7 +229,7 @@ bool HID::Vibrate(int32_t joystickIndex, float frequency, float amplitude) {
226229
}
227230

228231
void HID::VibrateJoycon(hid_device* handler, float frequency, float amplitude) {
229-
RETURN_IF_NULL(handler,);
232+
RETURN_IF_NULL(handler, );
230233

231234
uint8_t buf[0x40];
232235
memset(buf, 0x0, size_t(0x40));

core/src/Input/Joystick.cpp

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,22 @@ Joystick::Joystick() : connectedJoystickCount_(0) {
4343
bool Joystick::Initialize() {
4444
instance_ = MakeAsdShared<Joystick>();
4545

46-
instance_->hid_ = std::make_unique<HID>();
47-
if (!instance_->hid_->Initialize()) {
48-
Log::GetInstance()->Warn(LogCategory::Core, u"Joystick::Initialize: failed to initialize HID");
49-
instance_->hid_ = nullptr;
50-
}
46+
// TODO
47+
// instance_->hid_ = std::make_unique<HID>();
48+
// if (!instance_->hid_->Initialize()) {
49+
// Log::GetInstance()->Warn(LogCategory::Core, u"Joystick::Initialize: failed to initialize HID");
50+
// instance_->hid_ = nullptr;
51+
// }
5152

5253
instance_->RefreshInputState();
54+
5355
return true;
5456
};
5557

5658
void Joystick::Terminate() {
57-
if(instance_->hid_ != nullptr) {
58-
instance_->hid_->TerminateDevices();
59-
}
59+
// if (instance_->hid_ != nullptr) {
60+
// instance_->hid_->TerminateDevices();
61+
// }
6062

6163
instance_ = nullptr;
6264
}
@@ -158,9 +160,9 @@ void Joystick::RefreshInputState() {
158160
}
159161

160162
// HID
161-
if (hid_ != nullptr && isConnectionUpdated) {
162-
hid_->Refresh(connectedJoystickCount_);
163-
}
163+
// if (hid_ != nullptr && isConnectionUpdated) {
164+
// hid_->Refresh(connectedJoystickCount_);
165+
// }
164166
};
165167

166168
bool Joystick::IsPresent(int32_t joystickIndex) const {
@@ -258,37 +260,37 @@ float Joystick::GetAxisStateByType(int32_t joystickIndex, JoystickAxis type) con
258260
return gamepadCurrentAxis_[joystickIndex][index];
259261
};
260262

261-
bool Joystick::SetLight(int32_t joystickIndex, int number) {
262-
if (joystickIndex < 0 || MAX_JOYSTICKS_NUM <= joystickIndex) {
263-
Log::GetInstance()->Warn(LogCategory::Core, u"Joystick::SetLight: index '{0}' is out of range", joystickIndex);
264-
return false;
265-
}
263+
// bool Joystick::SetLight(int32_t joystickIndex, int number) {
264+
// if (joystickIndex < 0 || MAX_JOYSTICKS_NUM <= joystickIndex) {
265+
// Log::GetInstance()->Warn(LogCategory::Core, u"Joystick::SetLight: index '{0}' is out of range", joystickIndex);
266+
// return false;
267+
// }
266268

267-
const auto info = joystickInfo_[joystickIndex];
269+
// const auto info = joystickInfo_[joystickIndex];
268270

269-
if (info == nullptr) {
270-
Log::GetInstance()->Warn(LogCategory::Core, u"Joystick::SetLight: joystick is not connected as {0}", joystickIndex);
271-
return false;
272-
}
271+
// if (info == nullptr) {
272+
// Log::GetInstance()->Warn(LogCategory::Core, u"Joystick::SetLight: joystick is not connected at {0}", joystickIndex);
273+
// return false;
274+
// }
273275

274-
return hid_->SetLight(joystickIndex, number);
275-
}
276+
// return hid_->SetLight(joystickIndex, number);
277+
// }
276278

277-
bool Joystick::Vibrate(int32_t joystickIndex, float frequency, float amplitude) {
278-
if (joystickIndex < 0 || MAX_JOYSTICKS_NUM <= joystickIndex) {
279-
Log::GetInstance()->Warn(LogCategory::Core, u"Joystick::Vibrate: index '{0}' is out of range", joystickIndex);
280-
return false;
281-
}
279+
// bool Joystick::Vibrate(int32_t joystickIndex, float frequency, float amplitude) {
280+
// if (joystickIndex < 0 || MAX_JOYSTICKS_NUM <= joystickIndex) {
281+
// Log::GetInstance()->Warn(LogCategory::Core, u"Joystick::Vibrate: index '{0}' is out of range", joystickIndex);
282+
// return false;
283+
// }
282284

283-
const auto info = joystickInfo_[joystickIndex];
285+
// const auto info = joystickInfo_[joystickIndex];
284286

285-
if (info == nullptr) {
286-
Log::GetInstance()->Warn(LogCategory::Core, u"Joystick::Vibrate: joystick is not connected as {0}", joystickIndex);
287-
return false;
288-
}
287+
// if (info == nullptr) {
288+
// Log::GetInstance()->Warn(LogCategory::Core, u"Joystick::Vibrate: joystick is not connected at {0}", joystickIndex);
289+
// return false;
290+
// }
289291

290-
return hid_->Vibrate(joystickIndex, frequency, amplitude);
291-
}
292+
// return hid_->Vibrate(joystickIndex, frequency, amplitude);
293+
// }
292294

293295
} // namespace Altseed2
294296

core/src/Input/Joystick.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,6 @@ class Joystick : public BaseObject {
9696

9797
int32_t connectedJoystickCount_;
9898

99-
// std::array<std::unique_ptr<hid_device>, MAX_JOYSTICKS_NUM> hidapiDevices_;
100-
10199
std::array<std::shared_ptr<JoystickInfo>, MAX_JOYSTICKS_NUM> joystickInfo_;
102100
std::array<std::array<bool, MAX_BUTTONS_NUM>, MAX_JOYSTICKS_NUM> currentHit_;
103101
std::array<std::array<bool, MAX_BUTTONS_NUM>, MAX_JOYSTICKS_NUM> preHit_;
@@ -107,12 +105,11 @@ class Joystick : public BaseObject {
107105
std::array<std::array<bool, MAX_GAMEPAD_BUTTONS_NUM>, MAX_JOYSTICKS_NUM> gamepadPreHit_;
108106
std::array<std::array<float, MAX_GAMEPAD_AXES_NUM>, MAX_JOYSTICKS_NUM> gamepadCurrentAxis_;
109107

110-
std::unique_ptr<HID> hid_;
108+
// std::unique_ptr<HID> hid_;
111109

112110
std::u16string ToU16(const std::wstring& wstr) const;
113111

114112
public:
115-
116113
Joystick();
117114

118115
static bool Initialize();
@@ -133,8 +130,8 @@ class Joystick : public BaseObject {
133130
float GetAxisStateByIndex(int32_t joystickIndex, int32_t axisIndex) const;
134131
float GetAxisStateByType(int32_t joystickIndex, JoystickAxis type) const;
135132

136-
bool SetLight(int32_t joystickIndex, int32_t number);
137-
bool Vibrate(int32_t joystickIndex, float frequency, float amplitude);
133+
// bool SetLight(int32_t joystickIndex, int32_t number);
134+
// bool Vibrate(int32_t joystickIndex, float frequency, float amplitude);
138135
};
139136

140137
} // namespace Altseed2

core/test/Joystick.cpp

Lines changed: 40 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -20,115 +20,64 @@ bool IsPushedOrHolded(int joystickIndex, Altseed2::JoystickButton btn, int count
2020
}
2121
}
2222

23-
// TODO
24-
TEST(Joystick, SetLight) {
25-
auto config = Altseed2TestConfig(Altseed2::CoreModules::Joystick);
26-
EXPECT_TRUE(config != nullptr);
23+
// TEST(Joystick, SetLight) {
24+
// auto config = Altseed2TestConfig(Altseed2::CoreModules::Joystick);
25+
// EXPECT_TRUE(config != nullptr);
2726

28-
EXPECT_TRUE(Altseed2::Core::Initialize(u"Joystick ButtonState", 640, 480, config));
27+
// EXPECT_TRUE(Altseed2::Core::Initialize(u"Joystick ButtonState", 640, 480, config));
2928

30-
float time = 0.0f;
29+
// float time = 0.0f;
3130

32-
while (Altseed2::Core::GetInstance()->DoEvent()) {
33-
int joystickIndex = 0;
31+
// while (Altseed2::Core::GetInstance()->DoEvent()) {
32+
// int joystickIndex = 0;
3433

35-
float t = 0.5f * std::sin(time) + 0.5f;
34+
// float t = 0.5f * std::sin(time) + 0.5f;
3635

37-
int lightNumber = t * 15;
38-
Altseed2::Joystick::GetInstance()->SetLight(joystickIndex, lightNumber);
36+
// int lightNumber = t * 15;
37+
// Altseed2::Joystick::GetInstance()->SetLight(joystickIndex, lightNumber);
3938

40-
time += Altseed2::Core::GetInstance()->GetDeltaSecond();
39+
// time += Altseed2::Core::GetInstance()->GetDeltaSecond();
4140

42-
if (time > 10.0f) {
43-
break;
44-
}
45-
}
46-
}
41+
// if (time > 10.0f) {
42+
// break;
43+
// }
44+
// }
4745

48-
// TODO
49-
TEST(Joystick, Vibrate) {
50-
auto config = Altseed2TestConfig(Altseed2::CoreModules::Joystick);
51-
EXPECT_TRUE(config != nullptr);
46+
// Altseed2::Core::Terminate();
47+
// }
5248

53-
EXPECT_TRUE(Altseed2::Core::Initialize(u"Joystick ButtonState", 640, 480, config));
49+
// TEST(Joystick, Vibrate) {
50+
// auto config = Altseed2TestConfig(Altseed2::CoreModules::Joystick);
51+
// EXPECT_TRUE(config != nullptr);
5452

55-
float time = 0.0f;
53+
// EXPECT_TRUE(Altseed2::Core::Initialize(u"Joystick ButtonState", 640, 480, config));
5654

57-
while (Altseed2::Core::GetInstance()->DoEvent()) {
58-
int joystickIndex = 0;
59-
float frequency = 150.0f;
60-
float amplitude = 0.8f;
55+
// float time = 0.0f;
6156

62-
float t = 0.5f * std::sin(time) + 0.5f;
57+
// while (Altseed2::Core::GetInstance()->DoEvent()) {
58+
// int joystickIndex = 0;
59+
// float frequency = 150.0f;
60+
// float amplitude = 0.8f;
6361

64-
if (time < 5.0f) {
65-
amplitude *= t;
66-
} else {
67-
frequency *= t;
68-
}
62+
// float t = 0.5f * std::sin(time) + 0.5f;
6963

70-
Altseed2::Joystick::GetInstance()->Vibrate(joystickIndex, frequency, amplitude);
64+
// if (time < 5.0f) {
65+
// amplitude *= t;
66+
// } else {
67+
// frequency *= t;
68+
// }
7169

72-
time += Altseed2::Core::GetInstance()->GetDeltaSecond();
70+
// Altseed2::Joystick::GetInstance()->Vibrate(joystickIndex, frequency, amplitude);
7371

74-
if (time > 10.0f) {
75-
break;
76-
}
77-
}
72+
// time += Altseed2::Core::GetInstance()->GetDeltaSecond();
7873

79-
// while (Altseed2::Core::GetInstance()->DoEvent()) {
80-
// if (!vibrated && Altseed2::Joystick::GetInstance()->GetButtonStateByType(joystickIndex, Altseed2::JoystickButton::RightDown) == Altseed2::ButtonState::Push) {
81-
// Altseed2::Joystick::GetInstance()->Vibrate(joystickIndex, frequency, amplitude);
82-
// start = std::chrono::system_clock::now();
83-
// vibrated = true;
84-
// }
85-
86-
// if (IsPushedOrHolded(joystickIndex, Altseed2::JoystickButton::RightRight, count)) {
87-
// frequency += 10.0f;
88-
// std::cout << "freq: " << frequency << std::endl;
89-
// }
90-
91-
// if (IsPushedOrHolded(joystickIndex, Altseed2::JoystickButton::RightDown, count)) {
92-
// frequency -= 10.0f;
93-
// std::cout << "freq: " << frequency << std::endl;
94-
// }
95-
96-
// if (IsPushedOrHolded(joystickIndex, Altseed2::JoystickButton::RightUp, count)) {
97-
// amplitude += 0.05f;
98-
// std::cout << "amp : " << amplitude << std::endl;
99-
// }
100-
101-
// if (IsPushedOrHolded(joystickIndex, Altseed2::JoystickButton::RightLeft, count)) {
102-
// amplitude -= 0.05f;
103-
// std::cout << "amp : " << amplitude << std::endl;
104-
// }
105-
106-
// if (IsPushedOrHolded(joystickIndex, Altseed2::JoystickButton::RightBumper, count)) {
107-
// vibrate_time += 10.0f;
108-
// std::cout << "time: " << vibrate_time << std::endl;
109-
// }
110-
111-
// if (IsPushedOrHolded(joystickIndex, Altseed2::JoystickButton::LeftBumper, count)) {
112-
// vibrate_time -= 10.0f;
113-
// std::cout << "time: " << vibrate_time << std::endl;
114-
// }
115-
116-
// if (vibrated && std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() >= vibrate_time) {
117-
// Altseed2::Joystick::GetInstance()->Vibrate(joystickIndex, 100.0f, 0);
118-
// vibrated = false;
119-
// }
120-
121-
// end = std::chrono::system_clock::now();
122-
// count++;
123-
124-
// time += Altseed2::Core::GetInstance()->GetDeltaSecond();
125-
// if (time > 10.0f) {
126-
// break;
127-
// }
128-
// }
74+
// if (time > 10.0f) {
75+
// break;
76+
// }
77+
// }
12978

130-
Altseed2::Core::Terminate();
131-
}
79+
// Altseed2::Core::Terminate();
80+
// }
13281

13382
void printJoystickInformation() {
13483
for (int i = 0; i < 16; i++) {

0 commit comments

Comments
 (0)