keep state on root.parent

This commit is contained in:
rgarcia-herrera 2020-08-04 21:52:20 -05:00
parent 8c495ceb2f
commit a5362d719e
2 changed files with 78 additions and 85 deletions

View File

@ -2,6 +2,10 @@
#:import RiseInTransition kivy.uix.screenmanager.RiseInTransition #:import RiseInTransition kivy.uix.screenmanager.RiseInTransition
#:import facade plyer.compass #:import facade plyer.compass
<RootScreen>:
CompassScreen:
MapScreen:
<MapScreen>: <MapScreen>:
BoxLayout: BoxLayout:
@ -13,15 +17,20 @@
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' on_press: root.manager.current = 'compass' ; root.choose_destination()
MapView: MapView:
id: mapview id: mapview
zoom: 15 zoom: 15
on_map_relocated: centermark.lat = mapview.lat; centermark.lon = mapview.lon on_map_relocated: centermark.lat = mapview.lat; centermark.lon = mapview.lon
MapMarker: MapMarker:
@ -45,66 +54,39 @@
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'
BoxLayout:
orientation: 'horizontal'
size_hint_y: 0.07
Button:
id: enable_button
text: 'Enable Sensor'
disabled: False
on_release:
root.enable()
disable_button.disabled = not disable_button.disabled
enable_button.disabled = not enable_button.disabled
Button:
id: disable_button
text: 'Disable Sensor'
disabled: True
on_release:
root.disable()
disable_button.disabled = not disable_button.disabled
enable_button.disabled = not enable_button.disabled
Label:
text: root.gps_location
ToggleButton:
text: 'Start' if self.state == 'normal' else 'Stop'
on_state:
root.start(1000, 0) if self.state == 'down' else \
root.stop()
FloatLayout: FloatLayout:
canvas: canvas:
Color: Color:
rgb: .08, .08, .08 rgb: .08, .08, .08
Rectangle: Rectangle:
size: self.size size: self.size
Image: Image:
source: 'rose.png' source: 'rose.png'
Image: Image:
source: 'needle.png' source: 'needle.png'
canvas.before: canvas.before:
PushMatrix PushMatrix
Rotate: Rotate:
angle: root.needle_angle angle: root.needle_angle
axis: 0, 0, 1 axis: 0, 0, 1
origin: self.center origin: self.center
canvas.after: canvas.after:
PopMatrix PopMatrix
# BoxLayout: # BoxLayout:
# orientation: 'vertical' # orientation: 'vertical'
# canvas: # canvas:

103
main.py
View File

