VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "clsDS18S20"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
'@@clsDS18S20
'
'@@clsDS18S20_ClassSummary
'<GROUP clsDS18S20>
'<Title Class Summary>
' clsDS18S20 is a VB6 class that demonstrates how to work with
' a Dallas Semiconductor DS18S20 Temperature Sensor using an
' EDS HA7Net 1-Wire Bus Master.
'
'@@clsDS18S20_HardwareDescription
'<GROUP clsDS18S20>
'<Title Hardware Description>
'
' The DS18B20 temperature sensor is a general purpose 1-Wire temperature
' chip designed into many sensors from Embedded Data Systems,
' including:
' * STPS - Standard temperature probe
' * RTPS - Ruggedized temperature probe
' * STFS - Standard foil temperature sensor
' * T2001S - Wall mount temperature sensor.
' * HMP2001S - Humidity / Temperature sensor.
' * BAR2001S - Pressure / Temperature sensor.
'
' The DS18S20 Digital Thermometer provides 9–bit centigrade
' temperature measurements and has an alarm function with
' nonvolatile user-programmable upper and lower trigger points.
' The DS18S20 communicates over a 1-wire bus that by definition
' requires only one data line (and ground) for communication
' with a central microprocessor. It has an operating
' temperature range of –55°C to +125°C and is accurate to
' ±0.5°C over the range of –10°C to +85°C. In addition, the
' DS18S20 can derive power directly from the data line
' (“parasite power”), eliminating the need for an external
' power supply. Each DS18S20 has a unique 64-bit serial code,
' which allows multiple DS18S20s to function on the same 1–wire
' bus; thus, it is simple to use one microprocessor to control
' many DS18S20s distributed over a large area. Applications
' that can benefit from this feature include HVAC environmental
' controls, temperature monitoring systems inside buildings,
' equipment or machinery, and process monitoring and control
' systems.

'@@clsDS18S20_TypicalUsage
'<GROUP clsDS18S20>
'<Title Typical Usage>
' While several functions are provided, and are each documented
' in the source code, the primary function exposed is:
' * GetTemperature()
'   - Initiates a temperature conversion and then reads the temperature data from
'   the DS18S20's scratchpad.
'
' A typical usage scenario might look like:
'
' <CODE>
'     Dim temperature As Single
'     For Each ds18S20 As clsDS18S20 In ds18S20s
'       txtResults.Text = "Reading Temperature..." & vbCrLf
'       temperature = ds18S20.GetTemperature(True) 'Farenheight
'       txtResults.Text = txtResults.Text & "Current Temp: " & CStr(temperature) & " F" & vbCrLf
'     Next
' </CODE>

