'jleblanc 10.16.05: simple output to hypterm
' 10-bit A/D conversion
' Connect analog input to channel-0 (RA0)

DEFINE OSC 20

PAUSE 500               ' Wait .5 second

main:    
  'SEROUT2 PORTC.6, 16468, [4,9] ' or just [3]
  'PAUSE 500               ' Wait.01 second
  
  SEROUT2 PORTC.6, 16468, [101,0,0] ' or just [3]
  PAUSE 2000               ' Wait.01 second
  SEROUT2 PORTC.6, 16468, [101,1,0] ' or just [3]
  PAUSE 2000               ' Wait.01 second
  SEROUT2 PORTC.6, 16468, [101,2,0] ' or just [3]
  PAUSE 2000               ' Wait.01 second
  SEROUT2 PORTC.6, 16468, [101,3,0] ' or just [3]
  PAUSE 2000               ' Wait.01 second
  SEROUT2 PORTC.6, 16468, [101,4,0] ' or just [3]
  PAUSE 2000               ' Wait.01 second
  SEROUT2 PORTC.6, 16468, [101,5,0] ' or just [3]
  PAUSE 2000               ' Wait.01 second

GOTO main 

'/*
'  An important question is ultimately what information this program needs
'  I am in favor of giving it complete information in case...
'  Possible serial codes:
'    2 byte:
'      PLAYchange: [100,0],  PLAYfinger: [Finger, Vel]
'      MENUchange: [101,0],  MENUfinger: [Finger, 101] (unlock/lock)
'                                        [Finger, 0  ] (change)
'    3 byte:
'      PLAYchange: [100,0,0]
'*/

'import processing.serial.*;
'Serial myPort;  // The serial port

'Finger[] f= new Finger[10];
'PFont font;
'Smsg serialmsg;
'int initial=-1;
'boolean waiting;
'int count=0;

'//Global Variables
'  boolean Play; //true if in Play mode false if in Menu mode
'  int playmsg=64;
'  int menumsg=61;
 
'void setup()
'{
'  size(500,500);
'  background(0);
'  smooth();
  
'  //LOAD font
'  font = loadFont("ArialMT-48.vlw"); 
'  textFont(font, 12);  
  
'  //INITIALIZE Fingers
'  initializeF();
  
'  serialmsg= new Smsg(0,0,0);
'  Play=true;
  
'  println(Serial.list());
'  myPort = new Serial(this, Serial.list()[0], 9600);
  
'  //initialize getting to punctuation bit
'  while(initial != 101)
'  {
'    if (myPort.available() > 0)
'    {
'      initial = myPort.read();
'    }
'  }
'  waiting = false;
  
'} 

'void draw()
'{
'  background(0);
 
'  readPORT();
'  renderF();
'  resetFvel();
'}

'/////////////////////////

'//This stands in for the serial event code
'  void keyPressed() 
'  { 
'     int code = int(key)-48;
'     serialmsg.set1(code);
'     evalmsg(serialmsg);
'  }
  
'  void readPORT()
'  {
'    int code;
    
'    if (myPort.available() > 0 && waiting==true)
'    {
'      code = myPort.read();
'      if(code == 101)
'      {
'        waiting=false;
'        count=0;
'      }
'    }
'    else if (myPort.available() > 0 && waiting==false)
'    {
'      code = myPort.read();
'        if(count==0)
'        {
'          serialmsg.set1(code);
'          count=count+1;
'        }
'        if(count==1)
'        {
'          serialmsg.set2(code);
'          waiting=true;
'          evalmsg(serialmsg);
          
'        }
'    }
'  }
  
'  //END SERIAL SYSTEM


'  void evalmsg(Smsg SEmsg)
'  {
'   int msg;
'   //parse message apart here, assume its a array
'     msg=SEmsg.get1();
   
'   if(msg >-1 && msg <10)
'     {
'       if(Play)
'       {
'         f[msg].setVELOCITY(255);
'       }
'       else if(Play==false && mousePressed==true)//Unlock
'       {
'         f[msg].setLOCKED(!f[msg].getLOCKED());
'       }
'       else
'       {
'         if(f[msg].getLOCKED()==false)
'         {
'           f[msg].setSOUND((f[msg].getSOUND()+1)%15);
'         }
'         else
'         {
'           f[msg].setVELOCITY(100);
'         }
'       }
'     }
      
'     if(msg == playmsg)
'     {
'       Play=true;
'       lockF();
'     }
'     if(msg == menumsg)
'     {
'       Play=false;
'     }
'  }
  
'  /////////////////////////////////////
  
'   void resetFvel()
'  {
'     for(int i=0; i < f.length; i++)
'     {
'       if(f[i].getVELOCITY() > 0)
'       {
'         f[i].setVELOCITY(f[i].getVELOCITY()-1);
'       }
'     }  
'  }

'  void lockF()
'  {
'     for(int i=0; i < f.length; i++)
'     {
'       f[i].setLOCKED(true);
'     }
'  }
  
'  void renderF()
'  {
'     for(int i=0; i < f.length; i++)
'     {
'       f[i].render();
'     }
'  }
  
'  void initializeF()
'  {
'     for(int i=0; i < f.length; i++)
'     {
'       f[i]= new Finger(100 + i*20, 100, i, 0, true);
'     }
'  }
  
'  //////////////////////////////////////
  
'  class Finger
'{
'  float x, y;
'  int sound;
'  float velocity;
'  boolean locked;
'  String[] sounds= new String[15];
  
'  Finger(float x_, float y_, int sound_, float velocity_, boolean locked_)
'  {
'    x=x_;
'    y=y_;
'    sound=sound_;
'    velocity=velocity_;
'    locked=locked_;
    
'    sounds[0]="a";
'    sounds[1]="b";
'    sounds[2]="c";
'    sounds[3]="d";
'    sounds[4]="e";
'    sounds[5]="f";
'    sounds[6]="g";
'    sounds[7]="h";
'    sounds[8]="i";
'    sounds[9]="j";
'    sounds[10]="k";
'    sounds[11]="l";
'    sounds[12]="m";
'    sounds[13]="n";
'    sounds[14]="o";  
'  }//END Finger
  
'  int getSOUND() {return sound;}
'  void setSOUND(int sound_) {sound = sound_;}
'  float getVELOCITY() {return velocity;}
'  void setVELOCITY(float v_) {velocity=v_;}
'  boolean getLOCKED() {return locked;}
'  void setLOCKED(boolean l_) {locked=l_;}
  
'  void render()
'  {
'    noStroke();
'    fill(255-velocity);
    
'    ellipse(x,y,10,10);
'    text(sounds[sound], x, y-20);
   
'    if(locked==false)
'    {
'       for(int i=0; i<sounds.length; i++)
'       {
'         if(i==sound)
'           {fill(255,0,0);}
'         else
'           {fill(255);}
'         text(sounds[i],x,y+i*15+20);
'       }      
'      }   
'  }//END render


'}//END Finger

'////////////////////////////////////

'class Smsg
'{
'  int b1, b2, b3;
  
'  Smsg(int b1_, int b2_, int b3_)
'  {
'    b1=b1_;
'    b2=b2_;
'    b3=b3_;
'  }

'  int get1() {return b1;}
'  void set1(int b1_) {b1 = b1_;}
'  int get2() {return b2;}
'  void set2(int b2_) {b2 = b2_;}
'  int get3() {return b3;}
'  void set3(int b3_) {b3 = b3_;}

'}