Skip to content

Commit 2660ef1

Browse files
fri3d-2024: display works, battery works, tweaks
1 parent 6e795ad commit 2660ef1

File tree

4 files changed

+107
-5
lines changed

4 files changed

+107
-5
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# Hardware initialization for Fri3d Camp 2024 Badge
2+
3+
from machine import Pin, SPI
4+
import st7789
5+
import lcd_bus
6+
import machine
7+
import cst816s
8+
import i2c
9+
10+
import lvgl as lv
11+
import task_handler
12+
13+
import mpos.ui
14+
15+
# Pin configuration
16+
SPI_BUS = 2
17+
#SPI_FREQ = 40000000
18+
#SPI_BUS = 1
19+
SPI_FREQ = 20000000
20+
LCD_SCLK = 7
21+
LCD_MOSI = 6
22+
LCD_MISO = 8
23+
LCD_DC = 4
24+
LCD_CS = 5
25+
#LCD_BL = 1
26+
LCD_RST = 48
27+
28+
#I2C_BUS = 0
29+
#I2C_FREQ = 100000
30+
#TP_SDA = 48
31+
#TP_SCL = 47
32+
#TP_ADDR = 0x15
33+
#TP_REGBITS = 8
34+
35+
TFT_HOR_RES=296
36+
TFT_VER_RES=240
37+
38+
spi_bus = machine.SPI.Bus(
39+
host=SPI_BUS,
40+
mosi=LCD_MOSI,
41+
miso=LCD_MISO,
42+
sck=LCD_SCLK
43+
)
44+
display_bus = lcd_bus.SPIBus(
45+
spi_bus=spi_bus,
46+
freq=SPI_FREQ,
47+
dc=LCD_DC,
48+
cs=LCD_CS
49+
)
50+
51+
#rs=LCD_RST
52+
# lv.color_format_get_size(lv.COLOR_FORMAT.RGB565) = 2 bytes per pixel * 320 * 240 px = 153600 bytes
53+
# The default was /10 so 15360 bytes.
54+
# /2 = 76800 shows something on display and then hangs the board
55+
# /2 = 38400 works and pretty high framerate but camera gets ESP_FAIL
56+
# /2 = 19200 works, including camera at 9FPS
57+
# 28800 is between the two and still works with camera!
58+
# 30720 is /5 and is already too much
59+
#_BUFFER_SIZE = const(28800)
60+
buffersize = const(28800)
61+
fb1 = display_bus.allocate_framebuffer(buffersize, lcd_bus.MEMORY_INTERNAL | lcd_bus.MEMORY_DMA)
62+
fb2 = display_bus.allocate_framebuffer(buffersize, lcd_bus.MEMORY_INTERNAL | lcd_bus.MEMORY_DMA)
63+
64+
STATE_HIGH = 1
65+
STATE_LOW = 0
66+
67+
# see ./lvgl_micropython/api_drivers/py_api_drivers/frozen/display/display_driver_framework.py
68+
display = st7789.ST7789(
69+
data_bus=display_bus,
70+
frame_buffer1=fb1,
71+
frame_buffer2=fb2,
72+
display_width=TFT_VER_RES,
73+
display_height=TFT_HOR_RES,
74+
color_space=lv.COLOR_FORMAT.RGB565,
75+
color_byte_order=st7789.BYTE_ORDER_BGR,
76+
rgb565_byte_swap=True,
77+
reset_pin=LCD_RST,
78+
reset_state=STATE_LOW
79+
)
80+
81+
display.init()
82+
display.set_power(True)
83+
display.set_backlight(100)
84+
85+
display.set_color_inversion(False)
86+
87+
lv.init()
88+
display.set_rotation(lv.DISPLAY_ROTATION._270) # must be done after initializing display and creating the touch drivers, to ensure proper handling
89+
display.set_params(0x36, bytearray([0x28]))
90+
91+
print("boot.py finished")

internal_filesystem/lib/mpos/battery_voltage.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
try:
33
from machine import ADC, Pin
44
# Configure ADC on pin 5 (IO5 / BAT_ADC)
5-
adc = ADC(Pin(5))
5+
#adc = ADC(Pin(5)) # TouchColorPiggy
6+
adc = ADC(Pin(13)) # fri3d-2024
67
# Set ADC to 11dB attenuation for 0–3.3V range (common for ESP32)
78
adc.atten(ADC.ATTN_11DB)
89
except Exception as e:
@@ -19,6 +20,14 @@
1920
MIN_VOLTAGE = 3.7
2021
MAX_VOLTAGE = 4.2
2122

23+
# On fri3d-2024, it's 2367 for full
24+
# Battery voltage ranges from 3.15V (0%) to 4.15 (100%) as datasheet specifies
25+
# 3.0 +/- 0.1V (discharge cut-off) to 4.2V (no margin of error provided, assuming 0.05V)
26+
# Charger stops charging at 4.07V (92%) to reduce battery wear.
27+
#define RG_BATTERY_CALC_PERCENT(raw) (((raw) * 2.f - 3150.f) / (4150.f - 3150.f) * 100.f)
28+
#define RG_BATTERY_CALC_VOLTAGE(raw) ((raw) * 2.f * 0.001f)
29+
30+
2231
# USB connected, full battery: VBAT 4.179 5V: 5.1
2332
# read_battery_voltage raw_value: 1598.4
2433
# read_battery_voltage raw_value: 1598.1
@@ -67,7 +76,8 @@ def read_battery_voltage():
6776
#print(f"read_battery_voltage raw_value: {raw_value}")
6877
# Convert to voltage, accounting for divider and reference
6978
#voltage = (raw_value / ADC_MAX) * VREF * VOLTAGE_DIVIDER
70-
voltage = raw_value * 262 / 100000
79+
#voltage = raw_value * 262 / 100000 # 2inch
80+
voltage = raw_value * 2 / 1000 # fri3d-2024
7181
# Clamp to 0–4.2V range for LiPo battery
7282
voltage = max(0, min(voltage, MAX_VOLTAGE))
7383
#return raw_value

internal_filesystem/lib/mpos/ui/topmenu.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def create_notification_bar():
8484
# Time label
8585
time_label = lv.label(notification_bar)
8686
time_label.set_text("00:00:00")
87-
time_label.align(lv.ALIGN.LEFT_MID, 0, 0)
87+
time_label.align(lv.ALIGN.LEFT_MID, mpos.ui.pct_of_display_width(10), 0)
8888
temp_label = lv.label(notification_bar)
8989
temp_label.set_text("00°C")
9090
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():
108108
battery_icon = lv.label(notification_bar)
109109
battery_icon.set_text(lv.SYMBOL.BATTERY_FULL)
110110
#battery_icon.align_to(battery_label, lv.ALIGN.OUT_LEFT_MID, 0, 0)
111-
battery_icon.align(lv.ALIGN.RIGHT_MID, 0, 0)
111+
battery_icon.align(lv.ALIGN.RIGHT_MID, -mpos.ui.pct_of_display_width(10), 0)
112112
battery_icon.add_flag(lv.obj.FLAG.HIDDEN) # keep it hidden until it has a correct value
113113
# WiFi icon
114114
wifi_icon = lv.label(notification_bar)

scripts/install.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ if [ ! -z "$appname" ]; then
3939
fi
4040

4141

42-
$mpremote fs cp boot.py :/boot.py
42+
#$mpremote fs cp boot.py :/boot.py
43+
$mpremote fs cp boot_fri3d2024.py :/boot.py
4344
$mpremote fs cp main.py :/main.py
4445

4546
#$mpremote fs cp main.py :/system/button.py

0 commit comments

Comments
 (0)