/***************************************************************************
 * Copyright (c) 2005, Research Group Embedded Interaction                 *
 * Matthias Kranz <matthias@hcilab.org>                                    *
 * Paul Holleis <paul@hcilab.org>                                          *
 * Albrecht Schmidt <albrecht@hcilab.org>                                  *
 * All rights reserved.                                                    *
 *                                                                         *
 * Redistribution and use in source and binary forms, with or without      *
 * modification, are permitted provided that the following conditions      *
 * are met:                                                                *
 *                                                                         *
 *    * Redistributions of source code must retain the above copyright     *
 *      notice, this list of conditions and the following disclaimer.      *
 *    * Redistributions in binary form must reproduce the above copyright  *
 *      notice, this list of conditions and the following disclaimer in    *
 *      the documentation and/or other materials provided with the         *
 *      distribution.                                                      *
 *    * Neither the name of the <ORGANIZATION> nor the names of its        *
 *      contributors may be used to endorse or promote products derived    *
 *      from this software without specific prior written permission.      *
 *                                                                         *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS     *
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT       *
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR   *
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT    *
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,   *
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT        *
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,   *
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON       *
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR      *
 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF      *
 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH    *
 * DAMAGE.                                                                 *
 **************************************************************************/
// APPLICATION FILE
// uses ACK, RTC & PRS.

// own pins and stuff
#include "orb.h" 
// Color changes: red -> green -> blue (with intermediates for mixed colors)
#define ORB_COLOR_RED   0
#define ORB_COLOR_GREEN 1
#define ORB_COLOR_BLUE  2
#define ORB_COLOR_OFF   10
#define ORB_COLOR_ON    11

#define ORB_PACKET_TYPE_HI 98
#define ORB_PACKET_TYPE_LO 180

// function declarations:
#separate void SlotEndCallBack();
void powerOffOrb();
void powerOnOrb();
void initOrb();
void stopOrb();
void startOrb();
void processRFData();
void main();

// this function is called from the fsm at the end of an rf slot
// make sure that it terminates in time
#separate
void SlotEndCallBack()
{
  RTCUpdate();
  ACLACKRun();
  SDJSRun();
}


/**
 * Initializes pins for ledball.
 * defines pins used as output
 * powers ledball off
 * defines listener for ORB (from ambient orb) packets
 */
void initOrb() {
  // define pins used as output pins
  bit_clear(TRIS_CONN_03);
  bit_clear(TRIS_CONN_06);
  // start with a defined state - switched out
  powerOffOrb();
  //DelayMs(100);
  ACLSubscribe(ORB_PACKET_TYPE_HI, ORB_PACKET_TYPE_LO);
}

void powerOnOrb() {
    output_high(PIN_CONN_03);
}

void powerOffOrb() {
    output_low(PIN_CONN_03);
}

void stopOrb() {
    output_low(PIN_CONN_06);
    DelayMs(100);
    output_high(PIN_CONN_06);
    DelayMs(100);
    output_low(PIN_CONN_06);
}

void startOrb() {
    output_low(PIN_CONN_06);
    DelayMs(100);
    output_high(PIN_CONN_06);
    DelayMs(100);
    output_low(PIN_CONN_06);
}

void setOrbColor(int color) {
  switch (color){
    case ORB_COLOR_RED: {
      // red          
      //#define ORB_COLOR_RED   0
    output_low(PIN_CONN_06);
      powerOffOrb();
      powerOnOrb();
      DelayMs(25700);
      stopOrb();
      break;
    }
    case ORB_COLOR_GREEN: {
      // green
      //#define ORB_COLOR_GREEN 1
    output_low(PIN_CONN_06);
      powerOffOrb();
      powerOnOrb();
      DelayMs(7800);
      stopOrb();
      break;
    }
    case ORB_COLOR_BLUE: {
    output_low(PIN_CONN_06);
      // blue
      //#define ORB_COLOR_BLUE  2
      powerOffOrb();
      powerOnOrb();
      DelayMs(17000);
      stopOrb();
      break;
    }
    case ORB_COLOR_OFF: {
    output_low(PIN_CONN_06);
      // off
      //#define ORB_COLOR_OFF   10
      powerOffOrb();
      break;
    }
    case ORB_COLOR_ON: {
    output_low(PIN_CONN_06);
      // off
      //#define ORB_COLOR_ON   11
      powerOnOrb();
      break;
    }

    default: {
      break;
    }
  }
}

void processRFData() {
  signed int datalength = 0;
  int8 * rec_data;
  datalength = ACLGetReceivedDataLength(ORB_PACKET_TYPE_HI, ORB_PACKET_TYPE_LO);
  // 0 = no data, -1 data type not found
  if (datalength > 0)
  {
    int8 color;
    rec_data = ACLGetReceivedData(ORB_PACKET_TYPE_HI, ORB_PACKET_TYPE_LO);
    ACLLockReceiveBuffer();
    
    setOrbColor(rec_data[0]);
    
    ACLReleaseReceiveBuffer();
  }
  ACLSetDataToOld();
}

void main()
{
  // variable declarations go here


  //TYPICAL STARTUP FLOW
  //*****************************************************************
  // check if board is there, doesn't harm any settings, those are restored
  // is not dangerous, because all pins are set correct . 
  // bport is input, i2c and eeprom are initianlized as well
  PCInit();                     
  //SSimpInit();
  // init the stack and start it
  ACLInit();                      
  // must be done before lifesign and
  enable_interrupts(global);              

  //send out the result until board is away
  // PCSelfTestBoardIsThere(): this function does 
  // not harm the settings (it restores always)

  // now start your code here
  /*
  // e.g.: display board init
  SDisplayInit();
  // go on with initialization of the sensors
  SDisplaySensorsInit();
  // turn off sensors after initialization
  SDisplaySensorsOff();
  */
  
  // init the rest:
  ACLACKInit();
  RTCInit();
  SDJSInit();

  initOrb(); 
  
  
    output_low(PIN_CONN_06);
    DelayMs(100);
  // rest of your code goes here:
  while(TRUE)
  {
    
    if (ACLDataIsNew()) {
      processRFData();
    }
      
    // code
    /*
    setOrbColor(ORB_COLOR_RED);
    DelayMs(10000);
    etOrbColor(ORB_COLOR_GREEN);
    DelayMs(10000);
    */
    /*
    powerOnOrb();
    DelayMs(17000);
    stopOrb();
    */
    //powerOnOrb();
    //setOrbColor(ORB_COLOR_RED);

  }
}

