Darien Figueroa / Mbed 2 deprecated repo3

Dependencies:   mbed MAX14720 MAX30205 USBDevice

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TemperatureView.cs Source File

TemperatureView.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.ComponentModel;
00039 using System.Drawing;
00040 using System.Data;
00041 using System.Linq;
00042 using System.Text;
00043 using System.Windows.Forms;
00044 using MaximStyle;
00045 using MAX30205EVKit.View;
00046 
00047 namespace HealthSensorPlatform.CustomControls
00048 {
00049     public partial class TemperatureView : UserControl, ITemperatureView, IDeviceView
00050     {
00051         List<MaximCheckBox> temperatureConfigRegister1;
00052         List<MaximCheckBox> temperatureConfigRegister2;
00053 
00054         bool unitsCelsius = true;
00055         int refreshRate = 10;
00056         double temperature1C = 25;
00057         //int temperature1Hex;
00058         double temperature2C = 25;
00059 
00060         static string degreeCUnit = "°C";
00061         static string degreeFUnit = "°F";
00062         static string blankTemperature = "--.---";
00063 
00064         public static bool I2CErrorShown;
00065 
00066         bool internalUpdate = false;
00067         
00068 
00069         public event EventHandler<EventArgs> ReadAll;
00070         public event EventHandler<EventArgs> UnitsChanged;
00071         public event EventHandler<EventArgs> RequestReadTemperature;
00072         public event EventHandler<WriteRegisterEventArgs> WriteRegister;
00073         public event EventHandler<TemperatureEventArgs> OverTemperatureChanged;
00074         public event EventHandler<TemperatureEventArgs> HystersisChanged;
00075         public event EventHandler<DataFormatEventArgs> DataFormatChanged;
00076 
00077         public TemperatureView()
00078         {
00079             InitializeComponent();
00080 
00081             temperatureInitialize();
00082             temperatureEventsInitialize();
00083         }
00084 
00085         public bool UnitsCelsius
00086         {
00087             get
00088             {
00089                 return unitsCelsius;
00090             }
00091         }
00092 
00093         public int RefreshRate
00094         {
00095             get
00096             {
00097                 return refreshRate;
00098             }
00099             set
00100             {
00101                 refreshRate = value;
00102                 cboRefreshRate.Text = refreshRate.ToString();
00103                 timer1.Interval = value * 1000;
00104             }
00105         }
00106 
00107         public bool Connected { get; set; }
00108 
00109         public void DisplayTemperature(int index, TemperatureValue tempValue)
00110         {
00111             double temperature;
00112             int hex;
00113 
00114             if (unitsCelsius)
00115                 temperature = tempValue.TemperatureC;
00116             else
00117                 temperature = tempValue.TemperatureF;
00118 
00119             hex = tempValue.TemperatureHex;
00120 
00121             if (index == 0)
00122             {
00123                 temperature1C = tempValue.TemperatureC;
00124                 lblTemperature1.Text = temperature.ToString("F3");
00125                 lblTemperatureHex1.Text = "(" + hex.ToString("X4") + "h)";
00126             }
00127             else if (index == 1)
00128             {
00129                 temperature2C = tempValue.TemperatureC;
00130                 lblTemperature2.Text = temperature.ToString("F3");
00131                 lblTemperatureHex2.Text = "(" + hex.ToString("X4") + "h)";
00132             }
00133         }
00134 
00135         public void DisplayHysteresis(int index, TemperatureValue tempValue)
00136         {
00137             double temperature;
00138             int hex;
00139 
00140             if (unitsCelsius)
00141                 temperature = tempValue.TemperatureC;
00142             else
00143                 temperature = tempValue.TemperatureF;
00144 
00145             hex = tempValue.TemperatureHex;
00146 
00147             if (index == 0)
00148             {
00149                 txtHysteresis1.Text = temperature.ToString("F2");
00150                 lblHysteresis1.Text = "(" + hex.ToString("X4") + "h)";
00151             }
00152             else if (index == 1)
00153             {
00154                 txtHysteresis2.Text = temperature.ToString("F2");
00155                 lblHysteresis2.Text = "(" + hex.ToString("X4") + "h)";
00156             }
00157         }
00158 
00159         public void DisplayOverTemperature(int index, TemperatureValue tempValue)
00160         {
00161             double temperature;
00162             int hex;
00163 
00164             if (unitsCelsius)
00165                 temperature = tempValue.TemperatureC;
00166             else
00167                 temperature = tempValue.TemperatureF;
00168 
00169             hex = tempValue.TemperatureHex;
00170 
00171             if (index == 0)
00172             {
00173                 txtOvertemperature1.Text = temperature.ToString("F2");
00174                 lblOvertemperature1.Text = "(" + hex.ToString("X4") + "h)";
00175             }
00176             else if (index == 1)
00177             {
00178                 txtOvertemperature2.Text = temperature.ToString("F2");
00179                 lblOvertemperature2.Text = "(" + hex.ToString("X4") + "h)";
00180             }
00181         }
00182 
00183         public void DisplayConfigurationRegister(int index, int hex)
00184         {
00185             if (index == 0)
00186             {
00187                 byteToConfigurationCheckBoxes((byte)hex, temperatureConfigRegister1);
00188             }
00189             else if (index == 1)
00190             {
00191                 byteToConfigurationCheckBoxes((byte)hex, temperatureConfigRegister2);
00192 
00193             }
00194         }
00195 
00196         public void DisplayError(Control control, string message)
00197         {
00198             if (control!= null)
00199                 errorProvider1.SetError(control, message);
00200         }
00201 
00202         public void TimerState(bool state)
00203         {
00204             timer1.Enabled = state;
00205         }
00206 
00207 
00208         #region Temperature Tab UI
00209         void temperatureInitialize()
00210         {
00211             temperatureConfigRegister1 = new List<MaximCheckBox>();
00212             temperatureConfigRegister2 = new List<MaximCheckBox>();
00213 
00214             temperatureConfigRegister1.Add(chkOneShot1);
00215             temperatureConfigRegister1.Add(chkTimeout1);
00216             temperatureConfigRegister1.Add(chkDataFormat1);
00217             temperatureConfigRegister1.Add(chkFaultQueue11);
00218             temperatureConfigRegister1.Add(chkFaultQueue01);
00219             temperatureConfigRegister1.Add(chkOSPolarity1);
00220             temperatureConfigRegister1.Add(chkComparatorInterrupt1);
00221             temperatureConfigRegister1.Add(chkShutdown1);
00222 
00223             temperatureConfigRegister2.Add(chkOneShot2);
00224             temperatureConfigRegister2.Add(chkTimeout2);
00225             temperatureConfigRegister2.Add(chkDataFormat2);
00226             temperatureConfigRegister2.Add(chkFaultQueue12);
00227             temperatureConfigRegister2.Add(chkFaultQueue02);
00228             temperatureConfigRegister2.Add(chkOSPolarity2);
00229             temperatureConfigRegister2.Add(chkComparatorInterrupt2);
00230             temperatureConfigRegister2.Add(chkShutdown2);
00231 
00232             cboRefreshRate.SelectedIndex = 0;
00233         }
00234 
00235         void temperatureEventsInitialize()
00236         {
00237             tglCelsiusFahrenheit.CheckedChanged += new System.EventHandler(tglCelsiusFahrenheit_CheckedChanged);
00238             txtOvertemperature1.Leave += new System.EventHandler(temperatureRegisters_Leave);
00239             txtOvertemperature2.Leave += new System.EventHandler(temperatureRegisters_Leave);
00240             txtHysteresis1.Leave += new System.EventHandler(temperatureRegisters_Leave);
00241             txtHysteresis2.Leave += new System.EventHandler(temperatureRegisters_Leave);
00242             
00243             foreach (MaximCheckBox cb in temperatureConfigRegister1)
00244             {
00245                 cb.CheckedChanged += new System.EventHandler(configurationRegister_CheckedChanged);
00246             }
00247 
00248             foreach (MaximCheckBox cb in temperatureConfigRegister2)
00249             {
00250                 cb.CheckedChanged += new System.EventHandler(configurationRegister_CheckedChanged);
00251             }
00252         }
00253 
00254         void convertUnits(bool celsius)
00255         {
00256             if (celsius == true)
00257             {
00258                 lblUnits1.Text = degreeCUnit;
00259                 lblUnits2.Text = degreeCUnit;
00260                 lblUnits3.Text = degreeCUnit;
00261                 lblUnits4.Text = degreeCUnit;
00262                 lblUnits5.Text = degreeCUnit;
00263                 lblUnits6.Text = degreeCUnit;
00264 
00265                 //lblTemperature1.Text = fahrenheitToCelsius(Convert.ToDouble(lblTemperature1.Text)).ToString("F2");
00266                 //lblTemperature2.Text = fahrenheitToCelsius(Convert.ToDouble(lblTemperature2.Text)).ToString("F2");
00267                 //lblTemperature1.Text = blankTemperature;
00268                 //lblTemperature2.Text = blankTemperature;
00269 
00270                 txtOvertemperature1.Text = fahrenheitToCelsius(Convert.ToDouble(txtOvertemperature1.Text)).ToString("F2");
00271                 txtOvertemperature2.Text = fahrenheitToCelsius(Convert.ToDouble(txtOvertemperature2.Text)).ToString("F2");
00272 
00273                 txtHysteresis1.Text = fahrenheitToCelsius(Convert.ToDouble(txtHysteresis1.Text)).ToString("F2");
00274                 txtHysteresis2.Text = fahrenheitToCelsius(Convert.ToDouble(txtHysteresis2.Text)).ToString("F2");
00275             }
00276             else
00277             {
00278                 lblUnits1.Text = degreeFUnit;
00279                 lblUnits2.Text = degreeFUnit;
00280                 lblUnits3.Text = degreeFUnit;
00281                 lblUnits4.Text = degreeFUnit;
00282                 lblUnits5.Text = degreeFUnit;
00283                 lblUnits6.Text = degreeFUnit;
00284 
00285                 //lblTemperature1.Text = celsiusToFahrenheit(Convert.ToDouble(lblTemperature1.Text)).ToString("F2");
00286                 //lblTemperature2.Text = celsiusToFahrenheit(Convert.ToDouble(lblTemperature2.Text)).ToString("F2");
00287                 //lblTemperature1.Text = blankTemperature;
00288                 //lblTemperature2.Text = blankTemperature;
00289 
00290                 txtOvertemperature1.Text = celsiusToFahrenheit(Convert.ToDouble(txtOvertemperature1.Text)).ToString("F2");
00291                 txtOvertemperature2.Text = celsiusToFahrenheit(Convert.ToDouble(txtOvertemperature2.Text)).ToString("F2");
00292 
00293                 txtHysteresis1.Text = celsiusToFahrenheit(Convert.ToDouble(txtHysteresis1.Text)).ToString("F2");
00294                 txtHysteresis2.Text = celsiusToFahrenheit(Convert.ToDouble(txtHysteresis2.Text)).ToString("F2");
00295             }
00296         }
00297 
00298         void configurationRegisterToByte()
00299         {
00300             int config1, config2;
00301 
00302             config1 =
00303                 ((chkOneShot1.Checked == true ? 1 : 0) << 7) |
00304                 ((chkTimeout1.Checked == true ? 1 : 0) << 6) |
00305                 ((chkDataFormat1.Checked == true ? 1 : 0) << 5) |
00306                 ((chkFaultQueue11.Checked == true ? 1 : 0) << 4) |
00307                 ((chkFaultQueue01.Checked == true ? 1 : 0) << 3) |
00308                 ((chkOSPolarity1.Checked == true ? 1 : 0) << 2) |
00309                 ((chkComparatorInterrupt1.Checked == true ? 1 : 0) << 1) |
00310                 ((chkShutdown1.Checked == true ? 1 : 0) << 0);
00311 
00312             config2 =
00313                 ((chkOneShot2.Checked == true ? 1 : 0) << 7) |
00314                 ((chkTimeout2.Checked == true ? 1 : 0) << 6) |
00315                 ((chkDataFormat2.Checked == true ? 1 : 0) << 5) |
00316                 ((chkFaultQueue12.Checked == true ? 1 : 0) << 4) |
00317                 ((chkFaultQueue02.Checked == true ? 1 : 0) << 3) |
00318                 ((chkOSPolarity2.Checked == true ? 1 : 0) << 2) |
00319                 ((chkComparatorInterrupt2.Checked == true ? 1 : 0) << 1) |
00320                 ((chkShutdown2.Checked == true ? 1 : 0) << 0);
00321 
00322             lblConfigRegister1.Text = configurationByteToString((byte)config1);
00323             lblConfigRegister2.Text = configurationByteToString((byte)config2);
00324 
00325             if (WriteRegister != null && !internalUpdate)
00326             {
00327                 WriteRegister(this, new WriteRegisterEventArgs() { Index = 0, Register = 0x01, Data = config1 });
00328                 WriteRegister(this, new WriteRegisterEventArgs() { Index = 1, Register = 0x01, Data = config2 });
00329             }
00330         }
00331 
00332         string configurationByteToString(byte hex)
00333         {
00334             return "(" + hex.ToString("X2") + "h)";
00335         }
00336 
00337         void byteToConfigurationCheckBoxes(byte hex, List<MaximCheckBox> list)
00338         {
00339             internalUpdate = true;
00340             for (int i = 0; i < 8; i++ )
00341             {
00342                 list[i].Checked = (hex & (1 << (7 - i))) == (1 << (7 - i));
00343             }
00344             internalUpdate = false;
00345         }
00346 
00347         #endregion
00348 
00349         #region Helper Functions
00350         double celsiusToFahrenheit(double temperature)
00351         {
00352             return temperature * 9 / 5 + 32;
00353         }
00354 
00355         double fahrenheitToCelsius(double temperature)
00356         {
00357             return (temperature - 32) * 5 / 9;
00358         }
00359 
00360         short temperatureToHex(double temperature)
00361         {
00362             return (short)(temperature * Math.Pow(2, 8));
00363         }
00364 
00365         public double HexToTemperature(int hex, bool dataFormat)
00366         {
00367             return hexToTemperature(new byte[] { (byte)(hex >> 8), (byte)(hex & 0xFF) }, dataFormat);
00368         }
00369 
00370         double hexToTemperature(byte[] hex, bool dataFormat)
00371         {
00372             int rawCode;
00373             int normalCode;
00374 
00375             if (hex.Length != 2)
00376                 return 0;
00377 
00378             rawCode = (hex[0] << 8) + hex[1];
00379 
00380             if (rawCode > 0x7fff)
00381                 normalCode = rawCode - 0x10000;
00382             else
00383                 normalCode = rawCode;
00384 
00385             if (dataFormat == false)
00386                 return normalCode / Math.Pow(2, 8);
00387             else
00388                 return normalCode / Math.Pow(2, 8) + 64;
00389 
00390         }
00391 
00392         #endregion
00393 
00394 
00395         #region Temperature Tab Events
00396         private void tglCelsiusFahrenheit_CheckedChanged(object sender, EventArgs e)
00397         {
00398             MaximStyle.MaximToggleButton tgl = (MaximStyle.MaximToggleButton)sender;
00399 
00400             if (internalUpdate == true)
00401                 return;
00402 
00403             unitsCelsius = !tgl.Checked;
00404 
00405             convertUnits(unitsCelsius);
00406 
00407             if (lblTemperature1.Text != blankTemperature)
00408             {
00409                 if (unitsCelsius)
00410                 {
00411                     lblTemperature1.Text = temperature1C.ToString("F3");
00412                     lblTemperature2.Text = temperature2C.ToString("F3");
00413                 }
00414                 else
00415                 {
00416                     lblTemperature1.Text = celsiusToFahrenheit(temperature1C).ToString("F3");
00417                     lblTemperature2.Text = celsiusToFahrenheit(temperature2C).ToString("F3");
00418                 }
00419             }
00420         }
00421 
00422         void temperatureRegisters_Leave(object sender, EventArgs e)
00423         {
00424             TextBox tb = (TextBox)sender;
00425             byte[] tempData = new byte[2];
00426             double temperature; // Temperature in C
00427 
00428             if (Double.TryParse(tb.Text, out temperature))
00429             {
00430 
00431                 //To clear just the textBox that raised this event, send SetError with empty string
00432                 errorProvider1.SetError(tb, "");
00433 
00434                 // Save Temperature value in C
00435                 if (unitsCelsius == true)
00436                     temperature = Convert.ToDouble(tb.Text);
00437                 else
00438                     temperature = fahrenheitToCelsius(Convert.ToDouble(tb.Text));
00439 
00440                 if (OverTemperatureChanged != null && HystersisChanged != null)
00441                 {
00442                     if (ReferenceEquals(tb, txtOvertemperature1))
00443                     {
00444                         OverTemperatureChanged(this, new TemperatureEventArgs() { Index = 0, Temperature = temperature, Control = tb }); 
00445                     }
00446                     else if (ReferenceEquals(tb, txtOvertemperature2))
00447                     {
00448                         OverTemperatureChanged(this, new TemperatureEventArgs() { Index = 1, Temperature = temperature, Control = tb }); 
00449                     }
00450                     else if (ReferenceEquals(tb, txtHysteresis1))
00451                     {
00452                         HystersisChanged(this, new TemperatureEventArgs() { Index = 0, Temperature = temperature, Control = tb }); 
00453                     }
00454                     else if (ReferenceEquals(tb, txtHysteresis2))
00455                     {
00456                         HystersisChanged(this, new TemperatureEventArgs() { Index = 1, Temperature = temperature, Control = tb }); 
00457                     }
00458                 }
00459             }
00460             else
00461             {
00462                 errorProvider1.SetError(tb, "Invalid Temperature Format");
00463             }
00464         }
00465 
00466         private void configurationRegister_CheckedChanged(object sender, EventArgs e)
00467         {
00468             try
00469             {
00470                 configurationRegisterToByte();
00471             }
00472             catch (Exception)
00473             {
00474                 ShowI2CErrorMessage();
00475             }
00476         }
00477 
00478 
00479         #endregion
00480 
00481         private void maximButton1_Click(object sender, EventArgs e)
00482         {
00483             if (ReadAll != null)
00484             {
00485                 try
00486                 {
00487                     ReadAll(this, e);
00488                 }
00489                 catch (Exception)
00490                 {
00491                     ShowI2CErrorMessage();
00492                 }
00493             }
00494         }
00495 
00496         private void timer1_Tick(object sender, EventArgs e)
00497         {
00498             if (RequestReadTemperature != null)
00499             {
00500                 try
00501                 {
00502                     RequestReadTemperature(this, e);
00503                 }
00504                 catch (Exception)
00505                 {
00506                     ShowI2CErrorMessage();
00507                 }
00508             }
00509         }
00510 
00511         private static void ShowI2CErrorMessage()
00512         {
00513             string message = "MAX30205 was not found. Please reconnect and try again or choose 'Cancel' to exit.";
00514             string caption = "I2C Error";
00515             MessageBoxButtons buttons = MessageBoxButtons.RetryCancel;
00516             MessageBoxIcon icon = MessageBoxIcon.Warning;
00517             DialogResult result;
00518 
00519             // Displays the MessageBox if there isn't one displayed already.
00520 
00521             if (!I2CErrorShown)
00522             {
00523                 I2CErrorShown = true;
00524                 result = MessageBox.Show(message, caption, buttons, icon);
00525 
00526                 if (result == System.Windows.Forms.DialogResult.Cancel)
00527                 {
00528                     Application.Exit();
00529                 }
00530                 else
00531                 {
00532                     //Application.Restart();// Don't do this. Rerun address scan
00533                     //InitBoard();
00534                 }
00535 
00536                 I2CErrorShown = false;
00537 
00538             }
00539         }
00540 
00541         private void cboRefreshRate_SelectedIndexChanged(object sender, EventArgs e)
00542         {
00543             RefreshRate = Convert.ToInt32(cboRefreshRate.SelectedItem);
00544         }
00545 
00546         private void tglCelsiusFahrenheit_CheckedChanged_1(object sender, EventArgs e)
00547         {
00548 
00549         }
00550 
00551         private void lblTemperature1_Click(object sender, EventArgs e)
00552         {
00553 
00554         }
00555 
00556         private void chkDataFormat1_Click(object sender, EventArgs e)
00557         {
00558             MaximCheckBox chk = (MaximCheckBox)sender;
00559 
00560             if (DataFormatChanged != null)
00561             {
00562                 if (chk == chkDataFormat1)
00563                     DataFormatChanged(this, new DataFormatEventArgs() { Index = 0, DataFormat = chkDataFormat1.Checked });
00564                 else if (chk == chkDataFormat2)
00565                     DataFormatChanged(this, new DataFormatEventArgs() { Index = 1, DataFormat = chkDataFormat2.Checked });
00566                 else
00567                     throw new NotImplementedException();
00568             }
00569         }
00570     }
00571 }