def get_pressure(): ''' Obter a pressão atmosférica do barómetro do próprio dispositivo, se existir ''' 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 None altimeter = CMAltimeter.new() main_q = NSOperationQueue.mainQueue() altimeter.startRelativeAltitudeUpdatesToQueue_withHandler_( main_q, handler_block) try: while pressure is None: pass finally: altimeter.stopRelativeAltitudeUpdates() return pressure.floatValue() * 7.5006375541921
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
def main(): num_samples = 1000000 arrayA = [] arrayM = [] #arrayG = [] arrayP = [] arrayJ = [] arrayGPS = [] #GPS dataArray = [] 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) motion.start_updates() location.start_updates() # GPS print("Logging start...") sleep(1.0) for i in range(num_samples): sleep(0.05) a = motion.get_user_acceleration() m = motion.get_magnetic_field() j = motion.get_attitude() gps = location.get_location() # GPS if a[1] > 0.8: break dataArray.append([relativeAltitude, a[2], m[0], m[1]]) arrayA.append(a) arrayM.append(m) arrayJ.append(j) arrayP.append(relativeAltitude) arrayGPS.append(gps) #GPS motion.stop_updates() location.stop_updates() # GPS altimeter.stopRelativeAltitudeUpdates() print("Logging stop and Saving start...") import pickle f = open('yokohama.serialize', 'wb') #pickle.dump([arrayA, arrayM, arrayP],f) pickle.dump([arrayA, arrayM, arrayJ, arrayP, arrayGPS], f) #GPS f.close print("Saving is finished.") x_values = [x * 0.05 for x in range(len(dataArray))] for i, color, label in zip(range(3), 'rgb', 'XYZ'): plt.plot(x_values, [g[i] for g in arrayM], color, label=label, lw=2) plt.grid(True) plt.xlabel('t') plt.ylabel('G') plt.gca().set_ylim([-100, 100]) plt.legend() plt.show()
def get_synthesizer_and_voice( language: str = "ja-JP") -> Tuple[ObjCInstance, ObjCInstance]: assert isinstance(language, str), (type(language), language) AVSpeechSynthesizer = ObjCClass("AVSpeechSynthesizer") AVSpeechSynthesisVoice = ObjCClass("AVSpeechSynthesisVoice") synthesizer = AVSpeechSynthesizer.new() for voice in AVSpeechSynthesisVoice.speechVoices(): # print(voice, voice.description()) if language in str(voice.description()): return synthesizer, voice raise ValueError(f"No voice found for {language}")
def _setupEngine(self): AVAudioEngine = ObjCClass('AVAudioEngine') AVAudioSession = ObjCClass('AVAudioSession') error = ctypes.c_void_p(0) session = AVAudioSession.sharedInstance() category = session.setCategory('AVAudioSessionCategoryPlayback', error=ctypes.pointer(error)) if error: raise Exception('error setting up category') session.setActive(True, error=ctypes.pointer(error)) if error: raise Exception('error setting up session active') engine = AVAudioEngine.new() return engine
def sendEvent2_(_self, _cmd, _event): from objc_util import ObjCInstance, CGRect, ObjCClass UIView = ObjCClass('UIView') try: self = ObjCInstance(_self) e = ObjCInstance(_event) self.sendEvent2_(e) global __tvs global __show_touches if not __show_touches: if __tvs: if len(__tvs) > 0: for k, v in enumerate(__tvs): v[1].removeFromSuperview() __tvs.pop(k) return for tid in __tvs.keys(): ids = [id(t) for t in e.allTouches().allObjects()] if not tid in ids: __tvs[tid][1].removeFromSuperview() __tvs.pop(tid) for t in e.allTouches().allObjects(): if t.phase() == 3 or t.phase() == 4: __tvs[id(t)][1].removeFromSuperview() __tvs.pop(id(t), None) elif t.phase() == 0: vc = t.window().rootViewController() while vc.presentedViewController(): vc = vc.presentedViewController() v = UIView.new() v.size = (30, 30) v.center = t.locationInView_(vc.view()) v.backgroundColor = UIColor.blackColor() v.alpha = 0.4 v.layer().cornerRadius = 15 v.userInteractionEnabled = False vc.view().addSubview_(v) __tvs[id(t)] = [t, v] elif t.phase() == 1: __tvs[id(t)][0] = t vc = t.window().rootViewController() while vc.presentedViewController(): vc = vc.presentedViewController() v = __tvs[id(t)][1] v.center = t.locationInView_(vc.view()) except: pass
def main(): 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 True: pass finally: altimeter.stopRelativeAltitudeUpdates() print('Updates stopped.')
# coding: utf-8 # https://gist.github.com/filippocld/c0deb6714a9aff9f1da9 from objc_util import NSBundle, ObjCClass, on_main_thread NSBundle.bundleWithPath_( '/System/Library/Frameworks/MediaPlayer.framework').load() MPVolumeView = ObjCClass('MPVolumeView') volume_view = MPVolumeView.new().autorelease() @on_main_thread def set_system_volume(value): volume_view.subviews()[0].value = value @on_main_thread def get_system_volume(): return volume_view.subviews()[0].value() def upAction(): #Volume Up button has been pressed print('Up!') def downAction(): #Volume Down button has been pressed print('Down!')
HOST_VM_INFO = c_int(2) # This is a c macro KERN_SUCCESS = 0 # Another c macro (No c_int initializer used because we don't pass it to a c function) get_host_statistics = host_statistics(host_port, HOST_VM_INFO, cast(byref(vm_stat), POINTER(c_int)), byref(host_size)) if not get_host_statistics == int(KERN_SUCCESS): print("Failed to fetch vm statistics") mem_used = (vm_stat.active_count + vm_stat.inactive_count + vm_stat.wire_count) * int(page_size.value) mem_free = vm_stat.free_count * int(page_size.value) mem_total = mem_used + mem_free physical_memory = NSProcessInfo.processInfo().physicalMemory() byteCountFormtter = NSByteCountFormatter.new() mem_used = byteCountFormtter.stringFromByteCount_(mem_used) mem_free = byteCountFormtter.stringFromByteCount_(mem_free) mem_total = byteCountFormtter.stringFromByteCount_(mem_total) physical_memory = byteCountFormtter.stringFromByteCount_(physical_memory) print('used: ', mem_used) print('free: ', mem_free) print('total: ', mem_total) print('total (according to Cocoa): ', physical_memory) # NSProcessInfo.processInfo().activeProcessorCount()
def get_available_memory(self): """found in github what is the original source of this peace of magic? https://gist.github.com/lukaskollmer/a09c0278d2d224b9f4839a895ebb9988 https://forum.omz-software.com/topic/3146/share-code-get-available-memory """ NSProcessInfo = ObjCClass('NSProcessInfo') NSByteCountFormatter = ObjCClass('NSByteCountFormatter') class c_vm_statistics(Structure): _fields_ = [('free_count', c_uint), ('active_count', c_uint), ('inactive_count', c_uint), ('wire_count', c_uint), ('zero_fill_count', c_uint), ('reactivations', c_uint), ('pageins', c_uint), ('pageouts', c_uint), ('faults', c_uint), ('cow_faults', c_uint), ('lookups', c_uint), ('hits', c_uint), ('purgeable_count', c_uint), ('purges', c_uint), ('speculative_count', c_uint)] c = cdll.LoadLibrary(None) mach_host_self = c.mach_host_self mach_host_self.restype = c_uint mach_host_self.argtypes = [c_void_p] host_page_size = c.host_page_size host_page_size.restype = c_int host_page_size.argtypes = [c_uint, POINTER(c_uint)] host_statistics = c.host_statistics host_statistics.restype = c_int host_statistics.argtypes = [ c_uint, c_int, POINTER(c_int), POINTER(c_uint) ] host_port = c_uint() host_size = c_uint() page_size = c_uint() host_port = mach_host_self(None) host_size = c_uint(int(sizeof(c_vm_statistics) / sizeof(c_int))) host_page_size(host_port, byref(page_size)) vm_stat = c_vm_statistics() HOST_VM_INFO = c_int(2) # This is a c macro KERN_SUCCESS = 0 # Another c macro (No c_int initializer used because we don't pass it to a c function) get_host_statistics = host_statistics( host_port, HOST_VM_INFO, cast(byref(vm_stat), POINTER(c_int)), byref(host_size)) if not get_host_statistics == int(KERN_SUCCESS): print("Failed to fetch vm statistics") mem_used = (vm_stat.active_count + vm_stat.inactive_count + vm_stat.wire_count) * int(page_size.value) mem_free = vm_stat.free_count * int(page_size.value) mem_total = mem_used + mem_free physical_memory = NSProcessInfo.processInfo().physicalMemory() byteCountFormtter = NSByteCountFormatter.new() mem_used = byteCountFormtter.stringFromByteCount_(mem_used) mem_free = byteCountFormtter.stringFromByteCount_(mem_free) mem_total = byteCountFormtter.stringFromByteCount_(mem_total) physical_memory = byteCountFormtter.stringFromByteCount_( physical_memory) self.LogMessage(f"used {mem_used} free {mem_free} total {mem_total}")
def _setupSampler(self, engine): AVAudioUnitSampler = ObjCClass('AVAudioUnitSampler') sampler = AVAudioUnitSampler.new() engine.attachNode(sampler) engine.connect_to_format_(sampler, engine.mainMixerNode(), None) return sampler