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.

laserblast.c 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * laserblsat over OSC
  3. *
  4. * Sonoptic 2020
  5. */
  6. #include <stdio.h>
  7. #include <unistd.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <lo/lo.h> /* OSC library */
  11. /* ------------------------------------------------------------------- */
  12. #define BUFF_SIZE 8000
  13. static char big_buffer[BUFF_SIZE+2];
  14. static int curpos;
  15. static int la_scene, le_laser;
  16. static lo_address lo_addr;
  17. static int x_scale, y_scale;
  18. extern int verbosity;
  19. /* ------------------------------------------------------------------- */
  20. /*
  21. * - prepare OSC transmission
  22. */
  23. int blast_init(char *host, char *port, int scene, int laser)
  24. {
  25. #if DEBUG_LEVEL
  26. fprintf(stderr, ">>> %s ( '%s:%p' %d %d )\n", __func__, host, port,
  27. scene, laser);
  28. #endif
  29. lo_addr = lo_address_new(host, port);
  30. if (verbosity) {
  31. fprintf(stderr, "%s is sending to %s:%s\n", __FILE__, host, port);
  32. }
  33. la_scene = scene, le_laser = laser;
  34. x_scale = y_scale = 700;
  35. /* manage our internals memories */
  36. big_buffer[0] = '\0'; /* clear buffer */
  37. curpos = 0; /* index in buffer */
  38. return 0;
  39. }
  40. /* ------------------------------------------------------------------- */
  41. /*
  42. * this function is for test purpose, don't use in the
  43. * real world, Just don't, OK?
  44. */
  45. int blast_NOP(int reason)
  46. {
  47. int foo;
  48. char msg[50];
  49. fprintf(stderr, "===== BLAST NOP %d\n", reason);
  50. sprintf(msg, "BLAST NOP %d", reason);
  51. foo = lo_send(lo_addr, "/dev/null", "s", msg);
  52. if (-1==foo) {
  53. fprintf(stderr, "%s : error %d\n", __func__, foo);
  54. return -1;
  55. }
  56. return 0;
  57. }
  58. /* ------------------------------------------------------------------- */
  59. int blast_setscale(int width, int height)
  60. {
  61. #if DEBUG_LEVEL
  62. fprintf(stderr, ">>> %s ( %d %d )\n", __func__, width, height);
  63. #endif
  64. /* no validity check ? */
  65. x_scale = width;
  66. y_scale = height;
  67. return -1;
  68. }
  69. /* ------------------------------------------------------------------- */
  70. int blast_rewind(void)
  71. {
  72. if (verbosity > 1) fprintf(stderr, "Blast rewind\n");
  73. big_buffer[0] = '\0'; /* clear buffer */
  74. curpos = 0; /* index in buffer */
  75. strcat(big_buffer, "["); curpos++;
  76. return -1;
  77. }
  78. /* ------------------------------------------------------------------- */
  79. int blast_addpoint(float x, float y, long col)
  80. {
  81. char buff[100];
  82. int sz;
  83. sz = sprintf(buff, "(%.2f, %.2f, %ld),", x, y, col);
  84. if (verbosity > 1) fprintf(stderr, "%s --> %s\n", __func__, buff);
  85. /* check free space in bigbuffer */
  86. if ( (curpos+sz) > BUFF_SIZE ) {
  87. fprintf(stderr, "buffer overflow in %s\n", __func__);
  88. exit(1);
  89. }
  90. strcat(big_buffer, buff); curpos += sz;
  91. return -1;
  92. }
  93. /* ------------------------------------------------------------------- */
  94. int blast_flush(void)
  95. {
  96. int foo;
  97. char buffer[100];
  98. /* we have to kill the last ',' in the big buff before send */
  99. big_buffer[curpos-1] = ']'; big_buffer[curpos] = '\0';
  100. if (verbosity) fprintf(stderr, "BLAST %s\n", big_buffer);
  101. sprintf(buffer, "/pl/%d/%d", la_scene, le_laser);
  102. foo = lo_send(lo_addr, buffer, "s", big_buffer);
  103. if (-1==foo) {
  104. fprintf(stderr, "%s : error %d\n", __func__, foo);
  105. return -1;
  106. }
  107. return 0;
  108. }
  109. /* ------------------------------------------------------------------- */
  110. /* ------------------------------------------------------------------- */
  111. /* ------------------------------------------------------------------- */