Option Explicit
  Const PROTOCOL_CONVERT_T As String = "44"       'DS18S20 Function Command "CONVERT T"
  Const PROTOCOL_READ_SCRATCHPAD As String = "BE" 'DS18S20 Function Command "READ SCRATCHPAD"
  Const PROTOCOL_WRITE_SCRATCHPAD As String = "4E" 'DS18S20 Function Command "WRITE SCRATCHPAD"
  Const PROTOCOL_COPY_SCRATCHPAD As String = "48" 'DS18S20 Function Command "COPY SCRATCHPAD"
  Const PROTOCOL_RECALL_E2 As String = "B8"       'DS18S20 Function Commnad "RECALL E2"
  Const PROTOCOL_READ_POWER_SUPPLY As String = "B4" 'DS18s20 Function Command "READ POWER SUPPLY"

  Const T_CONV As Integer = 0.75 'Maximum time required for DS18S20 temperature conversion.

  Public ROMId As String
  Public BusMaster As clsHA7Net
      
  ' Used to initiate a temperature conversion on the DS18S20.
  '
  ' Since the DS18S20's are very power conservative, they do not
  ' continuously sample the temperature. Normally, the DS18S20
  ' will sit idle consuming almost no power until you
  ' specifically tell it to perform a temperature conversion,
  ' which is what this function accomplishes.
  '
  '
  ' Note that the act of performing a temperature conversion
  ' does not in itself give you the temperature data. Instead,
  ' the sensor will perform a conversion, and store the resulting
  ' data in its internal scratchpad memory. After the conversion
  ' is complete, you have to explicitly read the scratchpad data
  ' from the DS18S20 in order to determine the temperature at the
  ' time it was sampled.
  '
  Public Sub ConvertTemperature(Optional lockId As String)
    Dim startTime As Single
    Dim localLock As Boolean
    If lockId = "" Then
      localLock = True
      lockId = BusMaster.OW_GetLock()
    Else
      localLock = False
    End If
    With BusMaster
      .OW_WriteBlock PROTOCOL_CONVERT_T, Me.ROMId, lockId
      'EDS Bus Masters automatically provide strong pullup for the conversion.
      'Just need to sleep for the duration of the conversion so the sensor is provided
      'adequate power.
      startTime = Timer
      Do While Abs(Timer - startTime) <= T_CONV
        DoEvents
      Loop
      If localLock Then
        BusMaster.OW_ReleaseLock (lockId)
        lockId = ""
      End If
    End With
  End Sub

  ' This command allows the master to read the contents of the
  ' scratchpad. The data transfer starts with the least
  ' significant bit of byte 0 and continues through the
  ' scratchpad until the 9th byte (byte 8 – CRC) is read. The
  ' master may issue a reset to terminate reading at any time if
  ' only part of the scratchpad data is needed.
  '
  ' Returns
  ' DS18S20.ScratchPad - ScratchPad object containing both the
  ' raw scratchpad data, and fields representing the individual
  ' pieces of data parsed from the scratchpad.
  Public Function ReadScratchpad(Optional lockId As String = "") As clsDS18S20Scratchpad
    Dim response As String
    Dim retVal As clsDS18S20Scratchpad
    With BusMaster
      'Write the 'BE' command to the DS18S20, followed by 9 bytes of time slots to read the reply from the DS18S20.
      response = .OW_WriteBlock(PROTOCOL_READ_SCRATCHPAD & "FFFFFFFFFFFFFFFFFF", Me.ROMId, lockId)
    End With
    Set retVal = New clsDS18S20Scratchpad
    retVal.RawScratchPadData = Mid$(response, 3, 18)
    Set ReadScratchpad = retVal
  End Function
  
  ' This command allows the master to write 2 bytes of data to
  ' the DS18S20’s scratchpad. The first byte is written into the
  ' TH register (byte 2 of the scratchpad), and the second byte
  ' is written into the TL register (byte 3 of the scratchpad).
  ' Data must be transmitted least significant bit first. Both
  ' bytes MUST be written before the master issues a reset, or
  ' the data may be corrupted.
  Public Sub WriteScratchpad()
    'TODO - Implement the WriteScratchpad function
  End Sub
  
  ' This command copies the contents of the hardware's scratchpad
  ' TH and TL registers (bytes 2 and 3) to EEPROM. If the device
  ' is being used in parasite power mode, within 10 µs (max)
  ' after this command is issued the master must enable a strong
  ' pullup on the 1-wire bus for at least 10 ms.
  Public Sub CopyScratchpad()
    'TODO - Implement the CopyScratchpad function
  End Sub
  
  ' This command recalls the alarm trigger values (TH and TL)
  ' from EEPROM and places the data in bytes 2 and 3,
  ' respectively, in the scratchpad memory. The master device can
  ' issue read time slots following the Recall E2 command and the
  ' DS18S20 will indicate the status of the recall by
  ' transmitting 0 while the recall is in progress and 1 when the
  ' recall is done. The recall operation happens automatically at
  ' powerup, so valid data is available in the scratchpad as soon
  ' as power is applied to the device.
  Public Sub RecallE2()
   'TODO - Implement the RecallE2 function
  End Sub
  
  ' Used to determine whether the DS18S20 is operating from
  ' parasite power, or whether it is being powered externally.
  '
  ' In some situations the bus master may not know whether the
  ' DS18S20s on the bus are parasite powered or powered by
  ' external supplies. The master needs this information to
  ' determine if the strong bus pullup should be used during
  ' temperature conversions. To get this information, the master
  ' can address the device and issue a Read Power Supply [B4h]
  ' command followed by a “read-time slot”. During the read-time
  ' slot, parasite powered DS18S20s will pull the bus low, and
  ' externally powered DS18S20s will let the bus remain high.
  '
  ' Note
  ' The use of parasite power is not reccommended for
  ' temperatures above 100 C since the DS18S20 may not be able to
  ' sustain communications due to higher leakage currents that
  ' can exist at those temperatures.
  '
  ' Returns
  ' 0 - if sensor is being parasitically powered, 1 - if sensor is
  ' being externally powered
  Public Function ReadPowerSupply() As Byte
    Dim response As String
    With BusMaster
      'Send the 'B4' function command to the DS18S20, followed by 8 read time slots
      response = .OW_WriteBlock(PROTOCOL_READ_POWER_SUPPLY & "FF", Me.ROMId)
    End With
    ReadPowerSupply = CByte("&h" & Mid(response, 3, 2))
  End Function
  
  ' Reads the temperature data stored in the DS18S20's scratchpad
  ' during the last temperature conversion.
  '
  ' Note
  ' This function does not initiate a new temperature conversion.
  '
  ' Parameters
  ' Farenheight :  If True, temperature will be returned in Farehenheight
  '                units, otherwise Centegrade units will be used.
  Public Function GetLastTemperature(Optional ByVal Farenheight As Boolean = False, Optional lockId As String = "") As Single
    'Read scratchpad
    Dim scratchpad As clsDS18S20Scratchpad
    Set scratchpad = Me.ReadScratchpad(lockId)

    'Return the Temperature Data contained in the scratchpad
    GetLastTemperature = scratchpad.calcTemperature(Farenheight)
  End Function
  
  ' Initiates a temperature conversion and then reads the temperature data from
  ' the DS18S20's scratchpad.
  ' This function effectively combines ConvertTemperature() and GetLastTemperature() into
  ' a single call
  Public Function GetTemperature(Optional ByVal Farenheight As Boolean = False, Optional lockId As String = "") As Single
    ConvertTemperature lockId
    GetTemperature = Me.GetLastTemperature(Farenheight, lockId)
  End Function

