Darien Figueroa / Mbed 2 deprecated repo3

Dependencies:   mbed MAX14720 MAX30205 USBDevice

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TemperatureValue.cs Source File

TemperatureValue.cs

00001 /*******************************************************************************
00002 * Copyright (C) 2016 Maxim Integrated Products, Inc., All rights Reserved.
00003 * 
00004 * This software is protected by copyright laws of the United States and
00005 * of foreign countries. This material may also be protected by patent laws
00006 * and technology transfer regulations of the United States and of foreign
00007 * countries. This software is furnished under a license agreement and/or a
00008 * nondisclosure agreement and may only be used or reproduced in accordance
00009 * with the terms of those agreements. Dissemination of this information to
00010 * any party or parties not specified in the license agreement and/or
00011 * nondisclosure agreement is expressly prohibited.
00012 *
00013 * The above copyright notice and this permission notice shall be included
00014 * in all copies or substantial portions of the Software.
00015 *
00016 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00017 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00018 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00019 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
00020 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00021 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00022 * OTHER DEALINGS IN THE SOFTWARE.
00023 *
00024 * Except as contained in this notice, the name of Maxim Integrated
00025 * Products, Inc. shall not be used except as stated in the Maxim Integrated
00026 * Products, Inc. Branding Policy.
00027 *
00028 * The mere transfer of this software does not imply any licenses
00029 * of trade secrets, proprietary technology, copyrights, patents,
00030 * trademarks, maskwork rights, or any other form of intellectual
00031 * property whatsoever. Maxim Integrated Products, Inc. retains all
00032 * ownership rights.
00033 *******************************************************************************
00034 */
00035 
00036 using System;
00037 using System.Collections.Generic;
00038 using System.Linq;
00039 using System.Text;
00040 using System.Threading.Tasks;
00041 
00042 namespace HealthSensorPlatform
00043 {
00044     public class TemperatureValue
00045     {
00046         double temperature;
00047         int temperatureHex;
00048         //bool units;
00049         bool dataFormat;
00050 
00051 
00052         public TemperatureValue(double temperature)
00053         {
00054             this.temperature = temperature;
00055             temperatureHex = temperatureToHex(temperature, false);
00056         }
00057 
00058         public TemperatureValue(double temperature, bool dataFormat)
00059         {
00060             this.temperature = temperature;
00061             temperatureHex = temperatureToHex(temperature, dataFormat);
00062             this.dataFormat = dataFormat;
00063         }
00064 
00065         public TemperatureValue(int hex)
00066         {
00067             temperatureHex = hex;
00068             temperature = hexToTemperature(hex, false);
00069         }
00070 
00071         public TemperatureValue(int hex, bool dataFormat)
00072         {
00073             temperatureHex = hex;
00074             temperature = hexToTemperature(hex, dataFormat);
00075         }
00076 
00077         public double TemperatureC
00078         {
00079             get
00080             {
00081                 return temperature;
00082             }
00083         }
00084 
00085         public double TemperatureF
00086         {
00087             get
00088             {
00089                 return temperature * 9 / 5 + 32;
00090             }
00091         }
00092         public int TemperatureHex
00093         {
00094             get
00095             {
00096                 return temperatureHex;
00097             }
00098         }
00099         /*public bool UnitsCelsius
00100         {
00101             get
00102             {
00103                 return units;
00104             }
00105         }*/
00106         public bool DataFormat
00107         {
00108             get
00109             {
00110                 return dataFormat;
00111             }
00112             set
00113             {
00114                 dataFormat = DataFormat;
00115             }
00116         }
00117 
00118         double hexToTemperature(int hex, bool dataFormat)
00119         {
00120             int normalCode;
00121             int rawCode = hex;
00122             double celsius;
00123 
00124             if (rawCode > 0x7fff)
00125                 normalCode = rawCode - 0x10000;
00126             else
00127                 normalCode = rawCode;
00128 
00129             if (dataFormat == false)
00130                 celsius = normalCode / Math.Pow(2, 8);
00131             else
00132                 celsius = normalCode / Math.Pow(2, 8) + 64;
00133 
00134             return celsius;
00135         }
00136 
00137         int temperatureToHex(double temperature, bool dataFormat)
00138         {
00139             int code = 0;
00140             double temp = 0;
00141 
00142             if (dataFormat)
00143             {
00144                 temp = temperature - 64;
00145             }
00146             else
00147             {
00148                 temp = temperature;
00149             }
00150 
00151                 code = (short)(temp * Math.Pow(2, 8));
00152 
00153                 if (code < 0)
00154                     code += 0x10000;
00155 
00156                 return code;
00157         }
00158     }
00159 }