import stamp.core.*;
import stamp.util.text.*; // http://www.parallax.com/javelin/applications.asp#AN012
import stamp.peripheral.x10.insteon.*; // http://www.cls01.com/insteon

/*******************************************************************************
* Test program for INSTEON Class
* (http://www.cls01.com/insteon)
*
* @author Scott Herr (http://www.cls01.com/insteon)
* @version 0.03 April 23, 2006
*******************************************************************************/

public class InsteonTest {

  // INSTEON PowerLinc V2 Settings
  static final int PLCpinTX = CPU.pin9;
  static final int PLCpinRX = CPU.pin10;
  static Uart PLCtx = new Uart(Uart.dirTransmit, PLCpinTX, Uart.dontInvert, Uart.speed4800, Uart.stop1);
  static Uart PLCrx = new Uart(Uart.dirReceive, PLCpinRX, Uart.dontInvert, Uart.speed4800, Uart.stop1);
  static Timer PLCtimer = new Timer();
  static Insteon PLC = new Insteon(PLCtx,PLCrx,PLCtimer);

  // CONSTANTS
  final static String version = "InsteonTest V0.03";
  final static char[] testDimmer = {0x06,0x14,0x47}; // INSTEON address

  // VARIABLES
  static char[] eventBuffer = new char[32];
  static int rtnCode;

  //****************************************************************************
  // Main Program
  //****************************************************************************
  static void main() {
    int i;
    Format.printf("PROGRAM: %s\n",version);
    Format.printf("- Javelin free memory = %d\n",Memory.freeMemory());
    // Get version information
    rtnCode = PLC.getVersion();
    if (rtnCode < 0) {
      Format.printf("Error %d reading PLC version\n",rtnCode);
    }
    else {
      Format.printf("- PLC INSTEON address = %02x.",PLC.address[0] & 0xff);
      Format.printf("%02x.",PLC.address[1] & 0xff);
      Format.printf("%02x\n",PLC.address[2] & 0xff);
      Format.printf("- INSTEON device type = 0x%04x\n",PLC.deviceType);
      Format.printf("- PLC firmware = 0x%02x\n",PLC.firmware & 0xff);
    }
    // X10 A1 ON
    rtnCode = PLC.sendX10('A',1,Insteon.X10_ON);
    if (rtnCode < 0) Format.printf("Error = %d\n",rtnCode);
    else             Format.printf("*A1 ON*\n");
    // X10 A1 OFF
    CPU.delay(20000);
    rtnCode = PLC.sendX10('A',1,Insteon.X10_OFF);
    if (rtnCode < 0) Format.printf("Error = %d\n",rtnCode);
    else             Format.printf("*A1 OFF*\n");
    // X10 A1 DIM 10 times
    CPU.delay(20000);
    rtnCode = PLC.sendX10('A',1,Insteon.X10_DIM,10);
    if (rtnCode < 0) Format.printf("Error = %d\n",rtnCode);
    else             Format.printf("*A1 DIM*\n");
    // X10 A1 OFF
    CPU.delay(20000);
    rtnCode = PLC.sendX10('A',1,Insteon.X10_OFF);
    if (rtnCode < 0) Format.printf("Error = %d\n",rtnCode);
    else             Format.printf("*A1 OFF*\n");
    // INSTEON Lamp Dimmer Dim 50%
    CPU.delay(20000);
    rtnCode = PLC.sendInsteonCmd(Insteon.CMDTYPE_DIRECT,
      testDimmer,Insteon.CMD_ON,(char)0x80);
    if (rtnCode < 0) Format.printf("Error = %d\n",rtnCode);
    else             Format.printf("*Dimmer 50%%*\n");
    // INSTEON Lamp Dimmer Off
    CPU.delay(20000);
    rtnCode = PLC.sendInsteonCmd(Insteon.CMDTYPE_DIRECT,
      testDimmer,Insteon.CMD_OFF,(char)0);
    if (rtnCode < 0) Format.printf("Error = %d\n",rtnCode);
    else             Format.printf("*Dimmer Off*\n");
    // INSTEON Lamp Dimmer Full On
    CPU.delay(20000);
    rtnCode = PLC.sendInsteonCmd(Insteon.CMDTYPE_DIRECT,
      testDimmer,Insteon.CMD_ON,Insteon.CMD2_FULL_ON);
    if (rtnCode < 0) Format.printf("Error = %d\n",rtnCode);
    else             Format.printf("*Dimmer On*\n");
    // Monitor for events
    Format.printf("Monitoring for events...\n");
    while (true) {
      // Poll for events
      rtnCode = PLC.poll();
      if (rtnCode < 0) Format.printf("Poll error %d\n",rtnCode);
      // Read events from buffer
      while (PLC.readNextEvent(eventBuffer) == 0) {
        processEvent();
        //DEBUG Format.printf("- Javelin free memory = %d\n",Memory.freeMemory());
      }
    }
  }

  //****************************************************************************
  // Subroutines
  //****************************************************************************

  /**
   * processEvent: Take action for interesting events
   */
  static void processEvent() {
    int i;
    // Display event details
    Format.printf("Event: ");
    for (i=1;i<=eventBuffer[0];i++) {
      Format.printf("[%02x]",(eventBuffer[i] & 0xff));
    }
    Format.printf("\n");
    // X10 and INSTEON events would be processed next. See InsteonDemo1
    // program for an example.
    // ...
  } // END processEvent

} // END class