Attribute VB_Name = "modMeasurements"
Option Explicit

Public CWRefAvail As Boolean
Public lastCWMeasurement As Double
Public lastCWUnits As PWR_UNITS

Public PulseRefAvail As Boolean
Public lastPlsPulseMeas As Double
Public lastPlsPeakMeas As Double
Public lastPlsAvgMeas As Double
Public lastPlsDCMeas As Double
Public lastPlsUnits As PWR_UNITS

Function MeasureCW(addr As Long, valCW As Double, contiuous As Boolean) As Boolean
    Dim rslt As Long, currUnits As PWR_UNITS
    
    If Not contiuous Then Beep
    rslt = LB_InitializeSensor_Addr(addr)                               'initialize the sensor if needed
    rslt = LB_MeasureCW(addr, valCW)                                    'make the measurement
    If rslt < 0 Then                                                    'determine the return value
        MeasureCW = False
    Else
        MeasureCW = True
    End If
    If Not contiuous Then Beep
End Function

Function MeasureCW_PF(addr As Long, valCW As Double, pf As PASS_FAIL_RESULT, contiuous As Boolean) As Boolean
    Dim rslt As Long, currUnits As PWR_UNITS
    
    If Not contiuous Then Beep
    rslt = LB_InitializeSensor_Addr(addr)                               'initialize the sensor if needed
    rslt = LB_MeasureCW_PF(addr, valCW, pf)                             'make the measurement
    If rslt < 0 Then                                                    'determine the return value
        MeasureCW_PF = False
    Else
        MeasureCW_PF = True
    End If
    If Not contiuous Then Beep
End Function

Function MeasurePulse(addr As Long, valPulse As Double, valPeak As Double, valAvg As Double, valDC As Double, contiuous As Boolean) As Boolean
    Dim rslt As Long, currUnits As PWR_UNITS
    
    If Not contiuous Then Beep
    rslt = LB_InitializeSensor_Addr(addr)                               'initialize the sensor if needed
    rslt = LB_MeasurePulse(addr, valPulse, valPeak, valAvg, valDC)
    If rslt < 0 Then                                                    'determine the return value
        MeasurePulse = False
    Else
        MeasurePulse = True
    End If
    If Not contiuous Then Beep
End Function

Function MeasurePulse_PF(addr As Long, _
                valPulse As Double, _
                valPeak As Double, _
                valAvg As Double, _
                valDC As Double, _
                pf As PASS_FAIL_RESULT, _
                contiuous As Boolean) As Boolean
    Dim rslt As Long, currUnits As PWR_UNITS
    
    If Not contiuous Then Beep
    rslt = LB_InitializeSensor_Addr(addr)                               'initialize the sensor if needed
    rslt = LB_MeasurePulse_PF(addr, valPulse, valPeak, valAvg, valDC, pf)
    If rslt < 0 Then                                                    'determine the return value
        MeasurePulse_PF = False
    Else
        MeasurePulse_PF = True
    End If
    If Not contiuous Then Beep
End Function

Public Function PadLeft(tmpStr As String, lngWidth As Long) As String
    Dim lclStr As String
    
    lclStr = Trim(tmpStr)
    If (lngWidth - 1) > Len(lclStr) Then
        PadLeft = String(lngWidth - Len(lclStr), " ") & lclStr
    Else
        PadLeft = " " & Left(tmpStr, lngWidth - 1)
    End If
End Function

Public Function PadRight(tmpStr As String, lngWidth As Long) As String
    Dim lclStr As String
    
    lclStr = Trim(tmpStr)
    If (lngWidth - 1) > Len(lclStr) Then
        PadRight = lclStr & String(lngWidth - Len(lclStr), " ")
    Else
        PadRight = " " & Left(tmpStr, lngWidth - 1)
    End If
End Function

Public Function ByteAryToString(ba() As Byte) As String
    Dim i As Long
    
    ByteAryToString = vbNullString
    For i = LBound(ba) To UBound(ba)
        ByteAryToString = ByteAryToString & Chr(ba(i))
    Next i
End Function

Public Sub SDByteToSDString(m_sd() As SensorDescrption, sd() As SDByte)
    Dim i As Long
    
    For i = LBound(sd) To UBound(sd)
        m_sd(i).DeviceAddress = sd(i).DeviceAddress
        m_sd(i).DeviceIndex = sd(i).DeviceIndex
        m_sd(i).SerialNumber = ByteAryToString(sd(i).SerialNumber)
    Next i
End Sub
