Logiciels divers autour du protocole « Open Sound Control » aka OSC
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

joy2laser.c 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Joystick to Laser
  3. *
  4. * Sonoptic 2020
  5. */
  6. #include <stdio.h>
  7. #include <unistd.h>
  8. #include <stdlib.h>
  9. #include <getopt.h>
  10. #include <time.h>
  11. #include <lo/lo.h> /* OSC library */
  12. #include "laserblast.h"
  13. /* ------------------------------------------------------------------- */
  14. #define LOCAL_PORT "9000"
  15. #define REMOTE_HOST "localhost" /* just loling */
  16. #define REMOTE_PORT "9999"
  17. int verbosity = 0;
  18. int scenenumber = 1,
  19. lasernumber = 1;
  20. /* ------------------------------------------------------------------- */
  21. int draw_this_point(int x, int y)
  22. {
  23. float fx, fy;
  24. fx = (32768.0 + (float)x) / 94.7;
  25. fy = (32768.0 + (float)y) / 94.7;
  26. #define SZ 10.0
  27. blast_rewind();
  28. blast_addpoint(fx-SZ, fy-SZ, 0xffffff);
  29. blast_addpoint(fx-SZ, fy+SZ, 0xffffff);
  30. blast_addpoint(fx+SZ, fy+SZ, 0xffffff);
  31. blast_addpoint(fx+SZ, fy-SZ, 0xffffff);
  32. blast_addpoint(fx-SZ, fy-SZ, 0);
  33. blast_flush(0);
  34. return 0;
  35. }
  36. /* ------------------------------------------------------------------- */
  37. int xy_handler(const char *path, const char *types, lo_arg ** argv,
  38. int argc, void *data, void *user_data)
  39. {
  40. int val_x, val_y;
  41. int foo;
  42. #if DEBUG_LEVEL
  43. fprintf(stderr, ">>> %s ( %s %s %d )\n", __func__, path, types, argc);
  44. #endif
  45. val_x = argv[0]->i; val_y = argv[1]->i;
  46. if (verbosity) fprintf(stderr, "%6d %6d\n", val_x, val_y);
  47. foo = draw_this_point(val_x, val_y);
  48. return 0;
  49. }
  50. /* ------------------------------------------------------------------- */
  51. void error(int num, const char *msg, const char *path)
  52. {
  53. fprintf(stderr, "liblo server error %d in path %s : %s\n", num, path, msg);
  54. exit(1);
  55. }
  56. /* ------------------------------------------------------------------- */
  57. int help(int k)
  58. {
  59. puts("HELP ME !");
  60. puts("\t-p NNN\t\tlocal listening port");
  61. puts("\t-R a.b.c.d\tremote host");
  62. puts("\t-P NNN\t\tremote port");
  63. puts("\t-L N\t\tlaser number");
  64. puts("\t-S N\t\tscene number");
  65. puts("\t-v\t\tincrease verbosity");
  66. return 1;
  67. }
  68. /* ------------------------------------------------------------------- */
  69. int main(int argc, char *argv[])
  70. {
  71. char *local_port = LOCAL_PORT;
  72. char *remote_host = REMOTE_HOST;
  73. char *remote_port = REMOTE_PORT;
  74. int opt, foo;
  75. lo_server_thread st;
  76. long starttime;
  77. fprintf(stderr, "%s compiled %s at %s\n",argv[0], __DATE__, __TIME__);
  78. /* parsing command line options */
  79. while ((opt = getopt(argc, argv, "hp:vL:S:R:P:")) != -1) {
  80. switch (opt) {
  81. case 'h': if (help(0)) exit(1); break;
  82. case 'p': local_port = optarg; break;
  83. case 'R': remote_host = optarg; break;
  84. case 'P': remote_port = optarg; break;
  85. case 'L': lasernumber = atoi(optarg); break;
  86. case 'S': scenenumber = atoi(optarg); break;
  87. case 'v': verbosity++; break;
  88. }
  89. }
  90. if (verbosity) {
  91. fprintf(stderr, "-----------: %s\n", argv[0]);
  92. fprintf(stderr, "pid : %d\n", getpid());
  93. fprintf(stderr, "local port : %s\n", local_port);
  94. fprintf(stderr, "scene : %d\n", scenenumber);
  95. fprintf(stderr, "laser : %d\n", lasernumber);
  96. fprintf(stderr, "remote : %s %s\n", remote_host, remote_port);
  97. }
  98. foo = blast_init(remote_host, remote_port, scenenumber, lasernumber);
  99. if (foo) {
  100. fprintf(stderr, "blast init error : %d\n", foo);
  101. exit(1);
  102. }
  103. st = lo_server_thread_new(local_port, error);
  104. lo_server_thread_add_method(st, "/joystick/xy", "ii", xy_handler, NULL);
  105. lo_server_thread_start(st);
  106. starttime = time(NULL) / 60;
  107. for (;;) {
  108. if (verbosity)
  109. fprintf(stderr, "\tt = %ld min.\n",
  110. (time(NULL)/60L) - starttime);
  111. sleep(60);
  112. }
  113. return 8;
  114. }
  115. /* ------------------------------------------------------------------- */