| 1 |
# ProS3 MicroPython Helper Library |
| 2 |
# MIT license; Copyright (c) 2022 Seon Rozenblum - Unexpected Maker |
| 3 |
# |
| 4 |
# Project home: |
| 5 |
# http://pros3.io |
| 6 |
|
| 7 |
# Import required libraries |
| 8 |
from micropython import const |
| 9 |
from machine import Pin, ADC |
| 10 |
import time |
| 11 |
|
| 12 |
# ProS3 Hardware Pin Assignments |
| 13 |
|
| 14 |
# Sense Pins |
| 15 |
VBUS_SENSE = const(33) |
| 16 |
VBAT_SENSE = const(10) |
| 17 |
|
| 18 |
# RGB LED & LDO2 Pins |
| 19 |
RGB_DATA = const(18) |
| 20 |
LDO2 = const(17) |
| 21 |
|
| 22 |
# SPI |
| 23 |
SPI_MOSI = const(35) |
| 24 |
SPI_MISO = const(37) |
| 25 |
SPI_CLK = const(36) |
| 26 |
|
| 27 |
# I2C |
| 28 |
I2C_SDA = const(8) |
| 29 |
I2C_SCL = const(9) |
| 30 |
|
| 31 |
|
| 32 |
# Helper functions |
| 33 |
def set_ldo2_power(state): |
| 34 |
"""Enable or Disable power to the second LDO""" |
| 35 |
Pin(LDO2, Pin.OUT).value(state) |
| 36 |
|
| 37 |
|
| 38 |
def get_battery_voltage(): |
| 39 |
""" |
| 40 |
Returns the current battery voltage. If no battery is connected, returns 4.2V which is the charge voltage |
| 41 |
This is an approximation only, but useful to detect if the charge state of the battery is getting low. |
| 42 |
""" |
| 43 |
adc = ADC(Pin(VBAT_SENSE)) # Assign the ADC pin to read |
| 44 |
adc.atten(ADC.ATTN_2_5DB) # Needs 2.5DB attenuation for max voltage of 1.116V w/batt of 4.2V |
| 45 |
measuredvbat = adc.read() |
| 46 |
measuredvbat /= 3657 # Divide by 3657 as we are using 2.5dB attenuation, which is max input of 1.25V = 4095 counts |
| 47 |
measuredvbat *= 4.2 # Multiply by 4.2V, our max charge voltage for a 1S LiPo |
| 48 |
return round(measuredvbat, 2) |
| 49 |
|
| 50 |
|
| 51 |
def get_vbus_present(): |
| 52 |
"""Detect if VBUS (5V) power source is present""" |
| 53 |
return Pin(VBUS_SENSE, Pin.IN).value() == 1 |
| 54 |
|
| 55 |
|
| 56 |
# NeoPixel rainbow colour wheel |
| 57 |
def rgb_color_wheel(wheel_pos): |
| 58 |
"""Color wheel to allow for cycling through the rainbow of RGB colors.""" |
| 59 |
wheel_pos = wheel_pos % 255 |
| 60 |
|
| 61 |
if wheel_pos < 85: |
| 62 |
return 255 - wheel_pos * 3, 0, wheel_pos * 3 |
| 63 |
elif wheel_pos < 170: |
| 64 |
wheel_pos -= 85 |
| 65 |
return 0, wheel_pos * 3, 255 - wheel_pos * 3 |
| 66 |
else: |
| 67 |
wheel_pos -= 170 |
| 68 |
return wheel_pos * 3, 255 - wheel_pos * 3, 0 |