periph 1.1.0 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +61 -12
- package/examples/accelerometer/adxl345/complete.js +52 -0
- package/examples/accelerometer/adxl345/demo.js +34 -0
- package/examples/accelerometer/adxl345/minimal.js +18 -0
- package/examples/accelerometer/adxl362/complete.js +57 -0
- package/examples/accelerometer/adxl362/demo.js +51 -0
- package/examples/accelerometer/adxl362/minimal.js +21 -0
- package/examples/adc_dac/ad7705/complete.js +32 -0
- package/examples/adc_dac/ad7705/demo.js +42 -0
- package/examples/adc_dac/ad7705/minimal.js +16 -0
- package/examples/adc_dac/ad7706/complete.js +35 -0
- package/examples/adc_dac/ad7706/demo.js +44 -0
- package/examples/adc_dac/ad7706/minimal.js +16 -0
- package/examples/adc_dac/hx710a/complete.js +45 -0
- package/examples/adc_dac/hx710a/demo.js +52 -0
- package/examples/adc_dac/hx710a/minimal.js +16 -0
- package/examples/adc_dac/hx710b/complete.js +45 -0
- package/examples/adc_dac/hx710b/demo.js +64 -0
- package/examples/adc_dac/hx710b/minimal.js +16 -0
- package/examples/adc_dac/hx711/complete.js +7 -7
- package/examples/adc_dac/hx711/demo.js +6 -6
- package/examples/adc_dac/hx711/minimal.js +7 -7
- package/examples/adc_dac/mcp4725/complete.js +19 -17
- package/examples/adc_dac/mcp4725/demo.js +5 -5
- package/examples/adc_dac/mcp4725/minimal.js +8 -6
- package/examples/adc_dac/mcp4728/complete.js +24 -22
- package/examples/adc_dac/mcp4728/demo.js +22 -22
- package/examples/adc_dac/mcp4728/minimal.js +8 -6
- package/examples/adc_dac/pcf8591/complete.js +41 -0
- package/examples/adc_dac/pcf8591/demo.js +39 -0
- package/examples/adc_dac/pcf8591/minimal.js +19 -0
- package/examples/comms/mcp2515/complete.js +75 -0
- package/examples/comms/mcp2515/demo.js +66 -0
- package/examples/comms/mcp2515/minimal.js +27 -0
- package/examples/comms/rda5807m/complete.js +55 -0
- package/examples/comms/rda5807m/demo.js +68 -0
- package/examples/comms/rda5807m/minimal.js +15 -0
- package/examples/comms/rfm9x/complete.js +50 -0
- package/examples/comms/rfm9x/demo.js +55 -0
- package/examples/comms/rfm9x/minimal.js +20 -0
- package/examples/display/pcf8576/complete.js +18 -15
- package/examples/display/pcf8576/demo.js +6 -6
- package/examples/display/pcf8576/minimal.js +12 -10
- package/examples/environmental/aht21/complete.js +15 -13
- package/examples/environmental/aht21/demo.js +8 -6
- package/examples/environmental/aht21/minimal.js +5 -5
- package/examples/environmental/bme280/complete.js +23 -20
- package/examples/environmental/bme280/demo.js +14 -14
- package/examples/environmental/bme280/minimal.js +13 -11
- package/examples/environmental/bme680/complete.js +25 -22
- package/examples/environmental/bme680/demo.js +38 -36
- package/examples/environmental/bme680/minimal.js +14 -12
- package/examples/gas/ens160/complete.js +29 -27
- package/examples/gas/ens160/demo.js +39 -37
- package/examples/gas/ens160/minimal.js +17 -15
- package/examples/gnss/neo6/complete.js +49 -0
- package/examples/gnss/neo6/demo.js +54 -0
- package/examples/gnss/neo6/minimal.js +30 -0
- package/examples/gyroscope/l3g4200d/complete.js +56 -0
- package/examples/gyroscope/l3g4200d/demo.js +49 -0
- package/examples/gyroscope/l3g4200d/minimal.js +19 -0
- package/examples/gyroscope/l3gd20h/complete.js +42 -0
- package/examples/gyroscope/l3gd20h/demo.js +29 -0
- package/examples/gyroscope/l3gd20h/minimal.js +13 -0
- package/examples/humidity/dht11/complete.js +22 -0
- package/examples/humidity/dht11/demo.js +38 -0
- package/examples/humidity/dht11/minimal.js +18 -0
- package/examples/imu/mpu6050/complete.js +27 -25
- package/examples/imu/mpu6050/demo.js +29 -26
- package/examples/imu/mpu6050/minimal.js +6 -6
- package/examples/imu/mpu9250/complete.js +65 -0
- package/examples/imu/mpu9250/demo.js +53 -0
- package/examples/imu/mpu9250/minimal.js +21 -0
- package/examples/imu/mpu9255/complete.js +73 -0
- package/examples/imu/mpu9255/demo.js +73 -0
- package/examples/imu/mpu9255/minimal.js +21 -0
- package/examples/io_expander/mcp23017/complete.js +70 -77
- package/examples/io_expander/mcp23017/demo.js +37 -33
- package/examples/io_expander/mcp23017/minimal.js +15 -11
- package/examples/io_expander/pcf8574/complete.js +60 -65
- package/examples/io_expander/pcf8574/demo.js +30 -25
- package/examples/io_expander/pcf8574/minimal.js +15 -11
- package/examples/io_expander/pcf8575/complete.js +24 -20
- package/examples/io_expander/pcf8575/demo.js +27 -23
- package/examples/io_expander/pcf8575/minimal.js +16 -12
- package/examples/io_expander/tpic6b595/complete.js +57 -0
- package/examples/io_expander/tpic6b595/demo.js +75 -0
- package/examples/io_expander/tpic6b595/minimal.js +26 -0
- package/examples/led/apa102/complete.js +72 -0
- package/examples/led/apa102/demo.js +92 -0
- package/examples/led/apa102/minimal.js +21 -0
- package/examples/led/sk6812rgbw/complete.js +42 -40
- package/examples/led/sk6812rgbw/demo.js +16 -16
- package/examples/led/sk6812rgbw/minimal.js +15 -17
- package/examples/led/ws2812b/complete.js +40 -38
- package/examples/led/ws2812b/demo.js +16 -16
- package/examples/led/ws2812b/minimal.js +13 -14
- package/examples/led/ws2814/complete.js +71 -0
- package/examples/led/ws2814/demo.js +82 -0
- package/examples/led/ws2814/minimal.js +23 -0
- package/examples/light/apds-9930/complete.js +46 -0
- package/examples/light/apds-9930/demo.js +31 -0
- package/examples/light/apds-9930/minimal.js +20 -0
- package/examples/light/apds9960/complete.js +45 -43
- package/examples/light/apds9960/demo.js +11 -9
- package/examples/light/apds9960/minimal.js +5 -5
- package/examples/magnetometer/as5600/complete.js +31 -29
- package/examples/magnetometer/as5600/demo.js +35 -33
- package/examples/magnetometer/as5600/minimal.js +7 -7
- package/examples/magnetometer/hmc5883l/complete.js +47 -0
- package/examples/magnetometer/hmc5883l/demo.js +50 -0
- package/examples/magnetometer/hmc5883l/minimal.js +15 -0
- package/examples/memory/24aa02uid/complete.js +23 -21
- package/examples/memory/24aa02uid/demo.js +32 -30
- package/examples/memory/24aa02uid/minimal.js +5 -5
- package/examples/motor/drv8830/complete.js +50 -0
- package/examples/motor/drv8830/demo.js +57 -0
- package/examples/motor/drv8830/minimal.js +23 -0
- package/examples/other/mpr121/complete.js +50 -0
- package/examples/other/mpr121/demo.js +30 -0
- package/examples/other/mpr121/minimal.js +17 -0
- package/examples/power/ade7953/complete.js +44 -0
- package/examples/power/ade7953/demo.js +28 -0
- package/examples/power/ade7953/minimal.js +15 -0
- package/examples/power/ina219/complete.js +20 -18
- package/examples/power/ina219/demo.js +15 -13
- package/examples/power/ina219/minimal.js +8 -8
- package/examples/power/ina226/complete.js +24 -22
- package/examples/power/ina226/demo.js +22 -20
- package/examples/power/ina226/minimal.js +5 -5
- package/examples/power/ina3221/complete.js +51 -49
- package/examples/power/ina3221/demo.js +13 -13
- package/examples/power/ina3221/minimal.js +8 -8
- package/examples/pressure/bmp085/complete.js +29 -0
- package/examples/pressure/bmp085/demo.js +34 -0
- package/examples/pressure/bmp085/minimal.js +19 -0
- package/examples/pressure/bmp180/complete.js +20 -17
- package/examples/pressure/bmp180/demo.js +24 -22
- package/examples/pressure/bmp180/minimal.js +13 -11
- package/examples/pressure/bmp280/complete.js +23 -20
- package/examples/pressure/bmp280/demo.js +44 -41
- package/examples/pressure/bmp280/minimal.js +13 -11
- package/examples/pressure/bmp384/complete.js +29 -0
- package/examples/pressure/bmp384/demo.js +40 -0
- package/examples/pressure/bmp384/minimal.js +18 -0
- package/examples/pressure/bmp581/complete.js +34 -0
- package/examples/pressure/bmp581/demo.js +56 -0
- package/examples/pressure/bmp581/minimal.js +19 -0
- package/examples/pressure/lps22df/complete.js +50 -0
- package/examples/pressure/lps22df/demo.js +41 -0
- package/examples/pressure/lps22df/minimal.js +19 -0
- package/examples/pressure/lps28dfw/complete.js +37 -0
- package/examples/pressure/lps28dfw/demo.js +33 -0
- package/examples/pressure/lps28dfw/minimal.js +19 -0
- package/examples/pressure/lps33hw/complete.js +43 -0
- package/examples/pressure/lps33hw/demo.js +43 -0
- package/examples/pressure/lps33hw/minimal.js +19 -0
- package/examples/rfid/mfrc522/complete.js +44 -0
- package/examples/rfid/mfrc522/demo.js +70 -0
- package/examples/rfid/mfrc522/minimal.js +17 -0
- package/examples/rtc/ds3231/complete.js +77 -0
- package/examples/rtc/ds3231/demo.js +60 -0
- package/examples/rtc/ds3231/minimal.js +23 -0
- package/examples/rtc/pcf8523/complete.js +66 -0
- package/examples/rtc/pcf8523/demo.js +65 -0
- package/examples/rtc/pcf8523/minimal.js +20 -0
- package/examples/temperature/mcp9808/complete.js +89 -0
- package/examples/temperature/mcp9808/demo.js +62 -0
- package/examples/temperature/mcp9808/minimal.js +21 -0
- package/examples/temperature/tmp117/complete.js +90 -0
- package/examples/temperature/tmp117/demo.js +73 -0
- package/examples/temperature/tmp117/minimal.js +21 -0
- package/examples/tof/vl53l0x/complete.js +118 -0
- package/examples/tof/vl53l0x/demo.js +83 -0
- package/examples/tof/vl53l0x/minimal.js +25 -0
- package/examples/tof/vl53l1x/complete.js +151 -0
- package/examples/tof/vl53l1x/demo.js +100 -0
- package/examples/tof/vl53l1x/minimal.js +25 -0
- package/package.json +5 -1
- package/src/chips/accelerometer/adxl345.js +482 -0
- package/src/chips/accelerometer/adxl362.js +594 -0
- package/src/chips/adc_dac/ad7705.js +345 -0
- package/src/chips/adc_dac/ad7706.js +331 -0
- package/src/chips/adc_dac/hx710a.js +184 -0
- package/src/chips/adc_dac/hx710b.js +186 -0
- package/src/chips/adc_dac/hx711.js +15 -15
- package/src/chips/adc_dac/mcp4725.js +48 -40
- package/src/chips/adc_dac/mcp4728.js +57 -44
- package/src/chips/adc_dac/pcf8591.js +196 -0
- package/src/chips/comms/mcp2515.js +659 -0
- package/src/chips/comms/rda5807m.js +393 -0
- package/src/chips/comms/rfm9x.js +590 -0
- package/src/chips/display/pcf8576.js +42 -33
- package/src/chips/environmental/aht21.js +52 -37
- package/src/chips/environmental/bme280.js +85 -66
- package/src/chips/environmental/bme680.js +110 -87
- package/src/chips/gas/ens160.js +82 -64
- package/src/chips/gnss/neo6.js +438 -0
- package/src/chips/gyroscope/l3g4200d.js +366 -0
- package/src/chips/gyroscope/l3gd20h.js +314 -0
- package/src/chips/humidity/dht11.js +135 -0
- package/src/chips/imu/mpu6050.js +89 -66
- package/src/chips/imu/mpu9250.js +394 -0
- package/src/chips/imu/mpu9255.js +447 -0
- package/src/chips/io_expander/mcp23017.js +240 -157
- package/src/chips/io_expander/pcf8574.js +149 -124
- package/src/chips/io_expander/pcf8575.js +164 -135
- package/src/chips/io_expander/tpic6b595.js +242 -0
- package/src/chips/led/_NeoPixelRGBBase.js +204 -0
- package/src/chips/led/_NeoPixelRGBWBase.js +218 -0
- package/src/chips/led/apa102.js +236 -0
- package/src/chips/led/sk6812rgbw.js +15 -149
- package/src/chips/led/ws2812b.js +14 -139
- package/src/chips/led/ws2814.js +49 -0
- package/src/chips/light/apds-9930.js +342 -0
- package/src/chips/light/apds9960.js +158 -123
- package/src/chips/magnetometer/as5600.js +102 -82
- package/src/chips/magnetometer/hmc5883l.js +222 -0
- package/src/chips/memory/_24aa02uid.js +32 -29
- package/src/chips/motor/drv8830.js +239 -0
- package/src/chips/other/mpr121.js +375 -0
- package/src/chips/power/ade7953.js +427 -0
- package/src/chips/power/ina219.js +45 -40
- package/src/chips/power/ina226.js +50 -45
- package/src/chips/power/ina3221.js +95 -76
- package/src/chips/pressure/bmp085.js +251 -0
- package/src/chips/pressure/bmp180.js +54 -37
- package/src/chips/pressure/bmp280.js +72 -53
- package/src/chips/pressure/bmp384.js +410 -0
- package/src/chips/pressure/bmp581.js +489 -0
- package/src/chips/pressure/lps22df.js +341 -0
- package/src/chips/pressure/lps28dfw.js +314 -0
- package/src/chips/pressure/lps33hw.js +255 -0
- package/src/chips/rfid/mfrc522.js +674 -0
- package/src/chips/rtc/ds3231.js +504 -0
- package/src/chips/rtc/pcf8523.js +539 -0
- package/src/chips/temperature/mcp9808.js +488 -0
- package/src/chips/temperature/tmp117.js +471 -0
- package/src/chips/tof/_vl53_base.js +160 -0
- package/src/chips/tof/vl53l0x.js +816 -0
- package/src/chips/tof/vl53l1x.js +816 -0
- package/src/connection/connection.js +111 -0
- package/src/connection/dhtxx.js +138 -0
- package/src/connection/dhtxx_mock.js +39 -0
- package/src/{transport → connection}/hx711.js +49 -22
- package/src/connection/hx711_mock.js +52 -0
- package/src/{transport → connection}/i2c.js +13 -9
- package/src/connection/i2c_mock.js +91 -0
- package/src/connection/input_pin.js +156 -0
- package/src/connection/neo6_mock.js +45 -0
- package/src/{transport → connection}/neopixel.js +23 -4
- package/src/connection/output_pin.js +41 -0
- package/src/connection/sipo.js +159 -0
- package/src/{transport → connection}/smbus.js +14 -10
- package/src/{transport → connection}/spi.js +12 -8
- package/src/connection/spi_mock.js +151 -0
- package/src/{transport → connection}/uart.js +33 -26
- package/examples/gnss/.gitkeep +0 -0
- package/examples/humidity/.gitkeep +0 -0
- package/examples/motor/.gitkeep +0 -0
- package/examples/temperature/.gitkeep +0 -0
- package/examples/tof/.gitkeep +0 -0
- package/src/chips/gnss/.gitkeep +0 -0
- package/src/chips/motor/.gitkeep +0 -0
- package/src/chips/temperature/.gitkeep +0 -0
- package/src/chips/tof/.gitkeep +0 -0
|
@@ -0,0 +1,504 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const _REG_SECONDS = 0x00;
|
|
4
|
+
const _REG_MINUTES = 0x01;
|
|
5
|
+
const _REG_HOURS = 0x02;
|
|
6
|
+
const _REG_DAY = 0x03;
|
|
7
|
+
const _REG_DATE = 0x04;
|
|
8
|
+
const _REG_MONTH_CENTURY = 0x05;
|
|
9
|
+
const _REG_YEAR = 0x06;
|
|
10
|
+
const _REG_ALARM1_SECONDS = 0x07;
|
|
11
|
+
const _REG_ALARM1_MINUTES = 0x08;
|
|
12
|
+
const _REG_ALARM1_HOURS = 0x09;
|
|
13
|
+
const _REG_ALARM1_DAY_DATE = 0x0A;
|
|
14
|
+
const _REG_ALARM2_MINUTES = 0x0B;
|
|
15
|
+
const _REG_ALARM2_HOURS = 0x0C;
|
|
16
|
+
const _REG_ALARM2_DAY_DATE = 0x0D;
|
|
17
|
+
const _REG_CONTROL = 0x0E;
|
|
18
|
+
const _REG_CONTROL_STATUS = 0x0F;
|
|
19
|
+
const _REG_AGING_OFFSET = 0x10;
|
|
20
|
+
const _REG_TEMP_MSB = 0x11;
|
|
21
|
+
const _REG_TEMP_LSB = 0x12;
|
|
22
|
+
|
|
23
|
+
const _CTRL_EOSC = 0x80;
|
|
24
|
+
const _CTRL_BBSQW = 0x40;
|
|
25
|
+
const _CTRL_CONV = 0x20;
|
|
26
|
+
const _CTRL_RS2 = 0x10;
|
|
27
|
+
const _CTRL_RS1 = 0x08;
|
|
28
|
+
const _CTRL_INTCN = 0x04;
|
|
29
|
+
const _CTRL_A2IE = 0x02;
|
|
30
|
+
const _CTRL_A1IE = 0x01;
|
|
31
|
+
|
|
32
|
+
const _STATUS_OSF = 0x80;
|
|
33
|
+
const _STATUS_EN32KHZ = 0x08;
|
|
34
|
+
const _STATUS_BSY = 0x04;
|
|
35
|
+
const _STATUS_A2F = 0x02;
|
|
36
|
+
const _STATUS_A1F = 0x01;
|
|
37
|
+
|
|
38
|
+
const _RATE_CODES = { 1: 0x00, 1024: 0x08, 4096: 0x10, 8192: 0x18 }; // pre-shifted into RS2:RS1 (bits 4:3)
|
|
39
|
+
|
|
40
|
+
function _bcdToInt(b) {
|
|
41
|
+
return ((b >> 4) & 0x0F) * 10 + (b & 0x0F);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function _intToBcd(v) {
|
|
45
|
+
return (Math.floor(v / 10) << 4) | (v % 10);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function _delayMs(ms) {
|
|
49
|
+
const end = Date.now() + ms;
|
|
50
|
+
while (Date.now() < end) { /* spin */ }
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* DS3231 extremely accurate I²C-integrated RTC/TCXO/crystal (Analog
|
|
55
|
+
* Devices / Maxim Integrated) — minimal interface.
|
|
56
|
+
*
|
|
57
|
+
* Reads/writes the calendar clock and the free on-chip temperature reading,
|
|
58
|
+
* with no configuration beyond the connection. Fixed I²C address 0x68.
|
|
59
|
+
*
|
|
60
|
+
* This driver always operates the HOURS registers in 24-hour mode; the
|
|
61
|
+
* chip's native 12-hour/AM-PM encoding is never written or exposed.
|
|
62
|
+
* `weekday` follows ISO 8601 (1=Monday .. 7=Sunday) — the chip itself only
|
|
63
|
+
* requires the DAY register to be a sequential, user-defined 1-7 counter.
|
|
64
|
+
*
|
|
65
|
+
* Time/date register contents are undefined the first time VCC/VBAT is
|
|
66
|
+
* ever applied with no prior battery. Use `oscillatorStopped()` (Full) to
|
|
67
|
+
* detect this rather than trusting `getDatetime()` before the first
|
|
68
|
+
* `setDatetime()` call.
|
|
69
|
+
*/
|
|
70
|
+
class DS3231Minimal {
|
|
71
|
+
/**
|
|
72
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I²C connection at address 0x68.
|
|
73
|
+
*/
|
|
74
|
+
constructor(connection) {
|
|
75
|
+
this._conn = connection;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async _writeReg(reg, value) {
|
|
79
|
+
await this._conn.write(Buffer.from([reg & 0xFF, value & 0xFF]));
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
async _readReg(reg) {
|
|
83
|
+
return (await this._conn.writeRead(Buffer.from([reg & 0xFF]), 1))[0];
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async _readRegs(reg, n) {
|
|
87
|
+
return this._conn.writeRead(Buffer.from([reg & 0xFF]), n);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Confirm the device answers on the bus. The DS3231 has no
|
|
92
|
+
* WHO_AM_I/identity register, so this is a plain presence read; it
|
|
93
|
+
* makes no register writes.
|
|
94
|
+
* @returns {Promise<void>}
|
|
95
|
+
*/
|
|
96
|
+
async init() {
|
|
97
|
+
await this._readReg(_REG_CONTROL);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Read the calendar clock.
|
|
102
|
+
* @returns {Promise<{year: number, month: number, day: number, weekday: number, hour: number, minute: number, second: number}>}
|
|
103
|
+
* `year` is 2000-2099, `hour` is always 0-23, `weekday` is 1-7 (ISO 8601, see class doc).
|
|
104
|
+
*/
|
|
105
|
+
async getDatetime() {
|
|
106
|
+
const raw = await this._readRegs(_REG_SECONDS, 7);
|
|
107
|
+
const second = _bcdToInt(raw[0] & 0x7F);
|
|
108
|
+
const minute = _bcdToInt(raw[1] & 0x7F);
|
|
109
|
+
const hour = _bcdToInt(raw[2] & 0x3F); // bit 6 (12/24) assumed 0; bits 5:0 = 24-hour BCD
|
|
110
|
+
const weekday = raw[3] & 0x07;
|
|
111
|
+
const day = _bcdToInt(raw[4] & 0x3F);
|
|
112
|
+
const month = _bcdToInt(raw[5] & 0x1F);
|
|
113
|
+
const year = 2000 + _bcdToInt(raw[6]);
|
|
114
|
+
return { year, month, day, weekday, hour, minute, second };
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Set the calendar clock. Writes all seven clock/calendar registers,
|
|
119
|
+
* forces 24-hour mode, and clears OSF (the time is now known-good).
|
|
120
|
+
* @param {number} year - 2000-2099.
|
|
121
|
+
* @param {number} month - 1-12.
|
|
122
|
+
* @param {number} day - 1-31.
|
|
123
|
+
* @param {number} weekday - 1-7 (ISO 8601: 1=Monday..7=Sunday).
|
|
124
|
+
* @param {number} hour - 0-23.
|
|
125
|
+
* @param {number} minute - 0-59.
|
|
126
|
+
* @param {number} second - 0-59.
|
|
127
|
+
* @returns {Promise<void>}
|
|
128
|
+
*/
|
|
129
|
+
async setDatetime(year, month, day, weekday, hour, minute, second) {
|
|
130
|
+
const buf = Buffer.from([
|
|
131
|
+
_REG_SECONDS,
|
|
132
|
+
_intToBcd(second),
|
|
133
|
+
_intToBcd(minute),
|
|
134
|
+
_intToBcd(hour), // bit 6 = 0 -> 24-hour mode
|
|
135
|
+
weekday & 0x07,
|
|
136
|
+
_intToBcd(day),
|
|
137
|
+
_intToBcd(month), // bit 7 (century) left at 0
|
|
138
|
+
_intToBcd(year - 2000),
|
|
139
|
+
]);
|
|
140
|
+
await this._conn.write(buf);
|
|
141
|
+
const status = await this._readReg(_REG_CONTROL_STATUS);
|
|
142
|
+
await this._writeReg(_REG_CONTROL_STATUS, status & ~_STATUS_OSF);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Read the last completed temperature conversion. No wait: the chip
|
|
147
|
+
* converts autonomously every 64 s and on power-up, so the value may
|
|
148
|
+
* be up to 64 s stale. Use `forceTemperatureConversion()` (Full) for
|
|
149
|
+
* a fresh reading.
|
|
150
|
+
* @returns {Promise<number>} Temperature in °C.
|
|
151
|
+
*/
|
|
152
|
+
async readTemperature() {
|
|
153
|
+
const raw = await this._readRegs(_REG_TEMP_MSB, 2);
|
|
154
|
+
const msbSigned = raw[0] > 127 ? raw[0] - 256 : raw[0];
|
|
155
|
+
return msbSigned + (raw[1] >> 6) * 0.25;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* DS3231 full interface — extends DS3231Minimal with alarms, square-wave
|
|
161
|
+
* output, 32kHz output, oscillator control, forced temperature conversion,
|
|
162
|
+
* aging trim, and the Level-2 selectable-source interrupt API.
|
|
163
|
+
*
|
|
164
|
+
* `INT/SQW` is one physical pin multiplexed by the CONTROL register's
|
|
165
|
+
* INTCN bit — enabling alarm interrupts (`enableInterrupt`) and enabling
|
|
166
|
+
* the square wave (`enableSquareWave`) are mutually exclusive; whichever
|
|
167
|
+
* call happens last wins. `pollInterrupt()` still reports alarm matches
|
|
168
|
+
* correctly regardless of INTCN, since A1F/A2F latch independently of the
|
|
169
|
+
* pin's mode.
|
|
170
|
+
*/
|
|
171
|
+
class DS3231Full extends DS3231Minimal {
|
|
172
|
+
/**
|
|
173
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I²C connection at address 0x68.
|
|
174
|
+
*/
|
|
175
|
+
constructor(connection) {
|
|
176
|
+
super(connection);
|
|
177
|
+
this._callback = null;
|
|
178
|
+
this._edgeHandler = null;
|
|
179
|
+
this._pollTimer = null;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// -- Alarms ---------------------------------------------------------
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Read Alarm 1's configured match target and granularity.
|
|
186
|
+
* @returns {Promise<{second: number, minute: number, hour: number, dayOrDate: number, isDayOfWeek: boolean, matchMode: number}>}
|
|
187
|
+
*/
|
|
188
|
+
async getAlarm1() {
|
|
189
|
+
const raw = await this._readRegs(_REG_ALARM1_SECONDS, 4);
|
|
190
|
+
const a1m1 = (raw[0] >> 7) & 1;
|
|
191
|
+
const a1m2 = (raw[1] >> 7) & 1;
|
|
192
|
+
const a1m3 = (raw[2] >> 7) & 1;
|
|
193
|
+
const a1m4 = (raw[3] >> 7) & 1;
|
|
194
|
+
const isDayOfWeek = ((raw[3] >> 6) & 1) === 1;
|
|
195
|
+
return {
|
|
196
|
+
second: _bcdToInt(raw[0] & 0x7F),
|
|
197
|
+
minute: _bcdToInt(raw[1] & 0x7F),
|
|
198
|
+
hour: _bcdToInt(raw[2] & 0x3F),
|
|
199
|
+
dayOrDate: _bcdToInt(raw[3] & 0x3F),
|
|
200
|
+
isDayOfWeek,
|
|
201
|
+
matchMode: (a1m4 << 3) | (a1m3 << 2) | (a1m2 << 1) | a1m1,
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Configure and arm Alarm 1's match target and granularity (does not
|
|
207
|
+
* enable the interrupt — call `enableInterrupt(DS3231Full.SOURCE_ALARM1)` too).
|
|
208
|
+
*
|
|
209
|
+
* `matchMode` is one of the `DS3231Full.ALARM1_*` constants, each a
|
|
210
|
+
* direct bit-packing of the A1M4:A1M1 mask nibble (bit 3 = A1M4 .. bit
|
|
211
|
+
* 0 = A1M1) from the datasheet's Alarm Mask Bits table. Every mode
|
|
212
|
+
* except `ALARM1_MATCH_DATE_OR_DAY_HOURS_MINUTES_SECONDS` ignores
|
|
213
|
+
* `dayOrDate`/`isDayOfWeek`; that one mode uses `isDayOfWeek` to pick
|
|
214
|
+
* between a date-of-month match (`false`) and a day-of-week match
|
|
215
|
+
* (`true`), covering both DY/DT rows of the datasheet table with one
|
|
216
|
+
* mask code.
|
|
217
|
+
*
|
|
218
|
+
* @param {number} second
|
|
219
|
+
* @param {number} minute
|
|
220
|
+
* @param {number} hour - 0-23.
|
|
221
|
+
* @param {number} dayOrDate - Day-of-week (1-7) or day-of-month (1-31), per `isDayOfWeek`.
|
|
222
|
+
* @param {boolean} isDayOfWeek
|
|
223
|
+
* @param {number} matchMode - One of `DS3231Full.ALARM1_*`.
|
|
224
|
+
* @returns {Promise<void>}
|
|
225
|
+
*/
|
|
226
|
+
async setAlarm1(second, minute, hour, dayOrDate, isDayOfWeek, matchMode) {
|
|
227
|
+
const a1m1 = matchMode & 0x01;
|
|
228
|
+
const a1m2 = (matchMode >> 1) & 0x01;
|
|
229
|
+
const a1m3 = (matchMode >> 2) & 0x01;
|
|
230
|
+
const a1m4 = (matchMode >> 3) & 0x01;
|
|
231
|
+
const buf = Buffer.from([
|
|
232
|
+
_REG_ALARM1_SECONDS,
|
|
233
|
+
(a1m1 << 7) | _intToBcd(second),
|
|
234
|
+
(a1m2 << 7) | _intToBcd(minute),
|
|
235
|
+
(a1m3 << 7) | _intToBcd(hour),
|
|
236
|
+
(a1m4 << 7) | (isDayOfWeek ? 0x40 : 0x00) | _intToBcd(dayOrDate),
|
|
237
|
+
]);
|
|
238
|
+
await this._conn.write(buf);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Read Alarm 2's configured match target and granularity.
|
|
243
|
+
* @returns {Promise<{minute: number, hour: number, dayOrDate: number, isDayOfWeek: boolean, matchMode: number}>}
|
|
244
|
+
*/
|
|
245
|
+
async getAlarm2() {
|
|
246
|
+
const raw = await this._readRegs(_REG_ALARM2_MINUTES, 3);
|
|
247
|
+
const a2m2 = (raw[0] >> 7) & 1;
|
|
248
|
+
const a2m3 = (raw[1] >> 7) & 1;
|
|
249
|
+
const a2m4 = (raw[2] >> 7) & 1;
|
|
250
|
+
const isDayOfWeek = ((raw[2] >> 6) & 1) === 1;
|
|
251
|
+
return {
|
|
252
|
+
minute: _bcdToInt(raw[0] & 0x7F),
|
|
253
|
+
hour: _bcdToInt(raw[1] & 0x3F),
|
|
254
|
+
dayOrDate: _bcdToInt(raw[2] & 0x3F),
|
|
255
|
+
isDayOfWeek,
|
|
256
|
+
matchMode: (a2m4 << 2) | (a2m3 << 1) | a2m2,
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Configure and arm Alarm 2's match target and granularity (does not
|
|
262
|
+
* enable the interrupt — call `enableInterrupt(DS3231Full.SOURCE_ALARM2)` too).
|
|
263
|
+
*
|
|
264
|
+
* `matchMode` is one of the `DS3231Full.ALARM2_*` constants (A2M4:A2M2
|
|
265
|
+
* mask nibble, bit 2 = A2M4 .. bit 0 = A2M2); see `setAlarm1`'s doc for
|
|
266
|
+
* how `isDayOfWeek` disambiguates the finest-granularity mode.
|
|
267
|
+
*
|
|
268
|
+
* @param {number} minute
|
|
269
|
+
* @param {number} hour - 0-23.
|
|
270
|
+
* @param {number} dayOrDate - Day-of-week (1-7) or day-of-month (1-31), per `isDayOfWeek`.
|
|
271
|
+
* @param {boolean} isDayOfWeek
|
|
272
|
+
* @param {number} matchMode - One of `DS3231Full.ALARM2_*`.
|
|
273
|
+
* @returns {Promise<void>}
|
|
274
|
+
*/
|
|
275
|
+
async setAlarm2(minute, hour, dayOrDate, isDayOfWeek, matchMode) {
|
|
276
|
+
const a2m2 = matchMode & 0x01;
|
|
277
|
+
const a2m3 = (matchMode >> 1) & 0x01;
|
|
278
|
+
const a2m4 = (matchMode >> 2) & 0x01;
|
|
279
|
+
const buf = Buffer.from([
|
|
280
|
+
_REG_ALARM2_MINUTES,
|
|
281
|
+
(a2m2 << 7) | _intToBcd(minute),
|
|
282
|
+
(a2m3 << 7) | _intToBcd(hour),
|
|
283
|
+
(a2m4 << 7) | (isDayOfWeek ? 0x40 : 0x00) | _intToBcd(dayOrDate),
|
|
284
|
+
]);
|
|
285
|
+
await this._conn.write(buf);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// -- Square wave / 32kHz --------------------------------------------
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Drive INT/SQW as a square wave. Mutually exclusive with alarm
|
|
292
|
+
* interrupts (shares INTCN — see class doc).
|
|
293
|
+
* @param {number} [rateHz=8192] - One of 1, 1024, 4096, 8192.
|
|
294
|
+
* @param {boolean} [batteryBacked=false] - Keep the output driven on VBAT (BBSQW).
|
|
295
|
+
* @returns {Promise<void>}
|
|
296
|
+
*/
|
|
297
|
+
async enableSquareWave(rateHz = 8192, batteryBacked = false) {
|
|
298
|
+
const rsBits = _RATE_CODES[rateHz];
|
|
299
|
+
if (rsBits === undefined) {
|
|
300
|
+
throw new Error('rateHz must be one of 1, 1024, 4096, 8192');
|
|
301
|
+
}
|
|
302
|
+
let ctrl = await this._readReg(_REG_CONTROL);
|
|
303
|
+
ctrl &= ~(_CTRL_INTCN | _CTRL_RS2 | _CTRL_RS1 | _CTRL_BBSQW);
|
|
304
|
+
ctrl |= rsBits;
|
|
305
|
+
if (batteryBacked) ctrl |= _CTRL_BBSQW;
|
|
306
|
+
await this._writeReg(_REG_CONTROL, ctrl);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Return INT/SQW to interrupt mode (INTCN=1), stopping the square wave.
|
|
311
|
+
* @returns {Promise<void>}
|
|
312
|
+
*/
|
|
313
|
+
async disableSquareWave() {
|
|
314
|
+
const ctrl = await this._readReg(_REG_CONTROL);
|
|
315
|
+
await this._writeReg(_REG_CONTROL, ctrl | _CTRL_INTCN);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* @returns {Promise<boolean>} Whether the separate 32kHz output pin is enabled.
|
|
320
|
+
*/
|
|
321
|
+
async is32khzEnabled() {
|
|
322
|
+
return ((await this._readReg(_REG_CONTROL_STATUS)) & _STATUS_EN32KHZ) !== 0;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/** @returns {Promise<void>} */
|
|
326
|
+
async enable32khzOutput() {
|
|
327
|
+
const status = await this._readReg(_REG_CONTROL_STATUS);
|
|
328
|
+
await this._writeReg(_REG_CONTROL_STATUS, status | _STATUS_EN32KHZ);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/** @returns {Promise<void>} */
|
|
332
|
+
async disable32khzOutput() {
|
|
333
|
+
const status = await this._readReg(_REG_CONTROL_STATUS);
|
|
334
|
+
await this._writeReg(_REG_CONTROL_STATUS, status & ~_STATUS_EN32KHZ);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
// -- Oscillator -------------------------------------------------------
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* @returns {Promise<boolean>} True if the oscillator has stopped since OSF was last cleared —
|
|
341
|
+
* timekeeping data may be invalid.
|
|
342
|
+
*/
|
|
343
|
+
async oscillatorStopped() {
|
|
344
|
+
return ((await this._readReg(_REG_CONTROL_STATUS)) & _STATUS_OSF) !== 0;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/** Clear OSF only, preserving EN32kHz. @returns {Promise<void>} */
|
|
348
|
+
async clearOscillatorStopped() {
|
|
349
|
+
const status = await this._readReg(_REG_CONTROL_STATUS);
|
|
350
|
+
await this._writeReg(_REG_CONTROL_STATUS, status & ~_STATUS_OSF);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/** Keep the oscillator running on VBAT (power-on default). @returns {Promise<void>} */
|
|
354
|
+
async enableBatteryOscillator() {
|
|
355
|
+
const ctrl = await this._readReg(_REG_CONTROL);
|
|
356
|
+
await this._writeReg(_REG_CONTROL, ctrl & ~_CTRL_EOSC);
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/** Stop the oscillator when switched to VBAT, saving battery current. @returns {Promise<void>} */
|
|
360
|
+
async disableBatteryOscillator() {
|
|
361
|
+
const ctrl = await this._readReg(_REG_CONTROL);
|
|
362
|
+
await this._writeReg(_REG_CONTROL, ctrl | _CTRL_EOSC);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* Force an immediate temperature conversion and wait for it to finish
|
|
367
|
+
* (max 200 ms) rather than returning the last autonomous reading.
|
|
368
|
+
* @returns {Promise<number>} Temperature in °C.
|
|
369
|
+
*/
|
|
370
|
+
async forceTemperatureConversion() {
|
|
371
|
+
const ctrl = await this._readReg(_REG_CONTROL);
|
|
372
|
+
await this._writeReg(_REG_CONTROL, ctrl | _CTRL_CONV);
|
|
373
|
+
const deadline = Date.now() + 200;
|
|
374
|
+
// eslint-disable-next-line no-constant-condition
|
|
375
|
+
while (true) {
|
|
376
|
+
const status = await this._readReg(_REG_CONTROL_STATUS);
|
|
377
|
+
if ((status & _STATUS_BSY) === 0) break;
|
|
378
|
+
if (Date.now() >= deadline) break;
|
|
379
|
+
_delayMs(1);
|
|
380
|
+
}
|
|
381
|
+
return this.readTemperature();
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
// -- Aging offset -----------------------------------------------------
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* @returns {Promise<number>} Raw signed 8-bit oscillator trim code (-128 to 127).
|
|
388
|
+
* Not a physically-scaled unit — see the spec's Implementation Notes.
|
|
389
|
+
*/
|
|
390
|
+
async getAgingOffset() {
|
|
391
|
+
const raw = await this._readReg(_REG_AGING_OFFSET);
|
|
392
|
+
return raw > 127 ? raw - 256 : raw;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
* @param {number} offset - Raw signed 8-bit trim code, -128 to 127.
|
|
397
|
+
* @returns {Promise<void>}
|
|
398
|
+
*/
|
|
399
|
+
async setAgingOffset(offset) {
|
|
400
|
+
await this._writeReg(_REG_AGING_OFFSET, offset & 0xFF);
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
// -- Interrupt API (Level 2: SOURCE_ALARM1 / SOURCE_ALARM2) ----------
|
|
404
|
+
|
|
405
|
+
/**
|
|
406
|
+
* Read CONTROL_STATUS, clear A1F/A2F (leaving OSF/EN32kHz/BSY
|
|
407
|
+
* untouched), and return the pre-clear byte. Mask against
|
|
408
|
+
* `DS3231Full.SOURCE_ALARM1`/`DS3231Full.SOURCE_ALARM2` to test each
|
|
409
|
+
* source. Works regardless of INTCN — A1F/A2F latch on a match
|
|
410
|
+
* whether or not the pin is wired as an interrupt.
|
|
411
|
+
* @returns {Promise<number>}
|
|
412
|
+
*/
|
|
413
|
+
async pollInterrupt() {
|
|
414
|
+
const status = await this._readReg(_REG_CONTROL_STATUS);
|
|
415
|
+
const clearMask = status & (_STATUS_A1F | _STATUS_A2F);
|
|
416
|
+
if (clearMask) {
|
|
417
|
+
await this._writeReg(_REG_CONTROL_STATUS, status & ~clearMask);
|
|
418
|
+
}
|
|
419
|
+
return status & (DS3231Full.SOURCE_ALARM1 | DS3231Full.SOURCE_ALARM2);
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* Enable the given alarm's interrupt-enable bit and set INTCN=1 so
|
|
424
|
+
* INT/SQW carries alarm interrupts instead of the square wave.
|
|
425
|
+
* @param {number} source - `DS3231Full.SOURCE_ALARM1` or `SOURCE_ALARM2`.
|
|
426
|
+
* @returns {Promise<void>}
|
|
427
|
+
*/
|
|
428
|
+
async enableInterrupt(source) {
|
|
429
|
+
let ctrl = await this._readReg(_REG_CONTROL);
|
|
430
|
+
ctrl |= _CTRL_INTCN;
|
|
431
|
+
if (source & DS3231Full.SOURCE_ALARM1) ctrl |= _CTRL_A1IE;
|
|
432
|
+
if (source & DS3231Full.SOURCE_ALARM2) ctrl |= _CTRL_A2IE;
|
|
433
|
+
await this._writeReg(_REG_CONTROL, ctrl);
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/**
|
|
437
|
+
* Clear the given alarm's interrupt-enable bit.
|
|
438
|
+
* @param {number} source - `DS3231Full.SOURCE_ALARM1` or `SOURCE_ALARM2`.
|
|
439
|
+
* @returns {Promise<void>}
|
|
440
|
+
*/
|
|
441
|
+
async disableInterrupt(source) {
|
|
442
|
+
let ctrl = await this._readReg(_REG_CONTROL);
|
|
443
|
+
if (source & DS3231Full.SOURCE_ALARM1) ctrl &= ~_CTRL_A1IE;
|
|
444
|
+
if (source & DS3231Full.SOURCE_ALARM2) ctrl &= ~_CTRL_A2IE;
|
|
445
|
+
await this._writeReg(_REG_CONTROL, ctrl);
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
/**
|
|
449
|
+
* Subscribe to alarm interrupts. Uses `connection.intPin.onEdge()`
|
|
450
|
+
* when an INT-line InputPin is wired; falls back to a 5 ms polling
|
|
451
|
+
* loop otherwise.
|
|
452
|
+
* @param {function(number): void} callback - Called with the status byte (mask against SOURCE_*).
|
|
453
|
+
* @returns {Promise<void>}
|
|
454
|
+
*/
|
|
455
|
+
async onInterrupt(callback) {
|
|
456
|
+
this._callback = callback;
|
|
457
|
+
const dispatch = async () => {
|
|
458
|
+
const status = await this.pollInterrupt();
|
|
459
|
+
if (status && this._callback) this._callback(status);
|
|
460
|
+
};
|
|
461
|
+
if (this._conn.intPin) {
|
|
462
|
+
this._edgeHandler = dispatch;
|
|
463
|
+
await this._conn.intPin.onEdge(this._edgeHandler, 'falling');
|
|
464
|
+
} else {
|
|
465
|
+
this._pollTimer = setInterval(dispatch, 5);
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
/**
|
|
470
|
+
* Unsubscribe from alarm interrupts.
|
|
471
|
+
* @returns {Promise<void>}
|
|
472
|
+
*/
|
|
473
|
+
async offInterrupt() {
|
|
474
|
+
if (this._conn.intPin && this._edgeHandler) {
|
|
475
|
+
await this._conn.intPin.offEdge(this._edgeHandler);
|
|
476
|
+
this._edgeHandler = null;
|
|
477
|
+
}
|
|
478
|
+
if (this._pollTimer) {
|
|
479
|
+
clearInterval(this._pollTimer);
|
|
480
|
+
this._pollTimer = null;
|
|
481
|
+
}
|
|
482
|
+
this._callback = null;
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
/** Alarm 1 fired (bit 0 of CONTROL_STATUS). */
|
|
487
|
+
DS3231Full.SOURCE_ALARM1 = 0x01;
|
|
488
|
+
/** Alarm 2 fired (bit 1 of CONTROL_STATUS). */
|
|
489
|
+
DS3231Full.SOURCE_ALARM2 = 0x02;
|
|
490
|
+
|
|
491
|
+
/** Alarm 1 match-mode constants — see `setAlarm1` doc. */
|
|
492
|
+
DS3231Full.ALARM1_EVERY_SECOND = 0x0F;
|
|
493
|
+
DS3231Full.ALARM1_MATCH_SECONDS = 0x0E;
|
|
494
|
+
DS3231Full.ALARM1_MATCH_MINUTES_SECONDS = 0x0C;
|
|
495
|
+
DS3231Full.ALARM1_MATCH_HOURS_MINUTES_SECONDS = 0x08;
|
|
496
|
+
DS3231Full.ALARM1_MATCH_DATE_OR_DAY_HOURS_MINUTES_SECONDS = 0x00;
|
|
497
|
+
|
|
498
|
+
/** Alarm 2 match-mode constants — see `setAlarm2` doc. */
|
|
499
|
+
DS3231Full.ALARM2_EVERY_MINUTE = 0x07;
|
|
500
|
+
DS3231Full.ALARM2_MATCH_MINUTES = 0x06;
|
|
501
|
+
DS3231Full.ALARM2_MATCH_HOURS_MINUTES = 0x04;
|
|
502
|
+
DS3231Full.ALARM2_MATCH_DATE_OR_DAY_HOURS_MINUTES = 0x00;
|
|
503
|
+
|
|
504
|
+
module.exports = { DS3231Minimal, DS3231Full };
|