/*
 DOSonChip terminal
 language: processing

 This program is a basic serial terminal program.
 It replaces newline characters from the keyboard
 with return characters.

 created 2 Jan 2008

 by Tom Igoe
 */

import processing.serial.*;

Serial myPort;           // the serial port you're using
String portnum;          // name of the serial port
String outString = "";   // the string being sent out the serial port
String inString = "";    // the string coming in from the serial port
int receivedLines = 0;   // how many lines have been received in the serial port
int bufferedLines = 10;  // number of incoming lines to keep

void setup() {
  size(400, 300);        // window size

  // create a font with the second font available to the system:
  PFont myFont = createFont(PFont.list()[2], 14);
  textFont(myFont);

  // list all the serial ports:
  println(Serial.list());

  // based on the list of serial ports printed from the
  //previous command, change the 0 to your port's number:
  portnum = Serial.list()[0];
  // initialize the serial port:
  myPort = new Serial(this, portnum, 9600);
  println(makeDate());

}

void draw() {
  // clear the screen:
  background(0);
  // print the name of the serial port:
  text("Serial port: " + portnum, 10, 20);
  // Print out what you get:
  text("typed: " + outString, 10, 40);
  text("received:\n" + inString, 10, 80);
}

// This method responds to key presses when the
// program window is active:
void keyPressed() {
  switch (key) {
    // in OSX, if the user types return,
    // a linefeed is returned.  But to
    // communicate with the DOSonChip, you want a carriage return:
  case 'a':
    // set the baud rate for the Dosonchip with three returns:
    println("setting baud rate");
    myPort.write("\r\r\r");
    // set the time and date:
    delay(100);
    myPort.write("t " + makeDate() + "\r");
    break;
  case '\n':      /// send out whatever the person typed:
    myPort.write(outString + "\r");
    outString = "";
    break;
    case 'f':  // open a file on the dosonchip for writing:
    openFile();
    break;
    case 'z':   // write some random data to the card:
    writeData(int(random(1023)), 1);
    break;
  case 8:    // backspace
    // delete the last character in the string:
    outString = outString.substring(0, outString.length() -1);
    break;
  case 65535:  // If the user types the shift key, don't type anything:
    break;
    // any other key typed, add it to outString:
  default:
    // add the key to the end of the string:
    outString += key;
    break;
  }
}

// this method runs when bytes show up in the serial port:
void serialEvent(Serial myPort) {
  // read the next byte from the serial port:
  int inByte = myPort.read();
  // add it to  inString:
  inString += char(inByte);
  if (inByte == '\r') {
    // if the byte is a carriage return, print
    // a newline and carriage return:
    inString += '\n';
    // count the number of newlines:
    receivedLines++;
    // if there are more than 10 lines, delete the first one:
    if (receivedLines >  bufferedLines) {
      deleteFirstLine();
    }
  }
}
// deletes the top line of inString so that it all fits on the screen:
void deleteFirstLine() {
  // find the first newline:
  int firstChar = inString.indexOf('\n');
  // delete it:
  inString= inString.substring(firstChar+1);
}

void writeData(int dataValue, int fileHandle) {
  String dataString = makeDate() + "\t" + dataValue + "\n\r";
  String writeCommand = "w #" + fileHandle + " " + (dataString.length()) + "\r";
  println(dataString);
  println(dataString.length());
  myPort.write(writeCommand);
  delay(300);
  myPort.write(dataString);
}

void openFile() {
  String writeCommand = "ow 1.TXT\r";
  myPort.write(writeCommand);


}
String makeDate() {
  String today = "";
  if (minute() < 10)
    today += "0";
  today += minute() + ":";
  if (hour() < 10)
    today += "0";
  today += hour() + ":";
  if (day() < 10)
    today += "0";
  today += day() + ":";
  if (month() < 10)
    today += "0";
  today += month() + ":";

  String shortYear = "0" + (year() % 100);
  return today + shortYear;
}