arduino-2020-07-17-0835.py
01234567890123456789012345678901234567890123456789012345678901234567890123456789









881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927








952953954955956957958959960961962963964965966967968969970971 972973974975976977978979980981982983984985986987988989990991992993994995996











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




  # 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
      ('laser_red',       '?'),  # 1 byte
      ('laser_green',     '?'),  # 1 byte
      ('laser_blue',      '?'),  # 1 byte
      ('led_red',         'H'),  # 2 bytes
      ('led_green',       'H'),  # 2 bytes
      ('led_blue',        'H'),  # 2 bytes
      ('mode_switch_ack', '?'),  # 1 byte
      ('arduino_reset',   '?'),  # 1 byte
  )

  read_config = (
      ('servo_mode', '?'),  # 1 byte
      ('millis',     'L'),  # 4 bytes
  )
  #pylint: enable = bad-whitespace
  write_keys, write_format_tuple, write_format_string = SplitFormat(
      write_config)
  read_keys, unused_read_format_tuple, read_format_string = SplitFormat(
      read_config)

  # write_format: azimuth, altitude, R, G, & B intensity
  # read heartbeat: mode change & millis
  link = Serial(
      *SERVO_CONNECTION, read_timeout=60,
      error_pin=messageboard.GPIO_ERROR_ARDUINO_SERVO_CONNECTION,
      to_parent_q=to_parent_q,
      read_format=read_format_string, write_format=write_format_string,
      name='Servo')
  link.Open()

  last_flight = {}
  flight, json_desc_dict, configuration, additional_attr = InitialMessageValues(
      to_arduino_q)
  next_read = 0
  next_write = 0
  now = GetNow(json_desc_dict, additional_attr)
  gamma = GetGamma(configuration)
  brightness = 1.0




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




      new_flight = DifferentFlights(flight, last_flight)
      if new_flight:
        Log('Flight changed from %s to %s' % (
            messageboard.DisplayFlightNumber(last_flight),
            messageboard.DisplayFlightNumber(flight)
        ), ser=link)

        # Turn off laser so line isn't traced while it moves to new position
        message_dict = GenerateServoMessage(laser=LASER_OFF)
        message_tuple = DictToValueTuple(
            message_dict, write_keys, write_format_tuple)
        link.Write(message_tuple)

      last_flight = flight

    mode_switch_ack = False
    if time.time() >= next_read:
      bytes_read = []
      values_t = link.Read(bytes_read=bytes_read)
      values_d = dict(zip(read_keys, values_t))

      next_read = time.time() + READ_DELAY_TIME_SERVO
      if bytes_read and VERBOSE:
        Log('Read by Servo: %s' % str(values_d))

      if values_d['servo_mode']:

        # makes a copy so as to not modify underlying config; we don't want
        # to modify underlying because otherwise the settings will bounce
        # around (values read from disk -> new values set by arduino -> old
        # values from disk -> new values from disk after Arduino update).
        configuration = dict(configuration)

        # LED only -> Laser only -> Both
        sequence = ('led_only', 'laser_only', 'both')
        old_name = configuration['servo_mode']
        old_id = sequence.index(old_name)
        new_id = (old_id + 1) % len(sequence)
        new_name = sequence[new_id]
        Log('Hemisphere mode updated from %s to %s' % (old_name, new_name))
        configuration['servo_mode'] = new_name
        settings_string = messageboard.BuildSettings(configuration)
        to_parent_q.put(('update_configuration', (settings_string, )))
        mode_switch_ack = True

    now = GetNow(json_desc_dict, additional_attr)




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





01234567890123456789012345678901234567890123456789012345678901234567890123456789









881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927








952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997











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




  # 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
      ('laser_red',       '?'),  # 1 byte
      ('laser_green',     '?'),  # 1 byte
      ('laser_blue',      '?'),  # 1 byte
      ('led_red',         'H'),  # 2 bytes
      ('led_green',       'H'),  # 2 bytes
      ('led_blue',        'H'),  # 2 bytes
      ('mode_switch_ack', '?'),  # 1 byte
      ('arduino_reset',   '?'),  # 1 byte
  )

  read_config = (
      ('mode_switch', '?'),  # 1 byte
      ('millis',      'L'),  # 4 bytes
  )
  #pylint: enable = bad-whitespace
  write_keys, write_format_tuple, write_format_string = SplitFormat(
      write_config)
  read_keys, read_format_tuple, read_format_string = SplitFormat(
      read_config)

  # write_format: azimuth, altitude, R, G, & B intensity
  # read heartbeat: mode change & millis
  link = Serial(
      *SERVO_CONNECTION, read_timeout=60,
      error_pin=messageboard.GPIO_ERROR_ARDUINO_SERVO_CONNECTION,
      to_parent_q=to_parent_q,
      read_format=read_format_string, write_format=write_format_string,
      name='Servo')
  link.Open()

  last_flight = {}
  flight, json_desc_dict, configuration, additional_attr = InitialMessageValues(
      to_arduino_q)
  next_read = 0
  next_write = 0
  now = GetNow(json_desc_dict, additional_attr)
  gamma = GetGamma(configuration)
  brightness = 1.0




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




      new_flight = DifferentFlights(flight, last_flight)
      if new_flight:
        Log('Flight changed from %s to %s' % (
            messageboard.DisplayFlightNumber(last_flight),
            messageboard.DisplayFlightNumber(flight)
        ), ser=link)

        # Turn off laser so line isn't traced while it moves to new position
        message_dict = GenerateServoMessage(laser=LASER_OFF)
        message_tuple = DictToValueTuple(
            message_dict, write_keys, write_format_tuple)
        link.Write(message_tuple)

      last_flight = flight

    mode_switch_ack = False
    if time.time() >= next_read:
      bytes_read = []
      values_t = link.Read(bytes_read=bytes_read)
      values_d = dict(zip(read_keys, values_t))
      Log(str(values_t) + str(values_d))  #TODO
      next_read = time.time() + READ_DELAY_TIME_SERVO
      if bytes_read and VERBOSE:
        Log('Read by Servo: %s' % str(values_d))

      if values_d['mode_switch']:

        # makes a copy so as to not modify underlying config; we don't want
        # to modify underlying because otherwise the settings will bounce
        # around (values read from disk -> new values set by arduino -> old
        # values from disk -> new values from disk after Arduino update).
        configuration = dict(configuration)

        # LED only -> Laser only -> Both
        sequence = ('led_only', 'laser_only', 'both')
        old_name = configuration['servo_mode']
        old_id = sequence.index(old_name)
        new_id = (old_id + 1) % len(sequence)
        new_name = sequence[new_id]
        Log('Hemisphere mode updated from %s to %s' % (old_name, new_name))
        configuration['servo_mode'] = new_name
        settings_string = messageboard.BuildSettings(configuration)
        to_parent_q.put(('update_configuration', (settings_string, )))
        mode_switch_ack = True

    now = GetNow(json_desc_dict, additional_attr)




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