@ -14,12 +14,16 @@ from kivy.animation import Animation
from math import floor from math import floor
import random import random
#from LatLon23 import LatLon, Latitude, Longitude from pprint import pprint
root = None
class MapScreen(Screen): class MapScreen(Screen):
mapview = MapView()
def choose_destination(self):
root.parent.dest_lat = self.ids.centermark.lat
root.parent.dest_lon = self.ids.centermark.lon
class CompassScreen(Screen): class CompassScreen(Screen):
x_calib = NumericProperty(0) x_calib = NumericProperty(0)
@ -30,13 +34,6 @@ class CompassScreen(Screen):
gps_location = StringProperty() gps_location = StringProperty()
gps_status = StringProperty('Click Start to get GPS location updates') gps_status = StringProperty('Click Start to get GPS location updates')
def enable(self):
self.facade.enable()
Clock.schedule_interval(self.get_field, 1 / 5.)
def disable(self):
self.facade.disable()
Clock.unschedule(self.get_field)
def get_field(self, dt): def get_field(self, dt):
needle_angle = 7 needle_angle = 7
@ -60,65 +57,78 @@ class CompassScreen(Screen):
self._anim = Animation(needle_angle=needle_angle, d=.2, t='out_quad') self._anim = Animation(needle_angle=needle_angle, d=.2, t='out_quad')
self._anim.start(self) self._anim.start(self)
def sensors_start(self):
self.compass_enable()
self.gps_start(1000, 0)
def start(self, minTime, minDistance): 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) gps.start(minTime, minDistance)
def stop(self): def gps_stop(self):
gps.stop() gps.stop()
def on_pause(self): def on_pause(self):
gps.stop() self.compass_disable()
self.gps_stop()
return True return True
def on_resume(self): def on_resume(self):
gps.start(1000, 0) self.compass_enable()
self.gps_start(1000, 0)
@mainthread @mainthread
def on_location(self, **kwargs): def on_location(self, **kwargs):
print('aguas on location')
# print(kwargs)
# p = LatLon(Latitude(kwargs['lat']),
# Longitude(kwargs['lon']))
# destination_point = LatLon(Latitude(28.89335172), self.parent.parent.lat = kwargs['lat']
# Longitude(76.59449171)) self.parent.parent.lon = kwargs['lon']
# self.parent.speed = kwargs['speed']
self.gps_location = '\n'.join([ # self.parent.bearing = kwargs['bearing']
'{}={}'.format(k, v) for k, v in kwargs.items()])
# self.gps_location += str(p.heading_initial(destination_point))
# bearing = atan2(sin(long2-long1)*cos(lat2), cos(lat1)*sin(lat2)-sin(lat1)*cos(lat2)*cos(long2-long1)) # 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 = degrees(bearing)
# bearing = (bearing + 360) % 360 # bearing = (bearing + 360) % 360
# https://stackoverflow.com/questions/4913349/haversine-formula-in-python-bearing-and-distance-between-two-gps-points#4913653 # https://stackoverflow.com/questions/4913349/haversine-formula-in-python-bearing-and-distance-between-two-gps-points#4913653
@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): class FlockompassApp(App):
lat = 19.3419 lat = 19.3419
lon = -99.1499 lon = -99.1499
dest_lat = 19.3429
dest_lon = -99.15
def move_around(self, t): def move_around(self, t):
self.lat += random.uniform(-0.01,0.01) self.lat += random.uniform(-0.01,0.01)
self.lon += random.uniform(-0.01,0.01) self.lon += random.uniform(-0.01,0.01)
self.ms.ids.mapview.center_on(self.lat, self.lon) self.ms.ids.mapview.center_on(self.lat, self.lon)
# where the marker's at # where the marker's at
print(
self.ms.ids.centermark.lat,
self.ms.ids.centermark.lon
)
def request_android_permissions(self): def request_android_permissions(self):
from android.permissions import request_permissions, Permission from android.permissions import request_permissions, Permission
@ -133,33 +143,34 @@ class FlockompassApp(App):
callback) callback)
def build(self): def build(self):
global root
root = ScreenManager(transition=RiseInTransition()) root = ScreenManager(transition=RiseInTransition())
self.ms = MapScreen(name='map') self.ms = MapScreen(name='map')
root.add_widget(self.ms) root.add_widget(self.ms)
cs = CompassScreen(name='compass') cs = CompassScreen(name='compass')
cs._anim = None cs._anim = None
cs.p = None cs.p = None
root.add_widget(cs)
if platform == "android":
print("gps.py: Android detected. Requesting permissions")
self.request_android_permissions()
try: try:
gps.configure(on_location=cs.on_location, gps.configure(on_location=cs.on_location,
on_status=cs.on_status) on_status=cs.on_status)
except NotImplementedError: except NotImplementedError:
import traceback import traceback
traceback.print_exc() traceback.print_exc()
self.gps_status = 'GPS is not implemented for your platform' print('GPS is not implemented for your platform')
if platform == "android": Clock.schedule_interval(self.move_around, 7.0)
print("gps.py: Android detected. Requesting permissions")
self.request_android_permissions()
root.add_widget(cs)
Clock.schedule_interval(self.move_around, 2.0)
return root return root