Ted Grosch / Mbed 2 deprecated Nucleo_TOF_I2C

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers OPT3101DesignCoefficients.cpp Source File

OPT3101DesignCoefficients.cpp

Go to the documentation of this file.
00001 /*!
00002 * \file OPT3101DesignCoefficients.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 methods definitions for classes OPT3101::crosstalkTempCoffC OPT3101::calibrationC OPT3101::phaseAmbientCoffC OPT3101::phaseTempCoffC
00026 */
00027 #include "OPT3101DesignCoefficients.h "
00028 #include <stdlib.h>
00029 
00030 OPT3101::crosstalkTempCoffC::crosstalkTempCoffC()
00031 {
00032     /// <b>Algorithm of the method is as follows</b>
00033     this->coffI = 0.0; ///* Initilizes all the members to 0
00034     this->coffQ = 0.0;
00035     this->coffIReg = 0;
00036     this->coffQReg = 0;
00037     this->commonScale = 0;
00038 }
00039 
00040 void OPT3101::crosstalkTempCoffC::calculateCoff(OPT3101::crosstalkC *illumXtalk0, OPT3101::crosstalkC *illumXtalk1)
00041 {
00042     int32_t I[2], Q[2];
00043     /// <b>Algorithm of the method is as follows</b>
00044     I[0] = ((int32_t)illumXtalk0->I) << illumXtalk0->xtalkScale; ///* Scales the 16bit crosstalk registers OPT3101::crosstalkC::I and OPT3101::crosstalkC::Q to 24 bit level using OPT3101::crosstalkC::xtalkScale
00045     I[1] = ((int32_t)illumXtalk1->I) << illumXtalk1->xtalkScale; 
00046     Q[0] = ((int32_t)illumXtalk0->Q) << illumXtalk0->xtalkScale;
00047     Q[1] = ((int32_t)illumXtalk1->Q) << illumXtalk1->xtalkScale;
00048     ///* Makes the data base circular to avoid errorneous high slope measurements
00049     if (I[0] > 6291456 && I[1] < -6291456) // 3/4 of the 24 bit signed range
00050         I[1] += 16777216; //Adding 2**24 to this 
00051     if (I[1] > 6291456 && I[0] < -6291456) // 3/4 of the 24 bit signed range
00052         I[0] += 16777216; //Adding 2**24 to this 
00053     if (Q[0] > 6291456 && Q[1] < -6291456) // 3/4 of the 24 bit signed range
00054         Q[1] += 16777216; //Adding 2**24 to this 
00055     if (Q[1] > 6291456 && Q[0] < -6291456) // 3/4 of the 24 bit signed range
00056         Q[0] += 16777216; //Adding 2**24 to this 
00057                           
00058                           //printf("Temp diff is [%d]\n", illumXtalk0->tmain - illumXtalk1->tmain);
00059     if(!((illumXtalk0->tmain - illumXtalk1->tmain)==0)) {
00060         this->coffI = ((double)(I[0] - I[1])) / (illumXtalk0->tmain - illumXtalk1->tmain) / ((illumXtalk0->magnitude() + illumXtalk1->magnitude())/2.0);    ///* Calculates the double precision OPT3101::crosstalkTempCoffC::coffI and OPT3101::crosstalkTempCoffC::coffQ by finding difference between 24 bit registers divided by delta OPT3101::crosstalkC::tmain divided by OPT3101::crosstalkC::magnitude()
00061         this->coffQ = ((double)(Q[0] - Q[1])) / (illumXtalk0->tmain - illumXtalk1->tmain) / ((illumXtalk0->magnitude() + illumXtalk1->magnitude())/2.0);
00062     }
00063     else {
00064         this->coffI = 0.0;
00065         this->coffQ = 0.0;
00066     }
00067     ///* <b>Warning:</b> Makes the OPT3101::crosstalkTempCoffC::I and OPT3101::crosstalkTempCoffC::Q 0 if the OPT3101::crosstalkC::tmain are same for both the input arguments
00068 }
00069 
00070 void OPT3101::crosstalkTempCoffC::report()
00071 {
00072 #ifdef OPT3101_USE_STDIOLIB
00073     /// <b>Algorithm of the method is as follows</b>
00074     printf("--------------------------------\n");
00075     printf("Crosstalk Temp Coff Class Report\n");
00076     printf("--------------------------------\n"); ///* Prints all the members and values of members on screen. 
00077     printf("coffI=%4.2f\n",  this->coffI);
00078     printf("coffQ=%4.2f\n",  this->coffQ);
00079     printf("coffIReg=%d\n", this->coffIReg);
00080     printf("coffQReg=%d\n", this->coffQReg);
00081     printf("commonScale=%d\n", this->commonScale);
00082     printf("--------------------------------\n"); ///* Prints all the members and values of members on screen. 
00083 #endif
00084 }
00085 
00086 #ifdef OPT3101_USE_STDIOLIB
00087 void OPT3101::crosstalkTempCoffC::storeToFile(char * fileName)
00088 {
00089 #ifdef OPT3101_USE_STREAMLIB
00090     /// <b>Algorithm of the method is as follows</b>
00091     std::ofstream ofs(fileName);
00092     ofs << this;
00093     ofs.close(); ///* User needs to implement file storage based on host. 
00094 #endif
00095 }
00096 #endif
00097 
00098 #ifdef OPT3101_USE_STDIOLIB
00099 void OPT3101::crosstalkTempCoffC::loadFromFile(char * fileName)
00100 {
00101 #ifdef OPT3101_USE_STREAMLIB
00102     /// <b>Algorithm of the method is as follows</b>
00103     std::ifstream ifs(fileName);
00104     ifs >> this;
00105     ifs.close(); ///* User needs to implement file load/restore based on host. 
00106 #endif
00107 }
00108 #endif
00109 
00110 #ifdef OPT3101_USE_STREAMLIB
00111 std::ostream & OPT3101::operator<<(std::ostream & os, const crosstalkTempCoffC * data)
00112 {
00113     /// <b>Algorithm of the method is as follows</b>
00114     os << data->coffI << '\n'; ///* Serializes all the members and returns to the stream 
00115     os << data->coffQ << '\n';
00116     os << data->coffIReg << '\n';
00117     os << data->coffQReg << '\n';
00118     os << data->commonScale << '\n';
00119     return os;
00120 }
00121 #endif
00122 
00123 #ifdef OPT3101_USE_STREAMLIB
00124 std::istream & OPT3101::operator>>(std::istream & is, crosstalkTempCoffC * data)
00125 {
00126     /// <b>Algorithm of the method is as follows</b>
00127     is >> data->coffI; ///* Reads from stream and de-serializes all the members 
00128     is >> data->coffQ;
00129     is >> data->coffIReg;
00130     is >> data->coffQReg;
00131     is >> data->commonScale;
00132     return is;
00133 
00134 }
00135 #endif
00136 
00137 OPT3101::phaseTempCoffC::phaseTempCoffC()
00138 {
00139     /// <b>Algorithm of the method is as follows</b>
00140     this->coff = 0.0; ///* Initializes all members to 0. Sets OPT3101::phaseTempCoffC::istMainCoff to true
00141     this->coffReg = 0;
00142     this->commonScale = 0;
00143     this->istMainCoff = true;
00144 }
00145 
00146 void OPT3101::phaseTempCoffC::calculateCoff(OPT3101::frameData *frameData0, OPT3101::frameData *frameData1, bool istMainCoff)
00147 {
00148     int32_t phase[2];
00149 
00150     phase[0] = frameData0->phase;
00151     phase[1] = frameData1->phase;
00152     /// <b>Algorithm of the method is as follows</b>
00153     if (phase[0] > 49152 && phase[1] < 16384) // 3/4 of the 16 unsigned
00154         phase[1] += 65536; //Adding 2**16 to this 
00155     if (phase[1] > 49152 && phase[0] < 16384) // 3/4 of the 16 unsigned
00156         phase[0] += 65536; //Adding 2**16 to this 
00157     ///* Makes the data base circular to avoid errorneous high slope measurements
00158     this->istMainCoff = istMainCoff; ///* Assigns OPT3101::phaseTempCoffC::istMainCoff to the value assigned by argument
00159     if (istMainCoff)
00160         if (!(frameData0->tmain - frameData1->tmain) == 0)
00161             this->coff = ((double)((int32_t)phase[0] - (int32_t)phase[1])) / (frameData0->tmain - frameData1->tmain); ///* Calculates the double precision OPT3101::phaseTempCoffC::coff  by finding difference between OPT3101::frameData::phase divided by delta OPT3101::frameData::tmain or  OPT3101::frameData::tillum based on input flag
00162         else
00163             this->coff = 0.0;
00164     else
00165         if (!(frameData0->tillum - frameData1->tillum) == 0)
00166             this->coff = ((double)((int32_t)phase[0] - (int32_t)phase[1])) / (frameData0->tillum - frameData1->tillum);
00167         else
00168             this->coff = 0.0;
00169     ///* <b>Warning:</b> Makes the OPT3101::phaseTempCoffC::coff 0 if the OPT3101::frameData::tmain or OPT3101::frameData::tillum are same for both the input arguments
00170 
00171 }
00172 
00173 void OPT3101::phaseTempCoffC::report()
00174 {
00175 #ifdef OPT3101_USE_STDIOLIB
00176     /// <b>Algorithm of the method is as follows</b>
00177     printf("----------------------------\n");
00178     printf("Phase Temp Coff Class Report\n");
00179     printf("----------------------------\n"); ///* Prints all the members and values of members on screen. 
00180     printf("coff=%4.2f\n", this->coff);
00181     printf("coffReg=%d\n", this->coffReg);
00182     printf("commonScale=%d\n", this->commonScale);
00183     printf("----------------------------\n"); ///* Prints all the members and values of members on screen. 
00184 #endif
00185 }
00186 
00187 
00188 #ifdef OPT3101_USE_STDIOLIB
00189 void OPT3101::phaseTempCoffC::storeToFile(char * fileName)
00190 {
00191 #ifdef OPT3101_USE_STREAMLIB
00192     /// <b>Algorithm of the method is as follows</b>
00193     std::ofstream ofs(fileName);
00194     ofs << this;
00195     ofs.close(); ///* User needs to implement file storage based on host. 
00196 #endif
00197 }
00198 #endif
00199 
00200 #ifdef OPT3101_USE_STDIOLIB
00201 void OPT3101::phaseTempCoffC::loadFromFile(char * fileName)
00202 {
00203 #ifdef OPT3101_USE_STREAMLIB
00204     /// <b>Algorithm of the method is as follows</b>
00205     std::ifstream ifs(fileName);
00206     ifs >> this;
00207     ifs.close(); ///* User needs to implement file load/restore based on host. 
00208 #endif
00209 }
00210 #endif
00211 
00212 #ifdef OPT3101_USE_STREAMLIB
00213 std::ostream & OPT3101::operator<<(std::ostream & os, const phaseTempCoffC * data)
00214 {
00215     /// <b>Algorithm of the method is as follows</b>
00216     os << data->coff << '\n'; ///* Serializes all the members and returns to the stream 
00217     os << data->coffReg << '\n';
00218     os << data->commonScale << '\n';
00219     os << data->istMainCoff << '\n';
00220     return os;
00221 }
00222 #endif
00223 
00224 #ifdef OPT3101_USE_STREAMLIB
00225 std::istream & OPT3101::operator>>(std::istream & is, phaseTempCoffC * data)
00226 {
00227     /// <b>Algorithm of the method is as follows</b>
00228     is >> data->coff; ///* Reads from stream and de-serializes all the members 
00229     is >> data->coffReg;
00230     is >> data->commonScale;
00231     is >> data->istMainCoff;
00232     return is;
00233 }
00234 #endif
00235 
00236 OPT3101::phaseAmbientCoffC::phaseAmbientCoffC()
00237 {
00238     uint8_t c0;
00239     /// <b>Algorithm of the method is as follows</b>
00240     for (c0 = 0; c0 < 4; c0++)
00241         this->coff[c0] = 0; ///* Serializes all the members and returns to the stream 
00242     for (c0 = 0; c0<4; c0++)
00243         this->coffReg[c0] = 0;
00244     for (c0 = 0; c0 < 3; c0++)
00245         this->splitsReg[c0] = 0;
00246     this->commonScale = 0;
00247 }
00248 
00249 void OPT3101::phaseAmbientCoffC::calculateCoff(uint16_t freqCount)
00250 {
00251     uint8_t c0;
00252     uint8_t scaleCoff;
00253 
00254     double maxCoff=-1;
00255     /// <b>Algorithm of the method is as follows</b>
00256     for(c0=1;c0<4;c0++)
00257         maxCoff = abs(this->coff[c0]) > maxCoff ? abs(this->coff[c0]) : maxCoff; ///* Identifies max of absolute of OPT3101::phaseAmbientCoffC::coff values 
00258     
00259     maxCoff *= 16384 / freqCount; ///* scales the max coefficient with input frequencyCount 
00260     scaleCoff = 0;
00261     for (c0 = 0; c0 < 8; c0++) {
00262         scaleCoff = (maxCoff*(1 << (c0 + 2))) < (1 << 7) ? c0 : scaleCoff; ///* Finds a OPT3101::phaseAmbientCoffC::commonScale which can fit the OPT3101::phaseAmbientCoffC::coff to 8 bit registers
00263     }
00264     this->commonScale = scaleCoff;
00265     for (c0 = 0; c0 < 4; c0++) 
00266         this->coffReg[c0] = (uint8_t) (16384.0 / freqCount * this->coff[c0]*(1 << (this->commonScale+2)));  ///* Finds OPT3101::phaseAmbientCoffC::coffReg values (8 bit register values) based on OPT3101::phaseAmbientCoffC::coff and OPT3101::phaseAmbientCoffC::commonScale
00267 }
00268 
00269 void OPT3101::phaseAmbientCoffC::report()
00270 {
00271 #ifdef OPT3101_USE_STDIOLIB
00272     uint8_t c0;
00273     /// <b>Algorithm of the method is as follows</b>
00274     printf("-------------------------------\n");
00275     printf("Phase Ambient Coff Class Report\n");
00276     printf("-------------------------------\n"); ///* Prints all the members and values of members on screen. 
00277     for(c0=0;c0<4;c0++)
00278         printf("coff[%d]=%4.2f\n", c0,this->coff[c0]);
00279     for (c0 = 0; c0<4; c0++)
00280         printf("coffReg[%d]=%d\n", c0, this->coffReg[c0]);
00281     for (c0 = 0; c0<3; c0++)
00282         printf("splitsReg[%d]=%d\n", c0, this->splitsReg[c0]);
00283     printf("commonScale=%d\n", this->commonScale);
00284     printf("-------------------------------\n"); ///* Prints all the members and values of members on screen. 
00285 #endif
00286 }
00287 
00288 #ifdef OPT3101_USE_STDIOLIB
00289 void OPT3101::phaseAmbientCoffC::storeToFile(char * fileName)
00290 {
00291 #ifdef OPT3101_USE_STREAMLIB
00292     /// <b>Algorithm of the method is as follows</b>
00293     std::ofstream ofs(fileName);
00294     ofs << this;
00295     ofs.close(); ///* User needs to implement file storage based on host. 
00296 #endif
00297 }
00298 #endif
00299 
00300 #ifdef OPT3101_USE_STDIOLIB
00301 void OPT3101::phaseAmbientCoffC::loadFromFile(char * fileName)
00302 {
00303 #ifdef OPT3101_USE_STREAMLIB
00304     /// <b>Algorithm of the method is as follows</b>
00305     std::ifstream ifs(fileName);
00306     ifs >> this;
00307     ifs.close(); ///* User needs to implement file load/restore based on host. 
00308 #endif
00309 }
00310 #endif
00311 
00312 #ifdef OPT3101_USE_STREAMLIB
00313 std::ostream & OPT3101::operator<<(std::ostream & os, const phaseAmbientCoffC * data)
00314 {
00315     uint8_t c0;
00316     /// <b>Algorithm of the method is as follows</b>
00317     
00318     for(c0=0;c0<4;c0++)
00319         os << data->coff[c0] << '\n'; ///* Serializes all the members and returns to the stream 
00320     for (c0 = 0; c0<4; c0++)
00321         os << data->coffReg[c0] << '\n'; 
00322     for (c0 = 0; c0<3; c0++)
00323         os << data->splitsReg[c0] << '\n';
00324     os << data->commonScale << '\n';
00325 
00326     return os;
00327 }
00328 #endif
00329 
00330 #ifdef OPT3101_USE_STREAMLIB
00331 std::istream & OPT3101::operator>>(std::istream & is, phaseAmbientCoffC * data)
00332 {
00333     /// <b>Algorithm of the method is as follows</b>
00334     uint8_t c0;
00335     for (c0 = 0; c0 < 4; c0++) ///* Reads from stream and de-serializes all the members 
00336         is >> data->coff[c0];
00337     for (c0 = 0; c0 < 4; c0++)
00338         is >> data->coffReg[c0];
00339     for (c0 = 0; c0 < 3; c0++)
00340         is >> data->splitsReg[c0];
00341     is >> data->commonScale;
00342 
00343     return is;
00344 
00345 }
00346 #endif
00347 
00348