blacklight screen for Iothermostat
Backlight control installation for Waveshare 3.5inch version B screen
Control is performed via pin12 of the rasp2B 40points connector (gpio18 of the rpi connector) using a pwm command.
but the backlight can be switched off with a value of 0 or fully illuminated with a value of 1.
The iothermostat application works for local display in an X windows session in a simple luakit browser.
Checking the characteristics of the windows manager
iothermostat@iothermostat:~ $ xset q
Keyboard Control:
auto repeat: on key click percent: 0 LED mask: ffffe780
XKB indicators:
00: Caps Lock: off 01: Num Lock: off 02: Scroll Lock: off
03: Compose: off 04: Kana: off 05: Sleep: off
06: Suspend: off 07: Mute: on 08: Misc: on
09: Mail: on 10: Charging: on 11: Shift Lock: off
12: Group 2: off 13: Mouse Keys: on
auto repeat delay: 660 repeat rate: 25
auto repeating keys: 00ffffffdffffbbf
fadfffefffedffff
9fffffffffffff
fff7ffffffffffff
bell percent: 50 bell pitch: 400 bell duration: 100
Pointer Control:
acceleration: 2/1 threshold: 4
Screen Saver:
prefer blanking: yes allow exposures: yes
timeout: 600 cycle: 600
Colors:
default colormap: 0x20 BlackPixel: 0x0 WhitePixel: 0xffff
Font Path:
/usr/share/fonts/X11/misc,built-ins
DPMS (Energy Star):
Standby: 600 Suspend: 600 Off: 600
DPMS is Enabled
Monitor is Off
DPMS management is enabled for the display, but no hardware interface is processed by the device.
#### Backlight control check
GPIO programming: use of sysfs interface (present in app)
see https://www.ics.com/blog/gpio-programming-using-sysfs-interface
https://elinux.org/RPi_GPIO_Code_Samples
user connection iothermostat ( iothermostat must be in the gpio group )
iothermostat@iothermostat:~ $ ls /sys/class/gpio/ export gpiochip0 unexport
create access to gpio18
$ echo 18 >/sys/class/gpio/export
$ ls /sys/class/gpio export gpio18 gpiochip0 unexport
$ ls /sys/class/gpio/gpio18 active_low device direction edge power subsystem uevent value
now we need to define the output
$ echo out >/sys/class/gpio/gpio18/direction
then set the gpio port output value to 1
$ echo 1 >/sys/class/gpio/gpio18/value
Backlighting is now controllable
We’ll see that we can use the raspi-gpio library present in the configuration.
Implementation
Solution 1 modify DPMS management for the display
I haven’t been able to find a solution for processing DPMS to take the display into account.
Solution 2 control display according to DPMS control status
DPMS (Energy Star):
Standby: 600 Suspend: 600 Off: 600
DPMS is Enabled
Monitor is Off
Proposed here Controlling the LCD Backlight, I retain the bash script solution
(simple, no compilation required, and raspi-gpio library installed)
Bash script: backlight.sh
#!/bin/bash
#PWM_PIN=31
PIN=18
# Set pins as output
#raspi-gpio set $PWM_PIN op
raspi-gpio set $PIN op
# Start with both pins HIGH.* *If either of the
# pins go LOW, the backlight will turn off.
#raspi-gpio set $PWM_PIN dh
raspi-gpio set $PIN dh
# Get the current state
CURRENT_STATUS=$(xset q | grep “Monitor is” | awk '{print $3}')
LAST_STATUS=$CURRENT_STATUS
logger -p info -t “backlight_service” -s “Monitor is $CURRENT_STATUS”
# Loop indefinitely updating backlight in sync with monitor status
while true; do
# Check the current monitor status
CURRENT_STATUS=$(xset q | grep “Monitor is” | awk '{print $3}')
# Only turn the backlight on/off if the Monitor state has changed
if [ “$CURRENT_STATUS” != “$LAST_STATUS” ]; then
logger -p info -t “backlight_service” -s “Monitor is $CURRENT_STATUS”
# Control backlight pin according to the monitor status
if [ “$CURRENT_STATUS” = “On” ]; then
logger -p info -t “backlight_service” -s “Turning Backlight On”
raspi-gpio set $PIN dh
else
logger -p info -t “backlight_service” -s “Turning Backlight Off”
raspi-gpio set $PIN dl
fi
LAST_STATUS=$CURRENT_STATUS
fi
# Sleep before checking again
sleep 1s
done
#### integration in iothermostat app
iothermostat@iothermostat:~ $ pstree -a
systemd splash
│ ....
│
├─login -f
│ └─startx /usr/bin/startx -- -nocursor
│ └─xinit /home/iothermostat/.xinitrc -- /usr/bin/X :0 -nocursor vt1 -keeptty -auth /tmp/serverauth.Hz4MUFRc4L
│ ├─Xorg :0 -nocursor vt1 -keeptty -auth /tmp/serverauth.Hz4MUFRc4L
│ │ └─9*[{Xorg}]
│ └─sh /home/iothermostat/.xinitrc
│ ├─backlight.sh ./backlight.sh
│ ├─luakit -u http://localhost/iothermostat/index.php
│ │ ├─WebKitNetworkPr 4 16
│ │ └─11*[{WebKitNetworkPr}]
│ │ ├─WebKitWebProces 9 23
│ │ └─10*[{WebKitWebProces}]
│ │ └─18*[{luakit}]
│ └─matchbox-window
├─
spot controls DPMS status and turns backlight on or off accordingly
http://comfilewiki.co.kr/en/doku.php?id=comfilepi:configure_a_program_to_auto-start:index#configure_an_x_program_to_auto-start_eg_mono
In our case, it will be launched by xinitrc
root@iothermostat:/home/iothermostat# cat /home/iothermostat/.xinitrc
#!/bin/bash
FILE=~/calibrated
/usr/bin/matchbox-window-manager &
if [ -f “$FILE” ]; then
CURRENT_STATUS=$(xset q | grep “Monitor is” | awk '{print $3}')
logger -p info -t “backlight_service” -s “start Monitor is $CURRENT_STATUS”
./backlight.sh &
luakit -u http://localhost/iothermostat/index.php
else
xinput_calibrator
touch “$FILE”
fi
### Complements
http://comfilewiki.co.kr/en/doku.php?id=comfilepi:controlling_the_lcd_backlight:index