forked from kivy/plyer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
131 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
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() |
This file contains 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
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() |