Created
November 1, 2011 14:12
-
-
Save mitaken/1330588 to your computer and use it in GitHub Desktop.
Create Toshiba W55T LED Pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * W55TのLEDパターンを作るやーつ | |
| * | |
| * @cppyright mitaken | |
| */ | |
| class LED | |
| { | |
| /** | |
| * ファイル名 | |
| * @var string | |
| */ | |
| public $file; | |
| /** | |
| * ポイントリスト | |
| * @var array | |
| */ | |
| public $point = array(); | |
| /** | |
| * 標準の点灯時間 | |
| * @var int | |
| */ | |
| private $_defaultTime = 1; | |
| /** | |
| * 標準の明るさ | |
| * @var int | |
| */ | |
| private $_defaultBright = 1; | |
| /** | |
| * LEDシンボル | |
| * @var string | |
| */ | |
| const LED_SYMBOL = "DMT\x00K31\x00\x18\x00\x00\x00"; | |
| /** | |
| * Constructor | |
| * | |
| * @param string $file | |
| * @param boolean $load | |
| * @return void | |
| */ | |
| public function __construct($file, $load = false) | |
| { | |
| $this->file = $file; | |
| if ($load) { | |
| $this->_loadPoint(); | |
| } | |
| } | |
| /** | |
| * 標準の点灯時間の取得 | |
| * | |
| * @return int | |
| */ | |
| public function getDefaultTime() | |
| { | |
| return $this->_defaultTime; | |
| } | |
| /** | |
| * 標準の点灯時間の設定 | |
| * | |
| * @param int $time | |
| * @return boolean | |
| */ | |
| public function setDefaultTime($time) | |
| { | |
| if (!ctype_digit((string)$time) || $time == 0) { | |
| throw new Exception('不正な点灯時間です'); | |
| } | |
| $this->_defaultTime = (int)$time; | |
| return true; | |
| } | |
| /** | |
| * 標準の明るさの取得 | |
| * | |
| * @return int | |
| */ | |
| public function getDefaultBright() | |
| { | |
| return $this->_defaultBright; | |
| } | |
| /** | |
| * 標準の明るさの設定(1-4) | |
| * | |
| * @param int $bright | |
| * @return boolean | |
| */ | |
| public function setDefaultBright($bright) | |
| { | |
| if (!ctype_digit((string)$bright) || $bright == 0 || $bright > 4) { | |
| throw new Exception('不正な明るさです'); | |
| } | |
| $this->_defaultBright = (int)$bright; | |
| return true; | |
| } | |
| /** | |
| * ポイントの取得 | |
| * | |
| * @return array | |
| */ | |
| public function getPoint() | |
| { | |
| return $this->point; | |
| } | |
| /** | |
| * ポイントの設定 | |
| * | |
| * @param string $no | |
| * @param string $time | |
| * @param string $bright | |
| * @return boolean | |
| */ | |
| public function setPoint($no, $time = null, $bright = null) | |
| { | |
| $this->point = array(); | |
| $this->addPoint($no, $time, $bright); | |
| return true; | |
| } | |
| /** | |
| * ポイントの追加 | |
| * | |
| * @param int $no | |
| * @param int $time | |
| * @param int $bright | |
| * @param int $index | |
| * @return boolean | |
| */ | |
| public function addPoint($no, $time = null, $bright = null, $index = null) | |
| { | |
| if (!ctype_digit((string)$no) || $no < 33) { | |
| throw new Exception('不正な番号です'); | |
| } | |
| if (!is_null($time) && (!ctype_digit((string)$time) || $time == 0)) { | |
| throw new Exception('不正な点灯時間です'); | |
| } | |
| if (!is_null($bright) && (!ctype_digit((string)$bright) || $bright == 0 || $bright > 4)) { | |
| throw new Exception('不正な明るさです'); | |
| } | |
| $data = array($no, is_null($time) ? $this->getDefaultTime() : $time, is_null($bright) ? $this->getDefaultBright() : $bright); | |
| if (is_null($index) && ctype_digit((string)$index)) { | |
| $this->point[$index] = $data; | |
| } else { | |
| $this->point[] = $data; | |
| } | |
| return true; | |
| } | |
| /** | |
| * LEDファイル作成 | |
| * | |
| * @param string $file | |
| * @return boolean | |
| */ | |
| public function create($file = null) | |
| { | |
| if (($count = count($this->point)) === 0) { | |
| throw new Exception('ポイントが設定されていません'); | |
| } | |
| if ($count > 25) {// 25っていうよりも1024byte以上だとエラーになるから | |
| throw new Exception('ポイントは25個まで設定できます'); | |
| } | |
| $led = self::LED_SYMBOL . $this->_dec2bin(0) . $this->_dec2bin(1) . $this->_dec2bin($count);// シンボル+ファイルサイズ+定数+ポイント数 | |
| for ($i = 0;$i < $count;++$i) { | |
| list($no, $time, $bright) = $this->point[$i]; | |
| $led .= $this->_dec2bin($time) | |
| . $this->_dec2bin($bright) | |
| . $this->_dec2bin($no); | |
| } | |
| $led = substr_replace($led, $this->_dec2bin(strlen($led)), 12, 4);// ファイルサイズ書き込み | |
| return @file_put_contents(is_null($file) ? $this->file : $file, $led); | |
| } | |
| /** | |
| * ポイントの読み込み | |
| * | |
| * @return boolean | |
| */ | |
| private function _loadPoint() | |
| { | |
| if (!is_file($this->file)) { | |
| throw new Exception('ファイルを読み込めませんでした'); | |
| } | |
| $file = file_get_contents($this->file); | |
| $length = strlen($file); | |
| /** | |
| * ヘッダーチェック | |
| */ | |
| $header = self::LED_SYMBOL . $this->_dec2bin($length) . $this->_dec2bin(1); | |
| if (strpos($file, $header) !== 0) { | |
| throw new Exception('LEDファイルではありません'); | |
| } | |
| /** | |
| * ポイントチェック | |
| */ | |
| $point = $this->_bin2dec(substr($file, 20, 4)); | |
| if ($point !== ($length - 24) / 12) { | |
| throw new Exception('ポイント数が不正です'); | |
| } | |
| for ($i = 0;$i < $point;++$i) { | |
| $index = $i * 12 + 20; | |
| $time = $this->_bin2dec(substr($file, $index, 4)); | |
| $bright = $this->_bin2dec(substr($file, $index + 4, 4)); | |
| $no = $this->_bin2dec(substr($file, $index + 8, 4)); | |
| $this->addPoint($no, $time, $bright); | |
| } | |
| return true; | |
| } | |
| /** | |
| * 10進数からバイナリ | |
| * | |
| * @param int $decimal | |
| * @return string | |
| */ | |
| private function _dec2bin($decimal) | |
| { | |
| return pack('V', $decimal); | |
| } | |
| /** | |
| * バイナリから10進数 | |
| * | |
| * @param string $binary | |
| * @return int | |
| */ | |
| private function _bin2dec($binary) | |
| { | |
| return end(unpack('V', $binary)); | |
| } | |
| } | |
| /** | |
| * Sample | |
| */ | |
| $str = 'PRAYforJAPAN!'; | |
| $str = str_split($str); | |
| $led = new LED('test.lpt'); | |
| $led->setDefaultTime(1); | |
| $led->setDefaultBright(4); | |
| for ($i = 0;$i < count($str);++$i) { | |
| $led->addPoint(ord($str[$i]), 20); | |
| } | |
| $led->create(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment