Share My Creation Logic Analyzer

Logic analyzer 4 channels for frequencies up to 20KHz. Arduino Uno or Nano use the B4A app to display the logic. Waveforms aren't accurate for frequencies over 20KHz due to the low speed of the microcontroller.
The code sends 64 bytes of input sampling to form one screen, screen sampling is taken every 1 second. The Android device sends 1 byte that relates to frequency selected in the app..
Arduino inputs are at pins 8,9,10,11 when channel 1 is at pin 8. The Arduino is connected to the Android device via OTG cable.
B4X:
Sub Process_Globals
    Private usbserial As felUsbSerial
    Private manager As UsbManager
End Sub

Sub Globals
    Private usbserial As felUsbSerial
    Private manager As UsbManager
    Private btnConnect As Button
    Private pnlGraph As Panel
    Private cvsActivity, cvsGraph As Canvas
    Private Label1 As Label
    Private py(4) As Byte
    Private bar1 As SeekBar
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        manager.Initialize
    End If
   
    Activity.LoadLayout("main")        ' load the layout
    cvsActivity.Initialize(Activity)    ' initialize the Canvas for the activity
    cvsGraph.Initialize(pnlGraph)    ' initialize the Canvas for the panel

End Sub

Sub btnConnect_Click
    If manager.GetDevices.Length = 0 Then
        Label1.Text = "No usb device."
    Else
        Dim device As UsbDevice = manager.GetDevices(0) 'the device
        If manager.HasPermission(device) = False Then
            Label1.Text = "Click Connect"
            ToastMessageShow("Please allow connection and click again.", True)
            manager.RequestPermission(device)
        Else
            usbserial.Initialize("serial", device, -1)
            usbserial.BaudRate = 115200
            usbserial.DataBits = usbserial.DATA_BITS_8
            usbserial.StartReading
            Label1.Text = "Connected"
        End If
    End If
End Sub

Private Sub serial_DataAvailable (Buffer() As Byte)        '60 bytes received
    Dim nByte As Byte
    Dim sweep(1), ny(4) As Byte
    Dim x As Int  
   
    Private rect1 As Rect
    rect1.Initialize(0, 0, 114%y, 90%y)
    cvsGraph.DrawRect(rect1, Colors.White, True, 3dip)        'refresh screen
   
    'curve made out of 63 lines
    If Buffer.Length > 59 Then
        For i = 0 To 63
            x = i*2%y

            nByte=Buffer(i)
            ny(0) =  Bit.And(nByte,1)    'CH 1
            If ny(0) <> py(0) Then    'if level changed
                cvsGraph.DrawLine(x, 5%y, x, 20%y, Colors.Magenta, 1dip)
                py(0) = ny(0)
            End If
            If ny(0)=1 Then    'level
                cvsGraph.DrawLine(x, 5%y, x+2%y, 5%y, Colors.Magenta, 1dip)
            Else
                cvsGraph.DrawLine(x, 20%y, x+2%y, 20%y, Colors.Magenta, 1dip)
            End If

            ny(1) =  Bit.And(nByte,2)
            If ny(1) <> py(1) Then
                cvsGraph.DrawLine(x, 25%y, x, 40%y, Colors.Blue, 1dip)
                py(1) = ny(1)
            End If
            If ny(1)=2 Then
                cvsGraph.DrawLine(x, 25%y, x+2%y, 25%y, Colors.Blue, 1dip)
            Else
                cvsGraph.DrawLine(x, 40%y, x+2%y, 40%y, Colors.Blue, 1dip)
            End If
           
            ny(2) =  Bit.And(nByte,4)
            If ny(2) <> py(2) Then
                cvsGraph.DrawLine(x, 45%y, x, 60%y, Colors.Red, 1dip)
                py(2) = ny(2)
            End If
            If ny(2)=4 Then
                cvsGraph.DrawLine(x, 45%y, x+2%y, 45%y, Colors.Red, 1dip)
            Else
                cvsGraph.DrawLine(x, 60%y, x+2%y, 60%y, Colors.Red, 1dip)
            End If

            ny(3) =  Bit.And(nByte,8)
            If ny(3) <> py(3) Then
                cvsGraph.DrawLine(x, 65%y, x, 80%y, Colors.Green, 1dip)
                py(3) = ny(3)
            End If
            If ny(3)=8 Then
                cvsGraph.DrawLine(x, 65%y, x+2%y, 65%y, Colors.Green, 1dip)
            Else
                cvsGraph.DrawLine(x, 80%y, x+2%y, 80%y, Colors.Green, 1dip)
            End If

            pnlGraph.Invalidate
        Next
       
    End If
    Sleep(200)
    sweep(0)=bar1.Value
    usbserial.Write(sweep)
    Sleep(200)

   
End Sub
B4X:
Sub Process_Globals
    Private Serial1 As Serial
    Private AStream As AsyncStreams
    Private in8 As Pin
    Private in9 As Pin
    Private in10 As Pin
    Private in11 As Pin
    Private dly As Byte
    Private Timer1 As Timer
   
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    AStream.Initialize(Serial1.Stream, "Astream_NewData", "Astream_Error")
    in8.Initialize(8, in8.MODE_INPUT)
    in9.Initialize(9, in9.MODE_INPUT)
    in10.Initialize(10, in10.MODE_INPUT)
    in11.Initialize(11, in11.MODE_INPUT)

    Timer1.Initialize("Timer1_Tick", 1000)
    Timer1.Enabled = True

End Sub

Private Sub Timer1_Tick
    Private smp(64), in As Byte
   
    Do While in8.DigitalRead=False
    Loop
    Do While in8.DigitalRead=True
    Loop
   
    For i=0 To 63
        in=0
        If in8.DigitalRead=True Then in=in+1
        If in9.DigitalRead=True Then in=in+2
        If in10.DigitalRead=True Then in=in+4
        If in11.DigitalRead=True Then in=in+8
        smp(i)=in
       
        DelayMicroseconds(dly)
    Next
    AStream.Write(smp)
   
End Sub

Sub Astream_NewData (Buffer() As Byte)
   
    If Buffer.Length > 0 Then
        dly=Buffer(0)
    End If  
End Sub

Sub AStream_Error
    Log("error")
End Sub
 

Attachments

  • logic_analyzer405.jpg
    logic_analyzer405.jpg
    23.8 KB · Views: 1,044
  • logic_b4a.zip
    32.5 KB · Views: 80
  • logic_analyzer.jpg
    logic_analyzer.jpg
    28.6 KB · Views: 102
  • logic_b4r.zip
    1 KB · Views: 76
Top