I followed the recommendation of @paul- and changed to a solution based
on Python/smbus. The following replaces the part with C-programming and
the shell script in my previous how-to. All related to I2C is still
valid.

*Requirement:
*You just need to install the extension "smbus-python3.6.tcz". pCP will
add all dependencies, i.e. all other required extensions.

*Script:
*
Code:
--------------------
    
  #!/usr/local/bin/python3
  import smbus
  import time
  
  
  # Define some constants from the datasheet
  ADDRESS    = 0x23 # I2C-Adresse des BH1750 if ADDR is seto GND
  #ADDRESS    = 0x5C # I2C-Adresse des BH1750 if ADDR is seto 3.3V
  
  POWER_DOWN = 0x00 # No active state
  POWER_ON   = 0x01 # Power on
  RESET      = 0x07 # Reset data register value
  
  # Start measurement at 1 lx resolution. Time typically 120 ms
  CONTINUOUS_HIGH_RES_MODE_1 = 0x10
  # Start measurement at 1l x resolution. Time typically 120 ms
  # Device is automatically set to Power Down after measurement.
  ONE_TIME_HIGH_RES_MODE_1 = 0x20
  
  # Set DEBUG to 'True' if you want to print some debug info
  DEBUG = False
  
  # Creating an I2C instance
  bh1750 = smbus.SMBus(1)
  
  def convertToNumber(data):
  # Simple function to convert 2 bytes of data
  # into a decimal number. Optional parameter 'decimals'
  # will round to specified number of decimal places.
  result=(data[1] + (256 * data[0])) / 1.2
  return (result)
  
  def readLight(addr=ADDRESS):
  # Read data from I2C interface
  data = bh1750.read_i2c_block_data(addr,ONE_TIME_HIGH_RES_MODE_1)
  return convertToNumber(data)
  
  def calcTargetVal(lightLevel):
  # Simple algorithm to calculate the new brightness value of the
  # display.
  MIN_THRESHOLD  = 10    # minimum LUX value to consider
  MAX_THRESHOLD  = 1010  # maximum LUX value to consider
  MIN_BRIGHTNESS = 10    # minimum brightness value of the display
  MAX_BRIGHTNESS = 255   # maximum brightness value of the display
  LUX_RANGE = MAX_THRESHOLD - MIN_THRESHOLD
  BRIGHTNESS_RANGE = MAX_BRIGHTNESS - MIN_BRIGHTNESS
  # STEPSIZE: actually the LUX value that represents one brightness step
  STEPSIZE = LUX_RANGE / BRIGHTNESS_RANGE
  
  if lightLevel < MIN_THRESHOLD:
  target = MIN_BRIGHTNESS
  elif lightLevel > MAX_THRESHOLD:
  target = MAX_BRIGHTNESS
  else:
  target = MIN_BRIGHTNESS + ((lightLevel + MIN_THRESHOLD) // STEPSIZE)
  
  return (int(target))
  
  
  def main():
  
  while True:
  # Measuring the current LUX value
  lightLevel=readLight()
  if DEBUG:
  print("Light Level : " + format(lightLevel,'.2f') + " lx")
  # Calculating the target brightness of the display
  dsply_brightness = calcTargetVal(lightLevel)
  # Setting the brightness value of the display
  dsply_file = '/sys/class/backlight/rpi_backlight/brightness'
  f = open(dsply_file, 'wt')
  f.write(str(dsply_brightness))
  f.close()
  if DEBUG:
  print("Display: " + str(dsply_brightness))
  # Wait 5 seconds before the next calculation
  time.sleep(5)
  
  # main() is only invoked if display-control.py runs as a script
  if __name__=="__main__":
  main()
  
--------------------

I stored this script as "display-control.py" in the folder
/home/tc/lux/.

To ensure that the Python script is executed with startup of pCP, it
must be started by the boot script /opt/bootlocal.sh. Together with the
I2C stuff, I added the following lines to /opt/bootlocal.sh:

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


This solution runs since yesterday on the pCP in my bedroom and I am
happy with it.


------------------------------------------------------------------------
jd68's Profile: http://forums.slimdevices.com/member.php?userid=30795
View this thread: http://forums.slimdevices.com/showthread.php?t=112460

_______________________________________________
unix mailing list
[email protected]
http://lists.slimdevices.com/mailman/listinfo/unix

Reply via email to