Tscope5
keyboard.c
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // __ ______
4 // / /_______________ ____ ___ / ____/
5 // / __/ ___/ ___/ __ \/ __ \/ _ \ /___ )
6 // / /_(__ ) /__/ /_/ / /_/ / __/ ____/ /
7 // \__/____/\___/\____/ .___/\___/ /_____/
8 // /_/
9 //
10 /// \file keyboard.c
11 /// Definitions of keyboard functions.
12 /// \example keyboard01.c
13 ////////////////////////////////////////////////////////////////////////////////
14 
15 
16 #include "../include/tscope5/keyboard.h"
17 #include "../include/tscope5/timer.h"
18 #include "../include/tscope5/keyboard_internal.h"
19 #include "../include/tscope5/system_internal.h"
20 
21 
22 ////////////////////////////////////////////////////////////////////////////////
23 /// @name Response registration
24 /// The keyboard can be used as a response device.
25 /// See timer.c for more information about response registration.
26 ///
27 /// The keyboard can also be used for scanf type input.
28 /// See textio.c for more information about scanf type input.
29 //@{
30 ////////////////////////////////////////////////////////////////////////////////
31 
32 
33 ////////////////////////////////////////////////////////////////////////////////
34 /// Define a keyboard button as a response button.
35 ///
36 /// \param button Number of the button (buttons are counted from 1).
37 ///
38 /// \return The reponse number associated with the button.
39 ///
40 /// Possible button values are:
41 /// - TS5_KEY_A, TS5_KEY_B, TS5_KEY_C, TS5_KEY_D, TS5_KEY_E, TS5_KEY_F,
42 /// TS5_KEY_G, TS5_KEY_H, TS5_KEY_I, TS5_KEY_J TS5_KEY_K, TS5_KEY_L,
43 /// TS5_KEY_M, TS5_KEY_N, TS5_KEY_O, TS5_KEY_P, TS5_KEY_Q, TS5_KEY_R,
44 /// TS5_KEY_S, TS5_KEY_T, TS5_KEY_U, TS5_KEY_V, TS5_KEY_W, TS5_KEY_X,
45 /// TS5_KEY_Y, TS5_KEY_Z
46 ///
47 /// - TS5_KEY_0, TS5_KEY_1, TS5_KEY_2, TS5_KEY_3, TS5_KEY_4, TS5_KEY_5,
48 /// TS5_KEY_6, TS5_KEY_7, TS5_KEY_8, TS5_KEY_9
49 ///
50 /// - TS5_KEY_PAD_0, TS5_KEY_PAD_1, TS5_KEY_PAD_2, TS5_KEY_PAD_3,
51 /// TS5_KEY_PAD_4, TS5_KEY_PAD_5, TS5_KEY_PAD_6, TS5_KEY_PAD_7,
52 /// TS5_KEY_PAD_8, TS5_KEY_PAD_9
53 ///
54 /// - TS5_KEY_F1, TS5_KEY_F2, TS5_KEY_F3, TS5_KEY_F4, TS5_KEY_F5, TS5_KEY_F6,
55 /// TS5_KEY_F7, TS5_KEY_F8, TS5_KEY_F9, TS5_KEY_F10, TS5_KEY_F11, TS5_KEY_F12
56 ///
57 /// - TS5_KEY_ESCAPE, TS5_KEY_TILDE, TS5_KEY_MINUS, TS5_KEY_EQUALS,
58 /// TS5_KEY_BACKSPACE, TS5_KEY_TAB, TS5_KEY_OPENBRACE, TS5_KEY_CLOSEBRACE,
59 /// TS5_KEY_ENTER, TS5_KEY_SEMICOLON, TS5_KEY_QUOTE, TS5_KEY_BACKSLASH,
60 /// TS5_KEY_BACKSLASH2, TS5_KEY_COMMA, TS5_KEY_FULLSTOP, TS5_KEY_SLASH,
61 /// TS5_KEY_SPACE
62 ///
63 /// - TS5_KEY_INSERT, TS5_KEY_DELETE, TS5_KEY_HOME, TS5_KEY_END, TS5_KEY_PGUP,
64 /// TS5_KEY_PGDN, TS5_KEY_LEFT, TS5_KEY_RIGHT, TS5_KEY_UP, TS5_KEY_DOWN
65 ///
66 /// - TS5_KEY_PAD_SLASH, TS5_KEY_PAD_ASTERISK, TS5_KEY_PAD_MINUS,
67 /// TS5_KEY_PAD_PLUS, TS5_KEY_PAD_DELETE, TS5_KEY_PAD_ENTER,
68 /// TS5_KEY_PRINTSCREEN, TS5_KEY_PAUSE
69 ///
70 /// - TS5_KEY_ABNT_C1, TS5_KEY_YEN, TS5_KEY_KANA, TS5_KEY_CONVERT,
71 /// TS5_KEY_NOCONVERT, TS5_KEY_AT, TS5_KEY_CIRCUMFLEX, TS5_KEY_COLON2,
72 /// TS5_KEY_KANJI
73 ///
74 /// - TS5_KEY_PAD_EQUALS, TS5_KEY_BACKQUOTE, TS5_KEY_SEMICOLON2,
75 /// TS5_KEY_COMMAND, TS5_KEY_BACK
76 ///
77 /// - TS5_KEY_LSHIFT, TS5_KEY_RSHIFT, TS5_KEY_LCTRL, TS5_KEY_RCTRL,
78 /// TS5_KEY_ALT, TS5_KEY_ALTGR, TS5_KEY_LWIN, TS5_KEY_RWIN, TS5_KEY_MENU,
79 /// TS5_KEY_SCROLLLOCK, TS5_KEY_NUMLOCK, TS5_KEY_CAPSLOCK
80 ///
81 /// Give a positive number if you want to monitor button press events,
82 /// a negative number if you want to monitor button release events.
83 ///
84 ///
85 /// \warning Mac OS X makes no difference between the left and right versions
86 /// of the shift, control and alt buttons.
87 ////////////////////////////////////////////////////////////////////////////////
89 {
90  ts5_check_keyboard("ts5_define_keyboard_button");
91  ts5_log(TS5_LOGLEVEL_3, "ts5_define_keyboard_button(%d)\n", button);
92 
93  if (button==0) {
94  ts5_fatal("%s: %s\n", "ts5_define_keyboard_button",
95  "button argument is 0, response buttons are numbered from 1");
96  }
97 
98  if (abs(button)>_ts5_status.timer.keyboard.num_buttons) {
99  ts5_fatal("%s: %s %d, %s is %d\n", "ts5_define_keyboard_button",
100  "button argument is", button,
101  "number of keyboard buttons",
102  _ts5_status.timer.keyboard.num_buttons);
103  }
104 
105  if (button>0 && _ts5_status.timer.keyboard
106  .button_press_defined[button-1]!=0) {
107  ts5_fatal("%s: %s %d %s\n", "ts5_define_keyboard_button",
108  "button press", button, "is already defined");
109  }
110 
111  if (button<0 && _ts5_status.timer.keyboard
112  .button_release_defined[-button-1]!=0) {
113  ts5_fatal("%s: %s %d %s\n", "ts5_define_keyboard_button",
114  "button release", button, "is already defined");
115  }
116 
117  _ts5_status.timer.keyboard_is_response_device = 1;
118  _ts5_status.timer.num_defined_buttons++;
119  _ts5_status.timer.num_active_buttons++;
120  _ts5_status.timer.keyboard.num_defined_buttons++;
121  _ts5_status.timer.keyboard.num_active_buttons++;
122 
123 
124  if (button>0) {
125  _ts5_status.timer.keyboard.button_press_defined[button-1] =
126  _ts5_status.timer.num_defined_buttons;
127 
128  _ts5_status.timer.keyboard.button_press_active[button-1] =
129  _ts5_status.timer.num_defined_buttons;
130  }
131  else {
132  _ts5_status.timer.keyboard.button_release_defined[-button-1] =
133  _ts5_status.timer.num_defined_buttons;
134 
135  _ts5_status.timer.keyboard.button_release_active[-button-1] =
136  _ts5_status.timer.num_defined_buttons;
137  }
138 
139  return _ts5_status.timer.num_defined_buttons;
140 }
141 
142 
143 ////////////////////////////////////////////////////////////////////////////////
144 /// Get the number of buttons available on the keyboard.
145 ///
146 /// \return The number of buttons on the keyboard.
147 ///
148 /// This value is constant,
149 /// irrespective of the actual number of buttons on the keyboard.
150 ////////////////////////////////////////////////////////////////////////////////
152 {
153  ts5_check_keyboard("ts5_get_num_keyboard_buttons");
154  ts5_log(TS5_LOGLEVEL_4, "ts5_get_num_keyboard_buttons()\n");
155 
156  return _ts5_status.timer.keyboard.num_buttons;
157 }
158 
159 
160 ////////////////////////////////////////////////////////////////////////////////
161 //@}
162 ////////////////////////////////////////////////////////////////////////////////
int ts5_define_keyboard_button(int button)
Define a keyboard button as a response button.
Definition: keyboard.c:88
void ts5_log(const unsigned int level, const char *format,...)
Send info to a logging window.
Definition: system.c:45
void ts5_check_keyboard(char *calling_function)
Do some checks at the start of each keyboard function.
void ts5_fatal(const char *format,...)
Exit safely with an error message.
Definition: system.c:533
int ts5_get_num_keyboard_buttons()
Get the number of buttons available on the keyboard.
Definition: keyboard.c:151