### Sample #2 GPIO LED

Example of switching a LED via GPIO.
The app runs in fully filled screen with the main form as a window with size set in the project attributes
External libraries jPi4J 2.0, CSSUtils 1.11. These must be merged into the project.
MainForm Layout
Created with the B4J Visual Designer, is composed out of 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
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 = "v20170725"
Private CAPPTITLE As String = "B4J HowTo Raspberry Pi JavaFXPorts - GPIO Switch LED"
Private CAPPINFO As String = $"${CAPPVERSION} created by Robert W.B. Linn, powered by B4J @ www.b4x.com "$
' 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
' 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`
' 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`

Changed: 2018-06-07 09:22, Category: Raspberry Pi UI JavaFXPorts, File: T6C239M1751.htm, ID: 1751