Ds1307
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Properties;
public class Ds1307 extends Thread {
private static final String FILE = "ds1307_properties.xml";
private final Properties properties = new Properties();
private byte[] storage = new byte[64];
/**
* 0 -> Seconds 00..59 / uppermost bit is start(0)/stop(1)
* 1 -> Minutes 00..59
* 2 -> Hours 00..23
* 3 -> Weekday 0(Monday)..6(Sunday)
* 4 -> Day 1..28|29|30|31
* 5 -> Month 1..12
* 6 -> Year 00..99
* 7 -> Settings
* 8..63 can be used for whatever you want!
*/
public Ds1307() {
try {
properties.loadFromXML(new FileInputStream(FILE));
for (int t = 8; t < 64; t++) {
String value = null;
try {
value = "" + t;
if (properties.getProperty(value) != null)
storage[t] = (byte) Integer.parseInt(properties.getProperty(value));
} catch (Exception e) {
System.out.println("Ds1307.getProperty(" + value + "): " + e.getMessage());
}
}
} catch (Exception e) {
System.out.println("Ds1307.loadFromXML(): " + e.getMessage());
}
// The RTC runs in Standard Time!! Daylight Saving Time has one more hour!
// final Calendar calendar = new GregorianCalendar(2010, 9, 31, 2, 59, 50); // Switch from (Daylight Savings Time)
// final Calendar calendar = new GregorianCalendar(2010,2,28,1,59,50); // Switch from (Standard Time)
final Calendar calendar = new GregorianCalendar();
// Remove daylight savings compensation because the DS1307 won't compensate, too!
calendar.add(Calendar.HOUR_OF_DAY, -calendar.get(Calendar.DST_OFFSET) / 3600000);
storage[0] = decToBcd(calendar.get(Calendar.SECOND));
storage[1] = decToBcd(calendar.get(Calendar.MINUTE));
storage[2] = decToBcd(calendar.get(Calendar.HOUR_OF_DAY));
storage[3] = decToBcd((calendar.get(Calendar.DAY_OF_WEEK) + 6) % 7);
storage[4] = decToBcd(calendar.get(Calendar.DAY_OF_MONTH));
storage[5] = decToBcd(calendar.get(Calendar.MONTH) + 1);
storage[6] = decToBcd(calendar.get(Calendar.YEAR) % 100);
start();
}
public void run() {
//noinspection InfiniteLoopStatement
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
int second = bcdToDec(storage[0]);
int minute = bcdToDec(storage[1]);
int hour = bcdToDec(storage[2]);
int weekday = bcdToDec(storage[3]);
int day = bcdToDec(storage[4]);
int month = bcdToDec(storage[5]);
int year = bcdToDec(storage[6]);
// Clock only runs if start/stop-bit is NOT set!
if ((second & 0x80) == 0)
second++;
if (second == 60) {
second = 0;
minute++;
}
if (minute == 60) {
minute = 0;
hour++;
}
if (hour == 24) {
hour = 0;
day++;
weekday = (weekday + 1) % 7;
}
if (day == 30 && month == 2 && year % 4 == 0 ||
day == 29 && month == 2 && year % 4 != 0 ||
day == 31 && (month == 4 || month == 6 || month == 9 || month == 11) ||
day == 32 && (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)) {
day = 1;
month++;
}
if (month == 13) {
month = 1;
year++;
}
if (year == 100) {
year = 0;
}
storage[0] = decToBcd(second);
storage[1] = decToBcd(minute);
storage[2] = decToBcd(hour);
storage[3] = decToBcd(weekday);
storage[4] = decToBcd(day);
storage[5] = decToBcd(month);
storage[6] = decToBcd(year);
}
}
public void send(final int address, final byte data) {
storage[address % 64] = data;
if (address % 64 > 7) {
properties.setProperty("" + (address % 64), "" + ((int) data & 0xff));
try {
properties.storeToXML(new FileOutputStream(FILE),
"Local properties file for Joghurts 'BikeComp' project. " +
"See 'http://arduino.dg4sfw.de/bikecomp/' for further information.");
} catch (Exception e) {
System.out.println("Ds1307.storeToXML():" + e.getMessage());
}
}
}
public byte receive(final int address) {
return storage[address % 64];
}
private int bcdToDec(final byte bcd) {
return bcd / 16 * 10 + (bcd % 16);
}
private byte decToBcd(final int dec) {
return (byte) ((dec / 10 << 4) + dec % 10);
}
}