Lazarus

Programming => General => Topic started by: BLL on November 01, 2019, 04:50:42 pm

Title: Altering backlight from within application program
Post by: BLL on November 01, 2019, 04:50:42 pm
Hi
Is there a way to be able to adjust the brightness of the RasPi official touchscreen from within a lazarus program? I can do it with a 6 foot long console command but there must be a better way!

Thanks

Brian
Title: Re: Altering backlight from within application program
Post by: lucamar on November 01, 2019, 09:45:56 pm
Easiest? Use a TProcess to run the 6' command, getting the brightness from the commad line and translating it to the corresponding parameter for the TProcess, so you can call it with, say:
  dimlight 2
or whatever.
Title: Re: Altering backlight from within application program
Post by: MarkMLl on November 02, 2019, 09:50:12 am
If this device really is "official", is there any support for it in the /sys tree i.e. like the function of the LEDs may be changed?

MarkMLl
Title: Re: Altering backlight from within application program
Post by: BLL on November 02, 2019, 12:05:14 pm
Hi lucamar
Thanks for the suggestion, but it's rather above my expertise!

The console command I have used, with eleveated status is:

echo n > /sys/class/backlight/rpi_backlight/brightness, where n can be 0 - 255

but how I would us TProcess I don't understand.

Thanks
Brian

Title: Re: Altering backlight from within application program
Post by: lucamar on November 02, 2019, 02:01:32 pm
but how I would us TProcess I don't understand.

See: Executing External Programs (https://wiki.freepascal.org/Executing_External_Programs) in the wiki. In your case the "executable" should be either the shell ('bash', 'dash', 'sh', ...) or 'sudo'.

But if all you want is a program to avoid having to type that command, I'd just use a simple shell script:

Code: [Select]
#! /bin/sh
echo $1 > /sys/class/backlight/rpi_backlight/brightness

Note that from a programming p.o.v. what your "6' long command line" does is basically to open the "file" /sys/class/backlight/rpi_backlight/brightness and write to it a string representing the brigthness level, so you could also make do with a simple program/function that writes its parameter (after checking that it's a number in the desired range) to that file.

A problem might arise  if you really need to be root to access that "file" but that can be "solved" with a judicious requirement to "sudo" your program ;)
Title: Re: Altering backlight from within application program
Post by: jamie on November 02, 2019, 08:00:47 pm
Wouldn't the Linux file system need to monitor that file to know it got changed ?
Title: Re: Altering backlight from within application program
Post by: BLL on November 09, 2019, 10:05:08 am
Hi all

Backlight control now working well. I used a stringlist to read the brightness file and then wrote a file with the new value to the program's directory, followed by a TProcess execute to sudo cp the file in the prog's directory to the system directory.

Thanks for the help & suggestions.

Brian
Title: Re: Altering backlight from within application program
Post by: jamie on November 09, 2019, 03:25:14 pm
If you expect the app to be portable I would also ensure the system exist on the PC you are on and have it simply do a silent abort or grey out the option if GUI is your app.

 It's not nice assuming everyone should have hardware like yours
Title: Re: Altering backlight from within application program
Post by: BLL on November 09, 2019, 05:56:09 pm
Already taken care of!
Brian
TinyPortal © 2005-2018