Thursday, December 18, 2008

Up and running on the TouchShield Stealth and Slide in 5 minutes

EDIT: Chris has posted a new version of this article that fixes a lot of the issues that we found... specifically with the AVR compiler that ships with the Arduino IDE. Check it out in the new post here.

------------------

Preamble


Chris and I have been pretty busy the past month or so, and the TouchShield Stealth (128 x 128 pixels) and TouchShield Slide (320 x 240 pixels) are now both ready for a major "firmware" upgrade. Thank you to everyone who sent back code optimizations on the graphics routines (especially Sheldon and Omar, although technically it's not fair - you're actually real game programmers). The old TouchShield ran in the low 10-15 frames per second, but with this core now runs closer to 30-40 frames per second! Also, ever since my little project a few weeks back, I've had a lot of encouraging email support from the Processing community, and with a little bit of help, now both the Stealth and Slide both run SubProcessing (a subset of the Processing language). That means that programs can be written on the Arduino, and you can literally hot swap the Stealth and Slide in real time and the program works simultaneously, and even scales the graphics accordingly... (I'll definitely take some videos later this week).

In the meantime, there are a few things that Chris and I am still working on, namely picture uploading. Although the TouchShield Stealth image uploader still works, the upgraded chip in the TouchShield Slide means that I have to rewrite the application. This time, I'm going to write it in Java so the same program works on Mac, Windows, and Linux! I'm hoping to have that wrapped up before next week. Without further ado, here we go! (and if you set a playlist to William Tell's Overture, you'll definitely be done before it completes...)

This is the new TouchShield Core, version 0.9. You can click here, and download it on the "app store." It's all GNU GPL'd.


Act 1: Overture

The first step, is to make sure you're running the latest version of the Arduino IDE application, which as of December 12th, 2008, is version 0012. You can grab the latest version over at the arduino.cc website, here.

Please install version 0012 or greater (because David and team upgraded the AVR compiler, to a more efficient version, so that now it works a lot better!). Once the file is downloaded, unzip it to a new place on your hard-drive, and proceed to step 2...


Act 2: Legato

The next step is to install the "core" files into the new installation of the Arduino IDE. This means copying the touchStealth and touchSlide folders into the following folder:

arduino-0012/hardware/cores/

After this step, (and on my hard drive), the cores folder includes 5 folders:

touchStealth/
touchSlide/
blank/
atmega8/
arduino/

Now that the folders are installed, you just need to make sure that the Arduino IDE program knows about the folders. To do this, open the following file:

arduino-0012/hardware/boards.txt

