#Region  Project Attributes
    #ApplicationLabel: b4aHeartRateMonitor_2
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region
#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim nativeMe As JavaObject
    Private rp As RuntimePermissions
    Dim t As Timer
End Sub
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Private nqrcrv As HeartRateMonitor2
    
    Private Button1 As Button
    Private torchOn As Boolean = False
    
    Private Button2 As Button
    Private Button3 As Button
    
    Private lc1 As RealTimeLineChart
    Dim bufsize As Int = 100                                 'set the data buffer size here i.e how many values to display in the plot area
    Dim xlab(bufsize) As String
    
    Dim a As Int = 0
    Dim xlabeltrack As Int = 0
    Dim scanneractive As Boolean = False
    
End Sub
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("main")
    
    lc1.Visible = False
    nqrcrv.Visible = False
    t.Initialize("t", 1000)
    
    nativeMe.InitializeContext
    nqrcrv.AutofocusInterval = 500
    lc1.GraphTitleColor = Colors.Magenta
    lc1.GraphTitleSkewX = -0.15
    lc1.GraphTitleBold = True
    lc1.GraphTitleTextSize = 15.0
    lc1.GraphPlotAreaBackgroundColor = Colors.Black          'this will paint the plotting area DrakGray regardless of what GraphBackgroundColor has been set to
    lc1.GraphBackgroundColor = Colors.Transparent               'this will paint everything within the outer frame to be white
    lc1.GraphFrameColor = Colors.Red                            'this adjusts only the outer frame color
    lc1.GraphFrameWidth = 2.0
    lc1.GraphBufferSize = bufsize
    lc1.GraphTitle = "Heart Rate Monitor"
    lc1.DomainLabelColor = Colors.Black
    lc1.DomainLabelTextSize = 25
    lc1.DomianLabel = "Time in Seconds"
    
    lc1.YaxisRangeMode = lc1.YaxisMode_FIXED               
    lc1.YaxisRange(40.0, 120.0)
    lc1.YaxisDivisions = 10
    lc1.YaxisLabelTicks = 1
    lc1.YaxisShowZero = True
    lc1.YaxisTitleTextSize = 30
    lc1.YaxisTitleColor = Colors.Black
    lc1.YaxisGridLineColor = Colors.Yellow
    lc1.YaxisLabelTextSize = 25
    lc1.YaxisLabelColor = Colors.Red
    lc1.YaxisLabelOrientation = 0
    lc1.YaxisTitle = "BPM"
    
    '************************ If you comment this code then the x-axis labels will be the index value of the buffer
    For i = 0 To bufsize - 1
        xlab(i) = "" & i
    Next
    lc1.XAxisLabels = xlab
    '*************************************************************************************************************
        
    lc1.XaxisLabelTextSize = 20
    lc1.XaxisLabelTextColor = Colors.Green
    lc1.XaxisGridLineColor = Colors.Yellow
    lc1.XaxisLabelOrientation = 0
    lc1.XaxisDivisions = 30
    lc1.XaxisLabelTicks = 1
    
    
    lc1.LegendTextSize = 15.0
    lc1.LegendTextColor = Colors.Magenta
    lc1.LegendBackgroundColor = Colors.Transparent
    
    'setup for Line 1
    lc1.Line_1_LineColor = Colors.Red
    lc1.Line_1_LineWidth = 2.0
    lc1.Line_1_DrawDash = False
    lc1.Line_1_LegendText = "Heart Rate"
    lc1.Line_1_DrawCubic = True
    lc1.NumberOfLineCharts = 1
    lc1.DrawTheGraphs
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
    
    If scanneractive = True Then                 'scanner active? If yes, stop it
        torchOn = False
        nqrcrv.stopCamera
        t.Enabled = False
        scanneractive = False
    End If   
End Sub
Sub Button1_Click
    
    
    If nqrcrv.Visible = True Then                'switch on/off the falsh/torch (camera must have been started for flash/torch to work)
        If torchOn = False Then
            nqrcrv.TorchEnabled = True
            torchOn = True
        Else
            nqrcrv.TorchEnabled = False
            torchOn = False
            
        End If   
    End If
    
End Sub
Sub Button2_Click
    Dim Result As Boolean = True
    If Not(rp.Check(rp.PERMISSION_CAMERA)) Then
        rp.CheckAndRequest(rp.PERMISSION_CAMERA)
        Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    End If
    If Result Then
        If scanneractive = False Then           'scanner active? If not, start scanner
            t.Enabled = True                    'start the timer           
            nqrcrv.Visible = True               'make the scanner visible
            lc1.Visible = True                  'make the line chart visible
            nqrcrv.setBackCamera                'use the back camera
            nqrcrv.AutofocusInterval = 300000
            nqrcrv.startCamera                  'start the camera to display the preview
            scanneractive = True                'scanner has been started
            lc1.DrawTheGraphs                   'tell graphing lib to start drawing when it gets data
            lc1.START                           'start the graph
        End If   
    End If
End Sub
Sub Button3_Click
    
    If scanneractive = True Then              'is scanner active?
        t.Enabled = False
        nqrcrv.Visible = False                'hide the scanner view when we stop
        nqrcrv.stopCamera                     'stop the camera
        lc1.Visible = False                   'hide the line chart
        scanneractive = False                 'set boolean variable to false - scanner is not active as we have stopped the scanner
        lc1.ClearAllData                       'clear the data from the graph
        a = 0                                  'set the counter (of the number of data points) to zero
    End If   
    
End Sub
Sub t_tick
    Dim hr As Int = 0
    hr = nqrcrv.HeartRate
    Log("Heart Rate = " & hr)
    lc1.addData(hr, 0, 0, 0, 0)     'get un updated heart rate every second
    nativeMe.RunMethod("playTone", Null)          'play a tone every tick of the timer
    a = a + 1
    If a = bufsize + 1 Then a = 0                 
  
    If xlabeltrack = bufsize + 1 Then
        xlabeltrack = 0
        lc1.ClearAllData                        'THIS WILL CLEAR THE DATA
        a = 0
        For i = 0 To bufsize - 1
            xlab(i) = "" & i
        Next
        lc1.XAxisLabels = xlab
        lc1.DrawTheGraphs
        lc1.START
    End If
 
    '************************ If you comment this code then the x-axis labels will be the index of the buffer
'    xlabeltrack = xlabeltrack + 1
'    If xlabeltrack > bufsize Then
'        xlab = shiftarray(xlab)
'        xlab(bufsize - 1) = "" & xlabeltrack
'        lc1.XAxisLabels = xlab
'    End If
'    xlab(bufsize - 1) = "" & xlabeltrack
    '**************************************************************************************************************
    
End Sub
Sub shiftarray (oldarray() As String) As String()
    
    Dim newarray(bufsize) As String
    For i = 0 To bufsize - 2
        newarray(i) = oldarray(i + 1)
    Next
    Return newarray
    
End Sub
    
#if Java
import android.media.ToneGenerator;
import android.media.AudioManager;
  public void playTone() {
      final ToneGenerator tg = new ToneGenerator(AudioManager.STREAM_NOTIFICATION, 100);
      tg.startTone(ToneGenerator.TONE_PROP_BEEP);
  }     
#End If