from threading import Thread
from time import sleep

class LedMatrix(Thread):
  def __init__(self, pin, brightness):
    threading.Thread.__init__(self) 
    self.pin = pin
    self.brightness = brightness
    self.color = 16*f'{0x000000:>8b}'
    
    GPIO.setup(self.pin,GPIO.OUT)
    
  def set_color(self,red,green,blue):
    self.color = 16*f'{green:>08b}{red:>08b}{blue:>08b}'
    
  def stop(self):
    self.running = False
  
  def get_brightness(self):
    if self.brightness == 100:
      return 60
    
    return 60
    
  def run(self):
    self.running = True
    
    while(self.running):
      color = self.color
    
      for bit in color:
        # Send 1
        if bit == '1':
          GPIO.output(self.pin,True)
          time.sleep(0.7*math.pow(10.0,-6))
          GPIO.output(self.pin,False)
          time.sleep(0.6*math.pow(10.0,-6))
        # Send 0
        else:
          GPIO.output(self.pin,True)
          time.sleep(0.35*math.pow(10.0,-6))
          GPIO.output(self.pin,False)
          time.sleep(0.8*math.pow(10.0,-6))
    
      # Reset
      GPIO.output(self.pin,False)
      sleep_time = self.get_brightness()
      time.sleep(sleep_time*math.pow(10.0,-6))

if __name__ == "__main__":
  pin = 10
  matrix = LedMatrix(pin)
  matrix.start()
  
  matrix.set_color(0,255,0)
  
  time.sleep(2)
  matrix.stop()