FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Topics & Contributions
   Tangible Computing
(Moderator: REAS)
   Interface with pen device!
« No topic | Next topic »

Pages: 1 2 
   Author  Topic: Interface with pen device!  (Read 8339 times)
spacegirl


Interface with pen device!
« on: Dec 9th, 2002, 8:49pm »

This code shows how to interface a Wacom PenPartner to allow pen drawing... It's a useful example of how to reverse engineer existing hardware to use with P5.
 
// wacom 02
// by spacegirl
//
// sketches with the wacom penPartner tablet over serial
// this code tests the wacom codes, has startup code included
//
// reverse engineered packets at end
// vaguely reminiscent of "storing input" by REAS
 
// Created: 6 December 2002
// Functional: 9 December 2002
 
int stylusX, stylusY, stylusZ, stylusP, cursorX, cursorY, cursorZ, cursorP;
int numPoints = 60;
float mx[] = new float[numPoints];
float my[] = new float[numPoints];
float mr[] = new float[numPoints];
 
int wacom[] = new int[8];
boolean penDetected, pressed, penEnd, buttonPress,eraserEnd;
 
int temp;
int curr_byte=0;
 
/* used a serial monitoring program to extract the following start up codes:
 
initString:0D 23 7E 2A 46 32 0D 49 54 32 0D 53 55 33 0D 41 4C 31 0D 40 53 54 0D 40 53 54 0D 40 53 54 0D
refresh: 40 53 54 0D
*/
 
byte[] initString={13, 35, 126, 42, 70, 50, 13, 73, 84, 50, 13, 83, 85, 51, 13, 65, 76, 49, 13, 64, 83, 84, 13, 64, 83, 84, 13};
 
byte[] refresh={64, 83, 84, 13};
 
boolean recordOn=false, initResponse=false;
 
void setup()
{
  size(600, 400);
  background(0);
  noStroke();
  ellipseMode(CENTER_DIAMETER);
 
  beginSerial(9600);
  temp = serial;
  serialWrite(initString);
  beginSerial(19200);
  recordOn=true;
}
 
void loop()
{
  updatePenData();
  if (pressed){
    for(int i=1; i<numPoints; i++) {
      mx[i-1] = mx[i];
      my[i-1] = my[i];
      mr[i-1] = mr[i];
    }
  }
 
  mx[numPoints-1]= stylusX/8;
  my[numPoints-1]= stylusY/8;
  mr[numPoints-1]= stylusP*2;
//  println(mr[numPoints-1]);
 
  if (buttonPress) mr[numPoints-1] = 0;
  else if (eraserEnd){
    for (int i=1; i<numPoints; i++){
      mr[i-1]=0;
    }
  }
  for(int i=1; i<numPoints; i++) {
    fill(mr[i]);
    ellipse(mx[i], my[i], mr[i]/2, mr[i]/2);
  }
 
}
 
