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.

182 lines
4.9KB

  1. #include <stdio.h>
  2. #include <fcntl.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <linux/joystick.h>
  6. #include <netinet/in.h>
  7. #include "licks_message.h"
  8. int joystick_fd = -1;
  9. struct js_event event;
  10. char *jsdev="/dev/input/js0";
  11. int udp_socket_fd;
  12. struct sockaddr_in localaddr;
  13. struct sockaddr_in remoteaddr;
  14. licks_net_message m;
  15. #define MODE_IDLE 0
  16. #define MODE_TRANSLATE 1
  17. #define MODE_ROTATE 2
  18. #define MODE_WALK 3
  19. int
  20. main(int argc, char **argv) {
  21. int quit=0;
  22. int power=0;
  23. int joy_x,joy_y,joy_z,joy_rz;
  24. int mode;
  25. if((joystick_fd=open(jsdev,O_RDONLY|O_NONBLOCK))<0) {
  26. perror("open");
  27. exit(0);
  28. }
  29. udp_socket_fd=socket(PF_INET,SOCK_DGRAM,0);
  30. if(udp_socket_fd==-1) {
  31. perror("socket");
  32. exit(0);
  33. }
  34. localaddr.sin_family=AF_INET;
  35. localaddr.sin_port=htons(1337);
  36. inet_aton("0.0.0.0",&localaddr.sin_addr);
  37. remoteaddr.sin_family=AF_INET;
  38. remoteaddr.sin_port=htons(1337);
  39. inet_aton("192.168.1.43",&remoteaddr.sin_addr);
  40. if(bind(udp_socket_fd,(struct sockaddr *)&localaddr,sizeof(struct sockaddr_in))) {
  41. perror("bind");
  42. exit(0);
  43. }
  44. joy_x=0; joy_y=0; joy_z=0; joy_rz=0;
  45. mode=MODE_IDLE;
  46. while(!quit) {
  47. while(read(joystick_fd,&event,sizeof(struct js_event))==sizeof(struct js_event)) {
  48. switch(event.type) {
  49. case JS_EVENT_AXIS:
  50. switch(event.number) {
  51. case 0:
  52. joy_x=event.value;
  53. break;
  54. case 1:
  55. joy_y=event.value;
  56. break;
  57. case 2:
  58. joy_z=event.value;
  59. break;
  60. case 3:
  61. joy_rz=event.value;
  62. break;
  63. }
  64. break;
  65. case JS_EVENT_BUTTON:
  66. printf("button %d value %d\n",event.number,event.value);
  67. if(event.value) {
  68. switch(event.number) {
  69. case 6:
  70. mode=MODE_ROTATE;
  71. break;
  72. case 7:
  73. mode=MODE_TRANSLATE;
  74. break;
  75. case 9:
  76. power=1-power;
  77. m.type=MSG_POWER;
  78. m.i=power;
  79. sendto(udp_socket_fd,&m,sizeof(licks_net_message),0,(struct sockaddr *)&remoteaddr,sizeof(struct sockaddr_in));
  80. break;
  81. default:
  82. mode=MODE_WALK;
  83. }
  84. } else {
  85. mode=MODE_WALK;
  86. }
  87. break;
  88. }
  89. }
  90. switch(mode) {
  91. case MODE_TRANSLATE:
  92. m.type=MSG_MOVE_BODY;
  93. if(abs(joy_y)>5000) {
  94. m.move_parameters.v.x=-joy_y/100;
  95. } else {
  96. m.move_parameters.v.x=0;
  97. }
  98. if(abs(joy_x)>5000) {
  99. m.move_parameters.v.y=-joy_x/100;
  100. } else {
  101. m.move_parameters.v.y=0;
  102. }
  103. if(abs(joy_z)>5000) {
  104. m.move_parameters.v.z=joy_z/100;
  105. } else {
  106. m.move_parameters.v.z=0;
  107. }
  108. m.move_parameters.duration=1;
  109. sendto(udp_socket_fd,&m,sizeof(licks_net_message),0,(struct sockaddr *)&remoteaddr,sizeof(struct sockaddr_in));
  110. break;
  111. case MODE_ROTATE:
  112. m.type=MSG_ROTATE_BODY;
  113. if(abs(joy_x)>5000) {
  114. m.move_parameters.v.x=-joy_x/100;
  115. } else {
  116. m.move_parameters.v.x=0;
  117. }
  118. if(abs(joy_y)>5000) {
  119. m.move_parameters.v.y=joy_y/100;
  120. } else {
  121. m.move_parameters.v.y=0;
  122. }
  123. if(abs(joy_rz)>5000) {
  124. m.move_parameters.v.z=-joy_rz/100;
  125. } else {
  126. m.move_parameters.v.z=0;
  127. }
  128. m.move_parameters.duration=1;
  129. sendto(udp_socket_fd,&m,sizeof(licks_net_message),0,(struct sockaddr *)&remoteaddr,sizeof(struct sockaddr_in));
  130. break;
  131. case MODE_WALK:
  132. m.type=MSG_WALK;
  133. if(abs(joy_y)>5000) {
  134. m.move_parameters.v.x=-joy_y/2;
  135. } else {
  136. m.move_parameters.v.x=0;
  137. }
  138. if(abs(joy_x)>5000) {
  139. m.move_parameters.v.y=-joy_x/2;
  140. } else {
  141. m.move_parameters.v.y=0;
  142. }
  143. if(abs(joy_rz)>5000) {
  144. m.move_parameters.v.z=joy_rz/327.6;
  145. } else {
  146. m.move_parameters.v.z=0;
  147. }
  148. sendto(udp_socket_fd,&m,sizeof(licks_net_message),0,(struct sockaddr *)&remoteaddr,sizeof(struct sockaddr_in));
  149. break;
  150. }
  151. usleep(10000);
  152. }
  153. close(udp_socket_fd);
  154. close(joystick_fd);
  155. }