Esempio n. 1
0
def _write_config_file(run_as_user):
    caps = list_caps()

    if 'Credentials' not in caps:
        log.warning('Cannot obtain Jupyter password; will bail.')
        sys.exit(1)

    creds = acquire('Credentials')
    jupyter_password = None

    while True:
        jupyter_password = creds.get_jupyter_password()
        if jupyter_password is not None:
            break
        log.info('Waiting for Jupyter password to be set...')
        time.sleep(1)

    release(creds)

    log.info('Got Jupyter password.')

    user_home_dir_path = pwd.getpwnam(run_as_user).pw_dir

    with open(JUPYTER_CONFIG_TEMPLATE, 'r') as f_template:
        template = f_template.read()
        with open(JUPYTER_CONFIG_OUTPUT, 'w') as f_out:
            final_config = template
            final_config = final_config.replace(r'<JUPYTER_PASSWORD>',
                                                repr(jupyter_password))
            final_config = final_config.replace(r'<JUPYTER_START_DIR>',
                                                repr(user_home_dir_path))
            f_out.write(final_config)
Esempio n. 2
0
def _get_gyro_accum():
    global _GYRO_ACCUM
    try:
        _GYRO_ACCUM
    except NameError:
        caps = list_caps()
        if 'Gyroscope_accum' not in caps:
            raise AttributeError('This device has no gyroscope.')
        _GYRO_ACCUM = acquire('Gyroscope_accum')
    return _GYRO_ACCUM
Esempio n. 3
0
def _get_pid_steering():
    global _PID_STEERING
    try:
        _PID_STEERING
    except NameError:
        caps = list_caps()
        if 'PID_steering' not in caps:
            raise AttributeError('This device has no PID steering loop.')
        _PID_STEERING = acquire('PID_steering')
    return _PID_STEERING
Esempio n. 4
0
def _get_encoder():
    global _ENCODER
    try:
        _ENCODER
    except NameError:
        caps = list_caps()
        if 'Encoders' not in caps:
            raise AttributeError('This device has no encoder.')
        _ENCODER = acquire('Encoders')
        _ENCODER.enable(ENCODER_INDEX)
    return _ENCODER
Esempio n. 5
0
def _get_motors():
    global _MOTORS
    try:
        _MOTORS
    except NameError:
        caps = list_caps()
        if 'CarMotors' not in caps:
            raise AttributeError('This device is not a car.')
        _MOTORS = acquire('CarMotors')
        _MOTORS.on()
    return _MOTORS
Esempio n. 6
0
def buzz(notes):
    """
    Play the given `notes` on the device's buzzer.
    """
    global _BUZZER
    try:
        _BUZZER
    except NameError:
        caps = list_caps()
        if 'Buzzer' not in caps:
            raise AttributeError("This device does not have a buzzer.")
        _BUZZER = acquire('Buzzer')
    _BUZZER.play(notes)
    _BUZZER.wait()
Esempio n. 7
0
def global_camera(verbose=False):
    """
    Creates (for the first call) or retrieves (for later calls) the
    global camera object. This is a convenience function to facilitate
    quickly and easily creating and retrieving a camera singleton.
    """
    global GLOBAL_CAMERA
    try:
        GLOBAL_CAMERA
    except NameError:
        caps = list_caps()
        if 'Camera' not in caps:
            raise AttributeError("This device does not have a Camera.")
        camera = acquire('Camera')
        GLOBAL_CAMERA = _CameraRGB(camera)
        if verbose:
            auto._ctx_print_all("Instantiated a global camera object!")
    return GLOBAL_CAMERA
Esempio n. 8
0
def get_servo(servo_index, frequency=50, min_duty=0.025, max_duty=0.125):
    """
    Acquire the interface to the servo at index `servo_index`.
    The first servo is at index 0.
    """
    global _PWMs
    try:
        _PWMs
    except NameError:
        caps = list_caps()
        if 'PWMs' not in caps:
            raise AttributeError("This device does not have a PWM controller.")
        _PWMs = acquire('PWMs')

    n_pins = _PWMs.num_pins()

    if servo_index < 0 or servo_index >= n_pins:
        raise Exception(
            "Invalid servo index given: {}. This device has servos 0 through {}"
            .format(servo_index, n_pins - 1))

    return _ServoTemplate(_PWMs, servo_index, frequency, min_duty, max_duty)