arduino-2020-07-04-1755.py
01234567890123456789012345678901234567890123456789012345678901234567890123456789









805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843                  844845846847848849850851852853854855856857858859860861862863











                            <----SKIPPED LINES---->




  d['altitude'] = angles[1]
  d['laser_red'] = laser[0]
  d['laser_green'] = laser[1]
  d['laser_blue'] = laser[2]
  d['led_red'] = led[0]
  d['led_green'] = led[1]
  d['led_blue'] = led[2]
  d['arduino_reset'] = reset

  return d


def HexColorToRGBTuple(hex_color):
  """Converts i.e.: #329a43 to (50, 154, 67)."""
  r = hex_color[1:3]
  g = hex_color[3:5]
  b = hex_color[5:7]
  return (int(r, 16), int(g, 16), int(b, 16))


def Gamma(c, gamma):
  """Converts a desired brightness 0..255 to a gamma-corrected PWM 0..255."""
  # Based on https://hackaday.com/2016/08/23/
  # rgb-leds-how-to-master-gamma-and-hue-for-perfect-brightness/
  return ((c / MAX_PWM) ** gamma) * MAX_PWM


def PerceivedBrightness(rgb, gamma, multiplier):
  """Calculates the perceived brightness when RGB scaled by multiplier."""
  total = sum([(color * multiplier)**gamma for color in rgb]) ** (1/gamma)
  return total


def GammaRGB(rgb, gamma, brightness=1.0):
  """Gamma converts an RGB tuple at a selected brightness 0%..100%."""
  r = Gamma(rgb[0], gamma)
  g = Gamma(rgb[1], gamma)
  b = Gamma(rgb[2], gamma)
  return round(r * brightness), round(g * brightness), round(b * brightness)




















def GetGamma(configuration):
  """Returns gamma stored in configuration file."""
  return configuration['servo_rgb_gamma'] / 10  # gamma saved as whole number


def ServoMain(to_arduino_q, to_parent_q, shutdown):
  """Main servo controller for projecting the plane position on a hemisphere.

  Takes the latest flight from the to_arduino_q and converts that to the current
  azimuth and altitude of the plane on a hemisphere.
  """
  sys.stderr = open(messageboard.STDERR_FILE, 'a')

  Log('Process started with process id %d' % os.getpid())

  # Ensures that the child can exit if the parent exits unexpectedly
  # docs.python.org/2/library/multiprocessing.html
  # #multiprocessing.Queue.cancel_join_thread




                            <----SKIPPED LINES---->





01234567890123456789012345678901234567890123456789012345678901234567890123456789









805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834 835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880











                            <----SKIPPED LINES---->




  d['altitude'] = angles[1]
  d['laser_red'] = laser[0]
  d['laser_green'] = laser[1]
  d['laser_blue'] = laser[2]
  d['led_red'] = led[0]
  d['led_green'] = led[1]
  d['led_blue'] = led[2]
  d['arduino_reset'] = reset

  return d


def HexColorToRGBTuple(hex_color):
  """Converts i.e.: #329a43 to (50, 154, 67)."""
  r = hex_color[1:3]
  g = hex_color[3:5]
  b = hex_color[5:7]
  return (int(r, 16), int(g, 16), int(b, 16))


def Gamma(c, gamma, max_value=MAX_PWM):
  """Converts a desired brightness 0..max to a gamma-corrected PWM 0..max."""
  # Based on https://hackaday.com/2016/08/23/
  # rgb-leds-how-to-master-gamma-and-hue-for-perfect-brightness/
  return ((c / max_value) ** gamma) * max_value


def PerceivedBrightness(rgb, gamma):
  """Calculates the perceived brightness when RGB scaled by multiplier."""
  return sum(rgb) ** (1/gamma)



def GammaRGB(rgb, gamma, brightness=1.0):
  """Gamma converts an RGB tuple at a selected brightness 0%..100%."""
  r = Gamma(rgb[0], gamma)
  g = Gamma(rgb[1], gamma)
  b = Gamma(rgb[2], gamma)
  corrected_rgb = (round(r), round(g), round(b))
  if brightness == 1:
    return corrected_rgb

  perceived_brightness = PerceivedBrightness(corrected_rgb, gamma)
  desired_brightness = perceived_brightness * brightness
  min_error = float('inf')
  for factor in range(101):
    scaling_factor = factor / 100
    dimmed_r = round(Gamma(scaling_factor * rgb[0], gamma))
    dimmed_g = round(Gamma(scaling_factor * rgb[1], gamma))
    dimmed_b = round(Gamma(scaling_factor * rgb[2], gamma))
    dimmed_rgb = dimmed_r, dimmed_g, dimmed_b
    dimmed_brightness = PerceivedBrightness(dimmed_rgb, gamma)
    error = abs(desired_brightness - dimmed_brightness)
    if error < min_error:
      min_error = error
      best_rgb = dimmed_rgb
  return best_rgb


def GetGamma(configuration):
  """Returns gamma stored in configuration file."""
  return configuration['servo_rgb_gamma'] / 10  # gamma saved as whole number


def ServoMain(to_arduino_q, to_parent_q, shutdown):
  """Main servo controller for projecting the plane position on a hemisphere.

  Takes the latest flight from the to_arduino_q and converts that to the current
  azimuth and altitude of the plane on a hemisphere.
  """
  sys.stderr = open(messageboard.STDERR_FILE, 'a')

  Log('Process started with process id %d' % os.getpid())

  # Ensures that the child can exit if the parent exits unexpectedly
  # docs.python.org/2/library/multiprocessing.html
  # #multiprocessing.Queue.cancel_join_thread




                            <----SKIPPED LINES---->