-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.ts
70 lines (57 loc) · 1.86 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
namespace rtc {
const I2C_ADDR = 0x68
const REG_SECS = 0x00
const REG_MINS = 0x01
const REG_HOUR = 0x02
const REG_CTRL = 0x0E
function initialize() {
let buf = pins.createBuffer(2)
buf[0] = REG_CTRL
buf[1] = 0x4C
pins.i2cWriteBuffer(I2C_ADDR, buf)
}
initialize()
function leadingZero(value: number): string {
if (value < 10) {
return "0" + value
}
return "" + value
}
function getRegister(register: number): number {
let data = pins.createBuffer(1)
data[0] = register
pins.i2cWriteBuffer(I2C_ADDR, data)
return pins.i2cReadNumber(I2C_ADDR, NumberFormat.Int8LE)
}
function setRegister(register: number, value: number) {
let data = pins.createBuffer(2)
data[0] = register
data[1] = value
pins.i2cWriteBuffer(I2C_ADDR, data)
}
export function getTime(): number[] {
let hour = bcd.Decode(getRegister(REG_HOUR))
let mins = bcd.Decode(getRegister(REG_MINS))
let secs = bcd.Decode(getRegister(REG_SECS))
return [hour, mins, secs]
}
export function setTime(hour: number, mins: number, secs: number) {
setRegister(REG_HOUR, bcd.Encode(hour))
setRegister(REG_MINS, bcd.Encode(mins))
setRegister(REG_SECS, bcd.Encode(secs))
}
export function setTimeString(input: string) {
let time = helpers.stringSplit(input, ":")
let hour = parseInt(time[0]) % 24;
let mins = parseInt(time[1]) % 60;
let secs = parseInt(time[2]) % 60;
setTime(hour, mins, secs)
}
export function getTimeString(): string {
let time = getTime()
let hour = leadingZero(time[0])
let mins = leadingZero(time[1])
let secs = leadingZero(time[2])
return `${hour}:${mins}:${secs}`
}
}