Friday, 16 May 2008

TouchShield: Text & Touch Example

Overview
So you have a bunch of strings already and now you want to loop through them on the Touchshield with a touch?

Once you're all done, you'll see how Mike make the sketch with some really corny jokes, and his video to prove it.



Initializing
We'll store the strings in RAM as opposed to FLASH to make it easier to understand.

char myStr[][200] = {

"1f u c4n r34d th1s u r34lly n33d t0 g37 l41d",
"Microsoft: You've got questions. We've got dancing paperclips.",
"There are 10 types of people in the world: those who understand binary, and those who don't.", "My pokemon brings all the nerds to the yard, and they're like you wanna trade cards? Darn right, I wanna trade cards, I'll trade this but not my charizard.",
"If at first you don't succeed; call it version 1.0", "I'm not anti-social; I'm just not user friendly", "Alcohol & calculus don't mix. Never drink & derive", "File not found. Should I fake it? (Y/N)",

};

If you use the helper function i've included at the end, you can print each string with word wrap by calling this,

print(myStr[0],0,10);

Which prints the first string starting at column and row (0, 10).


Touch Events
You can loop through the strings by polling the touchscreen. This is done by creating a POINT this way,

POINT myTouch;

And testing the return value of a called function,

if (touch_get_cursor(&myTouch))
{
print(myStr[1],0,10);
}


If the function returns TRUE, then the screen coordinates are stored in myTouch.x and myTouch.y For this example, we're not do anything with the coordinates beacause we're just polling for a press.

Main Loop
So putting it together, we have the code below which loops through the halarious (corny) jokes.

void loop()
{

if (touch_get_cursor(&myTouch))
{

lcd_clearScreen(black);
print(myStr[x],0,10);

if (x>7)
x= 0; //loop back around

x++;

}


}


And the entire code with the print() function,

//Global variables
char myStr[][200] = {

"1f u c4n r34d th1s u r34lly n33d t0 g37 l41d",
"Microsoft: You've got questions. We've got dancing paperclips.",
"There are 10 types of people in the world: those who understand binary, and those who don't.", "My pokemon brings all the nerds to the yard, and they're like you wanna trade cards? Darn right, I wanna trade cards, I'll trade this but not my charizard.",
"If at first you don't succeed; call it version 1.0",
"I'm not anti-social; I'm just not user friendly",
"Alcohol & calculus don't mix. Never drink & derive", "File not found. Should I fake it? (Y/N)",

};

COLOR txtColor = {100,255,255};
COLOR black = {0,0,0};
unsigned char x=0;
unsigned char row=0;
unsigned char col=0;

void setup()
{
;
}

POINT myTouch; //a point for the touchscreen press.

void loop()
{

if (touch_get_cursor(&myTouch))
{

lcd_clearScreen(black);
print(myStr[x],0,10);

if (x>7)
x= 0;

x++;
}

}


void print(char * s,unsigned char x, unsigned char y)

{

col = x;
row = y;
unsigned int len = strlen(s);
while(*s)
{
if (s[0] == '\r')
col = 0;

if (s[0] == '\n')
row +=8;

if (col > 120)
{
col = 0;
row +=8;
}

if (row > 127)
row = 0;

lcd_putc(s[0],col,row,txtColor,black);
col+=6;
*s++;
}

}

0 comments:

Post a Comment