|
| 1 | +# TinyS2 Helper Library |
| 2 | +# 2021 Seon Rozenblum, Unexpected Maker |
| 3 | +# |
| 4 | +# Project home: |
| 5 | +# https://tinys2.io |
| 6 | +# |
| 7 | + |
| 8 | +# Import required libraries |
| 9 | +import time |
| 10 | +import board |
| 11 | +from digitalio import DigitalInOut, Direction, Pull |
| 12 | +from analogio import AnalogIn |
| 13 | + |
| 14 | +# Setup the NeoPixel power pin |
| 15 | +#pixel_power = DigitalInOut(board.NEOPIXEL_POWER) |
| 16 | +#pixel_power.direction = Direction.OUTPUT |
| 17 | + |
| 18 | +# Setup the BATTERY voltage sense pin |
| 19 | +vbat_voltage = AnalogIn(board.BATTERY) |
| 20 | + |
| 21 | +# Setup the VBUS sense pin |
| 22 | +vbus_sense = DigitalInOut(board.VBUS_SENSE) |
| 23 | +vbus_sense.direction = Direction.INPUT |
| 24 | + |
| 25 | + |
| 26 | +# Obstacle declaration |
| 27 | +obstacleCmd = DigitalInOut(board.IO33) |
| 28 | +obstacleCmd.direction = Direction.OUTPUT |
| 29 | + |
| 30 | +obstacleInput = [AnalogIn(board.IO4), AnalogIn(board.IO5), AnalogIn(board.IO6), AnalogIn(board.IO7)] |
| 31 | + |
| 32 | +# Helper functions |
| 33 | +def set_pixel_power(state): |
| 34 | + """Enable or Disable power to the onboard NeoPixel to either show colour, or to reduce power fro deep sleep.""" |
| 35 | + global pixel_power |
| 36 | + pixel_power.value = state |
| 37 | + |
| 38 | +def get_battery_voltage(): |
| 39 | + """Get the approximate battery voltage.""" |
| 40 | + # I don't really understand what CP is doing under the hood here for the ADC range & calibration, |
| 41 | + # but the onboard voltage divider for VBAT sense is setup to deliver 1.1V to the ADC based on it's |
| 42 | + # default factory configuration. |
| 43 | + # This forumla should show the nominal 4.2V max capacity (approximately) when 5V is present and the |
| 44 | + # VBAT is in charge state for a 1S LiPo battery with a max capacity of 4.2V |
| 45 | + global vbat_voltage |
| 46 | + return (vbat_voltage.value / 5371) |
| 47 | + |
| 48 | +def get_vbus_present(): |
| 49 | + """Detect if VBUS (5V) power source is present""" |
| 50 | + global vbus_sense |
| 51 | + return vbus_sense.value |
| 52 | + |
| 53 | +def rgb_color_wheel(wheel_pos): |
| 54 | + """Color wheel to allow for cycling through the rainbow of RGB colors.""" |
| 55 | + wheel_pos = wheel_pos % 255 |
| 56 | + |
| 57 | + if wheel_pos < 85: |
| 58 | + return 255 - wheel_pos * 3, 0, wheel_pos * 3 |
| 59 | + elif wheel_pos < 170: |
| 60 | + wheel_pos -= 85 |
| 61 | + return 0, wheel_pos * 3, 255 - wheel_pos * 3 |
| 62 | + else: |
| 63 | + wheel_pos -= 170 |
| 64 | + return wheel_pos * 3, 255 - wheel_pos * 3, 0 |
| 65 | + |
| 66 | +def get_obstacle(obstacle_pos): |
| 67 | + obstacle_pos = obstacle_pos |
| 68 | + |
| 69 | + value = 0 |
| 70 | + |
| 71 | + value = obstacleInput[obstacle_pos].value |
| 72 | + |
| 73 | + if value < 10000: |
| 74 | + return True |
| 75 | + else : |
| 76 | + return False |
0 commit comments