ports = array( $this->PORT_RS, $this->PORT_RW, $this->PORT_E, $this->PORT_DB4, $this->PORT_DB5, $this->PORT_DB6, $this->PORT_DB7 ); // GPIOポートを出力用として確保 foreach ($this->ports as $pos){ exec("echo ".$pos." > /sys/class/gpio/export"); exec("echo out > /sys/class/gpio/gpio".$pos."/direction"); } // ファイルポインタを取得 $this->rs = fopen("/sys/class/gpio/gpio".$this->PORT_RS."/value", "w"); $this->rw = fopen("/sys/class/gpio/gpio".$this->PORT_RW."/value", "w"); $this->e = fopen("/sys/class/gpio/gpio".$this->PORT_E."/value", "w"); $this->db4 = fopen("/sys/class/gpio/gpio".$this->PORT_DB4."/value", "w"); $this->db5 = fopen("/sys/class/gpio/gpio".$this->PORT_DB5."/value", "w"); $this->db6 = fopen("/sys/class/gpio/gpio".$this->PORT_DB6."/value", "w"); $this->db7 = fopen("/sys/class/gpio/gpio".$this->PORT_DB7."/value", "w"); // 作成したファイルポインタを配列として保存 $this->databits = array ( $this->db7, $this->db6, $this->db5, $this->db4 ); // LCD初期化 fwrite( $this->rs, "0", 1 ); fwrite( $this->rw, "0", 1 ); /************************************************** * function set *************************************************/ // 0010固定(上位4bitのみ送信) $this->output4( 0b00100000 ); // 上位4bit は 0010 固定 // 下位4bitは順に以下 // N: 表示行数(0:1行モード、1:2行モード) // ※1行モードにする場合、move メソッドも変更必要です // F: フォント // FT1, FT0: フォントテーブル(00:英語&日本語、01:西欧言語) $this->output8( 0b00101000 ); /************************************************** * Display ON/OFF Control *************************************************/ // 上位5bitは 00001 固定 // 以降、 // D: Sets entire Display // C: Sets Cursor // B: Sets Blinking $this->output8( 0b00001100 ); /************************************************** * Display Clear *************************************************/ // 固定 $this->output8( 0b00000001 ); /************************************************** * Entry Mode Set *************************************************/ // 上位6bitは 00001 固定 // 以降、 // I/D: 1: Increment、0: Decrement // SH : Accompanies display shift $this->output8( 0b0000110 ); } /** * 終了(デストラクタ) */ public function __destruct() { // 画面クリア @$this->output8( 0b00000001 ); // ファイルクローズ @fclose($this->rs); @fclose($this->rw); @fclose($this->e); @fclose($this->db4); @fclose($this->db5); @fclose($this->db6); @fclose($this->db7); // GPIOポートを開放 foreach ($this->ports as $pos){ exec("echo ".$pos." > /sys/class/gpio/unexport"); } } /** * カーソル移動 * @param $line 行(0:1行目、1:2行目) * @param $pos 列(0:1文字目、1:2文字目、…) */ public function movePos($line, $pos){ $data = 0b10000000; // set DDRAM Address モード // 2行目なら、アドレスに 0x40 を足す。 if( $line != 0 ) $data |= 0x40; // 列位置指定 $data |= $pos; $this->output8($data); } /** * 文字出力 * @param $str 出力する文字 */ public function printData($str){ fwrite( $this->rs, "1", 1 ); // Write Dataモード // 先頭から順に文字を出力 for($i = 0; $i < strlen($str); $i++){ $data = substr($str, $i, 1); $this->output8(ord($data)); // 1文字出力 } fwrite( $this->rs, "0", 1 ); } /** * char出力 * @param $str 出力する1文字 */ public function printChar($data){ fwrite( $this->rs, "1", 1 ); // Write Dataモード $this->output8($data); // 1文字出力 fwrite( $this->rs, "0", 1 ); } /** * 文字出力(出力位置指定可能) * @param $str 出力する文字 * @param $line 行(0:1行目、1:2行目) * @param $pos 列(0:1文字目、1:2文字目、…) */ public function printPosData($str, $line, $pos){ $this->movePos($line, $pos); $this->printData($str); } /** * 画面クリア(ホームに戻る) */ public function clearDisplay(){ $this->output8( 0b00000001 ); $this->output8( 0b00000010 ); } /** * 4bit データ出力 * @param $data 出力するデータ(上位4bitのみ有効) */ private function output4($data){ foreach ($this->databits as $pos){ // 8bit目を出力 fwrite( $pos, ($data & 0b10000000) != 0 ? "1" : "0", 1 ); $data <<= 1; // ビットシフト } // LCD にデータを読ませる。( E を High から Low に変化させる ) fwrite( $this->e, "1", 1 ); usleep(250); // 250ns待機 fwrite( $this->e, "0", 1 ); // Busy信号待ち //$this->waitBusy(); usleep(250); // 250ns待機 } /** * 8bit データ出力 * @param $data 出力するデータ(下位8bitの数値) */ private function output8($data){ $this->output4($data); // 上位4bit分 $data <<= 4; $this->output4($data); // 下位4bit分 } /** * Busy 信号待ち * ※現在は、信号が受け取れないため未使用...。※ */ private function waitBusy(){ // DB7 を読み込みに変更 exec("echo in > /sys/class/gpio/gpio".$this->PORT_DB7."/direction"); // Busy受け取り fwrite( $this->rw, "1", 1 ); // Busy信号が "0" になるまで待機 while(1){ $ret = exec("cat /sys/class/gpio/gpio".$this->PORT_DB7."/value", $output); if( $ret === "0" ) break; usleep(60); // 60マイクロ秒待機 }; // Busy受け取り終了 fwrite( $this->rw, "0", 1 ); // DB7 を書き込みに戻す exec("echo out > /sys/class/gpio/gpio".$this->PORT_DB7."/direction"); } } ?>