/*
Copyright 2003/2004, 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.
Contributors: Marco Hutta

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, 2003/2004/2005
*/

#ifndef ScatterWeb_Data
#define ScatterWeb_Data

#include "ScatterWeb.Time.h"

// States of Sensors
#define REDSTATE            (Data_redLed())
#define YELLOWSTATE         (Data_yellowLed())
#define GREENSTATE          (Data_greenLed())

// Bitmasks for the different sensor flags (::sensorFlags)
// movement sensor (on P13) 
#define SENSOR_MOVEMENT     (0x08)
// vibration sensor (on P14)
#define SENSOR_VIBRATION    (0x10)
// button (on P27)
#define SENSOR_BUTTON       (0x80)
// microphone (on P60, see ScatterWeb.HAL.h).
#define SENSOR_MICROPHONE   (0x01)
// temperature sensor (DS1629 or LM71)
#define SENSOR_TEMPERATURE  (0x02)
// RC5 receiving via IR diode
#define SENSOR_RC5          (0x04)
// supply voltage (not implemented, access direct via ScatterWeb.HAL.h)
#define SENSOR_VOLTAGE      (0x40)

typedef struct {
  UINT16 id;        ///< Id of node these readings belong to.
  UINT8 sensor;     ///< Must be one of ::SENSOR_PIR, ::SENSOR_VIB, ::SENSOR_BUTTON, ::SENSOR_MIC, ::SENSOR_TMP, ::SENSOR_RC5, ::SENSOR_VOLTAGE.
  time_t timeStamp; ///< Timestamp of sensor reading.
  UINT16 value;     ///< Value of the reading. 
} sdata_t;          ///< Holds sensor data record.

// Value of SensorFlags
extern UINT8 sensorFlags;

/*
 * Function to init Sensors
 *
 * @return bool     True if sensors available, otherwise False  
 *  
 */  
bool Data_init();

/*
 * Function to enable SensorPower
 * 
 * @return bool     True if sensors available, otherwise False    
 *
 */  
bool Data_on();

/*
 * Function to disable SensorPower
 * 
 * @return bool     True if sensors available, otherwise False    
 *
 */  
bool Data_off();

/*
 * Function to handle SensorFlags
 * 
 * @return bool     True if sensors available, otherwise False    
 *
 */  
bool Data_sensorHandler();

/*
 * Function to enable battery sensor
 * 
 * @param interval  interval for posting event
 * @param thresh    upper limit for posting event
 * 
 * @return bool     True if battery sensor available, otherwise False  
 *
 */
bool Data_batteryOn(UINT16 interval, UINT16 thresh);

/*
 * Function to disable battery sensor
 * 
 * @return bool     True if battery sensor available, otherwise False    
 *
 */
bool Data_batteryOff();

/*
 * Function to enable beeper
 *
 * @return bool     True if beeper sensor available, otherwise False  
 *  
 */
bool Data_beeperOn();

/*
 * Function to disable beeper
 * 
 * @return bool     True if beeper available, otherwise False    
 *
 */
bool Data_beeperOff();

/*
 * Function to toggle beeper
 * 
 * @return bool     True if beeper available, otherwise False    
 *
 */
bool Data_beeperToggle();

/*
 * Function to enable button sensor
 * 
 * @param delay     delay action
 * 
 * @return bool     True if button sensor available, otherwise False      
 *
 */
bool Data_buttonOn(UINT16 delay);

/*
 * Function to disable button sensor
 * 
 * @return bool     True if button sensor available, otherwise False    
 *
 */
bool Data_buttonOff();

/*
 * Function to enable green led
 * 
 * @return bool     True if green led available, otherwise False    
 *
 */
bool Data_greenOn();

/*
 * Function to disable green led
 * 
 * @return bool     True if green led available, otherwise False      
 *
 */
bool Data_greenOff();

