forked from espruino/EspruinoDocs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDS3231.js
116 lines (99 loc) · 3.15 KB
/
DS3231.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* Copyright (c) 2014 Peter Clarke. See the file LICENSE for copying permission. */
/*
Module is to set date time and read date time from a DS3231 over I2C.
For the daylight saving to work (UK only) the time must be read at least every second so that when it occurs we write back to the module the shifted time.
*/
var dstStatus = true;
function DS3231(_i2c) { this.i2c = _i2c; }
//private
var C = {
i2c_address: 0x68,
dateReg : 0x04,
monthReg : 0x05,
yearReg : 0x06,
secsReg : 0x00,
minsReg : 0x01,
hourReg : 0x02,
dowReg : 0x03
};
//private functions
// Convert Decimal value to BCD
function dec2bcd(val) {
return parseInt(val, 16);
}
// Convert BCD value to decimal
function bcd2dec(val) {
return ((val >> 4)*10+(val & 0x0F));
}
// Formatting
function format(val) {
return ("0"+val).substr(-2);
}
//DST
function isDST(day,month,dow) {
if ((month === 3) && (dow === 7) && (day > 23)) {
return true;
}
if ((month === 10) && (dow === 7) && (day > 23)) {
return false;
}
if ((month > 3) && (month < 11)) {
return true;
}
else {
return false;
}
}
// Public Constants
// Public functions
// Set the day of the week
DS3231.prototype.setDow = function(day) {
var days = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"];
var idx = days.indexOf(day);
if (idx<0) {
print("Not a valid day");
}
else {
this.i2c.writeTo(C.i2c_address,[C.dowReg, (dec2bcd(1+idx))]);
}
};
// Set the date
DS3231.prototype.setDate = function(date,month,year) {
this.i2c.writeTo(C.i2c_address,[C.dateReg, (dec2bcd(date))]);
this.i2c.writeTo(C.i2c_address,[C.monthReg, (dec2bcd(month))]);
this.i2c.writeTo(C.i2c_address,[C.yearReg, (dec2bcd(year))]);
dstStatus = isDST(date,month,year);
};
// Set the time
DS3231.prototype.setTime = function(hour,minute) {
this.i2c.writeTo(C.i2c_address,[C.secsReg, 0]);
this.i2c.writeTo(C.i2c_address,[C.minsReg, (dec2bcd(minute))]);
this.i2c.writeTo(C.i2c_address,[C.hourReg, (dec2bcd(hour))]);
};
// Read the current date & time
DS3231.prototype.readDateTime = function () {
this.i2c.writeTo(C.i2c_address, C.secsReg/* address*/);
var data = this.i2c.readFrom(C.i2c_address, 7/* bytes */); //read number of bytes from address
var seconds = bcd2dec(data[0]);
var minutes = bcd2dec(data[1]);
var hours = bcd2dec(data[2]);
var dow = bcd2dec(data[3]);
var date = bcd2dec(data[4]);
var month = bcd2dec(data[5]);
var year = bcd2dec(data[6]);
if (hours === 1 && minutes === 0 && seconds === 0 && isDST(date,month,dow) === true && dstStatus === false) { // clocks go forward
this.i2c.writeTo(C.i2c_address,[C.hourReg, (dec2bcd(2))]);
hours = 2;
dstStatus = true;
}
if (hours === 2 && minutes === 0 && seconds === 0 && isDST(date,month,dow) === false && dstStatus === true) { // clocks go back
this.i2c.writeTo(C.i2c_address,[C.hourReg, (dec2bcd(1))]);
hours = 1;
dstStatus = false;
}
var rtcDate = format(date)+"/"+format(month)+"/"+format(year);
var rtcTime = format(hours)+":"+format(minutes)+":"+format(seconds);
var rtcDateTime = rtcDate+" "+rtcTime;
return rtcDateTime;
};
exports.connect = function(i2c) { return new DS3231(i2c); };