Debian Codegraph @

ports/esp32/boards/UM_FEATHERS3NEO/modules/feathers3neo.py

raw
1 # FeatherS3 Neo MicroPython Helper Library
2 # MIT license; Copyright (c) 2024 Seon Rozenblum - Unexpected Maker
3 #
4 # Project home:
5 # http://esp32s3.com
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(15)
16 VBAT_SENSE = const(2)
17
18 # LDO2 & Other Pins
19 LDO2 = const(39)
20 LED = const(13)
21 AMB_LIGHT = const(4)
22
23 # RGB LED Pins
24 RGB_DATA = const(40)
25 RGB_PWR = const(39)
26
27 # RGB MATRIX LED Pins
28 RGB_MATRIX_DATA = const(16)
29 RGB_MATRIX_PWR = const(39)
30
31 # SPI
32 SPI_MOSI = const(35)
33 SPI_MISO = const(37)
34 SPI_CLK = const(36)
35
36 # I2C
37 I2C_SDA = const(8)
38 I2C_SCL = const(9)
39
40 # Helper functions
41
42
43 # LED & Ambient Light Sensor control
44 def led_set(state):
45 """Set the state of the BLUE LED on IO13"""
46 l = Pin(LED, Pin.OUT)
47 l.value(state)
48
49
50 def led_blink():
51 """Toggle the BLUE LED on IO13"""
52 l = Pin(LED, Pin.OUT)
53 l.value(not l.value())
54
55
56 # Create ADC and set attenuation and return the ambient light value from the onboard sensor
57 def get_amb_light():
58 """Get Ambient Light Sensor reading"""
59 adc = ADC(Pin(AMB_LIGHT))
60 adc.atten(ADC.ATTN_11DB)
61 return adc.read()
62
63
64 def set_ldo2_power(state):
65 """
66 Enable or Disable power to the second LDO, which is the LDO that powers the following items
67 RGB Matrix, RGB status LED, Ambient light Sensor.
68 This is ON by default and will automatically shut down when the ESP32 going into deep sleep.
69 """
70 Pin(LDO2, Pin.OUT).value(state)
71
72
73 def get_battery_voltage():
74 """
75 Returns the current battery voltage. If no battery is connected, returns 4.2V which is the charge voltage
76 This is an approximation only, but useful to detect if the charge state of the battery is getting low.
77 """
78 adc = ADC(Pin(VBAT_SENSE)) # Assign the ADC pin to read
79 # Max voltage on ADC input will be 4.2V divided by R2 (442K) & R5 (160K), 4.2 / (160+442) * 160 = 1.1163V
80 adc.atten(ADC.ATTN_2_5DB) # Needs 2.5DB attenuation to read a max voltage of 1.1163V
81 # Use read_uv() to get ADC reading as this will use the on-chip calibration data
82 measuredvbat = adc.read_uv() / 1000000 # Read micovolts and convert to volts
83 measuredvbat *= 3.7624 # Multiply by ratio of battery voltage to ADC pin voltage: 4.2/1.1163
84 return round(measuredvbat, 2)
85
86
87 def get_vbus_present():
88 """Detect if VBUS (5V) power source is present"""
89 return Pin(VBUS_SENSE, Pin.IN).value() == 1
90
91
92 # NeoPixel rainbow colour wheel
93 def rgb_color_wheel(wheel_pos):
94 """Color wheel to allow for cycling through the rainbow of RGB colors."""
95 wheel_pos = wheel_pos % 255
96
97 if wheel_pos < 85:
98 return 255 - wheel_pos * 3, 0, wheel_pos * 3
99 elif wheel_pos < 170:
100 wheel_pos -= 85
101 return 0, wheel_pos * 3, 255 - wheel_pos * 3
102 else:
103 wheel_pos -= 170
104 return wheel_pos * 3, 255 - wheel_pos * 3, 0