This file contains a description of all of the Arduino cores and compilers, that the IDE can recognize. At the end of this file, just add the lines below (or, you can copy over the version of the boards.txt file that I've included). Before you edit the boards.txt file, make sure that all versions of the Arduino IDE are closed, and you don't have any running on your computer. If you do, it might not save the changes to the boards.txt file correct. So just close and exit from any versions of the program you might have running, just to be safe!

##############################################################

stealth.name=TouchShield Stealth

stealth.upload.protocol=butterfly
stealth.upload.maximum_size=57342
stealth.upload.speed=57600

stealth.bootloader.low_fuses=0xFF
stealth.bootloader.high_fuses=0x98
stealth.bootloader.extended_fuses=0xFB
stealth.bootloader.path=TouchShield
stealth.bootloader.file=TouchShield_boot.hex
stealth.bootloader.unlock_bits=0xFF
stealth.bootloader.lock_bits=0xFF

stealth.build.mcu=atmega645
stealth.build.f_cpu=16000000L
stealth.build.core=touchStealth

##############################################################

slide.name=TouchShield Slide

slide.upload.protocol=butterfly
slide.upload.maximum_size=258048
slide.upload.speed=57600

slide.bootloader.low_fuses=0xFF
slide.bootloader.high_fuses=0x98
slide.bootloader.extended_fuses=0xFB
slide.bootloader.path=TouchShield
slide.bootloader.file=TouchShield_boot.hex
slide.bootloader.unlock_bits=0xFF
slide.bootloader.lock_bits=0xFF

slide.build.mcu=atmega2560
slide.build.f_cpu=16000000L
slide.build.core=touchSlide

##############################################################

Now, you're ready to move onto step 3!



Act 3: Autumn

The TouchShield Stealth and TouchShield Slide communicate with the Arduino using pins 2 and 3. That means it uses Software Serial, not the built-in hardware serial. In order to get Software Serial working properly, then, we're going to copy over the AFSoftSerial libraries into the appropriate location (thanks Limor!). While we're at it, we're also going to copy over the library Chris and I've written to display large numbers on the screen, in case you want to play around and make big timers or countdown clocks...

Just go to the folder:

arduino-0012/hardware/libraries

Now copy into this folder the two folders, touchLargeNums and AFSoftSerial.

On my computer, the libraries folder now has 12 folders in it:

Stepper/
Sprite/
SoftwareSerial/
Servo/
Matrix/
LiquidCrystal/
Firmata/
Ethernet/
EEPROM/
AFSoftSerial/
touchLargeNums/
Wire/

Voila!

Now, ready to move onto step 4...


Act 4: Allegro Moderato

You open the door. You walk through the door, you roll a die. A dwarf appears. Press 1 to use your magic wand. Press 2 to cast a spell. Press 3 to.... Ok, now it's time to boot everything up!

Open the newly installed and upgraded version of the Arduino IDE. This is the file in the arduino-0012/ folder called "arduino.exe" or "arduino", depending on what operating system you're using.

Place the TouchShield Stealth or TouchShield Slide on top of the Arduino. If this is the first time you're connecting your Arduino to your computer, you'll want to make sure you also install the FTDI drivers. The guys at arduino.cc have great tutorials for installing the drivers properly, over here:

http://arduino.cc/en/Guide/Windows
http://arduino.cc/en/Guide/MacOSX
http://www.arduino.cc/playground/Learning/Linux

Once the drivers are installed, or if you've already installed them before, connect the Arduino to the computer using a USB cable.

The Arduino IDE will take a few seconds, and then it will recognize the Arduino. The next step is to program the TouchShield and the Arduino with the appropriate programs. The TouchShield will run the SubProcessing library, which is a little version of the Processing Graphics library, written just for the TouchShield. From now on, any program written for the TouchShield Stealth will work on the Slide and vice versa, as well as being cross-compatible with any version of Processing you might have installed on your computer. This means I can prototype graphics on my PC, and then flash it down to the Arduino, and it'll look the same!

So the first step is to program the TouchShield. From December 18th, 2008 and onward, every TouchShield that gets mailed out will come pre-loaded with the latest version of the SubProcessing software as of that date, so if this is the first time you're using it, you can skip this step. Otherwise, continue to update the TouchShield's firmware:

Open up the TouchShield.txt file. It contains a small little program that puts the TouchShield into "canvas" mode. This means it sits there, waiting for display commands from the Arduino. That code looks like this:


void setup(void) {

}

void loop(void) {
beginCanvas();
}


Copy and paste this code into the Arduino IDE main window. Then, select the appropriate Serial port from the Tools->Serial Port menu. On my computer, it's serial port 18. Then, select the right board from the Tools->Board menu (either TouchShield Stealth or TouchShield Slide).

Click the compile (or play) button to compile the code. Now, press the reset button on the TouchShield. On the Stealth and the Slide, it's the button right next to the USB connector on the Arduino. This puts the TouchShield into reset mode, waiting for a program from the Arduino IDE.

Now press the program button, or "upload to I/O board". The LEDs will blink as the program is downloaded to the board, and then it'll finish. Done!

The final step is to load a program onto the Arduino.

Open the Arduino.txt file, and copy and paste this into a new program in the Arduino IDE window. Now, select "Arduino" from the Tools->Boards menu. Press play to compile, and then press the "upload to IO board" button to send the program to the Arduino. Done!

In about a second or two, the Arduino will reset, and will start sending commands to the TouchShield. The program is pretty simple, it just displays a little red square that you can drag around the screen. If you happen to have a Slide and a Stealth, you can hot-swap the two shields one after the other, and they'll both start displaying whatever the Arduino sends to them. In some cases, it might help to cycle the power on the Arduino and TouchShield, by reseting or re-plugging in the USB or Lithium Backpack.

If you have any questions, or want any help writing some code, or if anything at all doesn't work, or if you're just looking for someone to bounce ideas or talk to, just drop me an email!

Matt and Chris
(and Limor and Sheldon and Omar - thanks!)

25 comments:

Aaron, Vicki Tunell said...

can you still use the sliding functions on the touchshield stealth? I want to be able to draw on my stealth!

Matt said...

Yep... I'll take a video of that as soon as I can... it's crazy looking. Omar found a way to update the Stealth's core library to make real-time dragging on the screen, so the little red box can drag around on the stealth and on the slide. The only difference is that the touchshield slide does it natively in hardware, so its a little faster, whereas the stealth is software-driven.

Unknown said...

Hey Matt, this is great...

1.) Just Downloaded it
2.) It works
3.) Thanks
4.) Hi Aaron

