'==============================================
' Project: rEEZStudio
' Brief: Example with Button & Slider
' Description: Render an EEZ Studio (LVGL 9) UI on an ESP32 CYD using rLovyanGFXEx, handle button clicks and slider value changes in B4R.
' Author: Robert W.B. Linn (c) 2026 MIT — see LICENSE file.
' Date: 20261127
' DependsOn: rLovyanGFXEx, rEEZStudio
' Hardware: ESP32-CYD-SUNTON, B4R 4.10 (64 bit), IDE ESP32 Dev Module, Arduino Board Manager ESP32 3.3.12.
' Wiring: No additional wiring
'==============================================
#Region Project Attributes
#AutoFlushLogs: True
#CheckArrayBounds: True
#StackBufferSize: 4096
#DefineExtra: #define ESP32_2432S028
#End Region
'Ctrl+Click to open the C code folder: ide://run?File=%WINDIR%\System32\cmd.exe&args=/c&args=explorer.exe&Args=%PROJECT%\Objects\Src
Sub Process_Globals
Public Serial1 As Serial
Private eez As EEZStudio
Private lcd As LovyanGFXEx
Private lblTitle As ULong
Private btnTest As ULong
Private sldTest As ULong
Private ClickCount As Int = 0
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
Log("[AppStart] Starting rEEZStudio v0.25...")
' 1. Initialize CYD display driver FIRST
lcd.Initialize("LCD_TOUCHED")
lcd.DisplayRotation = 3
lcd.Brightness = 255
' 2. Initialize LVGL + EEZ Studio UI
eez.Initialize("EEZ_Click", 320, 240)
AddLooper("LoopEEZ")
' 3. Map EEZ Studio C object pointers to B4R ULong variables once
RunNative("MapObjects", Null)
Log("[AppStart] lblTitle=", lblTitle, ", btnTest=", btnTest, ", sldTest=", sldTest)
' 4. Register interactive widgets with numeric Tags
eez.RegisterClick(btnTest, 1)
eez.RegisterValueChanged("EEZ_ValueChanged", sldTest, 10)
' 5. Set initial slider value and label text
eez.SetSliderValue(sldTest, 50, False)
eez.SetLabelText(lblTitle, "Slider: 50".GetBytes)
eez.SetLabelColor(lblTitle, 0x0055FF)
eez.SetPos(lblTitle, 40, 20)
End Sub
' Looper updating LVGL timers and EEZ Studio UI state
Private Sub LoopEEZ
eez.Tick
End Sub
' Forward resistive touch coordinates from rLovyanGFXEx into LVGL 9
Private Sub LCD_TOUCHED(x As ULong, y As ULong)
Log("[LCD_TOUCHED] x=", x, ", y=", y)
eez.SetTouch(x, y)
End Sub
' Generic click event triggered by rEEZStudio for any registered widget
Private Sub EEZ_Click(Tag As Int)
Log("[EEZ_Click] Tag=", Tag)
Select Tag
Case 1
ClickCount = ClickCount + 1
Log("[EEZ_Click] Resetting slider to 50 (Click #", ClickCount, ")")
eez.SetSliderValue(sldTest, 50, True)
eez.SetLabelText(lblTitle, "Slider: 50 (Reset)".GetBytes)
eez.SetLabelColor(lblTitle, 0x008800)
End Select
End Sub
' Generic value-changed event triggered when dragging a slider
Private Sub EEZ_ValueChanged(Tag As Int, Value As Int)
Log("[EEZ_ValueChanged] Tag=", Tag, ", Value=", Value)
Select Tag
Case 10
eez.SetLabelText(lblTitle, JoinStrings(Array As String("Slider: ", NumberFormat(Value, 0, 0))).GetBytes)
eez.SetLabelColor(lblTitle, 0xCC0066)
' Adjust CYD backlight brightness (15..255) directly from the slider
lcd.Brightness = Max(15, (Value * 255) / 100)
End Select
End Sub
' --- OBJECT POINTER MAPPING ---
#If C
void MapObjects(B4R::Object* o) {
b4r_main::_lbltitle = (ULong)objects.lbl_title;
b4r_main::_btntest = (ULong)objects.btn_test;
b4r_main::_sldtest = (ULong)objects.sld_test;
}
#End If