Visca.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //implementation following this protocol : https://www.epiphan.com/userguides/LUMiO12x/Content/UserGuides/PTZ/3-operation/VISCAcommands.htm#InquiryCommands
  2. // var parameterPath = local.parameters;
  3. // var valuesPath = local.values;
  4. var ZoomPos = script.addFloatParameter("Zoom Position", "", 0., 0., 1.);
  5. // var state =
  6. var inquiry = ['CAM_ZoomPosInq', 'CAM_FocusModeInq', 'CAM_FocusPosInq'];
  7. var camID = 1;
  8. var waitTaskComplete = 0 ;
  9. var currentInquiry = '';
  10. /////////// HELPERS FUNCTIONS ////////////////
  11. function printArray(name, array){
  12. var printHeader = ' '+name+' : ';
  13. if (array.length){for(var i=0; i<array.length; i++){printHeader+=array[i] + ' ';}}
  14. script.log(printHeader);
  15. }
  16. /////////// VISCA FUNCTIONS ////////////////
  17. function setZoomPos(ID, position){
  18. var header = [1, 4, 71];
  19. var posCmd = [];
  20. var pos = Math.round(Math.floor(16384 * position));
  21. // script.log(pos);
  22. posCmd[3]=pos%16;
  23. pos = Math.round(Math.floor(pos/16));
  24. posCmd[2]=pos%16;
  25. pos=Math.round(Math.floor(pos/16));
  26. posCmd[1]=pos%16;
  27. posCmd[0]=Math.round(Math.floor(pos/16));
  28. // local.sendBytes(idHex, 1, 4, 71, posCommand, 255);
  29. sendHeaderCommand(camID, header, posCmd);
  30. }
  31. function setZoomSpeed(ID, speed){
  32. var header = [1, 4, 7];
  33. var speedCmd = speed < 0 ? 48 : 32;
  34. speedCmd = speed == 0 ? 0 : speedCmd ;
  35. speedCmd += Math.abs(speed);
  36. sendHeaderCommand(camID, header, speedCmd);
  37. }
  38. function sendInquiry(id, inquiryName){
  39. var command ;
  40. if (currentInquiry == ''){ //no inquiry running;
  41. currentInquiry = inquiryName ;
  42. if(inquiryName == 'CAM_ZoomPosInq'){ command = [9, 4, 71];}
  43. sendCommand(camID, command);
  44. }
  45. else{script.log("inquiry running");}
  46. }
  47. function sendCommand(id, command){
  48. var idHex = (parseInt("0x80") + id);
  49. printArray('command', command);
  50. local.sendBytes(idHex, command, 255);
  51. }
  52. function sendHeaderCommand(id, header, command){
  53. if (!waitTaskComplete){
  54. var idHex = (parseInt("0x80") + id);
  55. printArray('header', header);
  56. printArray('command', command);
  57. local.sendBytes(idHex, header, command, 255);
  58. waitTaskComplete = 1;
  59. }
  60. else { script.log('task not complete');}
  61. }
  62. /////////// SCRIPT FUNCTIONS ////////////////
  63. function init() {
  64. //loadFixtures();
  65. script.log("VISCA module loaded");
  66. script.setUpdateRate(100);
  67. }
  68. function update(deltatTime){
  69. if(!waitTaskComplete && (currentInquiry!=''))
  70. {
  71. sendInquiry(1, 'CAM_ZoomPosInq');
  72. }
  73. }
  74. var buffer=[];
  75. var bufferIndex = 0 ;
  76. function dataReceived(data)
  77. {
  78. //If mode is "Lines", you can expect data to be a single line String
  79. // script.log("Data received : " +data);
  80. //If mode is anything else, you can expect data to be an array of bytes
  81. // script.log(" Bytes received : "+data.length);
  82. // for(var i=0; i < data.length; i++)
  83. // {
  84. // script.log(" > " + parseInt((""+data[i]),16));
  85. // }
  86. //fill buffer with bytes until you find a 255
  87. for (var i = 0 ; i<data.length ; i++){
  88. if (data[i]!=255){
  89. buffer[bufferIndex]=data[i];
  90. bufferIndex += 1;
  91. }
  92. else{
  93. if(buffer[0]==144){ //0x90, you've got a message
  94. if (buffer[1]==65){script.log("received command");} //0x41
  95. else if (buffer[1]==81){script.log("complete command");waitTaskComplete=0;} //0x51
  96. else if (buffer[1]==96){script.log("syntax error");} //0x60
  97. else if (buffer[1]==97){//0x61
  98. var msg = "error :";
  99. if(buffer[2]==1){msg=msg+"message length";}
  100. else if(buffer[2]==2){msg=msg+"syntax";}
  101. else if(buffer[2]==3){msg=msg+"command buffer full";}
  102. else if(buffer[2]==4){msg=msg+"command cancelled";}
  103. else if(buffer[2]==5){msg=msg+"no socket";}
  104. else if(buffer[2]==65){msg=msg+"command not executable";}
  105. else{
  106. msg=msg+"unknow error";
  107. for(var j=2; j < buffer.length; j++)
  108. {
  109. script.log(" > " +data[i]);
  110. }
  111. }
  112. script.log(msg);
  113. }
  114. //inquiry answers
  115. else if (buffer[1]==80){//0x50
  116. if(currentInquiry == 'CAM_ZoomPosInq'){
  117. var position = buffer[2]*4096 + buffer[3]*256 + buffer[4]*16 + buffer[5];
  118. printArray("zoom position :", buffer);
  119. script.log(position);
  120. ZoomPos.set(position/16384.);
  121. }
  122. //free inquiry slot
  123. currentInquiry = '';
  124. }
  125. else{printArray("unknown answer :", buffer);}
  126. }
  127. bufferIndex = 0;
  128. buffer=[];
  129. }
  130. }
  131. //then process buffer
  132. }