-Mike

Phil Glau said...

I'm getting some errors when switching to TouchScreen Slide using the Mac Version of Arduino 12.

Specifically, an error building "Servo" which doesn't seem related, and a separate error building "touchLargeNums" which does.

I'm not sure if I should be ignoring these errors or not. I followed the tutorial very closely, so I'm pretty sure I've put everything in the right place. (I was using a fresh download of 0012 as well)

Here are my notes:



Arduino 0012 (for Mac OSX)
Physical Board: Duemilanove
Tools->Board Initially set to Arduino Diecimlia

When changing to "Tools->Board->TouchScreen Slide" I get the following errors:

Error building library "Servo"

In file included from /arduino-0012/hardware/cores/touchSlide/WProgram.h:6,
from Firmata.cpp:25:
/arduino-0012/hardware/tools/avr/bin/../lib/gcc/avr/4.3.0/../../../../avr/include/avr/signal.h:36:2: warning: #warning "This header file is obsolete. Use <avr/interrupt.h>."
In file included from /arduino-0012/hardware/cores/touchSlide/WProgram.h:6,
from LiquidCrystal.cpp:6:
/arduino-0012/hardware/tools/avr/bin/../lib/gcc/avr/4.3.0/../../../../avr/include/avr/signal.h:36:2: warning: #warning "This header file is obsolete. Use <avr/interrupt.h>."
Servo.cpp: In static member function 'static void Servo::seizeTimer1()':
Servo.cpp:40: error: 'clockCyclesPerMicrosecond' was not declared in this scope
Servo.cpp:46: error: 'TIMSK' was not declared in this scope
Servo.cpp:46: error: 'TICIE1' was not declared in this scope
Servo.cpp: In member function 'void Servo::write(int)':
Servo.cpp:118: error: 'clockCyclesPerMicrosecond' was not declared in this scope

When I switch back to "Tools->Board->Arduino Diecimlia", I then get this error:

Error building library "touchLargeNums"

In file included from touchLargeNums.cpp:5:
touchLargeNums.h:6:17: error: lcd.h: No such file or directory
In file included from touchLargeNums.cpp:5:
touchLargeNums.h:14: error: 'COLOR' does not name a type
touchLargeNums.h:15: error: 'COLOR' does not name a type
touchLargeNums.h:18: error: 'COLOR' has not been declared
touchLargeNums.h:19: error: 'COLOR' has not been declared
In file included from touchLargeNums.cpp:7:
font23x35.h:4: warning: only initialized variables can be placed into program memory area
touchLargeNums.cpp: In constructor 'largeNum::largeNum()':
touchLargeNums.cpp:11: error: '_largeNumForeC' was not declared in this scope
touchLargeNums.cpp: At global scope:
touchLargeNums.cpp:14: error: variable or field 'setForeColor' declared void
touchLargeNums.cpp:14: error: 'COLOR' was not declared in this scope
touchLargeNums.cpp:19: error: variable or field 'setBackColor' declared void
touchLargeNums.cpp:19: error: 'COLOR' was not declared in this scope
touchLargeNums.cpp: In member function 'void largeNum::putc(char, unsigned char, unsigned char)':
touchLargeNums.cpp:63: error: '_largeNumForeC' was not declared in this scope
touchLargeNums.cpp:63: error: 'lcd_pixel' was not declared in this scope
touchLargeNums.cpp:65: error: '_largeNumBackC' was not declared in this scope
touchLargeNums.cpp:65: error: 'lcd_pixel' was not declared in this scope
touchLargeNums.cpp:69: warning: comparison between signed and unsigned integer expressions
touchLargeNums.cpp: In member function 'void largeNum::putcb(char, unsigned char, unsigned char)':
touchLargeNums.cpp:119: error: '_largeNumForeC' was not declared in this scope
touchLargeNums.cpp:119: error: 'lcd_pixel' was not declared in this scope
touchLargeNums.cpp:125: warning: comparison between signed and unsigned integer expressions
touchLargeNums.cpp: In member function 'void largeNum::putct(char, unsigned char, unsigned char)':
touchLargeNums.cpp:175: error: '_largeNumForeC' was not declared in this scope
touchLargeNums.cpp:175: error: 'lcd_pixel' was not declared in this scope
touchLargeNums.cpp:185: warning: comparison between signed and unsigned integer expressions

