| 1 |
# TinyPICO MicroPython Helper Library |
| 2 |
# 2019 Seon Rozenblum, Matt Trentini |
| 3 |
# |
| 4 |
# Project home: |
| 5 |
# https://github.com/TinyPICO |
| 6 |
# |
| 7 |
# 2019-Mar-12 - v0.1 - Initial implementation |
| 8 |
# 2019-May-20 - v1.0 - Initial Release |
| 9 |
# 2019-Oct-23 - v1.1 - Removed temp sensor code, prep for frozen modules |
| 10 |
|
| 11 |
# Import required libraries |
| 12 |
from micropython import const |
| 13 |
from machine import Pin, SPI, ADC |
| 14 |
import machine, time, esp32 |
| 15 |
|
| 16 |
# TinyPICO Hardware Pin Assignments |
| 17 |
|
| 18 |
# Battery |
| 19 |
BAT_VOLTAGE = const(35) |
| 20 |
BAT_CHARGE = const(34) |
| 21 |
|
| 22 |
# APA102 Dotstar pins for production boards |
| 23 |
DOTSTAR_CLK = const(12) |
| 24 |
DOTSTAR_DATA = const(2) |
| 25 |
DOTSTAR_PWR = const(13) |
| 26 |
|
| 27 |
# SPI |
| 28 |
SPI_MOSI = const(23) |
| 29 |
SPI_CLK = const(18) |
| 30 |
SPI_MISO = const(19) |
| 31 |
|
| 32 |
# I2C |
| 33 |
I2C_SDA = const(21) |
| 34 |
I2C_SCL = const(22) |
| 35 |
|
| 36 |
# DAC |
| 37 |
DAC1 = const(25) |
| 38 |
DAC2 = const(26) |
| 39 |
|
| 40 |
# Helper functions |
| 41 |
|
| 42 |
|
| 43 |
# Get a *rough* estimate of the current battery voltage |
| 44 |
# If the battery is not present, the charge IC will still report it's trying to charge at X voltage |
| 45 |
# so it will still show a voltage. |
| 46 |
def get_battery_voltage(): |
| 47 |
""" |
| 48 |
Returns the current battery voltage. If no battery is connected, returns 3.7V |
| 49 |
This is an approximation only, but useful to detect of the charge state of the battery is getting low. |
| 50 |
""" |
| 51 |
adc = ADC(Pin(BAT_VOLTAGE)) # Assign the ADC pin to read |
| 52 |
measuredvbat = adc.read() # Read the value |
| 53 |
measuredvbat /= 4095 # divide by 4095 as we are using the default ADC voltage range of 0-1V |
| 54 |
measuredvbat *= 3.7 # Multiply by 3.7V, our reference voltage |
| 55 |
return measuredvbat |
| 56 |
|
| 57 |
|
| 58 |
# Return the current charge state of the battery - we need to read the value multiple times |
| 59 |
# to eliminate false negatives due to the charge IC not knowing the difference between no battery |
| 60 |
# and a full battery not charging - This is why the charge LED flashes |
| 61 |
def get_battery_charging(): |
| 62 |
""" |
| 63 |
Returns the current battery charging state. |
| 64 |
This can trigger false positives as the charge IC can't tell the difference between a full battery or no battery connected. |
| 65 |
""" |
| 66 |
measuredVal = 0 # start our reading at 0 |
| 67 |
io = Pin(BAT_CHARGE, Pin.IN) # Assign the pin to read |
| 68 |
|
| 69 |
for y in range( |
| 70 |
0, 10 |
| 71 |
): # loop through 10 times adding the read values together to ensure no false positives |
| 72 |
measuredVal += io.value() |
| 73 |
|
| 74 |
return measuredVal == 0 # return True if the value is 0 |
| 75 |
|
| 76 |
|
| 77 |
# Power to the on-board Dotstar is controlled by a PNP transistor, so low is ON and high is OFF |
| 78 |
# We also need to set the Dotstar clock and data pins to be inputs to prevent power leakage when power is off |
| 79 |
# This might be improved at a future date |
| 80 |
# The reason we have power control for the Dotstar is that it has a quiescent current of around 1mA, so we |
| 81 |
# need to be able to cut power to it to minimise power consumption during deep sleep or with general battery powered use |
| 82 |
# to minimise unneeded battery drain |
| 83 |
def set_dotstar_power(state): |
| 84 |
"""Set the power for the on-board Dotstar to allow no current draw when not needed.""" |
| 85 |
# Set the power pin to the inverse of state |
| 86 |
if state: |
| 87 |
Pin(DOTSTAR_PWR, Pin.OUT, None, value=0) # Drive output to LOW to enable transistor |
| 88 |
else: |
| 89 |
Pin(DOTSTAR_PWR, Pin.IN, None) # Disable output, external pull-up will disable transistor |
| 90 |
|
| 91 |
Pin( |
| 92 |
DOTSTAR_CLK, Pin.OUT if state else Pin.IN |
| 93 |
) # If power is on, set CLK to be output, otherwise input |
| 94 |
Pin( |
| 95 |
DOTSTAR_DATA, Pin.OUT if state else Pin.IN |
| 96 |
) # If power is on, set DATA to be output, otherwise input |
| 97 |
|
| 98 |
# A small delay to let the IO change state |
| 99 |
time.sleep(0.035) |
| 100 |
|
| 101 |
|
| 102 |
# Dotstar rainbow colour wheel |
| 103 |
def dotstar_color_wheel(wheel_pos): |
| 104 |
"""Color wheel to allow for cycling through the rainbow of RGB colors.""" |
| 105 |
wheel_pos = wheel_pos % 255 |
| 106 |
|
| 107 |
if wheel_pos < 85: |
| 108 |
return 255 - wheel_pos * 3, 0, wheel_pos * 3 |
| 109 |
elif wheel_pos < 170: |
| 110 |
wheel_pos -= 85 |
| 111 |
return 0, wheel_pos * 3, 255 - wheel_pos * 3 |
| 112 |
else: |
| 113 |
wheel_pos -= 170 |
| 114 |
return wheel_pos * 3, 255 - wheel_pos * 3, 0 |
| 115 |
|
| 116 |
|
| 117 |
# Go into deep sleep but shut down the APA first to save power |
| 118 |
# Use this if you want lowest deep sleep current |
| 119 |
def go_deepsleep(t): |
| 120 |
"""Deep sleep helper that also powers down the on-board Dotstar.""" |
| 121 |
set_dotstar_power(False) |
| 122 |
machine.deepsleep(t) |