Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Accessing Barometer Measurements
-
I am sure that @tszheichoi takes an extra flashlight when spelunking just in case the iPhone batteries are drained!
-
I am not used to ObjC stuff. Is there an easy way to get a single barometer reading from the sensor?
-
@victordomingos, this Apple API only gives you relative readings, not one absolute value. Thus it seems that one single call would not be very useful?
-
... but of course you have the pressure value there. Here’s a modification that gives you a simple
get_pressure
function (a bit of a hack because of the global variable use).from objc_util import ObjCInstance, ObjCClass, ObjCBlock, c_void_p pressure = None def get_pressure(): def handler(_cmd, _data, _error): global pressure pressure = ObjCInstance(_data).pressure() handler_block = ObjCBlock(handler, restype=None, argtypes=[c_void_p, c_void_p, c_void_p]) CMAltimeter = ObjCClass('CMAltimeter') NSOperationQueue = ObjCClass('NSOperationQueue') if not CMAltimeter.isRelativeAltitudeAvailable(): print('This device has no barometer.') return altimeter = CMAltimeter.new() main_q = NSOperationQueue.mainQueue() altimeter.startRelativeAltitudeUpdatesToQueue_withHandler_(main_q, handler_block) #print('Started altitude updates.') try: while pressure is None: pass finally: altimeter.stopRelativeAltitudeUpdates() #print('Updates stopped.') return pressure if __name__ == '__main__': print(get_pressure())
-
What kind of value (units) is this last version of the script printing out?
-
timestamp (an NSTimeInterval in seconds since the device has been booted)
relativeAltitude (an NSNumber in meters)
pressure (an NSNumber in kilopascal) -
I want to use the NSNumber for pressure in a calculation. How do I convert it to something I can use with mathematical functions?
-
@DaveClark, change
return pressure
on line 28 toreturn pressure.CGFloatValue()
. -
That did it and now I know I need to read the documents more. Thanks you for your help
-
It was working after I change line 28 to your suggestion. But today without changing anything(I think)
line 28 was return pressure
line 28 now return pressure.CGFloatValue()
Now it is showing an error message complaining about line 28. It says:
AttributeError
no method found for
CGFloatValueagain, I am at a loss
-
OK. I changed the
return pressure.CGFloatValue()
to
return pressure.floatValue()
and it works again. I still don't understand how it worked earlier.
Thanks for pointing me in the right direction
-
@DaveClark, did you update to 11.2.5?
-
Side note, just something to learn about
double
,float
,NSNumber
, ...CGFloat
is kind of unfortunate type name. In the C / ObjC world, it'sdouble
(C) on 64-bit platforms andfloat
(C) on the rest.On the other side, Python doesn't have
double
. It has "just"float
and almost all platforms map Pythonfloat
to IEEE-754 double precision ->double
(C).What I would like to say, you should get your value via [NSNumber doubleValue] instead of
floatValue
these days.Not saying that it affects your case, but it's good to know. For example
1.3
is actually stored as1.2999999523162841796875
(float
) and as1.30000000000000004440892098501
(double
), etc.You can learn more at Floating Point Arithmetic: Issues and Limitations or if you'd like to explore how is
float
(C) represented just check this nice converter. You can see what is actually stored, what's the error b/o conversion, ... Or just Google for "IEE 754 converter" to find more of them. -
@mikael said:
@DaveClark, did you update to 11.2.5?
Yes I did update to 11.2.5
@zrzka Thanks for the good info
-
As a general note, I just updated as well. CGFloatValue worked before the update, and works no longer.
Lesson learned: Only use stuff mentioned in the docs, not something that you discover in REPL.