Recent

Author Topic: Altering backlight from within application program  (Read 384 times)

BLL

  • Sr. Member
  • ****
  • Posts: 265
Altering backlight from within application program
« 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

lucamar

  • Hero Member
  • *****
  • Posts: 2080
Re: Altering backlight from within application program
« Reply #1 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.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.2/2.0.4  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

MarkMLl

  • Full Member
  • ***
  • Posts: 222
Re: Altering backlight from within application program
« Reply #2 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
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.

BLL

  • Sr. Member
  • ****
  • Posts: 265
Re: Altering backlight from within application program
« Reply #3 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


lucamar

  • Hero Member
  • *****
  • Posts: 2080
Re: Altering backlight from within application program
« Reply #4 on: November 02, 2019, 02:01:32 pm »
but how I would us TProcess I don't understand.

See: 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 ;)
« Last Edit: November 02, 2019, 02:03:58 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.2/2.0.4  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

jamie

  • Hero Member
  • *****
  • Posts: 2083
Re: Altering backlight from within application program
« Reply #5 on: November 02, 2019, 08:00:47 pm »
Wouldn't the Linux file system need to monitor that file to know it got changed ?
Number 1 at blue screen app creations!

BLL

  • Sr. Member
  • ****
  • Posts: 265
Re: Altering backlight from within application program
« Reply #6 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

jamie

  • Hero Member
  • *****
  • Posts: 2083
Re: Altering backlight from within application program
« Reply #7 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
Number 1 at blue screen app creations!

BLL

  • Sr. Member
  • ****
  • Posts: 265
Re: Altering backlight from within application program
« Reply #8 on: November 09, 2019, 05:56:09 pm »
Already taken care of!
Brian