// SRF08 Motor controller - Javalin Control

/* Notes:-

  See www.robot-electronics.co.uk for full details of the SRF08 hardware
  see www.parallax.com for details on the Javalin Stamp and the Board of Education development board (BOE)

  PIN OUTS:
    14 -> I2CBUS - SDA
    15 -> I2CBUS - SCL

  See the srf08 documentation for wiring diagram.

  This example programmed by James Burrows, on an Javalin Stamp v1, using the SRF08 distance sensor and
  the Parallax BOE.

  I have used 4.7K resisters to pull up the SDA and SCL lines to +5V - again see the SRF08 documenation diagram
*/

import stamp.core.*;
import stamp.peripheral.io.I2C;

public class srf08example
{
  final static char CLS = '\u0010';
  final static char HOME = 0x01;

  // phillips i2c bus --> SDA on pin 14; SCL on pin 15
  static I2C i2cBus = new I2C(CPU.pin14, CPU.pin15);

  // variable for direction.
  static int Distance=0;
  static int Light=0;

  // temp variables
  static int DataHigh = 0;
  static int DataLow = 0;

  // static constants for the SRF08 Address
  final static int SRF08_ADDRESS = 0xE0;
  final static int SRF08_ADDRESS_WRITE = 0xE1;

  // constant for getting data from SRF08
  final static int SRF08_GET_LIGHT = 0x1;
  final static int SRF08_GET_DISTANCE = 0x2;

  public static void main()
  {
    System.out.println(CLS);
    System.out.print(HOME);
    System.out.println("SRF08 Example:");

    while (true)
    {
      InitSRF08();                                          // Initialize the sensor

      CPU.delay(4000);                                      // need to wait a bit for the ranging to complete

      Light = getSRF08Data(SRF08_GET_LIGHT);                // light
      Distance = getSRF08Data(SRF08_GET_DISTANCE);          // Distance
      System.out.print("SRF08 Light is ");
      System.out.print(Light);
      System.out.print("  Distance(cm) is ");
      System.out.println(Distance);

      // wait a sec...
      CPU.delay(20000);
    }
  }


  public static void InitSRF08()
  {
      // tell the SRF08 via the i2cbus to start ranging.....
      System.out.print("SRF08 is initializing.....");
      i2cBus.start();                           //Send start byte
      i2cBus.write(SRF08_ADDRESS);              // srf08 address
      CPU.delay(1);
      i2cBus.write(0x0);                        //Specify Register
      CPU.delay(1);
      i2cBus.write(0x51);                       // cm rangeing
      i2cBus.stop();
      System.out.println("done");
  }

  public static int getSRF08Data(int register)
  {
    // get the SRF08 ranging data
    System.out.print("Reading SRF08 Data ");
    System.out.print(register);
    System.out.print(".....");

    i2cBus.start();                             // Send start byte
    i2cBus.write(SRF08_ADDRESS);                // srf08 address
    CPU.delay(1);
    i2cBus.write(register);                     // Specify Register (0x1 = light, 0x2 is distance)
    i2cBus.start();
    i2cBus.write(SRF08_ADDRESS_WRITE);          // srf08 address
    CPU.delay(1);
    DataHigh = i2cBus.read(0);                  // first byte, no ack
    DataLow = i2cBus.read(1);                   // second byte, ack
    i2cBus.stop();

    System.out.println("done");
    return(DataHigh+DataLow);
  }
}

