arduino-2020-07-02-0943.py
01234567890123456789012345678901234567890123456789012345678901234567890123456789









4950515253545556575859606162636465666768 6970717273747576777879808182838485868788








789790791792793794795796797798799800801802803804805806807808          809810811812813814815816817818819820821822823824825826827828








895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935











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





CONNECTION_FLAG_BLUETOOTH = 1
CONNECTION_FLAG_USB = 2
CONNECTION_FLAG_SIMULATED = 3
RASPBERRY_PI = psutil.sys.platform.title() == 'Linux'

SN_SERVO = '5583834303435111C1A0'
SERVO_CONNECTION = (CONNECTION_FLAG_BLUETOOTH, (2, '98:D3:11:FC:42:16', 1))

SN_REMOTE = '75835343130351802272'
REMOTE_CONNECTION = (CONNECTION_FLAG_BLUETOOTH, (1, '98:D3:91:FD:B3:C9', 1))

LASER_OFF = (False, False, False)
LASER_ALL = (True, True, True)
LASER_RED = (True, False, False)
LASER_GREEN = (False, True, False)
LASER_BLUE = (False, False, True)

LED_OFF = (0, 0, 0)
MAX_PWM = 255


if SIMULATE_ARDUINO:
  SERVO_CONNECTION = (
      CONNECTION_FLAG_SIMULATED, (SERVO_SIMULATED_IN, SERVO_SIMULATED_OUT))
  REMOTE_CONNECTION = (
      CONNECTION_FLAG_SIMULATED, (REMOTE_SIMULATED_IN, REMOTE_SIMULATED_OUT))

KEY_NOT_PRESENT_STRING = 'N/A'

DISP_LAST_FLIGHT_NUMB_ORIG_DEST = 0
DISP_LAST_FLIGHT_AZIMUTH_ELEVATION = 1
DISP_FLIGHT_COUNT_LAST_SEEN = 2
DISP_RADIO_RANGE = 3
DISPLAY_MODE_NAMES = [
    'LAST_FLIGHT_NUMB_ORIG_DEST', 'LAST_FLIGHT_AZIMUTH_ELEVATION',
    'FLIGHT_COUNT_LAST_SEEN', 'RADIO_RANGE']

