' BuWizz2
Public BLE_MAC As String = "50:FA:AB:38:A6:1A" ' BuWizz2 MAC address
Public BLE_DEVICE_NAME As String = "BuWizz2" ' Case sensitive
' BLE Service & Characteristics BuWizz V2
Public SERVICE_UUID As String = "4e050000-74fb-4481-88b3-9919b1676e93" ' BuWizz2 service UUID
Public CHAR_UUID_RX As String = "000092d1-0000-1000-8000-00805f9b34fb" ' Control characteristic UUID Flags read,notify,write
Public CHAR_UUID_TX As String = "000092d1-0000-1000-8000-00805f9b34fb" ' Control characteristic UUID Flags read,notify,write
' Ports
Public PORT_0 As Byte = 0
Public PORT_1 As Byte = 1
Public PORT_2 As Byte = 2
Public PORT_3 As Byte = 3
' RailBuzz
' Private CMD_DELAY As Long = 100
Private OUTPUT_LEVEL_DEFAULT As Byte = 1
' Commands
Public CMD_OUTPUT_LEVEL As Byte = 0x11
Public CMD_POWER_LEVEL As Byte = 0x10
' Power Level
Public POWER_LEVEL_FULL_BACKWARDS As Int = -127
Public POWER_LEVEL_STOP As Int = 0
Public POWER_LEVEL_FULL_FORWARDS As Int = 127
' Speed
Public SPEED_OFFSET As Int = 51
Public SPEED_MIN As Int = 0 '51
Public SPEED_MAX As Int = 127 '100
Public DIRECTION_STP As Int = 0
Public DIRECTION_FWD As Int = 1
Public DIRECTION_BCK As Int = 2
Private BuWizz As BleGattManager
Private Sub B4XPage_Created (Root1 As B4XView)
...
BuWizz.Initialize(Me, "BuWizz")
BuWizz.setWriteMode(BuWizz.WRITE_WITH_RESPONSE)
End Sub
Public Sub Connect
' Ensure to add permission to manifest ACCESS_FINE_LOCATION, BLUETOOTH_SCAN, BLUETOOTH_CONNECT)
Dim rp As RuntimePermissions
Dim Permissions As List
Dim phone As Phone
If phone.SdkVersion >= 31 Then
Permissions = Array("android.permission.BLUETOOTH_SCAN", "android.permission.BLUETOOTH_CONNECT", rp.PERMISSION_ACCESS_FINE_LOCATION)
Else
Permissions = Array(rp.PERMISSION_ACCESS_FINE_LOCATION)
End If
For Each per As String In Permissions
rp.CheckAndRequest(per)
Wait For B4XPage_PermissionResult (Permission As String, Result As Boolean)
If Result = False Then
ToastMessageShow("No permission: " & Permission, True)
Return
End If
Next
' Check if BLE is powered on
If BuWizz.IsBluetoothOn = False Then
Log("[Connect] BLE not powered on.")
Return
End If
BuWizz.Connect( _
BLE_MAC, _
SERVICE_UUID, _
CHAR_UUID_TX)
End Sub
' Events
Sub BuWizz_Connected
Log("[Connect] Connected")
End Sub
Sub BuWizz_ConnectionFailed
Log("[Connect] Connection failed")
End Sub
Sub BuWizz_Disconnected
Log("[Connect] Disconnected")
SetTilesInitialState
End Sub
Sub BuWizz_DeviceNotFound
Log("[Connect] BuWizz device not found")
End Sub
Sub BuWizz_DataReceived (Data() As Byte)
Log($"[DataReceived] ${bc.HexFromBytes(Data)}"$)
End Sub
' Write to the BuWizz2 Brick
' Set output level = mandatory befor driving motors
' Write 2 bytes:
' Byte 1 = 0x11
' Byte 2 = output level with low: 1, medium: 2, high: 3, ludicrous: 4
' level - default 1
Public Sub SetOutputLevel
Dim cmd() As Byte = Array As Byte(CMD_OUTPUT_LEVEL, OUTPUT_LEVEL_DEFAULT)
Log($"[SetOutputLevel] cmd=${Convert.HexFromBytes(cmd)}, level=${CMD_OUTPUT_LEVEL}, leveldefault=${OUTPUT_LEVEL_DEFAULT}"$)
BuWizz.Write(cmd)
End Sub
' Set power level for a port.
' Write 6 bytes:
' Byte 1 = 0x10
' Byte 2 = 7 bit signed channel 1 output value
' Byte 3 = 7 bit signed channel 2 output value
' Byte 4 = 7 bit signed channel 3 output value
' Byte 5 = 7 bit signed channel 4 output value
' Byte 6 = 0x00
' port - 0-3
' level -127 - 127
Public Sub SetPowerLevel(p As Byte, level As Int)
' Set mandatory output level
SetOutputLevel
' Define the command as byte array
Dim cmd() As Byte = Array As Byte(CMD_POWER_LEVEL, 0, 0, 0, 0, 0x00)
Log($"[SetPowerLevel] cmd=${Convert.HexFromBytes(cmd)}, port=${p}, level=${level}"$)
' Set the port level for bytes 1 (=port 1 with index 0) to 4 (=port 4 with index 3)
cmd(1 + p) = level
' Write the command
BuWizz.Write(cmd)
End Sub