SHOW:
|
|
- or go back to the newest paste.
| 1 | __version__ = '1.0' # declare the app version. Will be used by buildozer | |
| 2 | ||
| 3 | import time | |
| 4 | from kivy.app import App # for the main app | |
| 5 | from kivy.uix.floatlayout import FloatLayout # the UI layout | |
| 6 | from kivy.uix.label import Label # a label to show information | |
| 7 | from plyer import accelerometer # object to read the accelerometer | |
| 8 | from plyer import gyroscope | |
| 9 | from plyer import gps | |
| 10 | from plyer import temperature | |
| 11 | from plyer import compass | |
| 12 | from kivy.clock import Clock # clock to schedule a method | |
| 13 | ||
| 14 | ||
| 15 | class UI(FloatLayout): # the app ui | |
| 16 | def __init__(self, **kwargs): | |
| 17 | super(UI, self).__init__(**kwargs) | |
| 18 | self.lblAcce = Label(text="Датчики") | |
| 19 | self.add_widget(self.lblAcce) | |
| 20 | try: | |
| 21 | accelerometer.enable() | |
| 22 | gyroscope.enable() | |
| 23 | compass.enable() | |
| 24 | ||
| 25 | Clock.schedule_interval(self.update, 1.0 / 60) # 60 вызовов в секунду | |
| 26 | except: | |
| 27 | self.lblAcce.text = "Перезапустите приложение" | |
| 28 | def update(self, dt): | |
| 29 | txt = "" | |
| 30 | try: | |
| 31 | accel = accelerometer.acceleration | |
| 32 | gyro = gyroscope.rotation | |
| 33 | gps = compass.field | |
| 34 | txt = "Акселерометр:\nX = %.2f\nY = %.2f\nZ = %2.f " % ( | |
| 35 | accel[0], # read the X value | |
| 36 | accel[1], # Y | |
| 37 | accel[2]) + \ | |
| 38 | "\n\nГироскоп:\nX = %.2f\nY = %.2f\nZ = %2.f " % ( | |
| 39 | gyro[0], | |
| 40 | gyro[1], | |
| 41 | gyro[2]) + \ | |
| 42 | "\n\nКомпасс: \nX = %.2f\nY = %.2f\nZ = %2.f " % ( | |
| 43 | gps[0], | |
| 44 | gps[1], | |
| 45 | gps[2]) | |
| 46 | except: | |
| 47 | txt = "Перезапустите приложение" # error | |
| 48 | self.lblAcce.text = txt # add the correct text | |
| 49 | ||
| 50 | ||
| 51 | class Sensors(App): # our app | |
| 52 | def build(self): | |
| 53 | ui = UI() # create the UI | |
| 54 | return ui # show it | |
| 55 | ||
| 56 | ||
| 57 | if __name__ == '__main__': | |
| 58 | Sensors().run() # start our app |