Guest User

autobrightness

a guest
Aug 21st, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.13 KB | None | 0 0
  1. import serial
  2. import os
  3. import time
  4. import math
  5.  
  6.  
  7. #requires - pip install pyserial
  8. # and ddcutil
  9.  
  10. #this reads the arduino serial device from a serial port
  11. #and converts it to a luminosity value through a function like y=mx+b
  12. #where i just checked my prefered brightgness value and the luminosity of the room when my its darkest and when its brigetest
  13. #so i got something like (aproximating from memory) 4 monitor brightess at 35 lumens room brightness,
  14. #and 28 monitor brightess at 400 lumens room brightness
  15. #this got me to y=0.052*x+ 4.725 5 is the minimum brightness value and the code caps the max brightness at 26
  16.  
  17.  
  18.  
  19. current_milli_time = lambda: int(round(time.time() * 1000))
  20.  
  21. #realine() from the serial api was giving garbled/inconsistent output, so im using this instead
  22. #to parse the messages from the serial device
  23. def readln(srl):
  24.     srl.reset_input_buffer()
  25.     foundN = False
  26.     foundR = False
  27.     char = ''
  28.     outs = list()
  29.     while not(foundN and foundR) :
  30.         char = str(srl.read())
  31.         #print(char[3])
  32.         if(foundN or char[3] == 'n'):
  33.             #print(char[3])
  34.             outs.append(char[2])
  35.             foundN = True
  36.             if (char[3] == 'r'):
  37.                 foundR = True
  38.  
  39.     del outs[0]
  40.     del outs[len(outs) - 1]
  41.     #print(''.join(outs))
  42.     return ''.join(outs)
  43.  
  44.  
  45. ser = serial.Serial('/dev/ttyACM0')
  46.  
  47. print("connected to: " + ser.portstr)
  48.  
  49.  
  50. currentX = 0
  51. adjCount = 1000
  52. while True:
  53.     #ltime = int(current_milli_time())
  54.     #read luminosity
  55.     line = float(readln(ser))  
  56.     #print(line)
  57.     #compute brighness
  58.     x = round((0.052 * line) + 4.725)
  59.     #clamp it to 26
  60.     if x > 26 : x = 26
  61.     #print(x)
  62.     #if there is no significant change skip sending commands to the monitor
  63.     if x != currentX:
  64.         os.system("ddcutil -d 1 setvcp 10 %u" % x)
  65.         adjCount += 1
  66.         print(x)
  67.         #print the incremental number of changes just so that i can check that it isnt borked
  68.         print(adjCount)
  69.         #print(current_milli_time())
  70.         currentX = x
  71.     #ctime = int(current_milli_time())
  72.     #print (ctime - ltime)
  73.  
  74.  
  75. ser.close()
Add Comment
Please, Sign In to add comment