keep important session variables in globaloid scope

This commit is contained in:
rgarcia-herrera 2020-08-10 20:47:11 -05:00
parent 1190adc472
commit f85ba17996
2 changed files with 97 additions and 212 deletions

View File

@ -1,10 +1,9 @@
#:import random random.random #:import random random.random
#:import RiseInTransition kivy.uix.screenmanager.RiseInTransition #:import RiseInTransition kivy.uix.screenmanager.RiseInTransition
#:import facade plyer.compass
<RootScreen>: <RootScreen>:
CompassScreen:
MapScreen: MapScreen:
CompassScreen:
<MapScreen>: <MapScreen>:
@ -17,31 +16,25 @@
ActionPrevious: ActionPrevious:
title: 'Social Cycling' title: 'Social Cycling'
with_previous: False with_previous: False
ActionToggleButton:
text: 'Start' if self.state == 'normal' else 'Stop'
on_state:
root.sensors_start() if self.state == 'down' else \
root.sensors_stop()
ActionButton: ActionButton:
important: True important: True
text: 'we ride together' text: 'we ride together'
on_press: root.manager.current = 'compass' ; root.choose_destination() on_press: root.manager.current = 'compass'; app.set_destination()
MapView: MapView:
id: mapview id: mapview
zoom: 15 zoom: 16
on_map_relocated: centermark.lat = mapview.lat; centermark.lon = mapview.lon on_map_relocated: centermark.lat = mapview.lat; centermark.lon = mapview.lon
MapMarker: MapMarker:
source: 'marker.png' source: 'marker.png'
id: centermark id: centermark
anchor_x: 0.2 #anchor_x: 0.2
anchor_y: 0.3 #anchor_y: 0.3
<CompassScreen>: <CompassScreen>:
facade: facade
orientation: 'vertical' orientation: 'vertical'
padding: '20dp' padding: '20dp'
spacing: '10dp' spacing: '10dp'
@ -54,70 +47,13 @@
ActionPrevious: ActionPrevious:
title: 'Social Cycling' title: 'Social Cycling'
with_previous: False with_previous: False
ActionToggleButton:
text: 'Start' if self.state == 'normal' else 'Stop'
on_state:
root.sensors_start() if self.state == 'down' else \
root.sensors_stop()
ActionButton: ActionButton:
important: True important: True
text: 'set destination' text: 'set destination'
on_press: root.manager.current = 'map' on_press: root.manager.current = 'map'; app.cambia()
FloatLayout:
canvas:
Color:
rgb: .08, .08, .08
Rectangle:
size: self.size
Image:
source: 'rose.png'
Image:
source: 'needle.png'
canvas.before:
PushMatrix
Rotate:
angle: root.needle_angle
axis: 0, 0, 1
origin: self.center
canvas.after:
PopMatrix
# BoxLayout:
# orientation: 'vertical'
# canvas:
# Color:
# rgb: .98, .98, .98
# Rectangle:
# size: self.size
# Image:
# source: 'rose.png'
# Image:
# source: 'needle.png'
# ActionBar: Button:
# pos_hint: {'top':1} text: str(app.gps_data)
# ActionView: on_press: app.ms.cambia()
# use_separator: True
# ActionPrevious:
# title: 'Social Cycling'
# with_previous: False
# ActionButton:
# important: True
# text: 'set destination'
# on_press: root.manager.current = 'map'
# Label:
# text: app.gps_location
# Label:
# text: app.gps_status

213
main.py
View File

