code.py
 1  # SPDX-FileCopyrightText: 2020 Eva Herrada for Adafruit Industries
 2  #
 3  # SPDX-License-Identifier: MIT
 4  
 5  from time import time
 6  import adafruit_ble
 7  import board
 8  import pyloton
 9  
10  ble = adafruit_ble.BLERadio()    # pylint: disable=no-member
11  
12  CONNECTION_TIMEOUT = 45
13  
14  HEART = True
15  SPEED = True
16  CADENCE = True
17  AMS = True
18  DEBUG = False
19  
20  # 84.229 is wheel circumference (700x23 in my case)
21  pyloton = pyloton.Pyloton(ble, board.DISPLAY, 84.229, HEART, SPEED, CADENCE, AMS, DEBUG)
22  
23  pyloton.show_splash()
24  
25  ams = pyloton.ams_connect()
26  
27  
28  start = time()
29  hr_connection = None
30  speed_cadence_connections = []
31  while True:
32      if HEART:
33          if not hr_connection:
34              print("Attempting to connect to a heart rate monitor")
35              hr_connection = pyloton.heart_connect()
36              ble.stop_scan()
37      if SPEED or CADENCE:
38          if not speed_cadence_connections:
39              print("Attempting to connect to speed and cadence monitors")
40              speed_cadence_connections = pyloton.speed_cadence_connect()
41  
42      if time()-start >= CONNECTION_TIMEOUT:
43          pyloton.timeout()
44          break
45      # Stop scanning whether or not we are connected.
46      ble.stop_scan()
47  
48      #pylint: disable=too-many-boolean-expressions
49      if ((not HEART or (hr_connection and hr_connection.connected)) and
50              ((not SPEED and not CADENCE) or
51               (speed_cadence_connections and speed_cadence_connections[0].connected)) and
52              (not AMS or (ams and ams.connected))):
53          break
54  
55  pyloton.setup_display()
56  
57  while ((not HEART or hr_connection.connected) and
58         ((not SPEED or not CADENCE) or
59          (speed_cadence_connections and speed_cadence_connections[0].connected)) and
60         (not AMS or ams.connected)):
61      pyloton.update_display()
62      pyloton.ams_remote()
63  
64  print("\n\nNot all sensors are connected. Please reset to try again\n\n")