<commit_before>import time
import threading
from timeit import default_timer as timer
from lazylights import Lifx
from kafka import KafkaProducer

try:
   import Tkinter as tk
   import Queue as queue
except ImportError:
   import tkinter as tk
   import queue as queue
   
import RPi.GPIO as GPIO

KAFKA_CONNECT = False

MIN_REFRESH_INTERVAL = 5
LAST_UPDATE = 0
lastUpdateTimestamp = 0

lifx = Lifx(num_bulbs = 1)
@lifx.on_connected
def _connected():
    print "LIFX Connected"

MAXIMUM = 40.0
buffSize = 30

GPIO.setmode(GPIO.BCM)

top = tk.Tk()
top.attributes("-fullscreen", True)
top.configure(background = 'black')


onButton = tk.Button(top, text="on", bg="#001a00", fg='#004d00')
onButton.grid(column = 1, columnspan = 1, row = 0, sticky='nsew')

offButton = tk.Button(top, text="off", bg="#001a00", fg='#004d00')
offButton.grid(column = 0, columnspan = 1, row = 0, sticky='nsew')

sumR = 100
sumG = 100
sumB = 100
def printer(x, y):
   print ('x', x, 'y', y)
   return lambda event:1+2

barHeight = '20'
rCan = tk.Canvas(top, width='255', height=barHeight, relief='raised', bg='black', cursor='dot')
rCan.create_polygon(0, 0, 0, barHeight, sumR, barHeight, sumR, 0, fill = 'red')
rCan.grid(column=0, columnspan=2, row = 2, sticky='w', padx='5')

rCanStrVar = tk.StringVar()
rCanStrVar.set(str(100))
rLabel = tk.Label(top, anchor='center', bd=0, bg='black', cursor='dot', fg='red', textvariable=rCanStrVar)
rLabel.grid(column = 2, row = 2, sticky='w')

gCan = tk.Canvas(top, width='255', height=barHeight, relief='raised', cursor='dot', bg='black')
gCan.create_polygon(0, 0, 0, barHeight, sumG, barHeight, sumG, 0, fill='green')
gCan.grid(column=0, columnspan=2, row = 3, sticky='w', padx='5')
gCan.tag_bind(gCan, "<B1-Motion>", lambda x,y: printer(x, y))
 
gCanStrVar = tk.StringVar()
gCanStrVar.set(str(100))
gLabel = tk.Label(top, anchor='center', bd=0, cursor='dot', bg='black', fg='green', textvariable=gCanStrVar)
gLabel.grid(column = 2, row = 3, sticky='w')

bCan = tk.Canvas(top, width='255', height=barHeight, relief='raised', cursor='dot', bg='black')
bCan.create_polygon(0, 0, 0, barHeight, sumB, barHeight, sumB, 0, fill='blue')
bCan.grid(column=0, columnspan=2, row = 4, sticky='w', padx='5')

bCanStrVar = tk.StringVar()
bCanStrVar.set(str(100))
bLabel = tk.Label(top, anchor='center', bd=0, cursor='dot', fg='blue', bg='black', textvariable=bCanStrVar)
bLabel.grid(column = 2, row = 4, sticky='w')


top.columnconfigure(0, weight=3)
top.columnconfigure(1, weight=3)
top.columnconfigure(2, weight=1)
top.rowconfigure(0, weight=2)
top.rowconfigure(1, weight=1)
top.rowconfigure(2, weight=1)
top.rowconfigure(3, weight=1)
top.rowconfigure(4, weight=1)


def RGBtoHSB(r, g, b):
   print("R", r, "G", g, "B", b)
   hue = 0
   sat = 0
   bright = 0

   _max = max(r, g, b)/float(255)
   _min = min(r, g, b)/float(255)
   delta = float(_max - _min)
   
   bright = float(_max)
   if _max != 0:
      sat = delta / float(_max)
   else:
      sat = 0
   if sat != 0:
      if r == max(r, g, b):
         hue = float(g/float(255) - b/float(255)) / delta
      elif g == max(r, g, b):
         hue = 2 + (b/float(255) - r/float(255)) / delta
      else:
         hue = 4 + float(r/float(255) - g/float(255)) / delta
   else:
      hue = -1
   hue = hue * 60
   if hue < 0:
      hue+= 360
         
   return (hue, sat, bright)


def resend():
   print 'resend'
   if timer() > LAST_UPDATE + MIN_REFRESH_INTERVAL:
      hsb = RGBtoHSB(float(rCanStrVar.get()), float(gCanStrVar.get()), float(bCanStrVar.get()))
      print('HSB', hsb[0], " ", hsb[1], " ", hsb[2])
      
      lifx.set_light_state(hsb[0], hsb[1], hsb[2], 2700, timeout=2)
      print('updated')
   else:
      print("Timer:", timer(), " prv:", LAST_UPDATE)
   top.update_idletasks()
   top.update()

