/*
 * Copyright:      Fabian Maximilian Thiele  mailto:me@apetech.de
 * Author:         Fabian Maximilian Thiele
 * Remarks:        this Copyright must be included
 * known Problems: none
 * Version:        1.1
 * Description:    Graphic Library for KS0108- (and compatible) based LCDs
 * 
 */

#include "stm32f10x.h"
#include "STM32vldiscovery.h"
#include "font.h"							  

#ifndef	KS0108_H
#define KS0108_H

#define 	PGM_P   const char*
#define PSTR(s)	   ((const char *)(s))
//V AVR se používají funkce pro ukládání do flash
//Toto předefinování umožní převzít funkce v původním zápise

// Připojení displeje k MCU
#define RESport  GPIOC
#define RESpin	8
//RESET na pinu PA6
#define RWport	 GPIOC
#define RWpin	4
// R/W na pinu PC4
#define DIport	 GPIOA
#define DIpin	7
// D/I na pinu PA7
#define ENport	 GPIOC
#define ENpin	5
// EN na pinu PC5
#define CSport	GPIOC
//  CS1 je na PC6, CS2 na PC7
#define SELCHIP1	 ((1<<6)|(1<<(7+16)))
//vybere CS1 a zaroven vynuluje CS2
#define SELCHIP2	 ((1<<7)|(1<<(6+16)))
//vybere CS2 a zaroven vynuluje CS1


// Chips
#define CHIP1				0x00
#define CHIP2				0x01
#define RIGHT		1
#define LEFT		0

// Commands 
#define LCD_ON				0x3F
#define LCD_OFF				0x3E
#define LCD_SET_ADD			0x40
#define LCD_SET_PAGE		0xB8
#define LCD_DISP_START		0xC0
#define X_ADRESS	0xB8	/* Adress base for Page 0 */
#define Y_ADRESS	0x40	/* Adress base for Y0	  */
#define START_LINE	0xC0	/* Adress base for line 0 */
#define DISPLAY_ON	0x3F	/* Turn display on	  */
#define DISPLAY_OFF	0x3E	/* Turn display off	  */


// InitStatus
#define NON_INVERTED    0x00
#define INVERTED        0x01

// Colors
#define BLACK				0xFF
#define WHITE				0x00


// Font Indices
#define FONT_LENGTH			0
#define FONT_FIXED_WIDTH	2
#define FONT_HEIGHT			3
#define FONT_FIRST_CHAR		4
#define FONT_CHAR_COUNT		5
#define FONT_WIDTH_TABLE	6

#define LcdInstructionWrite    LCDWriteCommand
#define LcdDataWrite  LCDWriteData

typedef struct {
	uint8_t x;
	uint8_t y;
	uint8_t page;
} lcdCoord;

typedef uint8_t (*LCDFontCallback)(const uint8_t*);


//
// Function Prototypes
//


void LCDSetDot(uint8_t x, uint8_t y, uint8_t color);

void LCDClearScreen(void); 

#define pixel(x, y, color) {LCDSetDot(x, y, color);} //pridano pro kompatibility s lcdavr



// Control Functions
void LCDSetReset(uint8_t value);
void LCDGotoXY(uint8_t x, uint8_t y);
void LCDInit(void);
uint8_t LCDReadData(void);
void LCDWriteCommand(uint8_t cmd, uint8_t chip);
void LCDWriteData(uint8_t data);

// Font Functions
uint8_t LCDReadFontData(const uint8_t* ptr);		//Standard Read Callback
void LCDSelectFont(const uint8_t* font, LCDFontCallback callback, uint8_t color);
int LCDPutChar(unsigned char c);
void LCDPuts(char* str);
void LCDPuts_P(PGM_P str);
uint8_t LCDCharWidth(char c);
uint16_t LCDStringWidth(char* str);
uint16_t LCDStringWidth_P(PGM_P str);



void LCD_Printf (uint8_t *au8Text, FONT_DEF *fonttype);
void LcdPutchar (uint8_t u8Char, FONT_DEF *fonttype);
void LCD_Setpos (uint8_t u8Column,uint8_t u8Line); 

void bargraf(uint8_t xline,uint8_t start, uint8_t stop, uint8_t value);

#endif
