Darien Figueroa / Mbed 2 deprecated repo3

Dependencies:   mbed MAX14720 MAX30205 USBDevice

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers OpticalFileLogPresenter.cs Source File

OpticalFileLogPresenter.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 
00041 using HealthSensorPlatform.View;
00042 using HealthSensorPlatform.CustomControls;
00043 
00044 using RPCSupport.Streaming;
00045 
00046 namespace HealthSensorPlatform.Presenter
00047 {
00048     public class OpticalFileLogPresenter
00049     {
00050         IRawFileLogView optical;
00051         IRawFileLogView accel;
00052         HspForm mainForm;
00053         OpticalView opticalView;
00054 
00055         string logFileDirectory = null;
00056 
00057         int ppgCount = 0;
00058         int accelCount = 0;
00059 
00060         public OpticalFileLogPresenter(RPCSupport.RPCClient rpcClient, HspForm mainForm, OpticalView opticalView, IRawFileLogView optical, IRawFileLogView accel)
00061         {
00062             this.optical = optical;
00063             this.accel = accel;
00064             this.mainForm = mainForm;
00065             this.opticalView = opticalView;
00066 
00067             rpcClient.streaming.PartialArrayIntAvailable += new EventHandler<PartialArrayIntAvailableEventArgs>(OnStreamData);
00068             mainForm.OpticalFileLogEnable += new EventHandler<EnableEventArgs>(OnFileLogEnable);
00069             //opticalView.StreamingStartStop += new OpticalView.StreamingStartStopEventHandler(OnStreamingStartStop);
00070             opticalView.StreamingStartStop += new EventHandler<StreamingStartStopEventArgs>(OnStreamingStartStop);
00071         }
00072 
00073         public void ProcessOptical(int[] red, int[] ir, int[] green)
00074         {
00075             double[] time = new double[red.Length];
00076 
00077             for (int i = 0; i < time.Length; i++ )
00078             {
00079                 time[i] = ppgCount / (double)opticalView.OpticalSampleRate;
00080                 ppgCount++;
00081             }
00082 
00083             optical.DisplayPpg(time, new int[][] { red, ir, green });
00084         }
00085 
00086         public void ProcessXYZ(int[] x, int[] y, int[] z)
00087         {
00088             int[] decimalFormatX;
00089             int[] decimalFormatY;
00090             int[] decimalFormatZ;
00091             double[] time = new double[x.Length];
00092 
00093             decimalFormatX = ConvertXYZBinary(x);
00094             decimalFormatY = ConvertXYZBinary(y);
00095             decimalFormatZ = ConvertXYZBinary(z);
00096 
00097             for (int i = 0; i < time.Length; i++ )
00098             {
00099                 time[i] = accelCount / (double)opticalView.AccelSampleRate;
00100                 accelCount++;
00101             }
00102 
00103             accel.DisplayXYZ(time, new int[][] { decimalFormatX, decimalFormatY, decimalFormatZ });
00104         }
00105 
00106         public static int[] ConvertXYZBinary(int[] data)
00107         {
00108             int[] decimalFormat = new int[data.Length];
00109 
00110             for (int i = 0; i < data.Length; i++)
00111             {
00112                 if (data[i] > 0x8000)
00113                     decimalFormat[i] = data[i] - 0x10000;
00114                 else
00115                     decimalFormat[i] = data[i];
00116             }
00117 
00118             return decimalFormat;
00119         }
00120 
00121         public void OnStreamData(object sender, PartialArrayIntAvailableEventArgs e)
00122         {
00123             if ((e.reportID & 0xF0) == PartialArrayIntAvailableEventArgs.PACKET_MAX30101)
00124             {
00125                 if (optical.Enable)
00126                     ProcessOptical(e.array1, e.array2, e.array3);
00127             }
00128             if (e.reportID == PartialArrayIntAvailableEventArgs.PACKET_LIS2DH)
00129             {
00130                 if (accel.Enable)
00131                     ProcessXYZ(e.array1, e.array2, e.array3);
00132             }
00133         }
00134 
00135         void OnFileLogEnable(object sender, EnableEventArgs e)
00136         {
00137             optical = new RawFileLogView();
00138             accel = new RawFileLogView();
00139 
00140             if (logFileDirectory != null)
00141             {
00142                 optical.FileDirectory = logFileDirectory;
00143                 //accel.FileDirectory = logFileDirectory;
00144             }
00145 
00146             if (e.Enable && e.Stream == StreamType.Optical && optical.SelectCSVFile("hsp-optical"))
00147             {
00148                 accel.FileDirectory = optical.FileDirectory;
00149                 if (accel.SelectCSVFile("hsp-accel"))
00150                 {
00151                     mainForm.OpticalLogFileItem(true);
00152                     optical.Enable = true;
00153                     accel.Enable = true;
00154                     logFileDirectory = optical.FileDirectory;
00155                 }
00156             }
00157             else
00158             {
00159                 mainForm.OpticalLogFileItem(false);
00160                 optical.Enable = false;
00161                 accel.Enable = false;
00162             }
00163         }
00164 
00165         void OnStreamingStartStop(object sender, StreamingStartStopEventArgs e)
00166         {
00167             if (e.state == true)
00168             {
00169                 View.FileLogView.FileLogHeader fileLogHeader = new View.FileLogView.FileLogHeader();
00170                 ppgCount = 0;
00171                 accelCount = 0;
00172 
00173                 if (optical.Enable == true)
00174                 {
00175                     optical.WriteLine(opticalView.SettingsString());
00176                     optical.WriteLine(fileLogHeader.Optical);
00177                 }
00178                 if (accel.Enable == true)
00179                 {
00180                     accel.WriteLine(opticalView.AccelSettingString());
00181                     accel.WriteLine(fileLogHeader.Accelerometer);
00182 
00183                     // Try to have "0" be the same on both files
00184                     optical.StreamStartStop();
00185                     accel.StreamStartStop(); 
00186                 }
00187                 
00188             }
00189             else
00190             {
00191                 optical.Enable = false;
00192                 accel.Enable = false;
00193 
00194                 mainForm.LogFileItem(StreamType.Optical, false);
00195             }
00196         }
00197     }
00198 }