Recent

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

BLL

  • Sr. Member
  • ****
  • Posts: 276
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: 4217
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/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8538
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
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

BLL

  • Sr. Member
  • ****
  • Posts: 276
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: 4217
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/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

jamie

  • Hero Member
  • *****
  • Posts: 7587
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 ?
The only true wisdom is knowing you know nothing

BLL

  • Sr. Member
  • ****
  • Posts: 276
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: 7587
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
The only true wisdom is knowing you know nothing

BLL

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

 

TinyPortal © 2005-2018