/* Copyright (c) 2008 The Samraksh Company.
 * All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose, without fee, and without written
 * agreement is hereby granted, provided that the above copyright
 * notice, the following paragraph and any author information appear in
 * all copies of this source code.
 *
 * THIS SOFTWARE IS PROVIDED BY THE SAMRAKSH COMPANY `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 SAMRAKSH COMPANY BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, LOSS OF USE, DATA, OR PROFITS) 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.
 *
 *
 * Version: 1.0, Modified May 10, 2009
 * Author: Sandip Bapat, <sandip.bapat@samraksh.com>
*/

using Crossbow.platform.imote2;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;

// This library provides the capability to read the BumbleBee radar data using the ITS400 board from Crossbow.
// I and Q radar channels are read with very low inter-channel delay using the Scan-Sequence-Of-Channels mode
// Inter-sample delays with the default ADCSensor component may be too high for applications exploiting radar coherence.
// I and Q outputs are assumed to be connected to ADC0 and ADC1 ports on the ITS400 connector.

namespace Samraksh.BumbleBeeITS400
{
    public class BumbleBeeSensor
    {
        // Constants defined according to the MAX1363 datasheet
        const byte adc_config = 0x1b;     // 0, 00 (scan mode), 11 (enable readback), 01 (scan from ch 00 to ch 01), 1 (single-ended)
        const byte adc_setup = 0x82;      // 1, 00 (ref = VDD), 0 (int ref off), 0 (int clock), 0 (unipolar), 10
                
        // Returns ADC conversions from Channels 0 and 1 
        public void readPair(ushort[] result)
        {
            byte[] wrByte = { adc_config, adc_setup };
            byte[] readByte = new byte[4];
            
            I2CDevice myi2c = new I2CDevice(new I2CDevice.Configuration(0x34, 400));
            I2CDevice.I2CWriteTransaction command = myi2c.CreateWriteTransaction(wrByte);
            I2CDevice.I2CTransaction[] xAction = { command };

            myi2c.Execute(xAction, 20);

            I2CDevice.I2CReadTransaction data = myi2c.CreateReadTransaction(readByte);
            I2CDevice.I2CTransaction[] xAction_Read = { data };
            myi2c.Execute(xAction_Read, 20);

            result[0] = (ushort)(((readByte[0] & 0x0f) << 8) + readByte[1]);
            result[1] = (ushort)(((readByte[2] & 0x0f) << 8) + readByte[3]);
            myi2c.Dispose();
        }
    }
}
