omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    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

    Pythonista
    barometer sensor
    10
    25
    20877
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • mikael
      mikael last edited by

      ... 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())
      
      1 Reply Last reply Reply Quote 0
      • victordomingos
        victordomingos last edited by

        What kind of value (units) is this last version of the script printing out?

        1 Reply Last reply Reply Quote 0
        • JonB
          JonB last edited by

          timestamp (an NSTimeInterval in seconds since the device has been booted)
          relativeAltitude (an NSNumber in meters)
          pressure (an NSNumber in kilopascal)

          1 Reply Last reply Reply Quote 1
          • DaveClark
            DaveClark last edited by

            I want to use the NSNumber for pressure in a calculation. How do I convert it to something I can use with mathematical functions?

            mikael 1 Reply Last reply Reply Quote 0
            • mikael
              mikael @DaveClark last edited by

              @DaveClark, change return pressure on line 28 to return pressure.CGFloatValue().

              1 Reply Last reply Reply Quote 0
              • DaveClark
                DaveClark last edited by

                That did it and now I know I need to read the documents more. Thanks you for your help

                1 Reply Last reply Reply Quote 0
                • DaveClark
                  DaveClark last edited by

                  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
                  CGFloatValue

                  again, I am at a loss

                  1 Reply Last reply Reply Quote 0
                  • DaveClark
                    DaveClark last edited by

                    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

                    mikael 1 Reply Last reply Reply Quote 0
                    • mikael
                      mikael @DaveClark last edited by

                      @DaveClark, did you update to 11.2.5?

                      DaveClark 1 Reply Last reply Reply Quote 0
                      • zrzka
                        zrzka last edited by

                        Side note, just something to learn about double, float, NSNumber, ...

                        CGFloat is kind of unfortunate type name. In the C / ObjC world, it's double (C) on 64-bit platforms and float (C) on the rest.

                        On the other side, Python doesn't have double. It has "just" float and almost all platforms map Python float 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 as 1.2999999523162841796875 (float) and as 1.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.

                        1 Reply Last reply Reply Quote 1
                        • DaveClark
                          DaveClark @mikael last edited by

                          @mikael said:

                          @DaveClark, did you update to 11.2.5?

                          Yes I did update to 11.2.5

                          @zrzka Thanks for the good info

                          mikael 1 Reply Last reply Reply Quote 0
                          • mikael
                            mikael @DaveClark last edited by

                            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.

                            1 Reply Last reply Reply Quote 1
                            • First post
                              Last post
                            Powered by NodeBB Forums | Contributors