Chris said...

Filmo,

Yes those messages are normal. I get them too, but they won't affect your program.

It's trying to compile Arduino specific libraries that aren't needed for the TouchShield.

Chris

Matt said...

@Filmo - yeah, for some reason, the Arduino IDE likes to spit out a lot of debug messages when you switch cores. It's like it's trying to compile the core library files for the Arduino core even though you don't really need them... hmmmm. You know, I usually just open 2 copies of the Arduino IDE. I use 1 copy to type up programs for the Arduino, and another copy on the TouchShield. That way I don't have to switch back and forth a ton

Phil Glau said...

Thanks for the update. I'll press on then.. :)

Aaron, Vicki Tunell said...

1)Just wondering how to recalibrate the touchsceen. the old 30 second rule doesn't seem to be working anymore.
2) Hi Mike

Andy said...

Hi I am trying to use the ToucheShield: Text and Touch Example but shortly into the program debug process it tells me error: 'COLOR' does not name a type In function'void loop()': In function 'void print(char*, unsigned char, unsigned char)': I have it set to the Arduino Decim. Any ideas?
-Andy

JustinN said...

I just did the update and unfortunately I'm not seeing any differences. Is there any caching that I need to clear?? I'm still seeing the line drawing error (lines that should have an origin at 64,64 appear at other points on the screen when the line isn't in the upper left quadrant).

Can someone with the update run this and tell me what they get?

int endy = 0;
int endx = 0;

COLOR gaugeRed = {224, 38, 41};

void setup(){

}

void loop(){
lcd_line(64, 64, int(endx), int(endy), gaugeRed);
lcd_line(65, 64, int(endx), int(endy), gaugeRed);
lcd_line(63, 64, int(endx), int(endy), gaugeRed);
lcd_line(64, 65, int(endx), int(endy), gaugeRed);
lcd_line(64, 63, int(endx), int(endy), gaugeRed);
endx++;
//endy++;
delay(10);
}

Matt said...

@Andy - hi! hmmm... it sounds like you're compiling code that was meant for the TouchShield, but trying to put it on the Arduino... COLOR is something that's only defined on the TouchShield library. On the other hand, I think you also reminded me of a good point... a lot of the examples on the site are using the old core, where you just ran programs on the touchshield. On the other hand, this new core has a slightly different programming model... you know what? I'm going to write another blog entry just about this to try to clear things up.

@Justin - let me guess, you're using this on a Mac? For some reason, the Arduino IDE distribution comes with a version of the AVR compiler that doesn't handle the TouchShield slide processor correctly.... Chris is working on this right now in the other room, actually! Chris....?

JustinN said...

@ Matt

Yeah I'm using a Mac :) . No rush on the fix. I won't be doing any dev stuff anytime soon due to xmas family stuff.

BTW I saw that you guys have a bigger screen (the slide). You should do a trade in program. Trade a stealth in towards the price of a slide. :)

Matt said...

@Justin - you know, I hadn't even thought about that... but sure, why not! I wonder how trade-ins work. I think car places do this if you buy a new car, and you can trade in... I don't really know how that works though. You know, if I got a couple of people to trade in, maybe I could then find someone else who'd want to buy a "used" TouchShield for some smaller amount, and then that would mean the trade-in upgrade could be cheap for everyone involved? Hmmm... maybe that's too complicated. Well, if you're up for it, just shoot me an email at inthebitz/gmail :-)

