Announcement

Collapse
No announcement yet.

piCorePlayer and automatic screen brightness control.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    piCorePlayer and automatic screen brightness control.

    Hello community!
    I'm using Raspberry Pi 3+ with official 7 inch touchscreen in official case for all of this. It's working, playing music so well. But in one thing Squeezebox Touch was better. In darkness the screen of Touch becomes darker. There are so many sensors in market that can drive the brightness of screen. For example Yocto-Light, cheap Chinese sensor and many others. Does piCorePlayer can control the brightness of screen based on external light? May be it's already has a lib or programs to do this?
    I'm so sorry if my questions seems you so stupid. Sorry for my English too. It's my first steps on raspberry pi DIY.

    #2
    You can diy that all day long. I've used an i2c light sensor. from adafruit. Simple python/smbus libraries. But the yocto should be fine. Python dependencies can be a little fun, as some of them require alot of extra libraries. you should be able to install with pip --user option.
    piCorePlayer a small player for the Raspberry Pi in RAM.
    Homepage: https://www.picoreplayer.org

    Please donate if you like the piCorePlayer

    Comment


      #3
      Originally posted by paul- View Post
      You can diy that all day long. I've used an i2c light sensor. from adafruit. Simple python/smbus libraries. But the yocto should be fine. Python dependencies can be a little fun, as some of them require alot of extra libraries. you should be able to install with pip --user option.
      Do you mean this? It's marked as discontinued. About "user options". Where is it in pcp web interface?

      Comment


        #4
        Oh! It's seems you talk about VEML7700 LUX SENSOR.

        Comment


          #5
          Oh, it's not built into pCP, but everything should be there to DIY it.

          I used the TSL2561, but yes, its discontinued.....but any of the options should work out fine. Just make sure you understand dependencies.
          piCorePlayer a small player for the Raspberry Pi in RAM.
          Homepage: https://www.picoreplayer.org

          Please donate if you like the piCorePlayer

          Comment


            #6
            Step by step configuration is be better for me. What commands I'll can send to pcp, what module should I choose.

            Comment


              #7
              Hi WadDad,

              I am sure you will find for most light sensors a description how to use it with a RaspberryPi. Unfortunately, these descriptions are in most cases based on the Raspian OS and not on TinyCore Linux. For a transfer of these how-to's from Raspian to Tiny Core, you need to know how you can save changes in the TinyCore image (i.e. piCorePlayer image).

              pCP uses an image on the SD card to create a Linux system in the RAM of the RPi. Any changes that you do on the running system, are normally lost after a reboot of the pCP. On the homepage of TinyCoreLinux is a description how to save changes (see section 'Backup/Restore' at the end of the page):


              A useful picture:


              The according tools of the pCP is the command "pcp bu" on the CLI and the "Backup" button on the piCorePlayer web page (Main Page, section "Additional functions").
              Last edited by jd68; 2020-11-08, 15:55. Reason: added missing word "how"

              Comment


                #8
                How-To for BH1750

                I am using a BH1750 to control the brightness of my 7" display and will try to describe how I have done it.

                Beside of your RaspberryPi, display and the Audio-HAT, you need the following hardware:
                • BH1750 (digital light sensor)
                • Pin header 1 x 5 pin angled pitch 2.54, e.g. SL25WS5GC
                • Jumper cable (female - female)
                • SmartiPi Touch 2, case for official 7" Touchscreen Display and Raspberry Pi
                • Backcover for SmartiPi Touch 2, 35mm


                The BH1750 is a digital light sensor using the I2C bus and it is delivered with a straight pin header. The BH1750 is more compact with an angled pin header and fits better into the case.

                The SmartiPi Touch 2 is designed to house a RPi, the official 7" display and the official camera. The space for the camera is very helpful because it can be used by the BH1750. The Touch 2 has not enough space to house an HAT but it can be extended by an back cover. SmartiPi provides the back cover in two sizes: 15 mm and 35 mm. You will need the 35 mm version because the cabling on the GPIO header needs some space.

                This project uses the Hifiberry Amp2 as audio HAT and I prefer one single power supply so that the MeanWell GST60A18-P1J 18V DC 3.33A provides the power for all devices (RPi, display, Amp2 and BH1750). In case of a DAC, you can power your system via USB -- SmartiPi provides USB-splitter.

                The BH1750 is controlled in this project by a small C program and a shell script. The following extensions must be loaded via the web page of the piCorePlayer:
                These three are at least required for execution:
                • wiringpi.tcz
                • i2c-tools.tcz
                • bc.tcz

                Additional extensions required for compilation:
                • wiringpi-dev.tcz
                • i2c-tools-dev.tcz
                • gcc.tcz
                • gcc_base-dev.tcz
                • gcc_libs.tcz
                • gcc_libs-dev.tcz
                • glibc_base-dev.tcz
                • glibc_add_lib.tcz
                • binutils.tcz
                • flex.tcz

                some more extensions might be loaded automatically because of dependencies.

                Per default, I2C is not completely enabled on RPi with pCP. The part in config.txt is normally already done by pCP. Let's check:
                $ m1
                $ c1
                $ grep i2c config.txt


                You should get the following output:
                dtparam=i2c_arm=on,spi=on,i2s=on

                If not then you must add the entry 'dtparam=i2c_arm=on' to config.txt. Don't forget to save your changes:
                $ pcp bu

                Additionally, we need to load the i2c-dev module which is done by
                $ /sbin/modprobe i2c-dev

                You can check if the command was successful by
                $ lsmod | grep i2c
                One line of the output should be as folows:
                i2c_dev 20480 0

                The module is loaded only for the current runtime and not be available after a reboot. Therefore, we add the following lines at the end of the file /opt/bootlocal.sh:

                ### BH1750 start ------
                # Enabling kernel module i2c-dev
                /sbin/modprobe i2c-dev
                ### BH1750 stop ------


                Save your changes:
                $ pcp bu

                The brightness of the official 7" display is controlled by the content of the file '/sys/class/backlight/rpi_backlight/brightness'. It contains an integer value in the range of 1 ... 255. The higher the value, the higher the brightness. But you would need root permission to change the content of the file. Therefore, we make this file writable for other users, too. This is done by a new udev rule.

                New file:
                /etc/udev/rules.d/51-rpi-display.rules

                Content:
                # udev rules to allow write access to all users for RaspberryPi 7" Touch Display
                SUBSYSTEM=="backlight",RUN+="/bin/chmod 666 /sys/class/backlight/%k/brightness"


                Adding filename and path to /opt/.filetool.lst and backup changes via pCP web page (or via 'pcp bu' on CLI). Note: The file '.filetool.lst' contains the list of files and folders which are saved by 'pcp bu'.

                Before we connect the BH1750, we should reboot the system to activate the last changes.

                The light-sensitive part of the sensor is the small black rectangle above the lettering "BH1750". The angled pin header should be soldered on the sensor board so that the pins are on the side opposite of the sensor (otherwise the pins will cover the sensor which is not what we want).

                The five pins are now connected to the GPIO header of the RPi as described below:
                Code:
                  BH1750 |   RPi       |   Comment
                  -------|---------|------------------
                   VCC     | Pin  1    |  3.3V (Power)
                   GND      | Pin  9    |  GND (Ground)
                   SCL      | Pin  5   |  GPIO 3 (SCL1)
                   SDA      | Pin  3   |  GPIO 2 (SDA1)
                   ADDR    | Pin 14   |  GND (Ground)
                Note: I use pin 2 (5V) and 6 (GND) as power supply for the display.

                The RPi has two I2C channels (i2c0 and i2c1). I am using i2c1 which is available via the pins 3 and 5. The channel i2c1 (and its pins) is also used by the Hifiberry HATs (and probably other audio HATs) but I2C can handle several devices on one channel -- as long as they have different addresses. The channel i2c0 is available via the pins 27 and 28 and it can be activated via config.txt but it is normally reserved for the EEPROMs of an HAT. I recommend not to activate i2c0 at all because it causes instabilities in my system.

                If the sensor is recognized by the I2C bus can be checked by the command 'i2cdetect -y 1':
                Code:
                $ i2cdetect -y 1
                       0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
                  00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
                  10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
                  20: -- -- -- 23 -- -- -- -- -- -- -- -- -- -- -- -- 
                  30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
                  40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
                  50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
                  60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
                  70: -- -- -- -- -- -- -- --
                The address 0x23h represents the BH1750.

                A simple test program from sends the control code for a one-time measurement in high resolution mode with 1 lux precision to the BH1750. The value 0x20h, corresponds to 00100000. After a short wait, the code reads the measurement results. The bytes then return to the correct order, and display the measurement result.

                Code:
                $ cat lux.c
                  #include <wiringPiI2C.h>
                  #include <stdio.h>
                  #include <unistd.h>
                  
                  int main (void) {
                    /* BH1750 uses address 0x23 on the I2C bus */
                    int handle = wiringPiI2CSetup(0x23);
                    if (handle == -1) 
                      return 1;
                    /* The control code 0x20 tells BH1750 to execute a One-Time measurement
                       with high resolution (i.e. 1 lx resolution) */
                    wiringPiI2CWrite(handle,0x20);
                    sleep(1);  // Measurement Time is typically 120ms.
                    int word=wiringPiI2CReadReg16(handle,0x00);
                    int lux=((word & 0xff00)>>8) | ((word & 0x00ff)<<8);
                    printf("%d \n",lux);
                  
                    return 0 ;
                  }
                I stored the file in a new folder of the $HOME-directory of the user 'tc', i.e. under '/home/tc/lux/'. The complete $HOME-directory is normally saved with a 'pcp bu'.

                Save your changes:
                $ pcp bu

                Now we compile our source code, rename the executable and set the permissions accordingly.
                $ cc lux.c -lwiringPi
                $ mv a.out lux && chmod 755 lux


                Some results when running 'lux':
                [email protected]:~/lux$ ./lux
                20
                [email protected]:~/lux$ ./lux
                304


                Next we need a tool that sets the brightness of the display according to the measured light intensity. I use a shell script for this task because it can be easily adapted. Currently it is still a very basic script but it fulfils its main task.

                Code:
                $ cat display-control.sh
                #!/bin/sh
                
                WDIR=/home/tc/lux
                # if the measured value is below MIN_THRESHOLD then the display is set to MIN_BRIGHTNESS
                MIN_THRESHOLD=10
                MIN_BRIGHTNESS=10
                # if the measured value is above MAX_THRESHOLD then the display is set to MAX_BRIGHTNESS
                MAX_THRESHOLD=1010
                MAX_BRIGHTNESS=255
                LUX_RANGE=`echo "$MAX_THRESHOLD-$MIN_THRESHOLD" | bc`
                BRIGHTNESS_RANGE=`echo "$MAX_BRIGHTNESS-$MIN_BRIGHTNESS" | bc`
                STEPSIZE=$(echo "scale=2 ; $LUX_RANGE/$BRIGHTNESS_RANGE" | bc -l | cut -f1 -d\.);
                # time interval between two measurements
                INTERVAL=5
                
                # Debugging ...
                echo `date` > $WDIR/deleteme
                
                # Let's wait until pCP is up & running
                sleep 20
                
                while true; do
                  # Measuring the current LUX value
                  LUX=$($WDIR/lux)
                  # Calculating the target brightness of the display
                  if [ $LUX -lt $MIN_THRESHOLD ]; then
                    TRGT=$MIN_BRIGHTNESS
                  elif [ $LUX -gt $MAX_THRESHOLD ]; then
                    TRGT=$MAX_BRIGHTNESS
                  else
                    TRGT=$(echo "$MIN_BRIGHTNESS+(($LUX-$MIN_THRESHOLD)/$STEPSIZE)" | bc -l | cut -f1 -d\.);
                  fi
                
                    # Setting the brightness value of the display
                    echo $TRGT > /sys/class/backlight/rpi_backlight/brightness
                    
                    # Uncomment for debugging & testing
                #    echo $LUX
                #    echo $TRGT
                    
                    # Wait some seconds before the next calculation
                    sleep $INTERVAL
                done
                For verifying the configuration, you can execute the script manually (you should uncomment the lines with 'echo $LUX' and 'echo $TRGT').

                $ ./display-control.sh

                The script prints now the measured light intensity and the target value for the brightness of the display.

                Save your changes:
                $ pcp bu

                This script must be executed automatically when the piCorePlayer has started. This is done by enhancing the BH1750 part in the file /opt/bootlocal.sh:

                ### BH1750 start ------
                # Enabling kernel module i2c-dev
                /sbin/modprobe i2c-dev
                # starting shell script to control display brightness
                /home/tc/lux/display-control.sh
                ### BH1750 stop ------


                Save your changes:
                $ pcp bu

                tbc
                Last edited by jd68; 2020-07-04, 10:46. Reason: using CODE tags

                Comment


                  #9
                  How-To for BH1750 (part 2)

                  (my previous post reached the maximum number of 10000 letters)

                  The software part is nearly done now. In my case, the content of the display was upside down so that I needed to change the rotation of the display. This is done in the file config.txt. Search for 'lcd_rotate' and change its value accordingly:
                  # Screen rotation 0 => 0 degrees, 2 => 180 degrees
                  lcd_rotate=0


                  Save your changes:
                  $ pcp bu

                  The assembling of the hardware (case, display, RPi and HAT) is described on smarticase.com/setup. I placed the BH1750 at tzhe place which is intended for the official camera. Even the jumper cables of the BH1750 take the same path as the ribbon cable of the camera.

                  Comment


                    #10
                    I would recommend you get away from wiringpi. It is depreciated, and will not be supported down the road. (It also does not fully work with the pi4)

                    you can do all of the i2c stuff with python/smbus
                    piCorePlayer a small player for the Raspberry Pi in RAM.
                    Homepage: https://www.picoreplayer.org

                    Please donate if you like the piCorePlayer

                    Comment


                      #11
                      Alternative to/successor of wiringpi?

                      Thanks for the info about wiringpi, paul-.

                      Do you know whether there is an alternative to wiringpi for C/C++ programming?

                      Nevertheless, I will try to create the same task with Python.

                      Comment


                        #12
                        With this sensor and code (setting aside wiringpi), does the brightness go dimmer or darker than what can be achieved by manually setting the screen brightness to 0, which is bright enough for me during the day and too bright for nighttime?

                        I would love to have a way to make my screen even dimmer. Until I do, my RPi 4 with its 7 inch touchscreen is stuck in the living room and cannot come into the bedroom.

                        Comment


                          #13
                          Originally posted by jd68 View Post
                          Thanks for the info about wiringpi, paul-.

                          Do you know whether there is an alternative to wiringpi for C/C++ programming?

                          Nevertheless, I will try to create the same task with Python.
                          pigpio is what I'm using for GPIO now. It's a little less friendly to deal with, but works with all boards. We do have that extension for pCP.

                          I still think python is the way to go for simple (Not fast communications)
                          piCorePlayer a small player for the Raspberry Pi in RAM.
                          Homepage: https://www.picoreplayer.org

                          Please donate if you like the piCorePlayer

                          Comment


                            #14
                            Originally posted by paul- View Post
                            pigpio is what I'm using for GPIO now. It's a little less friendly to deal with, but works with all boards. We do have that extension for pCP.

                            I still think python is the way to go for simple (Not fast communications)
                            Hello,

                            I wanted to play around with pigpio and python. But pigpio doesn't seem to be available in python (import error). I installed the pigpio.tcz extension and the pigpio daemon is running. But there is no pigpio file in the /usr/local/lib/python2.7/dist-packages folder.
                            What can be the problem?

                            Best regards
                            PiPlayer

                            Comment


                              #15
                              I did not build pigpio with python....

                              Use RPi-GPIO-python3.6.tcz for general GPIO access and smbus-python3.6.tcz for i2c programming.
                              piCorePlayer a small player for the Raspberry Pi in RAM.
                              Homepage: https://www.picoreplayer.org

                              Please donate if you like the piCorePlayer

                              Comment

                              Working...
                              X