| 1 |
# FeatherS2 MicroPython Helper Library |
| 2 |
# 2021 Seon Rozenblum - Unexpected Maker |
| 3 |
# |
| 4 |
# Project home: |
| 5 |
# https://feathers2.io |
| 6 |
# |
| 7 |
# 2021-Mar-21 - v0.1 - Initial implementation |
| 8 |
|
| 9 |
# Import required libraries |
| 10 |
from micropython import const |
| 11 |
from machine import Pin, SPI, ADC |
| 12 |
import machine, time |
| 13 |
|
| 14 |
# FeatherS2 Hardware Pin Assignments |
| 15 |
|
| 16 |
# LDO |
| 17 |
LDO2 = const(21) |
| 18 |
|
| 19 |
# APA102 Dotstar pins |
| 20 |
DOTSTAR_CLK = const(45) |
| 21 |
DOTSTAR_DATA = const(40) |
| 22 |
|
| 23 |
# SPI |
| 24 |
SPI_MOSI = const(35) |
| 25 |
SPI_MISO = const(37) |
| 26 |
SPI_CLK = const(36) |
| 27 |
|
| 28 |
# I2C |
| 29 |
I2C_SDA = const(8) |
| 30 |
I2C_SCL = const(9) |
| 31 |
|
| 32 |
# DAC |
| 33 |
DAC1 = const(17) |
| 34 |
DAC2 = const(18) |
| 35 |
|
| 36 |
# LED & Ambient Light Sensor |
| 37 |
LED = const(13) |
| 38 |
AMB_LIGHT = const(4) |
| 39 |
|
| 40 |
# Helper functions |
| 41 |
|
| 42 |
|
| 43 |
# LED & Ambient Light Sensor control |
| 44 |
def set_led(state): |
| 45 |
l = Pin(LED, Pin.OUT) |
| 46 |
l.value(state) |
| 47 |
|
| 48 |
|
| 49 |
def toggle_led(state): |
| 50 |
l = Pin(LED, Pin.OUT) |
| 51 |
l.value(not l.value()) |
| 52 |
|
| 53 |
|
| 54 |
# Create ADC and set attenuation and return the ambient light value from the onboard sensor |
| 55 |
def get_amb_light(): |
| 56 |
adc = ADC(Pin(AMB_LIGHT)) |
| 57 |
adc.atten(ADC.ATTN_11DB) |
| 58 |
return adc.read() |
| 59 |
|
| 60 |
|
| 61 |
# LDO2 power control |
| 62 |
# When we manually turn off the second LDO we also set the DotStar DATA and CLK pins to input to |
| 63 |
# prevent parasitic power from lighting the LED even with the LDO off, causing current use. |
| 64 |
# The DotStar is a beautiful LED, but parasitic power makes it a terrible choice for battery use :( |
| 65 |
def set_ldo2_power(state): |
| 66 |
"""Set the power for the on-board Dotstar to allow no current draw when not needed.""" |
| 67 |
# Set the power pin to the inverse of state |
| 68 |
ldo2 = Pin(LDO2, Pin.OUT) |
| 69 |
ldo2.value(state) |
| 70 |
|
| 71 |
if state: |
| 72 |
Pin(DOTSTAR_CLK, Pin.OUT) |
| 73 |
Pin(DOTSTAR_DATA, Pin.OUT) # If power is on, set CLK to be output, otherwise input |
| 74 |
else: |
| 75 |
Pin(DOTSTAR_CLK, Pin.IN) |
| 76 |
Pin(DOTSTAR_DATA, Pin.IN) # If power is on, set CLK to be output, otherwise input |
| 77 |
|
| 78 |
# A small delay to let the IO change state |
| 79 |
time.sleep(0.035) |
| 80 |
|
| 81 |
|
| 82 |
# Dotstar rainbow colour wheel |
| 83 |
def dotstar_color_wheel(wheel_pos): |
| 84 |
"""Color wheel to allow for cycling through the rainbow of RGB colors.""" |
| 85 |
wheel_pos = wheel_pos % 255 |
| 86 |
|
| 87 |
if wheel_pos < 85: |
| 88 |
return 255 - wheel_pos * 3, 0, wheel_pos * 3 |
| 89 |
elif wheel_pos < 170: |
| 90 |
wheel_pos -= 85 |
| 91 |
return 0, wheel_pos * 3, 255 - wheel_pos * 3 |
| 92 |
else: |
| 93 |
wheel_pos -= 170 |
| 94 |
return wheel_pos * 3, 255 - wheel_pos * 3, 0 |
| 95 |
|
| 96 |
|
| 97 |
# Go into deep sleep but shut down the APA first to save power |
| 98 |
# Use this if you want lowest deep sleep current |
| 99 |
def go_deepsleep(t): |
| 100 |
"""Deep sleep helper that also powers down the on-board Dotstar.""" |
| 101 |
set_ldo2_power(False) |
| 102 |
machine.deepsleep(t) |