| 1 |
/* libticables - link cable library, a part of the TiLP project |
| 2 |
* Copyright (C) 1999-2002 Romain Lievin |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation; either version 2 of the License, or |
| 7 |
* (at your option) any later version. |
| 8 |
* |
| 9 |
* This program is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU General Public License |
| 15 |
* along with this program; if not, write to the Free Software Foundation, |
| 16 |
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 |
*/ |
| 18 |
|
| 19 |
#ifdef HAVE_CONFIG_H |
| 20 |
#include <config.h> |
| 21 |
#endif |
| 22 |
|
| 23 |
#include <cctype> |
| 24 |
#include <cstdio> |
| 25 |
#include <cstdlib> |
| 26 |
#include <cstring> |
| 27 |
#ifndef __WIN32__ |
| 28 |
#include <unistd.h> |
| 29 |
#else |
| 30 |
#include <conio.h> |
| 31 |
#endif |
| 32 |
|
| 33 |
#include "../src/ticables.h" |
| 34 |
|
| 35 |
#undef VERSION |
| 36 |
#define VERSION "Test program" |
| 37 |
|
| 38 |
static void print_lc_error(int errnum) |
| 39 |
{ |
| 40 |
char *msg; |
| 41 |
|
| 42 |
if (!ticables_error_get(errnum, &msg)) |
| 43 |
{ |
| 44 |
fprintf(stderr, "Link cable error (code %i)...\n<<%s>>\n", errnum, msg); |
| 45 |
ticables_error_free(msg); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
# define to_START(ref) { (ref) = ((1000*clock()) / CLOCKS_PER_SEC); } |
| 50 |
# define to_CURRENT(ref) ( (1000*clock()) / CLOCKS_PER_SEC - (ref) ) |
| 51 |
# define to_ELAPSED(ref, max) ( TO_CURRENT(ref) > (100*(max)) ) |
| 52 |
|
| 53 |
#if defined(__LINUX__) || defined(__MACOSX__) || defined(__BSD__) |
| 54 |
# include <unistd.h> |
| 55 |
# define PAUSE(x) usleep(1000*(x)) |
| 56 |
#elif defined(__WIN32__) |
| 57 |
# include <windows.h> |
| 58 |
# define PAUSE(x) Sleep((x)) |
| 59 |
#endif |
| 60 |
|
| 61 |
int main(int argc, char **argv) |
| 62 |
{ |
| 63 |
CableHandle *handle; |
| 64 |
int err; |
| 65 |
// int i, j; |
| 66 |
// uint8_t buf[65536], data; |
| 67 |
// int status, result; |
| 68 |
// uint8_t scr[3840 + 6]; |
| 69 |
// int **probing = NULL; |
| 70 |
|
| 71 |
#if 0 |
| 72 |
tiTIME ref, end; |
| 73 |
unsigned long k; |
| 74 |
|
| 75 |
to_START(ref); |
| 76 |
for(k = 0; k < 1000000; k++) printf(" "); |
| 77 |
to_START(end); |
| 78 |
printf("%lu %lu\n", ref, end); |
| 79 |
printf("%lu\n", to_CURRENT(ref)); |
| 80 |
printf("%i\n", to_ELAPSED(ref, 60)); |
| 81 |
|
| 82 |
return 0; |
| 83 |
#endif |
| 84 |
|
| 85 |
printf("USB support: %i\n", ticables_is_usb_enabled()); |
| 86 |
|
| 87 |
// init lib |
| 88 |
ticables_library_init(); |
| 89 |
|
| 90 |
print_lc_error(1); |
| 91 |
|
| 92 |
#if 0 |
| 93 |
ticables_probing_do(&probing, 5, PROBE_ALL); |
| 94 |
for (i = 1; i <= 7; i++) |
| 95 |
{ |
| 96 |
printf("%i: %i %i %i %i\n", i, probing[i][1], probing[i][2], probing[i][3], probing[i][4]); |
| 97 |
} |
| 98 |
ticables_probing_finish(&probing); |
| 99 |
#endif |
| 100 |
|
| 101 |
#if 0 |
| 102 |
{ |
| 103 |
int *list = NULL; |
| 104 |
int i, n; |
| 105 |
int *p; |
| 106 |
|
| 107 |
ticables_get_usb_devices(&list, &n); |
| 108 |
printf("List of devices:\n"); |
| 109 |
for(i = 0; i < n; i++) |
| 110 |
{ |
| 111 |
printf("%i: %04x\n", i, list[i]); |
| 112 |
} |
| 113 |
} |
| 114 |
#endif |
| 115 |
|
| 116 |
// set cable |
| 117 |
handle = ticables_handle_new(CABLE_PAR, PORT_1); |
| 118 |
if (handle == NULL) |
| 119 |
{ |
| 120 |
return -1; |
| 121 |
} |
| 122 |
|
| 123 |
ticables_options_set_timeout(handle, 15); |
| 124 |
ticables_options_set_delay(handle, 10); |
| 125 |
ticables_handle_show(handle); |
| 126 |
|
| 127 |
// open cable |
| 128 |
err = ticables_cable_open(handle); |
| 129 |
if (err) |
| 130 |
{ |
| 131 |
print_lc_error(err); |
| 132 |
} |
| 133 |
if (err) |
| 134 |
{ |
| 135 |
return -1; |
| 136 |
} |
| 137 |
#if 0 |
| 138 |
// simple test with DirectLink hand-helds (buf size req/neg) |
| 139 |
buf[0]=0x00; buf[1]=0x00; buf[2]=0x00; buf[3]=0x04; |
| 140 |
buf[4]=0x01; |
| 141 |
buf[5]=0x00; buf[6]=0x00; buf[7]=0x04; buf[8]=0x00; |
| 142 |
err = ticables_cable_send(handle, buf, 9); |
| 143 |
if (err) |
| 144 |
{ |
| 145 |
print_lc_error(err); |
| 146 |
} |
| 147 |
|
| 148 |
// display answer |
| 149 |
memset(buf, 0, sizeof(buf)); |
| 150 |
err = ticables_cable_recv(handle, buf, 9); |
| 151 |
if (err) |
| 152 |
{ |
| 153 |
print_lc_error(err); |
| 154 |
} |
| 155 |
|
| 156 |
for (i = 0; i < 9; i++) |
| 157 |
{ |
| 158 |
printf("%02x ", buf[i]); |
| 159 |
} |
| 160 |
printf("\n"); |
| 161 |
#endif |
| 162 |
|
| 163 |
#if 0 |
| 164 |
// mode set |
| 165 |
i = 0; |
| 166 |
buf[i++]=0x00; buf[i++]=0x00; buf[i++]=0x00; buf[i++]=0x10; |
| 167 |
buf[i++]=0x04; |
| 168 |
buf[i++]=0x00; buf[i++]=0x00; buf[i++]=0x00; buf[i++]=0x0a; |
| 169 |
buf[i++]=0x00; buf[i++]=0x01; |
| 170 |
buf[i++]=0x00; buf[i++]=0x03; |
| 171 |
buf[i++]=0x00; buf[i++]=0x01; |
| 172 |
buf[i++]=0x00; buf[i++]=0x00; |
| 173 |
buf[i++]=0x00; buf[i++]=0x00; |
| 174 |
buf[i++]=0x07; buf[i++]=0xd0; |
| 175 |
|
| 176 |
err = ticables_cable_send(handle, buf, i); |
| 177 |
if (err) |
| 178 |
{ |
| 179 |
print_lc_error(err); |
| 180 |
} |
| 181 |
|
| 182 |
err = ticables_cable_recv(handle, buf, 7); |
| 183 |
if (err) |
| 184 |
{ |
| 185 |
print_lc_error(err); |
| 186 |
} |
| 187 |
|
| 188 |
for(i = 0; i < 7; i++) |
| 189 |
{ |
| 190 |
printf("%02x ", buf[i]); |
| 191 |
} |
| 192 |
printf("\n"); |
| 193 |
|
| 194 |
// mode ack |
| 195 |
err = ticables_cable_recv(handle, buf, 15); |
| 196 |
if (err) |
| 197 |
{ |
| 198 |
print_lc_error(err); |
| 199 |
} |
| 200 |
|
| 201 |
for (i = 0; i < 15; i++) |
| 202 |
{ |
| 203 |
printf("%02x ", buf[i]); |
| 204 |
} |
| 205 |
printf("\n"); |
| 206 |
|
| 207 |
i = 0; |
| 208 |
buf[i++]=0x00; buf[i++]=0x00; buf[i++]=0x00; buf[i++]=0x02; |
| 209 |
buf[i++]=0x05; |
| 210 |
buf[i++]=0xe0; buf[i++]=0x00; |
| 211 |
|
| 212 |
err = ticables_cable_send(handle, buf, i); |
| 213 |
if (err) |
| 214 |
{ |
| 215 |
print_lc_error(err); |
| 216 |
} |
| 217 |
|
| 218 |
PAUSE(500); |
| 219 |
#endif |
| 220 |
|
| 221 |
#if 0 |
| 222 |
// param req (TI84+ only) |
| 223 |
i = 0; |
| 224 |
buf[i++]=0x00; buf[i++]=0x00; buf[i++]=0x00; buf[i++]=0xa; |
| 225 |
buf[i++]=0x04; |
| 226 |
buf[i++]=0x00; buf[i++]=0x00; buf[i++]=0x00; buf[i++]=0x04; |
| 227 |
buf[i++]=0x00; buf[i++]=0x07; |
| 228 |
buf[i++]=0x00; buf[i++]=0x01; |
| 229 |
buf[i++]=0x00; buf[i++]=0x22; |
| 230 |
|
| 231 |
err = ticables_cable_send(handle, buf, i); |
| 232 |
if (err) |
| 233 |
{ |
| 234 |
print_lc_error(err); |
| 235 |
} |
| 236 |
|
| 237 |
err = ticables_cable_recv(handle, buf, 7); |
| 238 |
if (err) |
| 239 |
{ |
| 240 |
print_lc_error(err); |
| 241 |
} |
| 242 |
|
| 243 |
for (i = 0; i < 7; i++) |
| 244 |
{ |
| 245 |
printf("%02x ", buf[i]); |
| 246 |
} |
| 247 |
printf("\n"); |
| 248 |
|
| 249 |
// delay ack |
| 250 |
err = ticables_cable_recv(handle, buf, 15); |
| 251 |
if (err) |
| 252 |
{ |
| 253 |
print_lc_error(err); |
| 254 |
} |
| 255 |
|
| 256 |
for (i = 0; i < 15; i++) |
| 257 |
{ |
| 258 |
printf("%02x ", buf[i]); |
| 259 |
} |
| 260 |
printf("\n"); |
| 261 |
|
| 262 |
i = 0; |
| 263 |
buf[i++]=0x00; buf[i++]=0x00; buf[i++]=0x00; buf[i++]=0x02; |
| 264 |
buf[i++]=0x05; |
| 265 |
buf[i++]=0xe0; buf[i++]=0x00; |
| 266 |
|
| 267 |
err = ticables_cable_send(handle, buf, i); |
| 268 |
if (err) |
| 269 |
{ |
| 270 |
print_lc_error(err); |
| 271 |
} |
| 272 |
|
| 273 |
// param data |
| 274 |
for (j = 0; j < 3; j++) |
| 275 |
{ |
| 276 |
err = ticables_cable_recv(handle, buf, 255); |
| 277 |
if (err) |
| 278 |
{ |
| 279 |
print_lc_error(err); |
| 280 |
} |
| 281 |
|
| 282 |
for (i = 0; i < 16; i++) |
| 283 |
{ |
| 284 |
printf("%02x ", buf[i]); |
| 285 |
} |
| 286 |
printf("\n"); |
| 287 |
|
| 288 |
i = 0; |
| 289 |
buf[i++]=0x00; buf[i++]=0x00; buf[i++]=0x00; buf[i++]=0x02; |
| 290 |
buf[i++]=0x05; |
| 291 |
buf[i++]=0xe0; buf[i++]=0x00; |
| 292 |
|
| 293 |
err = ticables_cable_send(handle, buf, i); |
| 294 |
if (err) |
| 295 |
{ |
| 296 |
print_lc_error(err); |
| 297 |
} |
| 298 |
|
| 299 |
} |
| 300 |
{ |
| 301 |
err = ticables_cable_recv(handle, buf, 36); |
| 302 |
if (err) |
| 303 |
{ |
| 304 |
print_lc_error(err); |
| 305 |
} |
| 306 |
|
| 307 |
for (i = 0; i < 16; i++) |
| 308 |
{ |
| 309 |
printf("%02x ", buf[i]); |
| 310 |
} |
| 311 |
printf("\n"); |
| 312 |
|
| 313 |
i = 0; |
| 314 |
buf[i++]=0x00; buf[i++]=0x00; buf[i++]=0x00; buf[i++]=0x02; |
| 315 |
buf[i++]=0x05; |
| 316 |
buf[i++]=0xe0; buf[i++]=0x00; |
| 317 |
|
| 318 |
err = ticables_cable_send(handle, buf, i); |
| 319 |
if (err) |
| 320 |
{ |
| 321 |
print_lc_error(err); |
| 322 |
} |
| 323 |
|
| 324 |
} |
| 325 |
PAUSE(500); |
| 326 |
#endif |
| 327 |
|
| 328 |
#if 0 |
| 329 |
// do a simple test with a TI89/92+ calculator |
| 330 |
buf[0] = 0x08; buf[1] = 0x68; buf[2] = 0x00; buf[3] = 0x00; // RDY |
| 331 |
err = ticables_cable_send(handle, buf, 4); |
| 332 |
if (err) |
| 333 |
{ |
| 334 |
print_lc_error(err); |
| 335 |
} |
| 336 |
|
| 337 |
// display answer |
| 338 |
memset(buf, 0xff, 4); |
| 339 |
err = ticables_cable_recv(handle, buf, 4); |
| 340 |
if (err) |
| 341 |
{ |
| 342 |
print_lc_error(err); |
| 343 |
} |
| 344 |
|
| 345 |
for (i = 0; i < 4; i++) |
| 346 |
{ |
| 347 |
printf("%02x ", buf[i]); |
| 348 |
} |
| 349 |
printf("\n"); |
| 350 |
#endif |
| 351 |
|
| 352 |
#if 0 |
| 353 |
// do a screendump |
| 354 |
buf[0] = 0x08; buf[1] = 0x6D; buf[2] = 0x00; buf[3] = 0x00; // SCR |
| 355 |
err = ticables_cable_send(handle, buf, 4); |
| 356 |
if (err) |
| 357 |
{ |
| 358 |
print_lc_error(err); |
| 359 |
} |
| 360 |
|
| 361 |
memset(buf, 0xff, 4); |
| 362 |
err = ticables_cable_recv(handle, buf, 4); // ACK |
| 363 |
if (err) |
| 364 |
{ |
| 365 |
print_lc_error(err); |
| 366 |
} |
| 367 |
|
| 368 |
err = ticables_cable_recv(handle, scr, 0x0f00 + 6); // XDP |
| 369 |
if (err) |
| 370 |
{ |
| 371 |
print_lc_error(err); |
| 372 |
} |
| 373 |
|
| 374 |
printf("%02x %02x\n", scr[2], scr[3]); |
| 375 |
|
| 376 |
buf[0] = 0x08; buf[1] = 0x56; buf[2] = 0x00; buf[3] = 0x00; // ACK |
| 377 |
err = ticables_cable_send(handle, buf, 4); |
| 378 |
if (err) |
| 379 |
{ |
| 380 |
print_lc_error(err); |
| 381 |
} |
| 382 |
|
| 383 |
#endif |
| 384 |
|
| 385 |
#if 0 |
| 386 |
// simple test for data arrival detection |
| 387 |
buf[0] = 0x08; buf[1] = 0x87; buf[2] = 'A'; buf[3] = 0x00; // KEY |
| 388 |
err = ticables_cable_send(handle, buf, 4); |
| 389 |
if (err) |
| 390 |
{ |
| 391 |
print_lc_error(err); |
| 392 |
} |
| 393 |
|
| 394 |
for(status = 0; !status;) |
| 395 |
{ |
| 396 |
err = ticables_cable_check(handle, &status); |
| 397 |
if (err) |
| 398 |
{ |
| 399 |
print_lc_error(err); |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
// display answer |
| 404 |
memset(buf, 0xff, 4); |
| 405 |
err = ticables_cable_recv(handle, buf, 4); |
| 406 |
if (err) |
| 407 |
{ |
| 408 |
print_lc_error(err); |
| 409 |
} |
| 410 |
|
| 411 |
for (i = 0; i < 4; i++) |
| 412 |
{ |
| 413 |
printf("%02x ", buf[i]); |
| 414 |
} |
| 415 |
printf("\n"); |
| 416 |
#endif |
| 417 |
|
| 418 |
#if 0 |
| 419 |
for(status = 0; !status;) |
| 420 |
{ |
| 421 |
//fprintf(stdout, "$\n"); |
| 422 |
//fflush(stdout); |
| 423 |
err = ticables_cable_check(handle, &status); |
| 424 |
if (err) |
| 425 |
{ |
| 426 |
print_lc_error(err); |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
// display answer |
| 431 |
memset(buf, 0xff, 4); |
| 432 |
err = ticables_cable_recv(handle, buf, 4); |
| 433 |
if (err) |
| 434 |
{ |
| 435 |
print_lc_error(err); |
| 436 |
} |
| 437 |
|
| 438 |
for (i = 0; i < 4; i++) |
| 439 |
{ |
| 440 |
printf("%02x ", buf[i]); |
| 441 |
} |
| 442 |
printf("\n"); |
| 443 |
#endif |
| 444 |
|
| 445 |
// close cable |
| 446 |
ticables_cable_close(handle); |
| 447 |
// release handle |
| 448 |
ticables_handle_del(handle); |
| 449 |
|
| 450 |
// exit lib |
| 451 |
ticables_library_exit(); |
| 452 |
#ifdef __WIN32__ |
| 453 |
while(!kbhit()); |
| 454 |
#endif |
| 455 |
return 0; |
| 456 |
} |
| 457 |
|
| 458 |
// Remote control: display a 'A' on the calc |
| 459 |
/* |
| 460 |
DISPLAY("Display a 'A' on the calc...\n"); |
| 461 |
err = lc.put(0x08); // 0x08: TI89, 0x09: TI92 |
| 462 |
err = lc.put(0x87); |
| 463 |
err = lc.put('A'); |
| 464 |
err = lc.put(0x00); |
| 465 |
printf("Data: %02X\n", data); |
| 466 |
err = lc.get(&data); |
| 467 |
printf("Data: %02X\n", data); |
| 468 |
err = lc.get(&data); |
| 469 |
printf("Data: %02X\n", data); |
| 470 |
err = lc.get(&data); |
| 471 |
printf("Data: %02X\n", data); |
| 472 |
err = lc.get(&data); |
| 473 |
*/ |