2025-05-12 00:11:49 +02:00
# Hardware initialization for ESP32-S3-Touch-LCD-2
# Manufacturer's website at https://www.waveshare.com/wiki/ESP32-S3-Touch-LCD-2
2025-04-21 11:37:30 +02:00
from machine import Pin , SPI
import st7789
import lcd_bus
import machine
import cst816s
import i2c
import lvgl as lv
import task_handler
2025-05-12 00:26:54 +02:00
import mpos . ui
2025-10-13 19:22:22 +02:00
import mpos . info
mpos . info . set_hardware_id ( " waveshare-esp32-s3-touch-lcd-2 " )
2025-05-12 00:26:54 +02:00
2025-04-21 11:37:30 +02:00
# Pin configuration
SPI_BUS = 2
SPI_FREQ = 40000000
LCD_SCLK = 39
LCD_MOSI = 38
LCD_MISO = 40
LCD_DC = 42
LCD_CS = 45
LCD_BL = 1
I2C_BUS = 0
2025-11-13 09:35:26 +01:00
I2C_FREQ = 400000
2025-04-21 11:37:30 +02:00
TP_SDA = 48
TP_SCL = 47
TP_ADDR = 0x15
TP_REGBITS = 8
TFT_HOR_RES = 320
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 ,
)
2025-05-09 17:49:16 +02:00
# 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
2025-05-09 20:58:39 +02:00
_BUFFER_SIZE = const ( 28800 )
2025-05-09 17:49:16 +02:00
fb1 = display_bus . allocate_framebuffer ( _BUFFER_SIZE , lcd_bus . MEMORY_INTERNAL | lcd_bus . MEMORY_DMA )
fb2 = display_bus . allocate_framebuffer ( _BUFFER_SIZE , lcd_bus . MEMORY_INTERNAL | lcd_bus . MEMORY_DMA )
2025-11-15 16:25:10 +01:00
mpos . ui . main_display = st7789 . ST7789 (
2025-04-21 11:37:30 +02:00
data_bus = display_bus ,
2025-05-09 17:49:16 +02:00
frame_buffer1 = fb1 ,
frame_buffer2 = fb2 ,
2025-04-21 11:37:30 +02:00
display_width = TFT_VER_RES ,
display_height = TFT_HOR_RES ,
backlight_pin = LCD_BL ,
backlight_on_state = st7789 . STATE_PWM ,
color_space = lv . COLOR_FORMAT . RGB565 ,
color_byte_order = st7789 . BYTE_ORDER_BGR ,
rgb565_byte_swap = True ,
)
2025-11-15 16:25:10 +01:00
mpos . ui . main_display . init ( )
mpos . ui . main_display . set_power ( True )
mpos . ui . main_display . set_backlight ( 100 )
2025-04-21 11:37:30 +02:00
# Touch handling:
i2c_bus = i2c . I2C . Bus ( host = I2C_BUS , scl = TP_SCL , sda = TP_SDA , freq = I2C_FREQ , use_locks = False )
touch_dev = i2c . I2C . Device ( bus = i2c_bus , dev_id = TP_ADDR , reg_bits = TP_REGBITS )
indev = cst816s . CST816S ( touch_dev , startup_rotation = lv . DISPLAY_ROTATION . _180 ) # button in top left, good
2025-05-05 21:50:03 +02:00
lv . init ( )
2025-11-15 16:25:10 +01:00
mpos . ui . main_display . set_rotation ( lv . DISPLAY_ROTATION . _90 ) # must be done after initializing display and creating the touch drivers, to ensure proper handling
2025-04-21 11:37:30 +02:00
2025-10-08 17:52:37 +02:00
# Battery voltage ADC measuring
import mpos . battery_voltage
2025-10-09 14:49:31 +02:00
mpos . battery_voltage . init_adc ( 5 , 262 / 100000 )
2025-10-08 17:52:37 +02:00
2025-11-13 09:35:26 +01:00
# On the Waveshare ESP32-S3-Touch-LCD-2, the camera is hard-wired to power on,
# so it needs a software power off to prevent it from staying hot all the time and quickly draining the battery.
try :
from machine import Pin , I2C
i2c = I2C ( 1 , scl = Pin ( 16 ) , sda = Pin ( 21 ) ) # Adjust pins and frequency
devices = i2c . scan ( )
print ( " Scan of I2C bus on scl=16, sda=21: " )
print ( [ hex ( addr ) for addr in devices ] ) # finds it on 60 = 0x3C after init
camera_addr = 0x3C # for OV5640
reg_addr = 0x3008
reg_high = ( reg_addr >> 8 ) & 0xFF # 0x30
reg_low = reg_addr & 0xFF # 0x08
power_off_command = 0x42 # Power off command
i2c . writeto ( camera_addr , bytes ( [ reg_high , reg_low , power_off_command ] ) )
except Exception as e :
print ( f " Warning: powering off camera got exception: { e } " )
2025-04-21 11:37:30 +02:00
print ( " boot.py finished " )