官术网_书友最值得收藏!

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:

  1. 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()
  2. In the Python file, import the usual packages to use Kivy.
  3. Import the Clock and plyer packages.
  4. In the class for the rule, define a class to stand by the gyroscope.
  5. Define the method to start to retrieve data from the gyroscope.
  6. 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.

主站蜘蛛池模板: 隆林| 濉溪县| 舒城县| 南丹县| 漳浦县| 余江县| 敦煌市| 平和县| 河源市| 固安县| 响水县| 宣汉县| 马关县| 奇台县| 纳雍县| 西吉县| 海丰县| 许昌县| 屏南县| 交城县| 嘉鱼县| 庆云县| 柯坪县| 筠连县| 屏边| 新蔡县| 上高县| 金沙县| 奇台县| 呼图壁县| 南阳市| 永兴县| 谷城县| 常宁市| 淮北市| 景东| 克拉玛依市| 金寨县| 吴川市| 田东县| 天门市|