WRITE_DELAY_TIME = 0.2  # write to arduino every n seconds
READ_DELAY_TIME_SERVO = 1  # read from arduino every n seconds
READ_DELAY_TIME_REMOTE = 0.1  # read from arduino every n seconds




                            <----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 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
  to_arduino_q.cancel_join_thread()
  to_parent_q.cancel_join_thread()

  #pylint: disable = bad-whitespace
  write_config = (
      ('azimuth',       'f'),  # 4 bytes
      ('altitude',      'f'),  # 4 bytes




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





    current_angles = AzimuthAltitude(flight, now)
    if time.time() > next_write:
      if (current_angles and
          current_angles[1] >=
          configuration['minimum_altitude_servo_tracking'] and
          configuration['servo_mode'] in ('laser_only', 'both')):

        if VERBOSE:
          Log('Flight #: %s current_angles: %s' % (
              messageboard.DisplayFlightNumber(flight), str(current_angles)))

        laser_rgb = LaserRGBFlight(flight)
        message_dict = GenerateServoMessage(
            laser=laser_rgb, angles=current_angles, led=LED_OFF)

      elif configuration['servo_mode'] == 'laser_only':
        message_dict = GenerateServoMessage(laser=LASER_OFF, led=LED_OFF)

      else:
        rgb_tuple = HexColorToRGBTuple(configuration['led_color'])
        message_dict = GenerateServoMessage(laser=LASER_OFF, led=rgb_tuple)

      message_tuple = DictToValueTuple(
          message_dict, write_keys, write_format_tuple)
      link.Write(message_tuple)

      next_write = time.time() + WRITE_DELAY_TIME

  # One final write telling Arduino to do a software reset
  message_dict = GenerateServoMessage(laser=LASER_OFF, reset=True)
  message_tuple = DictToValueTuple(message_dict, write_keys, write_format_tuple)
  link.Write(message_tuple)

  link.Close(SHUTDOWN_TEXT)


def LaserRGBFlight(flight):
  """Based on flight attributes, set the laser."""
  # Possible assignment based on:
  #   - ascending / descending / level




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





01234567890123456789012345678901234567890123456789012345678901234567890123456789









4950515253545556575859606162636465666768697071727374757677787980818283848586878889








790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839








906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946











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





CONNECTION_FLAG_BLUETOOTH = 1
CONNECTION_FLAG_USB = 2
CONNECTION_FLAG_SIMULATED = 3
RASPBERRY_PI = psutil.sys.platform.title() == 'Linux'

SN_SERVO = '5583834303435111C1A0'
SERVO_CONNECTION = (CONNECTION_FLAG_BLUETOOTH, (2, '98:D3:11:FC:42:16', 1))

SN_REMOTE = '75835343130351802272'
REMOTE_CONNECTION = (CONNECTION_FLAG_BLUETOOTH, (1, '98:D3:91:FD:B3:C9', 1))

LASER_OFF = (False, False, False)
LASER_ALL = (True, True, True)
LASER_RED = (True, False, False)
LASER_GREEN = (False, True, False)
LASER_BLUE = (False, False, True)

LED_OFF = (0, 0, 0)
MAX_PWM = 255
GAMMA = 2.2

if SIMULATE_ARDUINO:
  SERVO_CONNECTION = (
      CONNECTION_FLAG_SIMULATED, (SERVO_SIMULATED_IN, SERVO_SIMULATED_OUT))
  REMOTE_CONNECTION = (
      CONNECTION_FLAG_SIMULATED, (REMOTE_SIMULATED_IN, REMOTE_SIMULATED_OUT))

KEY_NOT_PRESENT_STRING = 'N/A'

DISP_LAST_FLIGHT_NUMB_ORIG_DEST = 0
DISP_LAST_FLIGHT_AZIMUTH_ELEVATION = 1
DISP_FLIGHT_COUNT_LAST_SEEN = 2
DISP_RADIO_RANGE = 3
DISPLAY_MODE_NAMES = [
    'LAST_FLIGHT_NUMB_ORIG_DEST', 'LAST_FLIGHT_AZIMUTH_ELEVATION',
    'FLIGHT_COUNT_LAST_SEEN', 'RADIO_RANGE']

WRITE_DELAY_TIME = 0.2  # write to arduino every n seconds
READ_DELAY_TIME_SERVO = 1  # read from arduino every n seconds
READ_DELAY_TIME_REMOTE = 0.1  # read from arduino every n seconds




                            <----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):
  """Converts a desired brightness 0..255 to a gamma-corrected PWM 0..255."""
  return round(((c / MAX_PWM) ** GAMMA) * MAX_PWM)


def GammaRGB(rgb):
  """Gamma converts an RGB tuple."""
  return Gamma(rgb[0]), Gamma(rgb[1]), Gamma(rgb[2])


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
  to_arduino_q.cancel_join_thread()
  to_parent_q.cancel_join_thread()

  #pylint: disable = bad-whitespace
  write_config = (
      ('azimuth',       'f'),  # 4 bytes
      ('altitude',      'f'),  # 4 bytes




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





    current_angles = AzimuthAltitude(flight, now)
    if time.time() > next_write:
      if (current_angles and
          current_angles[1] >=
          configuration['minimum_altitude_servo_tracking'] and
          configuration['servo_mode'] in ('laser_only', 'both')):

        if VERBOSE:
          Log('Flight #: %s current_angles: %s' % (
              messageboard.DisplayFlightNumber(flight), str(current_angles)))

        laser_rgb = LaserRGBFlight(flight)
        message_dict = GenerateServoMessage(
            laser=laser_rgb, angles=current_angles, led=LED_OFF)

      elif configuration['servo_mode'] == 'laser_only':
        message_dict = GenerateServoMessage(laser=LASER_OFF, led=LED_OFF)

      else:
        rgb_tuple = GammaRGB(HexColorToRGBTuple(configuration['led_color']))
        message_dict = GenerateServoMessage(laser=LASER_OFF, led=rgb_tuple)

      message_tuple = DictToValueTuple(
          message_dict, write_keys, write_format_tuple)
      link.Write(message_tuple)

      next_write = time.time() + WRITE_DELAY_TIME

  # One final write telling Arduino to do a software reset
  message_dict = GenerateServoMessage(laser=LASER_OFF, reset=True)
  message_tuple = DictToValueTuple(message_dict, write_keys, write_format_tuple)
  link.Write(message_tuple)

  link.Close(SHUTDOWN_TEXT)


def LaserRGBFlight(flight):
  """Based on flight attributes, set the laser."""
  # Possible assignment based on:
  #   - ascending / descending / level




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