/*
 * Function to toggle green led
 * 
 * @return bool     True if green led available, otherwise False      
 *
 */
bool Data_greenToggle();

/*
 * Function to enable red led
 * 
 * @return bool     True if red led available, otherwise False      
 *
 */
bool Data_redOn();

/*
 * Function to disable red led
 * 
 * @return bool     True if red led available, otherwise False        
 *
 */
bool Data_redOff();

/*
 * Function to toggle red led
 * 
 * @return bool     True if red led available, otherwise False        
 *
 */
bool Data_redToggle();

/*
 * Function to enable yellow led
 * 
 * @return bool     True if yellow led available, otherwise False        
 *
 */
bool Data_yellowOn();

/*
 * Function to disable yellow led
 * 
 * @return bool     True if yellow led available, otherwise False          
 *
 */
bool Data_yellowOff();

/*
 * Function to toggle yellow led
 * 
 * @return bool     True if yellow led available, otherwise False          
 *
 */
bool Data_yellowToggle();

/*
 * Function to enable microphone sensor
 * 
 * @param delay     delay action
 * @param diff      difference for posting event
 * 
 * @return bool     True if microphone sensor available, otherwise False      
 *
 */
bool Data_microphoneOn(UINT16 delay, UINT16 diff);

/*
 * Function to disable microphone sensor
 * 
 * @return bool     True if microphone sensor available, otherwise False        
 *
 */
bool Data_microphoneOff();

/*
 * Function to enable movement sensor
 * 
 * @param delay     delay action  
 * 
 * @return bool     True if movement sensor available, otherwise False        
 *
 */
bool Data_movementOn(UINT16 delay);

/*
 * Function to disable movement sensor
 * 
 * @return bool     True if movement sensor available, otherwise False        
 *
 */
bool Data_movementOff();

/*
 * Function to enable temperature sensor
 * 
 * @param interval  interval for posting event
 * @param range     range to start
 * @param diff      difference for posting event
 * @param spd       steps per diff  
 * 
 * @return bool     True if temperature sensor available, otherwise False        
 *
 */  
bool Data_temperatureOn(UINT16 interval, UINT16 range, UINT16 diff, UINT16 spd);

/*
 * Function to disable temperature sensor
 * 
 * @return bool     True if temperature sensor available, otherwise False        
 *
 */  
bool Data_temperatureOff();

/*
 * Function to enable vibration sensor
 * 
 * @param delay     delay action
 * 
 * @return bool     True if vibration sensor available, otherwise False        
 *
 */  
bool Data_vibrationOn(UINT16 delay);

/*
 * Function to disable vibration sensor
 * 
 * @return bool     True if vibration sensor available, otherwise False        
 *
 */  
bool Data_vibrationOff();

/*
 * Function to check green Led
 *  
 * @return UINT8  state of green Led
 *  
 */
UINT8 Data_greenLed();

/*
 * Function to check red Led
 *  
 * @return UINT8  state of red Led
 *  
 */
UINT8 Data_redLed();

/*
 * Function to check yellow Led
 *  
 * @return UINT8  state of yellow Led
 *  
 */
UINT8 Data_yellowLed();

/*
 * Function to send RC5
 *  
 * @param data     lower 12 bit of data as an RC5 command
 *
 * @return bool    True if ir sensor available, otherwise False
 *  
 */
volatile bool Data_RC5Send(int data);

/*
 * Function to enable RC5
 * 
 * @return bool    True if ir sensor available, otherwise False  
 *  
 */
bool Data_RC5Enable();

/*
 * Function to disable RC5
 * 
 * @return bool    True if ir sensor available, otherwise False  
 *  
 */
bool Data_RC5Disable();

/*
 * Function to handle receive
 *
 */
void Data_RC5ReceiveHandler();

/*
 * Function to handle event
 * 
 * @return bool    True if ir sensor available, otherwise False  
 *  
 */
void Data_RC5EventHandler();              

#endif
