com.google.android.things.pio.Pwm Java Examples

The following examples show how to use com.google.android.things.pio.Pwm. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BrightnessProperty.java    From edison-candle with Apache License 2.0 5 votes vote down vote up
@Override
public void setValue(Pwm pwm, float value) {
    mValue = Math.max(0f, Math.min(value, 100f));
    try {
        pwm.setPwmDutyCycle(mValue);
    } catch (IOException e) {
        Log.w(TAG, "Unable to set PWM duty cycle", e);
    }
}
 
Example #2
Source File: HomeActivity.java    From edison-candle with Apache License 2.0 5 votes vote down vote up
/**
 * Open a new LED connection to the provided port name
 * @throws IOException
 */
private Pwm openLed(String name) throws IOException {
    Pwm led = PeripheralManager.getInstance().openPwm(name);
    led.setPwmFrequencyHz(60.0f);
    led.setPwmDutyCycle(BRIGHTNESS_START);
    led.setEnabled(true);

    return led;
}
 
Example #3
Source File: HomeActivity.java    From edison-candle with Apache License 2.0 5 votes vote down vote up
/**
 * Close the provided PWM connection
 * @throws IOException
 */
private void closeLed(Pwm pwm) throws IOException {
    if (pwm != null) {
        pwm.setEnabled(false);
        pwm.close();
    }
}
 
Example #4
Source File: HomeActivity.java    From edison-candle with Apache License 2.0 5 votes vote down vote up
/**
 * Create an infinite animation to modify the LED brightness.
 */
private ObjectAnimator animateFlicker(Pwm led, long delay) {
    ObjectAnimator animator = ObjectAnimator
            .ofFloat(led, new BrightnessProperty(), BRIGHTNESS_START, BRIGHTNESS_END)
            .setDuration(DURATION_MS + delay);

    // "Bounce" at each end to create a flicker effect
    animator.setInterpolator(new BounceInterpolator());
    // Cycle through the animation forever
    animator.setRepeatMode(ValueAnimator.REVERSE);
    animator.setRepeatCount(ValueAnimator.INFINITE);

    return animator;
}
 
Example #5
Source File: Speaker.java    From contrib-drivers with Apache License 2.0 5 votes vote down vote up
/**
 * Create a Speaker connected to the given PWM pin name
 */
public Speaker(String pin) throws IOException {
    PeripheralManager pioService = PeripheralManager.getInstance();
    Pwm device = pioService.openPwm(pin);
    try {
        connect(device);
    } catch (IOException|RuntimeException e) {
        try {
            close();
        } catch (IOException|RuntimeException ignored) {
        }
        throw e;
    }
}
 
Example #6
Source File: Servo.java    From contrib-drivers with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new Servo that connects to the named pin and uses the specified frequency
 *
 * @param pin the PWM pin name
 * @param frequencyHz the frequency in Hertz
 * @throws IOException
 */
public Servo(String pin, double frequencyHz) throws IOException {
    PeripheralManager pioService = PeripheralManager.getInstance();
    Pwm device = pioService.openPwm(pin);
    try {
        connect(device, frequencyHz);
    } catch (IOException | RuntimeException e) {
        try {
            close();
        } catch (IOException | RuntimeException ignored) {
        }
        throw e;
    }
}
 
Example #7
Source File: Servo.java    From contrib-drivers with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new Servo that connects to the given PWM device and uses the specified frequency
 *
 * @param device the PWM device
 * @param frequencyHz the frequency in Hertz
 * @throws IOException
 */
@VisibleForTesting
/*package*/ Servo(Pwm device, double frequencyHz) throws IOException {
    try {
        connect(device, frequencyHz);
    } catch (IOException | RuntimeException e) {
        try {
            close();
        } catch (IOException | RuntimeException ignored) {
        }
        throw e;
    }
}
 
Example #8
Source File: BrightnessProperty.java    From edison-candle with Apache License 2.0 4 votes vote down vote up
@Override
public Float get(Pwm pwm) {
    return mValue;
}
 
Example #9
Source File: Speaker.java    From contrib-drivers with Apache License 2.0 4 votes vote down vote up
/**
 * Create a Speaker from a {@link Pwm} device
 */
@VisibleForTesting
/*package*/ Speaker(Pwm device) throws IOException {
    connect(device);
}
 
Example #10
Source File: Speaker.java    From contrib-drivers with Apache License 2.0 4 votes vote down vote up
private void connect(Pwm device) throws IOException {
    mPwm = device;
    mPwm.setPwmDutyCycle(50.0); // square wave
}
 
Example #11
Source File: Servo.java    From contrib-drivers with Apache License 2.0 4 votes vote down vote up
private void connect(Pwm device, double frequencyHz) throws IOException {
    mPwm = device;
    mPwm.setPwmFrequencyHz(frequencyHz);
    mPeriod = 1000.0 / frequencyHz;
    updateDutyCycle();
}
 
Example #12
Source File: Servo.java    From contrib-drivers with Apache License 2.0 2 votes vote down vote up
/**
 * Create a new Servo that connects to the given PWM device
 *
 * @param device the PWM device
 * @throws IOException
 */
@VisibleForTesting
/*package*/ Servo(Pwm device) throws IOException {
    this(device, DEFAULT_FREQUENCY_HZ);
}