Advertisement
Guest User

Python-handle for LED controller

a guest
Oct 26th, 2016
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.32 KB | None | 0 0
  1. import ImageGrab
  2. import time
  3. import serial
  4. import ImageGrab
  5. from win32api import GetSystemMetrics
  6.  
  7. #arduino
  8. # port = 'COM5'
  9. # serial = serial.Serial(port,9600,timeout=0)
  10. # time.sleep(2)
  11. ard = serial.Serial()
  12. def tryPort(com):
  13.     port_name = "COM"+str(com)
  14.     try:
  15.         global ard
  16.         ard = serial.Serial(port_name,9600,timeout=0)
  17.         return True
  18.     except:
  19.         print(port_name+" unavailable")
  20.         return False
  21.  
  22. def init():
  23.     available = False
  24.     for comPort in range (1,12):
  25.         if tryPort(comPort):
  26.             available = True
  27.             print ("Connection established at COM"+str(comPort)+", please wait.")
  28.             time.sleep(2)
  29.             break
  30.     if available:
  31.         #print ("Testing LEDs... Initializing RGB sequence")
  32.         # flash()
  33.         print ("Everything OK")
  34.         select_region()
  35.        
  36.        
  37. def flash():
  38.     ard.flush()
  39.     ard.write("255,0,0X")
  40.     time.sleep(0.5)
  41.     ard.write("0,255,0X")
  42.     time.sleep(0.5)
  43.     ard.write("0,0,255X")
  44.     time.sleep(0.5)
  45.     ard.write("0,0,0X")
  46.    
  47. active_region = 0
  48. def select_region():
  49.     print("Choose screen region (top/middle/bottom)")
  50.     global active_region
  51.     region = raw_input(">")
  52.     if region=="top": active_region = 500
  53.     elif region=="middle": active_region = h/2
  54.     elif region=="bottom": active_region = h-150
  55.     else: print "No valid region selected, selected middle as default"
  56.     print("Scanning pixels at height: "+str(h-active_region)+" px")
  57.  
  58.  
  59.  
  60. #window
  61. w = GetSystemMetrics(0)
  62. h = GetSystemMetrics(1)
  63. x1 = w/2-w/3
  64. x2 = w/2+w/3
  65.  
  66. def avg_color():
  67.     image = ImageGrab.grab()
  68.     # print 'width:',w,', height:',h
  69.     r,g,b=0,0,0
  70.     count = 0
  71.     temp_col = "" # used for y-values
  72.     y = active_region
  73.     _range = range(w/2-1000, w/2+1000+1, 400)
  74.     # print(_range)
  75.     for k in _range:
  76.         count+=2
  77.         color = str(image.getpixel((k,y))).translate(None,'() ')
  78.         rgb = color.split(',')
  79.         r+=int(rgb[0])
  80.         g+=int(rgb[1])
  81.         b+=int(rgb[2])
  82.         color = str(image.getpixel((k+200,int(y-y/3)))).translate(None,'() ')
  83.         rgb = color.split(',')
  84.         r+=int(rgb[0])
  85.         g+=int(rgb[1])
  86.         b+=int(rgb[2])
  87.     # output: (r, g, b)->r,g,b
  88.     r,g,b = r/count, g/count, b/count
  89.     # g = int(g*0.75)
  90.     # b = int(b*0.3)
  91.     r = int(r*1.5)
  92.     g= int(g*1.5)
  93.     b = int(b*1.1)
  94.  
  95.     return str(r)+","+str(g)+","+str(b)+"X"
  96.  
  97. def main():
  98.     init()
  99.     prev_col = None
  100.     while (True):
  101.         ard.flush()
  102.         col = avg_color()
  103.         if col!=prev_col:
  104.             ard.write(col)
  105.         prev_col = col
  106.         time.sleep(0.45)
  107.  
  108. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement