Tscope5
joystick.c
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // __ ______
4 // / /_______________ ____ ___ / ____/
5 // / __/ ___/ ___/ __ \/ __ \/ _ \ /___ )
6 // / /_(__ ) /__/ /_/ / /_/ / __/ ____/ /
7 // \__/____/\___/\____/ .___/\___/ /_____/
8 // /_/
9 //
10 /// \file joystick.c
11 /// Definitions of joystick functions.
12 /// \example joystick01.c
13 ////////////////////////////////////////////////////////////////////////////////
14 
15 
16 #include "../include/tscope5/joystick.h"
17 #include "../include/tscope5/timer.h"
18 #include "../include/tscope5/joystick_internal.h"
19 #include "../include/tscope5/system_internal.h"
20 
21 
22 ////////////////////////////////////////////////////////////////////////////////
23 /// @name Response registration
24 /// Joysticks can only be used as response devices.
25 /// See timer.c for more information about response registration.
26 //@{
27 ////////////////////////////////////////////////////////////////////////////////
28 
29 
30 ////////////////////////////////////////////////////////////////////////////////
31 /// Define a joystick button as a response button.
32 ///
33 /// \param device Number of the joystick (devices are numbered from 1).
34 /// \param button Number of the button (buttons are counted from 1).
35 ///
36 /// \return The reponse number associated with the button.
37 ///
38 /// Give a positive number if you want to monitor button press events,
39 /// a negative number if you want to monitor button release events.
40 ////////////////////////////////////////////////////////////////////////////////
41 int ts5_define_joystick_button(int device, int button)
42 {
43  ts5_check_joystick("ts5_define_joystick_button");
44  ts5_log(TS5_LOGLEVEL_3, "ts5_define_joystick_button(%d,%d)\n", device, button);
45 
46  if (device<1 || device>_ts5_status.timer.num_joysticks) {
47  ts5_fatal("%s: %s %d, %s %d\n", "ts5_define_joystick_button",
48  "device argument is", device,
49  "number of joysticks is",
50  _ts5_status.timer.num_joysticks);
51  }
52 
53  if (button==0) {
54  ts5_fatal("%s: %s\n", "ts5_define_joystick_button",
55  "button argument is 0, response buttons are numbered from 1");
56  }
57 
58  if (abs(button)>_ts5_status.timer.joystick[device-1].num_buttons) {
59  ts5_fatal("%s: %s %d, %s %d is %d\n", "ts5_define_joystick_button",
60  "button argument is", button,
61  "number of joystick buttons for device", device,
62  _ts5_status.timer.joystick[device-1].num_buttons);
63  }
64 
65  if (button>0 && _ts5_status.timer.joystick[device-1]
66  .button_press_defined[button-1]!=0) {
67  ts5_fatal("%s: %s %d %s\n", "ts5_define_joystick_button",
68  "button press", button, "is already defined");
69  }
70 
71  if (button<0 && _ts5_status.timer.joystick[device-1]
72  .button_release_defined[-button-1]!=0) {
73  ts5_fatal("%s: %s %d %s\n", "ts5_define_joystick_button",
74  "button release", button, "is already defined");
75  }
76 
77  _ts5_status.timer.joystick_is_response_device = 1;
78  _ts5_status.timer.num_defined_buttons++;
79  _ts5_status.timer.num_active_buttons++;
80  _ts5_status.timer.joystick[device-1].num_defined_buttons++;
81  _ts5_status.timer.joystick[device-1].num_active_buttons++;
82 
83  if (button>0) {
84  _ts5_status.timer.joystick[device-1]
85  .button_press_defined[button-1] =
86  _ts5_status.timer.num_defined_buttons;
87 
88  _ts5_status.timer.joystick[device-1]
89  .button_press_active[button-1] =
90  _ts5_status.timer.num_defined_buttons;
91  }
92  else {
93  _ts5_status.timer.joystick[device-1]
94  .button_release_defined[-button-1] =
95  _ts5_status.timer.num_defined_buttons;
96 
97  _ts5_status.timer.joystick[device-1]
98  .button_release_active[-button-1] =
99  _ts5_status.timer.num_defined_buttons;
100  }
101 
102  return _ts5_status.timer.num_defined_buttons;
103 }
104 
105 
106 ////////////////////////////////////////////////////////////////////////////////
107 /// Get the number of joysticks that are connected to the system.
108 ///
109 /// \return The number of joysticks that are connected to the system.
110 ////////////////////////////////////////////////////////////////////////////////
112 {
113  ts5_check_joystick("ts5_get_num_joysticks");
114  ts5_log(TS5_LOGLEVEL_4, "ts5_get_num_joysticks()\n");
115 
116  return _ts5_status.timer.num_joysticks;
117 }
118 
119 
120 ////////////////////////////////////////////////////////////////////////////////
121 /// Get the number of buttons available on a joystick.
122 ///
123 /// \param device Number of the joystick (devices are numbered from 1).
124 ///
125 /// \return The number of buttons on the joystick.
126 ////////////////////////////////////////////////////////////////////////////////
128 {
129  ts5_check_joystick("ts5_get_num_joystick_buttons");
130  ts5_log(TS5_LOGLEVEL_4, "ts5_get_num_joystick_buttons(%d)\n", device);
131 
132  return _ts5_status.timer.joystick[device-1].num_buttons;
133 }
134 
135 
136 ////////////////////////////////////////////////////////////////////////////////
137 //@}
138 ////////////////////////////////////////////////////////////////////////////////
void ts5_check_joystick(char *calling_function)
Do some checks at the start of each joystick function.
int ts5_define_joystick_button(int device, int button)
Define a joystick button as a response button.
Definition: joystick.c:41
int ts5_get_num_joysticks()
Get the number of joysticks that are connected to the system.
Definition: joystick.c:111
void ts5_log(const unsigned int level, const char *format,...)
Send info to a logging window.
Definition: system.c:45
int ts5_get_num_joystick_buttons(int device)
Get the number of buttons available on a joystick.
Definition: joystick.c:127
void ts5_fatal(const char *format,...)
Exit safely with an error message.
Definition: system.c:533