Ted Grosch / Mbed 2 deprecated Nucleo_TOF_I2C

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers OPT3101Crosstalk.cpp Source File

OPT3101Crosstalk.cpp

Go to the documentation of this file.
00001 /*!
00002 * \file OPT3101Crosstalk.cpp
00003 * \author  Karthik Rajagopal <krthik@ti.com>
00004 * \version 0.9.1
00005 *
00006 * \section COPYRIGHT
00007 * TEXAS INSTRUMENTS TEXT FILE LICENSE
00008 * Copyright (c) 2018 Texas Instruments Incorporated
00009 * All rights reserved not granted herein.
00010 * Limited License.
00011 * Texas Instruments Incorporated grants a world-wide, royalty-free, non-exclusive license under copyrights and patents it now or hereafter owns or controls to make, have made, use, import, offer to sell and sell ("Utilize") this software subject to the terms herein.  With respect to the foregoing patent license, such license is granted  solely to the extent that any such patent is necessary to Utilize the software alone.  The patent license shall not apply to any combinations which include this software, other than combinations with devices manufactured by or for TI ("TI Devices").  No hardware patent is licensed hereunder.
00012 * Redistributions must preserve existing copyright notices and reproduce this license (including the above copyright notice and the disclaimer and (if applicable) source code license limitations below) in the documentation and/or other materials provided with the distribution
00013 * Redistribution and use in binary form, without modification, are permitted provided that the following conditions are met:
00014 * * No reverse engineering, decompilation, or disassembly of this software is permitted with respect to any software provided in binary form.
00015 * * any redistribution and use are licensed by TI for use only with TI Devices.
00016 * * Nothing shall obligate TI to provide you with source code for the software licensed and provided to you in object code.
00017 * If software source code is provided to you, modification and redistribution of the source code are permitted provided that the following conditions are met:
00018 * * any redistribution and use of the source code, including any resulting derivative works, are licensed by TI for use only with TI Devices.
00019 * * any redistribution and use of any object code compiled from the source code and any resulting derivative works, are licensed by TI for use only with TI Devices.
00020 * Neither the name of Texas Instruments Incorporated nor the names of its suppliers may be used to endorse or promote products derived from this software without specific prior written permission.
00021 * DISCLAIMER.
00022 * THIS SOFTWARE IS PROVIDED BY TI AND TI'S LICENSORS "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 TI AND TI'S LICENSORS 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.
00023 *
00024 * \section DESCRIPTION
00025 * The file contains class OPT3101::crosstalkC member function definitions
00026 */
00027 #include <stdlib.h>
00028 #include <math.h>
00029 #include "OPT3101Crosstalk.h "
00030 #include "hostController.h "
00031 #include "OPT3101device.h "
00032 
00033 
00034 OPT3101::crosstalkC::crosstalkC(){
00035     /// <b>Algorithm of the method is as follows</b>
00036     this->I = 0;
00037     this->Q = 0;
00038     this->xtalkScale = 0;
00039     this->illumScale = 0;
00040     this->illumDac = 0;
00041     this->illumDCdac = 0;
00042     this->tmain = 0;
00043     this->tillum = 0;
00044     this->shiftIllumPhase = 0;
00045     this->commonScale = 0;
00046     this->illumXtalk=false; ///* sets OPT3101::crosstalkC::illumXtalk to false by default
00047 }
00048 
00049 void OPT3101::crosstalkC::readCrosstalkRegisters(OPT3101::device *dev){
00050     int32_t iRead,qRead;
00051     uint32_t mRead;
00052     uint8_t c0;
00053 
00054     /// <b>Algorithm of the method is as follows</b>
00055     iRead=((int32_t) (dev->reg.iphase_xtalk.read()<<8))>>8; ///* Reads register OPT3101::registers::iphase_xtalk in 24 bit from and converts to signed number
00056     qRead=((int32_t) (dev->reg.qphase_xtalk.read()<<8))>>8; ///* Reads register OPT3101::registers::qphase_xtalk in 24 bit from and converts to signed number
00057     mRead=abs(iRead)>abs(qRead)?abs(iRead):abs(qRead); ///* Finds absolute max among the read I and Q register values to determine the OPT3101::crosstalkC::illumXtalk
00058 
00059     this->xtalkScale = 0;
00060     for (c0 = 15; c0 < 23; c0++) {
00061         if (mRead > (((uint32_t)1) << c0) && mRead <= (((uint32_t)1) << (c0 + 1))){
00062             this->xtalkScale = c0-14; // replaced c0+1-15 due to weird problem
00063             break;
00064         }
00065     }
00066 
00067     ///* Determines OPT3101::crosstalkC::illumXtalk and assigns. The algorithm finds the minimum OPT3101::crosstalkC::illumXtalk value for which the raw I and Q registers can be fit to a 16 bit register 
00068     this->I=(int16_t) (iRead>>this->xtalkScale); ///* Scales down the  24 bit raw register I and Q values with OPT3101::crosstalkC::illumXtalk and assigns to OPT3101::crosstalkC::I and OPT3101::crosstalkC::Q
00069     this->Q=(int16_t) (qRead>>this->xtalkScale);
00070 }
00071 
00072 double OPT3101::crosstalkC::magnitude(){
00073     double amplitude, xTalkI, xTalkQ; 
00074     /// <b>Algorithm of the method is as follows</b>
00075     xTalkI = this->I>>(9-this->xtalkScale);
00076     xTalkQ = this->Q>>(9-this->xtalkScale);
00077     amplitude=sqrt(pow(xTalkI,2)+pow(xTalkQ,2))*1.646; ///* Calculates the magnitude of crosstalk based on a predefined formula. <b>Warning:</b> This uses floating point arithmetic and math.h library for power and sqrt functions
00078     return amplitude;
00079 }
00080 
00081 void OPT3101::crosstalkC::readTemperatureMain(OPT3101::device *dev){
00082     /// <b>Algorithm of the method is as follows</b>
00083     this->tmain=dev->reg.tmain.read();  ///* Reads register OPT3101Register::tmain and assigns to OPT3101::crosstalkC::tmain
00084 }
00085 void OPT3101::crosstalkC::readTemperatureIllum(OPT3101::device *dev){
00086     /// <b>Algorithm of the method is as follows</b>
00087     this->tillum=dev->reg.tillum.read(); ///* Reads register OPT3101Register::illum and assigns to OPT3101::crosstalkC::illum
00088 }
00089 
00090 #ifdef OPT3101_USE_STDIOLIB
00091 void OPT3101::crosstalkC::storeToFile(char *fileName)
00092 {
00093 #ifdef OPT3101_USE_STREAMLIB
00094     /// <b>Algorithm of the method is as follows</b>
00095     std::ofstream ofs(fileName);
00096     ofs << this;
00097     ofs.close(); ///* User needs to implement file storage based on host. 
00098 #endif
00099 }
00100 #endif 
00101 
00102 #ifdef OPT3101_USE_STDIOLIB
00103 void OPT3101::crosstalkC::loadFromFile(char * fileName)
00104 {
00105 #ifdef OPT3101_USE_STREAMLIB
00106     /// <b>Algorithm of the method is as follows</b>
00107     std::ifstream ifs(fileName);
00108     ifs >> this;
00109     ifs.close(); ///* User needs to implement file load/restore based on host. 
00110 #endif
00111 }
00112 #endif
00113 
00114 void OPT3101::crosstalkC::report()
00115 {
00116 //#ifdef OPT3101_USE_STDIOLIB
00117     // This is template function to report and analyze the crosstalk value. 
00118     /// <b>Algorithm of the method is as follows</b>
00119     host.printf("----------------------\r\n");
00120     host.printf("Crosstalk Class Report\r\n");
00121     host.printf("----------------------\r\n"); ///* Prints all the members and values of members on screen.
00122     host.printf("I=%d\r\n", this->I);
00123     host.printf("Q=%d\r\n", this->Q); ///* User needs to implement file load/restore based on host.
00124     host.printf("xtalkScale=%d\r\n", this->xtalkScale);
00125     host.printf("illumXtalk=%d\r\n", this->illumXtalk);
00126     host.printf("illumScale=%d\r\n", this->illumScale);
00127     host.printf("illumDac=%d\r\n", this->illumDac);
00128     host.printf("tmain=%d\r\n", this->tmain);
00129     host.printf("tillum=%d\r\n", this->tillum);
00130     host.printf("shiftIllumPhase=%d\r\n", this->shiftIllumPhase);
00131     host.printf("commonScale=%d\r\n", this->commonScale);
00132     host.printf("Magnitude=%4.2f\r\n", this->magnitude());
00133     host.printf("----------------------\r\n");
00134 
00135 //#endif
00136 }
00137 void OPT3101::crosstalkC::printHeader()
00138 {
00139 
00140     host.printfSetColor(0b001);
00141     host.printf("      I,");
00142     host.printf("      Q,");
00143     host.printf("S,");
00144     host.printf(" ScaledI,");
00145     host.printf(" ScaledQ,");
00146     host.printf("tMain,");
00147     host.printf("tIlum,");
00148     host.printf("tMain(C),");
00149     host.printf("  tIlum(C),");
00150     host.printf("Magnitd\u001b[0m\r\n");
00151     host.printfSetColor(0xFF);
00152 
00153 }
00154 void OPT3101::crosstalkC::print()
00155 {
00156 //#ifdef OPT3101_USE_STDIOLIB
00157     // This is template function to report and analyze the crosstalk value.
00158     /// <b>Algorithm of the method is as follows</b>
00159     host.printfSetColor(0b001);
00160     host.printf("%+07d,",this->I);
00161     host.printf("%+07d,",this->Q);
00162     host.printf("%01d,",this->xtalkScale);
00163     host.printf("%+08ld,",(int32_t) ((int32_t)this->I)<<this->xtalkScale);
00164     host.printf("%+08ld,",(int32_t) ((int32_t)this->Q)<<this->xtalkScale);
00165     host.printf(" %04d,",this->tmain);
00166     host.printf(" %04d,",this->tillum);
00167     host.printf("     %+03d,",(int8_t) ((((int16_t)this->tmain)>>3)-256));
00168     host.printf(" %+07.4f,",((double)(((int16_t)this->tillum)-2048))/16.0);
00169     host.printf("%5.1f\r\n",this->magnitude());
00170     host.printfSetColor(0xFF);
00171 //#endif
00172 }
00173 
00174 #ifdef OPT3101_USE_STREAMLIB
00175 std::istream& OPT3101::operator>>(std::istream &is, OPT3101::crosstalkC *data) {
00176     /// <b>Algorithm of the method is as follows</b>
00177     is >> data->I; ///* From input stream members are de-serialized and restored 
00178     is >> data->Q;
00179     is >> data->xtalkScale;
00180     is >> data->illumXtalk;
00181     is >> data->illumScale;
00182     is >> data->illumDac;
00183     is >> data->illumDCdac;
00184     is >> data->tmain;
00185     is >> data->tillum;
00186     is >> data->shiftIllumPhase;
00187     is >> data->commonScale;
00188     return is;
00189 }
00190 #endif
00191 
00192 #ifdef OPT3101_USE_STREAMLIB
00193 std::ostream& OPT3101::operator<<(std::ostream &os, const OPT3101::crosstalkC *data) {
00194     os << data->I << '\n'; ///* From class serialize the members
00195     os << data->Q << '\n';
00196     os << data->xtalkScale << '\n';
00197     os << data->illumXtalk << '\n';
00198     os << data->illumScale << '\n';
00199     os << data->illumDac << '\n';
00200     os << data->illumDCdac << '\n';
00201     os << data->tmain << '\n';
00202     os << data->tillum << '\n';
00203     os << data->shiftIllumPhase << '\n';
00204     os << data->commonScale << '\n';
00205     //printf("Writing the data to file I[%d] Q[%d]\n", xtalk->I, xtalk->Q);
00206     return os;
00207 }
00208 #endif
00209