Advertisement
Guest User

Untitled

a guest
Aug 11th, 2022
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.80 KB | None | 0 0
  1. # Write your code here :-)
  2. # https://github.com/adafruit/Adafruit_CircuitPython_VL53L1X/blob/main/adafruit_vl53l1x.py
  3. # https://github.com/adafruit/Adafruit_CircuitPython_BusDevice/blob/main/adafruit_bus_device/i2c_device.py
  4. import time
  5. import board
  6. import busio
  7. import digitalio
  8. import adafruit_vl53l1x
  9. from led import LED
  10. from i2cperipheral import I2CPeripheral
  11.  
  12.  
  13. fault = False
  14. led = LED()
  15. led.rainbow_cycle(0, halt='crit')
  16. I2CP = None
  17.  
  18.  
  19. try:
  20.     i2c = busio.I2C(board.SCL, board.SDA, frequency=400000)
  21. except Exception as err:
  22.     print(err)
  23.     fault = True
  24.  
  25.  
  26. if not fault:
  27.     sensors = {
  28.         'down': {
  29.             'pin': None,  # Remember this one will always be active so we set it first to get it off the default.
  30.             'address': 0x27
  31.         },
  32.         'up':{
  33.             'pin': board.D0,
  34.             'address': 0x2b
  35.         },
  36.         'left': {
  37.             'pin': board.D1,
  38.             'address': 0x2d
  39.         },
  40.         'right': {
  41.             'pin': board.D2,
  42.             'address': 0x2f
  43.         },
  44.         'front': {
  45.             'pin': board.D3,
  46.             'address': 0x31
  47.         },
  48.         'back': {
  49.             'pin': board.D6,
  50.             'address': 0x33
  51.         }
  52.     }
  53.  
  54.     led.rainbow_cycle(0, halt='cool')
  55.     try:
  56.         for sensor in sensors:  # Configure pins and set them low.
  57.             pin = sensors[sensor]['pin']
  58.             if pin is not None:
  59.                 pin = digitalio.DigitalInOut(pin)
  60.                 pin.switch_to_output(value=False)
  61.                 sensors[sensor]['pin'] = pin
  62.     except Exception as err:
  63.         fault = True
  64.         print(err)
  65.  
  66. # Configure devices.
  67.  
  68. def set_addr(addr, pin=None):
  69.     if pin is not None:
  70.         pin.value = True
  71.     dev = adafruit_vl53l1x.VL53L1X(i2c)
  72.     dev.set_address(addr)
  73.     return dev
  74.  
  75. # Set addresses.
  76.  
  77. if not fault:
  78.     led.rainbow_cycle(0, halt='warn')
  79.     try:
  80.         sensors['down']['device'] = set_addr(sensors['down']['address'])  # Set the first sensor's address as it's the only one not powered down.
  81.     except Exception as err:
  82.         fault = True
  83.         print('configuring down pin', err)
  84.  
  85.  
  86. if not fault:
  87.     for sensor in sensors:
  88.         device = sensors[sensor]
  89.         pin = device['pin']
  90.         address = device['address']
  91.         if pin is not None:
  92.             led.rainbow_cycle(0, halt='warn')
  93.             try:
  94.                 lidar = set_addr(address, pin)
  95.                 device['device'] = lidar
  96.             except Exception as err:
  97.                 led.rainbow_cycle(0, halt='fail')
  98.                 fault = True
  99.                 print('configuring pins', err)
  100.  
  101. # Check configuration.
  102.  
  103. if not fault:
  104.     for sensor in sensors:
  105.         led.rainbow_cycle(0, halt='err')
  106.         print('checking', sensor)
  107.         try:
  108.             device = sensors[sensor]
  109.             lidar = device['device']
  110.             lidar.start_ranging()
  111.             while not lidar.data_ready:
  112.                 pass
  113.             reading = lidar.distance
  114.             if reading is None:
  115.                 print('sensor failed to send data')
  116.                 break
  117.             print(reading)
  118.             lidar.stop_ranging()
  119.         except Exception as err:
  120.             fault = True
  121.             print('configuration check', err)
  122.         lidar.clear_interrupt()
  123.         pin.deinit()
  124.  
  125. sensors = None
  126.  
  127. while not i2c.try_lock():
  128.     pass
  129. print('scanning bus', i2c.scan())
  130.  
  131. if not fault:
  132.     try:
  133.         i2c.unlock()
  134.         i2c.deinit()
  135.         # Become i2c slave here.
  136.         I2CP = I2CPeripheral(board.SCL, board.SDA, (0x6e, 0x6f))
  137.     except Exception as err:
  138.         led.rainbow_cycle(0, halt='release')
  139.         fault = True
  140.         print('unlock', err)
  141.  
  142. if not fault:
  143.     led.rainbow_cycle(0, halt='good')
  144.     print('deviced ready to go, changing to peripheral')
  145.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement