Skip to content

Instantly share code, notes, and snippets.

@7sharp9
Created April 12, 2013 11:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 7sharp9/5371270 to your computer and use it in GitHub Desktop.
Save 7sharp9/5371270 to your computer and use it in GitHub Desktop.
[<Activity (Label = "HelloBarometer", MainLauncher = true)>]
type MainActivity () =
inherit Activity ()
let calculateAltitudeInFeet hPAs =
let pstd = 1013.25
(1.0 - Math.Pow((hPAs/pstd), 0.190284)) * 145366.45
let mainLabel = ref Unchecked.defaultof<_>
override x.OnCreate(bundle) =
base.OnCreate(bundle)
//Set our view from the "main" layout resource
x.SetContentView(Resource_Layout.Main)
//Detect the barometer
let sm = x.GetSystemService(Context.SensorService) :?> SensorManager
let pressure = sm.GetDefaultSensor(SensorType.Pressure)
//Subscribe to it
let eventBind = sm.RegisterListener(x, pressure, SensorDelay.Normal)
// find the main label and assign it
mainLabel := x.FindViewById<TextView>(Resource_Id.mainLabel)
interface ISensorEventListener with
member x.OnSensorChanged(pressureEvent) =
Console.WriteLine("Under pressure {0}",pressureEvent.ToString())
let hPAs = pressureEvent.Values.[0]
let msg = sprintf "Current pressure: %f hPA!" hPAs
(!mainLabel).Text <- msg
let calcedAltitude = calculateAltitudeInFeet(float hPAs)
x.FindViewById<TextView>(Resource_Id.barometer).Text <- String.Format("Calculated altitude is {0} ft", calcedAltitude)
member x.OnAccuracyChanged(sensor, accuracy) =
Console.WriteLine("Things have changed")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment