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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
use gamepad::Button;
use std::u16::MAX as U16_MAX;
use std::f32::consts::PI;
use std::error::Error as StdError;
use std::fmt;
pub use gamepad::Effect;
#[derive(Copy, Clone, PartialEq, Debug, Default)]
pub struct EffectData {
pub wave: Waveform,
pub direction: Direction,
pub period: u16,
pub magnitude: i16,
pub offset: i16,
pub phase: u16,
pub envelope: Envelope,
pub replay: Replay,
pub trigger: Trigger,
}
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum Waveform {
Square,
Triangle,
Sine,
}
impl Default for Waveform {
fn default() -> Self {
Waveform::Sine
}
}
#[derive(Copy, Clone, PartialEq, Debug, Default)]
pub struct Direction {
pub angle: u16,
}
impl Direction {
pub fn from_radians(ang: f32) -> Self {
let mut ang = ang % (2.0 * PI);
if ang < 0.0 {
ang = 2.0 * PI - ang
};
ang /= 2.0 * PI;
Direction { angle: (U16_MAX as f32 * ang) as u16 }
}
pub fn from_vector(vec: [f32; 2]) -> Self {
vec.into()
}
}
impl From<[f32; 2]> for Direction {
fn from(f: [f32; 2]) -> Self {
let mut val = f[1].atan2(f[0]);
if val.is_sign_negative() {
val += 2.0 * PI;
}
Self::from_radians(val)
}
}
#[derive(Copy, Clone, PartialEq, Debug, Default)]
pub struct Envelope {
pub attack_length: u16,
pub attack_level: u16,
pub fade_length: u16,
pub fade_level: u16,
}
#[derive(Copy, Clone, PartialEq, Debug, Default)]
#[repr(C)]
pub struct Replay {
pub length: u16,
pub delay: u16,
}
#[derive(Copy, Clone, PartialEq, Debug, Default)]
#[repr(C)]
pub struct Trigger {
pub button: Button,
pub interval: u16,
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Error {
NotEnoughSpace,
FfNotSupported,
EffectNotSupported,
FailedToPlay,
}
impl Error {
pub fn to_str(self) -> &'static str {
match self {
Error::NotEnoughSpace => "not enough space for new effect",
Error::FfNotSupported => "force feedback is not supported",
Error::EffectNotSupported => "effect is not supported by device",
Error::FailedToPlay => "can't play effect"
}
}
}
impl StdError for Error {
fn description(&self) -> &str {
self.to_str()
}
}
impl fmt::Display for Error {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.write_str(self.to_str())
}
}