예제 #1
0
  public static int readLine(
      InputStream in, OutputStream out, Display display, boolean echo, byte[] buf, int maxLen) {
    // get a line from the input stream into the buffer buf, of max length maxLen
    // line is terminated with a return character;
    // echo back to output stream
    // also write to line 0 of the display, up to 16 characters
    try {
      int data;
      int len = 0;
      maxLen--; // make room for a null byte
      while ((data = in.read()) != -1) {
        if (data == 0) continue;
        if (data == '\n') continue;
        if (echo) out.write(data); // echo it back
        byte ch = (byte) data;

        if (ch == '\r') {
          if (echo) out.write('\n');
          int dlen = len; // LCD panel display length
          if (dlen > 16) dlen = 16;
          byte[] screenBuf = new byte[dlen];
          for (int i = 0; i < dlen; i++) screenBuf[i] = buf[i];
          display.print(0, screenBuf); // echo it to the display
          buf[len] = 0;
          return len;
        } else if (len < maxLen) {
          buf[len++] = ch;
        }
      }
    } catch (Exception e) {
      display.print(0, "oops!");
    }
    return -1;
  }
예제 #2
0
 public static void writeCRLF(OutputStream out, String str) {
   try {
     char[] ary = str.toCharArray();
     int len = ary.length;
     for (int i = 0; i < len; i++) out.write((int) ary[i]);
     out.write('\r'); // CR
     out.write('\n'); // LF
   } catch (Exception e) {
     e.printStackTrace();
   }
 }