|
| 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(36) |
| 26 | +SPI_CLK = const(37) |
| 27 | + |
| 28 | +# I2C |
| 29 | +I2C_SDA = const(38) |
| 30 | +I2C_SCL = const(33) |
| 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 | +# LED & Ambient Light Sensor control |
| 43 | +def set_led(state): |
| 44 | + l = Pin(LED, Pin.OUT) |
| 45 | + l.value(state) |
| 46 | + |
| 47 | + |
| 48 | +def toggle_led(state): |
| 49 | + l = Pin(LED, Pin.OUT) |
| 50 | + l.value(not l.value()) |
| 51 | + |
| 52 | + |
| 53 | +# Create ADC and set attenuation and return teh ambient light value from the onboard sensor |
| 54 | +def get_amb_light(): |
| 55 | + adc = ADC(Pin(AMB_LIGHT)) |
| 56 | + adc.atten(ADC.ATTN_11DB) |
| 57 | + return adc.read() |
| 58 | + |
| 59 | + |
| 60 | +# LDO2 power control |
| 61 | +# When we manually turn off the second LDO we also set the DotStar DATA and CLK pins to input to |
| 62 | +# prevent parasitic power from lighting the LED even with the LDO off, causing current use. |
| 63 | +# The DotStar is a beautiful LED, but parasitic power makes it a terrible choice for battery use :( |
| 64 | +def set_ldo2_power(state): |
| 65 | + """Set the power for the on-board Dostar to allow no current draw when not needed.""" |
| 66 | + # Set the power pin to the inverse of state |
| 67 | + ldo2 = Pin(LDO2, Pin.OUT) |
| 68 | + ldo2.value(state) |
| 69 | + |
| 70 | + if state: |
| 71 | + Pin(DOTSTAR_CLK, Pin.OUT) |
| 72 | + Pin(DOTSTAR_DATA, Pin.OUT) # If power is on, set CLK to be output, otherwise input |
| 73 | + else: |
| 74 | + Pin(DOTSTAR_CLK, Pin.IN) |
| 75 | + Pin(DOTSTAR_DATA, Pin.IN) # If power is on, set CLK to be output, otherwise input |
| 76 | + |
| 77 | + # A small delay to let the IO change state |
| 78 | + time.sleep(0.035) |
| 79 | + |
| 80 | + |
| 81 | +# Dotstar rainbow colour wheel |
| 82 | +def dotstar_color_wheel(wheel_pos): |
| 83 | + """Color wheel to allow for cycling through the rainbow of RGB colors.""" |
| 84 | + wheel_pos = wheel_pos % 255 |
| 85 | + |
| 86 | + if wheel_pos < 85: |
| 87 | + return 255 - wheel_pos * 3, 0, wheel_pos * 3 |
| 88 | + elif wheel_pos < 170: |
| 89 | + wheel_pos -= 85 |
| 90 | + return 0, wheel_pos * 3, 255 - wheel_pos * 3 |
| 91 | + else: |
| 92 | + wheel_pos -= 170 |
| 93 | + return wheel_pos * 3, 255 - wheel_pos * 3, 0 |
| 94 | + |
| 95 | + |
| 96 | +# Go into deep sleep but shut down the APA first to save power |
| 97 | +# Use this if you want lowest deep sleep current |
| 98 | +def go_deepsleep(t): |
| 99 | + """Deep sleep helper that also powers down the on-board Dotstar.""" |
| 100 | + set_ldo2_power(False) |
| 101 | + machine.deepsleep(t) |
0 commit comments