// ECG Graphing sketch v2 - Sep13 - ecg.htm

// (based on original sketch http://arduino.cc/en/Tutorial/Graph)
   // press X to exit, S to save image to disk, C to clear display  import processing.serial.*;
 PrintWriter output; // for outputting text file  PFont f; // font for text    Serial myPort;        // The serial port  int xPos = 0; // horizontal position of the graph  int lastxPos = 0; // last point drawn  float lastinByte = 0; // last data plotted    void setup () {
   // output text file    output = createWriter("ecg.txt");
    
   // Create the font    println(PFont.list());
   f = createFont("Ariel", 14);
   textFont(f);
      
   // set the window size:    size(displayWidth, 300);
   
   // Serial port    println(Serial.list());
   myPort = new Serial(this, Serial.list()[0], 9600);
   myPort.bufferUntil('\n'); // don't generate a serialEvent() unless you get a newline character:       // set inital background:    background(255,255,255);
 }
 
 
 void scale() {
   // draw scale    stroke(#4EA53E);
   int sdiv = 100; // division spacing (arduino samples once every 10 ms so 100 samples = 1 second)    for (int i = 0; i <= width; i=i+sdiv) {
     line(i, height - 10 , i, 25 );
   }
   // text    fill(0, 102, 153);
   text("E.C.G. " + day() + "/" + month() + "/" + year() + " at " + hour() + ":" + minute() + " (1 second per division)", 20, 20);
 }


 void draw () {
    // all done in serial event  }      void clear() {
    // clear screen and start again        xPos = 0;        lastxPos = 0;        background(255,255,255);
 }
 
 
 void keyReleased() {
   // key pressed     switch (key) {
       case 'c': // clear screen          clear(); // clear screen and start again        break;
       case 's': // pause and save image          saveFrame("ecg###.jpg");
         delay(500);
       break;
       case 'x': // exit          output.flush(); // Writes the remaining data to the file          output.close(); // Finishes the file          exit(); // Stops the program        break;
    }
 }
 
 
 void serialEvent (Serial myPort) {
   // get the ASCII string:    String inString = myPort.readStringUntil('\n');
   
   if (inString != null) {
     inString = trim(inString); // trim off any whitespace:      float inByte = float(inString); // convert to an int            if (inByte < 0) {
       // bpm info received        fill (255,255,255); // erase last bpm        stroke (255,255,255);
       rect(500,8,100,18);
       stroke(0,0,0); // display bpm        fill(0, 102, 153);
       text("B.P.M. = " + int(-inByte) , 500, 20);
     }
     else {
       // raw data in        inByte = map(inByte, 0, 500, 0, height); // and map to the screen height                // if start of screen then draw scale        if (xPos == 0) scale();
       
         // draw the line:          stroke(0,0,0);
         line(lastxPos, height - lastinByte , xPos, height - inByte);
         lastxPos = xPos;   // store last point plotted          lastinByte = inByte;          output.println(inByte); // save the value to text file                          // at the edge of the screen, go back to the beginning:          if (xPos >= width) {
            clear(); // clear screen and start again          }          else {
           // increment the horizontal position:            xPos++;          }        }    }  }