Library for MAXREFDES131 OneWire GridEYE sensor interface

Dependents:   MAXREFDES131_Qt_Demo MAXREFDES130_131_Demo

OWGridEye.cpp

Committer:
j3
Date:
2016-04-30
Revision:
1:9e457e35e2e3
Child:
2:b10e7a2961ab

File content as of revision 1:9e457e35e2e3:

/**********************************************************************
* Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* Except as contained in this notice, the name of Maxim Integrated
* Products, Inc. shall not be used except as stated in the Maxim Integrated
* Products, Inc. Branding Policy.
*
* The mere transfer of this software does not imply any licenses
* of trade secrets, proprietary technology, copyrights, patents,
* trademarks, maskwork rights, or any other form of intellectual
* property whatsoever. Maxim Integrated Products, Inc. retains all
* ownership rights.
**********************************************************************/


#include "OWGridEye.h"
#include "grideye_api_lv1.h"
#include "grideye_api_lv2.h"
#include "grideye_api_lv3.h"


//*********************************************************************
OWGridEye::OWGridEye(OneWireMaster & owm, RomId & ds2413Rom)
: _owm(owm), _sw_rom(ds2413Rom)
{
}


//*********************************************************************
OWGridEye::CmdResult OWGridEye::disconnectGridEye(void)
{
    OWGridEye::CmdResult result = OWGridEye::OpFailure;
    Ds2413 ow_switch(_owm);
    uint8_t pio_state;
    
    Ds2413::CmdResult sw_result = ow_switch.pio_access_read_chB(_sw_rom, pio_state);
    if(sw_result == Ds2413::Success)
    {
        //if high take low, falling edge puts bridge and sensor to sleep
        if(pio_state)
        {
            sw_result = ow_switch.pio_access_write_chB(_sw_rom, 0);
            if(sw_result == Ds2413::Success)
            {
                result = OWGridEye::Success;
            }
        }
        else
        {
            //if low, take high, then low to get falling edge
            sw_result = ow_switch.pio_access_write_chB(_sw_rom, 1);
            if(sw_result == Ds2413::Success)
            {
                sw_result = ow_switch.pio_access_write_chB(_sw_rom, 0);
                if(sw_result == Ds2413::Success)
                {
                    result = OWGridEye::Success;
                }
            }
        }
    }
    
    return result;
}


//*********************************************************************
OWGridEye::CmdResult OWGridEye::connectGridEye(void)
{
    OWGridEye::CmdResult result = OWGridEye::OpFailure;
    
    Ds2413 ow_switch(_owm);
    
    Ds2413::CmdResult sw_result = ow_switch.pio_access_write_chB(_sw_rom, 1);
    if(sw_result == Ds2413::Success)
    {
        result = OWGridEye::Success;
    }
    
    return result;
}


//*********************************************************************
OWGridEye::CmdResult OWGridEye::connectOWbus(void)
{
    OWGridEye::CmdResult result = OWGridEye::OpFailure;
    
    Ds2413 ow_switch(_owm);
    
    Ds2413::CmdResult sw_result = ow_switch.pio_access_write_chA(_sw_rom, 0);
    if(sw_result == Ds2413::Success)
    {
        result = OWGridEye::Success;
    }
    
    return result;
}


//*********************************************************************
OWGridEye::CmdResult OWGridEye::disconnectOWbus(void)
{
    OWGridEye::CmdResult result = OWGridEye::OpFailure;
    
    Ds2413 ow_switch(_owm);
    
    Ds2413::CmdResult sw_result = ow_switch.pio_access_write_chA(_sw_rom, 1);
    if(sw_result == Ds2413::Success)
    {
        result = OWGridEye::Success;
    }
    
    return result;
}


//*********************************************************************
OWGridEye::CmdResult OWGridEye::gridEyeAccess(bool readWrite, GridEyeRegister regAdrs, uint8_t numBytes, uint8_t * dataBuf)
{
    OneWireMaster::CmdResult ow_result = OneWireMaster::OperationFailure;
    Ds28e17::CmdResult i2c_cmd_result;
    OWGridEye::CmdResult gridEye_result = OWGridEye::OpFailure;
    
    Ds28e17 i2c_bridge(_owm);
    
    OneWireMaster::SearchState search_state;
    search_state.romId.setFamilyCode(OWGridEye::DS28E17_FAMILY_CODE);
    
    _owm.OWTargetSetup(search_state);
    ow_result = _owm.OWNext(search_state);
    if(ow_result == OneWireMaster::Success)
    {
        i2c_bridge.set_rom_id(search_state.romId);
        uint8_t read_pointer = regAdrs;
        uint8_t status, wr_status;
        
        if(readWrite)
        {
            i2c_cmd_result = i2c_bridge.I2C_WriteReadDataWithStop((OWGridEye::I2C_ADRS << 1), 
                                                                  1, &read_pointer, numBytes, 
                                                                  status, wr_status, dataBuf);
        }
        else
        {
            i2c_cmd_result = i2c_bridge.I2C_WriteDataWithStop((OWGridEye::I2C_ADRS << 1), 
                                                               numBytes, dataBuf, status, 
                                                               wr_status);
        }
        
        if(i2c_cmd_result == Ds28e17::Success)
        {
            gridEye_result = OWGridEye::Success;
        }
    }
    
    return gridEye_result;
}


//*********************************************************************
OWGridEye::CmdResult OWGridEye::gridEyeGetThermistor(int16_t & thermTemp)
{
    OWGridEye::CmdResult ge_result;
    uint8_t data[2];
    
    ge_result = this->gridEyeAccess(1, OWGridEye::THERMISTOR_LOW, 2, data);
    if(ge_result == OWGridEye::Success)
    {
        thermTemp = shAMG_PUB_TMP_ConvThermistor(data);
    }
    
    return ge_result;
}
 

//*********************************************************************    
OWGridEye::CmdResult OWGridEye::gridEyeGetPixelTemperature(uint8_t pixelAdrs, int16_t & pixelTemp)
{
    OWGridEye::CmdResult ge_result;
    uint8_t data[2];
    
    ge_result = this->gridEyeAccess(1, (GridEyeRegister) pixelAdrs, 2, data);
    if(ge_result == OWGridEye::Success)
    {
        pixelTemp = shAMG_PUB_TMP_ConvTemperature(data);
    }
    
    return ge_result;
}


//*********************************************************************
OWGridEye::CmdResult OWGridEye::gridEyeGetFrameTemperature(int16_t * frameTemp)
{
    OWGridEye::CmdResult ge_result;
    uint8_t data[128];
    
    ge_result = this->gridEyeAccess(1,(GridEyeRegister) OWGridEye::PIXEL_BASE_ADRS, 128, data);
    if(ge_result == OWGridEye::Success)
    {
        vAMG_PUB_TMP_ConvTemperature64(data, frameTemp);
    }
    
    return ge_result;
}


//*********************************************************************
int16_t OWGridEye::gridEyeConvFtoS(float data)
{
    int16_t convData = 0;
    
    return convData;
}


//*********************************************************************
float OWGridEye::gridEyeConvStoF(int16_t data)
{
    float convData = fAMG_PUB_CMN_ConvStoF(data);
    return convData;
}