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









733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776








812813814815816817818819820821822823824825826827828829830831      832833834835836 837            838839840841842843844845846847848849850851852853854855856857











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





  comms_args = link, write_keys, write_format_tuple
  SendLEDTestMessage(*comms_args, MAX_PWM, MAX_PWM, MAX_PWM)

  for intensity in range(0, 255, 50):
    SendLEDTestMessage(*comms_args, intensity, 0, 0)

  for intensity in range(0, 255, 50):
    SendLEDTestMessage(*comms_args, 0, intensity, 0)

  for intensity in range(0, 255, 50):
    SendLEDTestMessage(*comms_args, 0, 0, intensity)

  SendLEDTestMessage(*comms_args, 0, 0, 0)


def ServoTestDim(link, write_keys, write_format_tuple, rgb, gamma):
  """Dim LEDs at selected color from 100% to 0% with gamma correction."""
  comms_args = link, write_keys, write_format_tuple

  for intensity in range(0, 101, 5):
    b = intensity / 100
    SendLEDTestMessage(*comms_args, *GammaRGB(rgb, gamma, brightness=b))
  for intensity in range(100, -1, 5):
    b = intensity / 100
    SendLEDTestMessage(*comms_args, *GammaRGB(rgb, gamma, brightness=b))


def ServoTestSweep(link, write_keys, write_format_tuple, altitude=45):
  """Sweep red laser around 360 degrees."""
  for azimuth in range(0, 360, 10):

    message_dict = GenerateServoMessage(
        laser=LASER_RED, angles=(azimuth, altitude))
    link.Write(DictToValueTuple(message_dict, write_keys, write_format_tuple))

    time.sleep(WRITE_DELAY_TIME)


last_angles = (0, 0)
last_laser = LASER_OFF
last_led = LED_OFF
def GenerateServoMessage(
    angles=None,




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




  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 round(((c / MAX_PWM) ** gamma) * MAX_PWM)








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

  return r, g, b














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









733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776








812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876











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





  comms_args = link, write_keys, write_format_tuple
  SendLEDTestMessage(*comms_args, MAX_PWM, MAX_PWM, MAX_PWM)

  for intensity in range(0, 255, 50):
    SendLEDTestMessage(*comms_args, intensity, 0, 0)

  for intensity in range(0, 255, 50):
    SendLEDTestMessage(*comms_args, 0, intensity, 0)

  for intensity in range(0, 255, 50):
    SendLEDTestMessage(*comms_args, 0, 0, intensity)

  SendLEDTestMessage(*comms_args, 0, 0, 0)


def ServoTestDim(link, write_keys, write_format_tuple, rgb, gamma):
  """Dim LEDs at selected color from 100% to 0% with gamma correction."""
  comms_args = link, write_keys, write_format_tuple

  for intensity in range(100, -1, -5):
    b = intensity / 100
    SendLEDTestMessage(*comms_args, *GammaRGB(rgb, gamma, brightness=b))
  for intensity in range(0, 101, 5):
    b = intensity / 100
    SendLEDTestMessage(*comms_args, *GammaRGB(rgb, gamma, brightness=b))


def ServoTestSweep(link, write_keys, write_format_tuple, altitude=45):
  """Sweep red laser around 360 degrees."""
  for azimuth in range(0, 360, 10):

    message_dict = GenerateServoMessage(
        laser=LASER_RED, angles=(azimuth, altitude))
    link.Write(DictToValueTuple(message_dict, write_keys, write_format_tuple))

    time.sleep(WRITE_DELAY_TIME)


last_angles = (0, 0)
last_laser = LASER_OFF
last_led = LED_OFF
def GenerateServoMessage(
    angles=None,




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




  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 round(((c / MAX_PWM) ** gamma) * MAX_PWM)


def PerceivedBrightness(rgb, gamma, brightness):
  """Calculates the perceived brightness when RGB scaled by 0..1 brightness."""
  total = sum([(color * brightness)**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 = round(Gamma(rgb[0], gamma) * brightness)
  g = round(Gamma(rgb[1], gamma) * brightness)
  b = round(Gamma(rgb[2], gamma) * brightness)
  if brightness == 1.0:
    return r, g, b

  perceived_full_brightness = PerceivedBrightness(rgb, gamma, 1.00)
  desired_brightness = perceived_full_brightness * brightness
  best_error = float('inf')
  for scaling_factor in range(101):
    perceived_brightness = PerceivedBrightness(rgb, gamma, scaling_factor / 100)
    error = abs(perceived_brightness - desired_brightness)
    if error < best_error:
      best_error = error
      best_scaling_factor = scaling_factor
  return (
      r * best_scaling_factor, g * best_scaling_factor, b * best_scaling_factor)


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---->