def updateHeight(can, val, _fill):
   if _fill == 'red':
      rCanStrVar.set(str(val))
   elif _fill == 'green':
      gCanStrVar.set(str(val))
   elif _fill == 'blue':
      bCanStrVar.set(str(val))
   can.create_polygon(0, 0, 0, barHeight, val, barHeight, val, 0, fill=_fill)
   can.create_polygon(val, 0, val, barHeight, 255, barHeight, 255, 0, fill='black')
   resend()
     
#t = threading.Timer(1, resend)
def updateCanvas(fill):
    #if fill == 'red':
    #    rCanStrVar.set(str(event.x))
    #elif fill == 'green':
    #    gCanStrVar.set(str(event.x))
    #elif fill == 'blue':
    #    bCanStrVar.set(str(event.x))
    #t.start()
    return lambda event:updateHeight(event.widget, event.x, fill)

#rCan.bind("<Button-1>", updateCanvas('red'))
rCan.tag_bind(rCan, "<B1-Motion>", lambda x,y:printer( x, y))

#gCan.bind("<Button-1>", updateCanvas('green'))
gCan.tag_bind(gCan, "<Button1-Motion>", lambda x,y:printer(x, y))
bCan.bind("<Button-1>", updateCanvas('blue'))


            
# Main program
lastUpdateTimestamp = timer()
LAST_UPDATE = timer()
if(KAFKA_CONNECT):
   producer = KafkaProducer(bootstrap_servers='172.16.31.10:2181', client_id='R_PI', api_version="0.10")


buffR = queue.Queue()
buffG = queue.Queue()<commit_msg>Added tagging to canvases<commit_after>import time
import threading
from timeit import default_timer as timer
from lazylights import Lifx
from kafka import KafkaProducer

try:
   import Tkinter as tk
   import Queue as queue
except ImportError:
   import tkinter as tk
   import queue as queue
   
import RPi.GPIO as GPIO

KAFKA_CONNECT = False

MIN_REFRESH_INTERVAL = 5
LAST_UPDATE = 0
lastUpdateTimestamp = 0

lifx = Lifx(num_bulbs = 1)
@lifx.on_connected
def _connected():
    print "LIFX Connected"

MAXIMUM = 40.0
buffSize = 30

GPIO.setmode(GPIO.BCM)

top = tk.Tk()
top.attributes("-fullscreen", True)
top.configure(background = 'black')


onButton = tk.Button(top, text="on", bg="#001a00", fg='#004d00')
onButton.grid(column = 1, columnspan = 1, row = 0, sticky='nsew')

offButton = tk.Button(top, text="off", bg="#001a00", fg='#004d00')
offButton.grid(column = 0, columnspan = 1, row = 0, sticky='nsew')

sumR = 100
sumG = 100
sumB = 100

def printer(x, y):
   print ('x', x, 'y', y)
   return lambda event:1+2

barHeight = '20'
rCan = tk.Canvas(top, width='255', height=barHeight, relief='raised', bg='black', cursor='dot')
rCanColorPoly = rCan.create_polygon(0, 0, 0, barHeight, sumR, barHeight, sumR, 0, fill = 'red')
rCanBlackPoly = rCan.create_polygon(sumR, 0, sumR, barHeight, 255, barHeight, 255, 0, fill='black')
rCan.itemconfig(rCanColorPoly, tags=('colorPoly'))
rCan.itemconfig(rCanBlackPoly, tags=('blackPoly'))
rCan.grid(column=0, columnspan=2, row = 2, sticky='w', padx='5')

rCanStrVar = tk.StringVar()
rCanStrVar.set(str(100))
rCanLabel = tk.Label(top, anchor='center', bd=0, bg='black', cursor='cross', fg='red', textvariable=rCanStrVar)
rLabel.grid(column = 2, row = 2, sticky='w')

gCan = tk.Canvas(top, width='255', height=barHeight, relief='raised', cursor='dot', bg='black')
gCanColorPoly = gCan.create_polygon(0, 0, 0, barHeight, sumG, barHeight, sumG, 0, fill='green')
gCanBlackPoly = gCan.create_polygon(sumG, 0, sumG, barHeight, 255, barHeight, 255, 0, fill='black')
gCan.itemconfig(gCanColorPoly, tags=('colorPoly'))
gCan.itemconfig(gCanBlackPoly, tags=('blackPoly'))
gCan.grid(column=0, columnspan=2, row = 3, sticky='w', padx='5')

gCanStrVar = tk.StringVar()
gCanStrVar.set(str(100))
gCanLabel = tk.Label(top, anchor='center', bd=0, cursor='dot', bg='black', fg='green', textvariable=gCanStrVar)
gCanLabel.grid(column = 2, row = 3, sticky='w')

bCan = tk.Canvas(top, width='255', height=barHeight, relief='raised', cursor='dot', bg='black')
bCanColorPoly = bCan.create_polygon(0, 0, 0, barHeight, sumB, barHeight, sumB, 0, fill='blue')
bCanBlackPoly = bCan.create_polygon(sumB, 0, sumB, barHeight, 255, barHeight, 255, 0, fill='black')
bCan.itemconfig(bCanColorPoly, tags=('colorPoly'))
bCan.itemconfig(bCanBlackPoly, tags=('blackPoly'))
bCan.grid(column=0, columnspan=2, row = 4, sticky='w', padx='5')

