Darien Figueroa / Mbed 2 deprecated repo3

Dependencies:   mbed MAX14720 MAX30205 USBDevice

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers RegisterExportPresenter.cs Source File

RegisterExportPresenter.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 System.Globalization;
00042 
00043 using HealthSensorPlatform.View;
00044 using Maxim.CustomControls;
00045 using Maxim.Utility;
00046 
00047 namespace HealthSensorPlatform.Presenter
00048 {
00049     class RegisterExportPresenter
00050     {
00051         IRegisterExportView exportView;
00052         IFormView hspMainForm;
00053         List<RegisterView> registerModels;
00054 
00055         Dictionary<string, RegisterView> registerModelDict;
00056 
00057         FileLog registerDataFile;
00058         FileCsvReader fileReader;
00059 
00060         public RegisterExportPresenter(IFormView hspMainForm, IRegisterExportView exportView, List<RegisterView> registerModels)
00061         {
00062             this.exportView = exportView;
00063             this.registerModels = registerModels;
00064             this.hspMainForm = hspMainForm;
00065 
00066             exportView.SelectionComplete += new EventHandler<CompleteEventArgs>(OnSelectionComplete);
00067 
00068             hspMainForm.RegisterExport += new EventHandler<EventArgs>(OnRegisterExport);
00069             hspMainForm.RegisterImport += new EventHandler<EventArgs>(OnRegisterImport);
00070             hspMainForm.Connected += new EventHandler<EventArgs>(OnConnected);
00071 
00072             registerDataFile = new FileLog();
00073             fileReader = new FileCsvReader();
00074 
00075             registerModelDict = new Dictionary<string, RegisterView>();
00076 
00077             foreach (RegisterView rV in registerModels)
00078             {
00079                 registerModelDict.Add(rV.DeviceName, rV);
00080                 if (rV.DeviceName == "MAX30001")
00081                     registerModelDict.Add("MAX30003", rV); // add a second reference for MAX30003
00082             }
00083 
00084         }
00085 
00086         public List<string> GetDeviceNames()
00087         {
00088             List<string> devices = new List<string>();
00089 
00090             foreach(RegisterView device in registerModels)
00091             {
00092                 devices.Add(device.DeviceName);
00093             }
00094 
00095             return devices;
00096         }
00097 
00098         private void OnConnected(object sender, EventArgs e)
00099         {
00100             exportView.Devices = GetDeviceNames();
00101             exportView.SelectedDevices = new List<string>();
00102         }
00103 
00104         private void OnRegisterExport(object sender, EventArgs e)
00105         {
00106             exportView.Show();            
00107         }
00108 
00109         private void OnRegisterImport(object sender, EventArgs e)
00110         {
00111             char[] hexSymbol = new char[] { 'h', 'H' };
00112             bool ok = true;
00113 
00114             if (fileReader.OpenCSVFile())
00115             {
00116                     while (fileReader.Peek() > 0)
00117                     {
00118                         string[] elements = fileReader.ReadLineElements();
00119                         string registerAddress;
00120                         string registerValue;
00121 
00122                         // End of data
00123                         if (elements[0].Contains('%') || elements[0] == String.Empty)
00124                             continue;
00125 
00126                         if (ok = registerModelDict.ContainsKey(elements[0]))
00127                         {
00128                             RegisterView view = registerModelDict[elements[0]]; // Look up in dictionary
00129 
00130                             registerAddress = elements[1].TrimEnd(hexSymbol);
00131                             registerValue = elements[3].TrimEnd(hexSymbol);
00132 
00133                             RPCSupport.DeviceSupport.RegisterInfo reg = view.RegisterAddress(Int32.Parse(registerAddress, NumberStyles.HexNumber));
00134 
00135                             if (reg.type == RPCSupport.DeviceSupport.RegisterInfo.RegisterType.None)
00136                                 view.WriteRegister(reg, Int32.Parse(registerValue, NumberStyles.HexNumber));
00137                         }
00138                         else
00139                             break;
00140                 }
00141 
00142                 fileReader.Close();
00143 
00144                 if (ok)
00145                 {
00146                     hspMainForm.UpdateRegisters();
00147                     hspMainForm.MessageBoxShow("Register load complete", "Register Import/Export");
00148                 }
00149                 else
00150                 {
00151                     hspMainForm.MessageBoxShow("Register load failed, file format incorrect", "Register Import/Export");
00152                 }
00153             }
00154         }
00155 
00156         private void OnSelectionComplete(object sender, CompleteEventArgs e)
00157         {
00158             List<string> devices = exportView.SelectedDevices;
00159 
00160             if (e.Complete == true) // Successful device selection
00161             {
00162                 if (registerDataFile.SelectCSVFile("hsp-register_data")) // Successful file selection
00163                 {
00164                     foreach (string device in devices) // Match string with RegisterView
00165                     {
00166                         RegisterView regView = registerModelDict[device]; // Find register view corresponding with device name
00167 
00168                         regView.ReadAll(); // Update register values
00169                         registerDataFile.WriteLine("% Device, Address (hex), Name, Value (hex)");
00170                         registerDataFile.WriteLine(regView.RegistersToString()); // Do the Export
00171                         registerDataFile.WriteLine("%"); // End of register data
00172                     }
00173 
00174                     registerDataFile.Close();
00175 
00176                     hspMainForm.MessageBoxShow("Register save complete", "Register Import/Export");
00177                 }
00178             }
00179 
00180         }
00181 
00182     }
00183 }