Friday, May 16, 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++;
}

}

6 comments:

ramirez said...

Hi,

when I tried to use the ADC pins in the arduino, I got an error with the analogRead function:

In function 'void loop()':
error: 'analogRead' was not declared in this scope

pinMode, and other related function work fine, but analogread fails.

I'm still a newbie in this, so if I'm doing something obviusly wrong, just let me know. :-)

Thanks,

LEo

Chris said...

Mortadelo,

Make sure you're compiling and sending code to the Diecimila.

To compile for the Diecimila, make sure you're selecting appropriate menu option.

Hope that helps!

ramirez said...

Hi,

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.

Any ideas?

LEo

Chris said...

Mortadelo,

I'm sorry I should have been more clear. To compile for the Diecimila, you select "Arduino NG or Older w/ ATmega168" under Tools->Board menu.

Does that help?

ramirez said...

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?

Thanks a lot!

LEo

Chris said...

Mortadelo,

Serially, at 9600 baud, any way you like!

The RX and TX pins are 2 and 3 on the TouchShield.

So communicate by setting up the Arduino to TX a byte and the TouchShield to RX a byte at 9600 baud.

There will be a tutorial on this very shortly, so stay tuned!