-
Notifications
You must be signed in to change notification settings - Fork 0
/
tinys3.py
executable file
·60 lines (48 loc) · 1.85 KB
/
tinys3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# TinyS3 Helper Library
# 2022 Seon Rozenblum, Unexpected Maker
#
# Project home:
# https://tinys3.io
#
# Import required libraries
import time
import board
from digitalio import DigitalInOut, Direction, Pull
from analogio import AnalogIn
# Setup the NeoPixel power pin
pixel_power = DigitalInOut(board.NEOPIXEL_POWER)
pixel_power.direction = Direction.OUTPUT
# Setup the BATTERY voltage sense pin
vbat_voltage = AnalogIn(board.BATTERY)
# Setup the VBUS sense pin
vbus_sense = DigitalInOut(board.VBUS_SENSE)
vbus_sense.direction = Direction.INPUT
# Helper functions
def set_pixel_power(state):
"""Enable or Disable power to the onboard NeoPixel to either show colour, or to reduce power fro deep sleep."""
global pixel_power
pixel_power.value = state
def get_battery_voltage():
"""Get the approximate battery voltage."""
# I don't really understand what CP is doing under the hood here for the ADC range & calibration,
# but the onboard voltage divider for VBAT sense is setup to deliver 1.1V to the ADC based on it's
# default factory configuration.
# This forumla should show the nominal 4.2V max capacity (approximately) when 5V is present and the
# VBAT is in charge state for a 1S LiPo battery with a max capacity of 4.2V
global vbat_voltage
return (vbat_voltage.value / 5371)
def get_vbus_present():
"""Detect if VBUS (5V) power source is present"""
global vbus_sense
return vbus_sense.value
def rgb_color_wheel(wheel_pos):
"""Color wheel to allow for cycling through the rainbow of RGB colors."""
wheel_pos = wheel_pos % 255
if wheel_pos < 85:
return 255 - wheel_pos * 3, 0, wheel_pos * 3
elif wheel_pos < 170:
wheel_pos -= 85
return 0, wheel_pos * 3, 255 - wheel_pos * 3
else:
wheel_pos -= 170
return wheel_pos * 3, 255 - wheel_pos * 3, 0