/*
    lcd_shift.h
    print and setting functions for a HD44780 compatible display
    Copyright (C) 2011  Marc Heimann <mheimann84@googlemail.com>

    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/>.
*/


#ifndef LCD_SHIFT_H
#define LCD_SHIFT_H

#include <avr/io.h>


// processor output (shift register & display input) //
// pin configuration
#define PIN_CLOCK0b10000000
#define PIN_DATA0b01000000
#define PIN_ENABLE0b00100000

// port configuration
#define PORT_OUT_CLOCKPORTD_OUT
#define PORT_OUT_DATAPORTD_OUT
#define PORT_OUT_ENABLEPORTD_OUT

#define PORT_DIR_CLOCKPORTD_DIR
#define PORT_DIR_DATAPORTD_DIR
#define PORT_DIR_ENABLEPORTD_DIR

// shift register output (display input) //
#define SHIFT_OUT_RS0b00001000
#define SHIFT_OUT_DB0_DB40b00010000
#define SHIFT_OUT_DB1_DB50b00100000
#define SHIFT_OUT_DB2_DB60b01000000
#define SHIFT_OUT_DB3_DB70b10000000


// display instructions //
// HD44780.pdf, p. 11, figure 4
#define CHARACTERS_PER_LINE40
#define LINE_1_ADDR0x00
#define LINE_2_ADDR0x40

#define PIN_DB00b00000001
#define PIN_DB10b00000010
#define PIN_DB20b00000100
#define PIN_DB30b00001000
#define PIN_DB40b00010000 
#define PIN_DB50b00100000
#define PIN_DB60b01000000
#define PIN_DB70b10000000

// HD44780.pdf, p. 24, 25
#define CLEAR_DISPLAYPIN_DB0

#define RETURN_HOMEPIN_DB1

#define ENTRY_MODE_SETPIN_DB2
#define ENTRY_MODE_SET_IDPIN_DB1
#define ENTRY_MODE_SET_SPIN_DB0

#define DISPLAY_ON_OFF_CONTROLPIN_DB3
#define DISPLAY_ON_OFF_CONTROL_DPIN_DB2
#define DISPLAY_ON_OFF_CONTROL_CPIN_DB1
#define DISPLAY_ON_OFF_CONTROL_BPIN_DB0

#define CURSOR_OR_DISPLAY_SHIFTPIN_DB4
#define CURSOR_OR_DISPLAY_SHIFT_SCPIN_DB3
#define CURSOR_OR_DISPLAY_SHIFT_RLPIN_DB2

#define FUNCTION_SETPIN_DB5
#define FUNCTION_SET_DLPIN_DB4
#define FUNCTION_SET_NPIN_DB3
#define FUNCTION_SET_FPIN_DB2

#define SET_DDRAM_ADDRESSPIN_DB7

// composed instructions (not complete, modify or add your own instructions) 
#define SET_4BIT_ONE_LINEFUNCTION_SET
#define SET_4BIT_TWO_LINESFUNCTION_SET | FUNCTION_SET_N
#define SET_DISPLAY_ONDISPLAY_ON_OFF_CONTROL | DISPLAY_ON_OFF_CONTROL_D
#define SET_DISPLAY_ON_WITH_CURSORDISPLAY_ON_OFF_CONTROL | DISPLAY_ON_OFF_CONTROL_D | DISPLAY_ON_OFF_CONTROL_C
#define INCREMENT_CURSORENTRY_MODE_SET | ENTRY_MODE_SET_ID
#define SHIFT_LEFTCURSOR_OR_DISPLAY_SHIFT | CURSOR_OR_DISPLAY_SHIFT_SC
#define SHIFT_RIGHTCURSOR_OR_DISPLAY_SHIFT | CURSOR_OR_DISPLAY_SHIFT_SC | CURSOR_OR_DISPLAY_SHIFT_RL

// function declarations
extern void lcd_enable( void );
extern void shift_byte( uint8_t data_byte );
extern void send_instr( uint8_t data );
extern void send_instruction( uint8_t data, uint16_t usec_delay );
extern void send_data( uint8_t data );
extern void init_lcd( void );
extern void print_lcd( char* buffer, uint8_t addr );

#endif // LCD_SHIFT_H