Skip to content

Commit 3cdd29b

Browse files
committed
check value range
1 parent 46c8bdf commit 3cdd29b

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

ds1307.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/**
2-
* makecode DS1307 RTC Package.
3-
* From microbit/micropython Chinese community.
4-
* http://www.micropython.org.cn
5-
*/
2+
* makecode DS1307 RTC Package.
3+
* From microbit/micropython Chinese community.
4+
* http://www.micropython.org.cn
5+
*/
66

77
/**
88
* DS1307 block
@@ -56,7 +56,7 @@ namespace DS1307 {
5656
* start ds1307 (go on)
5757
*/
5858
//% blockId="DS1307_START" block="start"
59-
//% weight=52 blockGap=8
59+
//% weight=52 blockGap=8
6060
//% parts=DS1307 trackArgs=0
6161
export function start() {
6262
let t = getSecond()
@@ -67,7 +67,7 @@ namespace DS1307 {
6767
* stop ds1307 (pause)
6868
*/
6969
//% blockId="DS1307_STOP" block="pause"
70-
//% weight=51 blockGap=8
70+
//% weight=51 blockGap=8
7171
//% parts=DS1307 trackArgs=0
7272
export function stop() {
7373
let t = getSecond()
@@ -78,7 +78,7 @@ namespace DS1307 {
7878
* get Year
7979
*/
8080
//% blockId="DS1307_GET_YEAR" block="year"
81-
//% weight=99 blockGap=8
81+
//% weight=99 blockGap=8
8282
//% parts=DS1307 trackArgs=0
8383
export function getYear(): number {
8484
return (HexToDec(getReg(DS1307_REG_YEAR)) + 2000)
@@ -89,7 +89,7 @@ namespace DS1307 {
8989
* @param dat is the Year will be set, eg: 2018
9090
*/
9191
//% blockId="DS1307_SET_YEAR" block="set year %dat"
92-
//% weight=69 blockGap=8
92+
//% weight=69 blockGap=8
9393
//% parts=DS1307 trackArgs=0
9494
export function setYear(dat: number): void {
9595
setReg(DS1307_REG_YEAR, DecToHex(dat % 100))
@@ -99,10 +99,10 @@ namespace DS1307 {
9999
* get Month
100100
*/
101101
//% blockId="DS1307_GET_MONTH" block="month"
102-
//% weight=98 blockGap=8
102+
//% weight=98 blockGap=8
103103
//% parts=DS1307 trackArgs=0
104104
export function getMonth(): number {
105-
return HexToDec(getReg(DS1307_REG_MONTH))
105+
return Math.max(Math.min(HexToDec(getReg(DS1307_REG_MONTH)), 12), 1)
106106
}
107107

108108
/**
@@ -121,10 +121,10 @@ namespace DS1307 {
121121
* get Day
122122
*/
123123
//% blockId="DS1307_GET_DAY" block="day"
124-
//% weight=97 blockGap=8
124+
//% weight=97 blockGap=8
125125
//% parts=DS1307 trackArgs=0
126126
export function getDay(): number {
127-
return HexToDec(getReg(DS1307_REG_DAY))
127+
return Math.max(Math.min(HexToDec(getReg(DS1307_REG_DAY)), 31), 1)
128128
}
129129

130130
/**
@@ -143,10 +143,10 @@ namespace DS1307 {
143143
* get Week Day
144144
*/
145145
//% blockId="DS1307_GET_WEEKDAY" block="weekday"
146-
//% weight=96 blockGap=8
146+
//% weight=96 blockGap=8
147147
//% parts=DS1307 trackArgs=0
148148
export function getWeekday(): number {
149-
return HexToDec(getReg(DS1307_REG_WEEKDAY))
149+
return Math.max(Math.min(HexToDec(getReg(DS1307_REG_WEEKDAY)), 7), 1)
150150
}
151151

152152
/**
@@ -165,10 +165,10 @@ namespace DS1307 {
165165
* get Hour
166166
*/
167167
//% blockId="DS1307_GET_HOUR" block="hour"
168-
//% weight=95 blockGap=8
168+
//% weight=95 blockGap=8
169169
//% parts=DS1307 trackArgs=0
170170
export function getHour(): number {
171-
return HexToDec(getReg(DS1307_REG_HOUR))
171+
return HexToDec(getReg(DS1307_REG_HOUR)) % 24
172172
}
173173

174174
/**
@@ -187,10 +187,10 @@ namespace DS1307 {
187187
* get Minute
188188
*/
189189
//% blockId="DS1307_GET_MINUTE" block="minute"
190-
//% weight=94 blockGap=8
190+
//% weight=94 blockGap=8
191191
//% parts=DS1307 trackArgs=0
192192
export function getMinute(): number {
193-
return HexToDec(getReg(DS1307_REG_MINUTE))
193+
return HexToDec(getReg(DS1307_REG_MINUTE)) % 60
194194
}
195195

196196
/**
@@ -209,10 +209,10 @@ namespace DS1307 {
209209
* get Second
210210
*/
211211
//% blockId="DS1307_GET_SECOND" block="second"
212-
//% weight=93 blockGap=8
212+
//% weight=93 blockGap=8
213213
//% parts=DS1307 trackArgs=0
214214
export function getSecond(): number {
215-
return HexToDec(getReg(DS1307_REG_SECOND))
215+
return HexToDec(getReg(DS1307_REG_SECOND)) % 60
216216
}
217217

218218
/**

0 commit comments

Comments
 (0)