/*
 * drawqr.c
 *
 * Created: 14. 5. 2015 18:32:23
 *  Author: Tom
 */ 



#include "init_1.h"
#include "drawqr.h"

void rawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color) 
{
  // Bounds check
  if ((x >= SSD1351WIDTH) || (y >= SSD1351HEIGHT))
  return;

  // X bounds check
  if (y+h > SSD1351HEIGHT)
  {
    h = SSD1351HEIGHT - y - 1;
  }

  if (h < 0) return;

  // set location
  writeCommand(SSD1351_CMD_SETCOLUMN);
  writeData(x);
  writeData(x);
  writeCommand(SSD1351_CMD_SETROW);
  writeData(y);
  writeData(y+h-1);
  // fill!
  writeCommand(SSD1351_CMD_WRITERAM);  

  for (uint16_t i=0; i < h; i++) {
    writeData(color >> 8);
    writeData(color);
  }
}

void rawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color) 
{
	// Bounds check
	if ((x >= SSD1351WIDTH) || (y >= SSD1351HEIGHT))
	return;

	// X bounds check
	if (x+w > SSD1351WIDTH)
	{
		w = SSD1351WIDTH - x - 1;
	}

	if (w < 0) return;

	// set location
	writeCommand(SSD1351_CMD_SETCOLUMN);
	writeData(x);
	writeData(x+w-1);
	writeCommand(SSD1351_CMD_SETROW);
	writeData(y);
	writeData(y);
	// fill!
	writeCommand(SSD1351_CMD_WRITERAM);

	for (uint16_t i=0; i < w; i++) {
		writeData(color >> 8);
		writeData(color);
	}
}

void clearScreen(void)
{
	int x, y;
	int color = WHITE;
	goTo(0, 0);
	for(x=0;x<SSD1351HEIGHT*SSD1351WIDTH;x++)
	{
		writeData(color >> 8);
		writeData(color);	
	}
	return;
	
	for(x=0;x<128;x++)
	{
		for(y=0;y<128;y++)
		{
			drawPixel(x, y, WHITE);
			//rawFastVLine(x, y, 2, BLUE);
		}
	}
}

void goTo(int x, int y) 
{
	if ((x >= SSD1351WIDTH) || (y >= SSD1351HEIGHT)) return;
	
	// set x and y coordinate
	writeCommand(SSD1351_CMD_SETCOLUMN);
	writeData(x);
	writeData(SSD1351WIDTH-1);

	writeCommand(SSD1351_CMD_SETROW);
	writeData(y);
	writeData(SSD1351HEIGHT-1);
	
	writeCommand(SSD1351_CMD_WRITERAM);
	
}

void drawPixel(int16_t x, int16_t y, uint16_t color)
{
	if ((x >= SSD1351WIDTH) || (y >= SSD1351HEIGHT)) return;
	if ((x < 0) || (y < 0)) return;

	goTo(x, y);
	writeData(color >> 8);
	writeData(color);
	
}

void rawFillRect(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t fillcolor) 
{
  // Bounds check
  if ((x >= SSD1351WIDTH) || (y >= SSD1351HEIGHT))
    return;

  // Y bounds check
  if (y+h > SSD1351HEIGHT)
  {
    h = SSD1351HEIGHT - y - 1;
  }

  // X bounds check
  if (x+w > SSD1351WIDTH)
  {
    w = SSD1351WIDTH - x - 1;
  }

  // set location
  writeCommand(SSD1351_CMD_SETCOLUMN);
  writeData(x);
  writeData(x+w-1);
  writeCommand(SSD1351_CMD_SETROW);
  writeData(y);
  writeData(y+h-1);
  // fill!
  writeCommand(SSD1351_CMD_WRITERAM);  

  for (uint16_t i=0; i < w*h; i++) {
    writeData(fillcolor >> 8);
    writeData(fillcolor);
  }
}


int draw_qr(unsigned char*pSourceData, int size)
{
int x,y;
int unWidth = size;

 //et024006_DrawFilledRect(0, 0, ET024006_WIDTH, ET024006_HEIGHT, BLUE);
 /*
 et024006_PrintString("start", //AUDIO_PLAYER_APPLICATION_VERSION,
 (unsigned char *) VERSION_FONT, VERSION_POS_X, VERSION_POS_Y,
 VERSION_FONT_COLOR, APPLI_BG_COLOR);
*/
 
//#define K
#define O 10
				
int K = 118/size;		
		
	for(y = 0; y < unWidth; y++)
	{
	
		for(x = 0; x < unWidth; x++)
		{
			if (*pSourceData & 1)
			{
				//et024006_DrawFilledRect( x*K+O, y*K+O, K, K, BLACK);
				//drawPixel(x*K+O, y*K+O, BLACK);
				rawFillRect( x*K+O, y*K+O, K, K, BLACK);
			}
			else
			{
				//et024006_DrawFilledRect( x*K+O, y*K+O, K, K, WHITE);
				//drawPixel(x*K+O, y*K+O, WHITE);
				rawFillRect(x*K+O, y*K+O, K, K, WHITE);
			}
			pSourceData++;
		}
	}
	return(0);	
}