bCanStrVar = tk.StringVar()
bCanStrVar.set(str(100))
bCanLabel = tk.Label(top, anchor='center', bd=0, cursor='dot', fg='blue', bg='black', textvariable=bCanStrVar)
bLabel.grid(column = 2, row = 4, sticky='w')


top.columnconfigure(0, weight=3)
top.columnconfigure(1, weight=3)
top.columnconfigure(2, weight=1)
top.rowconfigure(0, weight=2)
top.rowconfigure(1, weight=1)
top.rowconfigure(2, weight=1)
top.rowconfigure(3, weight=1)
top.rowconfigure(4, weight=1)


def RGBtoHSB(r, g, b):
   print("R", r, "G", g, "B", b)
   hue = 0
   sat = 0
   bright = 0

   _max = max(r, g, b)/float(255)
   _min = min(r, g, b)/float(255)
   delta = float(_max - _min)
   
   bright = float(_max)
   if _max != 0:
      sat = delta / float(_max)
   else:
      sat = 0
   if sat != 0:
      if r == max(r, g, b):
         hue = float(g/float(255) - b/float(255)) / delta
      elif g == max(r, g, b):
         hue = 2 + (b/float(255) - r/float(255)) / delta
      else:
         hue = 4 + float(r/float(255) - g/float(255)) / delta
   else:
      hue = -1
   hue = hue * 60
   if hue < 0:
      hue+= 360
         
   return (hue, sat, bright)


def resend():
   print 'resend'
   if timer() > LAST_UPDATE + MIN_REFRESH_INTERVAL:
      hsb = RGBtoHSB(float(rCanStrVar.get()), float(gCanStrVar.get()), float(bCanStrVar.get()))
      print('HSB', hsb[0], " ", hsb[1], " ", hsb[2])
      
      lifx.set_light_state(hsb[0], hsb[1], hsb[2], 2700, timeout=2)
      print('updated')
   else:
      print("Timer:", timer(), " prv:", LAST_UPDATE)
   top.update_idletasks()
   top.update()

def updateHeight(can, val, _fill):
    //Create new bar
    colorPoly = can.create_polygon(0, 0, 0, barHeight, val, barHeight, val, 0, fill=_fill)
    blackPoly = can.create_polygon(val, 0, val, barHeight, 255, barHeight, 255, 0, fill='black')
    
    //Remove old bar
    can.delete('colorPoly')
    can.delete('blackPoly')
    
    //Configure new bar
    can.itemconfig(colorPoly, tags=('colorPoly'))
    can.itemconfig(blackPoly, tags=('blackPoly'))
    
    if _fill == 'red':
      rCanStrVar.set(str(val))
#      rCanColorPoly = colorPoly
#      rCanBlackPoly = blackPoly
    elif _fill == 'green':
      gCanStrVar.set(str(val))
#      gCanColorPoly = colorPoly
#      gcanBlackPoly = blackPoly
    elif _fill == 'blue':
      bCanStrVar.set(str(val))
#      bCanColorPoly = colorPoly
#      bCanBlackPoly = blackPoly
    resend()
     
#t = threading.Timer(1, resend)
def updateCanvas(fill):
    #if fill == 'red':
    #    rCanStrVar.set(str(event.x))
    #elif fill == 'green':
    #    gCanStrVar.set(str(event.x))
    #elif fill == 'blue':
    #    bCanStrVar.set(str(event.x))
    #t.start()
    return lambda event:updateHeight(event.widget, event.x, fill)

#rCan.bind("<Button-1>", updateCanvas('red'))
rCan.tag_bind(rCan.find_withtag("colorPoly"), "<B1-Motion>", lambda x,y:printer( x, y))
rCan.tag_bind(rCan.find_withtag("blackPoly"), "<B1-Motion>", lambda x,y:printer(x, y))

#gCan.bind("<Button-1>", updateCanvas('green'))
gCan.tag_bind(gCan, "<Button1-Motion>", lambda x,y:printer(x, y))
gCan.tag_bind(gCan.find_withtag("blackPoly"), "<B1-Motion>", lambda x,y:printer(x, y))


bCan.bind("<Button-1>", updateCanvas('blue'))


            
# Main program
lastUpdateTimestamp = timer()
LAST_UPDATE = timer()
if(KAFKA_CONNECT):
   producer = KafkaProducer(bootstrap_servers='172.16.31.10:2181', client_id='R_PI', api_version="0.10")


buffR = queue.Queue()
buffG = queue.Queue()
{'fraction_non_alphanumeric': 0.1752495362600477, 'fraction_numerical': 0.037540853281512236, 'mean_word_length': 4.2343966712898755, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4371089', 'n_tokens_mistral': 4690, 'n_tokens_neox': 4381, 'n_words': 978}