| 1 |
# OMGS3 Helper Library |
| 2 |
# MIT license; Copyright (c) 2024 Seon Rozenblum - Unexpected Maker |
| 3 |
# |
| 4 |
# Project home: |
| 5 |
# https://omgs3.io |
| 6 |
|
| 7 |
# Import required libraries |
| 8 |
from micropython import const |
| 9 |
from machine import Pin, I2C |
| 10 |
from max17048 import MAX17048 |
| 11 |
import time |
| 12 |
|
| 13 |
# Initialize I2C bus |
| 14 |
fg_i2c = I2C(0, scl=Pin.board.I2C_SCL, sda=Pin.board.I2C_SDA) |
| 15 |
|
| 16 |
# Create an instance of the MAX17048 class |
| 17 |
max17048 = MAX17048(fg_i2c) |
| 18 |
|
| 19 |
|
| 20 |
# Helper functions |
| 21 |
def get_bat_voltage(): |
| 22 |
"""Read the battery voltage from the fuel gauge""" |
| 23 |
voltage = max17048.cell_voltage |
| 24 |
print(f"Bat Voltage: {voltage}V") |
| 25 |
return voltage |
| 26 |
|
| 27 |
|
| 28 |
def get_state_of_charge(): |
| 29 |
"""Read the battery state of charge from the fuel gauge""" |
| 30 |
soc = max17048.state_of_charge |
| 31 |
print(f"State of Charge: {soc}%") |
| 32 |
return soc |
| 33 |
|
| 34 |
|
| 35 |
def get_vbus_present(): |
| 36 |
"""Detect if VBUS (5V) power source is present""" |
| 37 |
return Pin(Pin.board.VBUS_SENSE, Pin.IN).value() == 1 |
| 38 |
|
| 39 |
|
| 40 |
def set_pixel_power(state): |
| 41 |
"""Enable or Disable power to the onboard NeoPixel to either show colour, or to reduce power for deep sleep.""" |
| 42 |
Pin(Pin.board.RGB_PWR, Pin.OUT).value(state) |
| 43 |
|
| 44 |
|
| 45 |
# NeoPixel rainbow colour wheel |
| 46 |
def rgb_color_wheel(wheel_pos): |
| 47 |
"""Color wheel to allow for cycling through the rainbow of RGB colors.""" |
| 48 |
wheel_pos = wheel_pos % 255 |
| 49 |
|
| 50 |
if wheel_pos < 85: |
| 51 |
return 255 - wheel_pos * 3, 0, wheel_pos * 3 |
| 52 |
elif wheel_pos < 170: |
| 53 |
wheel_pos -= 85 |
| 54 |
return 0, wheel_pos * 3, 255 - wheel_pos * 3 |
| 55 |
else: |
| 56 |
wheel_pos -= 170 |
| 57 |
return wheel_pos * 3, 255 - wheel_pos * 3, 0 |