From 1743a32830f7920a977d5e945e1c25814c2f2e6e Mon Sep 17 00:00:00 2001 From: "Maxim Ramanenka (Red Alert)" Date: Thu, 8 Sep 2022 14:31:36 +0300 Subject: [PATCH] updated examples for vibrator --- examples/vibrator/main.py | 28 +++++++++--- examples/vibrator/vibrator.kv | 26 +++++++++++ examples/vibrator/vibrator2.kv | 25 +++++++++++ examples/vibrator/vibrator_v2.py | 52 +++++++++++++++++++++ examples/vibrator/vibrator_v3.py | 77 ++++++++++++++++++++++++++++++++ 5 files changed, 202 insertions(+), 6 deletions(-) create mode 100644 examples/vibrator/vibrator.kv create mode 100644 examples/vibrator/vibrator2.kv create mode 100644 examples/vibrator/vibrator_v2.py create mode 100644 examples/vibrator/vibrator_v3.py diff --git a/examples/vibrator/main.py b/examples/vibrator/main.py index d7b0f30d6..8d0cd01af 100644 --- a/examples/vibrator/main.py +++ b/examples/vibrator/main.py @@ -1,6 +1,27 @@ from kivy.app import App from kivy.lang import Builder from kivy.uix.boxlayout import BoxLayout +from kivy.utils import platform + +if 'android' == platform: + # ------------------------------------------- # + # for permissions + from android.permissions import Permission, request_permissions, check_permission + + # check permissions + def check_permissions(perms): + for perm in perms: + if check_permission(perm) != True: + return False + return True + + # list perms + perms = [Permission.VIBRATE] + + # get perms + if check_permissions(perms)!= True: + request_permissions(perms) + # ------------------------------------------- # Builder.load_string(''' #:import vibrator plyer.vibrator @@ -29,23 +50,18 @@ text: 'vibrate pattern' on_release: vibrator.pattern([float(n) for n in ti.text.split(',')]) - ''') - class VibrationInterface(BoxLayout): '''Root Widget.''' pass - class VibrationApp(App): - def build(self): return VibrationInterface() def on_pause(self): return True - if __name__ == "__main__": - VibrationApp().run() + VibrationApp().run() \ No newline at end of file diff --git a/examples/vibrator/vibrator.kv b/examples/vibrator/vibrator.kv new file mode 100644 index 000000000..0276f7110 --- /dev/null +++ b/examples/vibrator/vibrator.kv @@ -0,0 +1,26 @@ +#:import vibrator plyer.vibrator +: + orientation: 'vertical' + Label: + size_hint_y: None + height: sp(40) + text: 'vibrator exists: ' + str(vibrator.exists()) + Button: + text: 'vibrate 10s' + on_release: vibrator.vibrate(10) + Button: + text: 'vibrate 1s' + on_release: vibrator.vibrate(1) + Button: + text: 'vibrate 0.1s' + on_release: vibrator.vibrate(0.1) + Button: + text: 'cancel vibration' + on_release: vibrator.cancel() + TextInput: + id: ti + text: '0.5,0.5,1,2,0.1,0.1,0.1,0.1,0.1,0.1' + Button: + text: 'vibrate pattern' + on_release: + vibrator.pattern([float(n) for n in ti.text.split(',')]) \ No newline at end of file diff --git a/examples/vibrator/vibrator2.kv b/examples/vibrator/vibrator2.kv new file mode 100644 index 000000000..896db9f20 --- /dev/null +++ b/examples/vibrator/vibrator2.kv @@ -0,0 +1,25 @@ +: + orientation: 'vertical' + Label: + size_hint_y: None + height: sp(40) + text: 'root exists: ' + str(root.exists()) + Button: + text: 'vibrate 10s' + on_release: root.vibro(10) + Button: + text: 'vibrate 1s' + on_release: root.vibro(1) + Button: + text: 'vibrate 0.1s' + on_release: root.vibro(0.1) + Button: + text: 'cancel vibration' + on_release: root.cancel() + TextInput: + id: ti + text: '0.5,0.5,1,2,0.1,0.1,0.1,0.1,0.1,0.1' + Button: + text: 'vibrate pattern' + on_release: + root.pattern([float(n) for n in ti.text.split(',')]) \ No newline at end of file diff --git a/examples/vibrator/vibrator_v2.py b/examples/vibrator/vibrator_v2.py new file mode 100644 index 000000000..c86101ed1 --- /dev/null +++ b/examples/vibrator/vibrator_v2.py @@ -0,0 +1,52 @@ +from kivy.app import App +from kivy.uix.boxlayout import BoxLayout + +import os.path +from os.path import dirname +from kivy.lang import Builder +from kivy.utils import platform + +if 'android' == platform: + # ------------------------------------------- # + # for permissions + from android.permissions import Permission, request_permissions, check_permission + + # check permissions + def check_permissions(perms): + for perm in perms: + if check_permission(perm) != True: + return False + return True + + # list perms + perms = [Permission.VIBRATE] + + # get perms + if check_permissions(perms)!= True: + request_permissions(perms) + # ------------------------------------------- # + +# Builder.load_file('vibrator.kv') # not work +Builder.load_file(os.path.join(dirname(__file__), 'vibrator.kv')) + +class VibrationInterface(BoxLayout): + '''Root Widget.''' + + def is_android(self): + if 'android' == platform: + return True + else: + return False + + def do_nothing(self): + pass + +class VibrationApp(App): + def build(self): + return VibrationInterface() + + def on_pause(self): + return True + +if __name__ == "__main__": + VibrationApp().run() \ No newline at end of file diff --git a/examples/vibrator/vibrator_v3.py b/examples/vibrator/vibrator_v3.py new file mode 100644 index 000000000..980dc1f4d --- /dev/null +++ b/examples/vibrator/vibrator_v3.py @@ -0,0 +1,77 @@ +from kivy.app import App +from kivy.uix.boxlayout import BoxLayout + +import os.path +from os.path import dirname +from kivy.lang import Builder +from kivy.utils import platform + +if 'android' == platform: + from plyer import vibrator + # ------------------------------------------- # + # for permissions + from android.permissions import Permission, request_permissions, check_permission + + # check permissions + def check_permissions(perms): + for perm in perms: + if check_permission(perm) != True: + return False + return True + + # list perms + perms = [Permission.VIBRATE] + + # get perms + if check_permissions(perms)!= True: + request_permissions(perms) + # ------------------------------------------- # + +# Builder.load_file('vibrator2.kv') # not work +Builder.load_file(os.path.join(dirname(__file__), 'vibrator2.kv')) + +class VibrationInterface(BoxLayout): + '''Root Widget.''' + + def vibro(self, n): + if self.__is_android(): + vibrator.vibrate(n) + else: + self.__do_nothing() + + def exists(self): + if self.__is_android(): + return vibrator.exists() + else: + self.__do_nothing() + + def cancel(self): + if self.__is_android(): + vibrator.cancel() + else: + self.__do_nothing() + + def pattern(self, arr): + if self.__is_android(): + vibrator.pattern(arr) + else: + self.__do_nothing() + + def __is_android(self): + if 'android' == platform: + return True + else: + return False + + def __do_nothing(self): + pass + +class VibrationApp(App): + def build(self): + return VibrationInterface() + + def on_pause(self): + return True + +if __name__ == "__main__": + VibrationApp().run() \ No newline at end of file