User Tools

Site Tools

한국어

comfilepi:controlling_the_lcd_backlight:index

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Last revision Both sides next revision
comfilepi:controlling_the_lcd_backlight:index [2023/08/30 12:44]
COMFILE Technology [Controlling the LCD Backlight]
comfilepi:controlling_the_lcd_backlight:index [2023/09/19 14:33]
COMFILE Technology [Bash Script Implementation]
Line 3: Line 3:
 The ComfilePi'​s LCD backlight is connected to GPIO 34.  The backlight can be turned on or off via the command line or via a programming language. The ComfilePi'​s LCD backlight is connected to GPIO 34.  The backlight can be turned on or off via the command line or via a programming language.
  
-Please be sure the Raspberry Pi [[https://​www.raspberrypi.com/​documentation/​computers/​configuration.html#​screen-blanking|Screen Blanking]] feature is disabled to prevent the two from interfering with one another. 
  
 ===== Command Line Interface ===== ===== Command Line Interface =====
Line 97: Line 96:
 #include <​chrono>​ #include <​chrono>​
 #include <​thread>​ #include <​thread>​
 +#include <​sys/​syslog.h>​ 
 +#include <​pigpiod_if2.h>​ 
 + 
 using namespace std; using namespace std;
 using namespace std::​chrono;​ using namespace std::​chrono;​
- + 
-#include <​pigpiod_if2.h>​ +
 #define PIN 34  // CPi-A/B/F/S #define PIN 34  // CPi-A/B/F/S
-// #define PIN 44  // CPi-C+// #define PIN 44 // CPi-C
  
 +void log_info(const char *const message)
 +{
 +    printf("​%s\n",​ message);
 +    syslog(LOG_INFO,​ "​%s",​ message);
 +}
 +
 +void log_err(const char *const message)
 +{
 +    printf("​%s\n",​ message);
 +    syslog(LOG_ERR,​ "​%s",​ message);
 +}
 + 
 void backlight_on() void backlight_on()
 { {
     auto instance = pigpio_start(NULL,​ NULL);     auto instance = pigpio_start(NULL,​ NULL);
 +    if (instance != 0)
 +    {
 +        log_err("​pigpio_start did not return success"​);​
 +        return;
 +    }
 + 
 +    auto result = set_mode(instance,​ PIN, PI_OUTPUT);
 +    if (instance != 0)
 +    {
 +        log_err("​set_mode did not return success"​);​
 +        return;
 +    }
  
-    ​set_mode(instance, PIN, PI_OUTPUT); +    ​result = gpio_write(instance, PIN, 1); 
-    ​gpio_write(instance, PIN, 1); +    ​if (instance ​!= 0) 
 +    { 
 +        log_err("​gpio_write did not return success"​); 
 +        return; 
 +    } 
 + 
     pigpio_stop(instance);​     pigpio_stop(instance);​
 } }
 + 
 void backlight_off() void backlight_off()
 { {
     auto instance = pigpio_start(NULL,​ NULL);     auto instance = pigpio_start(NULL,​ NULL);
 +    if (instance != 0)
 +    {
 +        log_err("​pigpio_start did not return success"​);​
 +        return;
 +    }
 + 
 +    auto result = set_mode(instance,​ PIN, PI_OUTPUT);
 +    if (instance != 0)
 +    {
 +        log_err("​set_mode did not return success"​);​
 +        return;
 +    }
  
-    ​set_mode(instance, PIN, PI_OUTPUT); +    ​result = gpio_write(instance, PIN, 0); 
-    ​gpio_write(instance, PIN, 0); +    ​if (instance ​!= 0
 +    { 
 +        log_err("​gpio_write did not return success"​); 
 +        return; 
 +    } 
 + 
     pigpio_stop(instance);​     pigpio_stop(instance);​
 } }
  
 +void set_backlight(CARD16 state)
 +{
 +    switch (state)
 +    {
 +        case DPMSModeOn:
 +            log_info("​Turning backlight on");
 +            backlight_on();​
 +            break;
 +        case DPMSModeOff:​
 +            log_info("​Turning backlight off");
 +            backlight_off();​
 +            break;
 +        default:
 +            log_info("​Invalid state"​);​
 +            break;
 +    }
 +}
 + 
 int main(int argc, char *argv[]) int main(int argc, char *argv[])
 { {
 +    openlog("​backlight_service",​ LOG_NDELAY, LOG_USER);
 +
 +    log_info("​Starting"​);​
 +
     Display *dpy;     Display *dpy;
     dpy = XOpenDisplay(NULL);​     dpy = XOpenDisplay(NULL);​
     if (dpy == NULL)     if (dpy == NULL)
     {     {
-        ​fprintf(stderr, ​"%s:  unable ​to open display\n", argv[0]);+        ​log_err("Unable ​to open display"​);​
         exit(EXIT_FAILURE);​         exit(EXIT_FAILURE);​
     }     }
 + 
     BOOL onoff;     BOOL onoff;
 +    CARD16 last_state;
     CARD16 state;     CARD16 state;
 +
 +    // Initialize last_state
 +    if (!DPMSInfo(dpy,​ &state, &​onoff))
 +    {
 +        log_err("​DPMSInfo returned FALSE"​);​
 +    }
 +    set_backlight(state);​
 +    last_state = state;
 +
     while(true)     while(true)
     {     {
-        DPMSInfo(dpy,​ &state, &onoff)+        ​if (!DPMSInfo(dpy,​ &state, &​onoff))
-        if (onoff)+
         {         {
-            ​switch ​(state+            ​log_err("​DPMSInfo returned FALSE"); 
-            ​+            ​this_thread::sleep_for(seconds(2)); 
-                case DPMSModeOn: +            ​continue
-                    backlight_on(); +        } 
-                    break+ 
-                case DPMSModeOff:​ +        // If DPMS is not enabled, then display a message 
-                    ​backlight_off(); +        ​if ​(!onoff
-                    break+        { 
-                ​default: +            log_err("​DPMSInfo is not enabled"​)
-                    break+            ​this_thread::​sleep_for(seconds(2))
-            }+            ​continue; 
 +        ​} 
 + 
 +        if (last_state != state) 
 +        { 
 +            set_backlight(state);​ 
 + 
 +            last_state = state;
         }         }
         this_thread::​sleep_for(milliseconds(200));​         this_thread::​sleep_for(milliseconds(200));​
     }     }
 + 
     XCloseDisplay(dpy);​     XCloseDisplay(dpy);​
  
 +    log_info("​Exiting"​);​
 + 
     return 0;     return 0;
 } }
 </​code>​ </​code>​
 +
  
 ==== Compile ==== ==== Compile ====
Line 180: Line 264:
 #!/bin/bash #!/bin/bash
  
-export DISPLAY=":​0.0"​ +PWM_PIN=31 
- +PIN=34
-PIN=34 ​  # CPi-A/​B/​F/​S +
-#​PIN=44 ​ # CPi-C+
  
 +# Set pins as output
 +raspi-gpio set $PWM_PIN op
 raspi-gpio set $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 # Loop indefinitely updating backlight in sync with monitor status
Line 191: Line 286:
  
   # Check the current monitor status   # Check the current monitor status
-  ​current_status=$(xset q | grep "​Monitor is" | awk '​{print $3}')+  ​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 +    ​# Control backlight pin according to the monitor status 
-  if [ "$current_status" = "​On"​ ]; then +    if [ "$CURRENT_STATUS" = "​On"​ ]; then 
-    raspi-gpio set $PIN dh +      ​logger -p info -t "​backlight_service"​ -s "​Turning Backlight On" 
-  else +      ​raspi-gpio set $PIN dh 
-    raspi-gpio set $PIN dl+    else 
 +      ​logger -p info -t "​backlight_service"​ -s "​Turning Backlight Off" 
 +      ​raspi-gpio set $PIN dl 
 +    fi 
 +    LAST_STATUS=$CURRENT_STATUS
   fi   fi
  
Line 211: Line 314:
  
 Add the line ''​@xset dpms 0 0 {timeout_in_seconds}''​ to the //​autostart//​ file to set the timeout each time the desktop loads. Add the line ''​@xset dpms 0 0 {timeout_in_seconds}''​ to the //​autostart//​ file to set the timeout each time the desktop loads.
 +
 +Run ''​journalctl -t backlight_service -r''​ to view the log entries.
  
  
comfilepi/controlling_the_lcd_backlight/index.txt · Last modified: 2023/12/06 10:00 by COMFILE Technology