forked from mworkfun/pxt-DS3231
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
347 lines (313 loc) · 9.99 KB
/
main.ts
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/**
* DS3231 RTC Package.
* Based on DS1307 package from https://github.com/makecode-extensions/DS1307
*/
// enums to choose alarm number etc - these must be outside the namespace!
enum alarmNum {
A1,
A2
}
enum mode {
Minute,
HourMinute,
DateHourMinute,
DayHourMinute
}
enum interruptEnable {
Enable,
Disable
}
/**
* DS3231 block
*/
//% weight=20 color=#b77ff0 icon="\uf017" block="DS3231"
namespace DS3231 {
let DS3231_I2C_ADDR = 0x68
let DS3231_REG_SECOND = 0x00
let DS3231_REG_MINUTE = 0x01
let DS3231_REG_HOUR = 0x02
let DS3231_REG_DAY = 0x03
let DS3231_REG_DATE = 0x04
let DS3231_REG_MONTH = 0x05
let DS3231_REG_YEAR = 0x06
let DS3231_REG_A1BASE = 0x08
let DS3231_REG_A2BASE = 0x0b
let DS3231_REG_CTRL = 0x0e
let DS3231_REG_STATUS = 0x0f
let DS3231_REG_TEMPU = 0x11
let DS3231_REG_TEMPL = 0x12
/**
* set a DS3231 reg
*/
function setReg(reg: number, dat: number) {
let buf = pins.createBuffer(2);
buf[0] = reg;
buf[1] = dat;
pins.i2cWriteBuffer(DS3231_I2C_ADDR, buf);
}
/**
* get a DS3231 reg value
*/
function regValue(reg: number){
pins.i2cWriteNumber(DS3231_I2C_ADDR, reg, NumberFormat.UInt8BE);
return pins.i2cReadNumber(DS3231_I2C_ADDR, NumberFormat.UInt8BE);
}
/**
* convert Bcd to Dec
*/
function bcdToDec(bcd: number){
return (bcd >> 4) * 10 + (bcd % 16);
}
/**
* convert Dec to Bcd
*/
function decToBcd(dec: number){
return Math.idiv(dec, 10) * 16 + (dec % 10)
}
/**
* get Year
*/
//% blockId="DS3231_GET_YEAR" block="year"
//% weight=99 blockGap=8
//% parts=DS3231 trackArgs=0
export function year(){
return Math.min(bcdToDec(regValue(DS3231_REG_YEAR)), 99) + 2000
}
/**
* get Month
*/
//% blockId="DS3231_GET_MONTH" block="month"
//% weight=98 blockGap=8
//% parts=DS3231 trackArgs=0
export function month() {
return Math.max(Math.min(bcdToDec(regValue(DS3231_REG_MONTH)), 12), 1)
}
/**
* get Date
*/
//% blockId="DS3231_GET_DATE" block="date"
//% weight=97 blockGap=8
//% parts=DS3231 trackArgs=0
export function date() {
return Math.max(Math.min(bcdToDec(regValue(DS3231_REG_DATE)), 31), 1)
}
/**
* get (Week) Day
*/
//% blockId="DS3231_GET_DAY" block="day"
//% weight=96 blockGap=8
//% parts=DS3231 trackArgs=0
export function day(){
return Math.max(Math.min(bcdToDec(regValue(DS3231_REG_DAY)), 7), 1)
}
/**
* get Hour
*/
//% blockId="DS3231_GET_HOUR" block="hour"
//% weight=95 blockGap=8
//% parts=DS3231 trackArgs=0
export function hour() {
return Math.min(bcdToDec(regValue(DS3231_REG_HOUR)), 23)
}
/**
* get Minute
*/
//% blockId="DS3231_GET_MINUTE" block="minute"
//% weight=94 blockGap=8
//% parts=DS3231 trackArgs=0
export function minute() {
return Math.min(bcdToDec(regValue(DS3231_REG_MINUTE)), 59)
}
/**
* get Second
*/
//% blockId="DS3231_GET_SECOND" block="second"
//% weight=93 blockGap=8
//% parts=DS3231 trackArgs=0
export function second() {
return Math.min(bcdToDec(regValue(DS3231_REG_SECOND)), 59)
}
/**
* get status register
*/
//% blockId="DS3231_GET_STATUS" block="status"
//% weight=90 blockGap=8
//% parts=DS3231 trackArgs=0
export function status() {
return (regValue(DS3231_REG_STATUS))
}
/**
* get control register
*/
//% blockId="DS3231_GET_CONTROL" block="control"
//% weight=88 blockGap=8
//% parts=DS3231 trackArgs=0
export function control(){
return (regValue(DS3231_REG_CTRL))
}
/**
* get temperature upper register
*/
//% blockId="DS3231_GET_TEMPU" block="temperature (upper)"
//% weight=86 blockGap=8
//% parts=DS3231 trackArgs=0
export function temperatureUpper() {
return (regValue(DS3231_REG_TEMPU))
}
/**
* get temperature lower register
*/
//% blockId="DS3231_GET_TEMPL" block="temperature (lower)"
//% weight=84 blockGap=8
//% parts=DS3231 trackArgs=0
export function temperatureLower(){
return (regValue(DS3231_REG_TEMPL))
}
/**
* set Date and Time
* @param year is the Year to be set, eg: 2020
* @param month is the Month to be set, eg: 2
* @param date is the Date to be set, eg: 15
* @param day is the day (of the week) to be set, eg: 4
* @param hour is the Hour to be set, eg: 0
* @param minute is the Minute to be set, eg: 0
* @param second is the Second to be set, eg: 0
*/
//% blockId="DS3231_SET_DATETIME" block="set year %year|month %month|date %date|day %day|hour %hour|minute %minute|second %second"
//% year.min=2000 year.max=2099
//% month.min=1 month.max=12
//% date.min=1 date.max=31
//% day.min=1 day.max=7
//% hour.min=0 hour.max=23
//% minute.min=0 minute.max=59
//% second.min=0 second.max=59
//% weight=60 blockGap
//% parts=DS3231 trackArgs=0
export function dateTime(year: number, month: number, date: number, day: number, hour: number, minute: number, second: number){
let buf = pins.createBuffer(8);
buf[0] = DS3231_REG_SECOND;
buf[1] = decToBcd(second);
buf[2] = decToBcd(minute);
buf[3] = decToBcd(hour);
buf[4] = decToBcd(day);
buf[5] = decToBcd(date);
buf[6] = decToBcd(month);
buf[7] = (year -2000)/10*16 + (year -2000)%10;
pins.i2cWriteBuffer(DS3231_I2C_ADDR, buf);
}
/**
* set Alarm mode and time registers for alarm An (n = 1 or 2)
* @param modeAn is the matching mode for An eg:hour minute
* @param interruptAn is the interrup enable for An eg: on
* @param name is the Alarm name (A1 or A2)
* @param date is the Date to be set, eg: 15
* @param day is the day (of the week) to be set, eg: 4
* @param hour is the Hour to be set, eg: 13
* @param minute is the Minute to be set, eg: 0
*/
//% blockId="DS3231_ALARM" block="set alarm %name| mode %modeAn| date %date|day %day|hour %hour|minute %minute"
//% date.min=1 date.max=31
//% day.min=1 day.max=7
//% hour.min=0 hour.max=23
//% minute.min=0 minute.max=59
//% weight=58 blockGap
//% parts=DS3231 trackArgs=0
export function setAlarm(name: alarmNum, modeAn: mode, date: number, day: number, hour: number, minute: number) {
let buf = pins.createBuffer(4)
buf[1] = decToBcd(minute) //raw minutes, before AnM2 is set
buf[2] = decToBcd(hour) //raw hours before AnM3 is set
buf[3] = decToBcd(date)
switch(name) {
case alarmNum.A1: buf[0] = DS3231_REG_A1BASE
break
case alarmNum.A2: buf[0] = DS3231_REG_A2BASE
}
switch(modeAn){
case mode.Minute: {
buf[3] = buf[3] | 0x80 //set AnM4
buf[2] = buf[2] | 0x80 //set AnM3
break
}
case mode.HourMinute: {
buf[3] = buf[3] | 0x80 //set AnM4
break
}
case mode.DateHourMinute: {
buf[3] = decToBcd(date)
break
}
case mode.DayHourMinute: {
buf[3] = decToBcd(day)
buf[3] = buf[3] | 0x40 //set DY bit
}
}
pins.i2cWriteBuffer(DS3231_I2C_ADDR, buf)
}
/**
* alarm interrupt enable An (1 or 2)
* @param name is the Alarm name (A1 or A2)
* @param mode is Enable or Disable
*/
//% blockId="DS3231_ALARM_INTERRUPT_ENABLE" block="alarm interrupt enable %name |%mode"
//% weight=56 blockGap=8
//% parts=DS3231 trackArgs=0
export function disableAlarm(name: alarmNum, mode: interruptEnable){
let control = regValue(DS3231_REG_CTRL)
switch(name) {
case alarmNum.A1:
switch(mode){
case interruptEnable.Enable: setReg(DS3231_REG_CTRL, control | 0x01)
break
case interruptEnable.Disable: setReg(DS3231_REG_CTRL, control & 0xfe)
}
break
case alarmNum.A2: setReg(DS3231_REG_CTRL, control & 0xfd)
switch(mode) {
case interruptEnable.Enable: setReg(DS3231_REG_CTRL, control | 0x02)
break
case interruptEnable.Disable: setReg(DS3231_REG_CTRL, control & 0xfd)
}
}
}
/**
* clear alarm flag An (n = 1 or 2)
* @param name is the Alarm name (A1 or A2)
*/
//% blockId="DS3231_CLEAR_ALARM_FLAG" block="clear alarm flag %name"
//% weight=52 blockGap=8
//% parts=DS3231 trackArgs=0
export function clearAlarmFlag(name: alarmNum){
let reg = regValue(DS3231_REG_STATUS)
switch(name) {
case alarmNum.A1: setReg(DS3231_REG_STATUS, reg & 0xfe)
break
case alarmNum.A2: setReg(DS3231_REG_STATUS, reg & 0xfd)
}
}
/**
* configure INTCN
* @param name is the Alarm name (A1 or A2)
*/
//% blockId="DS3231_CONFIG_INTCN" block="configure INTCN %mode"
//% weight=50 blockGap=8
//% parts=DS3231 trackArgs=0
export function configureINTCN(mode: interruptEnable){
let control = regValue(DS3231_REG_CTRL)
switch(mode){
case interruptEnable.Enable: control = control | 0x04 //set b2
break
case interruptEnable.Disable: control = control & 0xfb //reset b2
}
setReg(DS3231_REG_CTRL, control)
}
/**
* read any register - for DEBUG only
*/
//% blockId="DS3231_GET_ANYREG" block="read register %reg"
//% weight=48 blockGap=8
//% parts=DS3231 trackArgs=0
export function readReg(reg: number) {
return (regValue(reg))
}
}
//