Debian Codegraph @

ports/esp32/boards/UM_FEATHERS3/modules/feathers3.py

raw
1 # FeatherS3 MicroPython Helper Library
2 # MIT license; Copyright (c) 2022 Seon Rozenblum - Unexpected Maker
3 #
4 # Project home:
5 # http://feathers3.io
6
7 # Import required libraries
8 from micropython import const
9 from machine import Pin, ADC
10 import time
11
12 # FeatherS3 Hardware Pin Assignments
13
14 # Sense Pins
15 VBUS_SENSE = const(34)
16 VBAT_SENSE = const(2)
17
18 # RGB LED, LDO2 & Other Pins
19 RGB_DATA = const(40)
20 LDO2 = const(39)
21 LED = const(13)
22 AMB_LIGHT = const(4)
23
24 # SPI
25 SPI_MOSI = const(35)
26 SPI_MISO = const(37)
27 SPI_CLK = const(36)
28
29 # I2C
30 I2C_SDA = const(8)
31 I2C_SCL = const(9)
32
33 # Helper functions
34
35
36 # LED & Ambient Light Sensor control
37 def led_set(state):
38 """Set the state of the BLUE LED on IO13"""
39 l = Pin(LED, Pin.OUT)
40 l.value(state)
41
42
43 def led_blink():
44 """Toggle the BLUE LED on IO13"""
45 l = Pin(LED, Pin.OUT)
46 l.value(not l.value())
47
48
49 # Create ADC and set attenuation and return the ambient light value from the onboard sensor
50 def get_amb_light():
51 """Get Ambient Light Sensor reading"""
52 adc = ADC(Pin(AMB_LIGHT))
53 adc.atten(ADC.ATTN_11DB)
54 return adc.read()
55
56
57 def set_ldo2_power(state):
58 """Enable or Disable power to the second LDO"""
59 Pin(LDO2, Pin.OUT).value(state)
60
61
62 def get_battery_voltage():
63 """
64 Returns the current battery voltage. If no battery is connected, returns 4.2V which is the charge voltage
65 This is an approximation only, but useful to detect if the charge state of the battery is getting low.
66 """
67 adc = ADC(Pin(VBAT_SENSE)) # Assign the ADC pin to read
68 # Max voltage on ADC input will be 4.2V divided by R2 (442K) & R5 (160K), 4.2 / (160+442) * 160 = 1.1163V
69 adc.atten(ADC.ATTN_2_5DB) # Needs 2.5DB attenuation to read a max voltage of 1.1163V
70 # Use read_uv() to get ADC reading as this will use the on-chip calibration data
71 measuredvbat = adc.read_uv() / 1000000 # Read micovolts and convert to volts
72 measuredvbat *= 3.7624 # Multiply by ratio of battery voltage to ADC pin voltage: 4.2/1.1163
73 return round(measuredvbat, 2)
74
75
76 def get_vbus_present():
77 """Detect if VBUS (5V) power source is present"""
78 return Pin(VBUS_SENSE, Pin.IN).value() == 1
79
80
81 # NeoPixel rainbow colour wheel
82 def rgb_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