Aaron, Vicki Tunell said...

so still no answer on how to calibrate the touchscreen on the new core...
How do i do this?

Chris said...

Aaron,

You can calibrate the touchscreen by calling,

touch_calibrate();

in the setup function. I find that the a stylus give the most accuracy when touching the three circles.

Jorge Moreno Aguilera said...

In my ArduinoBT not run :(

Connecting to programmer: .avrdude: butterfly_recv(): programmer is not responding

any ideas?

Chris said...

@Jorge

I haven't tried the TouchShield on an ArduinoBT yet because I don't own one at the time.

I have heard others have gotten it to work by programming the touchShield first on a Diecimila or Duemilanova and then swap on the Bluetooth version.

Chris

Pedro said...

Hi, I'm trying to compile the Arduino.txt code, but I get the next error:
In function 'void setup()':
error: 'background' was not declared in this scope In function 'void loop()':
The compiler doesn't find neither the methods: fill, stroke and rect.

I have an Arduino Diecimila and a TouchShield Stealth.

Please help me with this issue.
Thanks, Merry Christmas!

Charles Harris said...

I am getting something similar as well when I try to compile the touchshield.txt file contents:
In function 'void loop(): error 'beginCanvas' was not declared in this scope

My system is on linux using arduino-12

Charles Harris said...

Nevermind, I was uploading the the arduino and not the touchshield.

Unknown said...

Followed all of this, uploaded code to my TouchShield, and it's giving some crazy results. For example, I calibrate the touch screen and it's still massively off. The values in mouseX and mouseY from gettouch() seem to be most accurate in the bottom right corner, and totally off in the upper left corner. I think it is these variables being filled with junk which causes the thing to crash.

I've uploaded a small video showing whats going on here:

http://www.youtube.com/watch?v=ca72xHvyeh0

And the code running on the touchshield:


int x=0;
int y=100;

void setup()
{
touch_calibrate();

background(0,0,0); //this paints the whole background black
stroke(0,0,0); //the outline of the next shape I draw will be blue
fill(0,0,255); //the inside fill of the next shape I draw will be blue
}

void loop()
{
gettouch(); //find out where a finger is touching

ellipse(x, y, 3, 3); // draw an ellipse(x, y, width, height)

x++;

ellipse(mouseX, mouseY, 3, 3);

delay(100);

}


Any ideas?

Chris said...

Japanese,

That looks crazy! Calibrating the touchscreen can be tricky because the small touchscreens are very sensitive to pressure. They also have a small usable range, however it should be more accurate than you've indicated in your video.

I've gotten similar accuracy when my calibration is off. It's not easy to calibrate because the calibration takes into account pressure.

To calibrate the touchscreen Mike suggested to make sure I'm pressing on each 3 circles with consistent pressure using a stylus.

If that fails, you could use this function that only returns a single point after release,

POINT p;

void loop(){
if (touch_get_cursor(&p))
{
ellipse(p.x,p.y,10,10);
}
}

If that doesn't work, feel free to shoot me an email and we'll get it sorted out.

Chris

bs_one said...

Hi!
I got my Touchshield Slide some Days ago. I already uploaded some code on it successfull, but when I try to use a Serial connection to communicate with the arduino I get this error:

In function 'void setup()':
error: 'Serial' was not declared in this scope

I know that when I get this error I have to declare "Serial" (obviosly XD), but I dont know how ...
I got the code from the PinVisualizer and just addapted it to turn an LED on when the TouchShield starts up.
Heres the code thats running on the TouchShield(I know its not very usefull, but I just wanted to test this function):

void setup(){
Serial.begin(9600);
Serial.print(1);
}

void loop(){
}

So yeah thats my problem and I hope you will response XD
Sorry for the bad english but english isnt my first language.

-bs_one

Radek said...

I have the same "canvas" problem like pedro, but with duemilanove and ide 0.8.4:

In function 'void setup()':
error: 'background' was not declared in this scope

where is problem?
thx
radek

Unknown said...

If i were to upload a game to the gamepack and i ran it on battery power i wouldn't have to keep it attached to the computer by usb? :)