Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add IrBlaster facade and Android implementation #118

Merged
merged 1 commit into from
Mar 8, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions plyer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,7 @@
#: Battery proxy to :class:`plyer.facades.Battery`
battery = Proxy(
'battery', facades.Battery)

#: IrBlaster proxy to :class:`plyer.facades.IrBlaster`
irblaster = Proxy(
'irblaster', facades.IrBlaster)
63 changes: 62 additions & 1 deletion plyer/facades.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

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


class Accelerometer(object):
Expand Down Expand Up @@ -422,3 +422,64 @@ def get_state(self):

def _get_state(self):
raise NotImplementedError()


class IrBlaster(object):
'''Infrared blaster facade.'''

@staticmethod
def periods_to_microseconds(frequency, pattern):
'''Convert a pattern from period counts to microseconds.
'''
period = 1000000. / frequency
return [period * x for x in pattern]

@staticmethod
def microseconds_to_periods(frequency, pattern):
'''Convert a pattern from microseconds to period counts.
'''
period = 1000000. / frequency
return [x / period for x in pattern]

@property
def frequencies(self):
'''Property which contains a list of frequency ranges
supported by the device in the form:

[(from1, to1),
(from2, to2),
...
(fromN, toN)]
'''
return self.get_frequencies()

def get_frequencies(self):
return self._get_frequencies()

def _get_frequencies(self):
raise NotImplementedError()

def transmit(self, frequency, pattern, mode='period'):
'''Transmit an IR sequence.

:parameters:
`frequency`: int
Carrier frequency for the IR transmission.
`pattern`: list[int]
Burst pair pattern to transmit.
`mode`: str, defaults to 'period'
Specifies the format of the pattern values.
Can be 'period' or 'microseconds'.
'''
return self._transmit(frequency, pattern, mode)

def _transmit(self, frequency, pattern, mode):
raise NotImplementedError()

def exists(self):
'''Check if the device has an infrared emitter.
'''
return self._exists()

def _exists(self):
raise NotImplementedError()
3 changes: 2 additions & 1 deletion plyer/platforms/android/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from os import environ
from jnius import autoclass

SDK_INT = autoclass('android.os.Build$VERSION').SDK_INT
ANDROID_VERSION = autoclass('android.os.Build$VERSION')
SDK_INT = ANDROID_VERSION.SDK_INT

if 'PYTHON_SERVICE_ARGUMENT' in environ:
PythonService = autoclass('org.renpy.android.PythonService')
Expand Down
53 changes: 53 additions & 0 deletions plyer/platforms/android/irblaster.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from jnius import autoclass

from plyer.facades import IrBlaster
from plyer.platforms.android import activity, SDK_INT, ANDROID_VERSION

if SDK_INT >= 19:
Context = autoclass('android.content.Context')
ir_manager = activity.getSystemService(Context.CONSUMER_IR_SERVICE)
else:
ir_manager = None

class AndroidIrBlaster(IrBlaster):
def _exists(self):
if ir_manager and ir_manager.hasIrEmitter():
return True
return False

@property
def multiply_pulse(self):
'''Android 4.4.3+ uses microseconds instead of period counts
'''
return not (SDK_INT == 19 and
int(str(ANDROID_VERSION.RELEASE).rsplit('.', 1)[-1]) < 3)

def _get_frequencies(self):
if not ir_manager:
return None

if hasattr(self, '_frequencies'):
return self._frequencies

ir_frequencies = ir_manager.getCarrierFrequencies()
if not ir_frequencies:
return []

frequencies = []
for freqrange in ir_frequencies:
freq = (freqrange.getMinFrequency(), freqrange.getMaxFrequency())
frequencies.append(freq)

self._frequencies = frequencies
return frequencies

def _transmit(self, frequency, pattern, mode):
if self.multiply_pulse and mode == 'period':
pattern = self.periods_to_microseconds(frequency, pattern)
elif not self.multiply_pulse and mode == 'microseconds':
pattern = self.microseconds_to_periods(frequency, pattern)
ir_manager.transmit(frequency, pattern)


def instance():
return AndroidIrBlaster()