Visca.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // var parameterPath = local.parameters;
  2. // var valuesPath = local.values;
  3. function init() {
  4. //loadFixtures();
  5. script.log("hello VISCA");
  6. }
  7. function setZoom(ID, position){
  8. var idHex = (parseInt("0x80") + ID);
  9. var pos = Math.round(4000 * position);
  10. var posString = ""+pos;
  11. var pad = "0000";
  12. posString = pad.substring(0, pad.length - posString.length) + posString;
  13. script.log(posString);
  14. // script.log(posString.charAt(1));
  15. var posCommand = [parseInt("0x0"+posString.charAt(0)),
  16. parseInt("0x0"+posString.charAt(1)),
  17. parseInt("0x0"+posString.charAt(2)),
  18. parseInt("0x0"+posString.charAt(3))
  19. ];
  20. // script.log(posCommand[0]);
  21. // script.log(posCommand[1]);
  22. // script.log(posCommand[2]);
  23. // script.log(posCommand[3]);
  24. // var packet = [idHex, , 00, 00, 00, 00, "ff"]
  25. // local.send(String.fromCharCode.apply(null, posCommand));
  26. // local.sendBytes(129, 1, 4, 7, 48, 255);
  27. // local.send()
  28. local.sendBytes(idHex, 1, 4, 71,
  29. posCommand, 255);
  30. }
  31. var buffer=[];
  32. var bufferIndex = 0 ;
  33. function dataReceived(data)
  34. {
  35. //If mode is "Lines", you can expect data to be a single line String
  36. // script.log("Data received : " +data);
  37. //If mode is anything else, you can expect data to be an array of bytes
  38. // script.log(" Bytes received : "+data.length);
  39. // for(var i=0; i < data.length; i++)
  40. // {
  41. // script.log(" > " + parseInt((""+data[i]),16));
  42. // }
  43. //fill buffer with bytes until you find a 255
  44. for (var i = 0 ; i<data.length ; i++){
  45. if (data[i]!=255){
  46. buffer[bufferIndex]=data[i];
  47. bufferIndex += 1;
  48. }
  49. else{
  50. if(buffer[0]==144){ //0x90, you've got a message
  51. if (buffer[1]==65){script.log("received command");} //0x41
  52. if (buffer[1]==81){script.log("complete command");} //0x51
  53. if (buffer[1]==97){//0x61
  54. var msg = "error :";
  55. if(buffer[2]==1){msg=msg+"message length";}
  56. if(buffer[2]==2){msg=msg+"syntax";}
  57. if(buffer[2]==3){msg=msg+"command buffer full";}
  58. if(buffer[2]==4){msg=msg+"command cancelled";}
  59. if(buffer[2]==5){msg=msg+"no socket";}
  60. if(buffer[2]==65){msg=msg+"command not executable";}
  61. else{
  62. msg=msg+"unknow error";
  63. for(var j=2; j < buffer.length; j++)
  64. {
  65. script.log(" > " +data[i]);
  66. }
  67. }
  68. script.log(msg);
  69. }
  70. }
  71. bufferIndex = 0;
  72. buffer=[];
  73. }
  74. }
  75. //then process buffer
  76. }