Attribute VB_Name = "DataLogger"
'-------------------------------------------------------------------------------
Option Explicit

' BasicX data logger

' This is the number of bytes required by each data sample.
Private Const ElementSize As Integer = 2
'-------------------------------------------------------------------------------
Public Sub Main()

    ' Modify according to processor type.
    Const UploadSwitchPin As Byte = 12   ' BX-24
    '>>Const UploadSwitchPin As Byte = 16   ' BX-35
    Const SwitchClosed As Byte = 0

    ' Define the desired block of EEPROM memory here. Caution -- refer to
    ' the MPP map file to make sure these addresses do not conflict with
    ' code storage.
    Const StartAddress As Long = 4760
    Const EndAddress As Long = 32760

    If (FirstTime) Then

        ' Uncomment these lines to clear the block of EEPROM memory.
        '>>Call ClearMemoryBlock(StartAddress, EndAddress)
        '>>Exit Sub
    End If

    If (GetPin(UploadSwitchPin) = SwitchClosed) Then
        Call UploadData(StartAddress, EndAddress)
    Else
        Call LogData(StartAddress, EndAddress)
    End If

End Sub
'-------------------------------------------------------------------------------
Private Sub LogData( _
    ByVal StartAddress As Long, _
    ByVal EndAddress As Long)

    Const SamplePeriod As Single = 300!  ' 300 s = 5 min.

    Dim CurrentAddress As Long
    Dim ADCdata(1 To 8) As Integer
    Dim Stopwatch As Single
    Dim Channel As Byte

    ' Set the clock to midnight.
    Call PutTime(0, 0, 0!)

    ' Get the EEPROM start address.
    CurrentAddress = StartAddress

    Do
        ' Get the seconds past midnight.
        StopWatch = Timer

        ' If stopwatch is greater or equal to "SamplePeriod" read each ADC
        ' channel and store the data.
        If (StopWatch >= SamplePeriod) Then

            ' Reset the clock to midnight.
            Call PutTime(0, 0, 0!)

            ' Read the ADC pins.
            Call ReadSensors(ADCdata)

            ' Store each of the 8 integers to the EEPROM.
            For Channel = 1 To 8

                Call PutEEPROM(CurrentAddress, ADCdata(Channel), ElementSize)

                ' Increment CurrentEEPROMaddress to the next new location.
                CurrentAddress = CurrentAddress + CLng(ElementSize)
            Next
        End If

        ' Check to see if we are out of storage space.
        If (CurrentAddress > EndAddress) Then

            ' We've run out of EEPROM storage space.
            Exit Sub
        End If

    Loop

End Sub
'-------------------------------------------------------------------------------
Private Sub ReadSensors( _
    ByRef ADCdata() As Integer)

' Reads ADC channels 1 to 8.

    Dim Channel As Byte

    ' Modify according to processor type.
    Const ADCpin As Byte = 13   ' BX-24
    '>>Const ADCpin As Byte = 33   ' BX-35

    For Channel = 1 To 8
        ADCdata(Channel) = GetADC(ADCpin + Channel - 1)
    Next

End Sub
'-------------------------------------------------------------------------------
Private Sub UploadData( _
    ByVal StartAddress As Long, _
    ByVal EndAddress As Long)

' This procedure reads data stored in EEPROM and transmits the data to
' the serial port.

    Dim ADCdata As Integer
    Dim Address As Long
    Dim Channel As Byte

    Channel = 1
    Address = StartAddress

    Do
        ' Read the data.
        Call GetEEPROM(Address, ADCdata, ElementSize)

        Debug.Print CStr(ADCdata);

        Address = Address + CLng(ElementSize)

        Channel = Channel + 1

        If (Channel <= 8) Then

            ' Separate each value with a comma delimiter.
            Debug.Print ", ";
        Else
            ' Insert a <CR><LF> at the end of the channel set.
            Debug.Print
            Channel = 1
        End If
    Loop Until (Address >= EndAddress)

    Debug.Print "EOF"

End Sub
'-------------------------------------------------------------------------------
Private Sub ClearMemoryBlock( _
    ByVal StartAddress As Long, _
    ByVal EndAddress As Long)

' This procedure clears the specified EEPROM memory block.

    Dim Address As Long
    Dim ElementCount As Long
    Dim N As Long
    Dim Temp As Integer

    Debug.Print "Clearing EEPROM block."
    Debug.Print "This may take 3 or 4 minutes."

    Temp = 0
    ElementCount = ( (EndAddress - StartAddress) \ CLng(ElementSize) ) + 1

    Address = StartAddress
    For N = 1 To ElementCount
        Call PutEEPROM(Address, Temp, ElementSize)
        Address = Address + CLng(ElementSize)
    Next

    Debug.Print "Done."

End Sub
'-------------------------------------------------------------------------------
