// Multi5.java by JWB 1 June 2006
// does same function/circuit as Multi3.bpe but uses Javelin/Java code
// based on Multi4.java that had new class temp1 (DS1620), and LCD formatting
// using Format.bprintf(..) to convert and format data for output
// this version will add new ADC0838 class (based on ADC0831 and Multi3..)

import stamp.core.*;
import stamp.peripheral.sensor.temperature.DS1620;
import stamp.peripheral.lcd.SerialLCD;
import stamp.util.text.Format;

public class Multi5 {
  static final int LCD_PIN    = CPU.pin14;
  final static int READ_TEMP     = 0xAA;

  public static void main() {

    char[] line1 = new char[20];   // LCD output line 1
    char[] line2 = new char[20];    // LCD output line 1
    int prog = 1;
    int scans = 222;
    int lgt = 33;
    int pot = 44;
    int tmp = 55;
    int switches = 0;

    //prepLines2( line1, line2, prog, scans, lgt, pot, tmp );
    /*temp1 indoor = new temp1(CPU.pin15,CPU.pin0,CPU.pin13);
    tmp = indoor.dsTemp(READ_TEMP)/2;

    Uart txOut = new Uart(Uart.dirTransmit, LCD_PIN, Uart.dontInvert,
                          Uart.speed9600,Uart.stop1);

    SerialLCD myLCD = new SerialLCD(txOut);
    myLCD.clearScr();
    myLCD.displayOn();
    CPU.delay(1000);
    myLCD.write(line1);
    myLCD.moveTo( myLCD.LINE2, 0);
    myLCD.write(line2);*/

    ADC0838 adc = new ADC0838(6,0,5);         // dataPin, clockPin, enablePin
                                // ADC         14 in, 17out   16       18
                                // Javelin          6         0        5

    while (true) {                           // main sample and display loop
      //tmp = indoor.dsTemp(READ_TEMP)/2;

      //switches = read_165();
      //Format.printf("switches=%02i \n", switches);

      lgt = adc.read(0);     // read lgt from chn 0
      lgt = (lgt*100)/255;
      pot = adc.read(1);     // read pot from chn 1
      pot = (pot*100)/255;
      Format.printf(" lgt=%02i",lgt);
      Format.printf(" pot=%02i \n",pot);

      /*prepLines2( line1, line2, prog, scans, lgt, pot, tmp );
      myLCD.moveTo( myLCD.LINE1, 0);
      // note: added write(char[] c) to SerialLCD class
      myLCD.write(line1);
      myLCD.moveTo( myLCD.LINE2, 0);
      myLCD.write(line2);
      //log( "tmpC=" + tmp );   e> causes out of memory (memory leak) when looping many times
      */
      CPU.delay(500);
    }
  }

 /*public static void prepLines2( char[] line1, char[] line2, int prog, int scans,
    int lgt, int pot, int tmp )
    {
      int k = 0;
      k=Format.bprintf(line1,k,"Prog:%01i",prog);
      k=Format.bprintf(line1,k," 100S:%03i",scans);
      k=0;
      k=Format.bprintf(line2,k,"Lgt:%02i",lgt);
      k=Format.bprintf(line2,k," Pot:%02i",pot);
      k=Format.bprintf(line2,k," %02i",tmp);
    }*/

  // use the following only outside large loops and for debug only
  // because it produces a memory leak ..orphaned Strings..
  /*public static void log( String s ) { System.out.println(s); }
  public static int read_165()
  { int dataIn = 0;
    CPU.writePin(0, false);   // set clockpin low for + pulses
    CPU.writePin(4,true);    // triggers on high to low
    CPU.delay(20);
    CPU.writePin(4,false);
    dataIn = CPU.shiftIn(1,0,8,CPU.PRE_CLOCK_MSB);
     // dataPin, clockPin, #bits_to_read, timing/order
    return dataIn;
  }*/
};  // end Multi5 