From a5362d719e1752ea0a14709ed1878cdcbc646d44 Mon Sep 17 00:00:00 2001 From: rgarcia-herrera Date: Tue, 4 Aug 2020 21:52:20 -0500 Subject: [PATCH] keep state on root.parent --- flockompass.kv | 60 ++++++++++------------------ main.py | 103 +++++++++++++++++++++++++++---------------------- 2 files changed, 78 insertions(+), 85 deletions(-) diff --git a/flockompass.kv b/flockompass.kv index 51979ec..b532723 100644 --- a/flockompass.kv +++ b/flockompass.kv @@ -2,6 +2,10 @@ #:import RiseInTransition kivy.uix.screenmanager.RiseInTransition #:import facade plyer.compass +: + CompassScreen: + MapScreen: + : BoxLayout: @@ -13,15 +17,20 @@ ActionPrevious: title: 'Social Cycling' 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: important: True text: 'we ride together' - on_press: root.manager.current = 'compass' + on_press: root.manager.current = 'compass' ; root.choose_destination() MapView: id: mapview zoom: 15 - + on_map_relocated: centermark.lat = mapview.lat; centermark.lon = mapview.lon MapMarker: @@ -45,66 +54,39 @@ ActionPrevious: title: 'Social Cycling' 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: important: True text: 'set destination' 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: - 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: diff --git a/main.py b/main.py index 998f870..abb0813 100644 --- a/main.py +++ b/main.py @@ -14,12 +14,16 @@ from kivy.animation import Animation from math import floor import random -#from LatLon23 import LatLon, Latitude, Longitude +from pprint import pprint +root = None 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): x_calib = NumericProperty(0) @@ -30,13 +34,6 @@ class CompassScreen(Screen): gps_location = StringProperty() 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): needle_angle = 7 @@ -60,65 +57,78 @@ class CompassScreen(Screen): 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 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) - def stop(self): + def gps_stop(self): gps.stop() def on_pause(self): - gps.stop() + self.compass_disable() + self.gps_stop() return True def on_resume(self): - gps.start(1000, 0) + self.compass_enable() + self.gps_start(1000, 0) @mainthread 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), - # Longitude(76.59449171)) - - self.gps_location = '\n'.join([ - '{}={}'.format(k, v) for k, v in kwargs.items()]) -# self.gps_location += str(p.heading_initial(destination_point)) + self.parent.parent.lat = kwargs['lat'] + self.parent.parent.lon = kwargs['lon'] +# self.parent.speed = kwargs['speed'] +# self.parent.bearing = kwargs['bearing'] -# 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 = (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): - print('aguas on status') self.gps_status = 'type={}\n{}'.format(stype, status) - + class FlockompassApp(App): lat = 19.3419 lon = -99.1499 + dest_lat = 19.3429 + dest_lon = -99.15 + def move_around(self, t): 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) # where the marker's at - print( - self.ms.ids.centermark.lat, - self.ms.ids.centermark.lon - ) - + def request_android_permissions(self): from android.permissions import request_permissions, Permission @@ -133,33 +143,34 @@ class FlockompassApp(App): callback) 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() + + try: gps.configure(on_location=cs.on_location, on_status=cs.on_status) except NotImplementedError: import traceback traceback.print_exc() - self.gps_status = 'GPS is not implemented for your platform' + print('GPS is not implemented for your platform') - if platform == "android": - print("gps.py: Android detected. Requesting permissions") - self.request_android_permissions() - - - - root.add_widget(cs) - - Clock.schedule_interval(self.move_around, 2.0) + Clock.schedule_interval(self.move_around, 7.0) return root