Skip to content

Instantly share code, notes, and snippets.

@devunwired
Created December 22, 2016 22:27
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save devunwired/8656c1902ec856e5f4b26f45cd21f2fd to your computer and use it in GitHub Desktop.
Save devunwired/8656c1902ec856e5f4b26f45cd21f2fd to your computer and use it in GitHub Desktop.
Quick Android Things demo using ObjectAnimator to animate the brightness of a PWM output. This example uses a BounceInterpolator to create a flickering effect on an LED (like a candle).
/*
* Copyright 2016 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.util.FloatProperty;
import android.util.Log;
import android.view.animation.BounceInterpolator;
import com.google.android.things.pio.PeripheralManagerService;
import com.google.android.things.pio.Pwm;
import java.io.IOException;
public class FlickerActivity extends Activity {
private static final String TAG = "FlickerActivity";
// PWM output pin names (Edison)
private static final String YELLOW_PWM = "IO6";
private static final String ORANGE_PWM = "IO5";
private static final int DURATION_MS = 350;
private static final int DELAY_MS = 150;
private PeripheralManagerService mService = new PeripheralManagerService();
private Pwm mYellowLed, mOrangeLed;
private ObjectAnimator mYellowFlame, mOrangeFlame;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
mYellowLed = openLed(YELLOW_PWM);
mOrangeLed = openLed(ORANGE_PWM);
} catch (IOException e) {
Log.w(TAG, "Unable to open LED connections", e);
}
// Create an run the flicker animations
mYellowFlame = animateFlicker(mYellowLed, 0);
mOrangeFlame = animateFlicker(mOrangeLed, DELAY_MS);
mYellowFlame.start();
mOrangeFlame.start();
}
@Override
public void onDestroy() {
super.onDestroy();
mYellowFlame.cancel();
mOrangeFlame.cancel();
try {
closeLed(mYellowLed);
closeLed(mOrangeLed);
} catch (IOException e) {
Log.w(TAG, "Unable to close LED connections", e);
}
}
// Create a new LED connection
private Pwm openLed(String name) throws IOException {
Pwm led = mService.openPwm(name);
led.setPwmFrequencyHz(240.0f);
led.setPwmDutyCycle(25.0);
led.setEnabled(true);
return led;
}
// Close an LED connection
private void closeLed(Pwm pwm) throws IOException {
if (pwm != null) {
pwm.setEnabled(false);
pwm.close();
}
}
// Create an animation that bounces, using a flicker effect
private ObjectAnimator animateFlicker(Pwm led, long delay) {
ObjectAnimator animator = ObjectAnimator
.ofFloat(led, new BrightnessProperty(), 100, 25)
.setDuration(DURATION_MS + delay);
animator.setInterpolator(new BounceInterpolator());
animator.setRepeatMode(ValueAnimator.REVERSE);
animator.setRepeatCount(ValueAnimator.INFINITE);
return animator;
}
/**
* Property used to animate the "brightness" of an
* LED as the duty cycle of a PWM.
*/
private class BrightnessProperty extends FloatProperty<Pwm> {
private static final String TAG = "BrightnessProperty";
// Cache the last set value since PWM can't report its state
private float mValue;
BrightnessProperty() {
super("PWM Brightness");
}
@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);
}
}
@Override
public Float get(Pwm pwm) {
// We can't ask PWM for its current duty value
return mValue;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment