Created
April 12, 2013 11:03
-
-
Save 7sharp9/5371270 to your computer and use it in GitHub Desktop.
F# conversion of Larry O'Brien Android Barometer post:
http://www.knowing.net/index.php/2013/04/06/accessing-the-android-barometer-using-monotouch-android/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[<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