Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import serial
- import os
- import time
- import math
- #requires - pip install pyserial
- # and ddcutil
- #this reads the arduino serial device from a serial port
- #and converts it to a luminosity value through a function like y=mx+b
- #where i just checked my prefered brightgness value and the luminosity of the room when my its darkest and when its brigetest
- #so i got something like (aproximating from memory) 4 monitor brightess at 35 lumens room brightness,
- #and 28 monitor brightess at 400 lumens room brightness
- #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
- current_milli_time = lambda: int(round(time.time() * 1000))
- #realine() from the serial api was giving garbled/inconsistent output, so im using this instead
- #to parse the messages from the serial device
- def readln(srl):
- srl.reset_input_buffer()
- foundN = False
- foundR = False
- char = ''
- outs = list()
- while not(foundN and foundR) :
- char = str(srl.read())
- #print(char[3])
- if(foundN or char[3] == 'n'):
- #print(char[3])
- outs.append(char[2])
- foundN = True
- if (char[3] == 'r'):
- foundR = True
- del outs[0]
- del outs[len(outs) - 1]
- #print(''.join(outs))
- return ''.join(outs)
- ser = serial.Serial('/dev/ttyACM0')
- print("connected to: " + ser.portstr)
- currentX = 0
- adjCount = 1000
- while True:
- #ltime = int(current_milli_time())
- #read luminosity
- line = float(readln(ser))
- #print(line)
- #compute brighness
- x = round((0.052 * line) + 4.725)
- #clamp it to 26
- if x > 26 : x = 26
- #print(x)
- #if there is no significant change skip sending commands to the monitor
- if x != currentX:
- os.system("ddcutil -d 1 setvcp 10 %u" % x)
- adjCount += 1
- print(x)
- #print the incremental number of changes just so that i can check that it isnt borked
- print(adjCount)
- #print(current_milli_time())
- currentX = x
- #ctime = int(current_milli_time())
- #print (ctime - ltime)
- ser.close()
Add Comment
Please, Sign In to add comment