fri3d-2024: display works, battery works, tweaks

This commit is contained in:
Thomas Farstrike
2025-10-07 20:51:41 +02:00
parent 6e795adaf5
commit 2660ef1adc
4 changed files with 107 additions and 5 deletions
+91
View File
@@ -0,0 +1,91 @@
# Hardware initialization for Fri3d Camp 2024 Badge
from machine import Pin, SPI
import st7789
import lcd_bus
import machine
import cst816s
import i2c
import lvgl as lv
import task_handler
import mpos.ui
# Pin configuration
SPI_BUS = 2
#SPI_FREQ = 40000000
#SPI_BUS = 1
SPI_FREQ = 20000000
LCD_SCLK = 7
LCD_MOSI = 6
LCD_MISO = 8
LCD_DC = 4
LCD_CS = 5
#LCD_BL = 1
LCD_RST = 48
#I2C_BUS = 0
#I2C_FREQ = 100000
#TP_SDA = 48
#TP_SCL = 47
#TP_ADDR = 0x15
#TP_REGBITS = 8
TFT_HOR_RES=296
TFT_VER_RES=240
spi_bus = machine.SPI.Bus(
host=SPI_BUS,
mosi=LCD_MOSI,
miso=LCD_MISO,
sck=LCD_SCLK
)
display_bus = lcd_bus.SPIBus(
spi_bus=spi_bus,
freq=SPI_FREQ,
dc=LCD_DC,
cs=LCD_CS
)
#rs=LCD_RST
# lv.color_format_get_size(lv.COLOR_FORMAT.RGB565) = 2 bytes per pixel * 320 * 240 px = 153600 bytes
# The default was /10 so 15360 bytes.
# /2 = 76800 shows something on display and then hangs the board
# /2 = 38400 works and pretty high framerate but camera gets ESP_FAIL
# /2 = 19200 works, including camera at 9FPS
# 28800 is between the two and still works with camera!
# 30720 is /5 and is already too much
#_BUFFER_SIZE = const(28800)
buffersize = const(28800)
fb1 = display_bus.allocate_framebuffer(buffersize, lcd_bus.MEMORY_INTERNAL | lcd_bus.MEMORY_DMA)
fb2 = display_bus.allocate_framebuffer(buffersize, lcd_bus.MEMORY_INTERNAL | lcd_bus.MEMORY_DMA)
STATE_HIGH = 1
STATE_LOW = 0
# see ./lvgl_micropython/api_drivers/py_api_drivers/frozen/display/display_driver_framework.py
display = st7789.ST7789(
data_bus=display_bus,
frame_buffer1=fb1,
frame_buffer2=fb2,
display_width=TFT_VER_RES,
display_height=TFT_HOR_RES,
color_space=lv.COLOR_FORMAT.RGB565,
color_byte_order=st7789.BYTE_ORDER_BGR,
rgb565_byte_swap=True,
reset_pin=LCD_RST,
reset_state=STATE_LOW
)
display.init()
display.set_power(True)
display.set_backlight(100)
display.set_color_inversion(False)
lv.init()
display.set_rotation(lv.DISPLAY_ROTATION._270) # must be done after initializing display and creating the touch drivers, to ensure proper handling
display.set_params(0x36, bytearray([0x28]))
print("boot.py finished")
@@ -2,7 +2,8 @@ have_adc=True
try:
from machine import ADC, Pin
# Configure ADC on pin 5 (IO5 / BAT_ADC)
adc = ADC(Pin(5))
#adc = ADC(Pin(5)) # TouchColorPiggy
adc = ADC(Pin(13)) # fri3d-2024
# Set ADC to 11dB attenuation for 03.3V range (common for ESP32)
adc.atten(ADC.ATTN_11DB)
except Exception as e:
@@ -19,6 +20,14 @@ VOLTAGE_DIVIDER = 3 # (R1 + R2) / R2 = (200k + 100k) / 100k = 3
MIN_VOLTAGE = 3.7
MAX_VOLTAGE = 4.2
# On fri3d-2024, it's 2367 for full
# Battery voltage ranges from 3.15V (0%) to 4.15 (100%) as datasheet specifies
# 3.0 +/- 0.1V (discharge cut-off) to 4.2V (no margin of error provided, assuming 0.05V)
# Charger stops charging at 4.07V (92%) to reduce battery wear.
#define RG_BATTERY_CALC_PERCENT(raw) (((raw) * 2.f - 3150.f) / (4150.f - 3150.f) * 100.f)
#define RG_BATTERY_CALC_VOLTAGE(raw) ((raw) * 2.f * 0.001f)
# USB connected, full battery: VBAT 4.179 5V: 5.1
# read_battery_voltage raw_value: 1598.4
# read_battery_voltage raw_value: 1598.1
@@ -67,7 +76,8 @@ def read_battery_voltage():
#print(f"read_battery_voltage raw_value: {raw_value}")
# Convert to voltage, accounting for divider and reference
#voltage = (raw_value / ADC_MAX) * VREF * VOLTAGE_DIVIDER
voltage = raw_value * 262 / 100000
#voltage = raw_value * 262 / 100000 # 2inch
voltage = raw_value * 2 / 1000 # fri3d-2024
# Clamp to 04.2V range for LiPo battery
voltage = max(0, min(voltage, MAX_VOLTAGE))
#return raw_value
+2 -2
View File
@@ -84,7 +84,7 @@ def create_notification_bar():
# Time label
time_label = lv.label(notification_bar)
time_label.set_text("00:00:00")
time_label.align(lv.ALIGN.LEFT_MID, 0, 0)
time_label.align(lv.ALIGN.LEFT_MID, mpos.ui.pct_of_display_width(10), 0)
temp_label = lv.label(notification_bar)
temp_label.set_text("00°C")
temp_label.align_to(time_label, lv.ALIGN.OUT_RIGHT_MID, mpos.ui.pct_of_display_width(7) , 0)
@@ -108,7 +108,7 @@ def create_notification_bar():
battery_icon = lv.label(notification_bar)
battery_icon.set_text(lv.SYMBOL.BATTERY_FULL)
#battery_icon.align_to(battery_label, lv.ALIGN.OUT_LEFT_MID, 0, 0)
battery_icon.align(lv.ALIGN.RIGHT_MID, 0, 0)
battery_icon.align(lv.ALIGN.RIGHT_MID, -mpos.ui.pct_of_display_width(10), 0)
battery_icon.add_flag(lv.obj.FLAG.HIDDEN) # keep it hidden until it has a correct value
# WiFi icon
wifi_icon = lv.label(notification_bar)
+2 -1
View File
@@ -39,7 +39,8 @@ if [ ! -z "$appname" ]; then
fi
$mpremote fs cp boot.py :/boot.py
#$mpremote fs cp boot.py :/boot.py
$mpremote fs cp boot_fri3d2024.py :/boot.py
$mpremote fs cp main.py :/main.py
#$mpremote fs cp main.py :/system/button.py