repo time

Dependencies:   mbed MAX14720 MAX30205 USBDevice

Committer:
darienf
Date:
Tue Apr 06 06:41:40 2021 +0000
Revision:
20:6d2af70c92ab
another repo

Who changed what in which revision?

UserRevisionLine numberNew 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.Linq;
darienf 20:6d2af70c92ab 39 using System.Text;
darienf 20:6d2af70c92ab 40 using System.Threading.Tasks;
darienf 20:6d2af70c92ab 41 using System.Timers;
darienf 20:6d2af70c92ab 42 using System.Windows.Forms;
darienf 20:6d2af70c92ab 43 using MAX30205EVKit.Model;
darienf 20:6d2af70c92ab 44 using MAX30205EVKit.View;
darienf 20:6d2af70c92ab 45
darienf 20:6d2af70c92ab 46 using HealthSensorPlatform.CustomControls;
darienf 20:6d2af70c92ab 47 using HealthSensorPlatform;
darienf 20:6d2af70c92ab 48
darienf 20:6d2af70c92ab 49 using Maxim.Utility;
darienf 20:6d2af70c92ab 50
darienf 20:6d2af70c92ab 51 namespace MAX30205EVKit.Presenter
darienf 20:6d2af70c92ab 52 {
darienf 20:6d2af70c92ab 53 class TemperaturePresenter
darienf 20:6d2af70c92ab 54 {
darienf 20:6d2af70c92ab 55 ITemperatureModel[] model;
darienf 20:6d2af70c92ab 56 ITemperatureView view;
darienf 20:6d2af70c92ab 57 IDeviceView connected;
darienf 20:6d2af70c92ab 58 //IFileLogView fileLogView;
darienf 20:6d2af70c92ab 59
darienf 20:6d2af70c92ab 60 TemperatureValue temperature;
darienf 20:6d2af70c92ab 61 TemperatureValue overTemperature;
darienf 20:6d2af70c92ab 62 TemperatureValue hystersis;
darienf 20:6d2af70c92ab 63
darienf 20:6d2af70c92ab 64 //bool firstTime = true;
darienf 20:6d2af70c92ab 65 //double time = 0.0;
darienf 20:6d2af70c92ab 66
darienf 20:6d2af70c92ab 67 public TemperaturePresenter(ITemperatureModel[] model, IDeviceView connected, ITemperatureView view)
darienf 20:6d2af70c92ab 68 {
darienf 20:6d2af70c92ab 69 this.model = model;
darienf 20:6d2af70c92ab 70 this.view = view;
darienf 20:6d2af70c92ab 71 this.connected = connected;
darienf 20:6d2af70c92ab 72
darienf 20:6d2af70c92ab 73 //this.fileLogView = fileLogView;
darienf 20:6d2af70c92ab 74
darienf 20:6d2af70c92ab 75 view.ReadAll += new EventHandler<EventArgs>(OnReadAll);
darienf 20:6d2af70c92ab 76 view.RequestReadTemperature += new EventHandler<EventArgs>(OnReadTemperature);
darienf 20:6d2af70c92ab 77 view.OverTemperatureChanged += new EventHandler<TemperatureEventArgs>(OnOverTemperatureChanged);
darienf 20:6d2af70c92ab 78 view.HystersisChanged += new EventHandler<TemperatureEventArgs>(OnHystersisChanged);
darienf 20:6d2af70c92ab 79 view.WriteRegister += new EventHandler<WriteRegisterEventArgs>(OnWriteRegister);
darienf 20:6d2af70c92ab 80 view.DataFormatChanged += new EventHandler<DataFormatEventArgs>(OnDataFormatChanged);
darienf 20:6d2af70c92ab 81 }
darienf 20:6d2af70c92ab 82
darienf 20:6d2af70c92ab 83 /*
darienf 20:6d2af70c92ab 84 public TemperaturePresenter(ITemperatureModel model, IDeviceView connected, ITemperatureView view, IFileLogView fileLogView)
darienf 20:6d2af70c92ab 85 {
darienf 20:6d2af70c92ab 86 this.model = model;
darienf 20:6d2af70c92ab 87 this.view = view;
darienf 20:6d2af70c92ab 88 this.connected = connected;
darienf 20:6d2af70c92ab 89
darienf 20:6d2af70c92ab 90 this.fileLogView = fileLogView;
darienf 20:6d2af70c92ab 91
darienf 20:6d2af70c92ab 92 view.ReadAll += new EventHandler<EventArgs>(OnReadAll);
darienf 20:6d2af70c92ab 93 view.RequestReadTemperature += new EventHandler<EventArgs>(OnReadTemperature);
darienf 20:6d2af70c92ab 94 view.OverTemperatureChanged += new EventHandler<TemperatureEventArgs>(OnOverTemperatureChanged);
darienf 20:6d2af70c92ab 95 view.HystersisChanged += new EventHandler<TemperatureEventArgs>(OnHystersisChanged);
darienf 20:6d2af70c92ab 96 view.WriteRegister += new EventHandler<WriteRegisterEventArgs>(OnWriteRegister);
darienf 20:6d2af70c92ab 97
darienf 20:6d2af70c92ab 98 }
darienf 20:6d2af70c92ab 99 */
darienf 20:6d2af70c92ab 100
darienf 20:6d2af70c92ab 101 public void WriteRegister(int index, int register, int data)
darienf 20:6d2af70c92ab 102 {
darienf 20:6d2af70c92ab 103 if (connected.Connected)
darienf 20:6d2af70c92ab 104 model[index].RegWrite(register, data);
darienf 20:6d2af70c92ab 105 }
darienf 20:6d2af70c92ab 106
darienf 20:6d2af70c92ab 107 public void OnReadAll(object sender, EventArgs e)
darienf 20:6d2af70c92ab 108 {
darienf 20:6d2af70c92ab 109 int temperatureHex;
darienf 20:6d2af70c92ab 110 int configuration;
darienf 20:6d2af70c92ab 111 int hystersisHex;
darienf 20:6d2af70c92ab 112 int overTemperatureHex;
darienf 20:6d2af70c92ab 113
darienf 20:6d2af70c92ab 114
darienf 20:6d2af70c92ab 115 if (connected.Connected)
darienf 20:6d2af70c92ab 116 {
darienf 20:6d2af70c92ab 117 for (int i = 0; i < model.Length; i++)
darienf 20:6d2af70c92ab 118 {
darienf 20:6d2af70c92ab 119 temperatureHex = model[i].RegRead(0x00);
darienf 20:6d2af70c92ab 120 configuration = model[i].RegRead(0x01);
darienf 20:6d2af70c92ab 121 hystersisHex = model[i].RegRead(0x02);
darienf 20:6d2af70c92ab 122 overTemperatureHex = model[i].RegRead(0x03);
darienf 20:6d2af70c92ab 123
darienf 20:6d2af70c92ab 124 temperature = new TemperatureValue(temperatureHex, model[i].DataFormat);
darienf 20:6d2af70c92ab 125 overTemperature = new TemperatureValue(overTemperatureHex, model[i].DataFormat);
darienf 20:6d2af70c92ab 126 hystersis = new TemperatureValue(hystersisHex, model[i].DataFormat);
darienf 20:6d2af70c92ab 127
darienf 20:6d2af70c92ab 128 view.DisplayTemperature(i, temperature);
darienf 20:6d2af70c92ab 129 view.DisplayConfigurationRegister(i, configuration);
darienf 20:6d2af70c92ab 130 view.DisplayOverTemperature(i, overTemperature);
darienf 20:6d2af70c92ab 131 view.DisplayHysteresis(i, hystersis);
darienf 20:6d2af70c92ab 132 }
darienf 20:6d2af70c92ab 133 }
darienf 20:6d2af70c92ab 134 }
darienf 20:6d2af70c92ab 135
darienf 20:6d2af70c92ab 136 public void OnDataFormatChanged(object sender, DataFormatEventArgs e)
darienf 20:6d2af70c92ab 137 {
darienf 20:6d2af70c92ab 138 int hysteresisHex = model[e.Index].Registers[0x02];
darienf 20:6d2af70c92ab 139 int overtemperature = model[e.Index].Registers[0x03];
darienf 20:6d2af70c92ab 140
darienf 20:6d2af70c92ab 141 if (connected.Connected)
darienf 20:6d2af70c92ab 142 {
darienf 20:6d2af70c92ab 143 overTemperature = new TemperatureValue(overtemperature, e.DataFormat);
darienf 20:6d2af70c92ab 144 hystersis = new TemperatureValue(hysteresisHex, e.DataFormat);
darienf 20:6d2af70c92ab 145
darienf 20:6d2af70c92ab 146 view.DisplayOverTemperature(e.Index, overTemperature);
darienf 20:6d2af70c92ab 147 view.DisplayHysteresis(e.Index, hystersis);
darienf 20:6d2af70c92ab 148 }
darienf 20:6d2af70c92ab 149 }
darienf 20:6d2af70c92ab 150
darienf 20:6d2af70c92ab 151 void OnReadTemperature(object sender, EventArgs e)
darienf 20:6d2af70c92ab 152 {
darienf 20:6d2af70c92ab 153 if (connected.Connected)
darienf 20:6d2af70c92ab 154 {
darienf 20:6d2af70c92ab 155 for (int i = 0; i < model.Length; i++)
darienf 20:6d2af70c92ab 156 {
darienf 20:6d2af70c92ab 157 int configuration = model[i].RegRead(0x01);
darienf 20:6d2af70c92ab 158
darienf 20:6d2af70c92ab 159 //model[i].RegWrite(0x01, configuration | 0x81);
darienf 20:6d2af70c92ab 160 model[i].RegWrite(0x01, configuration | 0x80);
darienf 20:6d2af70c92ab 161 System.Threading.Thread.Sleep(50);
darienf 20:6d2af70c92ab 162 int temp = model[i].RegRead(0x00);
darienf 20:6d2af70c92ab 163
darienf 20:6d2af70c92ab 164 TemperatureValue tempValue = new TemperatureValue(temp, model[i].DataFormat);
darienf 20:6d2af70c92ab 165 view.DisplayTemperature(i, tempValue);
darienf 20:6d2af70c92ab 166
darienf 20:6d2af70c92ab 167 //If user chooses to log Temperature, write lines to CSV
darienf 20:6d2af70c92ab 168 WriteTempToFileLog(tempValue);
darienf 20:6d2af70c92ab 169 }
darienf 20:6d2af70c92ab 170 }
darienf 20:6d2af70c92ab 171 }
darienf 20:6d2af70c92ab 172
darienf 20:6d2af70c92ab 173
darienf 20:6d2af70c92ab 174
darienf 20:6d2af70c92ab 175 void OnOverTemperatureChanged(object sender, TemperatureEventArgs e)
darienf 20:6d2af70c92ab 176 {
darienf 20:6d2af70c92ab 177 if (!IsWithinRange(e.Temperature.ToString(), model[e.Index].DataFormat))
darienf 20:6d2af70c92ab 178 view.DisplayError(e.Control, "Temperature Out of Range");
darienf 20:6d2af70c92ab 179 else
darienf 20:6d2af70c92ab 180 {
darienf 20:6d2af70c92ab 181 overTemperature = new TemperatureValue(e.Temperature, model[e.Index].DataFormat);
darienf 20:6d2af70c92ab 182
darienf 20:6d2af70c92ab 183 view.DisplayOverTemperature(e.Index, overTemperature);
darienf 20:6d2af70c92ab 184
darienf 20:6d2af70c92ab 185 if (connected.Connected)
darienf 20:6d2af70c92ab 186 model[e.Index].RegWrite(0x03, overTemperature.TemperatureHex);
darienf 20:6d2af70c92ab 187 }
darienf 20:6d2af70c92ab 188
darienf 20:6d2af70c92ab 189 }
darienf 20:6d2af70c92ab 190
darienf 20:6d2af70c92ab 191 void OnHystersisChanged(object sender, TemperatureEventArgs e)
darienf 20:6d2af70c92ab 192 {
darienf 20:6d2af70c92ab 193 if (!IsWithinRange(e.Temperature.ToString(), model[e.Index].DataFormat))
darienf 20:6d2af70c92ab 194 view.DisplayError(e.Control, "Temperature Out of Range");
darienf 20:6d2af70c92ab 195 else
darienf 20:6d2af70c92ab 196 {
darienf 20:6d2af70c92ab 197 hystersis = new TemperatureValue(e.Temperature, model[e.Index].DataFormat);
darienf 20:6d2af70c92ab 198
darienf 20:6d2af70c92ab 199 view.DisplayHysteresis(e.Index, hystersis);
darienf 20:6d2af70c92ab 200
darienf 20:6d2af70c92ab 201 if (connected.Connected)
darienf 20:6d2af70c92ab 202 model[e.Index].RegWrite(0x02, hystersis.TemperatureHex);
darienf 20:6d2af70c92ab 203 }
darienf 20:6d2af70c92ab 204
darienf 20:6d2af70c92ab 205
darienf 20:6d2af70c92ab 206 }
darienf 20:6d2af70c92ab 207
darienf 20:6d2af70c92ab 208 void OnWriteRegister(object sender, WriteRegisterEventArgs e)
darienf 20:6d2af70c92ab 209 {
darienf 20:6d2af70c92ab 210 WriteRegister(e.Index, e.Register, e.Data);
darienf 20:6d2af70c92ab 211 }
darienf 20:6d2af70c92ab 212
darienf 20:6d2af70c92ab 213 void WriteTempToFileLog(TemperatureValue tempValue)
darienf 20:6d2af70c92ab 214 {
darienf 20:6d2af70c92ab 215 /*
darienf 20:6d2af70c92ab 216 if (fileLogView.Enable)
darienf 20:6d2af70c92ab 217 {
darienf 20:6d2af70c92ab 218 if (firstTime)
darienf 20:6d2af70c92ab 219 {
darienf 20:6d2af70c92ab 220 firstTime = false;
darienf 20:6d2af70c92ab 221 fileLogView.WriteLine("Time (s), Temperature (°C)");
darienf 20:6d2af70c92ab 222 }
darienf 20:6d2af70c92ab 223 else
darienf 20:6d2af70c92ab 224 {
darienf 20:6d2af70c92ab 225 time = time + view.RefreshRate;
darienf 20:6d2af70c92ab 226 }
darienf 20:6d2af70c92ab 227 var temp = Math.Round(tempValue.TemperatureC, 3);
darienf 20:6d2af70c92ab 228 fileLogView.WriteTemp(time, temp);
darienf 20:6d2af70c92ab 229 }
darienf 20:6d2af70c92ab 230 */
darienf 20:6d2af70c92ab 231 }
darienf 20:6d2af70c92ab 232
darienf 20:6d2af70c92ab 233 bool IsWithinRange(string str, bool dataFormat)
darienf 20:6d2af70c92ab 234 {
darienf 20:6d2af70c92ab 235 double temp;
darienf 20:6d2af70c92ab 236 if (Double.TryParse(str, out temp))
darienf 20:6d2af70c92ab 237 {
darienf 20:6d2af70c92ab 238 if (!dataFormat)
darienf 20:6d2af70c92ab 239
darienf 20:6d2af70c92ab 240 return (temp >= -128.00) && (temp < 127.997);
darienf 20:6d2af70c92ab 241
darienf 20:6d2af70c92ab 242 return (temp >= -64.00) && (temp < 191.997);
darienf 20:6d2af70c92ab 243 }
darienf 20:6d2af70c92ab 244
darienf 20:6d2af70c92ab 245 return false;
darienf 20:6d2af70c92ab 246 }
darienf 20:6d2af70c92ab 247
darienf 20:6d2af70c92ab 248 /* void OnTimerTick(object sender, ElapsedEventArgs e)
darienf 20:6d2af70c92ab 249 {
darienf 20:6d2af70c92ab 250 if (connected.Connected)
darienf 20:6d2af70c92ab 251 {
darienf 20:6d2af70c92ab 252 int temperature;
darienf 20:6d2af70c92ab 253
darienf 20:6d2af70c92ab 254 model.RegWrite(0x01, model.Registers[0x01] | 0x81);
darienf 20:6d2af70c92ab 255 System.Threading.Thread.Sleep(50);
darienf 20:6d2af70c92ab 256 temperature = model.RegRead(0x00);
darienf 20:6d2af70c92ab 257
darienf 20:6d2af70c92ab 258 TemperatureValue tempValue = new TemperatureValue(temperature, model.DataFormat);
darienf 20:6d2af70c92ab 259 view.DisplayTemperature(0, tempValue);
darienf 20:6d2af70c92ab 260 }
darienf 20:6d2af70c92ab 261 }*/
darienf 20:6d2af70c92ab 262
darienf 20:6d2af70c92ab 263
darienf 20:6d2af70c92ab 264
darienf 20:6d2af70c92ab 265 /* double hexToTemperature(int hex, bool dataFormat, bool unitCelsius)
darienf 20:6d2af70c92ab 266 {
darienf 20:6d2af70c92ab 267 int normalCode;
darienf 20:6d2af70c92ab 268 int rawCode = hex;
darienf 20:6d2af70c92ab 269 double celsius;
darienf 20:6d2af70c92ab 270
darienf 20:6d2af70c92ab 271 if (rawCode > 0x7fff)
darienf 20:6d2af70c92ab 272 normalCode = rawCode - 0x10000;
darienf 20:6d2af70c92ab 273 else
darienf 20:6d2af70c92ab 274 normalCode = rawCode;
darienf 20:6d2af70c92ab 275
darienf 20:6d2af70c92ab 276 if (dataFormat == false)
darienf 20:6d2af70c92ab 277 celsius = normalCode / Math.Pow(2, 8);
darienf 20:6d2af70c92ab 278 else
darienf 20:6d2af70c92ab 279 celsius = normalCode / Math.Pow(2, 8) + 64;
darienf 20:6d2af70c92ab 280
darienf 20:6d2af70c92ab 281 if (unitCelsius == true)
darienf 20:6d2af70c92ab 282 return celsius;
darienf 20:6d2af70c92ab 283 else
darienf 20:6d2af70c92ab 284 return celsiusToFahrenheit(celsius);
darienf 20:6d2af70c92ab 285
darienf 20:6d2af70c92ab 286 }*/
darienf 20:6d2af70c92ab 287
darienf 20:6d2af70c92ab 288 // short temperatureToHex(double temperature)
darienf 20:6d2af70c92ab 289 // {
darienf 20:6d2af70c92ab 290 // return (short)(temperature * Math.Pow(2, 8));
darienf 20:6d2af70c92ab 291 // }
darienf 20:6d2af70c92ab 292 //
darienf 20:6d2af70c92ab 293 // double celsiusToFahrenheit(double temperature)
darienf 20:6d2af70c92ab 294 // {
darienf 20:6d2af70c92ab 295 // return temperature * 9 / 5 + 32;
darienf 20:6d2af70c92ab 296 // }
darienf 20:6d2af70c92ab 297
darienf 20:6d2af70c92ab 298 }
darienf 20:6d2af70c92ab 299 }