Darien Figueroa / Mbed 2 deprecated repo3

Dependencies:   mbed MAX14720 MAX30205 USBDevice

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers support.cs Source File

support.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.Windows.Forms;
00038 
00039 //------------------------------------------------------------------------------------------
00040 // OS24EVK-59 split into HeartRateApp EXE and MAX30101 DLL.
00041 // Moved all MAX30101 DLL classes into namespace Maxim.MAX30101GUI
00042 // Moved all HeartRateApp GUI classes into namespace Maxim.MAX30101
00043 // OS24EVK-59 Create separate project that builds Maxim.MAX30101GUI DLL library
00044 
00045 // OS24EVK-59 moved class support into namespace Maxim.MAX30101 instead of namespace HeartRateApp
00046 namespace RPCSupport
00047 {
00048     internal static class support
00049     {
00050         public const int DEFAULT_VENDOR_ID = 0x0B6A; // update USB VID PID from default Vid_16C0&Pid_0830
00051         public const int DEFAULT_PRODUCT_ID = 0x1366; // MAX30100EVKit USB Vid_0B6A&Pid_1364; MAX30101EVKit USB Vid_0B6A&Pid_1365
00052         public const byte DEFAULT_I2C_ADDRESS = 0xAE;   // 8-bit address (write address)
00053 
00054         public static Control getControlFromName(Control Page, string controlName)
00055         {
00056             // getControlFromName was split into two functions: 1) this function called by other subs so that "found" does not need to be declared and passed
00057             // and 2) getControlFromNameRecurse which uses the "found" parameter 
00058             Boolean found = false;
00059 
00060             try
00061             {
00062                 return getControlFromNameRecurse(Page, controlName, ref found);
00063             }
00064             catch (Exception ex)
00065             {
00066                 throw new Exception(ex.Message + Environment.NewLine + new System.Diagnostics.StackFrame().GetMethod().Name);
00067             }
00068         }
00069 
00070         private static Control getControlFromNameRecurse(Control Page, string controlName, ref Boolean found)
00071         {
00072             Control returnVal = null;
00073 
00074             try
00075             {
00076                 foreach (Control ctrl in Page.Controls)
00077                 {
00078                     if (!found)
00079                     {// found is passed by reference; hence when a recursive call finds the control, found will always be True for all calls under the initial call
00080                         if (String.Compare(ctrl.Name, controlName, true) == 0)
00081                         {
00082                             found = true;
00083                             returnVal = ctrl;
00084                         }
00085                         else if (ctrl.Controls.Count > 0)
00086                         {
00087                             returnVal = getControlFromNameRecurse(ctrl, controlName, ref found);
00088                         }
00089                     }
00090                 }
00091                 return returnVal;
00092             }
00093             catch (Exception ex)
00094             {
00095                 throw new Exception(ex.Message + Environment.NewLine + new System.Diagnostics.StackFrame().GetMethod().Name);
00096             }
00097         }
00098 
00099         public static byte getControlIndex(Control sender)
00100         {
00101             //This function finds the 'index' of a control 'array'. Since there are no control arrays in .Net,
00102             //we name all the controls consistently and suffix them with an integer. This integer is the 'index' and is returned by this function.
00103             int iterator;
00104             int i = 0;
00105             String name = sender.Name;
00106 
00107             try
00108             {
00109                 iterator = name.Length;
00110                 while (char.IsNumber(name, iterator - 1))
00111                 {
00112                     i+=1;
00113                     iterator-=1;
00114                     if (iterator == 0)
00115                         break;
00116                 }
00117                 if (i == 0)
00118                     throw new Exception("Not a control array");
00119                 return Convert.ToByte(name.Substring(name.Length - i, i));
00120             }
00121             catch (Exception ex)
00122             {
00123                 throw new Exception(ex.Message + Environment.NewLine + new System.Diagnostics.StackFrame().GetMethod().Name);
00124             }
00125         }
00126 
00127         //http://www.vbforums.com/showthread.php?t=546633
00128         //use this to make a delay
00129         public static void Pause(int msDelay)
00130         {
00131             DateTime Timeout;
00132             DateTime PrevTimer;
00133 
00134             try
00135             {
00136                 PrevTimer = System.DateTime.Now;
00137                 Timeout = PrevTimer.AddMilliseconds(msDelay);
00138                 while (PrevTimer < Timeout)
00139                 {
00140                     FileIO.Sleep(4); //-- Timer is only updated every 1/64 sec = 15.625 millisecs.
00141                     Application.DoEvents(); //DoEvents seems to be not recommended; is there a better way to do this?
00142                     //DoEvents really does cause trouble. Example: pause on device change causes closeHandles and openHandles NOT to run in order!
00143                     if (System.DateTime.Now < PrevTimer)
00144                         Timeout = Timeout.AddHours(-24); //-- pass midnight
00145                     PrevTimer = System.DateTime.Now;
00146                 }
00147             }
00148             catch (Exception ex)
00149             {
00150                 throw new Exception(ex.Message + Environment.NewLine + new System.Diagnostics.StackFrame().GetMethod().Name);
00151             }
00152         }
00153 
00154         public const int FORMAT_MESSAGE_FROM_SYSTEM = 0x1000;
00155         /// <summary>
00156         /// Get text that describes the result of an API call.
00157         /// </summary>
00158         /// 
00159         /// <param name="functionName"> the name of the API function. </param>
00160         /// 
00161         /// <returns>
00162         /// The text.
00163         /// </returns>
00164         /// 
00165         public static string ResultOfAPICall(string functionName) 
00166         {
00167             int bytes;
00168             int resultCode;
00169             string resultString = "";
00170             int length = 1000;
00171 
00172             try
00173             {
00174                 resultString = new String((char)0, length); // create space for string with all elements set to NULL
00175 
00176                 // Returns the result code for the last API call.
00177 
00178                 resultCode = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
00179 
00180                 // Get the result message that corresponds to the code.
00181 
00182                 bytes = FileIO.FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, resultCode, 0, resultString, length - 1, 0);
00183 
00184                 // Subtract all characters from the message from the CR LF and NULLs to the end
00185                 // Important to remove all NULLs since VB doesn't use NULL to demark end of string, and thus when concatenating strings, the NULLs are troublesome
00186 
00187                 if (bytes > 2)
00188                     resultString = resultString.Remove(bytes - 2, length - bytes + 2);
00189                 
00190                 // Create the string to return.
00191 
00192                 resultString = functionName + Environment.NewLine + "Result = " + resultString;
00193 
00194                 return resultString;
00195             }
00196             catch (Exception ex)
00197             {
00198                 throw new Exception(ex.Message + Environment.NewLine + new System.Diagnostics.StackFrame().GetMethod().Name);
00199             }
00200         }
00201     }
00202 }