-
Notifications
You must be signed in to change notification settings - Fork 279
/
ex_haptic.c
138 lines (103 loc) · 3.12 KB
/
ex_haptic.c
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
/*
* Example program for the Allegro library, by Beoran.
*
* This program tests haptic effects.
*/
#define ALLEGRO_UNSTABLE
#include <allegro5/allegro.h>
#include <allegro5/allegro_primitives.h>
#include "common.c"
static void test_haptic_joystick(ALLEGRO_JOYSTICK *joy)
{
ALLEGRO_HAPTIC_EFFECT_ID id;
ALLEGRO_HAPTIC *haptic;
ALLEGRO_HAPTIC_EFFECT effect;
const double intensity = 1.0;
const double duration = 1.0;
haptic = al_get_haptic_from_joystick(joy);
if (!haptic) {
log_printf("Could not get haptic device from joystick!");
return;
}
log_printf("Can play back %d haptic effects.\n",
al_get_max_haptic_effects(haptic));
log_printf("Set gain to 0.8: %d.\n",
al_set_haptic_gain(haptic, 0.8));
log_printf("Get gain: %lf.\n",
al_get_haptic_gain(haptic));
log_printf("Capabilities: %d.\n",
al_get_haptic_capabilities(haptic));
memset(&effect, 0, sizeof(effect));
effect.type = ALLEGRO_HAPTIC_RUMBLE;
effect.data.rumble.strong_magnitude = intensity;
effect.data.rumble.weak_magnitude = intensity;
effect.replay.delay = 0.1;
effect.replay.length = duration;
log_printf("Upload effect: %d.\n",
al_upload_haptic_effect(haptic, &effect, &id));
log_printf("Playing effect: %d.\n",
al_play_haptic_effect(&id, 3));
do {
al_rest(0.1);
} while (al_is_haptic_effect_playing(&id));
log_printf("Set gain to 0.4: %d.\n",
al_set_haptic_gain(haptic, 0.4));
log_printf("Get gain: %lf.\n",
al_get_haptic_gain(haptic));
log_printf("Playing effect again: %d.\n",
al_play_haptic_effect(&id, 5));
al_rest(2.0);
log_printf("Stopping effect: %d.\n",
al_stop_haptic_effect(&id));
do {
al_rest(0.1);
} while (al_is_haptic_effect_playing(&id));
log_printf("Release effect: %d.\n",
al_release_haptic_effect(&id));
log_printf("Release haptic: %d.\n",
al_release_haptic(haptic));
}
int main(int argc, char **argv)
{
ALLEGRO_DISPLAY *display;
int num_joysticks;
int i;
(void)argc;
(void)argv;
if (!al_init()) {
abort_example("Could not init Allegro.\n");
}
display = al_create_display(640, 480);
if (!display) {
abort_example("al_create_display failed\n");
}
if (!al_install_joystick()) {
abort_example("al_install_joystick failed\n");
}
if (!al_install_haptic()) {
abort_example("al_install_haptic failed\n");
}
open_log();
num_joysticks = al_get_num_joysticks();
log_printf("Found %d joysticks.\n", num_joysticks);
for (i = 0; i < num_joysticks; i++) {
ALLEGRO_JOYSTICK *joy = al_get_joystick(i);
if (!joy) {
continue;
}
if (al_is_joystick_haptic(joy)) {
log_printf("Joystick %s supports force feedback.\n",
al_get_joystick_name(joy));
test_haptic_joystick(joy);
}
else {
log_printf("Joystick %s does not support force feedback.\n",
al_get_joystick_name(joy));
}
al_release_joystick(joy);
}
log_printf("\nAll done!\n");
close_log(true);
return 0;
}
/* vim: set ts=8 sts=3 sw=3 et: */