void updatePenData(){
  if ((wacom[0]&(0x40))>0) penDetected=true;
  else penDetected=false;
  if ((wacom[0]&(0x0)>0) pressed=true;
  else pressed=false;
  stylusX=(wacom[1]<<7)+wacom[2];
  stylusY=(wacom[4]<<7)+wacom[5];
  stylusP= (wacom[6]%64)+((wacom[3]&0xC0)<<4);
  buttonPress=(wacom[3]&0x10)>0;
  eraserEnd=(wacom[3]&0x20)>0;
}
 
void serialEvent() {
  temp = serial;
  if (recordOn){
    if (temp>0x7F) curr_byte=0;
    else curr_byte++;
    wacom[curr_byte] = temp;
  }
}
 
/* Format of 7 bytes data packet for Wacom Tablets
 
Byte 1
bit 7  Sync bit always 1
bit 6  1 if anything is pressed.
bit 5  1 if device noticed
bit 4  Reserved
bit 3  1 cursor end/ 0 stylus end, also 1 when stylus down
bit 2  Reserved
bit 1  Always 0
bit 0  Always 0
 
Byte 2
bit 7  Always 0
bits 6-0 = X13 - X7
 
Byte 3
bit 7  Always 0
bits 6-0 = X6 - X0
 
Byte 4
bit 7  Always 0
bit 6  Always 0
bit 5  Erase end or no.
bit 4  B on or off
bit 3  P1
bit 2  P0
bit 1  Always 0
bit 0  Always 0
 
Byte 5
bit 7  Always 0
bits 6-0 = Y13 - Y7
 
Byte 6
bit 7  Always 0
bits 6-0 = Y6 - Y0
 
Byte 7
bit 7 Always 0
bit 6  Pen End
bit 5  P6
bit 4  P5
bit 3  P4
bit 2  P3
bit 1  P2
bit 0  P1
*/
 
 
 
« Last Edit: Dec 9th, 2002, 8:52pm by spacegirl »  
Glen Murphy

WWW Email
Re: Interface with pen device!
« Reply #1 on: Dec 10th, 2002, 12:50am »

That's awesome! Can't test, as I only have a USB Graphire, but daamn.
 
I'm thinking that tablet support in P5 will be a wanted feature for those demonstrating their apps on the Tablet PCs coming out.
 
REAS


WWW
Re: Interface with pen device!
« Reply #2 on: Dec 10th, 2002, 1:20pm »

Fantastic! I'm curious ... how robust is this?
 
Can you please explain why the different speeds:
 
Code:
beginSerial(9600);  
temp = serial;  
serialWrite(initString);  
beginSerial(19200);

 
and what is happening here:
 
Code:
void serialEvent() {  
  temp = serial;  
  if (recordOn){  
    if (temp>0x7F) curr_byte=0;  
    else curr_byte++;  
    wacom[curr_byte] = temp;  
  }  
}  
 
 
Are there any additional serial methods that would have made this code easier for you to write.
 
spacegirl


Re: Interface with pen device!
« Reply #3 on: Dec 12th, 2002, 6:00am »

on Dec 10th, 2002, 1:20pm, REAS wrote:
Fantastic! I'm curious ... how robust is this

 
Pretty robust... well, sometimes I have to start proce55ing up twice to get it to work, but not often.  Am trying to figure out when exactly that happens...
 
Quote:

Can you please explain why the different speeds:
 
Code:
beginSerial(9600);  
temp = serial;  
serialWrite(initString);  
beginSerial(19200);

 

 
I think what is happening is that wacom uses 9600 as a default speed for querying the tablets at startup because some of the older devices might run at a slower speed, and then switches to a higher speed to transact normal business.  It took me a while to figure this one out, I assure you!
 
Quote:

and what is happening here:
 
Code:
void serialEvent() {  
  temp = serial;  
  if (recordOn){  
    if (temp>0x7F) curr_byte=0;  
    else curr_byte++;  
    wacom[curr_byte] = temp;  
  }  
}  
 

 
"recordOn" prevents data from being written before the initialization has taken place.  This is because the tablet send back some packets IDing itself, and I didn't want these to cause an ArrayOutOfBounds exception in my wacom[].
 
The rest of it is a way of breaking the serial stream up into 7 byte data packets.  The wacom protocol reserves the MSB of each byte as a way to indicate the first byte of a data packet, so I use that as a way of indexing the data into an array that gets used by the rest of the program. At one point I was counting packets, but this is more robust... One tweak that I've made since posting this code is adding a bounds check before writing to wacom[], i.e.  
Code:

if (curr_byte<max_bytes) wacom[curr_byte] = temp;

 
Quote:

Are there any additional serial methods that would have made this code easier for you to write.

 
Hmm... it would be nice to be able to clear the send and receive buffers.  Also, I'm trying to figure out how to use endSerial() in the code to shut down the port when I stop the program.  I'm using non-p5 code now to get two serial streams to work simultaneously, and would love it if this was as easy as the current serial methods were.  But otherwise I've been extremely happy.
 
pitaru

WWW Email
Re: Interface with pen device!
« Reply #4 on: Dec 18th, 2002, 11:55pm »

Oh - this is promising!
 
I tried it with my 6x8 intuos wacom, but i think the packets are more complex (for tilt etc.) - it worked in very unpredictable ways
 
I was wondering if you tried hacking that one as well.
 
Also, for whatever its worth, there's a Java interface to wintab ( the generic pc tablet driver ) here:   http://www.csl.sony.co.jp/person/rekimoto/java/jwintab/
 
btw - which serial monitoring software did you use - HHD?
 
Great job
 
pod32
Guest
Email
Re: Interface with pen device!
« Reply #5 on: Oct 14th, 2003, 11:15am »

Did anybody found out how to integrate this to P5?
 
http://www.csl.sony.co.jp/person/rekimoto/java/jwintab/
 
mazben
Guest
Email
Re: Interface with pen device!
« Reply #6 on: Oct 23rd, 2003, 12:30pm »

Using jwintab with P5.
 
1. import jwintab.dll to P5 java.bin
2. load jwintab.class to 'code' folder
3. use jwintab methods
 
-mazben
 
mKoser

WWW Email
Re: Interface with pen device!
« Reply #7 on: Oct 26th, 2003, 1:43am »

i've had a go at using my Wacom USB Graphire Tablet (size A5) with processing and JWinTab 1.0
 
For some reason the system has a delay of 1-2 seconds - which is VERY annoying!  
(suggestions as to why this is, is very welcome!)
 
1) download JWinTab: http://www.csl.sony.co.jp/person/rekimoto/java/jwintab/
 
2) copy jwintab.dll to your Processing root folder
 
3) create an empty sketch called JWinTab (not sure this is necessary!)
 
4) create a 'code' folder within the new sketch folder
 
5) copy Jwintab.class into the 'code' folder
 
6) copy paste the code, and run it
 
+ mikkel
 
Code:

