<?php

/*
 * Copyright (C) 2014, NoccyLabs
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 */

namespace NoccyLabs\Gpio\Device\Display;

use NoccyLabs\Gpio\Device\Device;

class Pcd8544Device extends Device
{
    protected function configure()
    {
        $this
            ->setName("pcd8544")
            ->setDescription("Philips PCD8544 LCD Display Driver")
            ->addPin("dc", "data/command")
            ->addPin("sce", "chip select")
            ->addPin("scl", "clock")
            ->addPin("sda", "data")
            ->addPin("res", "reset")
            ->addPin("bl", "backlight")
            ;
    }
    
    protected function startup()
    {
        
    }
    
    public function setBacklightState($state)
    {
        $this->bl->setValue((bool)$state);
    }
    
    public function initialize()
    {
        $this->lcdInit();
    }
    
    public function lcdSend($byte, $command=false)
    {
        // assume clk is hi
        // Enable display controller (active low).
        $this->sce->setValue(0);

        $this->dc->setValue((int)$command); // command or data

        $this->shiftOut($this->sda, $this->scl, $byte);

        // Disable display controller.
        $this->sce->setValue(1);

/*
        if (!$command) {
	        $this->cx++;
	        if ($this->cx > (LCD_X_RES - 1)) {
		        cx = 0; cy++;
		        #ifdef PCD8544_FIX_YALIGN
		        // Soft wrapping when FIX_YALIGN is defined
		        lcd_cursor(cx,cy);
		        #endif
	        }
        }
*/       
    }
    
    public function lcdInit()
    {
        $this->sce->setDirection("out");
        $this->res->setDirection("out");
        $this->dc->setDirection("out");
        $this->scl->setDirection("out");
        $this->sda->setDirection("out");
        
        $this->res->setValue(1); // set RES
        $this->sce->setValue(0); // reset SCE
        $this->res->setValue(0); // pull RES low
        $this->res->setValue(0); // and back hick


        // Send sequence of command
        $this->lcdSend( 0x21, true );  // LCD Extended Commands.
        // lcd_send( 0xC8, true );  // Set LCD Vop (Contrast).
        $this->lcdSend( 0x80 | 0x70, true );  // Set LCD Vop (Contrast).
        $this->lcdSend( 0x06, true );  // Set Temp coefficent to 2.
        $this->lcdSend( 0x13, true );  // LCD bias mode 1:100.
        #ifdef PCD8544_FIX_YALIGN
        $this->lcdSend( 0x45, true );  // LCD blank - Shift LCD 5 up (row starts at 1)
        #endif
        $this->lcdSend( 0x20, true );  // LCD Standard Commands, Horizontal addressing mode.
        $this->lcdSend( 0x40, true );  // LCD blank
        $this->lcdSend( 0x08, true );  // LCD blank
        $this->lcdSend( 0x0C, true );  // LCD in inverse mode.

        //$this->lcdClear();
    
    }
    
}