Reading Illuminance (Lux) from a Light Sensor (VCL) — Delphi Source Code Collection
Create the Project and Design the Form
Launch Delphi, then select File → New → Windows VCL Application – Delphi (W) from the menu.
Drag and drop two TButton components and one TEdit component onto the form.
Entering the Source Code
Add the following source code to the event handlers such as Button1.OnClick, Button2.OnClick, and Form1.OnDestroy.
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls,
System.Sensors, System.TypInfo;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
LightSensor:TCustomLightSensor;
procedure LightDataChanged(Sender:TObject);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var SensorManager:TSensorManager;
i:Integer;
begin
//Get the sensor manager
SensorManager:=TSensorManager.Current;
//If the sensor manager is supported
if SensorManager.CanActivate then
begin
//Activate the sensor manager
SensorManager.Activate;
//Loop through all sensors
for i := 0 to SensorManager.Count-1 do
begin
//If a light sensor is found
if SensorManager.Sensors[i].Category = TSensorCategory.Light then
begin
LightSensor := TCustomLightSensor(SensorManager.Sensors[i]);
LightSensor.OnDataChanged := LightDataChanged;
//Enable the sensor
LightSensor.Start;
Edit1.Text := Format( '%5.0 fLux', [ LightSensor.Lux ] );
end;
end;
SensorManager.Deactivate;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
if assigned(LightSensor) then
begin
LightSensor.Stop;
LightSensor := nil;
Edit1.Text := 'Stopped';
end;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
if assigned(LightSensor) then
begin
LightSensor.Stop;
end;
end;
procedure TForm1.LightDataChanged(Sender: TObject);
begin
Edit1.Text := Format('%5.0f Lux',[LightSensor.Lux]);
end;
end.
Run the Application
Select Run → Run, or click the debug run button to compile and start the application.
When you click Button1, the light sensor will be activated (if your PC is equipped with one), and the illuminance value will be displayed in lux.
Clicking Button2 will stop the sensor.
Create the Project and Design the Form
Launch Delphi, then select File → New → Windows VCL Application – Delphi (W) from the menu.
Drag and drop two TButton components and one TEdit component onto the form.
Entering the Source Code
Add the following source code to the event handlers such as Button1.OnClick, Button2.OnClick, and Form1.OnDestroy.
unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, System.Sensors, System.TypInfo; type TForm1 = class(TForm) Button1: TButton; Button2: TButton; Edit1: TEdit; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure FormDestroy(Sender: TObject); private { Private declarations } LightSensor:TCustomLightSensor; procedure LightDataChanged(Sender:TObject); public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); var SensorManager:TSensorManager; i:Integer; begin //Get the sensor manager SensorManager:=TSensorManager.Current; //If the sensor manager is supported if SensorManager.CanActivate then begin //Activate the sensor manager SensorManager.Activate; //Loop through all sensors for i := 0 to SensorManager.Count-1 do begin //If a light sensor is found if SensorManager.Sensors[i].Category = TSensorCategory.Light then begin LightSensor := TCustomLightSensor(SensorManager.Sensors[i]); LightSensor.OnDataChanged := LightDataChanged; //Enable the sensor LightSensor.Start; Edit1.Text := Format( '%5.0 fLux', [ LightSensor.Lux ] ); end; end; SensorManager.Deactivate; end; end; procedure TForm1.Button2Click(Sender: TObject); begin if assigned(LightSensor) then begin LightSensor.Stop; LightSensor := nil; Edit1.Text := 'Stopped'; end; end; procedure TForm1.FormDestroy(Sender: TObject); begin if assigned(LightSensor) then begin LightSensor.Stop; end; end; procedure TForm1.LightDataChanged(Sender: TObject); begin Edit1.Text := Format('%5.0f Lux',[LightSensor.Lux]); end; end.
Run the Application
Select Run → Run, or click the debug run button to compile and start the application.
When you click Button1, the light sensor will be activated (if your PC is equipped with one), and the illuminance value will be displayed in lux.
Clicking Button2 will stop the sensor.