@ -1,7 +1,7 @@
from plyer import gps 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.properties import NumericProperty, StringProperty from kivy.properties import DictProperty
from kivy.clock import mainthread from kivy.clock import mainthread
from kivy.utils import platform from kivy.utils import platform
@ -13,120 +13,54 @@ from kivy.vector import Vector
from kivy.animation import Animation from kivy.animation import Animation
from math import floor from math import floor
import random import random
from time import sleep
from pprint import pprint from pprint import pprint
root = None
class MapScreen(Screen): class MapScreen(Screen):
def choose_destination(self): def cambia(self):
root.parent.dest_lat = self.ids.centermark.lat print(self.app_label)
root.parent.dest_lon = self.ids.centermark.lon if self.app_label == 'ms hola':
self.app_label = 'ms adios'
else:
self.app_label = 'ms hola'
print(self.app_label)
class CompassScreen(Screen): class CompassScreen(Screen):
x_calib = NumericProperty(0)
y_calib = NumericProperty(0)
z_calib = NumericProperty(0)
needle_angle = NumericProperty(0) def cambia(self):
gps_location = StringProperty() print(self.app_label)
gps_status = StringProperty('Click Start to get GPS location updates') if self.app_label == 'cs hola':
self.app_label = 'cs adios'
else:
def get_field(self, dt): self.app_label = 'cs hola'
needle_angle = 7 print(self.app_label)
if self.facade.field != (None, None, None):
self.x_calib, self.y_calib, self.z_calib = self.facade.field
x, y, z = self.facade.field
needle_angle = Vector(x, y).angle((0, 1)) + 90.
# fix animation transition around the unit circle
if (self.needle_angle % 360) - needle_angle > 180:
needle_angle += 360
elif (self.needle_angle % 360) - needle_angle < -180:
needle_angle -= 360
# add the number of revolutions to the result
needle_angle += 360 * floor(self.needle_angle / 360.)
# animate the needle
if self._anim:
self._anim.stop(self)
self._anim = Animation(needle_angle=needle_angle, d=.2, t='out_quad')
self._anim.start(self)
def sensors_start(self):
self.compass_enable()
self.gps_start(1000, 0)
def sensors_stop(self):
self.compass_disable()
self.gps_stop()
def compass_enable(self):
self.facade.enable()
Clock.schedule_interval(self.get_field, 1 / 5.0)
def compass_disable(self):
self.facade.disable()
Clock.unschedule(self.get_field)
def gps_start(self, minTime, minDistance):
gps.start(minTime, minDistance)
def gps_stop(self):
gps.stop()
def on_pause(self):
self.compass_disable()
self.gps_stop()
return True
def on_resume(self):
self.compass_enable()
self.gps_start(1000, 0)
@mainthread
def on_location(self, **kwargs):
self.parent.parent.lat = kwargs['lat']
self.parent.parent.lon = kwargs['lon']
# self.parent.speed = kwargs['speed']
# self.parent.bearing = kwargs['bearing']
# self.gps_location = '\n'.join([
# '{}={}'.format(k, v) for k, v in kwargs.items()])
# bearing = atan2(sin(long2-long1)*cos(lat2), cos(lat1)*sin(lat2)-sin(lat1)*cos(lat2)*cos(long2-long1))
# bearing = degrees(bearing)
# bearing = (bearing + 360) % 360
# https://stackoverflow.com/questions/4913349/haversine-formula-in-python-bearing-and-distance-between-two-gps-points#4913653
@mainthread
def on_status(self, stype, status):
self.gps_status = 'type={}\n{}'.format(stype, status)
class PruebitaApp(App):
class FlockompassApp(App): gps_data = DictProperty()
session_data = DictProperty()
lat = 19.3419 def dump(self, dt):
lon = -99.1499 print(dt, self.gps_data, self.session_data)
dest_lat = 19.3429 def set_destination(self):
dest_lon = -99.15 self.session_data['lat'] = self.ms.ids.centermark.lat
self.session_data['lon'] = self.ms.ids.centermark.lon
def move_around(self, t): def cambia(self):
self.lat += random.uniform(-0.01,0.01) print(self.app_label)
self.lon += random.uniform(-0.01,0.01) if self.app_label == 'hola':
self.app_label = 'adios'
self.ms.ids.mapview.center_on(self.lat, self.lon) else:
self.app_label = 'hola'
# where the marker's at print(self.app_label)
def request_android_permissions(self): def request_android_permissions(self):
@ -136,55 +70,70 @@ class FlockompassApp(App):
if all([res for res in results]): if all([res for res in results]):
print("aguas: callback. All permissions granted.") print("aguas: callback. All permissions granted.")
else: else:
print("aguas: callback. Some permissions refused.Permission.ACCESS_FINE_LOCATION") print("aguas: callback. Some permissions refused.", results)
request_permissions([Permission.ACCESS_COARSE_LOCATION, request_permissions([Permission.ACCESS_COARSE_LOCATION,
Permission.ACCESS_FINE_LOCATION], Permission.ACCESS_FINE_LOCATION],
callback) callback)
def gps_start(self, minTime, minDistance):
gps.start(minTime, minDistance)
def gps_stop(self):
gps.stop()
def on_pause(self):
self.gps_stop()
return True
def on_resume(self):
self.gps_start(1000, 0)
@mainthread
def on_location(self, **kwargs):
self.gps_data = kwargs
def build(self): def build(self):
global root
root = ScreenManager(transition=RiseInTransition())
self.ms = MapScreen(name='map')
root.add_widget(self.ms)
cs = CompassScreen(name='compass')
cs._anim = None
cs.p = None
root.add_widget(cs)
if platform == "android":
print("gps.py: Android detected. Requesting permissions")
self.request_android_permissions()
# start GPS
try: try:
gps.configure(on_location=cs.on_location, gps.configure(on_location=self.on_location)
on_status=cs.on_status) #on_status=self.on_status)
except NotImplementedError: except NotImplementedError:
import traceback import traceback
traceback.print_exc() traceback.print_exc()
print('GPS is not implemented for your platform') print('GPS is not implemented for your platform')
Clock.schedule_interval(self.move_around, 7.0) if platform == "android":
print("gps.py: Android detected. Requesting permissions")
self.request_android_permissions()
self.gps_start(1000, 0)
# setup app screens
screen_manager = ScreenManager(transition=RiseInTransition())
self.ms = MapScreen(name='map')
self.ms.app_label = 'ms hola'
screen_manager.add_widget(self.ms)
self.cs = CompassScreen(name='compass')
self.cs.app_label = 'cs hola'
screen_manager.add_widget(self.cs)
self.app_label = 'hola'
Clock.schedule_interval(self.dump, 2.0)
return screen_manager
return root
if __name__ == '__main__': if __name__ == '__main__':
FlockompassApp().run() app = PruebitaApp()
app.run()
# Button:
# text: 'Use FallOutTransition'
# on_release: root.manager.transition = FallOutTransition()
# Button:
# text: 'Use RiseInTransition'
# on_release: root.manager.transition = RiseInTransition()