Skip to content

Commit

Permalink
merge master
Browse files Browse the repository at this point in the history
  • Loading branch information
tito committed Jun 24, 2014
2 parents 5c68e8d + fbd435e commit 8eb13f4
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ Vibrator X
Sms (send messages) X
Compass X X X
Unique ID (IMEI or SN) X X X X X
Gyroscope X X
================================== ============= ============= === ======= === =====
39 changes: 38 additions & 1 deletion plyer/facades.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
'''

__all__ = ('Accelerometer', 'Camera', 'GPS', 'Notification',
'TTS', 'Email', 'Vibrator', 'Sms', 'Compass')
'TTS', 'Email', 'Vibrator', 'Sms', 'Compass',
'Gyroscope', 'UniqueID')


class Accelerometer(object):
Expand Down Expand Up @@ -318,6 +319,42 @@ def _get_orientation(self):
raise NotImplementedError()


class Gyroscope(object):
'''Gyroscope facade.
'''

@property
def orientation(self):
'''Property that returns values of the current Gyroscope sensors, as
a (x, y, z) tuple
'''
return self.get_orientation()

def enable(self):
'''Activate the Gyroscope sensor
'''
self._enable()

def disable(self):
'''Disable the Gyroscope sensor
'''
self._disable()

def get_orientation(self):
return self._get_orientation()

# private

def _enable(self):
raise NotImplementedError()

def _disable(self):
raise NotImplementedError()

def _get_orientation(self):
raise NotImplementedError()


class UniqueID(object):
'''UniqueID facade.
Expand Down
61 changes: 61 additions & 0 deletions plyer/platforms/android/gyroscope.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
'''
Android Gyroscope
---------------------
'''

from plyer.facades import Gyroscope
from jnius import PythonJavaClass, java_method, autoclass, cast
from plyer.platforms.android import activity

Context = autoclass('android.content.Context')
Sensor = autoclass('android.hardware.Sensor')
SensorManager = autoclass('android.hardware.SensorManager')

class GyroscopeSensorListener(PythonJavaClass):
__javainterfaces__ = ['android/hardware/SensorEventListener']

def __init__(self):
super(GyroscopeSensorListener, self).__init__()
self.SensorManager = cast('android.hardware.SensorManager',
activity.getSystemService(Context.SENSOR_SERVICE))
self.sensor = self.SensorManager.getDefaultSensor(
Sensor.TYPE_GYROSCOPE)

self.values = [0, 0, 0]

def enable(self):
self.SensorManager.registerListener(self, self.sensor,
SensorManager.SENSOR_DELAY_NORMAL)

def disable(self):
self.SensorManager.unregisterListener(self, self.sensor)

@java_method('()I')
def hashCode(self):
return id(self)

@java_method('(Landroid/hardware/SensorEvent;)V')
def onSensorChanged(self, event):
self.values = event.values[:3]

@java_method('(Landroid/hardware/Sensor;I)V')
def onAccuracyChanged(self, sensor, accuracy):
# Maybe, do something in future?
pass

class AndroidGyroscope(Gyroscope):
def __init__(self):
super(AndroidGyroscope, self).__init__()
self.listener = GyroscopeSensorListener()

def _enable(self):
self.listener.enable()

def _disable(self):
self.listener.disable()

def _get_orientation(self):
return tuple(self.listener.values)

def instance():
return AndroidGyroscope()
31 changes: 31 additions & 0 deletions plyer/platforms/ios/gyroscope.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'''
iOS Gyroscope
---------------------
'''

from plyer.facades import Gyroscope
from jnius import autoclass

Hardware = autoclass('org.renpy.Ios.Hardware')

class IosGyroscope(Gyroscope):

def __init__(self):
super(IosGyroscope, self).__init__()
self.bridge = autoclass('bridge').alloc().init()
self.bridge.motionManager.setGyroscopeUpdateInterval_(0.1)

def _enable(self):
self.bridge.startGyroscope()

def _disable(self):
self.bridge.stopGyroscope()

def _get_orientation(self):
return (
self.bridge.gy_x,
self.bridge.gy_y,
self.bridge.gy_z)

def instance():
return IosGyroscope()

0 comments on commit 8eb13f4

Please sign in to comment.