cannibalize plyer gps example

This commit is contained in:
rgarcia-herrera 2020-07-16 21:12:36 -05:00
parent 586f1ebcfd
commit 1d04daef0f
4 changed files with 45 additions and 52 deletions

View File

@ -1,7 +1,7 @@
[app] [app]
# (str) Title of your application # (str) Title of your application
title = Flockompass title = flockompass
# (str) Package name # (str) Package name
package.name = flockompass package.name = flockompass
@ -36,7 +36,7 @@ version = 0.1
# (list) Application requirements # (list) Application requirements
# comma separated e.g. requirements = sqlite3,kivy # comma separated e.g. requirements = sqlite3,kivy
requirements = python3,kivy,hostpython3,mapview,plyer,requests,urllib3,chardet,idna requirements = hostpython3, python3, kivy, plyer, android, mapview, requests, urllib3, chardet, idna
# (str) Custom source folders for requirements # (str) Custom source folders for requirements
# Sets custom source for any requirements with recipes # Sets custom source for any requirements with recipes
@ -85,7 +85,7 @@ fullscreen = 0
#android.presplash_color = #FFFFFF #android.presplash_color = #FFFFFF
# (list) Permissions # (list) Permissions
#android.permissions = INTERNET android.permissions = INTERNET,ACCESS_FINE_LOCATION,ACCESS_COARSE_LOCATION
# (int) Target Android API, should be as high as possible. # (int) Target Android API, should be as high as possible.
#android.api = 27 #android.api = 27

View File

@ -62,24 +62,12 @@
root.disable() root.disable()
disable_button.disabled = not disable_button.disabled disable_button.disabled = not disable_button.disabled
enable_button.disabled = not enable_button.disabled enable_button.disabled = not enable_button.disabled
BoxLayout:
orientation: 'vertical'
Label:
text: "Earth's Magnetic Field"
Label:
text: 'including hard iron calibration'
Label:
text: '(' + str(root.x_calib) + ','
Label:
text: str(root.y_calib) + ','
Label:
text: str(root.z_calib) + ')'
Label: Label:
text: app.gps_location text: root.gps_location
Label: Label:
text: app.gps_status text: root.gps_status
BoxLayout: BoxLayout:
size_hint_y: None size_hint_y: None
@ -89,10 +77,11 @@
ToggleButton: ToggleButton:
text: 'Start' if self.state == 'normal' else 'Stop' text: 'Start' if self.state == 'normal' else 'Stop'
on_state: on_state:
app.start(1000, 0) if self.state == 'down' else \ root.start(1000, 0) if self.state == 'down' else \
app.stop() root.stop()
FloatLayout: FloatLayout:
canvas: canvas:

68
main.py
View File

@ -1,12 +1,14 @@
from plyer import gps
from kivy.app import App from kivy.app import App
from kivy.clock import Clock from kivy.clock import Clock
from kivy.uix.screenmanager import ScreenManager, Screen, RiseInTransition
from kivy.properties import NumericProperty, StringProperty from kivy.properties import NumericProperty, StringProperty
from kivy.lang import Builder
from kivy_garden.mapview import MapView
from plyer import gps
from kivy.clock import mainthread from kivy.clock import mainthread
from kivy.utils import platform from kivy.utils import platform
from kivy.uix.screenmanager import ScreenManager, Screen, RiseInTransition
from kivy_garden.mapview import MapView
from kivy.vector import Vector from kivy.vector import Vector
from kivy.animation import Animation from kivy.animation import Animation
from math import floor from math import floor
@ -23,12 +25,13 @@ class CompassScreen(Screen):
y_calib = NumericProperty(0) y_calib = NumericProperty(0)
z_calib = NumericProperty(0) z_calib = NumericProperty(0)
needle_angle = NumericProperty(0) needle_angle = NumericProperty(0)
gps_location = StringProperty()
gps_status = StringProperty('Click Start to get GPS location updates')
def enable(self): def enable(self):
self.facade.enable() self.facade.enable()
Clock.schedule_interval(self.get_field, 1 / 20.) Clock.schedule_interval(self.get_field, 1 / 20.)
gps.start(1000, 0)
def disable(self): def disable(self):
self.facade.disable() self.facade.disable()
@ -57,25 +60,6 @@ class CompassScreen(Screen):
self._anim.start(self) self._anim.start(self)
class FlockompassApp(App):
gps_location = StringProperty()
gps_status = StringProperty('Click Start to get GPS location updates')
def request_android_permissions(self):
from android.permissions import request_permissions, Permission
def callback(permissions, results):
if all([res for res in results]):
print("callback. All permissions granted.")
else:
print("callback. Some permissions refused.")
request_permissions([Permission.ACCESS_COARSE_LOCATION,
Permission.ACCESS_FINE_LOCATION],
callback)
def start(self, minTime, minDistance): def start(self, minTime, minDistance):
gps.start(minTime, minDistance) gps.start(minTime, minDistance)
@ -89,22 +73,46 @@ class FlockompassApp(App):
def on_resume(self): def on_resume(self):
gps.start(1000, 0) gps.start(1000, 0)
@mainthread @mainthread
def on_location(self, **kwargs): def on_location(self, **kwargs):
print('aguas on location')
print(kwargs)
self.gps_location = '\n'.join([ self.gps_location = '\n'.join([
'{}={}'.format(k, v) for k, v in kwargs.items()]) '{}={}'.format(k, v) for k, v in kwargs.items()])
@mainthread @mainthread
def on_status(self, stype, status): def on_status(self, stype, status):
print('aguas on status')
self.gps_status = 'type={}\n{}'.format(stype, status) self.gps_status = 'type={}\n{}'.format(stype, status)
class FlockompassApp(App):
def request_android_permissions(self):
from android.permissions import request_permissions, Permission
def callback(permissions, results):
if all([res for res in results]):
print("aguas: callback. All permissions granted.")
else:
print("aguas: callback. Some permissions refused.Permission.ACCESS_FINE_LOCATION")
request_permissions([Permission.ACCESS_COARSE_LOCATION,
Permission.ACCESS_FINE_LOCATION],
callback)
def build(self): def build(self):
root = ScreenManager(transition=RiseInTransition())
root.add_widget(MapScreen(name='map'))
cs = CompassScreen(name='compass')
cs._anim = None
try: try:
gps.configure(on_location=self.on_location, gps.configure(on_location=cs.on_location,
on_status=self.on_status) on_status=cs.on_status)
except NotImplementedError: except NotImplementedError:
import traceback import traceback
traceback.print_exc() traceback.print_exc()
@ -114,10 +122,6 @@ class FlockompassApp(App):
print("gps.py: Android detected. Requesting permissions") print("gps.py: Android detected. Requesting permissions")
self.request_android_permissions() self.request_android_permissions()
root = ScreenManager(transition=RiseInTransition())
root.add_widget(MapScreen(name='map'))
cs = CompassScreen(name='compass')
cs._anim = None
root.add_widget(cs) root.add_widget(cs)
return root return root

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.4 KiB

After

Width:  |  Height:  |  Size: 3.4 KiB