#Region Project Attributes
#AutoFlushLogs: True
#CheckArrayBounds: True
#StackBufferSize: 300
#End Region
Sub Process_Globals
Public Serial1 As Serial
Public Wire As WireMaster
Private wireerror As Byte = 0
Private deviceaddress As Byte
Private tmr As Timer
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
Delay(500)
Wire.Initialize
Log("App start")
tmr.Initialize("tmr_Tick", 2000)
tmr.Enabled = True
Log("Start after 2 sec...")
End Sub
Sub tmr_Tick
Scanning
End Sub
Private Sub Scanning
Private bc As ByteConverter
Dim b(1) As Byte
Dim devices As Int
Log("Scanning started...")
devices = 0
For i = 0 To 127
deviceaddress = i
RunNative("icwirebegintransmisson", deviceaddress)
RunNative("icwireendtransmisson", Null)
b(0) = deviceaddress
If wireerror = 0 Then
Log("I2C device found at address: 0x", bc.HexFromBytes(b), " (", deviceaddress, ")")
devices = devices + 1
End If
If wireerror = 4 Then
Log("I2C device unknown at address: 0x", bc.HexFromBytes(b), " (", deviceaddress, ")")
End If
Next
If (devices = 0) Then
Log("No I2C devices.")
End If
Log("Scanning completed.")
End Sub
#if C
#include <Wire.h>
void icwirebegintransmisson (B4R::Object* o) {
Wire.beginTransmission(b4r_main::_deviceaddress);
}
void icwireendtransmisson (B4R::Object* o) {
b4r_main::_wireerror = Wire.endTransmission();
}
#End if