/externals/ggee/control/serial_bird.c

https://github.com/pd-l2ork/pd · C · 265 lines · 196 code · 62 blank · 7 comment · 9 complexity · 50aca3eb6989555570587bfdc83c694b MD5 · raw file

  1. /* (C) Guenter Geiger <geiger@epy.co.at> */
  2. #include <m_pd.h>
  3. #include <string.h>
  4. #define DEBUG(x)
  5. /*#define DEBUG(x) x*/
  6. static t_class *serial_bird_class;
  7. #define BIRD_DATA_START 0x80
  8. #define BIRDCMD_MODE_POS 86
  9. #define BIRD_BYTES_POS 6
  10. #define BIRDCMD_MODE_POSANG 89
  11. #define BIRD_BYTES_POSANG 12
  12. #define BIRDCMD_MODE_POSMAT 90
  13. #define BIRD_BYTES_POSMAT 24
  14. #define BIRDCMD_MODE_POSQUAT 93
  15. #define BIRD_BYTES_POSQUAT 14
  16. #define BIRDCMD_MODE_QUAT 92
  17. #define BIRD_BYTES_QUAT 8
  18. #define BIRDCMD_STREAM 64
  19. #define BIRDCMD_POINT 66
  20. #define BIRD_GETDATA(x,y) ((float)((short)(y<<9 | x<<2)))
  21. #define MAXBUFFER 32
  22. typedef struct _serial_bird
  23. {
  24. t_object x_obj;
  25. char x_c[MAXBUFFER];
  26. t_int x_dataformat;
  27. t_int x_maxcount;
  28. t_int x_count;
  29. t_float x_posx;
  30. t_float x_posy;
  31. t_float x_posz;
  32. t_outlet *x_out2;
  33. } t_serial_bird;
  34. static void serial_bird_reset( t_serial_bird* x)
  35. {
  36. x->x_posx=0;
  37. x->x_posy=0;
  38. x->x_count = 0;
  39. outlet_float(x->x_obj.ob_outlet, x->x_posx);
  40. outlet_float(x->x_out2, x->x_posy);
  41. }
  42. static void serial_bird_float( t_serial_bird* x,t_floatarg f)
  43. {
  44. unsigned char c = (unsigned char) f;
  45. t_atom at[BIRD_BYTES_POSMAT];
  46. t_int ac = 0;
  47. if (c&BIRD_DATA_START) {
  48. x->x_count=0;
  49. x->x_c[x->x_count] = c & 0x7f;
  50. }
  51. else
  52. x->x_c[x->x_count] = c;
  53. DEBUG(post("data %d in = %x, start = %d",x->x_count,c,c&BIRD_DATA_START);)
  54. if (x->x_count == x->x_maxcount-1) {
  55. switch (x->x_dataformat) {
  56. case BIRDCMD_MODE_POS:
  57. ac = 3;
  58. SETFLOAT(&at[0], 0.25*BIRD_GETDATA(x->x_c[0],x->x_c[1]));
  59. SETFLOAT(&at[1], 0.25*BIRD_GETDATA(x->x_c[2],x->x_c[3]));
  60. SETFLOAT(&at[2], 0.25*BIRD_GETDATA(x->x_c[4],x->x_c[5]));
  61. break;
  62. case BIRDCMD_MODE_POSANG:
  63. ac = 6;
  64. SETFLOAT(&at[0], 0.25*BIRD_GETDATA(x->x_c[0],x->x_c[1]));
  65. SETFLOAT(&at[1], 0.25*BIRD_GETDATA(x->x_c[2],x->x_c[3]));
  66. SETFLOAT(&at[2], 0.25*BIRD_GETDATA(x->x_c[4],x->x_c[5]));
  67. SETFLOAT(&at[3], BIRD_GETDATA(x->x_c[6],x->x_c[7]));
  68. SETFLOAT(&at[4], BIRD_GETDATA(x->x_c[8],x->x_c[9]));
  69. SETFLOAT(&at[5], BIRD_GETDATA(x->x_c[10],x->x_c[11]));
  70. break;
  71. case BIRDCMD_MODE_POSMAT:
  72. ac = 12;
  73. SETFLOAT(&at[0], BIRD_GETDATA(x->x_c[0],x->x_c[1]));
  74. SETFLOAT(&at[1], BIRD_GETDATA(x->x_c[2],x->x_c[3]));
  75. SETFLOAT(&at[2], BIRD_GETDATA(x->x_c[4],x->x_c[5]));
  76. SETFLOAT(&at[3], BIRD_GETDATA(x->x_c[6],x->x_c[7]));
  77. SETFLOAT(&at[4], BIRD_GETDATA(x->x_c[8],x->x_c[9]));
  78. SETFLOAT(&at[5], BIRD_GETDATA(x->x_c[10],x->x_c[11]));
  79. SETFLOAT(&at[6], BIRD_GETDATA(x->x_c[12],x->x_c[13]));
  80. SETFLOAT(&at[7], BIRD_GETDATA(x->x_c[14],x->x_c[15]));
  81. SETFLOAT(&at[8], BIRD_GETDATA(x->x_c[16],x->x_c[17]));
  82. SETFLOAT(&at[9], BIRD_GETDATA(x->x_c[18],x->x_c[19]));
  83. SETFLOAT(&at[10], BIRD_GETDATA(x->x_c[20],x->x_c[21]));
  84. SETFLOAT(&at[11], BIRD_GETDATA(x->x_c[22],x->x_c[23]));
  85. break;
  86. case BIRDCMD_MODE_POSQUAT:
  87. ac = 7;
  88. SETFLOAT(&at[0], BIRD_GETDATA(x->x_c[0],x->x_c[1]));
  89. SETFLOAT(&at[1], BIRD_GETDATA(x->x_c[2],x->x_c[3]));
  90. SETFLOAT(&at[2], BIRD_GETDATA(x->x_c[4],x->x_c[5]));
  91. SETFLOAT(&at[3], BIRD_GETDATA(x->x_c[6],x->x_c[7]));
  92. SETFLOAT(&at[4], BIRD_GETDATA(x->x_c[8],x->x_c[9]));
  93. SETFLOAT(&at[5], BIRD_GETDATA(x->x_c[10],x->x_c[11]));
  94. SETFLOAT(&at[6], BIRD_GETDATA(x->x_c[12],x->x_c[13]));
  95. break;
  96. case BIRDCMD_MODE_QUAT:
  97. ac = 4;
  98. SETFLOAT(&at[0], BIRD_GETDATA(x->x_c[0],x->x_c[1]));
  99. SETFLOAT(&at[1], BIRD_GETDATA(x->x_c[2],x->x_c[3]));
  100. SETFLOAT(&at[2], BIRD_GETDATA(x->x_c[4],x->x_c[5]));
  101. SETFLOAT(&at[3], BIRD_GETDATA(x->x_c[6],x->x_c[7]));
  102. break;
  103. }
  104. /* post("posx %d, posy %d",x->x_posx,x->x_posy);*/
  105. outlet_list(x->x_obj.ob_outlet,&s_list, ac, at);
  106. }
  107. x->x_count = (++x->x_count)%(x->x_maxcount);
  108. }
  109. static void serial_bird_poll( t_serial_bird* x)
  110. {
  111. post("poll");
  112. /* outlet_float(x->x_out2,(float)x->x_dataformat);*/
  113. outlet_float(x->x_out2,(float)BIRDCMD_POINT);
  114. }
  115. static void serial_bird_mode( t_serial_bird* x,t_symbol* s)
  116. {
  117. post("mode");
  118. /* outlet_float(x->x_out2,(float)x->x_dataformat);*/
  119. if (!strcmp(s->s_name,"position")) {
  120. x->x_dataformat = BIRDCMD_MODE_POS;
  121. x->x_maxcount = BIRD_BYTES_POS;
  122. }
  123. if (!strcmp(s->s_name,"positionangle")) {
  124. x->x_dataformat = BIRDCMD_MODE_POSANG;
  125. x->x_maxcount = BIRD_BYTES_POSANG;
  126. }
  127. if (!strcmp(s->s_name,"positionmatrix")) {
  128. x->x_dataformat = BIRDCMD_MODE_POSMAT;
  129. x->x_maxcount = BIRD_BYTES_POSMAT;
  130. }
  131. if (!strcmp(s->s_name,"positionquat")) {
  132. x->x_dataformat = BIRDCMD_MODE_POSQUAT;
  133. x->x_maxcount = BIRD_BYTES_POSQUAT;
  134. }
  135. if (!strcmp(s->s_name,"quaternion")) {
  136. x->x_dataformat = BIRDCMD_MODE_QUAT;
  137. x->x_maxcount = BIRD_BYTES_QUAT;
  138. }
  139. outlet_float(x->x_out2,(float)x->x_dataformat);
  140. }
  141. static void serial_bird_init( t_serial_bird* x)
  142. {
  143. t_atom cmd[8];
  144. SETFLOAT(cmd,14400.);
  145. outlet_anything(x->x_out2,gensym("speed"),1,cmd);
  146. SETFLOAT(cmd,0.);
  147. SETSYMBOL(cmd+1,gensym("CLOCAL"));
  148. SETSYMBOL(cmd+2,gensym("CREAD"));
  149. SETSYMBOL(cmd+3,gensym("CS8"));
  150. outlet_anything(x->x_out2,gensym("setcontrol"),4,cmd);
  151. SETFLOAT(cmd,0.);
  152. SETSYMBOL(cmd+1,gensym("IXOFF"));
  153. outlet_anything(x->x_out2,gensym("setinput"),2,cmd);
  154. SETFLOAT(cmd,0.);
  155. outlet_anything(x->x_out2,gensym("setlocal"),1,cmd);
  156. SETFLOAT(cmd,0.);
  157. SETFLOAT(cmd+1,20.);
  158. outlet_anything(x->x_out2,gensym("vtime"),2,cmd);
  159. SETSYMBOL(cmd,gensym("RTS"));
  160. SETFLOAT(cmd+1,0.);
  161. outlet_anything(x->x_out2,gensym("setlines"),2,cmd);
  162. SETSYMBOL(cmd,gensym("DTR"));
  163. SETFLOAT(cmd+1,1.);
  164. outlet_anything(x->x_out2,gensym("setlines"),2,cmd);
  165. /* start the polling on the serial device immediately */
  166. outlet_anything(x->x_out2,gensym("start"),0,cmd);
  167. }
  168. static void serial_bird_start( t_serial_bird* x)
  169. {
  170. post("start");
  171. /* outlet_float(x->x_out2,(float)x->x_dataformat);*/
  172. outlet_float(x->x_out2,(float)BIRDCMD_STREAM);
  173. }
  174. static void serial_bird_stop( t_serial_bird* x)
  175. {
  176. post("stop");
  177. outlet_float(x->x_out2,(float)BIRDCMD_POINT);
  178. }
  179. static void *serial_bird_new(t_symbol *s)
  180. {
  181. t_serial_bird *x = (t_serial_bird *)pd_new(serial_bird_class);
  182. x->x_count = 0;
  183. x->x_posx = 0;
  184. x->x_posy = 0;
  185. x->x_dataformat = BIRDCMD_MODE_POSANG;
  186. x->x_maxcount = BIRD_BYTES_POSANG;
  187. outlet_new(&x->x_obj, &s_float);
  188. x->x_out2 = outlet_new(&x->x_obj, &s_float);
  189. return x;
  190. }
  191. void serial_bird_setup(void)
  192. {
  193. serial_bird_class = class_new(gensym("serial_bird"), (t_newmethod)serial_bird_new,
  194. NULL,
  195. sizeof(t_serial_bird), 0,A_DEFSYM,0);
  196. class_addfloat(serial_bird_class,serial_bird_float);
  197. class_addmethod(serial_bird_class,(t_method) serial_bird_reset,gensym("reset"),0);
  198. class_addmethod(serial_bird_class,(t_method) serial_bird_init,gensym("init"),0);
  199. class_addmethod(serial_bird_class,(t_method) serial_bird_start,gensym("start"),0);
  200. class_addmethod(serial_bird_class,(t_method) serial_bird_stop,gensym("stop"),0);
  201. class_addmethod(serial_bird_class,(t_method) serial_bird_poll,gensym("poll"),0);
  202. class_addmethod(serial_bird_class,(t_method) serial_bird_mode,gensym("mode"),A_SYMBOL,NULL);
  203. }