Friday, May 16, 2008

TouchShield: Text & Touch Example

OverviewSo 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.

InitializingWe'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 EventsYou 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 LoopSo 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 variableschar 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)",

the thing is that when I replaced the boards.txt archive, the diecimilla was not more available as a board, as it was replaced by the TouchShield option in Tools->Board, so I can't choos the Diecimilla.

Ok. That helps a lot. I'm finally starting to understand how it works. So just one more question. How can I communicate the Arduino with the TouchShield? How can I read a value using AnalogRead and show it in the LCD?