Android Things driver for Adafruit's DC & Stepper Motor Hat for Raspberry Pi.
This is a port of the original Python library from Adafruit to Android Things. Please note this is still a work in progress, as only DC motors are supported (contributions to support stepper motors are most welcome though).
// Create a default object with no changes to I2C address or frequency
mh = new AdafruitMotorHat();
int motorIndex = 1; // Can be 1, 2, 3, 4
AdafruitDCMotor myMotor = mh.getMotor(motorIndex);
// Set the speed to start, from 0 (off) to 255 (max speed)
myMotor.setSpeed(150);
myMotor.run(AdafruitMotorHat.FORWARD);
myMotor.run(AdafruitMotorHat.FORWARD);
// Speed up.
for (int i = 0; i < 255; i++) {
myMotor.setSpeed(i);
Thread.sleep((long) (0.01 * 1000));
}
// Slow down.
for (int i = 254; i >= 0; i--) {
myMotor.setSpeed(i);
Thread.sleep((long) (0.01 * 1000));
}
myMotor.run(AdafruitMotorHat.BACKWARD);
// Speed up.
for (int i = 0; i < 255; i++) {
myMotor.setSpeed(i);
Thread.sleep((long) (0.01 * 1000));
}
// Slow down.
for (int i = 254; i >= 0; i--) {
myMotor.setSpeed(i);
Thread.sleep((long) (0.01 * 1000));
}
myMotor.run(AdafruitMotorHat.RELEASE);
Thread.sleep((long) (1.0 * 1000));
The Android Studio projects includes two modules:
The lib
module has the code you'll want to include on your own projects. It's a port of the original Python library using Android's com.google.android.things.pio.I2cDevice
under the hood.
The app
module shows a simple example of how to use the library (see the video linked above to see the MainActivity
running).
This library is not available on Maven (yet).
Please open one on adafruit-motor-hat/issues.
Feel free to reach out on Twitter.