- Kivy Cookbook
- Hugo Solis
- 711字
- 2021-07-16 20:39:57
Using the gyroscope
Mobile devices today also have a gyroscope. The gyroscope is considered as a no-touch event. This kind of event does not have a start or an end; it is always providing information.
Getting ready
For this recipe, we will use the Kv language for the design of the widgets. Also, this recipe will use the common button and label widgets for reference. Obviously, to run the code of this recipe properly, a device with a gyroscope is necessary.
It is necessary to install a package to use the gyroscope with any mobile device. While there is a way to deal with specific devices, we do not have to reinvent the wheel. Let's use the plyer
package, which is an effort from many of the Kivy developers. To install the package from the shell, use:
$ sudo pip plyer install
How to do it…
To complete this recipe, follow these steps:
- In the KV file, define the four labels and the button:
<Acce>: Label: id: label1 pos: 150, 300 text: 'X: ' Label: id: label2 pos: 150, 250 text: 'Y: ' Label: id: label3 pos: 150, 200 text: 'Z: ' Label: id: status pos: 150, 150 text: '' Button: id: button1 pos: 150, 50 text: 'Start' on_press: root.pressed1()
- In the Python file, import the usual packages to use Kivy.
- Import the
Clock
andplyer
packages. - In the class for the rule, define a class to stand by the gyroscope.
- Define the method to start to retrieve data from the gyroscope.
- Define the method when the button is pressed:
from kivy.app import App from kivy.properties import ObjectProperty from kivy.uix.widget import Widget from kivy.clock import Clock from plyer import gyroscope class Acce(Widget): def __init__(self): super(Acce, self).__init__() self.sensorEnabled = False def get_orientation(self, dt): val = gyroscope.orientation self.ids.label1.text = "X: " + str(val[0]) self.ids.label2.text = "Y: " + str(val[1]) self.ids.label3.text = "Z: " + str(val[2]) def pressed1(self): try: if not self.sensorEnabled: gyroscope.enable() Clock.schedule_interval(self.get_orientation, 1 / 20.) self.sensorEnabled = True self.ids.button1.text = "Stop" else: gyroscope.disable() Clock.unschedule(self.get_orientation) self.sensorEnabled = False self.ids.button1.text = "Start" except NotImplementedError: import traceback; traceback.print_exc() self.ids.status.text =\ "Gyroscope is not supported for your platform" class e4App(App): def build(self): return Acce() if __name__ == '__main__': e4App().run()
How it works…
The KV file that we are using is similar to the last recipe's KV file. We define four labels and the start button.
In the Python code, the first three lines are usual to import kivy
and the widgets. The fourth line:
from kivy.clock import Clock
We are importing the clock, which is used to get the accelerometer value in a time lap because, as we already said, this input is continuously providing data. The fifth line:
from plyer import gyroscope
This is importing the package plyer
, which is the one that makes it possible to retrieve the data from an iOS or Android device. The sixth line:
class Acce(Widget):
It is the line where the class for the rule starts and its name must be the same as in the Kivy rule. The seventh, eighth, and ninth lines are:
def __init__(self): super(Acce, self).__init__() self.sensorEnabled = False
Those are where we define the methods that initialize the class and put the accelerometer on standby to be used later. The tenth, eleventh, twelfth, thirteenth, and fourteenth lines are as follows:
def get_orientation(self, dt): val = gyroscope.orientation self.ids.label1.text = "X: " + str(val[0]) self.ids.label2.text = "Y: " + str(val[1]) self.ids.label3.text = "Z: " + str(val[2])
Those lines are allowing us to retrieve data from the gyroscope and enabling us to modify the text of the first three labels with the data of the orientation. The fifteenth line:
def pressed1(self):
It is where we define the method performed when the button is pressed. In the sixteenth line, we start a try
sentence to start to get data from the gyroscope. With the seventeenth line, we can open an if
sentence for the case that the gyroscope is unable, enable it. The eighteenth line:
Clock.schedule_interval(self.get_orientation, 1 / 20.)
In this line, we can get the intervals of the orientation by calling the get_orientation
method. The next class is the usual one to display our Kivy interface.
See also
If you want to run the code in a mobile device, see Chapter 9, Kivy for Mobile Devices.
- Spring Boot+Spring Cloud+Vue+Element項目實戰(zhàn):手把手教你開發(fā)權(quán)限管理系統(tǒng)
- 基于差分進化的優(yōu)化方法及應用
- Java面向?qū)ο蟪绦蜷_發(fā)及實戰(zhàn)
- 小學生C++創(chuàng)意編程(視頻教學版)
- 零基礎入門學習Python(第2版)
- Python極簡講義:一本書入門數(shù)據(jù)分析與機器學習
- Python深度學習:模型、方法與實現(xiàn)
- 時空數(shù)據(jù)建模及其應用
- Java并發(fā)編程之美
- JQuery風暴:完美用戶體驗
- UI設計基礎培訓教程(全彩版)
- Hacking Android
- Akka入門與實踐
- Web前端開發(fā)技術(shù):HTML、CSS、JavaScript
- HTML5 Game Development by Example:Beginner's Guide(Second Edition)