This driver supports real-time clock (RTC) peripherals built on the DS3231 chip.
NOTE: these drivers are not production-ready. They are offered as sample implementations of Android Things user space drivers for common peripherals as part of the Developer Preview release. There is no guarantee of correctness, completeness or robustness.
This driver is based on the DS3232RTC driver from JChristensen, the Arduino-DS3231 driver from jarzebski, the ds3231 driver from rodan, and the jpuderer-things-drivers driver from jpuderer
- Real-Time Clock Counts Seconds, Minutes, Hours, Date of the Month, Month, Day of the Week, and Year, with Leap-Year Compensation Valid Up to 2100
- Digital Temp Sensor
- Register for Aging Trim
- Two Time-of-Day Alarms
- Programmable Square-Wave Output Signal
If you are just interested in persisting the system wall clock time when you reboot or
power off your device, simply add the line below to your project's build.gradle
,
where <version>
matches the last version of the driver available on jcenter.
No additional code changes are required.
dependencies {
implementation 'com.leinardi.android.things:driver-ds3231-receiver:<version>'
}
If you are not interested in the automatic backup and restore feature, you can add just
the ds3231
driver:
dependencies {
implementation 'com.leinardi.android.things:driver-ds3231:<version>'
}
public class ClockActivity extends Activity {
private static final String TAG = ClockActivity.class.getSimpleName();
private Ds3231 mDevice;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Date date;
try {
mDevice = new Ds3231(BoardDefaults.getI2CPort());
Log.d(TAG, "isTimekeepingDataValid = " + mDevice.isTimekeepingDataValid());
Log.d(TAG, "isOscillatorEnabled = " + mDevice.isOscillatorEnabled());
Calendar calendar = Calendar.getInstance();
calendar.set(1982, Calendar.DECEMBER, 22);
date = calendar.getTime();
Log.d(TAG, "DateTime = " + date.toString());
mDevice.setTime(date);
Log.d(TAG, "getTime = " + mDevice.getTime().toString());
mDevice.setTime(date.getTime());
Log.d(TAG, "getTime = " + mDevice.getTime().toString());
date = new Date(System.currentTimeMillis());
Log.d(TAG, "DateTime = " + date.toString());
mDevice.setTime(date);
Log.d(TAG, "getTime = " + mDevice.getTime().toString());
mDevice.setTime(date.getTime());
Log.d(TAG, "getTime = " + mDevice.getTime().toString());
// Close the device.
mDevice.close();
} catch (IOException e) {
Log.e(TAG, "Error while opening screen", e);
throw new RuntimeException(e);
} finally {
mDevice = null;
}
}
}
Copyright 2018 Roberto Leinardi
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you 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.