// mikkel crone koser
// www.beyondthree.com
// october 26th 2003
//
// WARNING: PC VERSION ONLY!
// using WACOM Graphire USB A5 board with JwinTab:
// http://www.csl.sony.co.jp/person/rekimoto/java/jwintab/
//
// the entire surface of the wacom tablet is
// 1-to-1 with the surface of your canvas (width x height).
//
// WHY OH WHY?
// For some reason there is a delay of 1-2 seconds, I have
// no idea why this is... I am running USB 2.0 on my system
// so it can't be a data-transfer speed that is the problem.
//
// any suggestions, addintions, do give me a shout: m@beyondthree.com
 
int res;
int tx, ty;    // tablet x, y
float tp;      // preassure
int ptx, pty;  // previous tablet XY
float ang;     // angle
int rx, ry, lx, ly; // right XY & left XY
int prx, pry, plx, ply;  // previous --- " ----
int td[] = new int[6];   // data recieved from tablet
BFont ocr;
 
void setup(){
  size(640, 480);
  background(0);
  Jwintab.open();
  println("Jwintab version " + Jwintab.getVersion() + " startet!");
  fill(255);
  noStroke();
  ocr = loadFont("OCR-B.vlw.gz");
  textFont(ocr, 18);
}
 
void loop(){
  fill(0, 2);
  rect(0, 0, width, height);
  updateTablet();
  //outputTablet();
 
  line(ptx, pty, tx, ty);
 
  fill(255, 255, 255, 55);
  beginShape(QUADS);  
  vertex(lx, ly);  
  vertex(plx, ply);  
  vertex(prx, pry);
  vertex(rx, ry);  
  endShape();
}
 
void updateTablet(){
  // save memory
  if(tx-ptx > 1) ptx = tx;
  if(ty-pty > 1) pty = ty;
   
  prx = rx;
  pry = ry;
  plx = lx;
  ply = ly;
 
  res = Jwintab.getPacket(td);
   
  // the numbers 5102 and 3710 are strange
  // they were the magin-numbers of my wacom board
  // but i think they vary according to where your
  // proce55ing canvas-window opens up... fiddle fiddle!  
  tx = (int)((td[0] * width)/5102);  // x
  ty = height - (int)((td[1] * height)/3710);  // y
  tp = td[5]/10f;     // preassure
 
  ang = calcAngle(tx, ty, ptx, pty);
 
  rx = (int)(tp * cos(ang+90)) + tx;
  ry = (int)(tp * sin(ang+90)) + ty;
  lx = (int)(tp * cos(ang-90)) + tx;
  ly = (int)(tp * sin(ang-90)) + ty;
}
 
void outputTablet(){
  fill(255);
  text(tx + "   " + ty + "   " + ang, 10, height-25);
  text(td[0] + "   " + td[1] + "   " + td[2] + "   " + td[3] + "   " + td[4] + "   " + td[5], 10, height-10);
}
 
float calcAngle(int ax, int ay, int bx, int by){
  float a = atan2(ay-by, ax-bx);
  return a;
}
 
 

mikkel crone koser | www.beyondthree.com | http://processing.beyondthree.com
pitaru

WWW Email
Re: Interface with pen device!
« Reply #8 on: Oct 26th, 2003, 3:54am »

yep, i'm working on a new dll that will solve the lag.  
 
will be ready soon.
 
-amit
 
pitaru

WWW Email
Re: Interface with pen device!
« Reply #9 on: Oct 26th, 2003, 5:08am »

ok, its up on http://pitaru.com/jwintab_v2
 
have fun
-amit
 
mKoser

WWW Email
Re: Interface with pen device!
« Reply #10 on: Oct 26th, 2003, 1:13pm »

great stuff...!
 
it works perfect on my USB Graphire tablet!  
(allthough tilt isn't supported)
 

mikkel crone koser | www.beyondthree.com | http://processing.beyondthree.com
pitaru

WWW Email
Re: Interface with pen device!
« Reply #11 on: Oct 26th, 2003, 3:01pm »

yep - i think that graphire tablets do not support tilt. you'll need an intuos model for that.
 
-amit
 
mazben
Guest
Email
Re: Interface with pen device!
« Reply #12 on: Oct 28th, 2003, 6:49am »

Strange.. I'm using a intuos 2 usb tablet and I didn't have any lag problems with my experiment with the (original version) Jwintab. Ok, there where other problems, like the initializing.
 
BTW Pitaru, how did you optimate the dll. Can you post the
C files?
 
-mazben
 
pitaru

WWW Email
Re: Interface with pen device!
« Reply #13 on: Oct 28th, 2003, 2:53pm »

hey mazben,  
 
the original dll version worked fine as long as i had it on a seperate thread. the v2 dll cleans the tablet buffer on every call and only returns the last polled data. i need to cleanup the src (both c and java), and then post it on http://pitaru.com/jwintab_v2
 
 
-amit
 
mazben
Guest
Email
Re: Interface with pen device!
« Reply #14 on: Oct 28th, 2003, 3:29pm »

Great! Thanks.
 
Pages: 1 2 

« No topic | Next topic »