/*
 * Library zur Ansteuerung von OLEDs mit SSD1306 Chip
 * Nur I²C Anschluss
 *
 *  Created: 19.06.2016 11:38:00
 *  Author: Erwin Endres
 */ 
#include <avr/pgmspace.h>
#include "i2cmaster.h"

#define I2C_ADDR_OLED 0x78	// I²C Adresse Display

//#define FONTSIZE8
#define FONTSIZE16
#define FONTSIZE24
//#define FONTSIZE32
//#define FONTSIZE48
//#define FONTSIZE64

#define  FONTFILE8 "terminal8.h"
#define  FONTFILE16 "noto16.h" //"tahoma16_SSD1306.h"
#define  FONTFILE24 "noto24.h" //"helvetica_narrow24.h"	//tahoma24_SSD1306.h"
#define  FONTFILE32 "noto32.h"
#define  FONTFILE48 "noto48.h" //consolas48_SSD1306.h"			
#define  FONTFILE64 "noto64.h"

#define XRES 128	
#define YRES 64		

//**************************************************
#define EINVIERTEL "\xBC"
#define EINHALB "\xBD"
#define DREIVIERTEL "\xBE"

#define BATT0 0x9A
#define BATT1 0x9B
#define BATT2 0x9C
#define BATT3 0x9D
#define BATT4 0x9E
#define BATT5 0x9F
#define BATT6 0xA0
#define BATT7 0xA1
#define BATT8 0xA2
#define BATT9 0xA3

#define BATTLOAD0 0x8F
#define BATTLOAD1 0x90
#define BATTLOAD2 0x91
#define BATTLOAD3 0x92
#define BATTLOAD4 0x93
#define BATTLOAD5 0x94
#define BATTLOAD6 0x95
#define BATTLOAD7 0x96
#define BATTLOAD8 0x97
#define BATTLOAD9 0x98

// tImage: für font files
typedef struct {
	const uint8_t *data;	//Zeiger auf Array von uint8_t (->Array enthält die Bilddaten des Zeichens)
	const uint8_t width;	//Breite des Zeichens
} tImage;					//Bitmap und Breite eines Zeichens

#ifndef __oled_H
	#define __oled_H

	#include <inttypes.h>
	#include <avr/io.h>
	#include <avr/pgmspace.h>
	#include <stdlib.h>
	#include <stdarg.h>
	#include <ctype.h>
	#include <string.h>
	#include <util/delay.h>

	void oled_init(void);
	void oled_clear_screen(void);
	void oled_write_char(uint8_t c, uint8_t xPos, uint8_t yPos, uint8_t size, uint8_t invers);
	void oled_write_str(char* str, int8_t xpos, int8_t ypos, uint8_t size, uint8_t invers);
	void oled_write_str_right (char* str, int8_t xpos, int8_t ypos, uint8_t fontsize, uint8_t invers);
	void oled_line_v(uint8_t xstart, uint8_t ystart, uint8_t len, uint8_t width, uint8_t col);
//	void oled_write_P (const char *Buffer,...);
	void oled_scroll_right(uint8_t onoff, uint8_t startpage, uint8_t endpage);
	uint8_t oled_getsize(uint8_t c, uint8_t fontsize);
//	#define oled_write(format, args...)   oled_write_P(PSTR(format) , ## args)

	#define COMMAND						0x00
	#define DATA						0x40

	#define SET_CONTRAST_CONTROL		0x81
	#define SET_DISPLAY_ON				0xAF
	#define SET_DISPLAY_OFF				0xAE
	#define SET_DISPLAY_NORMAL			0xA6
	#define SET_DISPLAY_INVERSE			0xA7
	#define ENTIRE_DISPLAY_ON			0xA4
	
	#define SET_MEMORY_ADDR_MODE		0x20	//0x20 = Horizontal Adressing Mode
	#define HORIZONTAL_ADDRESSING_MODE	0x00
	#define VERTICAL_ADDRESSING_MODE	0x01
	#define PAGE_ADDRESSING_MODE		0x02
		
	#define SET_PAGE_START				0xB0	//~0xB7
	#define SET_COLUMN_ADDRESS			0x21
	#define SET_PAGE_ADDRESS			0x22
	
	#define SET_COM_OUTPUT_NORMAL		0xC0
	#define SET_COM_OUTPUT_REMAPPED		0xC8
	
	#define SET_DISPLAY_OFFSET			0xD3
	#define SET_DISPLAY_CLOCK			0xD5
	#define SET_PRECHARGE_PERIOD		0xD9
	#define SET_COM_PINS				0xDA
	
	#define SET_VCOMH_DESELECT			0xDB	//0x00,0x20,0x30
	
	#define	SET_LOWER_COLUMN			0x00
	#define	SET_HIGHER_COLUMN			0x10
	#define	SET_DISPLAY_START_LINE		0x40	//depends on implementation of the display 40h..7fh
	
	#define SET_SEGMENT_REMAP_O1		0xA0	
	#define SET_SEGMENT_REMAP_O2		0xA1
	
	#define SET_MULTIPLEX_RATIO			0xA8
	#define COMMAND_NO_OPERATION		0xE3
	
	#define CHARGE_BUMB_SETTING			0x8D
	
	#define HORIZONTAL_SCROLL_RIGHT		0x26
	#define HORIZONTAL_SCROLL_LEFT		0x27
	#define VHRIGHT_SCROLL_SETUP		0x29
	#define VHLEFT_SCROLL_SETUP			0x2A
	
#endif // __oled_H 
