/*
Copyright 2007, Freie Universitaet Berlin. All rights reserved.

These sources were developed at the Freie Universität Berlin, Computer
Systems and Telematics group.

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 Freie Universitaet Berlin (FUB) 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 FUB and the contributors on an "as is"
basis, without any representations or warranties of any kind, express
or implied including, but not limited to, representations or
warranties of non-infringement, merchantability or fitness for a
particular purpose. In no event shall FUB 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.

This implementation was developed by the CST group at the FUB.

For documentation and questions please use the web site
http://scatterweb.mi.fu-berlin.de and the mailinglist
scatterweb@lists.spline.inf.fu-berlin.de (subscription via the Website).
Berlin, 2007
*/

#include "ScatterWeb.System.h"

typedef struct {
  uint16_t x;
  uint16_t y;
  uint16_t z;
} TD_t;

/**
 *    gs_1   gs_2   type    unit
 *      0     0     1.5      g
 *      1     0     2.0      g
 *      0     1     4.0      g
 *      1     1     6.0      g
 */
#define SET_3D_1_5G do { P5OUT &= ~0x30; } while(0)
#define SET_3D_2_0G do { P5OUT &= ~0x30; P5OUT |= 0x10; } while(0)
#define SET_3D_4_0G do { P5OUT &= ~0x30; P5OUT |= 0x20; } while(0)
#define SET_3D_6_0G do { P5OUT &= ~0x30; P5OUT |= 0x30; } while(0)

#define SET_3D_OFF do { P5OUT &= ~0x40; } while(0)
#define SET_3D_ON do { P5OUT |= 0x40; } while(0)

fp_vp _enableFunc;
fp_vp _disableFunc;
fp_vp _readFunc;

bool _Data_redOn() {
  do{ P5OUT &= ~0x80; }while(0);
  return True;
}

bool _Data_redOff() {
  do{ P5OUT |=  0x80; }while(0);
  return True;
}

bool _Data_redLed() {
  return !(P5OUT & 0x80);
}

bool _Data_set3DSensitivity(uint16_t sen) {
  switch(sen) {
    case 2: SET_3D_2_0G; break;
    case 4: SET_3D_4_0G; break;
    case 6: SET_3D_6_0G; break;
    default: SET_3D_1_5G; break;
  }
  return True;
}

TD_t* _Data_get3D(TD_t* value) {
  value->x = ADC12MEM0;
  value->y = ADC12MEM3;
  value->z = ADC12MEM6;
  return value;
}

bool Data_init(fp_vp driver) {
  Data_boardID = 1;
  Data_channels = 2;
  SET_3D_1_5G;
  _enableFunc = 0;
  _disableFunc = 0;
  _readFunc = 0;
  if(driver != 0) ((fp_vp)driver)(NULL);
  return True;
}

bool Data_enable(uint8_t channel, uint8_t parameter_length, uint8_t* parameter) {
  if(channel >= Data_channels) return False;
  if(channel == 0) _Data_redOn();
  if(channel == 1) {
    if(parameter_length != 2) return False;
		SET_3D_ON;
    uint16_t temp = parameter[0];
    temp = temp << 8;
    temp = temp + parameter[1];
    _Data_set3DSensitivity(temp);
  }
  if(channel > 1) if(_enableFunc != 0) ((fp_vp)_enableFunc)(&channel);
  return True;
}

bool Data_disable(uint8_t channel) {
  if(channel >= Data_channels) return False;
  if(channel == 0) _Data_redOff();
  if(channel == 1) SET_3D_OFF;
  if(channel > 1) if(_disableFunc != 0) ((fp_vp)_disableFunc)(&channel);
  return True;
}

sdata_t Data_read(uint8_t channel) {
  Data_sensorValue.board = Data_boardID;
  Data_sensorValue.channel = 255;
  Data_sensorValue.timeStamp.secs = 0;
  Data_sensorValue.timeStamp.millis = 0;
  Data_sensorValue.value_length = 0;
  if(channel >= Data_channels) return Data_sensorValue;
  if(channel == 0) {
    Data_sensorValue.channel = 0;
    Data_sensorValue.value_length = 1;
    uint8_t temp = _Data_redLed() ? 1 : 0;
    Data_sensorValue.timeStamp = CURRENT_TIME;
    Data_sensorStream[0] = temp;
    Data_sensorValue.value = Data_sensorStream;
    return Data_sensorValue;
  }
  if(channel == 1) {
    Data_sensorValue.channel = 1;
    Data_sensorValue.value_length = 6;
    TD_t temp;
    _Data_get3D((TD_t*) &temp);
    Data_sensorValue.timeStamp = CURRENT_TIME;
    Data_sensorStream[1] = temp.x;
    Data_sensorStream[0] = temp.x >> 8;
    Data_sensorStream[3] = temp.y;
    Data_sensorStream[2] = temp.y >> 8;
    Data_sensorStream[5] = temp.z;
    Data_sensorStream[4] = temp.z >> 8;
    Data_sensorValue.value = Data_sensorStream;
    return Data_sensorValue;
  }
  if(channel > 1) if(_readFunc != 0) ((fp_vp)_readFunc)(&channel);
  return Data_sensorValue;
}

uint8_t Data_getBoardID() {
  return Data_boardID;
}

uint8_t Data_getChannels() {
  return Data_channels;
}

bool Data_registerDriver(fp_vp enableFunc, fp_vp disableFunc, fp_vp readFunc) {
  _enableFunc = enableFunc;
  _disableFunc = disableFunc;
  _readFunc = readFunc;
  return True;
}

/**
 * @brief Acceleration (read 3D): <code>rtd</code>
 * @ingroup commands
 *
 * Read acceleration raw data from the accelerometer.
 */
COMMAND(rtd, 0, cmdargs){
	TD_t temp;
	_Data_get3D(&temp);
	//String_write(cmdargs->response_args, "x:%i y:%i z:%i", temp.x, temp.y, temp.z);
	snprintf(cmdargs->response_args, cmdargs->response_bufsize,
			"x:%u y:%u z:%u",
			temp.x, temp.y, temp.z);
}

/**
 * @brief LED: <code>led [option]</code>
 * @ingroup commands
 * @param option '0' to switch the led off, '1' to switch it on
 * and omitted to read the current state
 *
 * Read the state of the LED or switch it on or off.
 */
COMMAND(led, 0, cmdargs) {
	if(cmdargs->arg_size) {
		if(*cmdargs->args == '1')
			_Data_redOn();
		else if(*cmdargs->args == '0')
			_Data_redOff();
		else
			snprintf(cmdargs->response_args, cmdargs->response_bufsize, "unknown parameter value: %c", *cmdargs->args);
	}
	else {
		snprintf(cmdargs->response_args, cmdargs->response_bufsize,
			"%c", (_Data_redLed() ? '1' : '0'));
	}
}
