'Code module
#Region Project Attributes
#ApplicationLabel: B4i Example
#Version: 1.0.0
'Orientation possible values: Portrait, LandscapeLeft, LandscapeRight AND PortraitUpsideDown
#iPhoneOrientations: Portrait
#iPadOrientations: Portrait
#End Region
Sub Process_Globals
Public App As Application
Public NavControl As NavigationController
Private Page1 As Page
Private mot As Motion
Private timer1 As Timer
Private bbStart, bbStop As BarButton
Private Label1 As Label
Private lblAccX As Label
Private lblAccY As Label
Private lblAccZ As Label
Private lblMagX As Label
Private lblMagY As Label
Private lblMagZ As Label
Private lblIntensity As Label
Private lblAttPitch As Label
Private lblAttRoll As Label
Private lblAttYaw As Label
Private MaxX, MaxY, MaxZ As Double
End Sub
Private Sub Application_Start (Nav As NavigationController)
NavControl = Nav
Page1.Initialize("Page1")
Page1.RootPanel.LoadLayout("1")
NavControl.ShowPage(Page1)
mot.Initialize
timer1.Initialize("timer1", 100)
bbStart = Page1.TopRightButtons.Get(0)
bbStop = Page1.TopRightButtons.Get(1)
End Sub
Sub Page1_BarButtonClick (Tag As String)
If Tag = "Start" Then
timer1.Enabled = True
mot.Start
mot.StartMagnetometer
Else
timer1.Enabled = False
mot.Stop
mot.StopMagnetometer
End If
bbStart.Enabled = Not(timer1.Enabled)
bbStop.Enabled = timer1.Enabled
End Sub
Sub Timer1_Tick
Dim attitude() As Double = mot.GetAttitude
Dim SFE_raw_accel() As Double = mot.GetUserAcceleration
Dim SFE_raw_magnet() As Double = mot.GetMagnetometer
lblAttYaw.Text = "yaw: " & NumberFormat(-attitude(0) * 180 / cPI, 1, 2) & Chr(0xB0)
lblAttPitch.Text = "pitch: " & NumberFormat(-attitude(1) * 180 / cPI, 1, 2) & Chr(0xB0)
lblAttRoll.Text = "roll: " & NumberFormat(attitude(2) * 180 / cPI, 1, 2) & Chr(0xB0)
MaxX = Max(MaxX, Abs(SFE_raw_accel(0)))
MaxY = Max(MaxY, Abs(SFE_raw_accel(1)))
MaxZ = Max(MaxZ, Abs(SFE_raw_accel(2)))
lblAccX.Text = "x: " & NumberFormat(SFE_raw_accel(0), 1, 2) & " max (" & NumberFormat(MaxX, 1, 2) & ") G"
lblAccY.Text = "y: " & NumberFormat(SFE_raw_accel(1), 1, 2) & " max (" & NumberFormat(MaxY, 1, 2) & ") G"
lblAccZ.Text = "z: " & NumberFormat(SFE_raw_accel(2), 1, 2) & " max (" & NumberFormat(MaxZ, 1, 2) & ") G"
Try
lblMagX.Text = "X: " & NumberFormat(SFE_raw_magnet(0), 1, 4)
lblMagY.Text = "Y: " & NumberFormat(SFE_raw_magnet(1), 1, 4)
lblMagZ.Text = "Z: " & NumberFormat(SFE_raw_magnet(2), 1, 4)
'Calculate actual magnetic field total intensity
Private Mag_totalintensity As Float = Sqrt(SFE_raw_magnet(0)*SFE_raw_magnet(0) + SFE_raw_magnet(1)*SFE_raw_magnet(1) + SFE_raw_magnet(2)*SFE_raw_magnet(2))
lblIntensity.Text = " Intensity: " & NumberFormat(Mag_totalintensity, 1, 2)
Catch
End Try
End Sub