Tscope5
cedrusbox.c
Go to the documentation of this file.
1 
2 
3 
4 
5 
6 
7 
8 #include "../include/tscope5/cedrusbox.h"
9 #include "../include/tscope5/cedrusbox_internal.h"
10 #include "../include/tscope5/system_internal.h"
11 
12 
17 
18 
19 
20 
32 int ts5_define_cedrusbox_key(int device, int key)
33 {
34  ts5_check_cedrusbox("ts5_define_cedrusbox_key");
35  ts5_log(TS5_LOGLEVEL_3, "ts5_define_cedrusbox_key(%d,%d)\n", device, key);
36 
37  if (device<1 || device>_ts5_status.timer_status.num_cedrusbox) {
38  ts5_fatal("ts5_define_cedrusbox_key: device argument is %d,",
39  " number of cedrusboxes is %d\n", device, _ts5_status.timer_status.num_cedrusbox);
40  }
41 
42  if (key==0) {
43  ts5_fatal("ts5_define_cedrusbox_key: key argument is 0, response keys are numbered from 1\n");
44  }
45 
46  if (abs(key)>_ts5_status.timer_status.cedrusbox[device-1].num_buttons) {
47  ts5_fatal("ts5_define_cedrusbox_key: key argument is %d,",
48  " number of cedrusbox buttons for device %d is %d\n",
49  key, device, _ts5_status.timer_status.cedrusbox[device-1].num_buttons);
50  }
51 
52  if (key>0 && _ts5_status.timer_status.cedrusbox[device-1].button_press_defined[key-1]!=0) {
53  ts5_fatal("ts5_define_cedrusbox_key: key press %d is already defined\n", key);
54  }
55 
56  if (key<0 && _ts5_status.timer_status.cedrusbox[device-1].button_release_defined[-key-1]!=0) {
57  ts5_fatal("ts5_define_cedrusbox_key: key release %d is already defined\n", key);
58  }
59 
60  _ts5_status.timer_status.cedrusbox_is_response_device = 1;
61  _ts5_status.timer_status.num_defined_buttons++;
62  _ts5_status.timer_status.num_active_buttons++;
63  _ts5_status.timer_status.cedrusbox[device-1].num_defined_buttons++;
64  _ts5_status.timer_status.cedrusbox[device-1].num_active_buttons++;
65 
66  if (key>0) {
67  _ts5_status.timer_status.cedrusbox[device-1].button_press_defined[key-1] =
68  _ts5_status.timer_status.num_defined_buttons;
69 
70  _ts5_status.timer_status.cedrusbox[device-1].button_press_active[key-1] =
71  _ts5_status.timer_status.num_defined_buttons;
72 
73  }
74  else {
75  _ts5_status.timer_status.cedrusbox[device-1].button_release_defined[-key-1] =
76  _ts5_status.timer_status.num_defined_buttons;
77 
78  _ts5_status.timer_status.cedrusbox[device-1].button_release_active[-key-1] =
79  _ts5_status.timer_status.num_defined_buttons;
80  }
81 
82  return _ts5_status.timer_status.num_defined_buttons;
83 }
84 
85 
112 int ts5_cedrusbox_set_parameter(int device, int parameter, int value)
113 {
114  ts5_check_cedrusbox("ts5_cedrusbox_set_parameter");
115  ts5_log(TS5_LOGLEVEL_4, "ts5_cedrusbox_set_parameter(%d,%d,%d)\n", device, parameter, value);
116 
117  if (_ts5_status.timer_status.cedrusbox[device-1].type!=TS5_CEDRUSBOX_SV1) {
118  ts5_fatal("ts5_cedrusbox_set_parameter: device %d is not a voice key\n", device);
119  }
120 
121  int port_num = _ts5_status.timer_status.cedrusbox[device-1].port_num;
122  int retval = ts5_cedrusbox_get_parameter(device, parameter);
123  char command[6];
124 
125  int min, max;
126 
127  switch (parameter) {
128 
129  case TS5_CEDRUSBOX_LOCKINGLEVEL:
130 
131  if (value != TS5_CEDRUSBOX_LOCK && value != TS5_CEDRUSBOX_UNLOCK) {
132  ts5_fatal("ts5_cedrusbox_set_parameter: TS5_CEDRUSBOX_LOCKINGLEVEL should be",
133  " TS5_CEDRUSBOX_LOCK or TS5_CEDRUSBOX_UNLOCK\n");
134  }
135 
136  sprintf(command, "f2%d", value);
137  break;
138 
139  case TS5_CEDRUSBOX_TRESHOLD:
140 
141  min = ts5_cedrusbox_get_parameter(device, TS5_CEDRUSBOX_MIN_TRESHOLD);
142  max = ts5_cedrusbox_get_parameter(device, TS5_CEDRUSBOX_MAX_TRESHOLD);
143 
144  if (value < min || value > max) {
145  ts5_fatal("ts5_cedrusbox_set_parameter: treshold should be",
146  " between min (%d) and max (%d)\n", min, max);
147  }
148 
149  sprintf(command, "b1%d", value);
150  break;
151 
152  case TS5_CEDRUSBOX_MIN_TRESHOLD:
153 
154  max = ts5_cedrusbox_get_parameter(device, TS5_CEDRUSBOX_MAX_TRESHOLD);
155 
156  if (value < 0 || value > max) {
157  ts5_fatal("ts5_cedrusbox_set_parameter: minimum treshold should be",
158  " between 0 and max (%d)\n", max);
159  }
160 
161  sprintf(command, "b6%d", value);
162  break;
163 
164  case TS5_CEDRUSBOX_MAX_TRESHOLD:
165 
166  min = ts5_cedrusbox_get_parameter(device, TS5_CEDRUSBOX_MIN_TRESHOLD);
167 
168  if (value < min || value > 255) {
169  ts5_fatal("ts5_cedrusbox_set_parameter: maximum treshold should be",
170  " between min (%d) and 255\n", min);
171  }
172 
173  sprintf(command, "b7%d", value);
174  break;
175 
176  case TS5_CEDRUSBOX_RISE_DELAY:
177 
178  min = ts5_cedrusbox_get_parameter(device, TS5_CEDRUSBOX_MIN_RISE_DELAY);
179  max = ts5_cedrusbox_get_parameter(device, TS5_CEDRUSBOX_MAX_RISE_DELAY);
180 
181  if (value < min || value > max) {
182  ts5_fatal("ts5_cedrusbox_set_parameter: rise delay should be",
183  " between min (%d) and max (%d)\n", min, max);
184  }
185 
186  sprintf(command, "b2%d", value);
187  break;
188 
189  case TS5_CEDRUSBOX_MIN_RISE_DELAY:
190 
191  max = ts5_cedrusbox_get_parameter(device, TS5_CEDRUSBOX_MAX_RISE_DELAY);
192 
193  if (value < 0 || value > max) {
194  ts5_fatal("ts5_cedrusbox_set_parameter: minimum rise delay should be",
195  " between 0 and max (%d)\n", max);
196  }
197 
198  sprintf(command, "b4%d", value);
199  break;
200 
201  case TS5_CEDRUSBOX_MAX_RISE_DELAY:
202 
203  min = ts5_cedrusbox_get_parameter(device, TS5_CEDRUSBOX_MIN_RISE_DELAY);
204 
205  if (value < min || value > 100) {
206  ts5_fatal("ts5_cedrusbox_set_parameter: maximum rise delay should be",
207  " between min (%d) and 100\n", min);
208  }
209 
210  sprintf(command, "b5%d", value);
211  break;
212 
213  case TS5_CEDRUSBOX_DROP_DELAY:
214 
215  if (value < 0 || value > 255) {
216  ts5_fatal("ts5_cedrusbox_set_parameter: drop delay 0 and 255\n");
217  }
218 
219  sprintf(command, "b3%d", value);
220  break;
221 
222  default:
223  ts5_fatal("ts5_cedrusbox_set_parameter: unknown parameter value (%d)\n", parameter);
224  }
225 
226  ts5_write_cedrusbox(port_num, command, 4);
227 
228  int newval = ts5_cedrusbox_get_parameter(device, parameter);
229 
230  if (newval != value) {
231  ts5_fatal("ts5_cedrusbox_set_parameter: could not change parameter %d\n", parameter);
232  }
233 
234  return retval;
235 }
236 
237 
248 int ts5_cedrusbox_get_parameter(int device, int parameter)
249 {
250  ts5_check_cedrusbox("ts5_cedrusbox_get_parameter");
251  ts5_log(TS5_LOGLEVEL_4, "ts5_cedrusbox_get_parameter(%d,%d)\n", device, parameter);
252 
253  if (_ts5_status.timer_status.cedrusbox[device-1].type!=TS5_CEDRUSBOX_SV1) {
254  ts5_fatal("ts5_cedrusbox_get_parameter: device %d is not a voice key\n", device);
255  }
256 
257  int port_num = _ts5_status.timer_status.cedrusbox[device-1].port_num;
258 
259  char command[4];
260 
261  switch (parameter) {
262 
263  case TS5_CEDRUSBOX_LOCKINGLEVEL:
264  sprintf(command, "_f2");
265  break;
266 
267  case TS5_CEDRUSBOX_TRESHOLD:
268  sprintf(command, "_b1");
269  break;
270 
271  case TS5_CEDRUSBOX_MIN_TRESHOLD:
272  sprintf(command, "_b6");
273  break;
274 
275  case TS5_CEDRUSBOX_MAX_TRESHOLD:
276  sprintf(command, "_b7");
277  break;
278 
279  case TS5_CEDRUSBOX_RISE_DELAY:
280  sprintf(command, "_b2");
281  break;
282 
283  case TS5_CEDRUSBOX_MIN_RISE_DELAY:
284  sprintf(command, "_b4");
285  break;
286 
287  case TS5_CEDRUSBOX_MAX_RISE_DELAY:
288  sprintf(command, "_b5");
289  break;
290 
291  case TS5_CEDRUSBOX_DROP_DELAY:
292  sprintf(command, "_b3");
293  break;
294 
295  default:
296  ts5_fatal("ts5_cedrusbox_get_parameter: unknown parameter value (%d)\n", parameter);
297  }
298 
299  ts5_write_cedrusbox(port_num, command, 3);
300 
301  char serial_buff[TS5_MAX_CHAR];
302  serial_buff[0] = '\0';
303  ts5_read_cedrusbox(port_num, serial_buff, 4);
304 
305  int value = 0;
306  value = serial_buff[3];
307 
308  return value;
309 }
310 
311 
313 
314