PageRenderTime 51ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/tests/pages/chor.php

https://github.com/mkweb/NabServ
PHP | 162 lines | 73 code | 42 blank | 47 comment | 7 complexity | ca2ce93e2fcfc02713504ba6b25d6e37 MD5 | raw file
  1. <?php
  2. namespace tests\pages;
  3. use \base;
  4. /**
  5. * @package tests.pages
  6. *
  7. * @author Mario Klug <mario.klug@mk-web.at>
  8. */
  9. class Chor extends base\Page {
  10. public $title = 'Choreographie';
  11. public function process() {
  12. $dirname = PATH_FILES . '/chor/choreographies/';
  13. $chor = $this->request->get('chor');
  14. $files = array();
  15. if(is_null($chor)) {
  16. $this->getFiles($dirname, $files);
  17. } else {
  18. $hex = array();
  19. $file = $dirname . $chor;
  20. $content = file_get_contents($file);
  21. for($i = 0; $i < strlen($content); $i++) {
  22. $char = substr($content, $i, $i + 1);
  23. $hex[] = sprintf("%02s", strtoupper(dechex(ord($char))));
  24. }
  25. $hexbak = $hex;
  26. $parsed = array();
  27. $slice = $this->slice($hex, 4);
  28. $this->pop($hex, 3);
  29. $blocks = array();
  30. while(($block = $this->readBlock($hex)) != array()) {
  31. $blocks[] = $block;
  32. }
  33. pr($blocks);
  34. pr($hexbak);
  35. }
  36. $this->set('files', $files);
  37. }
  38. private function readBlock(&$hex) {
  39. $block = array();
  40. $wait = array_shift($hex);
  41. $type = array_shift($hex);
  42. if(hexdec($type) > 0) {
  43. switch($type) {
  44. case '01': // tempo
  45. $block['type'] = 'tempo';
  46. $data = hexdec(array_shift($hex));
  47. $block['data'] = $data;
  48. break;
  49. case '07': // led
  50. $block['type'] = 'led';
  51. $block['wait'] = hexdec($wait);
  52. $data = $this->slice($hex, 6);
  53. $led = hexdec(array_shift($data));
  54. $block['led'] = $led;
  55. array_pop($data);
  56. array_pop($data);
  57. $block['color'] = join('', $data);
  58. break;
  59. case '08': // ear
  60. $block['type'] = 'ear';
  61. $block['wait'] = hexdec($wait);
  62. $data = $this->slice($hex, 3);
  63. $ear = hexdec(array_shift($data));
  64. $block['ear'] = ($ear == 0 ? 'right' : 'left');
  65. $pos = hexdec(array_shift($data)); // 0 - 18
  66. $block['position'] = $pos;
  67. $direction = hexdec(array_shift($data));
  68. $block['direction'] = ($direction == 0 ? 'forward' : 'backward');
  69. break;
  70. default:
  71. pr("unknown: " . $type);
  72. break;
  73. }
  74. }
  75. return $block;
  76. }
  77. private function getFiles($path, &$result = array()) {
  78. $files = glob(rtrim($path, "/*") . "/*");
  79. foreach($files as $file) {
  80. if(is_dir($file)) {
  81. $this->getFiles($file, $result);
  82. } else {
  83. $result[] = $file;
  84. }
  85. }
  86. }
  87. private function slice(&$arr, $to) {
  88. $return = array_slice($arr, 0, $to);
  89. for($i = 0; $i < $to; $i++) {
  90. array_shift($arr);
  91. }
  92. return $return;
  93. }
  94. private function pop(&$arr, $to) {
  95. $return = array();
  96. for($i = 0; $i < $to; $i++) {
  97. $return[] = array_pop($arr);
  98. }
  99. $return = array_reverse($return);
  100. return $return;
  101. }
  102. }
  103. ?>