/* Demoprogramm der t6963c.h Library für ein 240x128 Pixel Grafik LCD mit Toshiba T6963C Controller
 *
 * Gezeigt wird Textausgabe und eine animierte Liniengrafik (Flying Lines/Marquee)
 *
 * 3/2006 Florian Schäffer, http://www.blafusel.de
 */

#include <math.h>		// für rand(), ist ohne time() nicht wirklich zufällig
#include <t6963c.h>

#define lines 10		// Anzahl der Marquee-Linien
#define maxstep 5		// Maximaler Abstand zwischen zwei Linien
#define xmax 240		// Bildschirmpixel
#define ymax 128		// -"-

/**
	@brief	wartet Millisekunden
	@param 	Wartezeit
	@return	none
*/
void delay_ms(int ms)
{
  int t;
  for(t=0; t<=ms; t++)
	_delay_ms(1); 
}

int main(void)
{
	int	x1[lines],
		x2[lines],
		y1[lines],
		y2[lines],
		stepx1,
		stepx2,
		stepy1,
		stepy2,
		i;

	glcd_setup();		
	glcd_init();
	glcd_clear_text();
	glcd_clear_graph();
	glcd_print (10,8, "Hello world");
	glcd_box(0, 0, xmax-1, ymax-1, 1);				// Rand zeichnen

	for (i=0; i < lines; i++)						// alle Linien initisalisieren. Starten in 1,1 = direkt auf Randlinie
	{
		x1[i] = 1;
		x2[i] = 1;
		y1[i] = 1;
		y2[i] = 1;
	}

	stepx1 = rand() % 5 + 1;						// Zufallsschrittweite
	stepx2 = rand() % 5 + 1;
	stepy1 = rand() % 5 + 1;
	stepy2 = rand() % 5 + 1;
	x1[lines-1] = rand() % (xmax - 1) + 1;			// zufällige vorderste Linie platzieren; Zahlen von 1 bis (maximale Breite -1)
	x2[lines-1] = rand() % (xmax - 1) + 1;
	y1[lines-1] = rand() % (ymax - 1) + 1;
	y2[lines-1] = rand() % (ymax - 1) + 1;
	
	while (1)
	{
		glcd_line(x1[0], y1[0], x2[0], y2[0], 0);		// letzte Linie löschen

		for (i=0; i < lines-1; i++)						// alle weitern Linien eins aufrücken
		{
			x1[i] = x1[i+1];
			x2[i] = x2[i+1];
			y1[i] = y1[i+1];
			y2[i] = y2[i+1];
		}
		
		// Testen, ob ein Linien-Eckpunkt einen Rand berührt; wenn ja, Richtung umdrehen und neue Schrittweite
		// zuvor positiver Schrittwert
		if (x1[lines-1] + stepx1 >= xmax-1)
			stepx1 = -1 * (rand () % maxstep + 1);
		if (x2[lines-1] + stepx2 >= xmax-1)
			stepx2 = -1 * (rand () % maxstep + 1);
		if (y1[lines-1] + stepy1 >= ymax-1)
			stepy1 = -1 * (rand () % maxstep + 1);
		if (y2[lines-1] + stepy2 >= ymax-1)
			stepy2 = -1 * (rand () % maxstep + 1);
		
		// Testen, ob ein Linien-Eckpunkt einen Rand berührt; wenn ja, Richtung umdrehen und neue Schrittweite
		// zuvor negativer Schrittwert
		if (x1[lines-1] + stepx1 <= 0)
			stepx1 = rand () % maxstep + 1;
		if (x2[lines-1] + stepx2 <= 0)
			stepx2 = rand () % maxstep + 1;
		if (y1[lines-1] + stepy1 <= 0)
			stepy1 = rand () % maxstep + 1;
		if (y2[lines-1] + stepy2 <= 0)
			stepy2 = rand () % maxstep + 1;
		
		// vorderste Linie ein Schritt weiter als die bisher vorderste
		x1[lines-1] += stepx1;		
		x2[lines-1] += stepx2;		
		y1[lines-1] += stepy1;		
		y2[lines-1] += stepy2;		
		
		glcd_line(x1[lines-1], y1[lines-1], x2[lines-1], y2[lines-1], 1);		// vorderste Linie zeichnen

		delay_ms (100);		// warte etwas
	}
}


