/src/nabserv/choreographie.php

https://github.com/mkweb/NabServ · PHP · 169 lines · 67 code · 29 blank · 73 comment · 0 complexity · 219dc7adb8281c8aa95618b5eba8e807 MD5 · raw file

  1. <?php
  2. namespace nabserv;
  3. use \base\Logger;
  4. /**
  5. * Class Choreographie
  6. *
  7. * Creating an binary Choreographie-File
  8. *
  9. * @tutorial http://www.cs.uta.fi/hci/spi/jnabserver/documentation/index.html#choreographies
  10. *
  11. * @package nabserv
  12. *
  13. * @author Mario Klug <mario.klug@mk-web.at>
  14. */
  15. class Choreographie {
  16. /**
  17. * Cache-array for hexadecimal representation of chor
  18. * @var Array
  19. */
  20. private $hex = array();
  21. /**
  22. * Directory where chors are saved
  23. * @var String
  24. */
  25. private $dirname;
  26. /**
  27. * Constructor
  28. *
  29. * @access public
  30. *
  31. * @param Int
  32. */
  33. public function __construct($tempo) {
  34. $this->dirname = PATH_FILES . DS . 'chor' . DS . 'choreographies';
  35. $this->addTempo($tempo);
  36. }
  37. /**
  38. * Adds Tempo to result
  39. *
  40. * @access public
  41. *
  42. * @param Int
  43. */
  44. public function addTempo($tempo) {
  45. $tempo = dechex($tempo);
  46. $this->hex[] = '00'; // empty byte
  47. $this->hex[] = '01'; // typeCode
  48. $this->hex[] = $tempo;
  49. }
  50. /**
  51. * Adds LED-Command to chor
  52. *
  53. * @access public
  54. *
  55. * @param Int Led from 0 to 4 (bottom, right, center, left, nose)
  56. * @param String hexadecimal Colorvalue (6-digit)
  57. * @param Int Time to wait after last action
  58. */
  59. public function addLed($led, $color, $wait = 0) {
  60. $led = dechex($led);
  61. $color = str_split(ltrim($color, '#'), 2);
  62. $this->hex[] = dechex($wait); // wait
  63. $this->hex[] = '07'; // typeCode
  64. $this->hex[] = $led;
  65. $this->hex[] = $color[0];
  66. $this->hex[] = $color[1];
  67. $this->hex[] = $color[2];
  68. $this->hex[] = '00'; // empty byte
  69. $this->hex[] = '00'; // empty byte
  70. }
  71. /**
  72. * Adds EAR-Command to chor
  73. *
  74. * @access public
  75. *
  76. * @param Int Ear (0=right,1=left)
  77. * @param Int Position (0 - 180 degree)
  78. * @param Int Direction (0=forward, 1=backward)
  79. * @param Int Time to wait after last action
  80. */
  81. public function addEar($ear, $pos, $dir, $wait = 0) {
  82. $ear = sprintf('%02s', dechex($ear));
  83. $pos = sprintf('%02s', dechex($pos));
  84. $dir = sprintf('%02s', dechex($dir));
  85. $this->hex[] = dechex($wait); // wait
  86. $this->hex[] = '08'; // typeCode
  87. $this->hex[] = sprintf('%02s', $ear);
  88. $this->hex[] = sprintf('%02s', $pos);
  89. $this->hex[] = sprintf('%02s', $dir);
  90. }
  91. /**
  92. * Returns array with hexadecimal representation of chor
  93. *
  94. * @access public
  95. *
  96. * @return Array
  97. */
  98. public function getHex() {
  99. $hex = array();
  100. $hex[] = '00';
  101. $hex[] = '00';
  102. $hex[] = '01';
  103. $hex[] = dechex(count($this->hex));
  104. $hex = array_merge($hex, $this->hex);
  105. $hex[] = '00';
  106. $hex[] = '00';
  107. $hex[] = '00';
  108. $hex[] = '00';
  109. return $hex;
  110. }
  111. /**
  112. * Returns binary representation of chor
  113. *
  114. * @access public
  115. *
  116. * @retrun String
  117. */
  118. public function __toString() {
  119. $string = '';
  120. $hex = $this->getHex();
  121. foreach($hex as $h) {
  122. $string .= chr(hexdec($h));
  123. }
  124. return $string;
  125. }
  126. /**
  127. * Saves chor in default direction
  128. *
  129. * @access public
  130. *
  131. * @param String Filename without .chor
  132. */
  133. public function save($name) {
  134. Logger::debug("saving choregraphie $name");
  135. $fh = fopen($this->dirname . DS . $name . '.chor', 'w');
  136. fputs($fh, $this);
  137. fclose($fh);
  138. }
  139. }
  140. ?>