I just started playing with TVout a few weeks ago, and one of the first things I did was a simple ball demo. (I wrote up my experiments here: Arduino Pac-Man part 2 – It lives! | Sub-Etha Software) I will post the source here in case it helps. For moving a pixel, there are a few basic ways. Here's one using a for loop to draw the pixel across the screen:
uint8_t x; // X position of pixel
for (x=0; x<TV.vres(); x++)
{
TV.set_pixel(x, 0, WHITE);
TV.delay_frame(30); // delay 30 frames
}
That would make 'x' go from 0 to whatever the vertical resolution is (120 by default).
You could also take a more brute-force approach and manually increment the x coordinate each time in a loop:
uint8_t x;
x = 0;
while(x<TV.vres())
{
TV.set_pixel(x, 0, WHITE);
TV.delay_frame(30); // delay 30 frames
x = x +1;
}
For bouncing, if you track the x and y of the object, you can use a movement-x and movement-y variable that can either be +1 (when moving right or down) or -1 (when moving left or up) and do something like this:
uint8_t x, y; // position of object
int8_t mx, my; // movement offset of object
// Start out at top left of screen
x = 0;
y = 0;
// Start out moving right (x+1) and down (y+1)
mx = 1;
my = 1;
// Do this forever...
while(1)
{
// Draw pixel
TV.set_pixel(x, y, WHITE);
TV.delay_frame(30);
// Erase pixel
TV.set_pixel(x, y, BLACK);
// Do movement.
x = x + mx;
// If we hit the left or right of the screen, invert the movement.
if ( (x<=0) || (x>=TV.vres()-1) ) mx = -xm;
y = y + my;
if ( (y<=0) || (y>=TV.hres()-1) ) my = -my;
}
...something like that. The >TV.Xres() may be off by one.
Here is the sample I wrote to move a circle around the screen:
#include <TVout.h>
TVout TV;
void setup()
{
TV.begin(NTSC, 120, 96);
TV.clear_screen();
}
void loop()
{
uint8_t x, y; // X and Y position of ball
int8_t xm, ym; // X and Y movement of ball
// Center of screen
x = TV.hres()/2;
y = TV.vres()/2;
// Start moving to the right, and down.
xm = 1;
ym = 1;
// We will do our own control loop here.
while(1)
{
// Wait for end of screen to be drawn.
TV.delay_frame(1);
// Erase circle
TV.draw_circle(x, y, 4, BLACK);
x = x + xm;
if (x<4 || x>=TV.hres()-4) xm = -xm;
y = y + ym;
if (y<4 || y>=TV.vres()-4) ym = -ym;
TV.draw_circle(x, y, 4, WHITE);
}
}
And here is a modified version that allows bouncing multiple balls... I guess this was my "hello, world" program for TVout:
#include <TVout.h>
TVout TV;
#define BALLS 10 // Number of balls to bounce.
#define BALLSIZE 4 // Size of balls.
void setup()
{
TV.begin(NTSC, 120, 96);
TV.clear_screen();
}
void loop()
{
uint8_t x[BALLS], y[BALLS]; // X and Y position of ball
int8_t xm[BALLS], ym[BALLS]; // X and Y movement of ball
uint8_t i; // counter
// Initialize balls.
for (i=0; i<BALLS; i++)
{
// Random position
x[i] = random(BALLSIZE, TV.hres()-BALLSIZE-1);
y[i] = random(BALLSIZE, TV.vres()-BALLSIZE-1);
// Start moving to the right, and down.
xm[i] = 1;
ym[i] = 1;
}
// We will do our own control loop here.
while(1)
{
// Wait for end of screen to be drawn.
TV.delay_frame(1);
for (i=0; i<BALLS; i++)
{
// Erase balls.
TV.draw_circle(x[i], y[i], BALLSIZE, BLACK);
x[i] = x[i] + xm[i];
if (x[i]<=BALLSIZE || x[i]>TV.hres()-BALLSIZE-1) xm[i] = -xm[i];
y[i] = y[i] + ym[i];
if (y[i]<=BALLSIZE || y[i]>=TV.vres()-BALLSIZE-1) ym[i] = -ym[i];
TV.draw_circle(x[i], y[i], BALLSIZE, WHITE);
}
}
}