VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "clsDS18S20Scratchpad"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
  ' Class for working with the DS18S20's 9 byte scratchpad.
  '
  ' HARDWARE DESCRIPTION:
  '
  ' The scratchpad memory contains the 2-byte temperature
  ' register that stores the digital output from the temperature
  ' sensor. In addition, the scratchpad provides access to the
  ' 1-byte upper and lower alarm trigger registers (TH and TL).
  ' The TH and TL registers are nonvolatile (EEPROM), so they
  ' will retain data when the device is powered down.
  '
  'Note:
  ' Requires the rawbytes read from the physical device to be set
  ' in rawBytes
  
  Private ScratchPadBytes(9) As Byte      'Scratchpad bytes
  Private RawBytes As String 'Raw, unparsed bytes read from scratchpad
  
  Public Property Let RawScratchPadData(ByVal RawScratchPadData As String)
    RawBytes = RawScratchPadData
    parseRawBytes
  End Property
  Public Property Get RawScratchPadData() As String
    RawScratchPadData = RawBytes
  End Property
  
  'Description:
  ' ValidateCRC() will validate the CRC of the raw bytes stored in the scratchpad
  '
  'Returns:
  ' True if valid
  ' False otherwise
  Public Function ValidateCRC() As Boolean
    'TODO - Implement DS18x20.ScratchPad.ValidateCRC()
    ValidateCRC = True
  End Function
  
  'Parses the raw bytes and populates the
  'public byte array
  Private Sub parseRawBytes()
    ScratchPadBytes(0) = CByte("&H" & Mid(RawBytes, 1, 2))
    ScratchPadBytes(1) = CByte("&H" & Mid(RawBytes, 3, 2))
    ScratchPadBytes(2) = CByte("&H" & Mid(RawBytes, 5, 2))
    ScratchPadBytes(3) = CByte("&H" & Mid(RawBytes, 7, 2))
    ScratchPadBytes(4) = CByte("&H" & Mid(RawBytes, 9, 2))
    ScratchPadBytes(5) = CByte("&H" & Mid(RawBytes, 11, 2))
    ScratchPadBytes(6) = CByte("&H" & Mid(RawBytes, 13, 2))
    ScratchPadBytes(7) = CByte("&H" & Mid(RawBytes, 15, 2))
    ScratchPadBytes(8) = CByte("&H" & Mid(RawBytes, 17, 2))
  End Sub

' Description:
' Calculates the temperature from the existing scratchpad data.
'
' The DS18S20 output data is calibrated in degrees centigrade;
' for Fahrenheit applications, a lookup table or conversion
' routine must be used. The temperature sensor output has 9-bit
' resolution, which corresponds to 0.5°C steps. The temperature
' data is stored as a 16-bit sign-extended two’s complement
' number in the temperature register (see Figure 2). The sign
' bits (S) indicate if the temperature is positive or negative:
' for positive numbers S = 0 and for negative numbers S = 1.
' Table 2 gives examples of digital output data and the
' corresponding temperature reading. Resolutions greater than 9
' bits can be calculated using the data from the temperature,
' COUNT REMAIN and COUNT PER °C registers in the scratchpad.
' \Note that the COUNT PER °C register is hard-wired to 16
' (10h). After reading the scratchpad, the TEMP_READ value is
' \obtained by truncating the 0.5°C bit (bit 0) from the
' temperature data. The extended resolution temperature can
' then be calculated using the following equation:
'
' TEMPERATURE = TEMP_READ - 0.25 + ((COUNT_PER_C -
' COUNT_REMAIN) / COUNT_PER_C)
'
' Returns:
' Temperature in either Centegrade or Farenheight.
'
' Parameters:
' Farenheight :  Flag to indicate if the temperature is to be returned
'                in Farenheight. If not True, then the returned value
'                will be in Centegrade.
'
' See Also:
' DS18S20.ConvertTemperature(), DS18S20.GetLastTemperature(),
' DS18S20.GetTemperature()
Public Function calcTemperature(Optional ByVal Farenheight As Boolean = False) As Single
  Dim celcius As Single
  Dim tempRead As Integer
  Dim countPerC As Byte, countRemain As Byte
  Dim temperature_LSB As Byte, temperature_MSB As Byte

  temperature_LSB = ScratchPadBytes(0)
  temperature_MSB = ScratchPadBytes(1)
  countPerC = ScratchPadBytes(7)
  countRemain = ScratchPadBytes(6)

  tempRead = temperature_LSB
  If temperature_MSB <> 0 Then
    tempRead = tempRead * -1
  End If
  celcius = ((tempRead - 0.25@ + CDbl((CInt(countPerC) - CInt(countRemain))) / countPerC) / 2@)

  If Farenheight Then
    calcTemperature = (celcius * 1.8@) + 32@
  Else
    calcTemperature = celcius
  End If
End Function
