### Sample #2 GPIO LED

Example of the classic HelloWorld Application.
The app runs in fully filled screen with the main form as a window with size set in the project attributes
External libraries jPi4J, CSSUtils. These must be merged into the project.
MainForm Layout
Composed with the B4J Visual Designer using following Views: [Label (Title)]
[ToggleButton LED Switch]
[Label LED Status]
[Button Close]
[Label Info]
LED Wiring
Raspberry Pi = LED
Physical Pin 12 (GPIO.1 = wPi 1 = BCM 18) = + (Long Leg)
Physical Pin 9 GND = - GND
Circuit
RPi Pin12 - (K, Long Leg) LED (A, Short Leg) - R330Ohm - GND
B4J Code Snippet
`#Region Project Attributes
#MainFormWidth: 600
#MainFormHeight: 400
'Ensure to set True
#MergeLibraries: True
#End Region
`Sub Process_Globals
Private CAPPVERSION As String = "v20180419"
Private CAPPTITLE As String = "B4J HowTo Raspberry Pi Liberica JDK10 - GPIO Switch LED"
Private CAPPINFO As String = $"${CAPPVERSION} by Robert W.B. Linn"$
' Views
Private fx As JFX
Private MainForm As Form
Private lblTitle As Label
Private lblStatus As Label
Private btnClose As Button
Private tbLEDSwitch As ToggleButton
Private lblLEDState As Label
' GPIO
Private controllerGPIO As GpioController
Private pinLED As GpioPinDigitalOutput
Private pinLEDGPIONr As Int = 1
End Sub`
`Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.RootPane.LoadLayout("Main")
' MainForm.SetFormStyle("UTILITY")
' MainForm.Resizable = False
MainForm.Show
AppInit
End Sub`
`Sub AppClose
MainForm.Close
ExitApplication
End Sub`
'Return true to allow the default exceptions handler to handle the uncaught exception.
`Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
Return True
End Sub`
`Sub AppInit
' Labels
lblTitle.Text = CAPPTITLE
lblStatus.Text = $"${CAPPINFO} - Java ${GetSystemProperty("java.version","0.0.0_00")}"$
' CSS
CSSUtils.SetBackgroundColor(lblTitle, fx.Colors.Blue)
CSSUtils.SetStyleProperty(lblTitle, "-fx-text-fill", "white")
CSSUtils.SetBackgroundColor(MainForm.RootPane, fx.Colors.LightGray)
' GPIO
Try
controllerGPIO.Initialize
pinLED.Initialize(pinLEDGPIONr, False)
Catch
Log($"Error Init Controller: ${LastException.Message}"$)
Return
End Try
' Turn LED off
tbLEDSwitch_SelectedChange(False)
End Sub`
`Sub tbLEDSwitch_SelectedChange(Selected As Boolean)
pinLED.State = Selected
If Selected Then
CSSUtils.SetBackgroundColor(tbLEDSwitch, fx.Colors.Red)
CSSUtils.SetStyleProperty(tbLEDSwitch, "-fx-text-fill", "white")
tbLEDSwitch.Text = $"Switch LED OFF"$
lblLEDState.Text = $"${IsNow} - LED turned ON"$
Else
CSSUtils.SetBackgroundColor(tbLEDSwitch, fx.Colors.Green)
CSSUtils.SetStyleProperty(tbLEDSwitch, "-fx-text-fill", "black")
tbLEDSwitch.Text = $"Switch LED ON"$
lblLEDState.Text = $"${IsNow} - LED turned OFF"$
End If
End Sub`
`Sub btnClose_Action
AppClose
End Sub`
Region Helpers
' Returns the actual time using the datetime.timeformat set
`Sub IsNow As String 'ignore
Return DateTime.Time(DateTime.Now)
End Sub`
'Set the form to fill the screen with an offset
`Sub SetFormFillScreenOffset(frm As Form, offset As Int) 'ignore
Dim ps As Screen = fx.PrimaryScreen
frm.WindowLeft = ps.MinX + offset
frm.WindowWidth = ps.MaxX - frm.WindowLeft - offset
frm.WindowTop = ps.MinY + offset
frm.WindowHeight = ps.MaxY - frm.WindowTop - offset
End Sub`
End Region
`

Changed: 2018-06-07 09:22, Category: Raspberry Pi UI Liberica, File: T6C287M2163.htm, ID: 2163