![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
repo time
Dependencies: mbed MAX14720 MAX30205 USBDevice
HspGuiSourceV301/HSPGui/CustomControls/TemperatureView.cs@20:6d2af70c92ab, 2021-04-06 (annotated)
- Committer:
- darienf
- Date:
- Tue Apr 06 06:41:40 2021 +0000
- Revision:
- 20:6d2af70c92ab
another repo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
darienf | 20:6d2af70c92ab | 1 | /******************************************************************************* |
darienf | 20:6d2af70c92ab | 2 | * Copyright (C) 2016 Maxim Integrated Products, Inc., All rights Reserved. |
darienf | 20:6d2af70c92ab | 3 | * |
darienf | 20:6d2af70c92ab | 4 | * This software is protected by copyright laws of the United States and |
darienf | 20:6d2af70c92ab | 5 | * of foreign countries. This material may also be protected by patent laws |
darienf | 20:6d2af70c92ab | 6 | * and technology transfer regulations of the United States and of foreign |
darienf | 20:6d2af70c92ab | 7 | * countries. This software is furnished under a license agreement and/or a |
darienf | 20:6d2af70c92ab | 8 | * nondisclosure agreement and may only be used or reproduced in accordance |
darienf | 20:6d2af70c92ab | 9 | * with the terms of those agreements. Dissemination of this information to |
darienf | 20:6d2af70c92ab | 10 | * any party or parties not specified in the license agreement and/or |
darienf | 20:6d2af70c92ab | 11 | * nondisclosure agreement is expressly prohibited. |
darienf | 20:6d2af70c92ab | 12 | * |
darienf | 20:6d2af70c92ab | 13 | * The above copyright notice and this permission notice shall be included |
darienf | 20:6d2af70c92ab | 14 | * in all copies or substantial portions of the Software. |
darienf | 20:6d2af70c92ab | 15 | * |
darienf | 20:6d2af70c92ab | 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
darienf | 20:6d2af70c92ab | 17 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
darienf | 20:6d2af70c92ab | 18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
darienf | 20:6d2af70c92ab | 19 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
darienf | 20:6d2af70c92ab | 20 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
darienf | 20:6d2af70c92ab | 21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
darienf | 20:6d2af70c92ab | 22 | * OTHER DEALINGS IN THE SOFTWARE. |
darienf | 20:6d2af70c92ab | 23 | * |
darienf | 20:6d2af70c92ab | 24 | * Except as contained in this notice, the name of Maxim Integrated |
darienf | 20:6d2af70c92ab | 25 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
darienf | 20:6d2af70c92ab | 26 | * Products, Inc. Branding Policy. |
darienf | 20:6d2af70c92ab | 27 | * |
darienf | 20:6d2af70c92ab | 28 | * The mere transfer of this software does not imply any licenses |
darienf | 20:6d2af70c92ab | 29 | * of trade secrets, proprietary technology, copyrights, patents, |
darienf | 20:6d2af70c92ab | 30 | * trademarks, maskwork rights, or any other form of intellectual |
darienf | 20:6d2af70c92ab | 31 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
darienf | 20:6d2af70c92ab | 32 | * ownership rights. |
darienf | 20:6d2af70c92ab | 33 | ******************************************************************************* |
darienf | 20:6d2af70c92ab | 34 | */ |
darienf | 20:6d2af70c92ab | 35 | |
darienf | 20:6d2af70c92ab | 36 | using System; |
darienf | 20:6d2af70c92ab | 37 | using System.Collections.Generic; |
darienf | 20:6d2af70c92ab | 38 | using System.ComponentModel; |
darienf | 20:6d2af70c92ab | 39 | using System.Drawing; |
darienf | 20:6d2af70c92ab | 40 | using System.Data; |
darienf | 20:6d2af70c92ab | 41 | using System.Linq; |
darienf | 20:6d2af70c92ab | 42 | using System.Text; |
darienf | 20:6d2af70c92ab | 43 | using System.Windows.Forms; |
darienf | 20:6d2af70c92ab | 44 | using MaximStyle; |
darienf | 20:6d2af70c92ab | 45 | using MAX30205EVKit.View; |
darienf | 20:6d2af70c92ab | 46 | |
darienf | 20:6d2af70c92ab | 47 | namespace HealthSensorPlatform.CustomControls |
darienf | 20:6d2af70c92ab | 48 | { |
darienf | 20:6d2af70c92ab | 49 | public partial class TemperatureView : UserControl, ITemperatureView, IDeviceView |
darienf | 20:6d2af70c92ab | 50 | { |
darienf | 20:6d2af70c92ab | 51 | List<MaximCheckBox> temperatureConfigRegister1; |
darienf | 20:6d2af70c92ab | 52 | List<MaximCheckBox> temperatureConfigRegister2; |
darienf | 20:6d2af70c92ab | 53 | |
darienf | 20:6d2af70c92ab | 54 | bool unitsCelsius = true; |
darienf | 20:6d2af70c92ab | 55 | int refreshRate = 10; |
darienf | 20:6d2af70c92ab | 56 | double temperature1C = 25; |
darienf | 20:6d2af70c92ab | 57 | //int temperature1Hex; |
darienf | 20:6d2af70c92ab | 58 | double temperature2C = 25; |
darienf | 20:6d2af70c92ab | 59 | |
darienf | 20:6d2af70c92ab | 60 | static string degreeCUnit = "°C"; |
darienf | 20:6d2af70c92ab | 61 | static string degreeFUnit = "°F"; |
darienf | 20:6d2af70c92ab | 62 | static string blankTemperature = "--.---"; |
darienf | 20:6d2af70c92ab | 63 | |
darienf | 20:6d2af70c92ab | 64 | public static bool I2CErrorShown; |
darienf | 20:6d2af70c92ab | 65 | |
darienf | 20:6d2af70c92ab | 66 | bool internalUpdate = false; |
darienf | 20:6d2af70c92ab | 67 | |
darienf | 20:6d2af70c92ab | 68 | |
darienf | 20:6d2af70c92ab | 69 | public event EventHandler<EventArgs> ReadAll; |
darienf | 20:6d2af70c92ab | 70 | public event EventHandler<EventArgs> UnitsChanged; |
darienf | 20:6d2af70c92ab | 71 | public event EventHandler<EventArgs> RequestReadTemperature; |
darienf | 20:6d2af70c92ab | 72 | public event EventHandler<WriteRegisterEventArgs> WriteRegister; |
darienf | 20:6d2af70c92ab | 73 | public event EventHandler<TemperatureEventArgs> OverTemperatureChanged; |
darienf | 20:6d2af70c92ab | 74 | public event EventHandler<TemperatureEventArgs> HystersisChanged; |
darienf | 20:6d2af70c92ab | 75 | public event EventHandler<DataFormatEventArgs> DataFormatChanged; |
darienf | 20:6d2af70c92ab | 76 | |
darienf | 20:6d2af70c92ab | 77 | public TemperatureView() |
darienf | 20:6d2af70c92ab | 78 | { |
darienf | 20:6d2af70c92ab | 79 | InitializeComponent(); |
darienf | 20:6d2af70c92ab | 80 | |
darienf | 20:6d2af70c92ab | 81 | temperatureInitialize(); |
darienf | 20:6d2af70c92ab | 82 | temperatureEventsInitialize(); |
darienf | 20:6d2af70c92ab | 83 | } |
darienf | 20:6d2af70c92ab | 84 | |
darienf | 20:6d2af70c92ab | 85 | public bool UnitsCelsius |
darienf | 20:6d2af70c92ab | 86 | { |
darienf | 20:6d2af70c92ab | 87 | get |
darienf | 20:6d2af70c92ab | 88 | { |
darienf | 20:6d2af70c92ab | 89 | return unitsCelsius; |
darienf | 20:6d2af70c92ab | 90 | } |
darienf | 20:6d2af70c92ab | 91 | } |
darienf | 20:6d2af70c92ab | 92 | |
darienf | 20:6d2af70c92ab | 93 | public int RefreshRate |
darienf | 20:6d2af70c92ab | 94 | { |
darienf | 20:6d2af70c92ab | 95 | get |
darienf | 20:6d2af70c92ab | 96 | { |
darienf | 20:6d2af70c92ab | 97 | return refreshRate; |
darienf | 20:6d2af70c92ab | 98 | } |
darienf | 20:6d2af70c92ab | 99 | set |
darienf | 20:6d2af70c92ab | 100 | { |
darienf | 20:6d2af70c92ab | 101 | refreshRate = value; |
darienf | 20:6d2af70c92ab | 102 | cboRefreshRate.Text = refreshRate.ToString(); |
darienf | 20:6d2af70c92ab | 103 | timer1.Interval = value * 1000; |
darienf | 20:6d2af70c92ab | 104 | } |
darienf | 20:6d2af70c92ab | 105 | } |
darienf | 20:6d2af70c92ab | 106 | |
darienf | 20:6d2af70c92ab | 107 | public bool Connected { get; set; } |
darienf | 20:6d2af70c92ab | 108 | |
darienf | 20:6d2af70c92ab | 109 | public void DisplayTemperature(int index, TemperatureValue tempValue) |
darienf | 20:6d2af70c92ab | 110 | { |
darienf | 20:6d2af70c92ab | 111 | double temperature; |
darienf | 20:6d2af70c92ab | 112 | int hex; |
darienf | 20:6d2af70c92ab | 113 | |
darienf | 20:6d2af70c92ab | 114 | if (unitsCelsius) |
darienf | 20:6d2af70c92ab | 115 | temperature = tempValue.TemperatureC; |
darienf | 20:6d2af70c92ab | 116 | else |
darienf | 20:6d2af70c92ab | 117 | temperature = tempValue.TemperatureF; |
darienf | 20:6d2af70c92ab | 118 | |
darienf | 20:6d2af70c92ab | 119 | hex = tempValue.TemperatureHex; |
darienf | 20:6d2af70c92ab | 120 | |
darienf | 20:6d2af70c92ab | 121 | if (index == 0) |
darienf | 20:6d2af70c92ab | 122 | { |
darienf | 20:6d2af70c92ab | 123 | temperature1C = tempValue.TemperatureC; |
darienf | 20:6d2af70c92ab | 124 | lblTemperature1.Text = temperature.ToString("F3"); |
darienf | 20:6d2af70c92ab | 125 | lblTemperatureHex1.Text = "(" + hex.ToString("X4") + "h)"; |
darienf | 20:6d2af70c92ab | 126 | } |
darienf | 20:6d2af70c92ab | 127 | else if (index == 1) |
darienf | 20:6d2af70c92ab | 128 | { |
darienf | 20:6d2af70c92ab | 129 | temperature2C = tempValue.TemperatureC; |
darienf | 20:6d2af70c92ab | 130 | lblTemperature2.Text = temperature.ToString("F3"); |
darienf | 20:6d2af70c92ab | 131 | lblTemperatureHex2.Text = "(" + hex.ToString("X4") + "h)"; |
darienf | 20:6d2af70c92ab | 132 | } |
darienf | 20:6d2af70c92ab | 133 | } |
darienf | 20:6d2af70c92ab | 134 | |
darienf | 20:6d2af70c92ab | 135 | public void DisplayHysteresis(int index, TemperatureValue tempValue) |
darienf | 20:6d2af70c92ab | 136 | { |
darienf | 20:6d2af70c92ab | 137 | double temperature; |
darienf | 20:6d2af70c92ab | 138 | int hex; |
darienf | 20:6d2af70c92ab | 139 | |
darienf | 20:6d2af70c92ab | 140 | if (unitsCelsius) |
darienf | 20:6d2af70c92ab | 141 | temperature = tempValue.TemperatureC; |
darienf | 20:6d2af70c92ab | 142 | else |
darienf | 20:6d2af70c92ab | 143 | temperature = tempValue.TemperatureF; |
darienf | 20:6d2af70c92ab | 144 | |
darienf | 20:6d2af70c92ab | 145 | hex = tempValue.TemperatureHex; |
darienf | 20:6d2af70c92ab | 146 | |
darienf | 20:6d2af70c92ab | 147 | if (index == 0) |
darienf | 20:6d2af70c92ab | 148 | { |
darienf | 20:6d2af70c92ab | 149 | txtHysteresis1.Text = temperature.ToString("F2"); |
darienf | 20:6d2af70c92ab | 150 | lblHysteresis1.Text = "(" + hex.ToString("X4") + "h)"; |
darienf | 20:6d2af70c92ab | 151 | } |
darienf | 20:6d2af70c92ab | 152 | else if (index == 1) |
darienf | 20:6d2af70c92ab | 153 | { |
darienf | 20:6d2af70c92ab | 154 | txtHysteresis2.Text = temperature.ToString("F2"); |
darienf | 20:6d2af70c92ab | 155 | lblHysteresis2.Text = "(" + hex.ToString("X4") + "h)"; |
darienf | 20:6d2af70c92ab | 156 | } |
darienf | 20:6d2af70c92ab | 157 | } |
darienf | 20:6d2af70c92ab | 158 | |
darienf | 20:6d2af70c92ab | 159 | public void DisplayOverTemperature(int index, TemperatureValue tempValue) |
darienf | 20:6d2af70c92ab | 160 | { |
darienf | 20:6d2af70c92ab | 161 | double temperature; |
darienf | 20:6d2af70c92ab | 162 | int hex; |
darienf | 20:6d2af70c92ab | 163 | |
darienf | 20:6d2af70c92ab | 164 | if (unitsCelsius) |
darienf | 20:6d2af70c92ab | 165 | temperature = tempValue.TemperatureC; |
darienf | 20:6d2af70c92ab | 166 | else |
darienf | 20:6d2af70c92ab | 167 | temperature = tempValue.TemperatureF; |
darienf | 20:6d2af70c92ab | 168 | |
darienf | 20:6d2af70c92ab | 169 | hex = tempValue.TemperatureHex; |
darienf | 20:6d2af70c92ab | 170 | |
darienf | 20:6d2af70c92ab | 171 | if (index == 0) |
darienf | 20:6d2af70c92ab | 172 | { |
darienf | 20:6d2af70c92ab | 173 | txtOvertemperature1.Text = temperature.ToString("F2"); |
darienf | 20:6d2af70c92ab | 174 | lblOvertemperature1.Text = "(" + hex.ToString("X4") + "h)"; |
darienf | 20:6d2af70c92ab | 175 | } |
darienf | 20:6d2af70c92ab | 176 | else if (index == 1) |
darienf | 20:6d2af70c92ab | 177 | { |
darienf | 20:6d2af70c92ab | 178 | txtOvertemperature2.Text = temperature.ToString("F2"); |
darienf | 20:6d2af70c92ab | 179 | lblOvertemperature2.Text = "(" + hex.ToString("X4") + "h)"; |
darienf | 20:6d2af70c92ab | 180 | } |
darienf | 20:6d2af70c92ab | 181 | } |
darienf | 20:6d2af70c92ab | 182 | |
darienf | 20:6d2af70c92ab | 183 | public void DisplayConfigurationRegister(int index, int hex) |
darienf | 20:6d2af70c92ab | 184 | { |
darienf | 20:6d2af70c92ab | 185 | if (index == 0) |
darienf | 20:6d2af70c92ab | 186 | { |
darienf | 20:6d2af70c92ab | 187 | byteToConfigurationCheckBoxes((byte)hex, temperatureConfigRegister1); |
darienf | 20:6d2af70c92ab | 188 | } |
darienf | 20:6d2af70c92ab | 189 | else if (index == 1) |
darienf | 20:6d2af70c92ab | 190 | { |
darienf | 20:6d2af70c92ab | 191 | byteToConfigurationCheckBoxes((byte)hex, temperatureConfigRegister2); |
darienf | 20:6d2af70c92ab | 192 | |
darienf | 20:6d2af70c92ab | 193 | } |
darienf | 20:6d2af70c92ab | 194 | } |
darienf | 20:6d2af70c92ab | 195 | |
darienf | 20:6d2af70c92ab | 196 | public void DisplayError(Control control, string message) |
darienf | 20:6d2af70c92ab | 197 | { |
darienf | 20:6d2af70c92ab | 198 | if (control!= null) |
darienf | 20:6d2af70c92ab | 199 | errorProvider1.SetError(control, message); |
darienf | 20:6d2af70c92ab | 200 | } |
darienf | 20:6d2af70c92ab | 201 | |
darienf | 20:6d2af70c92ab | 202 | public void TimerState(bool state) |
darienf | 20:6d2af70c92ab | 203 | { |
darienf | 20:6d2af70c92ab | 204 | timer1.Enabled = state; |
darienf | 20:6d2af70c92ab | 205 | } |
darienf | 20:6d2af70c92ab | 206 | |
darienf | 20:6d2af70c92ab | 207 | |
darienf | 20:6d2af70c92ab | 208 | #region Temperature Tab UI |
darienf | 20:6d2af70c92ab | 209 | void temperatureInitialize() |
darienf | 20:6d2af70c92ab | 210 | { |
darienf | 20:6d2af70c92ab | 211 | temperatureConfigRegister1 = new List<MaximCheckBox>(); |
darienf | 20:6d2af70c92ab | 212 | temperatureConfigRegister2 = new List<MaximCheckBox>(); |
darienf | 20:6d2af70c92ab | 213 | |
darienf | 20:6d2af70c92ab | 214 | temperatureConfigRegister1.Add(chkOneShot1); |
darienf | 20:6d2af70c92ab | 215 | temperatureConfigRegister1.Add(chkTimeout1); |
darienf | 20:6d2af70c92ab | 216 | temperatureConfigRegister1.Add(chkDataFormat1); |
darienf | 20:6d2af70c92ab | 217 | temperatureConfigRegister1.Add(chkFaultQueue11); |
darienf | 20:6d2af70c92ab | 218 | temperatureConfigRegister1.Add(chkFaultQueue01); |
darienf | 20:6d2af70c92ab | 219 | temperatureConfigRegister1.Add(chkOSPolarity1); |
darienf | 20:6d2af70c92ab | 220 | temperatureConfigRegister1.Add(chkComparatorInterrupt1); |
darienf | 20:6d2af70c92ab | 221 | temperatureConfigRegister1.Add(chkShutdown1); |
darienf | 20:6d2af70c92ab | 222 | |
darienf | 20:6d2af70c92ab | 223 | temperatureConfigRegister2.Add(chkOneShot2); |
darienf | 20:6d2af70c92ab | 224 | temperatureConfigRegister2.Add(chkTimeout2); |
darienf | 20:6d2af70c92ab | 225 | temperatureConfigRegister2.Add(chkDataFormat2); |
darienf | 20:6d2af70c92ab | 226 | temperatureConfigRegister2.Add(chkFaultQueue12); |
darienf | 20:6d2af70c92ab | 227 | temperatureConfigRegister2.Add(chkFaultQueue02); |
darienf | 20:6d2af70c92ab | 228 | temperatureConfigRegister2.Add(chkOSPolarity2); |
darienf | 20:6d2af70c92ab | 229 | temperatureConfigRegister2.Add(chkComparatorInterrupt2); |
darienf | 20:6d2af70c92ab | 230 | temperatureConfigRegister2.Add(chkShutdown2); |
darienf | 20:6d2af70c92ab | 231 | |
darienf | 20:6d2af70c92ab | 232 | cboRefreshRate.SelectedIndex = 0; |
darienf | 20:6d2af70c92ab | 233 | } |
darienf | 20:6d2af70c92ab | 234 | |
darienf | 20:6d2af70c92ab | 235 | void temperatureEventsInitialize() |
darienf | 20:6d2af70c92ab | 236 | { |
darienf | 20:6d2af70c92ab | 237 | tglCelsiusFahrenheit.CheckedChanged += new System.EventHandler(tglCelsiusFahrenheit_CheckedChanged); |
darienf | 20:6d2af70c92ab | 238 | txtOvertemperature1.Leave += new System.EventHandler(temperatureRegisters_Leave); |
darienf | 20:6d2af70c92ab | 239 | txtOvertemperature2.Leave += new System.EventHandler(temperatureRegisters_Leave); |
darienf | 20:6d2af70c92ab | 240 | txtHysteresis1.Leave += new System.EventHandler(temperatureRegisters_Leave); |
darienf | 20:6d2af70c92ab | 241 | txtHysteresis2.Leave += new System.EventHandler(temperatureRegisters_Leave); |
darienf | 20:6d2af70c92ab | 242 | |
darienf | 20:6d2af70c92ab | 243 | foreach (MaximCheckBox cb in temperatureConfigRegister1) |
darienf | 20:6d2af70c92ab | 244 | { |
darienf | 20:6d2af70c92ab | 245 | cb.CheckedChanged += new System.EventHandler(configurationRegister_CheckedChanged); |
darienf | 20:6d2af70c92ab | 246 | } |
darienf | 20:6d2af70c92ab | 247 | |
darienf | 20:6d2af70c92ab | 248 | foreach (MaximCheckBox cb in temperatureConfigRegister2) |
darienf | 20:6d2af70c92ab | 249 | { |
darienf | 20:6d2af70c92ab | 250 | cb.CheckedChanged += new System.EventHandler(configurationRegister_CheckedChanged); |
darienf | 20:6d2af70c92ab | 251 | } |
darienf | 20:6d2af70c92ab | 252 | } |
darienf | 20:6d2af70c92ab | 253 | |
darienf | 20:6d2af70c92ab | 254 | void convertUnits(bool celsius) |
darienf | 20:6d2af70c92ab | 255 | { |
darienf | 20:6d2af70c92ab | 256 | if (celsius == true) |
darienf | 20:6d2af70c92ab | 257 | { |
darienf | 20:6d2af70c92ab | 258 | lblUnits1.Text = degreeCUnit; |
darienf | 20:6d2af70c92ab | 259 | lblUnits2.Text = degreeCUnit; |
darienf | 20:6d2af70c92ab | 260 | lblUnits3.Text = degreeCUnit; |
darienf | 20:6d2af70c92ab | 261 | lblUnits4.Text = degreeCUnit; |
darienf | 20:6d2af70c92ab | 262 | lblUnits5.Text = degreeCUnit; |
darienf | 20:6d2af70c92ab | 263 | lblUnits6.Text = degreeCUnit; |
darienf | 20:6d2af70c92ab | 264 | |
darienf | 20:6d2af70c92ab | 265 | //lblTemperature1.Text = fahrenheitToCelsius(Convert.ToDouble(lblTemperature1.Text)).ToString("F2"); |
darienf | 20:6d2af70c92ab | 266 | //lblTemperature2.Text = fahrenheitToCelsius(Convert.ToDouble(lblTemperature2.Text)).ToString("F2"); |
darienf | 20:6d2af70c92ab | 267 | //lblTemperature1.Text = blankTemperature; |
darienf | 20:6d2af70c92ab | 268 | //lblTemperature2.Text = blankTemperature; |
darienf | 20:6d2af70c92ab | 269 | |
darienf | 20:6d2af70c92ab | 270 | txtOvertemperature1.Text = fahrenheitToCelsius(Convert.ToDouble(txtOvertemperature1.Text)).ToString("F2"); |
darienf | 20:6d2af70c92ab | 271 | txtOvertemperature2.Text = fahrenheitToCelsius(Convert.ToDouble(txtOvertemperature2.Text)).ToString("F2"); |
darienf | 20:6d2af70c92ab | 272 | |
darienf | 20:6d2af70c92ab | 273 | txtHysteresis1.Text = fahrenheitToCelsius(Convert.ToDouble(txtHysteresis1.Text)).ToString("F2"); |
darienf | 20:6d2af70c92ab | 274 | txtHysteresis2.Text = fahrenheitToCelsius(Convert.ToDouble(txtHysteresis2.Text)).ToString("F2"); |
darienf | 20:6d2af70c92ab | 275 | } |
darienf | 20:6d2af70c92ab | 276 | else |
darienf | 20:6d2af70c92ab | 277 | { |
darienf | 20:6d2af70c92ab | 278 | lblUnits1.Text = degreeFUnit; |
darienf | 20:6d2af70c92ab | 279 | lblUnits2.Text = degreeFUnit; |
darienf | 20:6d2af70c92ab | 280 | lblUnits3.Text = degreeFUnit; |
darienf | 20:6d2af70c92ab | 281 | lblUnits4.Text = degreeFUnit; |
darienf | 20:6d2af70c92ab | 282 | lblUnits5.Text = degreeFUnit; |
darienf | 20:6d2af70c92ab | 283 | lblUnits6.Text = degreeFUnit; |
darienf | 20:6d2af70c92ab | 284 | |
darienf | 20:6d2af70c92ab | 285 | //lblTemperature1.Text = celsiusToFahrenheit(Convert.ToDouble(lblTemperature1.Text)).ToString("F2"); |
darienf | 20:6d2af70c92ab | 286 | //lblTemperature2.Text = celsiusToFahrenheit(Convert.ToDouble(lblTemperature2.Text)).ToString("F2"); |
darienf | 20:6d2af70c92ab | 287 | //lblTemperature1.Text = blankTemperature; |
darienf | 20:6d2af70c92ab | 288 | //lblTemperature2.Text = blankTemperature; |
darienf | 20:6d2af70c92ab | 289 | |
darienf | 20:6d2af70c92ab | 290 | txtOvertemperature1.Text = celsiusToFahrenheit(Convert.ToDouble(txtOvertemperature1.Text)).ToString("F2"); |
darienf | 20:6d2af70c92ab | 291 | txtOvertemperature2.Text = celsiusToFahrenheit(Convert.ToDouble(txtOvertemperature2.Text)).ToString("F2"); |
darienf | 20:6d2af70c92ab | 292 | |
darienf | 20:6d2af70c92ab | 293 | txtHysteresis1.Text = celsiusToFahrenheit(Convert.ToDouble(txtHysteresis1.Text)).ToString("F2"); |
darienf | 20:6d2af70c92ab | 294 | txtHysteresis2.Text = celsiusToFahrenheit(Convert.ToDouble(txtHysteresis2.Text)).ToString("F2"); |
darienf | 20:6d2af70c92ab | 295 | } |
darienf | 20:6d2af70c92ab | 296 | } |
darienf | 20:6d2af70c92ab | 297 | |
darienf | 20:6d2af70c92ab | 298 | void configurationRegisterToByte() |
darienf | 20:6d2af70c92ab | 299 | { |
darienf | 20:6d2af70c92ab | 300 | int config1, config2; |
darienf | 20:6d2af70c92ab | 301 | |
darienf | 20:6d2af70c92ab | 302 | config1 = |
darienf | 20:6d2af70c92ab | 303 | ((chkOneShot1.Checked == true ? 1 : 0) << 7) | |
darienf | 20:6d2af70c92ab | 304 | ((chkTimeout1.Checked == true ? 1 : 0) << 6) | |
darienf | 20:6d2af70c92ab | 305 | ((chkDataFormat1.Checked == true ? 1 : 0) << 5) | |
darienf | 20:6d2af70c92ab | 306 | ((chkFaultQueue11.Checked == true ? 1 : 0) << 4) | |
darienf | 20:6d2af70c92ab | 307 | ((chkFaultQueue01.Checked == true ? 1 : 0) << 3) | |
darienf | 20:6d2af70c92ab | 308 | ((chkOSPolarity1.Checked == true ? 1 : 0) << 2) | |
darienf | 20:6d2af70c92ab | 309 | ((chkComparatorInterrupt1.Checked == true ? 1 : 0) << 1) | |
darienf | 20:6d2af70c92ab | 310 | ((chkShutdown1.Checked == true ? 1 : 0) << 0); |
darienf | 20:6d2af70c92ab | 311 | |
darienf | 20:6d2af70c92ab | 312 | config2 = |
darienf | 20:6d2af70c92ab | 313 | ((chkOneShot2.Checked == true ? 1 : 0) << 7) | |
darienf | 20:6d2af70c92ab | 314 | ((chkTimeout2.Checked == true ? 1 : 0) << 6) | |
darienf | 20:6d2af70c92ab | 315 | ((chkDataFormat2.Checked == true ? 1 : 0) << 5) | |
darienf | 20:6d2af70c92ab | 316 | ((chkFaultQueue12.Checked == true ? 1 : 0) << 4) | |
darienf | 20:6d2af70c92ab | 317 | ((chkFaultQueue02.Checked == true ? 1 : 0) << 3) | |
darienf | 20:6d2af70c92ab | 318 | ((chkOSPolarity2.Checked == true ? 1 : 0) << 2) | |
darienf | 20:6d2af70c92ab | 319 | ((chkComparatorInterrupt2.Checked == true ? 1 : 0) << 1) | |
darienf | 20:6d2af70c92ab | 320 | ((chkShutdown2.Checked == true ? 1 : 0) << 0); |
darienf | 20:6d2af70c92ab | 321 | |
darienf | 20:6d2af70c92ab | 322 | lblConfigRegister1.Text = configurationByteToString((byte)config1); |
darienf | 20:6d2af70c92ab | 323 | lblConfigRegister2.Text = configurationByteToString((byte)config2); |
darienf | 20:6d2af70c92ab | 324 | |
darienf | 20:6d2af70c92ab | 325 | if (WriteRegister != null && !internalUpdate) |
darienf | 20:6d2af70c92ab | 326 | { |
darienf | 20:6d2af70c92ab | 327 | WriteRegister(this, new WriteRegisterEventArgs() { Index = 0, Register = 0x01, Data = config1 }); |
darienf | 20:6d2af70c92ab | 328 | WriteRegister(this, new WriteRegisterEventArgs() { Index = 1, Register = 0x01, Data = config2 }); |
darienf | 20:6d2af70c92ab | 329 | } |
darienf | 20:6d2af70c92ab | 330 | } |
darienf | 20:6d2af70c92ab | 331 | |
darienf | 20:6d2af70c92ab | 332 | string configurationByteToString(byte hex) |
darienf | 20:6d2af70c92ab | 333 | { |
darienf | 20:6d2af70c92ab | 334 | return "(" + hex.ToString("X2") + "h)"; |
darienf | 20:6d2af70c92ab | 335 | } |
darienf | 20:6d2af70c92ab | 336 | |
darienf | 20:6d2af70c92ab | 337 | void byteToConfigurationCheckBoxes(byte hex, List<MaximCheckBox> list) |
darienf | 20:6d2af70c92ab | 338 | { |
darienf | 20:6d2af70c92ab | 339 | internalUpdate = true; |
darienf | 20:6d2af70c92ab | 340 | for (int i = 0; i < 8; i++ ) |
darienf | 20:6d2af70c92ab | 341 | { |
darienf | 20:6d2af70c92ab | 342 | list[i].Checked = (hex & (1 << (7 - i))) == (1 << (7 - i)); |
darienf | 20:6d2af70c92ab | 343 | } |
darienf | 20:6d2af70c92ab | 344 | internalUpdate = false; |
darienf | 20:6d2af70c92ab | 345 | } |
darienf | 20:6d2af70c92ab | 346 | |
darienf | 20:6d2af70c92ab | 347 | #endregion |
darienf | 20:6d2af70c92ab | 348 | |
darienf | 20:6d2af70c92ab | 349 | #region Helper Functions |
darienf | 20:6d2af70c92ab | 350 | double celsiusToFahrenheit(double temperature) |
darienf | 20:6d2af70c92ab | 351 | { |
darienf | 20:6d2af70c92ab | 352 | return temperature * 9 / 5 + 32; |
darienf | 20:6d2af70c92ab | 353 | } |
darienf | 20:6d2af70c92ab | 354 | |
darienf | 20:6d2af70c92ab | 355 | double fahrenheitToCelsius(double temperature) |
darienf | 20:6d2af70c92ab | 356 | { |
darienf | 20:6d2af70c92ab | 357 | return (temperature - 32) * 5 / 9; |
darienf | 20:6d2af70c92ab | 358 | } |
darienf | 20:6d2af70c92ab | 359 | |
darienf | 20:6d2af70c92ab | 360 | short temperatureToHex(double temperature) |
darienf | 20:6d2af70c92ab | 361 | { |
darienf | 20:6d2af70c92ab | 362 | return (short)(temperature * Math.Pow(2, 8)); |
darienf | 20:6d2af70c92ab | 363 | } |
darienf | 20:6d2af70c92ab | 364 | |
darienf | 20:6d2af70c92ab | 365 | public double HexToTemperature(int hex, bool dataFormat) |
darienf | 20:6d2af70c92ab | 366 | { |
darienf | 20:6d2af70c92ab | 367 | return hexToTemperature(new byte[] { (byte)(hex >> 8), (byte)(hex & 0xFF) }, dataFormat); |
darienf | 20:6d2af70c92ab | 368 | } |
darienf | 20:6d2af70c92ab | 369 | |
darienf | 20:6d2af70c92ab | 370 | double hexToTemperature(byte[] hex, bool dataFormat) |
darienf | 20:6d2af70c92ab | 371 | { |
darienf | 20:6d2af70c92ab | 372 | int rawCode; |
darienf | 20:6d2af70c92ab | 373 | int normalCode; |
darienf | 20:6d2af70c92ab | 374 | |
darienf | 20:6d2af70c92ab | 375 | if (hex.Length != 2) |
darienf | 20:6d2af70c92ab | 376 | return 0; |
darienf | 20:6d2af70c92ab | 377 | |
darienf | 20:6d2af70c92ab | 378 | rawCode = (hex[0] << 8) + hex[1]; |
darienf | 20:6d2af70c92ab | 379 | |
darienf | 20:6d2af70c92ab | 380 | if (rawCode > 0x7fff) |
darienf | 20:6d2af70c92ab | 381 | normalCode = rawCode - 0x10000; |
darienf | 20:6d2af70c92ab | 382 | else |
darienf | 20:6d2af70c92ab | 383 | normalCode = rawCode; |
darienf | 20:6d2af70c92ab | 384 | |
darienf | 20:6d2af70c92ab | 385 | if (dataFormat == false) |
darienf | 20:6d2af70c92ab | 386 | return normalCode / Math.Pow(2, 8); |
darienf | 20:6d2af70c92ab | 387 | else |
darienf | 20:6d2af70c92ab | 388 | return normalCode / Math.Pow(2, 8) + 64; |
darienf | 20:6d2af70c92ab | 389 | |
darienf | 20:6d2af70c92ab | 390 | } |
darienf | 20:6d2af70c92ab | 391 | |
darienf | 20:6d2af70c92ab | 392 | #endregion |
darienf | 20:6d2af70c92ab | 393 | |
darienf | 20:6d2af70c92ab | 394 | |
darienf | 20:6d2af70c92ab | 395 | #region Temperature Tab Events |
darienf | 20:6d2af70c92ab | 396 | private void tglCelsiusFahrenheit_CheckedChanged(object sender, EventArgs e) |
darienf | 20:6d2af70c92ab | 397 | { |
darienf | 20:6d2af70c92ab | 398 | MaximStyle.MaximToggleButton tgl = (MaximStyle.MaximToggleButton)sender; |
darienf | 20:6d2af70c92ab | 399 | |
darienf | 20:6d2af70c92ab | 400 | if (internalUpdate == true) |
darienf | 20:6d2af70c92ab | 401 | return; |
darienf | 20:6d2af70c92ab | 402 | |
darienf | 20:6d2af70c92ab | 403 | unitsCelsius = !tgl.Checked; |
darienf | 20:6d2af70c92ab | 404 | |
darienf | 20:6d2af70c92ab | 405 | convertUnits(unitsCelsius); |
darienf | 20:6d2af70c92ab | 406 | |
darienf | 20:6d2af70c92ab | 407 | if (lblTemperature1.Text != blankTemperature) |
darienf | 20:6d2af70c92ab | 408 | { |
darienf | 20:6d2af70c92ab | 409 | if (unitsCelsius) |
darienf | 20:6d2af70c92ab | 410 | { |
darienf | 20:6d2af70c92ab | 411 | lblTemperature1.Text = temperature1C.ToString("F3"); |
darienf | 20:6d2af70c92ab | 412 | lblTemperature2.Text = temperature2C.ToString("F3"); |
darienf | 20:6d2af70c92ab | 413 | } |
darienf | 20:6d2af70c92ab | 414 | else |
darienf | 20:6d2af70c92ab | 415 | { |
darienf | 20:6d2af70c92ab | 416 | lblTemperature1.Text = celsiusToFahrenheit(temperature1C).ToString("F3"); |
darienf | 20:6d2af70c92ab | 417 | lblTemperature2.Text = celsiusToFahrenheit(temperature2C).ToString("F3"); |
darienf | 20:6d2af70c92ab | 418 | } |
darienf | 20:6d2af70c92ab | 419 | } |
darienf | 20:6d2af70c92ab | 420 | } |
darienf | 20:6d2af70c92ab | 421 | |
darienf | 20:6d2af70c92ab | 422 | void temperatureRegisters_Leave(object sender, EventArgs e) |
darienf | 20:6d2af70c92ab | 423 | { |
darienf | 20:6d2af70c92ab | 424 | TextBox tb = (TextBox)sender; |
darienf | 20:6d2af70c92ab | 425 | byte[] tempData = new byte[2]; |
darienf | 20:6d2af70c92ab | 426 | double temperature; // Temperature in C |
darienf | 20:6d2af70c92ab | 427 | |
darienf | 20:6d2af70c92ab | 428 | if (Double.TryParse(tb.Text, out temperature)) |
darienf | 20:6d2af70c92ab | 429 | { |
darienf | 20:6d2af70c92ab | 430 | |
darienf | 20:6d2af70c92ab | 431 | //To clear just the textBox that raised this event, send SetError with empty string |
darienf | 20:6d2af70c92ab | 432 | errorProvider1.SetError(tb, ""); |
darienf | 20:6d2af70c92ab | 433 | |
darienf | 20:6d2af70c92ab | 434 | // Save Temperature value in C |
darienf | 20:6d2af70c92ab | 435 | if (unitsCelsius == true) |
darienf | 20:6d2af70c92ab | 436 | temperature = Convert.ToDouble(tb.Text); |
darienf | 20:6d2af70c92ab | 437 | else |
darienf | 20:6d2af70c92ab | 438 | temperature = fahrenheitToCelsius(Convert.ToDouble(tb.Text)); |
darienf | 20:6d2af70c92ab | 439 | |
darienf | 20:6d2af70c92ab | 440 | if (OverTemperatureChanged != null && HystersisChanged != null) |
darienf | 20:6d2af70c92ab | 441 | { |
darienf | 20:6d2af70c92ab | 442 | if (ReferenceEquals(tb, txtOvertemperature1)) |
darienf | 20:6d2af70c92ab | 443 | { |
darienf | 20:6d2af70c92ab | 444 | OverTemperatureChanged(this, new TemperatureEventArgs() { Index = 0, Temperature = temperature, Control = tb }); |
darienf | 20:6d2af70c92ab | 445 | } |
darienf | 20:6d2af70c92ab | 446 | else if (ReferenceEquals(tb, txtOvertemperature2)) |
darienf | 20:6d2af70c92ab | 447 | { |
darienf | 20:6d2af70c92ab | 448 | OverTemperatureChanged(this, new TemperatureEventArgs() { Index = 1, Temperature = temperature, Control = tb }); |
darienf | 20:6d2af70c92ab | 449 | } |
darienf | 20:6d2af70c92ab | 450 | else if (ReferenceEquals(tb, txtHysteresis1)) |
darienf | 20:6d2af70c92ab | 451 | { |
darienf | 20:6d2af70c92ab | 452 | HystersisChanged(this, new TemperatureEventArgs() { Index = 0, Temperature = temperature, Control = tb }); |
darienf | 20:6d2af70c92ab | 453 | } |
darienf | 20:6d2af70c92ab | 454 | else if (ReferenceEquals(tb, txtHysteresis2)) |
darienf | 20:6d2af70c92ab | 455 | { |
darienf | 20:6d2af70c92ab | 456 | HystersisChanged(this, new TemperatureEventArgs() { Index = 1, Temperature = temperature, Control = tb }); |
darienf | 20:6d2af70c92ab | 457 | } |
darienf | 20:6d2af70c92ab | 458 | } |
darienf | 20:6d2af70c92ab | 459 | } |
darienf | 20:6d2af70c92ab | 460 | else |
darienf | 20:6d2af70c92ab | 461 | { |
darienf | 20:6d2af70c92ab | 462 | errorProvider1.SetError(tb, "Invalid Temperature Format"); |
darienf | 20:6d2af70c92ab | 463 | } |
darienf | 20:6d2af70c92ab | 464 | } |
darienf | 20:6d2af70c92ab | 465 | |
darienf | 20:6d2af70c92ab | 466 | private void configurationRegister_CheckedChanged(object sender, EventArgs e) |
darienf | 20:6d2af70c92ab | 467 | { |
darienf | 20:6d2af70c92ab | 468 | try |
darienf | 20:6d2af70c92ab | 469 | { |
darienf | 20:6d2af70c92ab | 470 | configurationRegisterToByte(); |
darienf | 20:6d2af70c92ab | 471 | } |
darienf | 20:6d2af70c92ab | 472 | catch (Exception) |
darienf | 20:6d2af70c92ab | 473 | { |
darienf | 20:6d2af70c92ab | 474 | ShowI2CErrorMessage(); |
darienf | 20:6d2af70c92ab | 475 | } |
darienf | 20:6d2af70c92ab | 476 | } |
darienf | 20:6d2af70c92ab | 477 | |
darienf | 20:6d2af70c92ab | 478 | |
darienf | 20:6d2af70c92ab | 479 | #endregion |
darienf | 20:6d2af70c92ab | 480 | |
darienf | 20:6d2af70c92ab | 481 | private void maximButton1_Click(object sender, EventArgs e) |
darienf | 20:6d2af70c92ab | 482 | { |
darienf | 20:6d2af70c92ab | 483 | if (ReadAll != null) |
darienf | 20:6d2af70c92ab | 484 | { |
darienf | 20:6d2af70c92ab | 485 | try |
darienf | 20:6d2af70c92ab | 486 | { |
darienf | 20:6d2af70c92ab | 487 | ReadAll(this, e); |
darienf | 20:6d2af70c92ab | 488 | } |
darienf | 20:6d2af70c92ab | 489 | catch (Exception) |
darienf | 20:6d2af70c92ab | 490 | { |
darienf | 20:6d2af70c92ab | 491 | ShowI2CErrorMessage(); |
darienf | 20:6d2af70c92ab | 492 | } |
darienf | 20:6d2af70c92ab | 493 | } |
darienf | 20:6d2af70c92ab | 494 | } |
darienf | 20:6d2af70c92ab | 495 | |
darienf | 20:6d2af70c92ab | 496 | private void timer1_Tick(object sender, EventArgs e) |
darienf | 20:6d2af70c92ab | 497 | { |
darienf | 20:6d2af70c92ab | 498 | if (RequestReadTemperature != null) |
darienf | 20:6d2af70c92ab | 499 | { |
darienf | 20:6d2af70c92ab | 500 | try |
darienf | 20:6d2af70c92ab | 501 | { |
darienf | 20:6d2af70c92ab | 502 | RequestReadTemperature(this, e); |
darienf | 20:6d2af70c92ab | 503 | } |
darienf | 20:6d2af70c92ab | 504 | catch (Exception) |
darienf | 20:6d2af70c92ab | 505 | { |
darienf | 20:6d2af70c92ab | 506 | ShowI2CErrorMessage(); |
darienf | 20:6d2af70c92ab | 507 | } |
darienf | 20:6d2af70c92ab | 508 | } |
darienf | 20:6d2af70c92ab | 509 | } |
darienf | 20:6d2af70c92ab | 510 | |
darienf | 20:6d2af70c92ab | 511 | private static void ShowI2CErrorMessage() |
darienf | 20:6d2af70c92ab | 512 | { |
darienf | 20:6d2af70c92ab | 513 | string message = "MAX30205 was not found. Please reconnect and try again or choose 'Cancel' to exit."; |
darienf | 20:6d2af70c92ab | 514 | string caption = "I2C Error"; |
darienf | 20:6d2af70c92ab | 515 | MessageBoxButtons buttons = MessageBoxButtons.RetryCancel; |
darienf | 20:6d2af70c92ab | 516 | MessageBoxIcon icon = MessageBoxIcon.Warning; |
darienf | 20:6d2af70c92ab | 517 | DialogResult result; |
darienf | 20:6d2af70c92ab | 518 | |
darienf | 20:6d2af70c92ab | 519 | // Displays the MessageBox if there isn't one displayed already. |
darienf | 20:6d2af70c92ab | 520 | |
darienf | 20:6d2af70c92ab | 521 | if (!I2CErrorShown) |
darienf | 20:6d2af70c92ab | 522 | { |
darienf | 20:6d2af70c92ab | 523 | I2CErrorShown = true; |
darienf | 20:6d2af70c92ab | 524 | result = MessageBox.Show(message, caption, buttons, icon); |
darienf | 20:6d2af70c92ab | 525 | |
darienf | 20:6d2af70c92ab | 526 | if (result == System.Windows.Forms.DialogResult.Cancel) |
darienf | 20:6d2af70c92ab | 527 | { |
darienf | 20:6d2af70c92ab | 528 | Application.Exit(); |
darienf | 20:6d2af70c92ab | 529 | } |
darienf | 20:6d2af70c92ab | 530 | else |
darienf | 20:6d2af70c92ab | 531 | { |
darienf | 20:6d2af70c92ab | 532 | //Application.Restart();// Don't do this. Rerun address scan |
darienf | 20:6d2af70c92ab | 533 | //InitBoard(); |
darienf | 20:6d2af70c92ab | 534 | } |
darienf | 20:6d2af70c92ab | 535 | |
darienf | 20:6d2af70c92ab | 536 | I2CErrorShown = false; |
darienf | 20:6d2af70c92ab | 537 | |
darienf | 20:6d2af70c92ab | 538 | } |
darienf | 20:6d2af70c92ab | 539 | } |
darienf | 20:6d2af70c92ab | 540 | |
darienf | 20:6d2af70c92ab | 541 | private void cboRefreshRate_SelectedIndexChanged(object sender, EventArgs e) |
darienf | 20:6d2af70c92ab | 542 | { |
darienf | 20:6d2af70c92ab | 543 | RefreshRate = Convert.ToInt32(cboRefreshRate.SelectedItem); |
darienf | 20:6d2af70c92ab | 544 | } |
darienf | 20:6d2af70c92ab | 545 | |
darienf | 20:6d2af70c92ab | 546 | private void tglCelsiusFahrenheit_CheckedChanged_1(object sender, EventArgs e) |
darienf | 20:6d2af70c92ab | 547 | { |
darienf | 20:6d2af70c92ab | 548 | |
darienf | 20:6d2af70c92ab | 549 | } |
darienf | 20:6d2af70c92ab | 550 | |
darienf | 20:6d2af70c92ab | 551 | private void lblTemperature1_Click(object sender, EventArgs e) |
darienf | 20:6d2af70c92ab | 552 | { |
darienf | 20:6d2af70c92ab | 553 | |
darienf | 20:6d2af70c92ab | 554 | } |
darienf | 20:6d2af70c92ab | 555 | |
darienf | 20:6d2af70c92ab | 556 | private void chkDataFormat1_Click(object sender, EventArgs e) |
darienf | 20:6d2af70c92ab | 557 | { |
darienf | 20:6d2af70c92ab | 558 | MaximCheckBox chk = (MaximCheckBox)sender; |
darienf | 20:6d2af70c92ab | 559 | |
darienf | 20:6d2af70c92ab | 560 | if (DataFormatChanged != null) |
darienf | 20:6d2af70c92ab | 561 | { |
darienf | 20:6d2af70c92ab | 562 | if (chk == chkDataFormat1) |
darienf | 20:6d2af70c92ab | 563 | DataFormatChanged(this, new DataFormatEventArgs() { Index = 0, DataFormat = chkDataFormat1.Checked }); |
darienf | 20:6d2af70c92ab | 564 | else if (chk == chkDataFormat2) |
darienf | 20:6d2af70c92ab | 565 | DataFormatChanged(this, new DataFormatEventArgs() { Index = 1, DataFormat = chkDataFormat2.Checked }); |
darienf | 20:6d2af70c92ab | 566 | else |
darienf | 20:6d2af70c92ab | 567 | throw new NotImplementedException(); |
darienf | 20:6d2af70c92ab | 568 | } |
darienf | 20:6d2af70c92ab | 569 | } |
darienf | 20:6d2af70c92ab | 570 | } |
darienf | 20:6d2af70c92ab | 571 | } |